Revert "metadata: Make JACK_METADATA_* constant"
[jack2.git] / tools / jack_control
blob1172cbcd31fabdff09a125699d76f7a62a8be5a3
1 #!/usr/bin/python3
3 from __future__ import print_function
4 name_base = 'org.jackaudio'
5 control_interface_name = name_base + '.JackControl'
6 configure_interface_name = name_base + '.Configure'
7 service_name = name_base + '.service'
9 import sys
10 import os
11 from traceback import print_exc
13 import dbus
15 def bool_convert(str_value):
16     if str_value.lower() == "false":
17         return False
19     if str_value.lower() == "off":
20         return False
22     if str_value.lower() == "no":
23         return False
25     if str_value == "0":
26         return False
28     if str_value.lower() == "(null)":
29         return False
31     return bool(str_value)
33 def dbus_type_to_python_type(dbus_value):
34     if type(dbus_value) == dbus.Boolean:
35         return bool(dbus_value)
36     if type(dbus_value) == dbus.Int32 or type(dbus_value) == dbus.UInt32:
37         return int(dbus_value)
38     return dbus_value
40 def python_type_to_jackdbus_type(value, type_char):
41     type_char = str(type_char)
43     if type_char == "b":
44         return bool_convert(value);
45     elif type_char == "y":
46         return dbus.Byte(ord(value));
47     elif type_char == "i":
48         return dbus.Int32(value)
49     elif type_char == "u":
50         return dbus.UInt32(value)
52     return value
54 def dbus_type_to_type_string(dbus_value):
55     if type(dbus_value) == dbus.Boolean:
56         return "bool"
57     if type(dbus_value) == dbus.Int32:
58         return "sint"
59     if type(dbus_value) == dbus.UInt32:
60         return "uint"
61     if type(dbus_value) == dbus.Byte:
62         return "char"
63     if type(dbus_value) == dbus.String:
64         return "str"
66     return None                         # throw exception here?
68 def dbus_typesig_to_type_string(type_char):
69     type_char = str(type_char)
70     if type_char == 'i':
71         return "sint"
72     if type_char == 'u':
73         return "uint"
74     if type_char == 'y':
75         return "char"
76     if type_char == 's':
77         return "str"
78     if type_char == 'b':
79         return "bool"
81     print('shit')
82     return None                         # throw exception here?
84 def get_parameters(iface, path):
85     params = iface.GetParametersInfo(path)
87     #print params
88     for param in params:
89         typestr = dbus_typesig_to_type_string(param[0])
90         name = param[1]
91         #print name
92         descr = param[2]
93         #print descr
94         isset, default, value = iface.GetParameterValue(path + [name])
95         #print typestr
96         if bool(isset):
97             isset = "set"
98         else:
99             isset = "notset"
100         value = dbus_type_to_python_type(value)
101         default = dbus_type_to_python_type(default)
103         print("%20s: %s (%s:%s:%s:%s)" %(name, descr, typestr, isset, default, value))
105 def print_help():
106     print("Usage: %s [command] [command] ..." % os.path.basename(sys.argv[0]))
107     print("Commands:")
108     print("    exit                       - exit jack dbus service (stops jack server if currently running)")
109     print("    help                       - print this help text")
110     print("    status                     - check whether jack server is started, return value is 0 if running and 1 otherwise")
111     print("    start                      - start jack server if not currently started")
112     print("    stop                       - stop jack server if currently started")
113     print("    sm                         - switch master to currently selected driver")
114     print("    dl                         - get list of available drivers")
115     print("    dg                         - get currently selected driver")
116     print("    ds <driver>                - select driver")
117     print("    dp                         - get parameters of currently selected driver")
118     print("    dpd <param>                - get long description for driver parameter")
119     print("    dps <param> <value>        - set driver parameter")
120     print("    dpr <param>                - reset driver parameter to its default value")
121     print("    asd <driver>               - add slave driver")
122     print("    rsd <driver>               - remove slave driver")
123     print("    il                         - get list of available internals")
124     print("    ip <name>                  - get parameters of given internal")
125     print("    ipd <name> <param>         - get long description for internal parameter")
126     print("    ips <name> <param> <value> - set internal parameter")
127     print("    ipr <name> <param>         - reset internal parameter to its default value")
128     print("    iload <name>               - load internal")
129     print("    iunload <name>             - unload internal")
130     print("    ep                         - get engine parameters")
131     print("    epd <param>                - get long description for engine parameter")
132     print("    eps <param> <value>        - set engine parameter")
133     print("    epr <param>                - reset engine parameter to its default value")
135 def maybe_print_param_constraint(iface, param):
136     is_range, is_strict, is_fake, values = iface.GetParameterConstraint(param)
137     if is_range:
138         print()
139         print(("allowed range: %s to %s (inclusive)" % (values[0][0], values[1][0])))
140     elif len(values):
141         print()
142         if is_strict:
143             print("allowed values:")
144         else:
145             print("suggested values:")
147         max_len = 0
148         for value in values:
149             if len(str(value[0])) > max_len:
150                    max_len = len(str(value[0]))
151         for value in values:
152             print(("%*s'%s' - %s" % (1 + max_len - len(str(value[0])), "", str(value[0]), str(value[1]))))
154 def main():
155     if len(sys.argv) == 1 or sys.argv[1] in ["-h", "--help", "help"]:
156         print_help()
157         return 0
159     bus = dbus.SessionBus()
161     controller = bus.get_object(service_name, "/org/jackaudio/Controller")
162     control_iface = dbus.Interface(controller, control_interface_name)
163     configure_iface = dbus.Interface(controller, configure_interface_name)
165     # check arguments
166     index = 1
167     while index < len(sys.argv):
168         arg = sys.argv[index]
169         index += 1
170         try:
171             if arg == "exit":
172                 print("--- exit")
173                 control_iface.Exit()
174             elif arg == "status":
175                 print("--- status")
176                 if control_iface.IsStarted():
177                     print("started")
178                     return 0
179                 else:
180                     print("stopped")
181                     return 1
182             elif arg == 'start':
183                 print("--- start")
184                 control_iface.StartServer()
185             elif arg == 'stop':
186                 print("--- stop")
187                 control_iface.StopServer()
188             elif arg == 'sm':
189                 print("--- switch master driver")
190                 control_iface.SwitchMaster()
191             elif arg == 'ism':
192                 if control_iface.IsManuallyActivated():
193                     print("Manually activated")
194                 else:
195                     print("Automatically activated")
196             elif arg == 'dl':
197                 print("--- drivers list")
198                 is_range, is_strict, is_fake_values, values = configure_iface.GetParameterConstraint(['engine', 'driver'])
199                 for value in values:
200                     print(value[1])
201             elif arg == 'dg':
202                 print("--- get selected driver")
203                 isset, default, value = configure_iface.GetParameterValue(['engine', 'driver'])
204                 print(value)
205             elif arg == 'ds':
206                 if index >= len(sys.argv):
207                     print("driver select command requires driver name argument")
208                     return 1
210                 arg = sys.argv[index]
211                 index += 1
213                 print("--- driver select \"%s\"" % arg)
214                 configure_iface.SetParameterValue(['engine', 'driver'], dbus.String(arg))
215             elif arg == 'dp':
216                 print("--- get driver parameters (type:isset:default:value)")
217                 get_parameters(configure_iface, ['driver'])
218             elif arg == 'dpd':
219                 if index >= len(sys.argv):
220                     print("get driver parameter long description command requires parameter name argument")
221                     return 1
223                 param = sys.argv[index]
224                 index += 1
226                 print("--- get driver parameter description (%s)" % param)
227                 type_char, name, short_descr, long_descr = configure_iface.GetParameterInfo(['driver', param])
228                 print(long_descr)
229                 maybe_print_param_constraint(configure_iface, ['driver', param])
230             elif arg == 'dps':
231                 if index + 1 >= len(sys.argv):
232                     print("driver parameter set command requires parameter name and value arguments")
233                     return 1
235                 param = sys.argv[index]
236                 index += 1
237                 value = sys.argv[index]
238                 index += 1
240                 print("--- driver param set \"%s\" -> \"%s\"" % (param, value))
242                 type_char, name, short_descr, long_descr = configure_iface.GetParameterInfo(['driver', param])
243                 configure_iface.SetParameterValue(['driver', param], python_type_to_jackdbus_type(value, type_char))
244             elif arg == 'dpr':
245                 if index >= len(sys.argv):
246                     print("driver parameter reset command requires parameter name argument")
247                     return 1
249                 param = sys.argv[index]
250                 index += 1
252                 print("--- driver param reset \"%s\"" % param)
253                 configure_iface.ResetParameterValue(['driver', param])
254             elif arg == 'ep':
255                 print("--- get engine parameters (type:isset:default:value)")
256                 get_parameters(configure_iface, ['engine'])
257             elif arg == 'epd':
258                 if index >= len(sys.argv):
259                     print("get engine parameter long description command requires parameter name argument")
260                     return 1
262                 param_name = sys.argv[index]
263                 index += 1
265                 print("--- get engine parameter description (%s)" % param_name)
267                 type_char, name, short_descr, long_descr = configure_iface.GetParameterInfo(['engine', param_name])
268                 print(long_descr)
269                 maybe_print_param_constraint(configure_iface, ['engine', param_name])
270             elif arg == 'eps':
271                 if index + 1 >= len(sys.argv):
272                     print("engine parameter set command requires parameter name and value arguments")
273                     return 1
275                 param = sys.argv[index]
276                 index += 1
277                 value = sys.argv[index]
278                 index += 1
280                 print("--- engine param set \"%s\" -> \"%s\"" % (param, value))
282                 type_char, name, short_descr, long_descr = configure_iface.GetParameterInfo(['engine', param])
283                 configure_iface.SetParameterValue(['engine', param], python_type_to_jackdbus_type(value, type_char))
284             elif arg == 'epr':
285                 if index >= len(sys.argv):
286                     print("engine parameter reset command requires parameter name")
287                     return 1
289                 param = sys.argv[index]
290                 index += 1
292                 print("--- engine param reset \"%s\"" % param)
294                 type_char, name, short_descr, long_descr = configure_iface.GetParameterInfo(['engine', param])
295                 configure_iface.ResetParameterValue(['engine', param])
296             elif arg == 'il':
297                 print("--- internals list")
298                 is_leaf, internals = configure_iface.ReadContainer(['internals'])
299                 for internal in internals:
300                     print(internal)
301             elif arg == 'ip':
302                 print("--- get internal parameters (type:isset:default:value)")
304                 if index >= len(sys.argv):
305                     print("internal parameters command requires internal name argument")
306                     return 1
308                 internal_name = sys.argv[index]
309                 index += 1
311                 get_parameters(configure_iface, ['internals', internal_name])
312             elif arg == 'ipd':
313                 if index + 1 >= len(sys.argv):
314                     print("get internal parameter long description command requires internal and parameter name arguments")
315                     return 1
317                 name = sys.argv[index]
318                 index += 1
319                 param = sys.argv[index]
320                 index += 1
322                 print("--- get internal parameter description (%s)" % param)
323                 type_char, name, short_descr, long_descr = configure_iface.GetParameterInfo(['internals', name, param])
324                 print(long_descr)
325             elif arg == 'ips':
326                 if index + 2 >= len(sys.argv):
327                     print("internal parameter set command requires internal, parameter name and value arguments")
328                     return 1
330                 internal_name = sys.argv[index]
331                 index += 1
332                 param = sys.argv[index]
333                 index += 1
334                 value = sys.argv[index]
335                 index += 1
337                 print("--- internal param set \"%s\" -> \"%s\"" % (param, value))
339                 type_char, name, short_descr, long_descr = configure_iface.GetParameterInfo(['internals', internal_name, param])
340                 configure_iface.SetParameterValue(['internals', internal_name, param], python_type_to_jackdbus_type(value, type_char))
341             elif arg == 'ipr':
342                 if index + 1 >= len(sys.argv):
343                     print("reset internal parameter command requires internal and parameter name arguments")
344                     return 1
346                 internal_name = sys.argv[index]
347                 index += 1
348                 param = sys.argv[index]
349                 index += 1
351                 print("--- internal param reset \"%s\"" % param)
353                 configure_iface.ResetParameterValue(['internals', internal_name, param])
354             elif arg == 'iload':
355                 print("--- load internal")
357                 if index >= len(sys.argv):
358                     print("load internal command requires internal name argument")
359                     return 1
361                 name = sys.argv[index]
362                 index += 1
363                 result = control_iface.LoadInternal(name)
364             elif arg == 'iunload':
365                 print("--- unload internal")
367                 if index >= len(sys.argv):
368                     print("unload internal command requires internal name argument")
369                     return 1
371                 name = sys.argv[index]
372                 index += 1
373                 result = control_iface.UnloadInternal(name)
374             elif arg == 'asd':
375                 print("--- add slave driver")
377                 if index >= len(sys.argv):
378                     print("add slave driver command requires driver name argument")
379                     return 1
381                 name = sys.argv[index]
382                 index += 1
383                 result = control_iface.AddSlaveDriver(name)
384             elif arg == 'rsd':
385                 print("--- remove slave driver")
387                 if index >= len(sys.argv):
388                     print("remove slave driver command requires driver name argument")
389                     return 1
391                 name = sys.argv[index]
392                 index += 1
393                 result = control_iface.RemoveSlaveDriver(name)
394             else:
395                 print("Unknown command '%s'" % arg)
396         except dbus.DBusException as e:
397             print("DBus exception: %s" % str(e))
398             return 1
400         return 0
402 if __name__ == '__main__':
403     sys.exit(main())