From ae58192951f616c66e5969f00b7512e37764fe36 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sat, 16 Feb 2019 16:12:46 +0100 Subject: [PATCH] Move selection API under the "selection" global This moves selection_acquire to selection.acquire, selection_getter to selection.getter and selection_watcher to selection.watcher. The only user-visible change due to this is that type(selection) is now "table" instead of "function". If that breaks something for you, tough luck. Signed-off-by: Uli Schlachter --- .luacheckrc | 3 --- selection.c | 38 +++++++++++++++++++++++++++++++ tests/test-selection-getter.lua | 8 +++---- tests/test-selection-transfer.lua | 24 +++++++++---------- tests/test-selection-watcher.lua | 6 ++--- 5 files changed, 57 insertions(+), 22 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index d5a3ccad..12721317 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -27,10 +27,7 @@ read_globals = { "keygrabber", "mousegrabber", "root", - "selection_acquire", - "selection_getter", "selection", - "selection_watcher", "tag", "window", "table.unpack", diff --git a/selection.c b/selection.c index 36f1c20f..2b83a063 100644 --- a/selection.c +++ b/selection.c @@ -132,10 +132,48 @@ luaA_selection_get(lua_State *L) return 0; } +static void +move_global_to_table(lua_State *L, int index, const char *global_name, const char *local_name) +{ + index = luaA_absindex(L, index); + + /* Get the global */ + lua_getglobal(L, global_name); + assert(!lua_isnil(L, -1)); + + /* Save it locally */ + lua_setfield(L, index, local_name); + + /* Set the global to nil */ + lua_pushnil(L); + lua_setglobal(L, global_name); +} + void selection_setup(lua_State *L) { + /* This table will be the "selection" global */ + lua_newtable(L); + + /* Setup a metatable */ + lua_newtable(L); + + /* metatable.__index = metatable */ + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + + /* Set some more fields */ lua_pushcfunction(L, luaA_selection_get); + lua_setfield(L, -2, "__call"); + + move_global_to_table(L, -2, "selection_acquire", "acquire"); + move_global_to_table(L, -2, "selection_getter", "getter"); + move_global_to_table(L, -2, "selection_watcher", "watcher"); + + /* Set the metatable */ + lua_setmetatable(L, -2); + + /* Set the "selection" global */ lua_setglobal(L, "selection"); } diff --git a/tests/test-selection-getter.lua b/tests/test-selection-getter.lua index a2ae4d71..682dbc81 100644 --- a/tests/test-selection-getter.lua +++ b/tests/test-selection-getter.lua @@ -61,7 +61,7 @@ runner.run_steps{ -- Now query the state of the clipboard (should be empty) continue = false - local s = selection_getter{ selection = "CLIPBOARD", target = "TARGETS" } + local s = selection.getter{ selection = "CLIPBOARD", target = "TARGETS" } s:connect_signal("data", function(...) error("Got unexpected data: " .. dump_return{...}) end) s:connect_signal("data_end", function() assert(not continue) @@ -97,7 +97,7 @@ runner.run_steps{ -- Query whether the clipboard contains a text (UTF8_STRING) continue = false - local s = selection_getter{ selection = "CLIPBOARD", target = "TARGETS" } + local s = selection.getter{ selection = "CLIPBOARD", target = "TARGETS" } local data = nil s:connect_signal("data", function(_, d) assert(not data) @@ -120,7 +120,7 @@ runner.run_steps{ -- Query the text in the clipboard continue = false - local s = selection_getter{ selection = "CLIPBOARD", target = "UTF8_STRING" } + local s = selection.getter{ selection = "CLIPBOARD", target = "UTF8_STRING" } local data = nil s:connect_signal("data", function(_, d) assert(data == nil) @@ -161,7 +161,7 @@ runner.run_steps{ -- Query the image in the clipboard continue = false - local s = selection_getter{ selection = "CLIPBOARD", target = "image/bmp" } + local s = selection.getter{ selection = "CLIPBOARD", target = "image/bmp" } local data = {} s:connect_signal("data", function(_, d) table.insert(data, d) diff --git a/tests/test-selection-transfer.lua b/tests/test-selection-transfer.lua index 1f851bb8..137f68ba 100644 --- a/tests/test-selection-transfer.lua +++ b/tests/test-selection-transfer.lua @@ -53,7 +53,7 @@ assert_equal(clipboard:wait_for_targets(), nil) assert_equal(clipboard:wait_for_text(), nil) ]] .. done_footer -local selection +local selection_object local selection_released local continue @@ -66,22 +66,22 @@ end runner.run_steps{ function() -- Get the selection - local s = assert(selection_acquire{ selection = "CLIPBOARD" }, + local s = assert(selection.acquire{ selection = "CLIPBOARD" }, "Failed to acquire the clipboard selection") -- Steal selection ownership from ourselves and test that it works local s_released s:connect_signal("release", function() s_released = true end) - selection = assert(selection_acquire{ selection = "CLIPBOARD" }, + selection_object = assert(selection.acquire{ selection = "CLIPBOARD" }, "Failed to acquire the clipboard selection") assert(s_released) -- Now test selection transfers - selection = assert(selection_acquire{ selection = "CLIPBOARD" }, + selection_object = assert(selection.acquire{ selection = "CLIPBOARD" }, "Failed to acquire the clipboard selection") - selection:connect_signal("request", function(_, target, transfer) + selection_object:connect_signal("request", function(_, target, transfer) if target == "TARGETS" then transfer:send{ format = "atom", @@ -108,9 +108,9 @@ runner.run_steps{ continue = false -- Now test piece-wise selection transfers - selection = assert(selection_acquire{ selection = "CLIPBOARD" }, + selection_object = assert(selection.acquire{ selection = "CLIPBOARD" }, "Failed to acquire the clipboard selection") - selection:connect_signal("request", function(_, target, transfer) + selection_object:connect_signal("request", function(_, target, transfer) if target == "TARGETS" then transfer:send{ format = "atom", @@ -148,9 +148,9 @@ runner.run_steps{ continue = false -- Now test a huge transfer - selection = assert(selection_acquire{ selection = "CLIPBOARD" }, + selection_object = assert(selection.acquire{ selection = "CLIPBOARD" }, "Failed to acquire the clipboard selection") - selection:connect_signal("request", function(_, target, transfer) + selection_object:connect_signal("request", function(_, target, transfer) if target == "TARGETS" then transfer:send{ format = "atom", @@ -195,7 +195,7 @@ runner.run_steps{ continue = false -- Now test that :release() works - selection:release() + selection_object:release() awesome.sync() spawn.with_line_callback({ "lua", "-e", check_empty_selection }, { stdout = function(line) @@ -214,9 +214,9 @@ runner.run_steps{ continue = false -- Test for "release" signal when we lose selection - selection = assert(selection_acquire{ selection = "CLIPBOARD" }, + selection_object = assert(selection.acquire{ selection = "CLIPBOARD" }, "Failed to acquire the clipboard selection") - selection:connect_signal("release", function() selection_released = true end) + selection_object:connect_signal("release", function() selection_released = true end) awesome.sync() spawn.with_line_callback({ "lua", "-e", acquire_and_clear_clipboard }, { exit = function() continue = true end }) diff --git a/tests/test-selection-watcher.lua b/tests/test-selection-watcher.lua index 59fb7465..6142cf70 100644 --- a/tests/test-selection-watcher.lua +++ b/tests/test-selection-watcher.lua @@ -29,9 +29,9 @@ Gtk.main() local had_error = false local owned_clipboard_changes, unowned_clipboard_changes = 0, 0 -local clipboard_watcher = selection_watcher("CLIPBOARD") -local clipboard_watcher_inactive = selection_watcher("CLIPBOARD") -local primary_watcher = selection_watcher("PRIMARY") +local clipboard_watcher = selection.watcher("CLIPBOARD") +local clipboard_watcher_inactive = selection.watcher("CLIPBOARD") +local primary_watcher = selection.watcher("PRIMARY") clipboard_watcher:connect_signal("selection_changed", function(_, owned) if owned then