From fe7a9bf5b0d59d25ef87ca6ccf47e40055596b84 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Fri, 29 Jul 2011 18:10:29 +0200 Subject: Simplify syntax of keys.lua keybindings are now added like this some_keymode['Ctrl F1'] = 'RootMenu' This should be more intuitive and it allows things like this foo['Ctrl F1'] = bar['Mod1 F2'] --- data/keys.lua | 172 +++++++++++++++++++++++++++++----------------------------- src/Keys.cc | 122 ++++++++++++++++++++++++++++------------- 2 files changed, 170 insertions(+), 124 deletions(-) diff --git a/data/keys.lua b/data/keys.lua index aa21145..2c526be 100644 --- a/data/keys.lua +++ b/data/keys.lua @@ -1,137 +1,137 @@ -- click on the desktop to get menus -default_keymode:addBinding('OnDesktop Mouse1', 'HideMenus') -default_keymode:addBinding('OnDesktop Mouse2', 'WorkspaceMenu') -default_keymode:addBinding('OnDesktop Mouse3', 'RootMenu') +default_keymode['OnDesktop Mouse1'] = 'HideMenus' +default_keymode['OnDesktop Mouse2'] = 'WorkspaceMenu' +default_keymode['OnDesktop Mouse3'] = 'RootMenu' -- scroll on the desktop to change workspaces -default_keymode:addBinding('OnDesktop Mouse4', 'PrevWorkspace') -default_keymode:addBinding('OnDesktop Mouse5', 'NextWorkspace') +default_keymode['OnDesktop Mouse4'] = 'PrevWorkspace' +default_keymode['OnDesktop Mouse5'] = 'NextWorkspace' -- scroll on the toolbar to change current window -default_keymode:addBinding('OnToolbar Mouse4', 'PrevWindow {static groups} (iconhidden=no)') -default_keymode:addBinding('OnToolbar Mouse5', 'NextWindow {static groups} (iconhidden=no)') +default_keymode['OnToolbar Mouse4'] = 'PrevWindow {static groups} (iconhidden=no)' +default_keymode['OnToolbar Mouse5'] = 'NextWindow {static groups} (iconhidden=no)' -- alt + left/right click to move/resize a window -default_keymode:addBinding('OnWindow Mod1 Mouse1', 'MacroCmd {Raise} {Focus} {StartMoving}') -default_keymode:addBinding('OnWindowBorder Move1', 'StartMoving') +default_keymode['OnWindow Mod1 Mouse1'] = 'MacroCmd {Raise} {Focus} {StartMoving}' +default_keymode['OnWindowBorder Move1'] = 'StartMoving' -default_keymode:addBinding('OnWindow Mod1 Mouse3', 'MacroCmd {Raise} {Focus} {StartResizing NearestCorner}') -default_keymode:addBinding('OnLeftGrip Move1', 'StartResizing bottomleft') -default_keymode:addBinding('OnRightGrip Move1', 'StartResizing bottomright') +default_keymode['OnWindow Mod1 Mouse3'] = 'MacroCmd {Raise} {Focus} {StartResizing NearestCorner}' +default_keymode['OnLeftGrip Move1'] = 'StartResizing bottomleft' +default_keymode['OnRightGrip Move1'] = 'StartResizing bottomright' -- alt + middle click to lower the window -default_keymode:addBinding('OnWindow Mod1 Mouse2', 'Lower') +default_keymode['OnWindow Mod1 Mouse2'] = 'Lower' -- control-click a window's titlebar and drag to attach windows -default_keymode:addBinding('OnTitlebar Control Mouse1', 'StartTabbing') +default_keymode['OnTitlebar Control Mouse1'] = 'StartTabbing' -- double click on the titlebar to shade -default_keymode:addBinding('OnTitlebar Double Mouse1', 'Shade') +default_keymode['OnTitlebar Double Mouse1'] = 'Shade' -- left click on the titlebar to move the window -default_keymode:addBinding('OnTitlebar Mouse1', 'MacroCmd {Raise} {Focus} {ActivateTab}') -default_keymode:addBinding('OnTitlebar Move1 ', 'StartMoving') +default_keymode['OnTitlebar Mouse1'] = 'MacroCmd {Raise} {Focus} {ActivateTab}' +default_keymode['OnTitlebar Move1 '] = 'StartMoving' -- middle click on the titlebar to lower -default_keymode:addBinding('OnTitlebar Mouse2', 'Lower') +default_keymode['OnTitlebar Mouse2'] = 'Lower' -- right click on the titlebar for a menu of options -default_keymode:addBinding('OnTitlebar Mouse3', 'WindowMenu') +default_keymode['OnTitlebar Mouse3'] = 'WindowMenu' -- alt-tab -default_keymode:addBinding('Mod1 Tab', 'NextWindow {groups} (workspace=[current])') -default_keymode:addBinding('Mod1 Shift Tab', 'PrevWindow {groups} (workspace=[current])') +default_keymode['Mod1 Tab'] = 'NextWindow {groups} (workspace=[current])' +default_keymode['Mod1 Shift Tab'] = 'PrevWindow {groups} (workspace=[current])' -- cycle through tabs in the current window -default_keymode:addBinding('Mod4 Tab', 'NextTab') -default_keymode:addBinding('Mod4 Shift Tab', 'PrevTab') +default_keymode['Mod4 Tab'] = 'NextTab' +default_keymode['Mod4 Shift Tab'] = 'PrevTab' -- go to a specific tab in the current window -default_keymode:addBinding('Mod4 1', 'Tab 1') -default_keymode:addBinding('Mod4 2', 'Tab 2') -default_keymode:addBinding('Mod4 3', 'Tab 3') -default_keymode:addBinding('Mod4 4', 'Tab 4') -default_keymode:addBinding('Mod4 5', 'Tab 5') -default_keymode:addBinding('Mod4 6', 'Tab 6') -default_keymode:addBinding('Mod4 7', 'Tab 7') -default_keymode:addBinding('Mod4 8', 'Tab 8') -default_keymode:addBinding('Mod4 9', 'Tab 9') +default_keymode['Mod4 1'] = 'Tab 1' +default_keymode['Mod4 2'] = 'Tab 2' +default_keymode['Mod4 3'] = 'Tab 3' +default_keymode['Mod4 4'] = 'Tab 4' +default_keymode['Mod4 5'] = 'Tab 5' +default_keymode['Mod4 6'] = 'Tab 6' +default_keymode['Mod4 7'] = 'Tab 7' +default_keymode['Mod4 8'] = 'Tab 8' +default_keymode['Mod4 9'] = 'Tab 9' -- open a terminal -default_keymode:addBinding('Mod1 F1', 'Exec xterm') +default_keymode['Mod1 F1'] = 'Exec xterm' -- open a dialog to run programs -default_keymode:addBinding('Mod1 F2', 'Exec fbrun') +default_keymode['Mod1 F2'] = 'Exec fbrun' -- volume settings, using common keycodes -- if these don't work, use xev to find out your real keycodes -default_keymode:addBinding('176', 'Exec amixer sset Master,0 1+') -default_keymode:addBinding('174', 'Exec amixer sset Master,0 1-') -default_keymode:addBinding('160', 'Exec amixer sset Master,0 toggle') +default_keymode['176'] = 'Exec amixer sset Master,0 1+' +default_keymode['174'] = 'Exec amixer sset Master,0 1-' +default_keymode['160'] = 'Exec amixer sset Master,0 toggle' -- current window commands -default_keymode:addBinding('Mod1 F4', 'Close') -default_keymode:addBinding('Mod1 F5', 'Kill') -default_keymode:addBinding('Mod1 F9', 'Minimize') -default_keymode:addBinding('Mod1 F10', 'Maximize') -default_keymode:addBinding('Mod1 F11', 'Fullscreen') +default_keymode['Mod1 F4'] = 'Close' +default_keymode['Mod1 F5'] = 'Kill' +default_keymode['Mod1 F9'] = 'Minimize' +default_keymode['Mod1 F10'] = 'Maximize' +default_keymode['Mod1 F11'] = 'Fullscreen' -- open the window menu -default_keymode:addBinding('Mod1 space', 'WindowMenu') +default_keymode['Mod1 space'] = 'WindowMenu' -- exit fluxbox -default_keymode:addBinding('Control Mod1 Delete', 'Exit') +default_keymode['Control Mod1 Delete'] = 'Exit' -- change to previous/next workspace -default_keymode:addBinding('Control Mod1 Left', 'PrevWorkspace') -default_keymode:addBinding('Control Mod1 Right', 'NextWorkspace') +default_keymode['Control Mod1 Left'] = 'PrevWorkspace' +default_keymode['Control Mod1 Right'] = 'NextWorkspace' -- send the current window to previous/next workspace -default_keymode:addBinding('Mod4 Left', 'SendToPrevWorkspace') -default_keymode:addBinding('Mod4 Right', 'SendToNextWorkspace') +default_keymode['Mod4 Left'] = 'SendToPrevWorkspace' +default_keymode['Mod4 Right'] = 'SendToNextWorkspace' -- send the current window and follow it to previous/next workspace -default_keymode:addBinding('Control Mod4 Left', 'TakeToPrevWorkspace') -default_keymode:addBinding('Control Mod4 Right', 'TakeToNextWorkspace') +default_keymode['Control Mod4 Left'] = 'TakeToPrevWorkspace' +default_keymode['Control Mod4 Right'] = 'TakeToNextWorkspace' -- change to a specific workspace -default_keymode:addBinding('Control F1', 'Workspace 1') -default_keymode:addBinding('Control F2', 'Workspace 2') -default_keymode:addBinding('Control F3', 'Workspace 3') -default_keymode:addBinding('Control F4', 'Workspace 4') -default_keymode:addBinding('Control F5', 'Workspace 5') -default_keymode:addBinding('Control F6', 'Workspace 6') -default_keymode:addBinding('Control F7', 'Workspace 7') -default_keymode:addBinding('Control F8', 'Workspace 8') -default_keymode:addBinding('Control F9', 'Workspace 9') -default_keymode:addBinding('Control F10', 'Workspace 10') -default_keymode:addBinding('Control F11', 'Workspace 11') -default_keymode:addBinding('Control F12', 'Workspace 12') +default_keymode['Control F1'] = 'Workspace 1' +default_keymode['Control F2'] = 'Workspace 2' +default_keymode['Control F3'] = 'Workspace 3' +default_keymode['Control F4'] = 'Workspace 4' +default_keymode['Control F5'] = 'Workspace 5' +default_keymode['Control F6'] = 'Workspace 6' +default_keymode['Control F7'] = 'Workspace 7' +default_keymode['Control F8'] = 'Workspace 8' +default_keymode['Control F9'] = 'Workspace 9' +default_keymode['Control F10'] = 'Workspace 10' +default_keymode['Control F11'] = 'Workspace 11' +default_keymode['Control F12'] = 'Workspace 12' -- send the current window to a specific workspace -default_keymode:addBinding('Mod4 F1', 'SendToWorkspace 1') -default_keymode:addBinding('Mod4 F2', 'SendToWorkspace 2') -default_keymode:addBinding('Mod4 F3', 'SendToWorkspace 3') -default_keymode:addBinding('Mod4 F4', 'SendToWorkspace 4') -default_keymode:addBinding('Mod4 F5', 'SendToWorkspace 5') -default_keymode:addBinding('Mod4 F6', 'SendToWorkspace 6') -default_keymode:addBinding('Mod4 F7', 'SendToWorkspace 7') -default_keymode:addBinding('Mod4 F8', 'SendToWorkspace 8') -default_keymode:addBinding('Mod4 F9', 'SendToWorkspace 9') -default_keymode:addBinding('Mod4 F10', 'SendToWorkspace 10') -default_keymode:addBinding('Mod4 F11', 'SendToWorkspace 11') -default_keymode:addBinding('Mod4 F12', 'SendToWorkspace 12') +default_keymode['Mod4 F1'] = 'SendToWorkspace 1' +default_keymode['Mod4 F2'] = 'SendToWorkspace 2' +default_keymode['Mod4 F3'] = 'SendToWorkspace 3' +default_keymode['Mod4 F4'] = 'SendToWorkspace 4' +default_keymode['Mod4 F5'] = 'SendToWorkspace 5' +default_keymode['Mod4 F6'] = 'SendToWorkspace 6' +default_keymode['Mod4 F7'] = 'SendToWorkspace 7' +default_keymode['Mod4 F8'] = 'SendToWorkspace 8' +default_keymode['Mod4 F9'] = 'SendToWorkspace 9' +default_keymode['Mod4 F10'] = 'SendToWorkspace 10' +default_keymode['Mod4 F11'] = 'SendToWorkspace 11' +default_keymode['Mod4 F12'] = 'SendToWorkspace 12' -- send the current window and change to a specific workspace -default_keymode:addBinding('Control Mod4 F1', 'TakeToWorkspace 1') -default_keymode:addBinding('Control Mod4 F2', 'TakeToWorkspace 2') -default_keymode:addBinding('Control Mod4 F3', 'TakeToWorkspace 3') -default_keymode:addBinding('Control Mod4 F4', 'TakeToWorkspace 4') -default_keymode:addBinding('Control Mod4 F5', 'TakeToWorkspace 5') -default_keymode:addBinding('Control Mod4 F6', 'TakeToWorkspace 6') -default_keymode:addBinding('Control Mod4 F7', 'TakeToWorkspace 7') -default_keymode:addBinding('Control Mod4 F8', 'TakeToWorkspace 8') -default_keymode:addBinding('Control Mod4 F9', 'TakeToWorkspace 9') -default_keymode:addBinding('Control Mod4 F10', 'TakeToWorkspace 10') -default_keymode:addBinding('Control Mod4 F11', 'TakeToWorkspace 11') -default_keymode:addBinding('Control Mod4 F12', 'TakeToWorkspace 12') +default_keymode['Control Mod4 F1'] = 'TakeToWorkspace 1' +default_keymode['Control Mod4 F2'] = 'TakeToWorkspace 2' +default_keymode['Control Mod4 F3'] = 'TakeToWorkspace 3' +default_keymode['Control Mod4 F4'] = 'TakeToWorkspace 4' +default_keymode['Control Mod4 F5'] = 'TakeToWorkspace 5' +default_keymode['Control Mod4 F6'] = 'TakeToWorkspace 6' +default_keymode['Control Mod4 F7'] = 'TakeToWorkspace 7' +default_keymode['Control Mod4 F8'] = 'TakeToWorkspace 8' +default_keymode['Control Mod4 F9'] = 'TakeToWorkspace 9' +default_keymode['Control Mod4 F10'] = 'TakeToWorkspace 10' +default_keymode['Control Mod4 F11'] = 'TakeToWorkspace 11' +default_keymode['Control Mod4 F12'] = 'TakeToWorkspace 12' diff --git a/src/Keys.cc b/src/Keys.cc index aa040b2..f741be6 100644 --- a/src/Keys.cc +++ b/src/Keys.cc @@ -147,6 +147,8 @@ public: static void initKeys(FbTk::Lua &l); static int addBinding(lua::state *l); static int newKeyMode(lua::state *l); + static int index(lua::state *l); + static int newindex(lua::state *l); bool equalExact(const RefKey &x) { return type == x->type && key == x->key && context == x->context @@ -194,7 +196,7 @@ public: static FbTk::Lua::RegisterInitFunction registerInitKeys; }; -int Keys::t_key::addBinding(lua::state *l) +int Keys::t_key::newindex(lua::state *l) { l->checkstack(2); @@ -203,33 +205,43 @@ int Keys::t_key::addBinding(lua::state *l) RefKey k = *l->checkudata(1, keymode_metatable); - if(! l->isstring(2)) { - throw KeyError(_FB_CONSOLETEXT(Keys, Bad2ndArg, "2nd argument is not a string.", - "2nd argument is not a string.")); - } vector val; - FbTk::StringUtil::stringtok(val, l->tostring(-2).c_str()); + FbTk::StringUtil::stringtok(val, l->checkstring(2).c_str()); - if(! (l->isstring(3) || l->isfunction(3)) ) { - throw KeyError(_FB_CONSOLETEXT(Keys, Bad3rdArg, "3rd argument is not a command.", - "3rd argument is not a command.")); + RefKey k2; + try { + k2 = *l->checkudata(3, keymode_metatable); } - FbTk::RefCount > cmd; - if(l->isstring(3)) - cmd.reset(FbTk::CommandParser::instance().parse(l->tostring(-1))); - else { - l->pushvalue(3); - cmd.reset(new FbCommands::LuaCmd(*l)); + catch(lua::check_error &) { + k2.reset(new t_key); + + if(l->isstring(3)) + k2->m_command.reset(FbTk::CommandParser::instance().parse(l->tostring(-1))); + else if(l->isfunction(3)) { + l->pushvalue(3); + k2->m_command.reset(new FbCommands::LuaCmd(*l)); + } else if(l->isnil(3)) + k2.reset(); + else { + throw KeyError(_FB_CONSOLETEXT(Keys, Bad3rdArg, "3rd argument is not a command.", + "3rd argument is not a command.")); + } } FindPair p = k->findBinding(val, true); - - k = *p.first; - k->m_command = cmd; - k->keylist.clear(); + if(k2) { + RefKey t = *p.first; + k2->type = t->type; + k2->mod = t->mod; + k2->key = t->key; + k2->context = t->context; + k2->isdouble = t->isdouble; + *p.first = k2; + } else + p.second.keylist.erase(p.first); } catch(std::runtime_error &e) { - cerr << "addBinding: " << e.what() << endl; + cerr << "keymode newindex: " << e.what() << endl; } return 0; @@ -244,6 +256,42 @@ int Keys::t_key::newKeyMode(lua::state *l) { } return 1; } +int Keys::t_key::index(lua::state *l) { + l->checkstack(2); + + try { + l->checkargno(2); + + RefKey k = *l->checkudata(1, keymode_metatable); + + string str = l->checkstring(2); + + if(str == "activate") + l->pushfunction(&setKeyModeWrapper); + else { + vector val; + FbTk::StringUtil::stringtok(val, str.c_str()); + + FindPair p = k->findBinding(val, false); + if(p.first == p.second.keylist.end()) + l->pushnil(); + else { + l->createuserdata(*p.first); { + l->rawgetfield(lua::REGISTRYINDEX, keymode_metatable); + l->setmetatable(-2); + } + } + + } + } + catch(std::runtime_error &e) { + cerr << "keymode index: " << e.what() << endl; + l->pushnil(); + } + + return 1; +} + void Keys::t_key::initKeys(FbTk::Lua &l) { l.checkstack(3); lua::stack_sentry s(l); @@ -252,13 +300,11 @@ void Keys::t_key::initKeys(FbTk::Lua &l) { l.pushdestructor(); l.rawsetfield(-2, "__gc"); - l.newtable(); { - l.pushfunction(&addBinding); - l.rawsetfield(-2, "addBinding"); + l.pushfunction(&index); + l.rawsetfield(-2, "__index"); - l.pushfunction(&setKeyModeWrapper); - l.rawsetfield(-2, "activate"); - } l.rawsetfield(-2, "__index"); + l.pushfunction(&newindex); + l.rawsetfield(-2, "__newindex"); } l.pop(); newKeyMode(&l); @@ -549,18 +595,18 @@ void Keys::loadDefaults(FbTk::Lua &l) { fbdbg<<"Loading default key bindings"<