2019-01-21 08:35:18 +01:00
|
|
|
--- A module to build a set of properties based on a graph of rules.
|
|
|
|
--
|
|
|
|
-- Sources
|
|
|
|
-- =======
|
|
|
|
--
|
2019-10-07 03:53:08 +02:00
|
|
|
-- This module holds the business logic used by `ruled.client`. It provides an
|
2019-01-21 08:35:18 +01:00
|
|
|
-- object on which one can add sets of rules or, alternatively, functions.
|
|
|
|
-- In this module, the sets of rules or custom functions are called sources.
|
|
|
|
--
|
|
|
|
-- The sources are used to build a property table. Once all sources are
|
|
|
|
-- evaluated, the `:apply()` method will set the properties on the target
|
|
|
|
-- object.
|
|
|
|
--
|
|
|
|
-- Sources can have dependencies between them and the property table can only
|
|
|
|
-- be built if the sources graph can be resolved.
|
|
|
|
--
|
|
|
|
-- Rules
|
|
|
|
-- =====
|
|
|
|
--
|
|
|
|
-- The `rules` sources themselves are composed, as the name imply, of a set of
|
|
|
|
-- rule. A rule is a table with a `properties` *or* `callbacks` attribute along
|
|
|
|
-- with either `rule` or `rule_any`. It is also possible to add an `except` or
|
|
|
|
-- `except_any` attribute to narrow the scope in which the rule is applied.
|
|
|
|
-- Here's a basic example of a minimal `gears.matcher`.
|
|
|
|
--
|
|
|
|
-- @DOC_text_gears_matcher_default_EXAMPLE@
|
|
|
|
--
|
2019-07-29 03:09:19 +02:00
|
|
|
-- This example shows the different matching sections:
|
|
|
|
--
|
2019-07-22 04:05:45 +02:00
|
|
|
-- @DOC_text_gears_matcher_types_EXAMPLE@
|
|
|
|
--
|
2019-10-07 03:53:08 +02:00
|
|
|
-- More examples are available in `ruled.client`.
|
2019-01-21 08:35:18 +01:00
|
|
|
--
|
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2009 Julien Danjou
|
2019-10-07 03:53:08 +02:00
|
|
|
-- @see ruled.client
|
2019-01-21 08:35:18 +01:00
|
|
|
-- @module gears.matcher
|
|
|
|
|
|
|
|
local gtable = require("gears.table")
|
|
|
|
local gsort = require("gears.sort")
|
|
|
|
local gdebug = require("gears.debug")
|
2019-05-11 01:53:58 +02:00
|
|
|
local gobject = require("gears.object")
|
2019-01-21 08:35:18 +01:00
|
|
|
local protected_call = require("gears.protected_call")
|
|
|
|
|
|
|
|
local matcher = {}
|
|
|
|
|
2019-05-11 01:53:58 +02:00
|
|
|
--- A rule has been added to a set of matching rules.
|
|
|
|
-- @signal rule::appended
|
|
|
|
-- @tparam table gears.matcher The matcher.
|
|
|
|
-- @tparam table rule The rule.
|
|
|
|
-- @tparam table source The matching rules source name.
|
|
|
|
-- @tparam table content The matching rules source content.
|
|
|
|
-- @see append_rule
|
|
|
|
-- @see append_rules
|
|
|
|
|
|
|
|
--- A rule has been removed to a set of matching rules.
|
|
|
|
-- @signal rule::removed
|
|
|
|
-- @tparam table gears.matcher The matcher.
|
|
|
|
-- @tparam table rule The rule.
|
|
|
|
-- @tparam table source The matching rules source name.
|
|
|
|
-- @tparam table content The matching rules source content.
|
|
|
|
-- @see remove_rule
|
|
|
|
|
|
|
|
--- A matching source function has been added.
|
|
|
|
-- @signal matching_function::added
|
|
|
|
-- @tparam table gears.matcher The matcher.
|
|
|
|
-- @tparam function callback The callback.
|
|
|
|
-- @see add_matching_function
|
|
|
|
|
|
|
|
--- A matching source table has been added.
|
|
|
|
-- @signal matching_rules::added
|
|
|
|
-- @tparam table gears.matcher The matcher.
|
|
|
|
-- @tparam function callback The callback.
|
|
|
|
-- @see add_matching_rules
|
|
|
|
|
|
|
|
--- A matching source function has been removed.
|
|
|
|
-- @signal matching_source::removed
|
|
|
|
-- @tparam table gears.matcher The matcher.
|
|
|
|
-- @see remove_matching_source
|
|
|
|
|
2019-07-29 00:51:57 +02:00
|
|
|
local function default_matcher(a, b)
|
2020-04-21 19:51:11 +02:00
|
|
|
local result = a == b
|
|
|
|
if result then return result end
|
|
|
|
if type(a) == "string" and type(b) == "string" then
|
|
|
|
result = a:match(b)
|
|
|
|
if result == '' then result = nil end
|
|
|
|
end
|
|
|
|
return result
|
2019-07-29 00:51:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function greater_matcher(a, b)
|
|
|
|
return a > b
|
|
|
|
end
|
|
|
|
|
|
|
|
local function lesser_matcher(a, b)
|
|
|
|
return a < b
|
|
|
|
end
|
|
|
|
|
2019-01-21 08:35:18 +01:00
|
|
|
-- Check if an object matches a rule.
|
|
|
|
-- @param o The object.
|
|
|
|
-- #tparam table rule The rule to check.
|
|
|
|
-- @treturn boolean True if it matches, false otherwise.
|
2019-07-29 00:51:57 +02:00
|
|
|
-- @method _match
|
|
|
|
function matcher:_match(o, rule, matcher_f)
|
2019-01-21 08:35:18 +01:00
|
|
|
if not rule then return false end
|
2019-07-29 00:51:57 +02:00
|
|
|
|
|
|
|
matcher_f = matcher_f or default_matcher
|
|
|
|
|
2019-01-21 08:35:18 +01:00
|
|
|
for field, value in pairs(rule) do
|
2019-06-04 06:03:35 +02:00
|
|
|
local pm = self._private.prop_matchers[field]
|
2019-07-29 00:51:57 +02:00
|
|
|
if pm and pm(o, value, field) then
|
|
|
|
return true
|
|
|
|
elseif not matcher_f(o[field], value) then
|
2019-01-21 08:35:18 +01:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2019-07-29 00:51:57 +02:00
|
|
|
local function field_matcher(self, o, field, value, matcher_f)
|
|
|
|
matcher_f = matcher_f or default_matcher
|
|
|
|
|
2019-07-22 04:05:45 +02:00
|
|
|
local pm = self._private.prop_matchers[field]
|
|
|
|
|
|
|
|
if pm and pm(o, value, field) then
|
|
|
|
return true
|
2019-07-29 00:51:57 +02:00
|
|
|
elseif matcher_f(o[field] , value) then
|
2019-07-22 04:05:45 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2019-01-21 08:35:18 +01:00
|
|
|
-- Check if an object matches any part of a rule.
|
|
|
|
-- @param o The object.
|
2019-07-22 04:05:45 +02:00
|
|
|
-- #tparam table rule The rule _match_anyto check.
|
2019-01-21 08:35:18 +01:00
|
|
|
-- @treturn boolean True if at least one rule is matched, false otherwise.
|
2019-07-29 00:51:57 +02:00
|
|
|
-- @method _match_any
|
2019-01-21 08:35:18 +01:00
|
|
|
function matcher:_match_any(o, rule)
|
|
|
|
if not rule then return false end
|
|
|
|
for field, values in pairs(rule) do
|
|
|
|
if o[field] then
|
2019-07-29 00:51:57 +02:00
|
|
|
|
|
|
|
-- Special case, "all"
|
|
|
|
if type(values) == "boolean" and values then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2019-01-21 08:35:18 +01:00
|
|
|
for _, value in ipairs(values) do
|
2019-07-29 00:51:57 +02:00
|
|
|
if field_matcher(self, o, field, value) then
|
|
|
|
return true
|
|
|
|
end
|
2019-01-21 08:35:18 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-07-22 04:05:45 +02:00
|
|
|
|
2019-01-21 08:35:18 +01:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2019-07-22 04:05:45 +02:00
|
|
|
-- Check if an object matches at least one of every part of a rule.
|
|
|
|
--
|
|
|
|
-- @param o The object.
|
|
|
|
-- @tparam table rule The rule _match_anyto check.
|
|
|
|
-- @tparam boolean multi If the entries are table of choices.
|
|
|
|
-- @treturn boolean True if all rules are matched.
|
2019-07-29 00:51:57 +02:00
|
|
|
-- @method _match_every
|
2019-07-22 04:05:45 +02:00
|
|
|
function matcher:_match_every(o, rule)
|
|
|
|
if not rule then return true end
|
|
|
|
|
|
|
|
for field, values in pairs(rule) do
|
|
|
|
local found = false
|
|
|
|
for _, value in ipairs(values) do
|
|
|
|
if not field_matcher(self, o, field, value) then
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if not found then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2019-01-21 08:35:18 +01:00
|
|
|
--- Does a given rule entry match an object?
|
|
|
|
-- @param o The object.
|
|
|
|
-- @tparam table entry Rule entry (with keys `rule`, `rule_any`, `except` and/or
|
|
|
|
-- `except_any`).
|
|
|
|
-- @treturn boolean If `o` matches `entry`.
|
2019-07-20 02:00:25 +02:00
|
|
|
-- @method matches_rule
|
2019-01-21 08:35:18 +01:00
|
|
|
function matcher:matches_rule(o, entry)
|
|
|
|
local match = self:_match(o, entry.rule) or self:_match_any(o, entry.rule_any)
|
2019-07-29 00:51:57 +02:00
|
|
|
|
|
|
|
-- If there was `rule` or `rule_any` and they failed to match, look no further.
|
|
|
|
if (not match) and (entry.rule or entry.rule_any) then return false end
|
|
|
|
|
|
|
|
if not self:_match_every(o, entry.rule_every) then return false end
|
|
|
|
|
|
|
|
-- Negative matching.
|
|
|
|
if entry.except and self:_match(o, entry.except) then return false end
|
|
|
|
if entry.except_any and self:_match_any(o, entry.except_any) then return false end
|
|
|
|
|
|
|
|
-- Other operators.
|
|
|
|
if entry.rule_greater and not self:_match(o, entry.rule_greater, greater_matcher) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
if entry.rule_lesser and not self:_match(o, entry.rule_lesser, lesser_matcher) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
2019-01-21 08:35:18 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Get list of matching rules for an object.
|
|
|
|
--
|
|
|
|
-- If the `rules` argument is not provided, the rules added with
|
|
|
|
-- `add_matching_rules` will be used.
|
|
|
|
--
|
|
|
|
-- @param o The object.
|
|
|
|
-- @tparam[opt=nil] table rules The rules to check. List with "rule", "rule_any",
|
2019-08-06 07:50:18 +02:00
|
|
|
-- "except" and "except_any" keys. If no rules are provided, all rules
|
|
|
|
-- registered with a source will be matched.
|
2019-07-20 02:00:25 +02:00
|
|
|
-- @method matching_rules
|
2019-01-21 08:35:18 +01:00
|
|
|
function matcher:matching_rules(o, rules)
|
2019-08-06 07:50:18 +02:00
|
|
|
|
|
|
|
-- Match all sources.
|
|
|
|
if not rules then
|
|
|
|
local ret = {}
|
|
|
|
|
|
|
|
for _, r in pairs(self._matching_rules) do
|
|
|
|
gtable.merge(ret, self:matching_rules(o, r))
|
|
|
|
end
|
|
|
|
|
|
|
|
return ret
|
|
|
|
end
|
2019-05-10 23:48:10 +02:00
|
|
|
|
2019-01-21 08:35:18 +01:00
|
|
|
local result = {}
|
2019-05-10 23:48:10 +02:00
|
|
|
|
|
|
|
if not rules then
|
|
|
|
gdebug.print_warning("This matcher has no rule source")
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2019-01-21 08:35:18 +01:00
|
|
|
for _, entry in ipairs(rules) do
|
|
|
|
if self:matches_rule(o, entry) then
|
|
|
|
table.insert(result, entry)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Check if an object matches a given set of rules.
|
|
|
|
-- @param o The object.
|
|
|
|
-- @tparam table rules The rules to check. List of tables with `rule`,
|
|
|
|
-- `rule_any`, `except` and `except_any` keys.
|
|
|
|
-- @treturn boolean True if at least one rule is matched, false otherwise.
|
2019-07-20 02:00:25 +02:00
|
|
|
-- @method matches_rules
|
2019-01-21 08:35:18 +01:00
|
|
|
function matcher:matches_rules(o, rules)
|
|
|
|
for _, entry in ipairs(rules) do
|
|
|
|
if self:matches_rule(o, entry) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2019-06-04 06:03:35 +02:00
|
|
|
--- Assign a function to match an object property against a value.
|
|
|
|
--
|
|
|
|
-- The default matcher uses the `==` operator for all types. It also uses the
|
|
|
|
-- `:match()` method for string and allows pattern matching. If the value is a
|
|
|
|
-- function, then that function is called with the object and the current
|
|
|
|
-- properties to be applied. If the function returns true, the match is
|
|
|
|
-- accepted.
|
|
|
|
--
|
|
|
|
-- Custom property matcher are useful when objects are compared. This avoids
|
|
|
|
-- having to implement custom metatable for everything.
|
|
|
|
--
|
|
|
|
-- The `f` function receives 3 arguments:
|
|
|
|
--
|
|
|
|
-- * The object to match against (anything)
|
|
|
|
-- * The value to compare
|
|
|
|
-- * The property/field name.
|
|
|
|
--
|
|
|
|
-- It should return `true` if it matches and `false` otherwise.
|
|
|
|
--
|
|
|
|
-- @tparam string name The property name.
|
|
|
|
-- @tparam function f The matching function.
|
|
|
|
-- @method add_property_matcher
|
|
|
|
-- @usage -- Manually match the screen in various ways.
|
|
|
|
-- matcher:add_property_matcher("screen", function(c, value)
|
|
|
|
-- return c.screen == value
|
|
|
|
-- or screen[c.screen] == value
|
|
|
|
-- or c.screen.outputs[value] ~= nil
|
|
|
|
-- or value == "any"
|
|
|
|
-- or (value == "primary" and c.screen == screen.primary)
|
|
|
|
-- end)
|
|
|
|
--
|
|
|
|
function matcher:add_property_matcher(name, f)
|
|
|
|
assert(not self._private.prop_matchers[name], name .. " already has a matcher")
|
|
|
|
|
|
|
|
self._private.prop_matchers[name] = f
|
|
|
|
|
|
|
|
self:emit_signal("property_matcher::added", name, f)
|
|
|
|
end
|
|
|
|
|
2019-07-18 04:06:04 +02:00
|
|
|
--- Add a special setter for a property.
|
|
|
|
--
|
|
|
|
-- This is useful to add more properties to object which only make sense within
|
|
|
|
-- the context of a rule.
|
|
|
|
--
|
|
|
|
-- @method add_property_setter
|
|
|
|
-- @tparam string name The property name.
|
|
|
|
-- @tparam function f The setter function.
|
|
|
|
function matcher:add_property_setter(name, f)
|
|
|
|
assert(not self._private.prop_setters[name], name .. " already has a matcher")
|
|
|
|
|
|
|
|
self._private.prop_setters[name] = f
|
|
|
|
|
|
|
|
self:emit_signal("property_setter::added", name, f)
|
|
|
|
end
|
|
|
|
|
2019-01-21 08:35:18 +01:00
|
|
|
local function default_rules_callback(self, o, props, callbacks, rules)
|
|
|
|
for _, entry in ipairs(self:matching_rules(o, rules)) do
|
|
|
|
gtable.crush(props, entry.properties or {})
|
|
|
|
|
|
|
|
if entry.callback then
|
|
|
|
table.insert(callbacks, entry.callback)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Add a set of matching rules.
|
|
|
|
--
|
|
|
|
-- @tparam string name The provider name. It must be unique.
|
|
|
|
-- @tparam table rules A set of rules (see how they work at the top of this
|
|
|
|
-- page).
|
|
|
|
-- @tparam[opt={}] table depends_on A list of names of sources this source
|
|
|
|
-- depends on (sources that must be executed *before* `name`).
|
|
|
|
-- @tparam[opt={}] table precede A list of names of sources this source has a
|
|
|
|
-- priority over.
|
|
|
|
-- @treturn boolean Returns false if a dependency conflict was found.
|
2019-07-20 02:00:25 +02:00
|
|
|
-- @method add_matching_rules
|
2019-01-21 08:35:18 +01:00
|
|
|
function matcher:add_matching_rules(name, rules, depends_on, precede)
|
|
|
|
local function matching_fct(_self, c, props, callbacks)
|
|
|
|
default_rules_callback(_self, c, props, callbacks, rules)
|
|
|
|
end
|
|
|
|
|
|
|
|
self._matching_rules[name] = rules
|
|
|
|
|
2019-05-11 01:53:58 +02:00
|
|
|
self:emit_signal("matching_rules::added", rules)
|
|
|
|
|
2019-01-21 08:35:18 +01:00
|
|
|
return self:add_matching_function(name, matching_fct, depends_on, precede)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Add a matching function.
|
|
|
|
--
|
|
|
|
-- @tparam string name The provider name. It must be unique.
|
|
|
|
-- @tparam function callback The callback that is called to produce properties.
|
|
|
|
-- @tparam gears.matcher callback.self The matcher object.
|
|
|
|
-- @param callback.o The object.
|
|
|
|
-- @tparam table callback.properties The current properties. The callback should
|
|
|
|
-- add to and overwrite properties in this table.
|
|
|
|
-- @tparam table callback.callbacks A table of all callbacks scheduled to be
|
|
|
|
-- executed after the main properties are applied.
|
|
|
|
-- @tparam[opt={}] table depends_on A list of names of sources this source depends on
|
|
|
|
-- (sources that must be executed *before* `name`).
|
|
|
|
-- @tparam[opt={}] table precede A list of names of sources this source has a
|
|
|
|
-- priority over.
|
|
|
|
-- @treturn boolean Returns false if a dependency conflict was found.
|
2019-07-20 02:00:25 +02:00
|
|
|
-- @method add_matching_function
|
2019-01-21 08:35:18 +01:00
|
|
|
function matcher:add_matching_function(name, callback, depends_on, precede)
|
|
|
|
depends_on = depends_on or {}
|
|
|
|
precede = precede or {}
|
|
|
|
assert(type( depends_on ) == "table")
|
|
|
|
assert(type( precede ) == "table")
|
|
|
|
|
|
|
|
for _, v in ipairs(self._matching_source) do
|
|
|
|
-- Names must be unique
|
|
|
|
assert(
|
|
|
|
v.name ~= name,
|
|
|
|
"Name must be unique, but '" .. name .. "' was already registered."
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
local new_sources = self._rule_source_sort:clone()
|
|
|
|
|
|
|
|
new_sources:prepend(name, precede )
|
|
|
|
new_sources:append (name, depends_on )
|
|
|
|
|
|
|
|
local res, err = new_sources:sort()
|
|
|
|
|
|
|
|
if err then
|
|
|
|
gdebug.print_warning("Failed to add the rule source: "..err)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Only replace the source once the additions has been proven to be safe.
|
|
|
|
self._rule_source_sort = new_sources
|
|
|
|
|
|
|
|
local callbacks = {}
|
|
|
|
|
|
|
|
-- Get all callbacks for *existing* sources.
|
|
|
|
-- It is important to remember that names can be used in the sorting even
|
|
|
|
-- if the source itself doesn't (yet) exist.
|
|
|
|
for _, v in ipairs(self._matching_source) do
|
|
|
|
callbacks[v.name] = v.callback
|
|
|
|
end
|
|
|
|
|
|
|
|
self._matching_source = {}
|
|
|
|
callbacks[name] = callback
|
|
|
|
|
|
|
|
for _, v in ipairs(res) do
|
|
|
|
if callbacks[v] then
|
|
|
|
table.insert(self._matching_source, 1, {
|
|
|
|
callback = callbacks[v],
|
|
|
|
name = v
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-11 01:53:58 +02:00
|
|
|
self:emit_signal("matching_function::added", callback)
|
|
|
|
|
2019-01-21 08:35:18 +01:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Remove a source.
|
|
|
|
--
|
|
|
|
-- This removes sources added with `add_matching_function` or
|
|
|
|
-- `add_matching_rules`.
|
|
|
|
--
|
|
|
|
-- @tparam string name The source name.
|
|
|
|
-- @treturn boolean If the source has been removed.
|
2019-07-20 02:00:25 +02:00
|
|
|
-- @method remove_matching_source
|
2019-01-21 08:35:18 +01:00
|
|
|
function matcher:remove_matching_source(name)
|
|
|
|
self._rule_source_sort:remove(name)
|
|
|
|
|
|
|
|
for k, v in ipairs(self._matching_source) do
|
|
|
|
if v.name == name then
|
2019-05-11 01:53:58 +02:00
|
|
|
self:emit_signal("matching_source::removed", v)
|
2019-01-21 08:35:18 +01:00
|
|
|
table.remove(self._matching_source, k)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-11 01:53:58 +02:00
|
|
|
|
2019-01-21 08:35:18 +01:00
|
|
|
self._matching_rules[name] = nil
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2019-10-07 03:53:08 +02:00
|
|
|
--- Apply ruled.client.rules to an object.
|
2019-01-21 08:35:18 +01:00
|
|
|
--
|
|
|
|
-- Calling this will apply all properties provided by the matching functions
|
|
|
|
-- and rules.
|
|
|
|
--
|
|
|
|
-- @param o The object.
|
2019-07-20 02:00:25 +02:00
|
|
|
-- @method apply
|
2019-01-21 08:35:18 +01:00
|
|
|
function matcher:apply(o)
|
|
|
|
local callbacks, props = {}, {}
|
|
|
|
for _, v in ipairs(self._matching_source) do
|
|
|
|
v.callback(self, o, props, callbacks)
|
|
|
|
end
|
|
|
|
|
|
|
|
self:_execute(o, props, callbacks)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Execute the rules for the object `o`.
|
|
|
|
-- @param o The object.
|
|
|
|
-- @tparam table props A list of properties to apply.
|
|
|
|
-- @tparam table callbacks A list of callback to execute with the object `o` as
|
|
|
|
-- sole argument. The callbacks are executed *before* applying the properties.
|
|
|
|
-- @see gears.matcher.apply
|
|
|
|
function matcher:_execute(o, props, callbacks)
|
|
|
|
-- Apply all callbacks.
|
|
|
|
if callbacks then
|
|
|
|
for _, callback in pairs(callbacks) do
|
|
|
|
protected_call(callback, o)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for property, value in pairs(props) do
|
|
|
|
if type(value) == "function" then
|
|
|
|
value = value(o, props)
|
|
|
|
end
|
|
|
|
|
2019-07-18 04:06:04 +02:00
|
|
|
if self._private.prop_setters[property] then
|
|
|
|
self._private.prop_setters[property](o, value)
|
|
|
|
elseif type(o[property]) == "function" then
|
2019-01-21 08:35:18 +01:00
|
|
|
o[property](o, value)
|
|
|
|
else
|
|
|
|
o[property] = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-10 23:48:10 +02:00
|
|
|
--- Add a new rule to the default set.
|
|
|
|
-- @tparam string source The source name.
|
|
|
|
-- @tparam table rule A valid rule.
|
|
|
|
-- @method append_rule
|
|
|
|
function matcher:append_rule(source, rule)
|
|
|
|
if not self._matching_rules[source] then
|
|
|
|
self:add_matching_rules(source, {}, {}, {})
|
|
|
|
end
|
|
|
|
table.insert(self._matching_rules[source], rule)
|
2019-05-11 01:53:58 +02:00
|
|
|
self:emit_signal("rule::appended", rule, source, self._matching_rules[source])
|
2019-05-10 23:48:10 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Add a new rules to the default set.
|
|
|
|
-- @tparam string source The source name.
|
|
|
|
-- @tparam table rules A table with rules.
|
|
|
|
-- @method append_rules
|
|
|
|
function matcher:append_rules(source, rules)
|
|
|
|
for _, rule in ipairs(rules) do
|
|
|
|
self:append_rule(source, rule)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-23 18:39:12 +02:00
|
|
|
--- Remove a new rule from the default set.
|
2019-05-10 23:48:10 +02:00
|
|
|
-- @tparam string source The source name.
|
2019-05-25 16:07:00 +02:00
|
|
|
-- @tparam string|table rule An existing rule or its `id`.
|
2019-05-10 23:48:10 +02:00
|
|
|
-- @treturn boolean If the rule was removed.
|
|
|
|
-- @method remove_rule
|
|
|
|
function matcher:remove_rule(source, rule)
|
|
|
|
if not self._matching_rules[source] then return end
|
|
|
|
|
|
|
|
for k, v in ipairs(self._matching_rules[source]) do
|
2019-05-25 16:07:00 +02:00
|
|
|
if v == rule or v.id == rule then
|
2019-05-10 23:48:10 +02:00
|
|
|
table.remove(self._matching_rules[source], k)
|
2019-05-11 01:53:58 +02:00
|
|
|
self:emit_signal("rule::removed", rule, source, self._matching_rules[source])
|
2019-05-10 23:48:10 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2019-01-21 08:35:18 +01:00
|
|
|
local module = {}
|
|
|
|
|
|
|
|
--- Create a new rule solver object.
|
2019-07-20 02:00:25 +02:00
|
|
|
-- @constructorfct gears.matcher
|
2019-01-21 08:35:18 +01:00
|
|
|
-- @return A new rule solver object.
|
|
|
|
|
|
|
|
local function new()
|
2019-05-11 01:53:58 +02:00
|
|
|
local ret = gobject()
|
|
|
|
|
2019-07-18 04:06:04 +02:00
|
|
|
rawset(ret, "_private", {
|
|
|
|
rules = {}, prop_matchers = {}, prop_setters = {}
|
|
|
|
})
|
2019-01-21 08:35:18 +01:00
|
|
|
|
|
|
|
-- Contains the sources.
|
|
|
|
-- The elements are ordered "first in, first executed". Thus, the higher the
|
|
|
|
-- index, the higher the priority. Each entry is a table with a `name` and a
|
|
|
|
-- `callback` field. This table is exposed for debugging purpose. The API
|
|
|
|
-- is private and should only be modified using the public accessors.
|
|
|
|
ret._matching_source = {}
|
|
|
|
ret._rule_source_sort = gsort.topological()
|
|
|
|
ret._matching_rules = {}
|
|
|
|
|
|
|
|
gtable.crush(ret, matcher, true)
|
|
|
|
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
--@DOC_rule_COMMON@
|
|
|
|
|
|
|
|
--@DOC_object_COMMON@
|
|
|
|
|
|
|
|
return setmetatable(module, {__call = new})
|