Debian Codegraph @

src/scripts/device/find-preferred-profile.lua

raw
1 -- WirePlumber
2 --
3 -- Copyright © 2024 Collabora Ltd.
4 --
5 -- SPDX-License-Identifier: MIT
6 --
7 -- Finds the user preferred profile for a device, based on the priorities
8 -- defined in the "device.profile.priority.rules" section of the configuration.
9
10 cutils = require ("common-utils")
11 log = Log.open_topic ("s-device")
12
13 config = {}
14 config.rules = Conf.get_section_as_json ("device.profile.priority.rules", Json.Array {})
15
16 SimpleEventHook {
17 name = "device/find-preferred-profile",
18 after = "device/find-stored-profile",
19 before = "device/find-best-profile",
20 interests = {
21 EventInterest {
22 Constraint { "event.type", "=", "select-profile" },
23 },
24 },
25 execute = function (event)
26 local selected_profile = event:get_data ("selected-profile")
27
28 -- skip hook if the profile is already selected for this device.
29 if selected_profile then
30 return
31 end
32
33 local device = event:get_subject ()
34 local props = JsonUtils.match_rules_update_properties (
35 config.rules, device.properties)
36 local p_array = props["priorities"]
37
38 -- skip hook if the profile priorities are NOT defined for this device.
39 if not p_array then
40 return nil
41 end
42
43 local p_json = Json.Raw(p_array)
44 local priorities = p_json:parse()
45 local device_name = device.properties["device.name"] or ""
46
47 for _, priority_profile in ipairs(priorities) do
48 for p in device:iterate_params("EnumProfile") do
49 local device_profile = cutils.parseParam(p, "EnumProfile")
50 if device_profile and device_profile.name == priority_profile then
51 selected_profile = device_profile
52 goto profile_set
53 end
54 end
55 end
56
57 ::profile_set::
58 if selected_profile then
59 log:info (device, string.format (
60 "Found preferred profile '%s' (%d) for device '%s'",
61 selected_profile.name, selected_profile.index, device_name))
62 event:set_data ("selected-profile", selected_profile)
63 else
64 log:info (device, "Profiles listed in 'device.profile.priority.rules'"
65 .. " do not match the available ones of device: " .. device_name)
66 end
67
68 end
69 }:register()