Debian Codegraph @

src/scripts/device/state-profile.lua

raw
1 -- WirePlumber
2 --
3 -- Copyright © 2022 Collabora Ltd.
4 --
5 -- SPDX-License-Identifier: MIT
6 --
7 -- This file contains all the logic related to saving device profiles
8 -- to a state file and restoring them later on.
9
10 -- A devices profile needs to be selected for any new device. the script selects
11 -- the device profile from the user preferences, as well as store the user
12 -- selected device profile to state file
13
14 cutils = require ("common-utils")
15 log = Log.open_topic ("s-device")
16
17 -- the state storage
18 state = nil
19 state_table = nil
20
21 find_stored_profile_hook = SimpleEventHook {
22 name = "device/find-stored-profile",
23 interests = {
24 EventInterest {
25 Constraint { "event.type", "=", "select-profile" },
26 },
27 },
28 execute = function (event)
29 local selected_profile = event:get_data ("selected-profile")
30
31 -- skip hook if profile is already selected
32 if selected_profile then
33 return
34 end
35
36 local device = event:get_subject ()
37 local device_props = device.properties
38 local dev_name = device_props["device.name"]
39 local dont_restore_off_profile = cutils.parseBool (
40 device_props["session.dont-restore-off-profile"])
41 if not dev_name then
42 log:critical (device, "invalid device.name")
43 return
44 end
45
46 local profile_name = state_table[dev_name]
47
48 if profile_name then
49 for p in device:iterate_params ("EnumProfile") do
50 local profile = cutils.parseParam (p, "EnumProfile")
51 if profile.name == profile_name and profile.available ~= "no" and
52 (not dont_restore_off_profile or profile.index ~= 0) then
53 selected_profile = profile
54 break
55 end
56 end
57 end
58
59 if selected_profile then
60 log:info (device, string.format (
61 "Found stored profile '%s' (%d) for device '%s'",
62 selected_profile.name, selected_profile.index, dev_name))
63 event:set_data ("selected-profile", selected_profile)
64 end
65 end
66 }
67
68 store_user_selected_profile_hook = SimpleEventHook {
69 name = "device/store-user-selected-profile",
70 interests = {
71 EventInterest {
72 Constraint { "event.type", "=", "device-params-changed" },
73 Constraint { "event.subject.param-id", "=", "Profile" },
74 },
75 },
76 execute = function (event)
77 local device = event:get_subject ()
78
79 for p in device:iterate_params ("Profile") do
80 local profile = cutils.parseParam (p, "Profile")
81 if profile.save then
82 -- store only if this was a user-generated action (save == true)
83 updateStoredProfile (device, profile)
84 end
85 end
86 end
87 }
88
89 function updateStoredProfile (device, profile)
90 local dev_name = device.properties["device.name"]
91 local index = nil
92
93 if not dev_name then
94 log:critical (device, "invalid device.name")
95 return
96 end
97
98 log:debug (device, string.format (
99 "update stored profile to '%s' (%d) for device '%s'",
100 profile.name, profile.index, dev_name))
101
102 -- check if the new profile is the same as the current one
103 if state_table[dev_name] == profile.name then
104 log:debug (device, " ... profile is already stored")
105 return
106 end
107
108 -- find the full profile from EnumProfile, making also sure that the
109 -- user / client application has actually set an existing profile
110 for p in device:iterate_params ("EnumProfile") do
111 local enum_profile = cutils.parseParam (p, "EnumProfile")
112 if enum_profile.name == profile.name then
113 index = enum_profile.index
114 end
115 end
116
117 if not index then
118 log:info (device, string.format (
119 "profile '%s' (%d) is not valid on device '%s'",
120 profile.name, profile.index, dev_name))
121 return
122 end
123
124 state_table[dev_name] = profile.name
125 state:save_after_timeout (state_table)
126
127 log:info (device, string.format (
128 "stored profile '%s' (%d) for device '%s'",
129 profile.name, index, dev_name))
130 end
131
132 function toggleState (enable)
133 if enable and not state then
134 state = State ("default-profile")
135 state_table = state:load ()
136 find_stored_profile_hook:register ()
137 store_user_selected_profile_hook:register ()
138 elseif not enable and state then
139 state = nil
140 state_table = nil
141 find_stored_profile_hook:remove ()
142 store_user_selected_profile_hook:remove ()
143 end
144 end
145
146 Settings.subscribe ("device.restore-profile", function ()
147 toggleState (Settings.get_boolean ("device.restore-profile"))
148 end)
149 toggleState (Settings.get_boolean ("device.restore-profile"))