1 // KeyUtil.hh for FbTk
2 // Copyright (c) 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 // $Id: KeyUtil.hh 3991 2005-05-06 09:22:53Z mathias $
23 
24 #ifndef FBTK_KEYUTIL_HH
25 #define FBTK_KEYUTIL_HH
26 
27 #include <X11/Xlib.h>
28 #include <X11/keysym.h>
29 
30 #include <memory>
31 
32 namespace FbTk {
33 
34 class KeyUtil {
35 public:
36 
37     KeyUtil();
38     ~KeyUtil();
39 
40     void init();
41     static KeyUtil &instance();
42 
43     /**
44        Grab the specified key
45     */
46     static void grabKey(unsigned int key, unsigned int mod);
47 
48     /**
49        convert the string to the keysym
50        @return the keysym corresponding to the string, or zero
51     */
52     static unsigned int getKey(const char *keystr);
53 
54     /**
55        @return the modifier for the modstr else zero on failure.
56     */
57     static unsigned int getModifier(const char *modstr);
58 
59     /**
60        ungrabs all keys
61      */
62     static void ungrabKeys();
63 
64     /**
65         Strip out modifiers we want to ignore
66         @return the cleaned state number
67     */
cleanMods(unsigned int mods)68     unsigned int cleanMods(unsigned int mods) {
69         //remove numlock, capslock and scrolllock
70          return mods & ~(capslock() | numlock() );
71     }
72 
73     /**
74        strip away everything which is actually not a modifier
75        eg, xkb-keyboardgroups are encoded as bit 13 and 14
76     */
isolateModifierMask(unsigned int mods)77     unsigned int isolateModifierMask(unsigned int mods) {
78         return mods & (ShiftMask|LockMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask);
79     }
80 
81     /**
82        Convert the specified key into appropriate modifier mask
83        @return corresponding modifier mask
84     */
85     static unsigned int keycodeToModmask(unsigned int keycode);
numlock() const86     int numlock() const { return Mod2Mask; } //m_numlock; }
capslock() const87     int capslock() const { return LockMask; } //m_capslock; }
scrolllock() const88     int scrolllock() const { return Mod5Mask; } //m_scrolllock; }
89 
90 private:
91     void loadModmap();
92 
93     XModifierKeymap *m_modmap;
94     int m_capslock, m_numlock, m_scrolllock;
95     static std::auto_ptr<KeyUtil> s_keyutil;
96 };
97 
98 } // end namespace FbTk
99 
100 
101 #endif // FBTK_KEYUTIL_HH
102