jack1 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

277 lines
8.9KB

  1. #!/usr/bin/env python
  2. import libjack
  3. import state
  4. import subprocess
  5. from optparse import OptionParser
  6. from ConfigParser import SafeConfigParser
  7. import os
  8. try:
  9. import dbus.service
  10. import gobject
  11. have_dbus = True
  12. except:
  13. have_dbus = False
  14. defaults = { "jackclientname": "sessionmanager", "sessiondir": "~/jackSessions" }
  15. class SessionManager( object ):
  16. def __init__( self ):
  17. self.config = SafeConfigParser( defaults )
  18. self.config.read( os.path.expanduser( "~/.jacksessionrc" ) )
  19. self.jackname = self.config.get( "DEFAULT", "jackclientname" )
  20. self.cl = libjack.JackClient( self.jackname )
  21. if self.config.has_section( "infra" ):
  22. self.infra_clients = {}
  23. for inf in self.config.items( "infra" ):
  24. self.infra_clients[inf[0]] = inf[1]
  25. else:
  26. self.infra_clients = { "a2j": "a2jmidid" }
  27. self.implicit_clients = [ "system" ]
  28. self.sessiondir = os.path.expanduser( self.config.get( "DEFAULT", "sessiondir" ) ) + "/"
  29. if not os.path.exists( self.sessiondir ):
  30. print "Sessiondir %s does not exist. Creating it..."%self.sessiondir
  31. os.mkdir( self.sessiondir )
  32. def list_projects( self ):
  33. files = os.listdir( self.sessiondir )
  34. files = filter( lambda x: os.path.isdir( os.path.join( self.sessiondir, x ) ), files )
  35. return files
  36. def load_session( self, name ):
  37. if not os.path.exists( self.sessiondir+name+"/session.xml" ):
  38. print "Session %s does not exist"%name
  39. return -1
  40. sd = state.SessionDom( self.sessiondir+name+"/session.xml" )
  41. g=self.cl.get_graph()
  42. children = []
  43. for ic in sd.get_infra_clients():
  44. if not ic[0] in g.clients.keys():
  45. children.append( subprocess.Popen( ic[1], shell=True ) )
  46. print sd.get_client_names()
  47. if opt.renames:
  48. g.ensure_clientnames( sd.get_reg_client_names() )
  49. # get graph again... renaming isnt prefect yet.
  50. g=self.cl.get_graph()
  51. # fixup names, doing this unconditionally, because
  52. # a client rename might have failed.
  53. sd.fixup_client_names( g )
  54. # now we have mangled all the names, lets reserve them.
  55. for (uuid, clientname) in sd.get_uuid_client_pairs():
  56. print "reserving name %s"%clientname
  57. g.reserve_name( uuid, clientname )
  58. # build up list of port connections
  59. conns = []
  60. for p in sd.get_port_names():
  61. for c in sd.get_connections_for_port( p ):
  62. conns.append( (p,c) )
  63. print conns
  64. # now fire up the processes
  65. for cname in sd.get_reg_client_names():
  66. cmd = sd.get_commandline_for_client( cname )
  67. children.append( subprocess.Popen( cmd, shell=True ) )
  68. avail_ports = g.get_port_list()
  69. # wait for ports to appear, and connect em.
  70. while len(conns) > 0:
  71. p = self.cl.port_queue.get()
  72. print p[0],p[1]
  73. pname = libjack.port_name(p[0])
  74. for c1 in conns:
  75. if c1[0]==pname:
  76. if c1[1] in avail_ports:
  77. self.cl.connect( pname, c1[1] )
  78. conns.remove( c1 )
  79. if (c1[1],c1[0]) in conns:
  80. conns.remove( (c1[1],c1[0]) )
  81. break
  82. if c1[1]==pname:
  83. if c1[0] in avail_ports:
  84. self.cl.connect( pname, c1[0] )
  85. conns.remove( c1 )
  86. if (c1[1],c1[0]) in conns:
  87. conns.remove( (c1[1],c1[0]) )
  88. break
  89. avail_ports.append( pname )
  90. print "session restored..."
  91. return 0
  92. def quit_session( self, name ):
  93. self.save_session( name, True )
  94. def save_session( self, name, quit=False ):
  95. if os.path.exists( self.sessiondir+name ):
  96. print "session %s already exists"%name
  97. return -1
  98. os.mkdir( self.sessiondir+name )
  99. g=self.cl.get_graph()
  100. if quit:
  101. notify = self.cl.session_save_and_quit( self.sessiondir+name+"/" )
  102. else:
  103. notify = self.cl.session_save( self.sessiondir+name+"/" )
  104. for n in notify:
  105. c = g.get_client( n.clientname )
  106. c.set_commandline( n.commandline )
  107. c.set_uuid( n.uuid )
  108. sd = state.SessionDom()
  109. for c in g.clients.values():
  110. if c.get_commandline() == "":
  111. if not c.name in self.infra_clients.keys()+self.implicit_clients:
  112. g.remove_client( c.name )
  113. elif c.name in self.implicit_clients:
  114. g.remove_client_only( c.name )
  115. else:
  116. c.set_infra( self.infra_clients[c.name] )
  117. for i in g.clients.values():
  118. sd.add_client(i)
  119. f = file( self.sessiondir+name+"/session.xml", "w" )
  120. f.write( sd.get_xml() )
  121. f.close()
  122. print sd.get_xml()
  123. return 0
  124. def exit( self ):
  125. self.cl.close()
  126. if have_dbus:
  127. class DbusSM( dbus.service.Object ):
  128. def __init__( self, sm ):
  129. self.sm = sm
  130. dbus.service.Object.__init__( self, None,
  131. "/org/jackaudio/sessionmanager",
  132. dbus.service.BusName( "org.jackaudio.sessionmanager", bus=dbus.SessionBus() ) )
  133. @dbus.service.method( dbus_interface="org.jackaudio.sessionmanager",
  134. in_signature="s", out_signature="i" )
  135. def save_as( self, name ):
  136. return self.sm.save_session( name )
  137. @dbus.service.method( dbus_interface="org.jackaudio.sessionmanager",
  138. in_signature="s", out_signature="i" )
  139. def quit_as( self, name ):
  140. return self.sm.quit_session( name )
  141. @dbus.service.method( dbus_interface="org.jackaudio.sessionmanager",
  142. in_signature="s", out_signature="i" )
  143. def load( self, name ):
  144. return self.sm.load_session( name )
  145. @dbus.service.method( dbus_interface="org.jackaudio.sessionmanager",
  146. in_signature="", out_signature="as" )
  147. def list( self ):
  148. return self.sm.list_projects()
  149. @dbus.service.method( dbus_interface="org.jackaudio.sessionmanager",
  150. in_signature="", out_signature="" )
  151. def daemon_quit( self ):
  152. loop.quit()
  153. oparser = OptionParser()
  154. oparser.add_option( "--nodbus", action="store_false", dest="dbus", default=have_dbus,
  155. help="Dont use DBUS to issue commands to a running instance" )
  156. oparser.add_option( "--daemon", action="store_true", dest="daemon", default=False,
  157. help="Start in daemon mode, and listen for dbus commands" )
  158. #oparser.add_option( "--save", action="store_true", dest="save", default=False,
  159. # help="Tell SessionManger to save." )
  160. oparser.add_option( "--saveas", action="store", type="string", dest="saveas",
  161. help="Save Session As <name>" )
  162. #oparser.add_option( "--quit", action="store_true", dest="quit", default=False,
  163. # help="Tell SessionManager to Save And Quit" )
  164. oparser.add_option( "--list", action="store_true", dest="list", default=False,
  165. help="List Projects" )
  166. oparser.add_option( "--quitdaemon", action="store_true", dest="quitdaemon", default=False,
  167. help="Tell SessionManager Daemon to Exit" )
  168. oparser.add_option( "--quitas", action="store", dest="quitas", type="string",
  169. help="SaveAs And Quit" )
  170. oparser.add_option( "--load", action="store", dest="load", type="string",
  171. help="Load Session with <name>" )
  172. oparser.add_option( "--renames", action="store_true", dest="renames", default=False,
  173. help="Allow renaming offending clients" )
  174. (opt,args) = oparser.parse_args()
  175. if not opt.dbus:
  176. sm = SessionManager()
  177. try:
  178. if opt.saveas:
  179. sm.save_session( opt.saveas )
  180. if opt.load:
  181. sm.load_session( opt.load )
  182. if opt.quitas:
  183. sm.quit_session( opt.quitas )
  184. except:
  185. sm.exit()
  186. raise
  187. sm.exit()
  188. else:
  189. if opt.daemon:
  190. try:
  191. sm = SessionManager()
  192. from dbus.mainloop.glib import DBusGMainLoop
  193. DBusGMainLoop(set_as_default=True)
  194. dbsm = DbusSM( sm )
  195. loop = gobject.MainLoop()
  196. loop.run()
  197. except:
  198. sm.exit()
  199. raise
  200. sm.exit()
  201. else:
  202. session_bus = dbus.SessionBus()
  203. sm_proxy = session_bus.get_object( "org.jackaudio.sessionmanager", "/org/jackaudio/sessionmanager" )
  204. sm_iface = dbus.Interface( sm_proxy, "org.jackaudio.sessionmanager" )
  205. if opt.saveas:
  206. sm_iface.save_as( opt.saveas )
  207. if opt.quitas:
  208. sm_iface.quit_as( opt.quitas )
  209. if opt.load:
  210. sm_iface.load( opt.load )
  211. if opt.list:
  212. projects = sm_iface.list()
  213. for i in projects:
  214. print i
  215. if opt.quitdaemon:
  216. sm_iface.quit()