| 1 |
-- WirePlumber |
| 2 |
-- |
| 3 |
-- Copyright © 2022 Collabora Ltd. |
| 4 |
-- |
| 5 |
-- SPDX-License-Identifier: MIT |
| 6 |
|
| 7 |
local module = { |
| 8 |
-- table of device info |
| 9 |
dev_infos = {}, |
| 10 |
} |
| 11 |
|
| 12 |
SimpleEventHook { |
| 13 |
name = "lib/device-info-cache/cleanup", |
| 14 |
interests = { |
| 15 |
EventInterest { |
| 16 |
Constraint { "event.type", "=", "device-removed" }, |
| 17 |
}, |
| 18 |
}, |
| 19 |
execute = function (event) |
| 20 |
local props = event:get_properties () |
| 21 |
local device_id = props ["object.serial"] |
| 22 |
Log.trace ("cleaning up dev_info for object.serial = " .. device_id) |
| 23 |
module.dev_infos [device_id] = nil |
| 24 |
end |
| 25 |
}:register() |
| 26 |
|
| 27 |
function module.get_device_info (self, device) |
| 28 |
local device_properties = device.properties |
| 29 |
local device_id = device_properties ["object.serial"] |
| 30 |
local dev_info = self.dev_infos [device_id] |
| 31 |
|
| 32 |
-- new device |
| 33 |
if not dev_info then |
| 34 |
local device_name = device_properties ["device.name"] |
| 35 |
if not device_name then |
| 36 |
Log.warning (device, "invalid device.name") |
| 37 |
return nil |
| 38 |
end |
| 39 |
|
| 40 |
Log.trace (device, string.format ( |
| 41 |
"create dev_info for '%s', object.serial = %s", device_name, device_id)) |
| 42 |
|
| 43 |
dev_info = { |
| 44 |
name = device_name, |
| 45 |
active_profile = -1, |
| 46 |
route_infos = {}, |
| 47 |
} |
| 48 |
self.dev_infos [device_id] = dev_info |
| 49 |
end |
| 50 |
|
| 51 |
return dev_info |
| 52 |
end |
| 53 |
|
| 54 |
function module.find_route_info (dev_info, route, return_new) |
| 55 |
local ri = dev_info.route_infos [route.index] |
| 56 |
if not ri and return_new then |
| 57 |
ri = { |
| 58 |
index = route.index, |
| 59 |
name = route.name, |
| 60 |
direction = route.direction, |
| 61 |
devices = route.devices or {}, |
| 62 |
profiles = route.profiles, |
| 63 |
priority = route.priority or 0, |
| 64 |
available = route.available or "unknown", |
| 65 |
prev_available = route.available or "unknown", |
| 66 |
active = false, |
| 67 |
prev_active = false, |
| 68 |
save = false, |
| 69 |
} |
| 70 |
end |
| 71 |
return ri |
| 72 |
end |
| 73 |
|
| 74 |
return module |