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.

240 lines
6.5KB

  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. g.ensure_clientnames( sd.get_reg_client_names() )
  48. # get graph again... renaming isnt prefect yet.
  49. g=self.cl.get_graph()
  50. # build up list of port connections
  51. conns = []
  52. for p in sd.get_port_names():
  53. for c in sd.get_connections_for_port( p ):
  54. conns.append( (p,c) )
  55. print conns
  56. # now fire up the processes
  57. for cname in sd.get_reg_client_names():
  58. cmd = sd.get_commandline_for_client( cname )
  59. children.append( subprocess.Popen( cmd, shell=True ) )
  60. avail_ports = g.get_port_list()
  61. # wait for ports to appear, and connect em.
  62. while len(conns) > 0:
  63. p = self.cl.port_queue.get()
  64. print p[0],p[1]
  65. pname = libjack.port_name(p[0])
  66. for c1 in conns:
  67. if c1[0]==pname:
  68. if c1[1] in avail_ports:
  69. self.cl.connect( pname, c1[1] )
  70. conns.remove( c1 )
  71. if (c1[1],c1[0]) in conns:
  72. conns.remove( (c1[1],c1[0]) )
  73. break
  74. if c1[1]==pname:
  75. if c1[0] in avail_ports:
  76. self.cl.connect( pname, c1[0] )
  77. conns.remove( c1 )
  78. if (c1[1],c1[0]) in conns:
  79. conns.remove( (c1[1],c1[0]) )
  80. break
  81. avail_ports.append( pname )
  82. print "session restored..."
  83. return 0
  84. def save_session( self, name ):
  85. if os.path.exists( self.sessiondir+name ):
  86. print "session %s already exists"
  87. return -1
  88. os.mkdir( self.sessiondir+name )
  89. g=self.cl.get_graph()
  90. notify = self.cl.session_save( self.sessiondir+name+"/" )
  91. for n in notify:
  92. c = g.get_client( n.clientname )
  93. c.set_commandline( n.commandline )
  94. sd = state.SessionDom()
  95. for c in g.clients.values():
  96. if c.get_commandline() == "":
  97. if not c.name in self.infra_clients.keys()+self.implicit_clients:
  98. g.remove_client( c.name )
  99. elif c.name in self.implicit_clients:
  100. g.remove_client_only( c.name )
  101. else:
  102. c.set_infra( self.infra_clients[c.name] )
  103. for i in g.clients.values():
  104. sd.add_client(i)
  105. f = file( self.sessiondir+name+"/session.xml", "w" )
  106. f.write( sd.get_xml() )
  107. f.close()
  108. print sd.get_xml()
  109. return 0
  110. def exit( self ):
  111. self.cl.close()
  112. if have_dbus:
  113. class DbusSM( dbus.service.Object ):
  114. def __init__( self, sm ):
  115. self.sm = sm
  116. dbus.service.Object.__init__( self, None,
  117. "/org/jackaudio/sessionmanager",
  118. dbus.service.BusName( "org.jackaudio.sessionmanager", bus=dbus.SessionBus() ) )
  119. @dbus.service.method( dbus_interface="org.jackaudio.sessionmanager",
  120. in_signature="s", out_signature="i" )
  121. def save_as( self, name ):
  122. return self.sm.save_session( name )
  123. @dbus.service.method( dbus_interface="org.jackaudio.sessionmanager",
  124. in_signature="s", out_signature="i" )
  125. def load( self, name ):
  126. return self.sm.load_session( name )
  127. @dbus.service.method( dbus_interface="org.jackaudio.sessionmanager",
  128. in_signature="", out_signature="as" )
  129. def list( self ):
  130. return self.sm.list_projects()
  131. @dbus.service.method( dbus_interface="org.jackaudio.sessionmanager",
  132. in_signature="", out_signature="" )
  133. def daemon_quit( self ):
  134. loop.quit()
  135. oparser = OptionParser()
  136. oparser.add_option( "--nodbus", action="store_false", dest="dbus", default=have_dbus,
  137. help="Dont use DBUS to issue commands to a running instance" )
  138. oparser.add_option( "--daemon", action="store_true", dest="daemon", default=False,
  139. help="Start in daemon mode, and listen for dbus commands" )
  140. #oparser.add_option( "--save", action="store_true", dest="save", default=False,
  141. # help="Tell SessionManger to save." )
  142. oparser.add_option( "--saveas", action="store", type="string", dest="saveas",
  143. help="Save Session As <name>" )
  144. #oparser.add_option( "--quit", action="store_true", dest="quit", default=False,
  145. # help="Tell SessionManager to Save And Quit" )
  146. oparser.add_option( "--list", action="store_true", dest="list", default=False,
  147. help="List Projects" )
  148. oparser.add_option( "--quitdaemon", action="store_true", dest="quitdaemon", default=False,
  149. help="Tell SessionManager Daemon to Exit" )
  150. #oparser.add_option( "--quitas", action="store", dest="quitas", type="string",
  151. # help="SaveAs And Quit" )
  152. oparser.add_option( "--load", action="store", dest="load", type="string",
  153. help="Load Session with <name>" )
  154. (opt,args) = oparser.parse_args()
  155. if not opt.dbus:
  156. sm = SessionManager()
  157. if opt.saveas:
  158. sm.save_session( opt.saveas )
  159. if opt.load:
  160. sm.load_session( opt.load )
  161. sm.exit()
  162. else:
  163. if opt.daemon:
  164. sm = SessionManager()
  165. from dbus.mainloop.glib import DBusGMainLoop
  166. DBusGMainLoop(set_as_default=True)
  167. dbsm = DbusSM( sm )
  168. loop = gobject.MainLoop()
  169. loop.run()
  170. sm.exit()
  171. else:
  172. session_bus = dbus.SessionBus()
  173. sm_proxy = session_bus.get_object( "org.jackaudio.sessionmanager", "/org/jackaudio/sessionmanager" )
  174. sm_iface = dbus.Interface( sm_proxy, "org.jackaudio.sessionmanager" )
  175. if opt.saveas:
  176. sm_iface.save_as( opt.saveas )
  177. if opt.load:
  178. sm_iface.load( opt.load )
  179. if opt.list:
  180. projects = sm_iface.list()
  181. for i in projects:
  182. print i
  183. if opt.quitdaemon:
  184. sm_iface.quit()