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.

252 lines
8.4KB

  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 save_session( self, name ):
  93. if os.path.exists( self.sessiondir+name ):
  94. print "session %s already exists"%name
  95. return -1
  96. os.mkdir( self.sessiondir+name )
  97. g=self.cl.get_graph()
  98. notify = self.cl.session_save( self.sessiondir+name+"/" )
  99. for n in notify:
  100. c = g.get_client( n.clientname )
  101. c.set_commandline( n.commandline )
  102. c.set_uuid( n.uuid )
  103. sd = state.SessionDom()
  104. for c in g.clients.values():
  105. if c.get_commandline() == "":
  106. if not c.name in self.infra_clients.keys()+self.implicit_clients:
  107. g.remove_client( c.name )
  108. elif c.name in self.implicit_clients:
  109. g.remove_client_only( c.name )
  110. else:
  111. c.set_infra( self.infra_clients[c.name] )
  112. for i in g.clients.values():
  113. sd.add_client(i)
  114. f = file( self.sessiondir+name+"/session.xml", "w" )
  115. f.write( sd.get_xml() )
  116. f.close()
  117. print sd.get_xml()
  118. return 0
  119. def exit( self ):
  120. self.cl.close()
  121. if have_dbus:
  122. class DbusSM( dbus.service.Object ):
  123. def __init__( self, sm ):
  124. self.sm = sm
  125. dbus.service.Object.__init__( self, None,
  126. "/org/jackaudio/sessionmanager",
  127. dbus.service.BusName( "org.jackaudio.sessionmanager", bus=dbus.SessionBus() ) )
  128. @dbus.service.method( dbus_interface="org.jackaudio.sessionmanager",
  129. in_signature="s", out_signature="i" )
  130. def save_as( self, name ):
  131. return self.sm.save_session( name )
  132. @dbus.service.method( dbus_interface="org.jackaudio.sessionmanager",
  133. in_signature="s", out_signature="i" )
  134. def load( self, name ):
  135. return self.sm.load_session( name )
  136. @dbus.service.method( dbus_interface="org.jackaudio.sessionmanager",
  137. in_signature="", out_signature="as" )
  138. def list( self ):
  139. return self.sm.list_projects()
  140. @dbus.service.method( dbus_interface="org.jackaudio.sessionmanager",
  141. in_signature="", out_signature="" )
  142. def daemon_quit( self ):
  143. loop.quit()
  144. oparser = OptionParser()
  145. oparser.add_option( "--nodbus", action="store_false", dest="dbus", default=have_dbus,
  146. help="Dont use DBUS to issue commands to a running instance" )
  147. oparser.add_option( "--daemon", action="store_true", dest="daemon", default=False,
  148. help="Start in daemon mode, and listen for dbus commands" )
  149. #oparser.add_option( "--save", action="store_true", dest="save", default=False,
  150. # help="Tell SessionManger to save." )
  151. oparser.add_option( "--saveas", action="store", type="string", dest="saveas",
  152. help="Save Session As <name>" )
  153. #oparser.add_option( "--quit", action="store_true", dest="quit", default=False,
  154. # help="Tell SessionManager to Save And Quit" )
  155. oparser.add_option( "--list", action="store_true", dest="list", default=False,
  156. help="List Projects" )
  157. oparser.add_option( "--quitdaemon", action="store_true", dest="quitdaemon", default=False,
  158. help="Tell SessionManager Daemon to Exit" )
  159. #oparser.add_option( "--quitas", action="store", dest="quitas", type="string",
  160. # help="SaveAs And Quit" )
  161. oparser.add_option( "--load", action="store", dest="load", type="string",
  162. help="Load Session with <name>" )
  163. oparser.add_option( "--renames", action="store_true", dest="renames", default=False,
  164. help="Allow renaming offending clients" )
  165. (opt,args) = oparser.parse_args()
  166. if not opt.dbus:
  167. sm = SessionManager()
  168. if opt.saveas:
  169. sm.save_session( opt.saveas )
  170. if opt.load:
  171. sm.load_session( opt.load )
  172. sm.exit()
  173. else:
  174. if opt.daemon:
  175. sm = SessionManager()
  176. from dbus.mainloop.glib import DBusGMainLoop
  177. DBusGMainLoop(set_as_default=True)
  178. dbsm = DbusSM( sm )
  179. loop = gobject.MainLoop()
  180. loop.run()
  181. sm.exit()
  182. else:
  183. session_bus = dbus.SessionBus()
  184. sm_proxy = session_bus.get_object( "org.jackaudio.sessionmanager", "/org/jackaudio/sessionmanager" )
  185. sm_iface = dbus.Interface( sm_proxy, "org.jackaudio.sessionmanager" )
  186. if opt.saveas:
  187. sm_iface.save_as( opt.saveas )
  188. if opt.load:
  189. sm_iface.load( opt.load )
  190. if opt.list:
  191. projects = sm_iface.list()
  192. for i in projects:
  193. print i
  194. if opt.quitdaemon:
  195. sm_iface.quit()