From bac3668d83d90727b81f40b2b57e2f9f8e24454c Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Mon, 25 Jul 2011 15:44:55 +0200 Subject: Reintroduce support for multiple keymodes new keymodes are created (in lua) by calling newKeyMode() and activated by calling keymode:activate() --- src/Keys.cc | 56 ++++++++++++++++++++++++++++++++++++++------------------ src/Keys.hh | 6 ++++++ 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/Keys.cc b/src/Keys.cc index 6becfa2..7b927ca 100644 --- a/src/Keys.cc +++ b/src/Keys.cc @@ -145,6 +145,7 @@ public: static void initKeys(FbTk::Lua &l); static int addBindingWrapper(lua::state *l); + static int newKeyMode(lua::state *l); // constructor / destructor t_key(int type = 0, unsigned int mod = 0, unsigned int key = 0, int context = 0, @@ -197,18 +198,9 @@ int Keys::t_key::addBindingWrapper(lua::state *l) l->checkstack(2); try { - if(l->gettop() != 3) { - throw KeyError(_FB_CONSOLETEXT(Keys, WrongArgNo, "Wrong number of arguments.", - "Wrong number of arguments to a function")); - } + l->checkargno(3); - l->getmetatable(1); l->rawgetfield(lua::REGISTRYINDEX, keymode_metatable); { - if(! l->rawequal(-1, -2)) { - throw KeyError(_FB_CONSOLETEXT(Keys, Bad1stArg, "1st argument is not a keymode.", - "1st argument is not a keymode.")); - } - } l->pop(2); - const RefKey &k = *static_cast(l->touserdata(1)); + const RefKey &k = *l->checkudata(1, keymode_metatable); if(! l->isstring(2)) { throw KeyError(_FB_CONSOLETEXT(Keys, Bad2ndArg, "2nd argument is not a string.", @@ -238,24 +230,37 @@ int Keys::t_key::addBindingWrapper(lua::state *l) return 0; } +int Keys::t_key::newKeyMode(lua::state *l) { + l->checkstack(2); + + l->createuserdata(new t_key()); { + l->rawgetfield(lua::REGISTRYINDEX, keymode_metatable); + l->setmetatable(-2); + } return 1; +} + void Keys::t_key::initKeys(FbTk::Lua &l) { l.checkstack(3); lua::stack_sentry s(l); - l.newtable(); { + l.newmetatable(keymode_metatable); { l.pushdestructor(); l.rawsetfield(-2, "__gc"); l.newtable(); { l.pushfunction(&addBindingWrapper); l.rawsetfield(-2, "addBinding"); + + l.pushfunction(&setKeyModeWrapper); + l.rawsetfield(-2, "activate"); } l.rawsetfield(-2, "__index"); - } l.rawsetfield(lua::REGISTRYINDEX, keymode_metatable); + } l.pop(); - l.pushstring(default_keymode); l.createuserdata(new t_key()); { - l.rawgetfield(lua::REGISTRYINDEX, keymode_metatable); - l.setmetatable(-2); - } l.readOnlySet(lua::GLOBALSINDEX); + newKeyMode(&l); + l.readOnlySetField(lua::GLOBALSINDEX, default_keymode); + + l.pushfunction(&newKeyMode); + l.readOnlySetField(lua::GLOBALSINDEX, "newKeyMode"); } FbTk::Lua::RegisterInitFunction Keys::t_key::registerInitKeys(&Keys::t_key::initKeys); @@ -406,6 +411,18 @@ Keys::~Keys() { delete m_reloader; } +int Keys::setKeyModeWrapper(lua::state *l) { + try { + l->checkargno(1); + const RefKey &k = *l->checkudata(1, keymode_metatable); + Fluxbox::instance()->keys()->setKeyMode(k); + } + catch(std::runtime_error &e) { + cerr << "activate: " << e.what() << endl; + } + return 0; +} + /// Destroys the keytree void Keys::deleteTree() { @@ -494,9 +511,12 @@ void Keys::reload() { lua::stack_sentry s(l); deleteTree(); + l.getglobal(default_keymode); assert(l.isuserdata(-1)); - RefKey t = *static_cast(l.touserdata(-1)); + RefKey t = *static_cast(l.touserdata(-1)) = RefKey(new t_key); + l.pop(); + next_key.reset(); saved_keymode.reset(); diff --git a/src/Keys.hh b/src/Keys.hh index 33aa398..6cff6b6 100644 --- a/src/Keys.hh +++ b/src/Keys.hh @@ -31,6 +31,10 @@ class WinClient; +namespace lua { + class state; +} + namespace FbTk { class EventHandler; class AutoReloadHelper; @@ -90,6 +94,8 @@ private: typedef std::map WindowMap; typedef std::map HandlerMap; + static int setKeyModeWrapper(lua::state *l); + void deleteTree(); void grabKey(unsigned int key, unsigned int mod); -- cgit v0.11.2