From 41f9e75600c008ffe66063a3e6bb763ab56cc77f Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Fri, 22 Jul 2011 17:55:55 +0200 Subject: Use RefCount to store keybindings I'm doing this because I want to have access to keybindings from lua and for that I need more flexible ownership semantics. --- src/Keys.cc | 74 ++++++++++++++++++++++--------------------------------------- src/Keys.hh | 11 ++++----- 2 files changed, 33 insertions(+), 52 deletions(-) diff --git a/src/Keys.cc b/src/Keys.cc index 64eb05d..b73eb67 100644 --- a/src/Keys.cc +++ b/src/Keys.cc @@ -127,22 +127,20 @@ int extractKeyFromString(const std::string& in, const char* start_pattern, unsig return ret; } -} // end of anonymouse namespace +} // end of anonymous namespace // helper class 'keytree' class Keys::t_key { public: // typedefs - typedef std::list keylist_t; + typedef std::list keylist_t; // constructor / destructor - t_key(int type, unsigned int mod, unsigned int key, int context, - bool isdouble); - t_key(t_key *k); - ~t_key(); + t_key(int type = 0, unsigned int mod = 0, unsigned int key = 0, int context = 0, + bool isdouble = false); - t_key *find(int type_, unsigned int mod_, unsigned int key_, + RefKey find(int type_, unsigned int mod_, unsigned int key_, int context_, bool isdouble_) { // t_key ctor sets context_ of 0 to GLOBAL, so we must here too context_ = context_ ? context_ : GLOBAL; @@ -154,7 +152,7 @@ public: FbTk::KeyUtil::instance().isolateModifierMask(mod_)) return *it; } - return 0; + return RefKey(); } // member variables @@ -180,22 +178,6 @@ Keys::t_key::t_key(int type_, unsigned int mod_, unsigned int key_, context = context_ ? context_ : GLOBAL; } -Keys::t_key::t_key(t_key *k) { - key = k->key; - mod = k->mod; - type = k->type; - context = k->context; - isdouble = k->isdouble; - m_command = k->m_command; -} - -Keys::t_key::~t_key() { - for (keylist_t::iterator list_it = keylist.begin(); list_it != keylist.end(); ++list_it) - delete *list_it; - keylist.clear(); -} - - Keys::Keys(): m_reloader(new FbTk::AutoReloadHelper()), @@ -218,10 +200,10 @@ Keys::~Keys() { /// Destroys the keytree void Keys::deleteTree() { - destroyAndClearSecond(m_map); - m_keylist = 0; - next_key = 0; - saved_keymode = 0; + m_map.clear(); + m_keylist.reset(); + next_key.reset(); + saved_keymode.reset(); } // keys are only grabbed in global context @@ -324,7 +306,7 @@ void Keys::reload() { // free memory of previous grabs deleteTree(); - m_map["default:"] = new t_key(0,0,0,0,false); + m_map["default:"] = FbTk::makeRef(); unsigned int current_line = 0; //so we can tell the user where the fault is @@ -354,7 +336,7 @@ void Keys::loadDefaults() { fbdbg<<"Loading default key bindings"<(); addBinding("OnDesktop Mouse1 :HideMenus"); addBinding("OnDesktop Mouse2 :WorkspaceMenu"); addBinding("OnDesktop Mouse3 :RootMenu"); @@ -387,14 +369,14 @@ bool Keys::addBinding(const string &linebuffer) { int type = 0, context = 0; bool isdouble = false; size_t argc = 0; - t_key *current_key=m_map["default:"]; - t_key *first_new_keylist = current_key, *first_new_key=0; + RefKey current_key = m_map["default:"]; + RefKey first_new_keylist = current_key, first_new_key; if (val[0][val[0].length()-1] == ':') { argc++; keyspace_t::iterator it = m_map.find(val[0]); if (it == m_map.end()) - m_map[val[0]] = new t_key(0,0,0,0,false); + m_map[val[0]] = FbTk::makeRef(); current_key = m_map[val[0]]; } // for each argument @@ -489,14 +471,14 @@ bool Keys::addBinding(const string &linebuffer) { current_key = current_key->find(type, mod, key, context, isdouble); if (!current_key) { - first_new_key = new t_key(type, mod, key, context, - isdouble); + first_new_key.reset( new t_key(type, mod, key, context, + isdouble) ); current_key = first_new_key; } else if (current_key->m_command) // already being used return false; } else { - t_key *temp_key = new t_key(type, mod, key, context, - isdouble); + RefKey temp_key( new t_key(type, mod, key, context, + isdouble) ); current_key->keylist.push_back(temp_key); current_key = temp_key; } @@ -514,12 +496,10 @@ bool Keys::addBinding(const string &linebuffer) { const char *str = FbTk::StringUtil::strcasestr(linebuffer.c_str(), val[argc].c_str()); if (str) // +1 to skip ':' - current_key->m_command = FbTk::CommandParser::instance().parse(str + 1); + current_key->m_command.reset(FbTk::CommandParser::instance().parse(str + 1)); - if (!str || current_key->m_command == 0 || mod) { - delete first_new_key; + if (!str || current_key->m_command == 0 || mod) return false; - } // success first_new_keylist->keylist.push_back(first_new_key); @@ -563,7 +543,7 @@ bool Keys::doAction(int type, unsigned int mods, unsigned int key, next_key = m_keylist; mods = FbTk::KeyUtil::instance().cleanMods(mods); - t_key *temp_key = next_key->find(type, mods, key, context, isdouble); + RefKey temp_key = next_key->find(type, mods, key, context, isdouble); // just because we double-clicked doesn't mean we shouldn't look for single // click commands @@ -581,10 +561,10 @@ bool Keys::doAction(int type, unsigned int mods, unsigned int key, if (type == KeyPress && !FbTk::KeyUtil::instance().keycodeToModmask(key)) { // if we're in the middle of an emacs-style keychain, exit it - next_key = 0; + next_key.reset(); if (saved_keymode) { setKeyMode(saved_keymode); - saved_keymode = 0; + saved_keymode.reset(); } } return false; @@ -603,9 +583,9 @@ bool Keys::doAction(int type, unsigned int mods, unsigned int key, if (saved_keymode) { if (next_key == m_keylist) // don't reset keymode if command changed it setKeyMode(saved_keymode); - saved_keymode = 0; + saved_keymode.reset(); } - next_key = 0; + next_key.reset(); return true; } @@ -636,7 +616,7 @@ void Keys::keyMode(const string& keyMode) { setKeyMode(it->second); } -void Keys::setKeyMode(t_key *keyMode) { +void Keys::setKeyMode(const FbTk::RefCount &keyMode) { ungrabKeys(); ungrabButtons(); diff --git a/src/Keys.hh b/src/Keys.hh index 2dc1c10..e51911d 100644 --- a/src/Keys.hh +++ b/src/Keys.hh @@ -93,7 +93,8 @@ public: private: class t_key; // helper class to build a 'keytree' - typedef std::map keyspace_t; + typedef FbTk::RefCount RefKey; + typedef std::map keyspace_t; typedef std::map WindowMap; typedef std::map HandlerMap; @@ -107,16 +108,16 @@ private: // Load default keybindings for when there are errors loading the keys file void loadDefaults(); - void setKeyMode(t_key *keyMode); + void setKeyMode(const FbTk::RefCount &keyMode); // member variables FbTk::AutoReloadHelper* m_reloader; - t_key *m_keylist; + RefKey m_keylist; keyspace_t m_map; - // former doAction static var, we need to access it from deleteTree - t_key *next_key, *saved_keymode; + RefKey next_key; + RefKey saved_keymode; WindowMap m_window_map; HandlerMap m_handler_map; -- cgit v0.11.2