diff --git a/awesomerc.lua.in b/awesomerc.lua.in index ab01665e3..c7a3403e2 100644 --- a/awesomerc.lua.in +++ b/awesomerc.lua.in @@ -158,11 +158,11 @@ for s = 1, screen.count() do -- Create an imagebox widget which will contains an icon indicating which layout we're using. -- We need one layoutbox per screen. mylayoutbox[s] = awful.widget.layoutbox(s) - mylayoutbox[s].buttons = awful.util.table.join( + mylayoutbox[s]:buttons(awful.util.table.join( awful.button({ }, 1, function () awful.layout.inc(layouts, 1) end), awful.button({ }, 3, function () awful.layout.inc(layouts, -1) end), awful.button({ }, 4, function () awful.layout.inc(layouts, 1) end), - awful.button({ }, 5, function () awful.layout.inc(layouts, -1) end)) + awful.button({ }, 5, function () awful.layout.inc(layouts, -1) end))) -- Create a taglist widget mytaglist[s] = awful.widget.taglist(s, awful.widget.taglist.label.all, mytaglist.buttons) @@ -191,10 +191,11 @@ end -- }}} -- {{{ Mouse bindings -root.buttons = awful.util.table.join( +root.buttons(awful.util.table.join( awful.button({ }, 3, function () mymainmenu:toggle() end), awful.button({ }, 4, awful.tag.viewnext), - awful.button({ }, 5, awful.tag.viewprev)) + awful.button({ }, 5, awful.tag.viewprev) +)) -- }}} -- {{{ Key bindings @@ -317,7 +318,7 @@ for i = 1, keynumber do end -- Set keys -root.keys = globalkeys +root.keys(globalkeys) -- }}} -- {{{ Hooks @@ -368,10 +369,11 @@ awful.hooks.manage.register(function (c, startup) awful.titlebar.add(c, { modkey = modkey }) end -- Add mouse bindings - c.buttons = awful.util.table.join( + c:buttons(awful.util.table.join( awful.button({ }, 1, function (c) client.focus = c; c:raise() end), awful.button({ modkey }, 1, awful.mouse.client.move), - awful.button({ modkey }, 3, awful.mouse.client.resize)) + awful.button({ modkey }, 3, awful.mouse.client.resize) + )) -- New client may not receive focus -- if they're not focusable, so set border anyway. c.border_width = beautiful.border_width @@ -402,7 +404,7 @@ awful.hooks.manage.register(function (c, startup) client.focus = c -- Set key bindings - c.keys = clientkeys + c:keys(clientkeys) -- Set the windows at the slave, -- i.e. put it at the end of others instead of setting it master. diff --git a/button.c b/button.c index d96f3c0aa..6d5cc1a59 100644 --- a/button.c +++ b/button.c @@ -61,6 +61,49 @@ luaA_button_new(lua_State *L) return 1; } +/** Set a button array with a Lua table. + * \param L The Lua VM state. + * \param oidx The index of the object to store items into. + * \param idx The index of the Lua table. + * \param buttons The array button to fill. + */ +void +luaA_button_array_set(lua_State *L, int oidx, int idx, button_array_t *buttons) +{ + luaA_checktable(L, idx); + + foreach(button, *buttons) + luaA_object_unref_item(L, oidx, *button); + + button_array_wipe(buttons); + button_array_init(buttons); + + lua_pushnil(L); + while(lua_next(L, idx)) + if(luaA_toudata(L, -1, "button")) + button_array_append(buttons, luaA_object_ref_item(L, oidx, -1)); + else + lua_pop(L, 1); +} + +/** Push an array of button as an Lua table onto the stack. + * \param L The Lua VM state. + * \param oidx The index of the object to get items from. + * \param buttons The button array to push. + * \return The number of elements pushed on stack. + */ +int +luaA_button_array_get(lua_State *L, int oidx, button_array_t *buttons) +{ + lua_createtable(L, buttons->len, 0); + for(int i = 0; i < buttons->len; i++) + { + luaA_object_push_item(L, oidx, buttons->tab[i]); + lua_rawseti(L, -2, i + 1); + } + return 1; +} + /** Button object. * \param L The Lua VM state. * \return The number of elements pushed on stack. diff --git a/button.h b/button.h index a89749b34..59d731c5f 100644 --- a/button.h +++ b/button.h @@ -40,6 +40,10 @@ struct button_t lua_class_t button_class; LUA_OBJECT_FUNCS(button_class, button_t, button, "button") +ARRAY_FUNCS(button_t *, button, DO_NOTHING) + +int luaA_button_array_get(lua_State *, int, button_array_t *); +void luaA_button_array_set(lua_State *, int, int, button_array_t *); #endif // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/client.c b/client.c index adccc8381..e0fa8dbc2 100644 --- a/client.c +++ b/client.c @@ -49,6 +49,8 @@ static int luaA_client_gc(lua_State *L) { client_t *c = luaL_checkudata(L, 1, "client"); + button_array_wipe(&c->buttons); + key_array_wipe(&c->keys); xcb_get_wm_protocols_reply_wipe(&c->protocols); p_delete(&c->class); p_delete(&c->startup_id); @@ -1582,14 +1584,6 @@ luaA_client_newindex(lua_State *L) c->skiptb = luaA_checkboolean(L, 3); hook_property(client, c, "skip_taskbar"); break; - case A_TK_KEYS: - luaA_object_unref_item(L, 1, c->keys); - c->keys = luaA_object_ref_item(L, 1, 3); - break; - case A_TK_BUTTONS: - luaA_object_unref_item(L, 1, c->buttons); - c->buttons = luaA_object_ref_item(L, 1, 3); - break; default: return 0; } @@ -1905,10 +1899,6 @@ luaA_client_index(lua_State *L) } } break; - case A_TK_KEYS: - return luaA_object_push_item(L, 1, c->keys); - case A_TK_BUTTONS: - return luaA_object_push_item(L, 1, c->buttons); default: return 0; } @@ -1916,6 +1906,46 @@ luaA_client_index(lua_State *L) return 1; } +/** Get or set mouse buttons bindings for a client. + * \param L The Lua VM state. + * \return The number of element pushed on stack. + * \luastack + * \lvalue A client. + * \lparam An array of mouse button bindings objects, or nothing. + * \return The array of mouse button bindings objects of this client. + */ +static int +luaA_client_buttons(lua_State *L) +{ + client_t *client = luaA_client_checkudata(L, 1); + button_array_t *buttons = &client->buttons; + + if(lua_gettop(L) == 2) + luaA_button_array_set(L, 1, 2, buttons); + + return luaA_button_array_get(L, 1, buttons); +} + +/** Get or set keys bindings for a client. + * \param L The Lua VM state. + * \return The number of element pushed on stack. + * \luastack + * \lvalue A client. + * \lparam An array of key bindings objects, or nothing. + * \return The array of key bindings objects of this client. + */ +static int +luaA_client_keys(lua_State *L) +{ + client_t *c = luaA_client_checkudata(L, 1); + key_array_t *keys = &c->keys; + + if(lua_gettop(L) == 2) + luaA_key_array_set(L, 1, 2, keys); + + return luaA_key_array_get(L, 1, keys); +} + /* Client module. * \param L The Lua VM state. * \return The number of pushed elements. @@ -1974,6 +2004,8 @@ const struct luaL_reg awesome_client_meta[] = { "isvisible", luaA_client_isvisible }, { "geometry", luaA_client_geometry }, { "struts", luaA_client_struts }, + { "buttons", luaA_client_buttons }, + { "keys", luaA_client_keys }, { "tags", luaA_client_tags }, { "kill", luaA_client_kill }, { "swap", luaA_client_swap }, diff --git a/client.h b/client.h index 6ce9a479b..3b4ebf494 100644 --- a/client.h +++ b/client.h @@ -140,9 +140,9 @@ struct client_t /** Titlebar */ wibox_t *titlebar; /** Button bindings */ - void *buttons; + button_array_t buttons; /** Key bindings */ - void *keys; + key_array_t keys; /** Icon */ image_t *icon; /** Size hints */ diff --git a/common/tokenize.gperf b/common/tokenize.gperf index ddaff051a..d8dc02d28 100644 --- a/common/tokenize.gperf +++ b/common/tokenize.gperf @@ -14,7 +14,6 @@ border_padding border_width bottom button -buttons button_press button_release center @@ -49,7 +48,6 @@ image imagebox instance key -keys key_press key_release keysym diff --git a/event.c b/event.c index db837faa4..f21dd527c 100644 --- a/event.c +++ b/event.c @@ -42,34 +42,25 @@ #include "common/atoms.h" #include "common/xutil.h" -#define DO_EVENT_HOOK_CALLBACK(type, prefix, xcbtype, xcbeventprefix, match) \ +#define DO_EVENT_HOOK_CALLBACK(type, prefix, xcbtype, xcbeventprefix, arraytype, match) \ static int \ event_##xcbtype##_callback(xcb_##xcbtype##_press_event_t *ev, \ - void *arr, \ + arraytype *arr, \ int oud, \ int nargs, \ void *data) \ { \ int abs_oud = oud < 0 ? ((lua_gettop(globalconf.L) + 1) + oud) : oud; \ int item_matching = 0, item_matched = 0; \ - if(oud) \ - luaA_object_push_item(globalconf.L, abs_oud, arr); \ - else \ - luaA_object_push(globalconf.L, arr); \ - lua_pushnil(globalconf.L); \ - while(luaA_next(globalconf.L, -2)) \ - { \ - type *item = luaA_toudata(globalconf.L, -1, #prefix); \ - if(item && match(ev, item, data)) \ + foreach(item, *arr) \ + if(match(ev, *item, data)) \ { \ - lua_insert(globalconf.L, -3); \ + if(oud) \ + luaA_object_push_item(globalconf.L, abs_oud, *item); \ + else \ + prefix##_push(globalconf.L, *item); \ item_matching++; \ } \ - else \ - lua_pop(globalconf.L, 1); \ - } \ - /* remove the table */ \ - lua_pop(globalconf.L, 1); \ for(item_matched = item_matching; item_matching > 0; item_matching--) \ { \ type *item = luaL_checkudata(globalconf.L, -1, #prefix); \ @@ -80,7 +71,14 @@ { \ for(int i = 0; i < nargs; i++) \ lua_pushvalue(globalconf.L, - nargs - item_matching); \ - luaA_object_push_item(globalconf.L, - nargs - 1, item->press); \ + if(oud) \ + { \ + luaA_object_push_item(globalconf.L, abs_oud, item); \ + luaA_object_push_item(globalconf.L, -1, item->press); \ + lua_remove(globalconf.L, -2); \ + } \ + else \ + prefix##_push_item(globalconf.L, item, item->press); \ luaA_dofunction(globalconf.L, nargs, 0); \ } \ break; \ @@ -89,7 +87,14 @@ { \ for(int i = 0; i < nargs; i++) \ lua_pushvalue(globalconf.L, - nargs - item_matching); \ - luaA_object_push_item(globalconf.L, - nargs - 1, item->release); \ + if(oud) \ + { \ + luaA_object_push_item(globalconf.L, abs_oud, item); \ + luaA_object_push_item(globalconf.L, -1, item->release); \ + lua_remove(globalconf.L, -2); \ + } \ + else \ + prefix##_push_item(globalconf.L, item, item->release); \ luaA_dofunction(globalconf.L, nargs, 0); \ } \ break; \ @@ -117,8 +122,8 @@ event_key_match(xcb_key_press_event_t *ev, keyb_t *k, void *data) && (k->mod == XCB_BUTTON_MASK_ANY || k->mod == ev->state)); } -DO_EVENT_HOOK_CALLBACK(button_t, button, button, XCB_BUTTON, event_button_match) -DO_EVENT_HOOK_CALLBACK(keyb_t, key, key, XCB_KEY, event_key_match) +DO_EVENT_HOOK_CALLBACK(button_t, button, button, XCB_BUTTON, button_array_t, event_button_match) +DO_EVENT_HOOK_CALLBACK(keyb_t, key, key, XCB_KEY, key_array_t, event_key_match) /** Handle an event with mouse grabber if needed * \param x The x coordinate. @@ -180,7 +185,7 @@ event_handle_button(void *data, xcb_connection_t *connection, xcb_button_press_e } wibox_push(globalconf.L, wibox); - event_button_callback(ev, wibox->buttons, -1, 1, NULL); + event_button_callback(ev, &wibox->buttons, -1, 1, NULL); /* then try to match a widget binding */ widget_t *w = widget_getbycoords(wibox->orientation, &wibox->widgets, @@ -191,13 +196,13 @@ event_handle_button(void *data, xcb_connection_t *connection, xcb_button_press_e { widget_push(globalconf.L, w); wibox_push(globalconf.L, wibox); - event_button_callback(ev, w->buttons, -2, 2, NULL); + event_button_callback(ev, &w->buttons, -2, 2, NULL); } } else if((c = client_getbywin(ev->event))) { client_push(globalconf.L, c); - event_button_callback(ev, c->buttons, -1, 1, NULL); + event_button_callback(ev, &c->buttons, -1, 1, NULL); xcb_allow_events(globalconf.connection, XCB_ALLOW_REPLAY_POINTER, XCB_CURRENT_TIME); @@ -206,7 +211,7 @@ event_handle_button(void *data, xcb_connection_t *connection, xcb_button_press_e for(screen = 0; screen < nb_screen; screen++) if(xutil_screen_get(connection, screen)->root == ev->event) { - event_button_callback(ev, globalconf.buttons, 0, 0, NULL); + event_button_callback(ev, &globalconf.buttons, 0, 0, NULL); return 0; } @@ -598,15 +603,15 @@ event_handle_key(void *data __attribute__ ((unused)), if((c = client_getbywin(ev->event))) { client_push(globalconf.L, c); - if(!event_key_callback(ev, c->keys, -1, 1, &keysym)) - if(!event_key_callback(ev, globalconf.keys, 0, 0, &keysym)) + if(!event_key_callback(ev, &c->keys, -1, 1, &keysym)) + if(!event_key_callback(ev, &globalconf.keys, 0, 0, &keysym)) xcb_allow_events(globalconf.connection, XCB_ALLOW_REPLAY_KEYBOARD, XCB_CURRENT_TIME); } else - event_key_callback(ev, globalconf.keys, 0, 0, &keysym); + event_key_callback(ev, &globalconf.keys, 0, 0, &keysym); xcb_allow_events(globalconf.connection, XCB_ALLOW_SYNC_KEYBOARD, diff --git a/key.c b/key.c index 06c4ec235..feddeba6b 100644 --- a/key.c +++ b/key.c @@ -997,6 +997,49 @@ luaA_key_new(lua_State *L) return 1; } +/** Set a key array with a Lua table. + * \param L The Lua VM state. + * \param oidx The index of the object to store items into. + * \param idx The index of the Lua table. + * \param keys The array key to fill. + */ +void +luaA_key_array_set(lua_State *L, int oidx, int idx, key_array_t *keys) +{ + luaA_checktable(L, idx); + + foreach(key, *keys) + luaA_object_unref_item(L, oidx, *key); + + key_array_wipe(keys); + key_array_init(keys); + + lua_pushnil(L); + while(lua_next(L, idx)) + if(luaA_toudata(L, -1, "key")) + key_array_append(keys, luaA_object_ref_item(L, oidx, -1)); + else + lua_pop(L, 1); +} + +/** Push an array of key as an Lua table onto the stack. + * \param L The Lua VM state. + * \param oidx The index of the object to get items from. + * \param keys The key array to push. + * \return The number of elements pushed on stack. + */ +int +luaA_key_array_get(lua_State *L, int oidx, key_array_t *keys) +{ + lua_createtable(L, keys->len, 0); + for(int i = 0; i < keys->len; i++) + { + luaA_object_push_item(L, oidx, keys->tab[i]); + lua_rawseti(L, -2, i + 1); + } + return 1; +} + /** Push a modifier set to a Lua table. * \param L The Lua VM state. * \param modifiers The modifier. diff --git a/key.h b/key.h index 40a53461f..afb070489 100644 --- a/key.h +++ b/key.h @@ -41,10 +41,14 @@ typedef struct keyb_t lua_class_t key_class; LUA_OBJECT_FUNCS(key_class, keyb_t, key, "key") +DO_ARRAY(keyb_t *, key, DO_NOTHING) bool key_press_lookup_string(xcb_keysym_t, char *, ssize_t); xcb_keysym_t key_getkeysym(xcb_keycode_t, uint16_t); +void luaA_key_array_set(lua_State *, int, int, key_array_t *); +int luaA_key_array_get(lua_State *, int, key_array_t *); + int luaA_pushmodifiers(lua_State *, uint16_t); uint16_t luaA_tomodifiers(lua_State *L, int ud); diff --git a/lib/awful/menu.lua.in b/lib/awful/menu.lua.in index ce8c8f33f..9ad8a4a2a 100644 --- a/lib/awful/menu.lua.in +++ b/lib/awful/menu.lua.in @@ -200,7 +200,7 @@ local function add_item(data, num, item_info) label.bg_image = icon end - item.buttons = bindings + item:buttons(bindings) function label.mouse_enter() item_enter(data, num, true) end function item.mouse_enter() item_enter(data, num, true) end @@ -210,7 +210,7 @@ local function add_item(data, num, item_info) if type(item_info[2]) == "table" then submenu = widget({ type = "imagebox" }) submenu.image = data.theme.submenu_icon and image(data.theme.submenu_icon) - submenu.buttons = bindings + submenu:buttons(bindings) function submenu.mouse_enter() item_enter(data, num, true) end end diff --git a/lib/awful/titlebar.lua.in b/lib/awful/titlebar.lua.in index 980559e9e..6d8feefc9 100644 --- a/lib/awful/titlebar.lua.in +++ b/lib/awful/titlebar.lua.in @@ -80,10 +80,11 @@ function add(c, args) end -- Redirect relevant events to the client the titlebar belongs to - title.buttons = util.table.join( + local bts = util.table.join( abutton({ }, 1, button_callback_focus_raise_move), abutton({ args.modkey }, 1, button_callback_move), abutton({ args.modkey }, 3, button_callback_resize)) + title:buttons(bts) local appicon = capi.widget({ type = "imagebox" }) appicon.image = c.icon @@ -172,6 +173,8 @@ end -- @param theme The theme from beautifull. Used to get the image paths -- @param state The state the button is associated to. Containse path the action and info about the image local function button_new(c, name, modkey, theme, state) + local bts = abutton({ }, 1, nil, state.action) + -- get the image path from the theme. Only return a button if we find an image local img img = "titlebar_" .. name .. "_button_" .. state.img @@ -184,9 +187,13 @@ local function button_new(c, name, modkey, theme, state) local bname = name .. "_" .. state.idx local button = widget.button({ image = img }) if not button then return end + local rbts = button:buttons() - button.buttons = util.table.join(button.buttons, abutton({ }, 1, nil, state.action)) + for k, v in pairs(rbts) do + bts[#bts + 1] = v + end + button:buttons(bts) button.visible = false return button end diff --git a/lib/awful/widget/button.lua.in b/lib/awful/widget/button.lua.in index bce6153da..4dfa60c5c 100644 --- a/lib/awful/widget/button.lua.in +++ b/lib/awful/widget/button.lua.in @@ -32,7 +32,7 @@ function new(args) args.type = "imagebox" local w = capi.widget(args) w.image = img_release - w.buttons = button({}, 1, function () w.image = img_press end, function () w.image = img_release end) + w:buttons(button({}, 1, function () w.image = img_press end, function () w.image = img_release end)) function w.mouse_leave(s) w.image = img_release end function w.mouse_enter(s) if capi.mouse.coords().buttons[1] then w.image = img_press end end return w diff --git a/lib/awful/widget/common.lua.in b/lib/awful/widget/common.lua.in index d712d14b6..a301b086d 100644 --- a/lib/awful/widget/common.lua.in +++ b/lib/awful/widget/common.lua.in @@ -65,8 +65,8 @@ function list_update(w, buttons, label, data, widgets, objects) data[o][kb] = capi.button(b.modifiers, b.button, press, release) end end - w[k].buttons = data[o] - w[k + 1].buttons = data[o] + w[k]:buttons(data[o]) + w[k + 1]:buttons(data[o]) end local text, bg, bg_image, icon = label(o) diff --git a/lib/awful/widget/launcher.lua.in b/lib/awful/widget/launcher.lua.in index 1013bf693..80a5715aa 100644 --- a/lib/awful/widget/launcher.lua.in +++ b/lib/awful/widget/launcher.lua.in @@ -21,11 +21,12 @@ function new(args) if not w then return end if args.command then - w.buttons = util.table.join(w.buttons, button({}, 1, nil, function () util.spawn(args.command) end)) + b = util.table.join(w:buttons(), button({}, 1, nil, function () util.spawn(args.command) end)) elseif args.menu then - w.buttons = util.table.join(w.buttons, button({}, 1, nil, function () args.menu:toggle() end)) + b = util.table.join(w:buttons(), button({}, 1, nil, function () args.menu:toggle() end)) end + w:buttons(b) return w end diff --git a/lib/naughty.lua.in b/lib/naughty.lua.in index d006d38bf..2e51b8137 100644 --- a/lib/naughty.lua.in +++ b/lib/naughty.lua.in @@ -312,7 +312,7 @@ function notify(args) -- create textbox local textbox = capi.widget({ type = "textbox", align = "flex" }) - textbox.buttons = util.table.join(button({ }, 1, run), button({ }, 3, die)) + textbox:buttons(util.table.join(button({ }, 1, run), button({ }, 3, die))) textbox:margin({ right = margin, left = margin, bottom = margin, top = margin }) textbox.text = string.format('%s%s', font, title, text) if hover_timeout then textbox.mouse_enter = hover_destroy end @@ -328,7 +328,7 @@ function notify(args) -- if we have an icon, use it if icon then iconbox = capi.widget({ type = "imagebox", align = "left" }) - iconbox.buttons = util.table.join(button({ }, 1, run), button({ }, 3, die)) + iconbox:buttons(util.table.join(button({ }, 1, run), button({ }, 3, die))) local img if type(icon) == "string" then img = capi.image(icon) diff --git a/luaa.c b/luaa.c index cafab36f8..5f7d23b60 100644 --- a/luaa.c +++ b/luaa.c @@ -49,8 +49,7 @@ extern const struct luaL_reg awesome_dbus_lib[]; extern const struct luaL_reg awesome_hooks_lib[]; extern const struct luaL_reg awesome_keygrabber_lib[]; extern const struct luaL_reg awesome_mousegrabber_lib[]; -extern const struct luaL_reg awesome_root_methods[]; -extern const struct luaL_reg awesome_root_meta[]; +extern const struct luaL_reg awesome_root_lib[]; extern const struct luaL_reg awesome_button_methods[]; extern const struct luaL_reg awesome_button_meta[]; extern const struct luaL_reg awesome_image_methods[]; @@ -649,8 +648,9 @@ luaA_init(xdgHandle* xdg) /* Export awesome lib */ luaA_openlib(L, "awesome", awesome_lib, awesome_lib); - /* Export root */ - luaA_openlib(L, "root", awesome_root_methods, awesome_root_meta); + /* Export root lib */ + luaL_register(L, "root", awesome_root_lib); + lua_pop(L, 1); /* luaL_register() leaves the table on stack */ /* Export hooks lib */ luaL_register(L, "hooks", awesome_hooks_lib); diff --git a/root.c b/root.c index af2ebb707..e3c79f38e 100644 --- a/root.c +++ b/root.c @@ -22,6 +22,7 @@ #include #include "structs.h" +#include "button.h" #include "wibox.h" #include "common/xcursor.h" #include "common/tokenize.h" @@ -100,6 +101,82 @@ luaA_root_fake_input(lua_State *L) return 0; } +/** Get or set global key bindings. + * This binding will be available when you'll press keys on root window. + * \param L The Lua VM state. + * \return The number of element pushed on stack. + * \luastack + * \lparam An array of key bindings objects, or nothing. + * \lreturn The array of key bindings objects of this client. + */ +static int +luaA_root_keys(lua_State *L) +{ + if(lua_gettop(L) == 1) + { + luaA_checktable(L, 1); + + foreach(key, globalconf.keys) + key_unref(globalconf.L, *key); + + key_array_wipe(&globalconf.keys); + key_array_init(&globalconf.keys); + + lua_pushnil(L); + while(lua_next(L, 1)) + key_array_append(&globalconf.keys, key_ref(L, -1)); + + return 1; + } + + lua_createtable(L, globalconf.keys.len, 0); + for(int i = 0; i < globalconf.keys.len; i++) + { + key_push(L, globalconf.keys.tab[i]); + lua_rawseti(L, -2, i + 1); + } + + return 1; +} + +/** Get or set global mouse bindings. + * This binding will be available when you'll click on root window. + * \param L The Lua VM state. + * \return The number of element pushed on stack. + * \luastack + * \lparam An array of mouse button bindings objects, or nothing. + * \lreturn The array of mouse button bindings objects. + */ +static int +luaA_root_buttons(lua_State *L) +{ + if(lua_gettop(L) == 1) + { + luaA_checktable(L, 1); + + foreach(button, globalconf.buttons) + button_unref(globalconf.L, *button); + + button_array_wipe(&globalconf.buttons); + button_array_init(&globalconf.buttons); + + lua_pushnil(L); + while(lua_next(L, 1)) + button_array_append(&globalconf.buttons, button_ref(L, -1)); + + return 1; + } + + lua_createtable(L, globalconf.buttons.len, 0); + for(int i = 0; i < globalconf.buttons.len; i++) + { + button_push(L, globalconf.buttons.tab[i]); + lua_rawseti(L, -2, i + 1); + } + + return 1; +} + /** Set the root cursor. * \param L The Lua VM state. * \return The number of element pushed on stack. @@ -150,58 +227,13 @@ luaA_root_wiboxes(lua_State *L) return 1; } -static int -luaA_root_index(lua_State *L) -{ - size_t len; - const char *attr = luaL_checklstring(L, 2, &len); - - switch(a_tokenize(attr, len)) - { - case A_TK_KEYS: - return luaA_object_push(L, globalconf.keys); - case A_TK_BUTTONS: - return luaA_object_push(L, globalconf.buttons); - default: - return 0; - } -} - -static int -luaA_root_newindex(lua_State *L) -{ - size_t len; - const char *attr = luaL_checklstring(L, 2, &len); - - switch(a_tokenize(attr, len)) - { - case A_TK_KEYS: - luaA_object_unref(L, globalconf.keys); - globalconf.keys = luaA_object_ref(L, 3); - break; - case A_TK_BUTTONS: - luaA_object_unref(L, globalconf.buttons); - globalconf.buttons = luaA_object_ref(L, 3); - break; - default: - break; - } - - return 0; -} - -const struct luaL_reg awesome_root_methods[] = +const struct luaL_reg awesome_root_lib[] = { + { "buttons", luaA_root_buttons }, + { "keys", luaA_root_keys }, { "cursor", luaA_root_cursor }, { "fake_input", luaA_root_fake_input }, { "wiboxes", luaA_root_wiboxes }, - { "__index", luaA_root_index }, - { "__newindex", luaA_root_newindex }, - { NULL, NULL } -}; - -const struct luaL_reg awesome_root_meta[] = -{ { NULL, NULL } }; diff --git a/structs.h b/structs.h index 431af4593..c808dcff8 100644 --- a/structs.h +++ b/structs.h @@ -68,9 +68,9 @@ struct awesome_t /** True if xinerama is active */ bool xinerama_is_active; /** Root window key bindings */ - void *keys; + key_array_t keys; /** Root window mouse bindings */ - void *buttons; + button_array_t buttons; /** Modifiers masks */ uint16_t numlockmask, shiftlockmask, capslockmask, modeswitchmask; /** Check for XTest extension */ diff --git a/wibox.c b/wibox.c index 7220f6eaa..bdfa37601 100644 --- a/wibox.c +++ b/wibox.c @@ -40,6 +40,7 @@ luaA_wibox_gc(lua_State *L) wibox_t *wibox = luaL_checkudata(L, 1, "wibox"); p_delete(&wibox->cursor); wibox_wipe(wibox); + button_array_wipe(&wibox->buttons); widget_node_array_wipe(&wibox->widgets); return luaA_object_gc(L); } @@ -906,8 +907,6 @@ luaA_wibox_index(lua_State *L) return luaA_object_push_item(L, 1, wibox->shape.bounding); case A_TK_SHAPE_CLIP: return luaA_object_push_item(L, 1, wibox->shape.clip); - case A_TK_BUTTONS: - return luaA_object_push_item(L, 1, wibox->buttons); default: return 0; } @@ -1130,9 +1129,6 @@ luaA_wibox_newindex(lua_State *L) luaA_object_unref_item(L, 1, wibox->shape.clip); wibox->shape.clip = luaA_object_ref_item(L, 1, 3); wibox->need_shape_update = true; - case A_TK_BUTTONS: - luaA_object_unref_item(L, 1, wibox->buttons); - wibox->buttons = luaA_object_ref_item(L, 1, 3); break; default: switch(wibox->type) @@ -1147,6 +1143,27 @@ luaA_wibox_newindex(lua_State *L) return 0; } +/** Get or set mouse buttons bindings to a wibox. + * \param L The Lua VM state. + * \luastack + * \lvalue A wibox. + * \lparam An array of mouse button bindings objects, or nothing. + * \return The array of mouse button bindings objects of this wibox. + */ +static int +luaA_wibox_buttons(lua_State *L) +{ + wibox_t *wibox = luaL_checkudata(L, 1, "wibox"); + + if(lua_gettop(L) == 2) + { + luaA_button_array_set(L, 1, 2, &wibox->buttons); + return 1; + } + + return luaA_button_array_get(L, 1, &wibox->buttons); +} + const struct luaL_reg awesome_wibox_methods[] = { LUA_CLASS_METHODS(wibox) @@ -1156,6 +1173,7 @@ const struct luaL_reg awesome_wibox_methods[] = const struct luaL_reg awesome_wibox_meta[] = { LUA_OBJECT_META(wibox) + { "buttons", luaA_wibox_buttons }, { "geometry", luaA_wibox_geometry }, { "__index", luaA_wibox_index }, { "__newindex", luaA_wibox_newindex }, diff --git a/wibox.h b/wibox.h index f66667045..ce7b109a9 100644 --- a/wibox.h +++ b/wibox.h @@ -65,7 +65,7 @@ struct wibox_t /* Banned? used for titlebars */ bool isbanned; /** Button bindings */ - void *buttons; + button_array_t buttons; /** The window object. */ xcb_window_t window; /** The pixmap copied to the window object. */ diff --git a/widget.c b/widget.c index 24a8068a3..c1bd5a023 100644 --- a/widget.c +++ b/widget.c @@ -42,6 +42,7 @@ luaA_widget_gc(lua_State *L) widget_t *widget = luaL_checkudata(L, 1, "widget"); if(widget->destructor) widget->destructor(widget); + button_array_wipe(&widget->buttons); return luaA_object_gc(L); } @@ -448,6 +449,28 @@ luaA_widget_new(lua_State *L) return 1; } +/** Get or set mouse buttons bindings to a widget. + * \param L The Lua VM state. + * + * \luastack + * \lvalue A widget. + * \lparam An array of mouse button bindings objects, or nothing. + * \return The array of mouse button bindings objects of this widget. + */ +static int +luaA_widget_buttons(lua_State *L) +{ + widget_t *widget = luaL_checkudata(L, 1, "widget"); + + if(lua_gettop(L) == 2) + { + luaA_button_array_set(L, 1, 2, &widget->buttons); + return 1; + } + + return luaA_button_array_get(L, 1, &widget->buttons); +} + /** Generic widget. * \param L The Lua VM state. * \return The number of elements pushed on stack. @@ -476,8 +499,6 @@ luaA_widget_index(lua_State *L) return luaA_object_push_item(L, 1, widget->mouse_enter); case A_TK_MOUSE_LEAVE: return luaA_object_push_item(L, 1, widget->mouse_leave); - case A_TK_BUTTONS: - return luaA_object_push_item(L, 1, widget->buttons); default: break; } @@ -512,10 +533,6 @@ luaA_widget_newindex(lua_State *L) luaA_object_unref_item(L, 1, widget->mouse_leave); widget->mouse_leave = luaA_object_ref_item(L, 1, 3); return 0; - case A_TK_BUTTONS: - luaA_object_unref_item(L, 1, widget->buttons); - widget->buttons = luaA_object_ref_item(L, 1, 3); - break; default: return widget->newindex ? widget->newindex(L, token) : 0; } @@ -557,6 +574,7 @@ const struct luaL_reg awesome_widget_methods[] = const struct luaL_reg awesome_widget_meta[] = { LUA_OBJECT_META(widget) + { "buttons", luaA_widget_buttons }, { "extents", luaA_widget_extents }, { "__index", luaA_widget_index }, { "__newindex", luaA_widget_newindex }, diff --git a/widget.h b/widget.h index f0d5261a4..29582804d 100644 --- a/widget.h +++ b/widget.h @@ -51,7 +51,7 @@ struct widget_t /** Misc private data */ void *data; /** Button bindings */ - void *buttons; + button_array_t buttons; /** True if the widget is visible */ bool isvisible; };