kranewm

An ICCCM & EWMH compliant X11 reparenting, dynamic window manager, written in C++
git clone git://git.deurzen.net/kranewm
Log | Files | Refs | LICENSE

config.cc (5628B)


      1 #include "config.hh"
      2 #include "defaults.hh"
      3 
      4 #include <fstream>
      5 #include <iostream>
      6 #include <sstream>
      7 #include <string>
      8 
      9 Config::Config()
     10 {
     11     { // set config directory
     12         std::stringstream configdir_ss;
     13 
     14         if (const char* env_xdgconf = std::getenv("XDG_CONFIG_HOME"))
     15             configdir_ss << env_xdgconf << "/" << WM_NAME << "/";
     16         else
     17             configdir_ss << "$HOME/.config/" << WM_NAME << "/";
     18 
     19         directory = configdir_ss.str();
     20     }
     21 
     22     { // produce vector of to-ignore-{producers,consumers}
     23         std::ifstream in(directory + std::string("consumeignore"));
     24 
     25         if (in.good()) {
     26             std::string line;
     27 
     28             while (std::getline(in, line)) {
     29                 std::string::size_type pos = line.find('#');
     30 
     31                 if (pos != std::string::npos)
     32                     line = line.substr(0, pos);
     33 
     34                 if (line.length() < 5)
     35                     continue;
     36 
     37                 line.erase(4, line.find_first_not_of(" \t\n\r\f\v"));
     38                 line.erase(line.find_last_not_of(" \t\n\r\f\v") + 1);
     39 
     40                 if (line.length() < 5)
     41                     continue;
     42 
     43                 std::vector<SearchSelector_ptr>* ignored;
     44                 switch (line[0]) {
     45                 case '<': ignored = &ignored_producers; break;
     46                 case '>': ignored = &ignored_consumers; break;
     47                 default: continue;
     48                 }
     49 
     50                 SearchSelector::SelectionCriterium criterium;
     51 
     52                 switch (line[2]) {
     53                 case 'N':
     54                 {
     55                     switch (line[1]) {
     56                     case '=': criterium = SearchSelector::SelectionCriterium::ByNameEquals;   break;
     57                     case '~': criterium = SearchSelector::SelectionCriterium::ByNameContains; break;
     58                     default: continue;
     59                     }
     60 
     61                     break;
     62                 }
     63                 case 'C':
     64                 {
     65                     switch (line[1]) {
     66                     case '=': criterium = SearchSelector::SelectionCriterium::ByClassEquals;   break;
     67                     case '~': criterium = SearchSelector::SelectionCriterium::ByClassContains; break;
     68                     default: continue;
     69                     }
     70 
     71                     break;
     72                 }
     73                 case 'I':
     74                 {
     75                     switch (line[1]) {
     76                     case '=': criterium = SearchSelector::SelectionCriterium::ByInstanceEquals;   break;
     77                     case '~': criterium = SearchSelector::SelectionCriterium::ByInstanceContains; break;
     78                     default: continue;
     79                     }
     80 
     81                     break;
     82                 }
     83                 default: continue;
     84                 }
     85 
     86                 ignored->push_back(new SearchSelector{criterium, line.substr(4)});
     87             }
     88         }
     89     }
     90 
     91     { // produce vector of default spawn rules
     92         std::ifstream in(directory + std::string("defaultrules"));
     93 
     94         if (in.good()) {
     95             std::string line;
     96 
     97             while (std::getline(in, line)) {
     98                 std::string::size_type pos = line.find('#');
     99 
    100                 if (pos != std::string::npos)
    101                     line = line.substr(0, pos);
    102 
    103                 if (line.length() < 5)
    104                     continue;
    105 
    106                 line.erase(4, line.find_first_not_of(" \t\n\r\f\v"));
    107                 line.erase(line.find_last_not_of(" \t\n\r\f\v") + 1);
    108 
    109                 if (line.length() < 5)
    110                     continue;
    111 
    112                 pos = line.find(':');
    113                 std::string rules = line.substr(0, pos);
    114 
    115                 if (pos == std::string::npos || rules.empty() || pos >= (line.size() - 1))
    116                     continue;
    117 
    118                 line = line.substr(pos + 1);
    119 
    120                 if (line.length() < 5)
    121                     continue;
    122 
    123                 SearchSelector::SelectionCriterium criterium;
    124 
    125                 switch (line[1]) {
    126                 case 'N':
    127                 {
    128                     switch (line[0]) {
    129                     case '=': criterium = SearchSelector::SelectionCriterium::ByNameEquals;   break;
    130                     case '~': criterium = SearchSelector::SelectionCriterium::ByNameContains; break;
    131                     default: continue;
    132                     }
    133 
    134                     break;
    135                 }
    136                 case 'C':
    137                 {
    138                     switch (line[0]) {
    139                     case '=': criterium = SearchSelector::SelectionCriterium::ByClassEquals;   break;
    140                     case '~': criterium = SearchSelector::SelectionCriterium::ByClassContains; break;
    141                     default: continue;
    142                     }
    143 
    144                     break;
    145                 }
    146                 case 'I':
    147                 {
    148                     switch (line[0]) {
    149                     case '=': criterium = SearchSelector::SelectionCriterium::ByInstanceEquals;   break;
    150                     case '~': criterium = SearchSelector::SelectionCriterium::ByInstanceContains; break;
    151                     default: continue;
    152                     }
    153 
    154                     break;
    155                 }
    156                 default: continue;
    157                 }
    158 
    159                 default_rules.push_back({
    160                     new SearchSelector{criterium, line.substr(3)},
    161                     Rules::parse_rules(rules, true)
    162                 });
    163             }
    164         }
    165     }
    166 }
    167 
    168 Config::~Config()
    169 {
    170     for (std::size_t i = 0; i < ignored_producers.size(); ++i)
    171         delete ignored_producers[i];
    172 
    173     for (std::size_t i = 0; i < ignored_consumers.size(); ++i)
    174         delete ignored_consumers[i];
    175 }