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.

168 lines
4.3KB

  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. SESSION_PATH="/home/torbenh/jackSessions/"
  9. defaults = { "jackclientname": "sessionmanager", "sessiondir": "~/jackSessions" }
  10. class SessionManager( object ):
  11. def __init__( self ):
  12. self.config = SafeConfigParser( defaults )
  13. self.config.read( os.path.expanduser( "~/.jacksessionrc" ) )
  14. self.jackname = self.config.get( "DEFAULT", "jackclientname" )
  15. self.cl = libjack.JackClient( self.jackname )
  16. if self.config.has_section( "infra" ):
  17. self.infra_clients = {}
  18. for inf in self.config.items( "infra" ):
  19. self.infra_clients[inf[0]] = inf[1]
  20. else:
  21. self.infra_clients = { "a2j": "a2jmidid" }
  22. self.implicit_clients = [ "system" ]
  23. self.sessiondir = os.path.expanduser( self.config.get( "DEFAULT", "sessiondir" ) ) + "/"
  24. if not os.path.exists( self.sessiondir ):
  25. print "Sessiondir %s does not exist. Creating it..."%self.sessiondir
  26. os.mkdir( self.sessiondir )
  27. def load_session( self, name ):
  28. if not os.path.exists( self.sessiondir+name+"/session.xml" ):
  29. print "Session %s does not exist"%name
  30. return
  31. sd = state.SessionDom( self.sessiondir+name+"/session.xml" )
  32. g=self.cl.get_graph()
  33. children = []
  34. for ic in sd.get_infra_clients():
  35. if not ic[0] in g.clients.keys():
  36. children.append( subprocess.Popen( ic[1], shell=True ) )
  37. print sd.get_client_names()
  38. g.ensure_clientnames( sd.get_reg_client_names() )
  39. # get graph again... renaming isnt prefect yet.
  40. g=self.cl.get_graph()
  41. # build up list of port connections
  42. conns = []
  43. for p in sd.get_port_names():
  44. for c in sd.get_connections_for_port( p ):
  45. conns.append( (p,c) )
  46. print conns
  47. # now fire up the processes
  48. for cname in sd.get_reg_client_names():
  49. cmd = sd.get_commandline_for_client( cname )
  50. children.append( subprocess.Popen( cmd, shell=True ) )
  51. avail_ports = g.get_port_list()
  52. # wait for ports to appear, and connect em.
  53. while len(conns) > 0:
  54. p = self.cl.port_queue.get()
  55. print p[0],p[1]
  56. pname = libjack.port_name(p[0])
  57. for c1 in conns:
  58. if c1[0]==pname:
  59. if c1[1] in avail_ports:
  60. self.cl.connect( pname, c1[1] )
  61. conns.remove( c1 )
  62. if (c1[1],c1[0]) in conns:
  63. conns.remove( (c1[1],c1[0]) )
  64. break
  65. if c1[1]==pname:
  66. if c1[0] in avail_ports:
  67. self.cl.connect( pname, c1[0] )
  68. conns.remove( c1 )
  69. if (c1[1],c1[0]) in conns:
  70. conns.remove( (c1[1],c1[0]) )
  71. break
  72. avail_ports.append( pname )
  73. print "session restored..."
  74. def save_session( self, name ):
  75. if os.path.exists( self.sessiondir+name ):
  76. print "session %s already exists"
  77. return
  78. os.mkdir( self.sessiondir+name )
  79. g=self.cl.get_graph()
  80. notify = self.cl.session_save( self.sessiondir+name+"/" )
  81. for n in notify:
  82. c = g.get_client( n.clientname )
  83. c.set_commandline( n.commandline )
  84. sd = state.SessionDom()
  85. for c in g.clients.values():
  86. if c.get_commandline() == "":
  87. if not c.name in self.infra_clients.keys()+self.implicit_clients:
  88. g.remove_client( c.name )
  89. elif c.name in self.implicit_clients:
  90. g.remove_client_only( c.name )
  91. else:
  92. c.set_infra( self.infra_clients[c.name] )
  93. for i in g.clients.values():
  94. sd.add_client(i)
  95. f = file( self.sessiondir+name+"/session.xml", "w" )
  96. f.write( sd.get_xml() )
  97. f.close()
  98. print sd.get_xml()
  99. def exit( self ):
  100. self.cl.close()
  101. oparser = OptionParser()
  102. #oparser.add_option( "--dbus", action="store_true", dest="dbus", default=False,
  103. # help="Use DBUS to issue commands to a running instance" )
  104. #oparser.add_option( "--save", action="store_true", dest="save", default=False,
  105. # help="Tell SessionManger to save." )
  106. oparser.add_option( "--saveas", action="store", type="string", dest="saveas",
  107. help="Save Session As <name>" )
  108. #oparser.add_option( "--quit", action="store_true", dest="quit", default=False,
  109. # help="Tell SessionManager to Save And Quit" )
  110. #oparser.add_option( "--quitas", action="store", dest="quitas", type="string",
  111. # help="SaveAs And Quit" )
  112. oparser.add_option( "--load", action="store", dest="load", type="string",
  113. help="Load Session with <name>" )
  114. (opt,args) = oparser.parse_args()
  115. sm = SessionManager()
  116. if opt.saveas:
  117. sm.save_session( opt.saveas )
  118. if opt.load:
  119. sm.load_session( opt.load )
  120. sm.exit()