Properly handle stop of apps with jack clients without ports. Fix for #94
[ladish.git] / ladish_control
blobe8f5f31e4563ffbdba55f90a174fb786366eea24
1 #!/usr/bin/env python
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'
34 import sys
35 import os
36 import time
37 from traceback import print_exc
39 import dbus
41 def bool_convert(str_value):
42 if str_value.lower() == "false":
43 return False
45 if str_value.lower() == "off":
46 return False
48 if str_value.lower() == "no":
49 return False
51 if str_value == "0":
52 return False
54 if str_value.lower() == "(null)":
55 return False
57 return bool(str_value)
59 def dbus_type_to_python_type(dbus_value):
60 if type(dbus_value) == dbus.Boolean:
61 return bool(dbus_value)
62 if type(dbus_value) == dbus.Int32 or type(dbus_value) == dbus.UInt32:
63 return int(dbus_value)
64 return dbus_value
66 def dbus_type_to_type_string(dbus_value):
67 if type(dbus_value) == dbus.Boolean:
68 return "bool"
69 if type(dbus_value) == dbus.Int32:
70 return "sint"
71 if type(dbus_value) == dbus.UInt32:
72 return "uint"
73 if type(dbus_value) == dbus.Byte:
74 return "char"
75 if type(dbus_value) == dbus.String:
76 return "str"
78 return None # throw exception here?
80 def dbus_typesig_to_type_string(type_char):
81 type_char = str(type_char)
82 if type_char == 'i':
83 return "sint"
84 if type_char == 'u':
85 return "uint"
86 if type_char == 'y':
87 return "char"
88 if type_char == 's':
89 return "str"
90 if type_char == 'b':
91 return "bool"
93 print('unknown dbus typesig')
94 return None # throw exception here?
96 def main():
97 if len(sys.argv) == 1:
98 print("Usage: %s [command] [command] ..." % os.path.basename(sys.argv[0]))
99 print("Commands:")
100 print(" exit - exit ladish dbus service")
101 print(" slist - list studios")
102 print(" alist - list apps")
103 print(" sload <studioname> - load studio")
104 print(" sdel <studioname> - delete studio")
105 print(" snew [studioname] - new studio")
106 print(" sisloaded - is studio loaded?")
107 print(" sname - get studio name")
108 print(" ssave - save studio")
109 print(" sunload - unload studio")
110 print(" srename <studioname> - rename studio")
111 print(" sstart - start studio")
112 print(" sstop - stop studio")
113 print(" rtlist - list room templates")
114 print(" rtdel <roomtemplatename> - delete room template")
115 print(" rtnew <roomtemplatename> - create new room template")
116 print(" snewroom <rname> <rtname> - create new studio room")
117 print(" srlist - list studio rooms")
118 print(" sdelroom <rname> - delete studio room")
119 sys.exit(0)
121 bus = dbus.SessionBus()
122 control_obj = None
123 studio_obj = None
125 # check arguments
126 index = 1
127 while index < len(sys.argv):
128 arg = sys.argv[index]
129 index += 1
130 try:
131 if not control_obj:
132 control_obj = bus.get_object(service_name, control_object_path)
133 control_iface = dbus.Interface(control_obj, control_interface_name)
135 if arg == "exit":
136 print("--- exit")
137 control_iface.Exit()
138 time.sleep(1)
139 # we have deactivated the object and we need to get new connection if there are more commands
140 control_obj = None
141 control_iface = None
142 elif arg == 'slist':
143 print("--- studio list")
144 for studio in control_iface.GetStudioList():
145 name = studio[0]
146 mtime = studio[1]['Modification Time']
147 print('"%s" last modified on %s' % (name, time.ctime(mtime)))
148 elif arg == 'alist':
149 print("--- app list")
150 for app in control_iface.GetApplicationList():
151 print(app)
152 elif arg == 'sload':
153 print("--- studio load")
154 if index >= len(sys.argv):
155 print("load studio command requires studio name argument")
156 sys.exit()
158 arg = sys.argv[index]
159 index += 1
161 open_options = {}
162 #open_options["option1"] = "asd"
163 #open_options["option2"] = True
165 control_iface.LoadStudio(arg, open_options)
166 elif arg == 'sdel':
167 print("--- studio delete")
168 if index >= len(sys.argv):
169 print("delete studio command requires studio name argument")
170 sys.exit()
172 arg = sys.argv[index]
173 index += 1
175 control_iface.DeleteStudio(arg)
176 elif arg == 'snew':
177 print("--- studio new")
178 name = ""
179 if index < len(sys.argv):
180 name = sys.argv[index]
181 index += 1
183 control_iface.NewStudio(name)
184 elif arg == 'sisloaded':
185 print("--- studio is loaded")
186 if control_iface.IsStudioLoaded():
187 print("yes")
188 else:
189 print("no")
190 elif arg == 'rtlist':
191 print("--- list room templates")
192 for studio in control_iface.GetRoomTemplateList():
193 name = studio[0]
194 print('"%s"' % name)
195 elif arg == 'rtnew':
196 print("--- create new room template")
197 if index >= len(sys.argv):
198 print("create new room template command requires room template name argument")
199 sys.exit()
201 arg = sys.argv[index]
202 index += 1
204 control_iface.CreateRoomTemplate(arg)
205 elif arg == 'rtdel':
206 print("--- delete room template")
207 if index >= len(sys.argv):
208 print("delete room template command requires room template name argument")
209 sys.exit()
211 arg = sys.argv[index]
212 index += 1
214 control_iface.DeleteRoomTemplate(arg)
215 else:
216 if not studio_obj:
217 studio_obj = bus.get_object(service_name, studio_object_path)
218 studio_iface = dbus.Interface(studio_obj, studio_interface_name)
220 if arg == 'sname':
221 print("--- studio get name")
222 print("\"%s\"" % studio_iface.GetName())
223 elif arg == 'ssave':
224 print("--- studio save")
225 studio_iface.Save()
226 elif arg == 'sunload':
227 print("--- studio unload")
228 studio_iface.Unload()
229 studio_obj = None
230 studio_iface = None
231 elif arg == 'srename':
232 print("--- studio rename")
233 if index >= len(sys.argv):
234 print("rename studio command requires studio name argument")
235 sys.exit()
237 arg = sys.argv[index]
238 index += 1
240 studio_iface.Rename(arg)
241 elif arg == 'sstart':
242 print("--- studio start")
243 studio_iface.Start()
244 elif arg == 'sstop':
245 print("--- studio stop")
246 studio_iface.Stop()
247 elif arg == 'snewroom':
248 print("--- create new studio room")
249 if index + 1 >= len(sys.argv):
250 print("creation of studio room requires room name and room template name arguments")
251 sys.exit()
253 room_name = sys.argv[index]
254 index += 1
255 room_template_name = sys.argv[index]
256 index += 1
258 studio_iface.CreateRoom(room_name, room_template_name)
259 elif arg == 'srlist':
260 print("--- list studio rooms")
261 for room in studio_iface.GetRoomList():
262 #print repr(room)
263 opath = room[0]
264 name = room[1]["name"]
265 if room[1].has_key("template"):
266 template = str(room[1]["template"])
267 else:
268 template = None
270 if template:
271 print('"%s" from template "%s" (%s)' % (name, template, opath))
272 else:
273 print('"%s" (%s)' % (name, opath))
274 elif arg == 'sdelroom':
275 print("--- delete studio room")
276 if index >= len(sys.argv):
277 print("delete studio room command requires room name argument")
278 sys.exit()
280 arg = sys.argv[index]
281 index += 1
283 studio_iface.DeleteRoom(arg)
284 else:
285 print("Unknown command '%s'" % arg)
286 except dbus.DBusException, e:
287 print("DBus exception: %s" % str(e))
289 if __name__ == '__main__':
290 main()