wmi

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

binder.h (6975B)


      1 // Copyright (c) 2003 - 2009 Anselm R Garbe <anselm@garbe.us>
      2 // Copyright (c) 2003 - 2004 Marcel Manthe <schneegloeckchen at gmx.li>
      3 // See LICENSE for license details.
      4 
      5 #ifndef __BINDER_H
      6 #define __BINDER_H
      7 
      8 #include "singleton.h"
      9 
     10 extern "C" {
     11 #include <X11/Xlib.h>
     12 }
     13 
     14 #include <list>
     15 #include <map>
     16 #include <set>
     17 
     18 #ifdef POSIX_REGEX
     19 #include <stack>
     20 #include "logger.h"
     21 #include <regex.h>
     22 #endif
     23 
     24 #include <string>
     25 #include "wmi.h"
     26 
     27 // forward declaration
     28 class Action;
     29 class Binder;
     30 class Group;
     31 class Kernel;
     32 class Shortcut;
     33 
     34 typedef set<string> Sstring;
     35 typedef map<string, Action *, less<string> > MBindings;
     36 typedef list<Action *> LAction;
     37 
     38 /**
     39  * Main class of all key- and button-grabbing related logic.
     40  */
     41 class Binder : public Singleton<Binder>
     42 {
     43 
     44 public:
     45 
     46     /** Constructs a new grab manager instance.  */
     47     Binder();
     48 
     49     ~Binder();
     50 
     51     /** Key initialization for a specific window. */
     52     void initKeys(Window window);
     53 
     54     /** Key event handler, window is for grabbing. */
     55     void handleKey(Window window, XKeyEvent *e);
     56 
     57     /** Button event handler, window is for grabbing. */
     58     void handleButton(Window window, XButtonEvent *e);
     59 
     60     /** Grabs the shortcut. */
     61     void grabShortcut(Shortcut *shortcut, Window window);
     62 
     63     /** Ungrabs shortcut. */
     64     void ungrabShortcut(Shortcut *shortcut, Window window);
     65 
     66     /** Grabs all buttons for the modMask of the specific window. */
     67     void grabButtons(Window window, unsigned long modMask);
     68 
     69     /** Ungrabs all buttons for the specific window. */
     70     void ungrabButtons(Window window);
     71 
     72     /** Returns pretty print of current key configuration. */
     73     string prettyPrintKeyBindings();
     74 
     75     /**
     76      * Queries all actions for the given pattern.
     77      * Returns absolute equal pattern of <i>all</i> entries.
     78      * @param pattern contains the pattern.
     79      * @param set where to put matching action keys.
     80      */
     81     string queryActionKeysForPattern(string pattern, Sstring *actionKeys);
     82 
     83     /** Similiar to previous, except actions aren't validated. */
     84     string queryActionKeysWithoutValidationForPattern(string pattern,
     85                                                       Sstring *actionKeys);
     86 
     87 
     88     /** Similiar to previous, except that internal actions are ignored. */
     89     string queryExternChainActionsForPattern(
     90                             string pattern, Sstring *actionKeys);
     91 
     92     /**
     93      * Queries all executables for the given pattern.
     94      * Returns absolute equal pattern of <i>all</i> executables.
     95      * @param pattern contains the pattern.
     96      * @param set where to put matching executables.
     97      */
     98     string queryCommandForPattern(string pattern, Sstring *expands);
     99 
    100     /**
    101      * Queries all workspace names of the focused monitor.
    102      * Returns absolute equal pattern of <i>all</i> workspaces.
    103      * @param pattern contains the pattern.
    104      * @param set where to put matching names.
    105      */
    106     string queryWorkspacesForPattern(string pattern, Sstring *workspaces);
    107 
    108     /**
    109      * Queries all monitor names.
    110      * Returns absolute equal pattern of <i>all</i> monitors.
    111      * @param pattern contains the pattern.
    112      * @param set where to put matching names.
    113      */
    114     string queryMonitorsForPattern(string pattern, Sstring *monitors);
    115 
    116     /**
    117      * Queries all client names of the focused monitor.
    118      * Returns absolute equal pattern of <i>all</i> clients.
    119      * @param pattern contains the pattern.
    120      * @param set where to put matching names.
    121      */
    122     string queryClientsForPattern(string pattern, Sstring *clients);
    123 
    124     /**
    125      * Queries all detached client names of the focused monitor.
    126      * Returns absolute equal pattern of <i>all</i> clients.
    127      * @param pattern contains the pattern.
    128      * @param set where to put matching names.
    129      */
    130     string queryDetachedClientsForPattern(string pattern, Sstring *clients);
    131 
    132     /** Same as above, except instead of client names, client ids. */
    133     string queryClientIdsForPattern(string pattern, Sstring *clients);
    134 
    135     /**
    136      * Queries all frame names of the focused workspace.
    137      * Returns absolute equal pattern of <i>all</i> frame ids.
    138      * @param pattern contains the pattern.
    139      * @param set where to put matching names.
    140      */
    141     string queryFramesForPattern(string pattern, Sstring *sFrames);
    142 
    143     unsigned int validModMask() const;
    144 
    145 #ifdef POSIX_REGEX
    146     string popRegexPattern(){
    147         string result;
    148         if (!regexPattern_.empty()) regexPattern_.pop();
    149         if (regexPattern_.empty()){
    150             result = "";
    151         }else{
    152             result = regexPattern_.top();
    153         }
    154 
    155         return result;
    156     }
    157         
    158     void pushRegexPattern(string pattern){
    159        regexPattern_.push(pattern);
    160 
    161        stack<string> temp = regexPattern_;
    162        string res;
    163        while (!temp.empty()){
    164            res= res + temp.top() + " ---  " ;
    165            temp.pop();
    166        }
    167        LOGDEBUG("STACK: " + res);      
    168    
    169     }
    170     void clearRegexPattern(){
    171         while (!regexPattern_.empty()) regexPattern_.pop();
    172     } 
    173 #endif
    174 
    175 private:
    176 
    177     /** Helper handler for handleKey and handleButton action. */
    178     void handleShortcut(Window window, unsigned long modMask,
    179                         KeyCode keyCode, unsigned int button);
    180 
    181     /** Own key press processing shortcut handler (for postfixed
    182       shortcuts). */
    183     void handleShortcutChains(Window window, Shortcut *prefix,
    184                               LAction *grabbedActions);
    185     /** Helper for handleShortcutChains. Keyboard should be grabbed. */
    186     void nextKeystroke(unsigned long *modMask, KeyCode *keyCode);
    187 
    188     /** Emulates key press for client. */
    189     void emulateKeyPress(unsigned long modMask, KeyCode keyCode);
    190 
    191     /** Inits lock modifier detection (NumLock and ScrollLock). */
    192     void initLockModifiers();
    193 
    194     /**
    195      * Returns absolute pattern of the given set, at least pattern.
    196      * @param offset begin of comparisions within set.
    197      */
    198     string absolutePattern(string pattern, Sstring *names, unsigned int offset);
    199 
    200     /**
    201      * Matches the pattern.
    202      * @param digest the string to check match.
    203      * @param pattern the pattern to match.
    204      * @param strings the container set to insert matched strings.
    205      * @param patternLength the pattern length.
    206      */
    207     void matchPattern(string digest, string pattern, Sstring *strings,
    208                       unsigned int patternLength);
    209 
    210     void initRegex(string);
    211 
    212 #ifdef POSIX_REGEX
    213     bool regex_first_match(const string& pattern, const string& haystack,
    214                            int& begin, int& end);
    215     bool regex_match(const string& pattern, const string& haystack);
    216     void regex_prepare(){regcomp(&temp_regex_, "", REG_EXTENDED); temp_pattern_="";}
    217     string temp_pattern_;
    218     regex_t temp_regex_;
    219     bool doRegex_;
    220     stack<string> regexPattern_;
    221 #endif
    222 
    223 
    224     MBindings *actionBindings_;
    225 
    226     unsigned int numLockMask_;
    227     unsigned int scrollLockMask_;
    228     unsigned int validModMask_;
    229 };
    230 
    231 #endif // __BINDER_H