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