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.

242 lines
6.6KB

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