diff --git a/.luacheckrc b/.luacheckrc index aa7e4f10..6cbe09f9 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -26,7 +26,6 @@ read_globals = { "key", "keygrabber", "mousegrabber", - "root", "selection", "tag", "window", @@ -43,6 +42,7 @@ read_globals = { globals = { "screen", "mouse", + "root", "client" } diff --git a/lib/awful/_compat.lua b/lib/awful/_compat.lua index 07016b14..b4226826 100644 --- a/lib/awful/_compat.lua +++ b/lib/awful/_compat.lua @@ -5,6 +5,8 @@ local util = require("awful.util" ) local spawn = require("awful.spawn") local gdebug = require("gears.debug") +local capi = {root = root} + function timer(...) -- luacheck: ignore gdebug.deprecate("gears.timer", {deprecated_in=4}) return gtimer(...) @@ -25,3 +27,32 @@ util.pread = function() .. "for an asynchronous alternative", {deprecated_in=4}) return "" end + +-- Allow properties to be set on the root object. This helps to migrate some +-- capi function to an higher level Lua implementation. +do + local root_props, root_object = {}, {} + + capi.root.set_newindex_miss_handler(function(_,key,value) + if root_object["set_"..key] then + root_object["set_"..key](value) + elseif not root_object["get_"..key] then + root_props[key] = value + else + -- If there is a getter, but no setter, then the property is read-only + error("Cannot set '" .. tostring(key) .. " because it is read-only") + end + end) + + capi.root.set_index_miss_handler(function(_,key) + if root_object["get_"..key] then + return root_object["get_"..key]() + else + return root_props[key] + end + end) + + root._private = {} + root.object = root_object + assert(root.object == root_object) +end diff --git a/tests/examples/shims/root.lua b/tests/examples/shims/root.lua index 6ceaf787..78ca955e 100644 --- a/tests/examples/shims/root.lua +++ b/tests/examples/shims/root.lua @@ -176,13 +176,29 @@ end function root.set_newindex_miss_handler(h) - rawset(mouse, "_ni_handler", h) + rawset(root, "_ni_handler", h) end function root.set_index_miss_handler(h) - rawset(mouse, "_i_handler", h) + rawset(root, "_i_handler", h) end -return root +return setmetatable(root, { + __index = function(self, key) + if key == "screen" then + return screen[1] + end + local h = rawget(root,"_i_handler") + if h then + return h(self, key) + end + end, + __newindex = function(...) + local h = rawget(root,"_ni_handler") + if h then + h(...) + end + end, +}) -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80