wmi

git clone git://oldgit.suckless.org/wmi/
Log | Files | Refs | LICENSE

shortcut.cpp (2281B)


      1 // Copyright (c) 2003 - 2009 Anselm R Garbe <anselm@garbe.us>
      2 // See LICENSE for license details.
      3 
      4 #include "shortcut.h"
      5 
      6 #include "logger.h"
      7 #include "util.h"
      8 #include "xcore.h"
      9 
     10 Shortcut::Shortcut(unsigned long modMask, KeyCode keyCode,
     11                    Shortcut *next, unsigned int button)
     12 {
     13     modMask_ = modMask;
     14     button_ = button;
     15     keyCode_ = keyCode;
     16     next_ = next;
     17 }
     18 
     19 Shortcut::~Shortcut() {
     20     if (next_) {
     21         delete next_;
     22     }
     23 }
     24 
     25 
     26 Shortcut *Shortcut::shortcut(string keys) {
     27 
     28     if (keys == "") {
     29         return 0;
     30     }
     31 
     32     LOGDEBUG("entered shortcut construction");
     33     // shortcut handling
     34     Shortcut *root = 0;
     35     Shortcut *tmp = 0;
     36     keys = Util::truncate(keys, ' ');
     37     unsigned int length = keys.length();
     38     bool sep;
     39     string subkeys = "";
     40     for (int i = 0; i < (int)length; i++) {
     41         sep = keys[i] == ':';
     42         if (!sep) {
     43             subkeys += keys[i];
     44         }
     45         else {
     46             i++; // ignore next ':'
     47         }
     48 
     49         if (sep || (i == (int)(length - 1))) {
     50             LOGDEBUG("next shortcut: " + subkeys);
     51             // if no modifier is given, none is assumed
     52             if (subkeys.find('+') == string::npos) {
     53                 subkeys = "none+" + subkeys;
     54             }
     55             string key = Util::lastToken(subkeys, '+');
     56             // check if the key sequence contains a Button identifier
     57             unsigned int button = Util::buttonForString(key);
     58             Shortcut *shortcut = new Shortcut(
     59                 Util::modMaskForString(subkeys),
     60                 XCORE->stringToKeyCode(key), 0, button);
     61             if (!root) {
     62                 root = shortcut;
     63             }
     64             else {
     65                 tmp->setNext(shortcut);
     66             }
     67             tmp = shortcut;
     68             subkeys = "";
     69         }
     70     }
     71     return root;
     72 }
     73 
     74 
     75 KeyCode Shortcut::keyCode() {
     76     return keyCode_;
     77 }
     78 
     79 unsigned long Shortcut::modMask() const {
     80     return modMask_;
     81 }
     82 
     83 unsigned int Shortcut::button() const {
     84     return button_;
     85 }
     86 
     87 Shortcut *Shortcut::next() const {
     88     return next_;
     89 }
     90 
     91 void Shortcut::setNext(Shortcut *next) {
     92     next_ = next;
     93 }
     94 
     95 void Shortcut::setKeyCode(KeyCode keyCode) {
     96     keyCode_ = keyCode;
     97 }
     98 
     99 void Shortcut::setModMask(unsigned long modMask) {
    100     modMask_ = modMask;
    101 }