diff options
Diffstat (limited to 'src/Keys.cc')
-rw-r--r-- | src/Keys.cc | 157 |
1 files changed, 10 insertions, 147 deletions
diff --git a/src/Keys.cc b/src/Keys.cc index 1667f43..fb677aa 100644 --- a/src/Keys.cc +++ b/src/Keys.cc | |||
@@ -19,7 +19,7 @@ | |||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | 19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. | 20 | // DEALINGS IN THE SOFTWARE. |
21 | 21 | ||
22 | //$Id: Keys.cc,v 1.37 2003/09/06 13:58:06 fluxgen Exp $ | 22 | //$Id: Keys.cc,v 1.38 2003/10/05 07:19:30 rathnor Exp $ |
23 | 23 | ||
24 | 24 | ||
25 | #include "Keys.hh" | 25 | #include "Keys.hh" |
@@ -74,20 +74,16 @@ | |||
74 | using namespace std; | 74 | using namespace std; |
75 | 75 | ||
76 | Keys::Keys(const char *filename): | 76 | Keys::Keys(const char *filename): |
77 | m_display(FbTk::App::instance()->display()), | 77 | m_display(FbTk::App::instance()->display()) |
78 | m_modmap(0) { | 78 | { |
79 | |||
80 | loadModmap(); | ||
81 | 79 | ||
82 | if (filename != 0) | 80 | if (filename != 0) |
83 | load(filename); | 81 | load(filename); |
84 | } | 82 | } |
85 | 83 | ||
86 | Keys::~Keys() { | 84 | Keys::~Keys() { |
87 | if (m_modmap) | ||
88 | XFreeModifiermap(m_modmap); | ||
89 | 85 | ||
90 | ungrabKeys(); | 86 | FbTk::KeyUtil::ungrabKeys(); |
91 | deleteTree(); | 87 | deleteTree(); |
92 | } | 88 | } |
93 | 89 | ||
@@ -100,14 +96,6 @@ void Keys::deleteTree() { | |||
100 | } | 96 | } |
101 | } | 97 | } |
102 | 98 | ||
103 | /// Ungrabs the keys | ||
104 | void Keys::ungrabKeys() { | ||
105 | for (int screen=0; screen<ScreenCount(m_display); screen++) { | ||
106 | XUngrabKey(m_display, AnyKey, AnyModifier, | ||
107 | RootWindow(m_display, screen)); | ||
108 | } | ||
109 | } | ||
110 | |||
111 | /** | 99 | /** |
112 | Load and grab keys | 100 | Load and grab keys |
113 | TODO: error checking | 101 | TODO: error checking |
@@ -118,7 +106,7 @@ bool Keys::load(const char *filename) { | |||
118 | return false; | 106 | return false; |
119 | 107 | ||
120 | //ungrab all keys | 108 | //ungrab all keys |
121 | ungrabKeys(); | 109 | FbTk::KeyUtil::ungrabKeys(); |
122 | 110 | ||
123 | //free memory of previous grabs | 111 | //free memory of previous grabs |
124 | deleteTree(); | 112 | deleteTree(); |
@@ -158,15 +146,15 @@ bool Keys::load(const char *filename) { | |||
158 | if (val[argc][0] != ':') { // parse key(s) | 146 | if (val[argc][0] != ':') { // parse key(s) |
159 | keyarg++; | 147 | keyarg++; |
160 | if (keyarg==1) //first arg is modifier | 148 | if (keyarg==1) //first arg is modifier |
161 | mod = getModifier(val[argc].c_str()); | 149 | mod = FbTk::KeyUtil::getModifier(val[argc].c_str()); |
162 | else if (keyarg>1) { | 150 | else if (keyarg>1) { |
163 | 151 | ||
164 | //keyarg=0; | 152 | //keyarg=0; |
165 | int tmpmod = getModifier(val[argc].c_str()); | 153 | int tmpmod = FbTk::KeyUtil::getModifier(val[argc].c_str()); |
166 | if(tmpmod) | 154 | if(tmpmod) |
167 | mod|=tmpmod; //If it's a modifier | 155 | mod|=tmpmod; //If it's a modifier |
168 | else { | 156 | else { |
169 | key = getKey(val[argc].c_str()); // else get the key | 157 | key = FbTk::KeyUtil::getKey(val[argc].c_str()); // else get the key |
170 | if (key == 0) { | 158 | if (key == 0) { |
171 | cerr<<"["<<filename<<"]: Invalid key/modifier on line("<< | 159 | cerr<<"["<<filename<<"]: Invalid key/modifier on line("<< |
172 | line<<"): "<<linebuffer<<endl; | 160 | line<<"): "<<linebuffer<<endl; |
@@ -221,114 +209,6 @@ bool Keys::load(const char *filename) { | |||
221 | return true; | 209 | return true; |
222 | } | 210 | } |
223 | 211 | ||
224 | void Keys::loadModmap() { | ||
225 | if (m_modmap) | ||
226 | XFreeModifiermap(m_modmap); | ||
227 | |||
228 | m_modmap = XGetModifierMapping(m_display); | ||
229 | // force reinit of modifiers | ||
230 | FbTk::KeyUtil::init(); | ||
231 | } | ||
232 | |||
233 | /** | ||
234 | Grabs a key with the modifier | ||
235 | and with numlock,capslock and scrollock | ||
236 | */ | ||
237 | void Keys::grabKey(unsigned int key, unsigned int mod) { | ||
238 | const int capsmod = FbTk::KeyUtil::capslockMod(); | ||
239 | const int nummod = FbTk::KeyUtil::numlockMod(); | ||
240 | const int scrollmod = FbTk::KeyUtil::scrolllockMod(); | ||
241 | |||
242 | for (int screen=0; screen<ScreenCount(m_display); screen++) { | ||
243 | |||
244 | Window root = RootWindow(m_display, screen); | ||
245 | |||
246 | XGrabKey(m_display, key, mod, | ||
247 | root, True, | ||
248 | GrabModeAsync, GrabModeAsync); | ||
249 | |||
250 | // Grab with numlock, capslock and scrlock | ||
251 | |||
252 | //numlock | ||
253 | XGrabKey(m_display, key, mod|nummod, | ||
254 | root, True, | ||
255 | GrabModeAsync, GrabModeAsync); | ||
256 | //scrolllock | ||
257 | XGrabKey(m_display, key, mod|scrollmod, | ||
258 | root, True, | ||
259 | GrabModeAsync, GrabModeAsync); | ||
260 | //capslock | ||
261 | XGrabKey(m_display, key, mod|capsmod, | ||
262 | root, True, | ||
263 | GrabModeAsync, GrabModeAsync); | ||
264 | |||
265 | //capslock+numlock | ||
266 | XGrabKey(m_display, key, mod|capsmod|nummod, | ||
267 | root, True, | ||
268 | GrabModeAsync, GrabModeAsync); | ||
269 | |||
270 | //capslock+scrolllock | ||
271 | XGrabKey(m_display, key, mod|capsmod|scrollmod, | ||
272 | root, True, | ||
273 | GrabModeAsync, GrabModeAsync); | ||
274 | |||
275 | //capslock+numlock+scrolllock | ||
276 | XGrabKey(m_display, key, mod|capsmod|scrollmod|nummod, | ||
277 | root, True, | ||
278 | GrabModeAsync, GrabModeAsync); | ||
279 | |||
280 | //numlock+scrollLock | ||
281 | XGrabKey(m_display, key, mod|nummod|scrollmod, | ||
282 | root, True, | ||
283 | GrabModeAsync, GrabModeAsync); | ||
284 | |||
285 | } | ||
286 | |||
287 | } | ||
288 | |||
289 | /** | ||
290 | @return the modifier for the modstr else zero on failure. | ||
291 | TODO fix more masks | ||
292 | */ | ||
293 | unsigned int Keys::getModifier(const char *modstr) { | ||
294 | if (!modstr) | ||
295 | return 0; | ||
296 | struct t_modlist{ | ||
297 | char *str; | ||
298 | unsigned int mask; | ||
299 | bool operator == (const char *modstr) { | ||
300 | return (strcasecmp(str, modstr) == 0 && mask !=0); | ||
301 | } | ||
302 | } modlist[] = { | ||
303 | {"SHIFT", ShiftMask}, | ||
304 | {"CONTROL", ControlMask}, | ||
305 | {"MOD1", Mod1Mask}, | ||
306 | {"MOD2", Mod2Mask}, | ||
307 | {"MOD3", Mod3Mask}, | ||
308 | {"MOD4", Mod4Mask}, | ||
309 | {"MOD5", Mod5Mask}, | ||
310 | {0, 0} | ||
311 | }; | ||
312 | |||
313 | // find mod mask string | ||
314 | for (unsigned int i=0; modlist[i].str !=0; i++) { | ||
315 | if (modlist[i] == modstr) | ||
316 | return modlist[i].mask; | ||
317 | } | ||
318 | |||
319 | return 0; | ||
320 | } | ||
321 | |||
322 | /** | ||
323 | @return keycode of keystr on success else 0 | ||
324 | */ | ||
325 | unsigned int Keys::getKey(const char *keystr) { | ||
326 | if (!keystr) | ||
327 | return 0; | ||
328 | return XKeysymToKeycode(m_display, | ||
329 | XStringToKeysym(keystr)); | ||
330 | } | ||
331 | |||
332 | /** | 212 | /** |
333 | @return the KeyAction of the XKeyEvent | 213 | @return the KeyAction of the XKeyEvent |
334 | */ | 214 | */ |
@@ -399,7 +279,7 @@ bool Keys::mergeTree(t_key *newtree, t_key *basetree) { | |||
399 | } | 279 | } |
400 | 280 | ||
401 | if (baselist_i == m_keylist.size()) { | 281 | if (baselist_i == m_keylist.size()) { |
402 | grabKey(newtree->key, newtree->mod); | 282 | FbTk::KeyUtil::grabKey(newtree->key, newtree->mod); |
403 | m_keylist.push_back(new t_key(newtree)); | 283 | m_keylist.push_back(new t_key(newtree)); |
404 | if (newtree->keylist.size()) | 284 | if (newtree->keylist.size()) |
405 | return mergeTree(newtree->keylist[0], m_keylist.back()); | 285 | return mergeTree(newtree->keylist[0], m_keylist.back()); |
@@ -420,7 +300,7 @@ bool Keys::mergeTree(t_key *newtree, t_key *basetree) { | |||
420 | } | 300 | } |
421 | //if it wasn't in the list grab the key and add it to the list | 301 | //if it wasn't in the list grab the key and add it to the list |
422 | if (baselist_i==basetree->keylist.size()) { | 302 | if (baselist_i==basetree->keylist.size()) { |
423 | grabKey(newtree->key, newtree->mod); | 303 | FbTk::KeyUtil::grabKey(newtree->key, newtree->mod); |
424 | basetree->keylist.push_back(new t_key(newtree)); | 304 | basetree->keylist.push_back(new t_key(newtree)); |
425 | if (newtree->keylist.size()) | 305 | if (newtree->keylist.size()) |
426 | return mergeTree(newtree->keylist[0], basetree->keylist.back()); | 306 | return mergeTree(newtree->keylist[0], basetree->keylist.back()); |
@@ -453,20 +333,3 @@ Keys::t_key::~t_key() { | |||
453 | } | 333 | } |
454 | 334 | ||
455 | } | 335 | } |
456 | |||
457 | unsigned int Keys::keycodeToModmask(unsigned int keycode) { | ||
458 | if (!m_modmap) return 0; | ||
459 | |||
460 | // search through modmap for this keycode | ||
461 | for (int mod=0; mod < 8; mod++) { | ||
462 | for (int key=0; key < m_modmap->max_keypermod; ++key) { | ||
463 | // modifiermap is an array with 8 sets of keycodes | ||
464 | // each max_keypermod long, but in a linear array. | ||
465 | if (m_modmap->modifiermap[m_modmap->max_keypermod*mod + key] == keycode) { | ||
466 | return (1<<mod); | ||
467 | } | ||
468 | } | ||
469 | } | ||
470 | // no luck | ||
471 | return 0; | ||
472 | } | ||