From 6984eb8970b61c0a6bcacba301707807d47d9b42 Mon Sep 17 00:00:00 2001 From: rathnor Date: Sun, 5 Oct 2003 07:20:47 +0000 Subject: shuffle bunch of stuff from Keys into FbTk/KeyUtil --- ChangeLog | 4 ++ src/FbTk/KeyUtil.cc | 200 ++++++++++++++++++++++++++++++++++++++++------------ src/FbTk/KeyUtil.hh | 56 +++++++++++---- src/FbTk/TextBox.cc | 5 +- src/Keys.cc | 157 +++-------------------------------------- src/Keys.hh | 21 +----- src/fluxbox.cc | 11 +-- 7 files changed, 225 insertions(+), 229 deletions(-) diff --git a/ChangeLog b/ChangeLog index ec0c288..8af4f33 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,10 @@ (Format: Year/Month/Day) Changes for 0.9.6: *03/10/05: + * Move a bunch of functionality from Keys into FbTk::KeyUtil (Simon) + - also fix issue where Capslock mod was taken to be whatever caps + key was mapped to (why??). Now uses LockMask (ditto num,scroll). + Keys.hh/cc KeyUtil.hh/cc TextBox.cc fluxbox.cc * Fix reading of auto raise delay (Simon) fluxbox.hh/cc Screen.cc Timer.hh/cc * Make focusLast work for sloppy focus when changing workspace or diff --git a/src/FbTk/KeyUtil.cc b/src/FbTk/KeyUtil.cc index 54d7c35..71dc22a 100644 --- a/src/FbTk/KeyUtil.cc +++ b/src/FbTk/KeyUtil.cc @@ -19,63 +19,175 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -// $Id: KeyUtil.cc,v 1.2 2003/09/08 19:18:22 fluxgen Exp $ +// $Id: KeyUtil.cc,v 1.3 2003/10/05 07:20:16 rathnor Exp $ #include "KeyUtil.hh" #include "App.hh" -#include +#include namespace FbTk { -int KeyUtil::s_capslock_mod = 0; -int KeyUtil::s_numlock_mod = 0; -int KeyUtil::s_scrolllock_mod = 0; -bool KeyUtil::s_init = false; +KeyUtil *KeyUtil::s_keyutil = 0; + +KeyUtil *KeyUtil::instance() { + if (s_keyutil == 0) + s_keyutil = new KeyUtil(); + return s_keyutil; +} + + +KeyUtil::KeyUtil() + : m_modmap(0) +{ + init(); +} void KeyUtil::init() { - Display *disp = FbTk::App::instance()->display(); - - XModifierKeymap *modmap = XGetModifierMapping(disp); - - // mask to use for modifier - int mods[] = { - ShiftMask, - LockMask, - ControlMask, - Mod1Mask, - Mod2Mask, - Mod3Mask, - Mod4Mask, - Mod5Mask, - 0 - }; + loadModmap(); +} + +KeyUtil::~KeyUtil() { + if (m_modmap) + XFreeModifiermap(m_modmap); +} + +void KeyUtil::loadModmap() { + if (m_modmap) + XFreeModifiermap(m_modmap); + + m_modmap = XGetModifierMapping(App::instance()->display()); +} + + + +/** + Grabs a key with the modifier + and with numlock,capslock and scrollock +*/ +void KeyUtil::grabKey(unsigned int key, unsigned int mod) { + Display *display = App::instance()->display(); + const unsigned int capsmod = LockMask; + const unsigned int nummod = Mod2Mask; + const unsigned int scrollmod = Mod5Mask; + + for (int screen=0; screenmax_keypermod; ++key, ++realkey) { - - if (modmap->modifiermap[realkey] == 0) - continue; - - KeySym ks = XKeycodeToKeysym(disp, modmap->modifiermap[realkey], 0); - - switch (ks) { - case XK_Caps_Lock: - s_capslock_mod = mods[i]; - break; - case XK_Scroll_Lock: - s_scrolllock_mod = mods[i]; - break; - case XK_Num_Lock: - s_numlock_mod = mods[i]; - break; - } + //capslock+numlock + XGrabKey(display, key, mod|capsmod|nummod, + root, True, + GrabModeAsync, GrabModeAsync); + + //capslock+scrolllock + XGrabKey(display, key, mod|capsmod|scrollmod, + root, True, + GrabModeAsync, GrabModeAsync); + + //capslock+numlock+scrolllock + XGrabKey(display, key, mod|capsmod|scrollmod|nummod, + root, True, + GrabModeAsync, GrabModeAsync); + + //numlock+scrollLock + XGrabKey(display, key, mod|nummod|scrollmod, + root, True, + GrabModeAsync, GrabModeAsync); + + } + +} + +/** + @return keycode of keystr on success else 0 +*/ +unsigned int KeyUtil::getKey(const char *keystr) { + if (!keystr) + return 0; + return XKeysymToKeycode(App::instance()->display(), + XStringToKeysym(keystr)); +} + +/** + @return the modifier for the modstr else zero on failure. +*/ +unsigned int KeyUtil::getModifier(const char *modstr) { + if (!modstr) + return 0; + struct t_modlist{ + char *str; + unsigned int mask; + bool operator == (const char *modstr) { + return (strcasecmp(str, modstr) == 0 && mask !=0); } + } modlist[] = { + {"SHIFT", ShiftMask}, + {"CONTROL", ControlMask}, + {"MOD1", Mod1Mask}, + {"MOD2", Mod2Mask}, + {"MOD3", Mod3Mask}, + {"MOD4", Mod4Mask}, + {"MOD5", Mod5Mask}, + {0, 0} + }; + + // find mod mask string + for (unsigned int i=0; modlist[i].str !=0; i++) { + if (modlist[i] == modstr) + return modlist[i].mask; } + + return 0; +} - s_init = true; - XFreeModifiermap(modmap); +/// Ungrabs the keys +void KeyUtil::ungrabKeys() { + Display * display = App::instance()->display(); + for (int screen=0; screenm_modmap; + + if (!modmap) return 0; + + // search through modmap for this keycode + for (int mod=0; mod < 8; mod++) { + for (int key=0; key < modmap->max_keypermod; ++key) { + // modifiermap is an array with 8 sets of keycodes + // each max_keypermod long, but in a linear array. + if (modmap->modifiermap[modmap->max_keypermod*mod + key] == keycode) { + return (1< +#include + namespace FbTk { class KeyUtil { public: + + KeyUtil(); + ~KeyUtil(); + + void init(); + static KeyUtil *instance(); + + /** + Grab the specified key + */ + static void grabKey(unsigned int key, unsigned int mod); + + /** + convert the string to the keysym + @return the keysym corresponding to the string, or zero + */ + static unsigned int getKey(const char *keystr); + + /** + @return the modifier for the modstr else zero on failure. + */ + static unsigned int KeyUtil::getModifier(const char *modstr); + + /** + ungrabs all keys + */ + static void ungrabKeys(); + /** Strip out modifiers we want to ignore @return the cleaned state number */ static unsigned int cleanMods(unsigned int mods) { - if (!s_init) - init(); - //remove numlock, capslock and scrolllock - return mods & (~s_capslock_mod & ~s_numlock_mod & ~s_scrolllock_mod); + //remove numlock(Mod2), capslock and scrolllock(Mod5) + return mods & ~(LockMask | Mod2Mask | Mod5Mask); } - static int capslockMod() { if (!s_init) init(); return s_capslock_mod; } - static int numlockMod() { if (!s_init) init(); return s_numlock_mod; } - static int scrolllockMod() { if (!s_init) init(); return s_scrolllock_mod; } - /// so one can force a reinit of modifiers - static void init(); + /** + Convert the specified key into appropriate modifier mask + @return corresponding modifier mask + */ + static unsigned int keycodeToModmask(unsigned int keycode); + private: - static int s_capslock_mod, s_numlock_mod, s_scrolllock_mod; ///< modifiers - static bool s_init; + void loadModmap(); + + XModifierKeymap *m_modmap; + static KeyUtil *s_keyutil; }; } // end namespace FbTk diff --git a/src/FbTk/TextBox.cc b/src/FbTk/TextBox.cc index 01451e4..519b56e 100644 --- a/src/FbTk/TextBox.cc +++ b/src/FbTk/TextBox.cc @@ -19,7 +19,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -// $Id: TextBox.cc,v 1.2 2003/09/08 21:26:19 fluxgen Exp $ +// $Id: TextBox.cc,v 1.3 2003/10/05 07:20:47 rathnor Exp $ #include "TextBox.hh" #include "Font.hh" @@ -185,8 +185,7 @@ void TextBox::buttonPressEvent(XButtonEvent &event) { void TextBox::keyPressEvent(XKeyEvent &event) { // strip numlock and scrolllock mask - event.state &= ~FbTk::KeyUtil::numlockMod(); - event.state &= ~FbTk::KeyUtil::scrolllockMod(); + event.state = KeyUtil::cleanMods(event.state); KeySym ks; char keychar[1]; 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 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -//$Id: Keys.cc,v 1.37 2003/09/06 13:58:06 fluxgen Exp $ +//$Id: Keys.cc,v 1.38 2003/10/05 07:19:30 rathnor Exp $ #include "Keys.hh" @@ -74,20 +74,16 @@ using namespace std; Keys::Keys(const char *filename): - m_display(FbTk::App::instance()->display()), - m_modmap(0) { - - loadModmap(); + m_display(FbTk::App::instance()->display()) +{ if (filename != 0) load(filename); } Keys::~Keys() { - if (m_modmap) - XFreeModifiermap(m_modmap); - ungrabKeys(); + FbTk::KeyUtil::ungrabKeys(); deleteTree(); } @@ -100,14 +96,6 @@ void Keys::deleteTree() { } } -/// Ungrabs the keys -void Keys::ungrabKeys() { - for (int screen=0; screen1) { //keyarg=0; - int tmpmod = getModifier(val[argc].c_str()); + int tmpmod = FbTk::KeyUtil::getModifier(val[argc].c_str()); if(tmpmod) mod|=tmpmod; //If it's a modifier else { - key = getKey(val[argc].c_str()); // else get the key + key = FbTk::KeyUtil::getKey(val[argc].c_str()); // else get the key if (key == 0) { cerr<<"["<key, newtree->mod); + FbTk::KeyUtil::grabKey(newtree->key, newtree->mod); m_keylist.push_back(new t_key(newtree)); if (newtree->keylist.size()) return mergeTree(newtree->keylist[0], m_keylist.back()); @@ -420,7 +300,7 @@ bool Keys::mergeTree(t_key *newtree, t_key *basetree) { } //if it wasn't in the list grab the key and add it to the list if (baselist_i==basetree->keylist.size()) { - grabKey(newtree->key, newtree->mod); + FbTk::KeyUtil::grabKey(newtree->key, newtree->mod); basetree->keylist.push_back(new t_key(newtree)); if (newtree->keylist.size()) return mergeTree(newtree->keylist[0], basetree->keylist.back()); @@ -453,20 +333,3 @@ Keys::t_key::~t_key() { } } - -unsigned int Keys::keycodeToModmask(unsigned int keycode) { - if (!m_modmap) return 0; - - // search through modmap for this keycode - for (int mod=0; mod < 8; mod++) { - for (int key=0; key < m_modmap->max_keypermod; ++key) { - // modifiermap is an array with 8 sets of keycodes - // each max_keypermod long, but in a linear array. - if (m_modmap->modifiermap[m_modmap->max_keypermod*mod + key] == keycode) { - return (1< m_keylist; Display *m_display; ///< display connection - XModifierKeymap *m_modmap; }; diff --git a/src/fluxbox.cc b/src/fluxbox.cc index 2fa7714..bc985a7 100644 --- a/src/fluxbox.cc +++ b/src/fluxbox.cc @@ -22,7 +22,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -// $Id: fluxbox.cc,v 1.195 2003/10/05 06:28:47 rathnor Exp $ +// $Id: fluxbox.cc,v 1.196 2003/10/05 07:19:38 rathnor Exp $ #include "fluxbox.hh" @@ -43,6 +43,7 @@ #include "defaults.hh" #include "FbTk/Image.hh" +#include "FbTk/KeyUtil.hh" //Use GNU extensions #ifndef _GNU_SOURCE @@ -840,9 +841,9 @@ void Fluxbox::handleEvent(XEvent * const e) { #ifdef DEBUG cerr<<__FILE__<<"("<<__FUNCTION__<<"): MappingNotify"<loadModmap(); - } + + FbTk::KeyUtil::instance()->init(); // reinitialise the key utils + break; case CreateNotify: break; @@ -1182,7 +1183,7 @@ void Fluxbox::handleKeyEvent(XKeyEvent &ke) { if (m_watching_screen && m_watch_keyrelease) { // mask the mod of the released key out // won't mask anything if it isn't a mod - ke.state &= ~m_key->keycodeToModmask(ke.keycode); + ke.state &= ~FbTk::KeyUtil::keycodeToModmask(ke.keycode); if ((m_watch_keyrelease & ke.state) == 0) { -- cgit v0.11.2