1 // Keys.hh for Fluxbox - an X11 Window manager
2 // Copyright (c) 2001 - 2003 Henrik Kinnunen (fluxgen at fluxbox dot org)
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21 
22 #ifndef KEYS_HH
23 #define KEYS_HH
24 
25 #include "FbTk/NotCopyable.hh"
26 #include "FbTk/RefCount.hh"
27 
28 #include <X11/Xlib.h>
29 #include <string>
30 #include <map>
31 
32 class WinClient;
33 
34 namespace FbTk {
35     class EventHandler;
36     class AutoReloadHelper;
37 }
38 
39 class Keys:private FbTk::NotCopyable  {
40 public:
41 
42     // contexts for events
43     // it's ok if there is overlap; it will be worked out in t_key::find()
44     // eventHandlers should submit bitwise-or of contexts the event happened in
45     enum {
46         GLOBAL =            1 << 0,
47         ON_DESKTOP =        1 << 1,
48         ON_TOOLBAR =        1 << 2,
49         ON_ICONBUTTON =     1 << 3,
50         ON_TITLEBAR =       1 << 4,
51         ON_WINDOW =         1 << 5,
52         ON_WINDOWBORDER =   1 << 6,
53         ON_LEFTGRIP =       1 << 7,
54         ON_RIGHTGRIP =      1 << 8,
55         ON_TAB =            1 << 9,
56         ON_SLIT =           1 << 10
57         // and so on...
58     };
59 
60     /// constructor
61     explicit Keys();
62     /// destructor
63     ~Keys();
64 
65     /// bind a key action from a string
66     /// @return false on failure
67     bool addBinding(const std::string &binding);
68 
69     /**
70        do action from XKeyEvent; return false if not bound to anything
71     */
72     bool doAction(int type, unsigned int mods, unsigned int key, int context,
73                   WinClient *current = 0, Time time = 0);
74 
75     /// register a window so that proper keys/buttons get grabbed on it
76     void registerWindow(Window win, FbTk::EventHandler &handler, int context);
77     /// unregister window
78     void unregisterWindow(Window win);
79 
80     /// grab keys again when keymap changes
81     void regrab();
82 
filename() const83     const std::string& filename() const { return m_filename; }
84     /**
85        Load configuration from file
86     */
87     void reload();
88     /**
89        Reload configuration if keys file has changed
90     */
91     void reconfigure();
92     void keyMode(const std::string& keyMode);
93 
inKeychain() const94     bool inKeychain() const { return saved_keymode != 0; }
95 
96 private:
97     class t_key; // helper class to build a 'keytree'
98     typedef FbTk::RefCount<t_key> RefKey;
99     typedef std::map<std::string, RefKey> keyspace_t;
100     typedef std::map<Window, int> WindowMap;
101     typedef std::map<Window, FbTk::EventHandler*> HandlerMap;
102 
103     void deleteTree();
104 
105     void grabKey(unsigned int key, unsigned int mod);
106     void ungrabKeys();
107     void grabButton(unsigned int button, unsigned int mod, int context);
108     void ungrabButtons();
109     void grabWindow(Window win);
110 
111     // Load default keybindings for when there are errors loading the keys file
112     void loadDefaults();
113     void setKeyMode(const FbTk::RefCount<t_key> &keyMode);
114 
115 
116     // member variables
117     std::string m_filename;
118     FbTk::AutoReloadHelper* m_reloader;
119     RefKey m_keylist;
120     keyspace_t m_map;
121 
122     RefKey next_key;
123     RefKey saved_keymode;
124 
125     WindowMap m_window_map;
126     HandlerMap m_handler_map;
127 };
128 
129 #endif // KEYS_HH
130