Debian Codegraph @

src/scripts/monitors/libcamera/name-device.lua

raw
1 -- WirePlumber
2 --
3 -- Copyright © 2023 Collabora Ltd.
4 -- @author Ashok Sidipotu <[email protected]>
5 --
6 -- SPDX-License-Identifier: MIT
7
8 mutils = require ("monitor-utils")
9
10 log = Log.open_topic ("s-monitors-libcamera")
11
12 SimpleEventHook {
13 name = "monitor/libcamera/name-device",
14 interests = {
15 EventInterest {
16 Constraint { "event.type", "=", "create-libcamera-device" },
17 },
18 },
19 execute = function(event)
20 local parent = event:get_subject ()
21 local properties = event:get_data ("device-properties")
22 local id = event:get_data ("device-sub-id")
23
24 local name = "libcamera_device." ..
25 (properties["device.name"] or
26 properties["device.bus-id"] or
27 properties["device.bus-path"] or
28 tostring (id)):gsub ("([^%w_%-%.])", "_")
29
30 properties["device.name"] = name
31
32 -- deduplicate devices with the same name
33 for counter = 2, 99, 1 do
34 if mutils.find_duplicate (parent, id, "device.name", properties["device.name"]) then
35 properties["device.name"] = name .. "." .. counter
36 else
37 break
38 end
39 end
40
41 -- ensure the device has a description
42 properties["device.description"] =
43 properties["device.description"]
44 or properties["device.product.name"]
45 or "Unknown device"
46
47 event:set_data ("device-properties", properties)
48 end
49 }:register ()