+ JACK host: implement '-M index' option in JACK1-compatible way
[calf.git] / bigbull / jackvis.py
blob795e7cfc31d0bbc761d9dd74cd77a7a7e5d363ed
1 #!/usr/bin/python
2 import pygtk
3 pygtk.require('2.0')
4 import gtk
5 import calfpytools
6 import conndiagram
7 from calfgtkutils import *
8 Colors = conndiagram.Colors
10 class JACKGraphParser():
11 def __init__(self):
12 self.client = calfpytools.JackClient()
13 self.client.open("jackvis")
14 def get_port_color(self, portData):
15 if portData.get_type() == calfpytools.JACK_DEFAULT_AUDIO_TYPE:
16 color = Colors.audioPort
17 if portData.get_type() == calfpytools.JACK_DEFAULT_MIDI_TYPE:
18 color = Colors.eventPort
19 return color
20 def is_port_input(self, portData):
21 return (portData.get_flags() & calfpytools.JackPortIsInput) != 0
22 def get_port_name(self, portData):
23 return portData.get_name()
24 def get_port_id(self, portData):
25 return portData.get_full_name()
26 def get_module_name(self, moduleData):
27 return moduleData.name
28 def get_module_port_list(self, moduleData):
29 return [(port, self.client.get_port(port)) for port in self.client.get_ports(moduleData.name+":.*", moduleData.type, moduleData.flags)]
30 def can_connect(self, first, second):
31 if self.is_port_input(first) == self.is_port_input(second):
32 return False
33 if first.get_type() != second.get_type():
34 return False
35 return True
36 def connect(self, first, second):
37 if self.client.connect(first.get_full_name(), second.get_full_name()) != True:
38 raise RuntimeError, "Connection error"
39 def disconnect(self, name_first, name_second):
40 self.client.disconnect(name_first, name_second)
42 class JACKClientData():
43 def __init__(self, name, type, flags):
44 (self.name, self.type, self.flags) = (name, type, flags)
46 class App:
47 def __init__(self):
48 self.parser = JACKGraphParser()
49 self.cgraph = conndiagram.ConnectionGraphEditor(self, self.parser)
51 def canvas_popup_menu_handler(self, widget):
52 self.canvas_popup_menu(0, 0, 0)
53 return True
55 def canvas_button_press_handler(self, widget, event):
56 if event.button == 3:
57 self.canvas_popup_menu(event.x, event.y, event.time)
58 return True
60 def canvas_popup_menu(self, x, y, time):
61 menu = gtk.Menu()
62 items = self.cgraph.get_data_items_at(x, y)
63 types = set([di[0] for di in items])
64 if 'wire' in types:
65 for i in items:
66 if i[0] == "wire":
67 wire = i[1]
68 add_option(menu, "Disconnect", self.disconnect, wire)
69 else:
70 return
71 menu.show_all()
72 menu.popup(None, None, None, 3, time)
74 def disconnect(self, wire):
75 self.parser.disconnect(wire.src.get_id(), wire.dest.get_id())
76 wire.delete()
78 def create(self):
79 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
80 self.window.connect("delete_event", self.delete_event)
81 self.window.connect("destroy", self.destroy)
82 self.main_vbox = gtk.VBox()
83 self.create_main_menu()
84 self.scroll = gtk.ScrolledWindow()
85 self.scroll.add(self.cgraph.create())
86 self.main_vbox.pack_start(self.menu_bar, expand = False, fill = False)
87 self.main_vbox.add(self.scroll)
88 self.window.add(self.main_vbox)
89 self.window.show_all()
90 self.main_vbox.connect("popup-menu", self.canvas_popup_menu_handler)
91 self.cgraph.canvas.connect("button-press-event", self.canvas_button_press_handler)
92 self.cgraph.canvas.update()
93 self.add_clients(0.0, 0.0, calfpytools.JackPortIsOutput)
94 self.add_clients(200, 0.0, calfpytools.JackPortIsInput)
95 self.cgraph.canvas.update()
96 self.add_wires()
97 self.cgraph.blow_up()
99 def add_clients(self, x, y, flags):
100 ports = self.parser.client.get_ports("", "", flags)
101 clients = set([p.split(":")[0] for p in ports])
102 for cl in clients:
103 y += self.cgraph.add_module(JACKClientData(cl, "", flags), x, y).rect.props.height
105 def add_wires(self):
106 ports = self.parser.client.get_ports("", "", calfpytools.JackPortIsInput)
107 pmap = self.cgraph.get_port_map()
108 for p in ports:
109 conns = self.parser.client.get_port(p).get_connections()
110 for c in conns:
111 if p in pmap and c in pmap:
112 print "Connect %s to %s" % (c, p)
113 self.cgraph.connect(pmap[c], pmap[p])
114 else:
115 print "Connect %s to %s - not found" % (c, p)
117 def create_main_menu(self):
118 self.menu_bar = gtk.MenuBar()
119 self.file_menu = add_submenu(self.menu_bar, "_File")
120 add_option(self.file_menu, "_Blow-up", self.blow_up)
121 add_option(self.file_menu, "_Exit", self.exit)
123 def blow_up(self, data):
124 self.cgraph.blow_up()
126 def exit(self, data):
127 self.window.destroy()
129 def delete_event(self, widget, data = None):
130 gtk.main_quit()
131 def destroy(self, widget, data = None):
132 gtk.main_quit()
134 app = App()
135 app.create()
136 gtk.main()