From 668ed6135c0c344b65b9950870278a4a0aeafb24 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Sun, 17 Nov 2019 19:48:25 -0500 Subject: [PATCH] client: Add a `:grant()` and `:deny()` method for permissions. This is a lower level API than what most people will end up using (the rules), but it is useful enough to expose to the public API. --- lib/awful/client.lua | 17 +++++++++++++++++ lib/awful/permissions/_common.lua | 28 +++++++++++++++++++++++++++- lib/awful/permissions/init.lua | 10 +++++++++- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/lib/awful/client.lua b/lib/awful/client.lua index fc06f612..574e3dc1 100644 --- a/lib/awful/client.lua +++ b/lib/awful/client.lua @@ -15,6 +15,7 @@ local grect = require("gears.geometry").rectangle local gmath = require("gears.math") local gtable = require("gears.table") local amousec = require("awful.mouse.client") +local pcommon = require("awful.permissions._common") local pairs = pairs local type = type local ipairs = ipairs @@ -1485,6 +1486,22 @@ function client.object.activate(c, args) end end +--- Grant a permission for a client. +-- +-- @method grant +-- @tparam string permission The permission name (just the name, no `request::`). +-- @tparam string context The reason why this permission is requested. +-- @see awful.permissions + +--- Deny a permission for a client. +-- +-- @method deny +-- @tparam string permission The permission name (just the name, no `request::`). +-- @tparam string context The reason why this permission is requested. +-- @see awful.permissions + +pcommon.setup_grant(client.object, "client") + --- Return true if the client is active (has focus). -- -- This property is **READ ONLY**. Use `c:activate { context = "myreason" }` diff --git a/lib/awful/permissions/_common.lua b/lib/awful/permissions/_common.lua index 43503360..368c95da 100644 --- a/lib/awful/permissions/_common.lua +++ b/lib/awful/permissions/_common.lua @@ -19,11 +19,19 @@ local default_permissions = { } } -function module.check(class, request, context) +function module.check(object, class, request, context) if not default_permissions[class] then return true end if not default_permissions[class][request] then return true end if default_permissions[class][request][context] == nil then return true end + local ret = nil + + if object._private.permissions and object._private.permissions[request] then + ret = object._private.permissions[request][context] + end + + if ret ~= nil then return ret end + return default_permissions[class][request][context] end @@ -53,4 +61,22 @@ function module._deprecated_autofocus_in_use() module.set("client", "autoactivate", "history" , true) end +local function set_object_permission_common(self, request, context, v) + self._private.permissions = self._private.permissions or {} + if not self._private.permissions[request] then + self._private.permissions[request] = {} + end + self._private.permissions[request][context] = v +end + +-- Add the grant and deny methods to the objects. +function module.setup_grant(class, classname) -- luacheck: no unused + function class.grant(self, request, context) + set_object_permission_common(self, request, context, true) + end + function class.deny(self, request, context) + set_object_permission_common(self, request, context, false) + end +end + return module diff --git a/lib/awful/permissions/init.lua b/lib/awful/permissions/init.lua index 3b284b3c..cf4fcab6 100644 --- a/lib/awful/permissions/init.lua +++ b/lib/awful/permissions/init.lua @@ -146,6 +146,8 @@ end -- @tparam[opt=false] boolean hints.switch_to_tags Select all tags associated -- with the client. function permissions.activate(c, context, hints) -- luacheck: no unused args + if not pcommon.check(c, "client", "activate", context) then return end + hints = hints or {} if c.focusable == false and not hints.force then @@ -333,6 +335,8 @@ local context_mapper = { -- @tparam string context The context -- @tparam[opt={}] table hints The hints to pass to the handler function permissions.geometry(c, context, hints) + if not pcommon.check(c, "client", "geometry", context) then return end + local layout = c.screen.selected_tag and c.screen.selected_tag.layout or nil -- Setting the geometry will not work unless the client is floating. @@ -403,6 +407,8 @@ end -- @tparam string context The context -- @tparam[opt={}] table hints The hints to pass to the handler function permissions.merge_maximization(c, context, hints) + if not pcommon.check(c, "client", "geometry", context) then return end + if context ~= "client_maximize_horizontal" and context ~= "client_maximize_vertical" then return end @@ -484,6 +490,8 @@ end -- @tparam string context The context -- @tparam[opt={}] table hints The hints to pass to the handler function permissions.client_geometry_requests(c, context, hints) + if not pcommon.check(c, "client", "geometry", context) then return end + if context == "ewmh" and hints then if c.immobilized_horizontal then hints = gtable.clone(hints) @@ -679,7 +687,7 @@ local activate_context_map = { -- -- @signalhandler awful.permissions.autoactivate function permissions.autoactivate(c, context, args) - if not pcommon.check("client", "autoactivate", context) then return end + if not pcommon.check(c, "client", "autoactivate", context) then return end local ctx = activate_context_map[context] and activate_context_map[context] or context