Files
wireplumber/src/scripts/monitors/v4l2/name-device.lua
George Kiagiadakis 43aa2d4952 scripts: don't use 'local' for file-wide scoped variables
Since all scripts run in a sandbox with their own global environment,
it means that they don't interfere with each other's global variables.
Therefore, all file-wide variables can be declared global without
any change in behavior. In my understanding, it is better to do so
because this means that any code accessing those variables is going
to access them directly from the global environment table with a simple
lookup rather than having each variable referenced in the local closure
of each function separately.
2023-09-29 23:13:28 +03:00

50 lines
1.3 KiB
Lua

-- WirePlumber
--
-- Copyright © 2023 Collabora Ltd.
-- @author Ashok Sidipotu <ashok.sidipotu@collabora.com>
--
-- SPDX-License-Identifier: MIT
mutils = require ("monitor-utils")
log = Log.open_topic ("s-monitors-v4l2")
SimpleEventHook {
name = "monitor/v4l2/name-device",
interests = {
EventInterest {
Constraint { "event.type", "=", "create-v4l2-device" },
},
},
execute = function(event)
local properties = event:get_data ("device-properties")
local parent = event:get_subject ()
local id = event:get_data ("device-sub-id")
local name = "v4l2_device." ..
(properties["device.name"] or
properties["device.bus-id"] or
properties["device.bus-path"] or
tostring (id)):gsub ("([^%w_%-%.])", "_")
properties["device.name"] = name
-- deduplicate devices with the same name
for counter = 2, 99, 1 do
if mutils.find_duplicate (parent, id, "device.name", properties["node.name"]) then
properties["device.name"] = name .. "." .. counter
else
break
end
end
-- ensure the device has a description
properties["device.description"] =
properties["device.description"]
or properties["device.product.name"]
or "Unknown device"
event:set_data ("device-properties", properties)
end
}:register ()