3 # LADI Session Handler (ladish)
5 # Copyright (C) 2008, 2009, 2010 Nedko Arnaudov <nedko@arnaudov.name>
7 #*************************************************************************
8 # This file contains code of the commandline control app
9 #*************************************************************************
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.
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.
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.
26 service_name
= 'org.ladish'
28 control_object_path
= "/org/ladish/Control"
29 studio_object_path
= "/org/ladish/Studio"
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'
39 from traceback
import print_exc
43 def bool_convert(str_value
):
44 if str_value
.lower() == "false":
47 if str_value
.lower() == "off":
50 if str_value
.lower() == "no":
56 if str_value
.lower() == "(null)":
59 return bool(str_value
)
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
)
68 def dbus_type_to_type_string(dbus_value
):
69 if type(dbus_value
) == dbus
.Boolean
:
71 if type(dbus_value
) == dbus
.Int32
:
73 if type(dbus_value
) == dbus
.UInt32
:
75 if type(dbus_value
) == dbus
.Byte
:
77 if type(dbus_value
) == dbus
.String
:
80 return None # throw exception here?
82 def dbus_typesig_to_type_string(type_char
):
83 type_char
= str(type_char
)
95 print('unknown dbus typesig')
96 return None # throw exception here?
98 def parse_new_app_args(params_array
):
100 cmdline
= params_array
[0]
107 if index
< len(params_array
):
108 if params_array
[index
] == '-':
109 return index
+ 1, cmdline
, name
, level
, term
110 name
= params_array
[index
]
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
])
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':
127 if index
< len(params_array
) and params_array
[index
] == '-':
130 return index
, cmdline
, name
, level
, term
132 def add_app(obj
, cmdline
, name
, level
, term
):
133 dbus
.Interface(obj
, app_supervisor_interface_name
).RunCustom(term
, cmdline
, name
, level
)
135 def get_room_obj_by_name(bus
, studio_iface
, room_name
):
136 for room
in studio_iface
.GetRoomList():
139 name
= room
[1]["name"]
140 if name
== room_name
:
141 return bus
.get_object(service_name
, opath
)
144 if len(sys
.argv
) == 1:
145 argv0
= os
.path
.basename(sys
.argv
[0])
146 print("Usage: %s [command] [command] ..." % argv0
)
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)")
174 print("Add new app arguments:");
175 print(" <commandline> [<name> [<level>] [term]] [-]");
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");
185 print(" * Add to studio jack_mixer instance named \"mixer\", at level 1, without terminal");
187 print(" $ %s snewapp jack_mixer mixer 1" % argv0
);
189 print(" * Add to room \"main\" fluidjack instance named \"fluid\", at level 0, with terminal");
191 print(" $ %s rnewapp main \"fluidjack FluidR3.SF2\" fluid 0 term" % argv0
);
195 bus
= dbus
.SessionBus()
201 while index
< len(sys
.argv
):
202 arg
= sys
.argv
[index
]
206 control_obj
= bus
.get_object(service_name
, control_object_path
)
207 control_iface
= dbus
.Interface(control_obj
, control_interface_name
)
213 # we have deactivated the object and we need to get new connection if there are more commands
217 print("--- studio list")
218 for studio
in control_iface
.GetStudioList():
220 mtime
= studio
[1]['Modification Time']
221 print('"%s" last modified on %s' % (name
, time
.ctime(mtime
)))
223 print("--- app list")
224 for app
in control_iface
.GetApplicationList():
227 print("--- studio load")
228 if index
>= len(sys
.argv
):
229 print("load studio command requires studio name argument")
232 arg
= sys
.argv
[index
]
236 #open_options["option1"] = "asd"
237 #open_options["option2"] = True
239 control_iface
.LoadStudio(arg
, open_options
)
241 print("--- studio delete")
242 if index
>= len(sys
.argv
):
243 print("delete studio command requires studio name argument")
246 arg
= sys
.argv
[index
]
249 control_iface
.DeleteStudio(arg
)
251 print("--- studio new")
253 if index
< len(sys
.argv
):
254 name
= sys
.argv
[index
]
257 control_iface
.NewStudio(name
)
258 elif arg
== 'sisloaded':
259 print("--- studio is loaded")
260 if control_iface
.IsStudioLoaded():
264 elif arg
== 'rtlist':
265 print("--- list room templates")
266 for studio
in control_iface
.GetRoomTemplateList():
270 print("--- create new room template")
271 if index
>= len(sys
.argv
):
272 print("create new room template command requires room template name argument")
275 arg
= sys
.argv
[index
]
278 control_iface
.CreateRoomTemplate(arg
)
280 print("--- delete room template")
281 if index
>= len(sys
.argv
):
282 print("delete room template command requires room template name argument")
285 arg
= sys
.argv
[index
]
288 control_iface
.DeleteRoomTemplate(arg
)
291 studio_obj
= bus
.get_object(service_name
, studio_object_path
)
292 studio_iface
= dbus
.Interface(studio_obj
, studio_interface_name
)
295 print("--- studio get name")
296 print("\"%s\"" % studio_iface
.GetName())
298 print("--- studio save")
300 elif arg
== 'sunload':
301 print("--- studio unload")
302 studio_iface
.Unload()
305 elif arg
== 'srename':
306 print("--- studio rename")
307 if index
>= len(sys
.argv
):
308 print("rename studio command requires studio name argument")
311 arg
= sys
.argv
[index
]
314 studio_iface
.Rename(arg
)
315 elif arg
== 'sstart':
316 print("--- studio start")
319 print("--- studio 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")
327 room_name
= sys
.argv
[index
]
329 room_template_name
= sys
.argv
[index
]
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():
338 name
= room
[1]["name"]
339 if room
[1].has_key("template"):
340 template
= str(room
[1]["template"])
345 print('"%s" from template "%s" (%s)' % (name
, template
, opath
))
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")
354 arg
= sys
.argv
[index
]
357 studio_iface
.DeleteRoom(arg
)
359 print("--- load project")
360 if index
+ 1 >= len(sys
.argv
):
361 print("load project command requires room name and project dir arguments")
364 room_name
= sys
.argv
[index
]
366 project_dir
= sys
.argv
[index
]
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")
376 room_name
= sys
.argv
[index
]
379 dbus
.Interface(get_room_obj_by_name(bus
, studio_iface
, room_name
), room_interface_name
).UnloadProject()
381 print("--- save project")
382 if index
>= len(sys
.argv
):
383 print("save project command requires room name argument")
386 room_name
= sys
.argv
[index
]
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")
396 room_name
= sys
.argv
[index
]
398 project_dir
= sys
.argv
[index
]
400 project_name
= sys
.argv
[index
]
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
:])
408 add_app(studio_obj
, cmdline
, name
, level
, term
)
409 elif arg
== 'rnewapp':
410 print("--- new room app")
411 arg
= sys
.argv
[index
]
414 count
, cmdline
, name
, level
, term
= parse_new_app_args(sys
.argv
[index
:])
417 add_app(get_room_obj_by_name(bus
, studio_iface
, arg
), cmdline
, name
, level
, term
)
419 print("Unknown command '%s'" % arg
)
420 except dbus
.DBusException
, e
:
421 print("DBus exception: %s" % str(e
))
423 if __name__
== '__main__':