Debian Codegraph @

src/scripts/monitors/libcamera/name-node.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-node",
14 interests = {
15 EventInterest {
16 Constraint { "event.type", "=", "create-libcamera-device-node" },
17 },
18 },
19 execute = function(event)
20 local properties = event:get_data ("node-properties")
21 local parent = event:get_subject ()
22 local dev_props = parent.properties
23 local factory = event:get_data ("factory")
24 local id = event:get_data ("node-sub-id")
25 local location = properties ["api.libcamera.location"]
26
27 -- set the device id and spa factory name; REQUIRED, do not change
28 properties ["device.id"] = parent ["bound-id"]
29 properties ["factory.name"] = factory
30
31 -- set the default pause-on-idle setting
32 properties ["node.pause-on-idle"] = false
33
34 -- set the node name
35 local name =
36 (factory:find ("sink") and "libcamera_output") or
37 (factory:find ("source") and "libcamera_input" or factory)
38 .. "." ..
39 (dev_props ["device.name"]:gsub ("^libcamera_device%.(.+)", "%1") or
40 dev_props ["device.name"] or
41 dev_props ["device.nick"] or
42 dev_props ["device.alias"] or
43 "libcamera-device")
44 -- sanitize name
45 name = name:gsub ("([^%w_%-%.])", "_")
46
47 properties ["node.name"] = name
48
49 -- deduplicate nodes with the same name
50 for counter = 2, 99, 1 do
51 if mutils.find_duplicate (parent, id, "node.name", properties ["node.name"]) then
52 properties ["node.name"] = name .. "." .. counter
53 else
54 break
55 end
56 end
57
58 -- set the node description
59 local desc = dev_props ["device.description"] or "libcamera-device"
60 if location == "front" then
61 desc = I18n.gettext ("Built-in Front Camera")
62 elseif location == "back" then
63 desc = I18n.gettext ("Built-in Back Camera")
64 end
65 -- sanitize description, replace ':' with ' '
66 properties ["node.description"] = desc:gsub ("(:)", " ")
67
68 -- set the node nick
69 local nick = properties ["node.nick"] or
70 dev_props ["device.product.name"] or
71 dev_props ["device.description"] or
72 dev_props ["device.nick"]
73 properties ["node.nick"] = nick:gsub ("(:)", " ")
74
75 -- set priority
76 if not properties ["priority.session"] then
77 local priority = 700
78 if location == "external" then
79 priority = priority + 150
80 elseif location == "front" then
81 priority = priority + 100
82 elseif location == "back" then
83 priority = priority + 50
84 end
85 properties ["priority.session"] = priority
86 end
87
88 event:set_data ("node-properties", properties)
89 end
90 }:register ()