Created
June 30, 2025 04:33
-
-
Save noosxe/c1b9db1a55482bb2260e208c7d434caa to your computer and use it in GitHub Desktop.
A lua script to list and switch default sources and sinks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/wpexec | |
| Flag_show = nil | |
| Flag_set = nil | |
| Flag_value = nil | |
| function ValidateShowSetFlag(flag) | |
| if (flag ~= "source") and (flag ~= "sink") then | |
| print(string.format("invalid flag: %s, supported values are: 'source' and 'sink'", flag)) | |
| os.exit(1) | |
| end | |
| end | |
| local argv = ... | |
| if argv then | |
| argv = argv:parse() | |
| if argv.show ~= nil then | |
| ValidateShowSetFlag(argv.show) | |
| Flag_show = argv.show | |
| end | |
| if argv.set ~= nil then | |
| ValidateShowSetFlag(argv.set) | |
| Flag_set = argv.set | |
| end | |
| if argv.value ~= nil then | |
| Flag_value = argv.value | |
| end | |
| if Flag_set ~= nil and Flag_value == nil then | |
| print("set flag requires a value flag") | |
| os.exit(1) | |
| end | |
| end | |
| -- Returns the node with given name | |
| -- Defaults to nil if not found | |
| function FindNodeWithName(om, nodeName, mediaClass) | |
| local interest = Interest({ | |
| type = "node", | |
| Constraint({ "node.name", "equals", nodeName }), | |
| Constraint({ | |
| "media.class", "matches", mediaClass }) | |
| }) | |
| for obj in om:iterate(interest) do | |
| return obj | |
| end | |
| return nil | |
| end | |
| Core.require_api("default-nodes", function(default_nodes) | |
| obj_mgr = ObjectManager({ | |
| Interest({ type = "client" }), | |
| Interest({ type = "device" }), | |
| Interest({ type = "node" }), | |
| }) | |
| obj_mgr:connect("installed", function(om) | |
| local function printNode(node) | |
| local id = node["bound-id"] | |
| local properties = node.properties | |
| local global_props = node["global-properties"] | |
| print(string.format("%s\t%s", properties["node.description"], properties["node.name"])) | |
| end | |
| if Flag_show == "sink" then | |
| local interest = Interest({ type = "node", Constraint({ "media.class", "matches", "*/Sink" }) }) | |
| for obj in om:iterate(interest) do | |
| printNode(obj) | |
| end | |
| end | |
| if Flag_show == "source" then | |
| local interest = Interest({ type = "node", Constraint({ "media.class", "matches", "*/Source" }) }) | |
| for obj in om:iterate(interest) do | |
| printNode(obj) | |
| end | |
| end | |
| if Flag_set == "source" then | |
| local node = FindNodeWithName(om, Flag_value, "*/Source") | |
| if node == nil then | |
| print(string.format("node not found: %s", Flag_value)) | |
| else | |
| default_nodes:call("set-default-configured-node-name", "Audio/Source", Flag_value) | |
| end | |
| end | |
| if Flag_set == "sink" then | |
| local node = FindNodeWithName(om, Flag_value, "*/Sink") | |
| if node == nil then | |
| print(string.format("node not found: %s", Flag_value)) | |
| else | |
| default_nodes:call("set-default-configured-node-name", "Audio/Sink", Flag_value) | |
| end | |
| end | |
| Core.quit() | |
| end) | |
| obj_mgr:activate() | |
| end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment