| 1 | #!/usr/bin/env python |
|---|
| 2 | # |
|---|
| 3 | # LADI Session Handler (ladish) |
|---|
| 4 | # |
|---|
| 5 | # Copyright (C) 2008, 2009, 2010 Nedko Arnaudov <nedko@arnaudov.name> |
|---|
| 6 | # |
|---|
| 7 | #************************************************************************* |
|---|
| 8 | # This file contains code of the commandline control app |
|---|
| 9 | #************************************************************************* |
|---|
| 10 | # |
|---|
| 11 | # LADI Session Handler is free software; you can redistribute it and/or modify |
|---|
| 12 | # it under the terms of the GNU General Public License as published by |
|---|
| 13 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 14 | # (at your option) any later version. |
|---|
| 15 | # |
|---|
| 16 | # LADI Session Handler is distributed in the hope that it will be useful, |
|---|
| 17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 19 | # GNU General Public License for more details. |
|---|
| 20 | # |
|---|
| 21 | # You should have received a copy of the GNU General Public License |
|---|
| 22 | # along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/> |
|---|
| 23 | # or write to the Free Software Foundation, Inc., |
|---|
| 24 | # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|---|
| 25 | |
|---|
| 26 | service_name = 'org.ladish' |
|---|
| 27 | |
|---|
| 28 | control_object_path = "/org/ladish/Control" |
|---|
| 29 | studio_object_path = "/org/ladish/Studio" |
|---|
| 30 | |
|---|
| 31 | control_interface_name = 'org.ladish.Control' |
|---|
| 32 | studio_interface_name = 'org.ladish.Studio' |
|---|
| 33 | app_supervisor_interface_name = 'org.ladish.AppSupervisor' |
|---|
| 34 | room_interface_name = 'org.ladish.Room' |
|---|
| 35 | |
|---|
| 36 | import sys |
|---|
| 37 | import os |
|---|
| 38 | import time |
|---|
| 39 | from traceback import print_exc |
|---|
| 40 | |
|---|
| 41 | import dbus |
|---|
| 42 | |
|---|
| 43 | def bool_convert(str_value): |
|---|
| 44 | if str_value.lower() == "false": |
|---|
| 45 | return False |
|---|
| 46 | |
|---|
| 47 | if str_value.lower() == "off": |
|---|
| 48 | return False |
|---|
| 49 | |
|---|
| 50 | if str_value.lower() == "no": |
|---|
| 51 | return False |
|---|
| 52 | |
|---|
| 53 | if str_value == "0": |
|---|
| 54 | return False |
|---|
| 55 | |
|---|
| 56 | if str_value.lower() == "(null)": |
|---|
| 57 | return False |
|---|
| 58 | |
|---|
| 59 | return bool(str_value) |
|---|
| 60 | |
|---|
| 61 | def dbus_type_to_python_type(dbus_value): |
|---|
| 62 | if type(dbus_value) == dbus.Boolean: |
|---|
| 63 | return bool(dbus_value) |
|---|
| 64 | if type(dbus_value) == dbus.Int32 or type(dbus_value) == dbus.UInt32: |
|---|
| 65 | return int(dbus_value) |
|---|
| 66 | return dbus_value |
|---|
| 67 | |
|---|
| 68 | def dbus_type_to_type_string(dbus_value): |
|---|
| 69 | if type(dbus_value) == dbus.Boolean: |
|---|
| 70 | return "bool" |
|---|
| 71 | if type(dbus_value) == dbus.Int32: |
|---|
| 72 | return "sint" |
|---|
| 73 | if type(dbus_value) == dbus.UInt32: |
|---|
| 74 | return "uint" |
|---|
| 75 | if type(dbus_value) == dbus.Byte: |
|---|
| 76 | return "char" |
|---|
| 77 | if type(dbus_value) == dbus.String: |
|---|
| 78 | return "str" |
|---|
| 79 | |
|---|
| 80 | return None # throw exception here? |
|---|
| 81 | |
|---|
| 82 | def dbus_typesig_to_type_string(type_char): |
|---|
| 83 | type_char = str(type_char) |
|---|
| 84 | if type_char == 'i': |
|---|
| 85 | return "sint" |
|---|
| 86 | if type_char == 'u': |
|---|
| 87 | return "uint" |
|---|
| 88 | if type_char == 'y': |
|---|
| 89 | return "char" |
|---|
| 90 | if type_char == 's': |
|---|
| 91 | return "str" |
|---|
| 92 | if type_char == 'b': |
|---|
| 93 | return "bool" |
|---|
| 94 | |
|---|
| 95 | print('unknown dbus typesig') |
|---|
| 96 | return None # throw exception here? |
|---|
| 97 | |
|---|
| 98 | def parse_new_app_args(params_array): |
|---|
| 99 | #print params_array |
|---|
| 100 | cmdline = params_array[0] |
|---|
| 101 | index = 1 |
|---|
| 102 | |
|---|
| 103 | name = '' |
|---|
| 104 | level = 0 |
|---|
| 105 | term = False |
|---|
| 106 | |
|---|
| 107 | if index < len(params_array): |
|---|
| 108 | if params_array[index] == '-': |
|---|
| 109 | return index + 1, cmdline, name, level, term |
|---|
| 110 | name = params_array[index] |
|---|
| 111 | index += 1 |
|---|
| 112 | |
|---|
| 113 | if index < len(params_array): |
|---|
| 114 | if params_array[index] == '-': |
|---|
| 115 | return index + 1, cmdline, name, level, term |
|---|
| 116 | # direct conversion to dbus.Byte is wrong because ord() is used ("1" -> 0x31 instead of "1" -> 0x01) |
|---|
| 117 | level = int(params_array[index]) |
|---|
| 118 | index += 1 |
|---|
| 119 | |
|---|
| 120 | if index < len(params_array): |
|---|
| 121 | if params_array[index] == '-': |
|---|
| 122 | return index + 1, cmdline, name, level, term |
|---|
| 123 | if params_array[index] == 'term': |
|---|
| 124 | term = True |
|---|
| 125 | index += 1 |
|---|
| 126 | |
|---|
| 127 | if index < len(params_array) and params_array[index] == '-': |
|---|
| 128 | index += 1 |
|---|
| 129 | |
|---|
| 130 | return index, cmdline, name, level, term |
|---|
| 131 | |
|---|
| 132 | def add_app(obj, cmdline, name, level, term): |
|---|
| 133 | dbus.Interface(obj, app_supervisor_interface_name).RunCustom(term, cmdline, name, level) |
|---|
| 134 | |
|---|
| 135 | def get_room_obj_by_name(bus, studio_iface, room_name): |
|---|
| 136 | for room in studio_iface.GetRoomList(): |
|---|
| 137 | #print repr(room) |
|---|
| 138 | opath = room[0] |
|---|
| 139 | name = room[1]["name"] |
|---|
| 140 | if name == room_name: |
|---|
| 141 | return bus.get_object(service_name, opath) |
|---|
| 142 | |
|---|
| 143 | def main(): |
|---|
| 144 | if len(sys.argv) == 1: |
|---|
| 145 | argv0 = os.path.basename(sys.argv[0]) |
|---|
| 146 | print("Usage: %s [command] [command] ..." % argv0) |
|---|
| 147 | print("Commands:") |
|---|
| 148 | print(" exit - exit ladish dbus service") |
|---|
| 149 | print(" slist - list studios") |
|---|
| 150 | print(" alist - list apps") |
|---|
| 151 | print(" sload <studioname> - load studio") |
|---|
| 152 | print(" sdel <studioname> - delete studio") |
|---|
| 153 | print(" snew [studioname] - new studio") |
|---|
| 154 | print(" sisloaded - is studio loaded?") |
|---|
| 155 | print(" sname - get studio name") |
|---|
| 156 | print(" ssave - save studio") |
|---|
| 157 | print(" sunload - unload studio") |
|---|
| 158 | print(" srename <studioname> - rename studio") |
|---|
| 159 | print(" sstart - start studio") |
|---|
| 160 | print(" sstop - stop studio") |
|---|
| 161 | print(" rtlist - list room templates") |
|---|
| 162 | print(" rtdel <roomtemplatename> - delete room template") |
|---|
| 163 | print(" rtnew <roomtemplatename> - create new room template") |
|---|
| 164 | print(" snewroom <rname> <rtname> - create new studio room") |
|---|
| 165 | print(" srlist - list studio rooms") |
|---|
| 166 | print(" sdelroom <rname> - delete studio room") |
|---|
| 167 | print(" pload <rname> <proj_dir> - load project into room") |
|---|
| 168 | print(" punload <rname> - unload project from room") |
|---|
| 169 | print(" psave <rname> - save project") |
|---|
| 170 | print(" psaveas <rname> <proj_dir> <proj_name> - save as project") |
|---|
| 171 | print(" snewapp <appargs> - add new app to studio (see below for more info)") |
|---|
| 172 | print(" rnewapp <rname> <appargs> - add new app to room (see below for more info)") |
|---|
| 173 | print(""); |
|---|
| 174 | print("Add new app arguments:"); |
|---|
| 175 | print(" <commandline> [<name> [<level>] [term]] [-]"); |
|---|
| 176 | print(""); |
|---|
| 177 | print(" <commandline> - the commandline to execut"); |
|---|
| 178 | print(" <name> - app name"); |
|---|
| 179 | print(" <level> - level, default is 0"); |
|---|
| 180 | print(" term - if specified, app will be run in terminal"); |
|---|
| 181 | print(" - - marks end of new app params, useful if there are other commands following"); |
|---|
| 182 | print(""); |
|---|
| 183 | print("Examples:"); |
|---|
| 184 | print(""); |
|---|
| 185 | print(" * Add to studio jack_mixer instance named \"mixer\", at level 1, without terminal"); |
|---|
| 186 | print(""); |
|---|
| 187 | print(" $ %s snewapp jack_mixer mixer 1" % argv0); |
|---|
| 188 | print(""); |
|---|
| 189 | print(" * Add to room \"main\" fluidjack instance named \"fluid\", at level 0, with terminal"); |
|---|
| 190 | print(""); |
|---|
| 191 | print(" $ %s rnewapp main \"fluidjack FluidR3.SF2\" fluid 0 term" % argv0); |
|---|
| 192 | print(""); |
|---|
| 193 | sys.exit(0) |
|---|
| 194 | |
|---|
| 195 | bus = dbus.SessionBus() |
|---|
| 196 | control_obj = None |
|---|
| 197 | studio_obj = None |
|---|
| 198 | |
|---|
| 199 | # check arguments |
|---|
| 200 | index = 1 |
|---|
| 201 | while index < len(sys.argv): |
|---|
| 202 | arg = sys.argv[index] |
|---|
| 203 | index += 1 |
|---|
| 204 | try: |
|---|
| 205 | if not control_obj: |
|---|
| 206 | control_obj = bus.get_object(service_name, control_object_path) |
|---|
| 207 | control_iface = dbus.Interface(control_obj, control_interface_name) |
|---|
| 208 | |
|---|
| 209 | if arg == "exit": |
|---|
| 210 | print("--- exit") |
|---|
| 211 | control_iface.Exit() |
|---|
| 212 | time.sleep(1) |
|---|
| 213 | # we have deactivated the object and we need to get new connection if there are more commands |
|---|
| 214 | control_obj = None |
|---|
| 215 | control_iface = None |
|---|
| 216 | elif arg == 'slist': |
|---|
| 217 | print("--- studio list") |
|---|
| 218 | for studio in control_iface.GetStudioList(): |
|---|
| 219 | name = studio[0] |
|---|
| 220 | mtime = studio[1]['Modification Time'] |
|---|
| 221 | print('"%s" last modified on %s' % (name, time.ctime(mtime))) |
|---|
| 222 | elif arg == 'alist': |
|---|
| 223 | print("--- app list") |
|---|
| 224 | for app in control_iface.GetApplicationList(): |
|---|
| 225 | print(app) |
|---|
| 226 | elif arg == 'sload': |
|---|
| 227 | print("--- studio load") |
|---|
| 228 | if index >= len(sys.argv): |
|---|
| 229 | print("load studio command requires studio name argument") |
|---|
| 230 | sys.exit() |
|---|
| 231 | |
|---|
| 232 | arg = sys.argv[index] |
|---|
| 233 | index += 1 |
|---|
| 234 | |
|---|
| 235 | open_options = {} |
|---|
| 236 | #open_options["option1"] = "asd" |
|---|
| 237 | #open_options["option2"] = True |
|---|
| 238 | |
|---|
| 239 | control_iface.LoadStudio(arg, open_options) |
|---|
| 240 | elif arg == 'sdel': |
|---|
| 241 | print("--- studio delete") |
|---|
| 242 | if index >= len(sys.argv): |
|---|
| 243 | print("delete studio command requires studio name argument") |
|---|
| 244 | sys.exit() |
|---|
| 245 | |
|---|
| 246 | arg = sys.argv[index] |
|---|
| 247 | index += 1 |
|---|
| 248 | |
|---|
| 249 | control_iface.DeleteStudio(arg) |
|---|
| 250 | elif arg == 'snew': |
|---|
| 251 | print("--- studio new") |
|---|
| 252 | name = "" |
|---|
| 253 | if index < len(sys.argv): |
|---|
| 254 | name = sys.argv[index] |
|---|
| 255 | index += 1 |
|---|
| 256 | |
|---|
| 257 | control_iface.NewStudio(name) |
|---|
| 258 | elif arg == 'sisloaded': |
|---|
| 259 | print("--- studio is loaded") |
|---|
| 260 | if control_iface.IsStudioLoaded(): |
|---|
| 261 | print("yes") |
|---|
| 262 | else: |
|---|
| 263 | print("no") |
|---|
| 264 | elif arg == 'rtlist': |
|---|
| 265 | print("--- list room templates") |
|---|
| 266 | for studio in control_iface.GetRoomTemplateList(): |
|---|
| 267 | name = studio[0] |
|---|
| 268 | print('"%s"' % name) |
|---|
| 269 | elif arg == 'rtnew': |
|---|
| 270 | print("--- create new room template") |
|---|
| 271 | if index >= len(sys.argv): |
|---|
| 272 | print("create new room template command requires room template name argument") |
|---|
| 273 | sys.exit() |
|---|
| 274 | |
|---|
| 275 | arg = sys.argv[index] |
|---|
| 276 | index += 1 |
|---|
| 277 | |
|---|
| 278 | control_iface.CreateRoomTemplate(arg) |
|---|
| 279 | elif arg == 'rtdel': |
|---|
| 280 | print("--- delete room template") |
|---|
| 281 | if index >= len(sys.argv): |
|---|
| 282 | print("delete room template command requires room template name argument") |
|---|
| 283 | sys.exit() |
|---|
| 284 | |
|---|
| 285 | arg = sys.argv[index] |
|---|
| 286 | index += 1 |
|---|
| 287 | |
|---|
| 288 | control_iface.DeleteRoomTemplate(arg) |
|---|
| 289 | else: |
|---|
| 290 | if not studio_obj: |
|---|
| 291 | studio_obj = bus.get_object(service_name, studio_object_path) |
|---|
| 292 | studio_iface = dbus.Interface(studio_obj, studio_interface_name) |
|---|
| 293 | |
|---|
| 294 | if arg == 'sname': |
|---|
| 295 | print("--- studio get name") |
|---|
| 296 | print("\"%s\"" % studio_iface.GetName()) |
|---|
| 297 | elif arg == 'ssave': |
|---|
| 298 | print("--- studio save") |
|---|
| 299 | studio_iface.Save() |
|---|
| 300 | elif arg == 'sunload': |
|---|
| 301 | print("--- studio unload") |
|---|
| 302 | studio_iface.Unload() |
|---|
| 303 | studio_obj = None |
|---|
| 304 | studio_iface = None |
|---|
| 305 | elif arg == 'srename': |
|---|
| 306 | print("--- studio rename") |
|---|
| 307 | if index >= len(sys.argv): |
|---|
| 308 | print("rename studio command requires studio name argument") |
|---|
| 309 | sys.exit() |
|---|
| 310 | |
|---|
| 311 | arg = sys.argv[index] |
|---|
| 312 | index += 1 |
|---|
| 313 | |
|---|
| 314 | studio_iface.Rename(arg) |
|---|
| 315 | elif arg == 'sstart': |
|---|
| 316 | print("--- studio start") |
|---|
| 317 | studio_iface.Start() |
|---|
| 318 | elif arg == 'sstop': |
|---|
| 319 | print("--- studio stop") |
|---|
| 320 | studio_iface.Stop() |
|---|
| 321 | elif arg == 'snewroom': |
|---|
| 322 | print("--- create new studio room") |
|---|
| 323 | if index + 1 >= len(sys.argv): |
|---|
| 324 | print("creation of studio room requires room name and room template name arguments") |
|---|
| 325 | sys.exit() |
|---|
| 326 | |
|---|
| 327 | room_name = sys.argv[index] |
|---|
| 328 | index += 1 |
|---|
| 329 | room_template_name = sys.argv[index] |
|---|
| 330 | index += 1 |
|---|
| 331 | |
|---|
| 332 | studio_iface.CreateRoom(room_name, room_template_name) |
|---|
| 333 | elif arg == 'srlist': |
|---|
| 334 | print("--- list studio rooms") |
|---|
| 335 | for room in studio_iface.GetRoomList(): |
|---|
| 336 | #print repr(room) |
|---|
| 337 | opath = room[0] |
|---|
| 338 | name = room[1]["name"] |
|---|
| 339 | if room[1].has_key("template"): |
|---|
| 340 | template = str(room[1]["template"]) |
|---|
| 341 | else: |
|---|
| 342 | template = None |
|---|
| 343 | |
|---|
| 344 | if template: |
|---|
| 345 | print('"%s" from template "%s" (%s)' % (name, template, opath)) |
|---|
| 346 | else: |
|---|
| 347 | print('"%s" (%s)' % (name, opath)) |
|---|
| 348 | elif arg == 'sdelroom': |
|---|
| 349 | print("--- delete studio room") |
|---|
| 350 | if index >= len(sys.argv): |
|---|
| 351 | print("delete studio room command requires room name argument") |
|---|
| 352 | sys.exit() |
|---|
| 353 | |
|---|
| 354 | arg = sys.argv[index] |
|---|
| 355 | index += 1 |
|---|
| 356 | |
|---|
| 357 | studio_iface.DeleteRoom(arg) |
|---|
| 358 | elif arg == 'pload': |
|---|
| 359 | print("--- load project") |
|---|
| 360 | if index + 1 >= len(sys.argv): |
|---|
| 361 | print("load project command requires room name and project dir arguments") |
|---|
| 362 | sys.exit() |
|---|
| 363 | |
|---|
| 364 | room_name = sys.argv[index] |
|---|
| 365 | index += 1 |
|---|
| 366 | project_dir = sys.argv[index] |
|---|
| 367 | index += 1 |
|---|
| 368 | |
|---|
| 369 | dbus.Interface(get_room_obj_by_name(bus, studio_iface, room_name), room_interface_name).LoadProject(project_dir) |
|---|
| 370 | elif arg == 'punload': |
|---|
| 371 | print("--- unload project") |
|---|
| 372 | if index >= len(sys.argv): |
|---|
| 373 | print("load project command requires room name argument") |
|---|
| 374 | sys.exit() |
|---|
| 375 | |
|---|
| 376 | room_name = sys.argv[index] |
|---|
| 377 | index += 1 |
|---|
| 378 | |
|---|
| 379 | dbus.Interface(get_room_obj_by_name(bus, studio_iface, room_name), room_interface_name).UnloadProject() |
|---|
| 380 | elif arg == 'psave': |
|---|
| 381 | print("--- save project") |
|---|
| 382 | if index >= len(sys.argv): |
|---|
| 383 | print("save project command requires room name argument") |
|---|
| 384 | sys.exit() |
|---|
| 385 | |
|---|
| 386 | room_name = sys.argv[index] |
|---|
| 387 | index += 1 |
|---|
| 388 | |
|---|
| 389 | dbus.Interface(get_room_obj_by_name(bus, studio_iface, room_name), room_interface_name).SaveProject("", "") |
|---|
| 390 | elif arg == 'psaveas': |
|---|
| 391 | print("--- save project as") |
|---|
| 392 | if index + 2 >= len(sys.argv): |
|---|
| 393 | print("save project as command requires room name, project dir and project name arguments") |
|---|
| 394 | sys.exit() |
|---|
| 395 | |
|---|
| 396 | room_name = sys.argv[index] |
|---|
| 397 | index += 1 |
|---|
| 398 | project_dir = sys.argv[index] |
|---|
| 399 | index += 1 |
|---|
| 400 | project_name = sys.argv[index] |
|---|
| 401 | index += 1 |
|---|
| 402 | |
|---|
| 403 | dbus.Interface(get_room_obj_by_name(bus, studio_iface, room_name), room_interface_name).SaveProject(project_dir, project_name) |
|---|
| 404 | elif arg == 'snewapp': |
|---|
| 405 | print("--- new studio app") |
|---|
| 406 | count, cmdline, name, level, term = parse_new_app_args(sys.argv[index:]) |
|---|
| 407 | index += count |
|---|
| 408 | add_app(studio_obj, cmdline, name, level, term) |
|---|
| 409 | elif arg == 'rnewapp': |
|---|
| 410 | print("--- new room app") |
|---|
| 411 | arg = sys.argv[index] |
|---|
| 412 | index += 1 |
|---|
| 413 | |
|---|
| 414 | count, cmdline, name, level, term = parse_new_app_args(sys.argv[index:]) |
|---|
| 415 | index += count |
|---|
| 416 | |
|---|
| 417 | add_app(get_room_obj_by_name(bus, studio_iface, arg), cmdline, name, level, term) |
|---|
| 418 | else: |
|---|
| 419 | print("Unknown command '%s'" % arg) |
|---|
| 420 | except dbus.DBusException, e: |
|---|
| 421 | print("DBus exception: %s" % str(e)) |
|---|
| 422 | |
|---|
| 423 | if __name__ == '__main__': |
|---|
| 424 | main() |
|---|