]> git.0d.be Git - jack_mixer.git/blob - jack_mixer.py
Merge branch 'master' of ssh://repo.or.cz/srv/git/jack_mixer
[jack_mixer.git] / jack_mixer.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # This file is part of jack_mixer
5 #
6 # Copyright (C) 2006-2009 Nedko Arnaudov <nedko@arnaudov.name>
7 # Copyright (C) 2009 Frederic Peters <fpeters@0d.be>
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; version 2 of the License
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 from optparse import OptionParser
23
24 import gtk
25 import gobject
26 import sys
27 import os
28 import signal
29
30 try:
31     import lash
32 except:
33     lash = None
34     print >> sys.stderr, "Cannot load LASH python bindings, you want them unless you enjoy manual jack plumbing each time you use this app"
35
36 # temporary change Python modules lookup path to look into installation
37 # directory ($prefix/share/jack_mixer/)
38 old_path = sys.path
39 sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), '..', 'share', 'jack_mixer'))
40
41 import jack_mixer_c
42 import scale
43 from channel import *
44
45 import gui
46 from preferences import PreferencesDialog
47
48 from serialization_xml import XmlSerialization
49 from serialization import SerializedObject, Serializator
50
51 # restore Python modules lookup path
52 sys.path = old_path
53
54 class JackMixer(SerializedObject):
55
56     # scales suitable as meter scales
57     meter_scales = [scale.IEC268(), scale.Linear70dB(), scale.IEC268Minimalistic()]
58
59     # scales suitable as volume slider scales
60     slider_scales = [scale.Linear30dB(), scale.Linear70dB()]
61
62     # name of settngs file that is currently open
63     current_filename = None
64
65     def __init__(self, name, lash_client):
66         self.mixer = jack_mixer_c.Mixer(name)
67         if not self.mixer:
68             return
69         self.monitor_channel = self.mixer.add_output_channel("Monitor", True, True)
70
71         self.save = False
72
73         if lash_client:
74             # Send our client name to server
75             lash_event = lash.lash_event_new_with_type(lash.LASH_Client_Name)
76             lash.lash_event_set_string(lash_event, name)
77             lash.lash_send_event(lash_client, lash_event)
78
79             lash.lash_jack_client_name(lash_client, name)
80
81         gtk.window_set_default_icon_name('jack_mixer')
82
83         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
84         self.window.set_title(name)
85
86         self.gui_factory = gui.Factory(self.window, self.meter_scales, self.slider_scales)
87
88         self.vbox_top = gtk.VBox()
89         self.window.add(self.vbox_top)
90
91         self.menubar = gtk.MenuBar()
92         self.vbox_top.pack_start(self.menubar, False)
93
94         mixer_menu_item = gtk.MenuItem("_Mixer")
95         self.menubar.append(mixer_menu_item)
96         edit_menu_item = gtk.MenuItem('_Edit')
97         self.menubar.append(edit_menu_item)
98         help_menu_item = gtk.MenuItem('_Help')
99         self.menubar.append(help_menu_item)
100
101         self.window.set_default_size(120, 300)
102
103         mixer_menu = gtk.Menu()
104         mixer_menu_item.set_submenu(mixer_menu)
105
106         add_input_channel = gtk.ImageMenuItem('New _Input Channel')
107         mixer_menu.append(add_input_channel)
108         add_input_channel.connect("activate", self.on_add_input_channel)
109
110         add_output_channel = gtk.ImageMenuItem('New _Output Channel')
111         mixer_menu.append(add_output_channel)
112         add_output_channel.connect("activate", self.on_add_output_channel)
113
114         mixer_menu.append(gtk.SeparatorMenuItem())
115         open = gtk.ImageMenuItem(gtk.STOCK_OPEN)
116         mixer_menu.append(open)
117         open.connect('activate', self.on_open_cb)
118         save = gtk.ImageMenuItem(gtk.STOCK_SAVE)
119         mixer_menu.append(save)
120         save.connect('activate', self.on_save_cb)
121         save_as = gtk.ImageMenuItem(gtk.STOCK_SAVE_AS)
122         mixer_menu.append(save_as)
123         save_as.connect('activate', self.on_save_as_cb)
124
125         mixer_menu.append(gtk.SeparatorMenuItem())
126
127         quit = gtk.ImageMenuItem(gtk.STOCK_QUIT)
128         mixer_menu.append(quit)
129         quit.connect('activate', self.on_quit_cb)
130
131         edit_menu = gtk.Menu()
132         edit_menu_item.set_submenu(edit_menu)
133
134         self.channel_edit_input_menu_item = gtk.MenuItem('_Edit Input Channel')
135         edit_menu.append(self.channel_edit_input_menu_item)
136         self.channel_edit_input_menu = gtk.Menu()
137         self.channel_edit_input_menu_item.set_submenu(self.channel_edit_input_menu)
138
139         self.channel_edit_output_menu_item = gtk.MenuItem('Edit _Output Channel')
140         edit_menu.append(self.channel_edit_output_menu_item)
141         self.channel_edit_output_menu = gtk.Menu()
142         self.channel_edit_output_menu_item.set_submenu(self.channel_edit_output_menu)
143
144         self.channel_remove_input_menu_item = gtk.MenuItem('Remove _Input Channel')
145         edit_menu.append(self.channel_remove_input_menu_item)
146         self.channel_remove_input_menu = gtk.Menu()
147         self.channel_remove_input_menu_item.set_submenu(self.channel_remove_input_menu)
148
149         self.channel_remove_output_menu_item = gtk.MenuItem('_Remove Output Channel')
150         edit_menu.append(self.channel_remove_output_menu_item)
151         self.channel_remove_output_menu = gtk.Menu()
152         self.channel_remove_output_menu_item.set_submenu(self.channel_remove_output_menu)
153
154         channel_remove_all_menu_item = gtk.ImageMenuItem(gtk.STOCK_CLEAR)
155         edit_menu.append(channel_remove_all_menu_item)
156         channel_remove_all_menu_item.connect("activate", self.on_channels_clear)
157
158         edit_menu.append(gtk.SeparatorMenuItem())
159
160         preferences = gtk.ImageMenuItem(gtk.STOCK_PREFERENCES)
161         preferences.connect('activate', self.on_preferences_cb)
162         edit_menu.append(preferences)
163
164         help_menu = gtk.Menu()
165         help_menu_item.set_submenu(help_menu)
166
167         about = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
168         help_menu.append(about)
169         about.connect("activate", self.on_about)
170
171         self.hbox_top = gtk.HBox()
172         self.vbox_top.pack_start(self.hbox_top, True)
173
174         self.scrolled_window = gtk.ScrolledWindow()
175         self.hbox_top.pack_start(self.scrolled_window, True)
176
177         self.hbox_inputs = gtk.HBox()
178         self.hbox_inputs.set_spacing(0)
179         self.hbox_inputs.set_border_width(0)
180         self.hbox_top.set_spacing(0)
181         self.hbox_top.set_border_width(0)
182         self.channels = []
183         self.output_channels = []
184
185         self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
186         self.scrolled_window.add_with_viewport(self.hbox_inputs)
187
188         self.main_mix = MainMixChannel(self)
189         self.hbox_outputs = gtk.HBox()
190         self.hbox_outputs.set_spacing(0)
191         self.hbox_outputs.set_border_width(0)
192         frame = gtk.Frame()
193         frame.add(self.main_mix)
194         self.hbox_outputs.pack_start(frame, False)
195         self.hbox_top.pack_start(self.hbox_outputs, False)
196
197         self.window.connect("destroy", gtk.main_quit)
198
199         gobject.timeout_add(80, self.read_meters)
200         self.lash_client = lash_client
201
202         gobject.timeout_add(200, self.lash_check_events)
203
204     def sighandler(self, signum, frame):
205         #print "Signal %d received" % signum
206         if signum == signal.SIGUSR1:
207             self.save = True
208         elif signum == signal.SIGTERM:
209             gtk.main_quit()
210         elif signum == signal.SIGINT:
211             gtk.main_quit()
212         else:
213             print "Unknown signal %d received" % signum
214
215     def cleanup(self):
216         print "Cleaning jack_mixer"
217         if not self.mixer:
218             return
219
220         for channel in self.channels:
221             channel.unrealize()
222
223     def on_open_cb(self, *args):
224         dlg = gtk.FileChooserDialog(title='Open', parent=self.window,
225                         action=gtk.FILE_CHOOSER_ACTION_OPEN,
226                         buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
227                                  gtk.STOCK_OPEN, gtk.RESPONSE_OK))
228         dlg.set_default_response(gtk.RESPONSE_OK)
229         if dlg.run() == gtk.RESPONSE_OK:
230             filename = dlg.get_filename()
231             try:
232                 f = file(filename, 'r')
233                 self.load_from_xml(f)
234             except:
235                 err = gtk.MessageDialog(self.window,
236                             gtk.DIALOG_MODAL,
237                             gtk.MESSAGE_ERROR,
238                             gtk.BUTTONS_OK,
239                             "Failed loading settings.")
240                 err.run()
241                 err.destroy()
242             else:
243                 self.current_filename = filename
244             finally:
245                 f.close()
246         dlg.destroy()
247
248     def on_save_cb(self, *args):
249         if not self.current_filename:
250             return self.on_save_as_cb()
251         f = file(self.current_filename, 'w')
252         self.save_to_xml(f)
253         f.close()
254
255     def on_save_as_cb(self, *args):
256         dlg = gtk.FileChooserDialog(title='Save', parent=self.window,
257                         action=gtk.FILE_CHOOSER_ACTION_SAVE,
258                         buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
259                                  gtk.STOCK_SAVE, gtk.RESPONSE_OK))
260         dlg.set_default_response(gtk.RESPONSE_OK)
261         if dlg.run() == gtk.RESPONSE_OK:
262             self.current_filename = dlg.get_filename()
263             self.on_save_cb()
264         dlg.destroy()
265
266     def on_quit_cb(self, *args):
267         gtk.main_quit()
268
269     preferences_dialog = None
270     def on_preferences_cb(self, widget):
271         if not self.preferences_dialog:
272             self.preferences_dialog = PreferencesDialog(self)
273         self.preferences_dialog.show()
274         self.preferences_dialog.present()
275
276     def on_add_input_channel(self, widget):
277         dialog = NewChannelDialog(app=self)
278         dialog.set_transient_for(self.window)
279         dialog.show()
280         ret = dialog.run()
281         dialog.hide()
282
283         if ret == gtk.RESPONSE_OK:
284             result = dialog.get_result()
285             channel = self.add_channel(**result)
286             self.window.show_all()
287
288     def on_add_output_channel(self, widget):
289         dialog = NewOutputChannelDialog(app=self)
290         dialog.set_transient_for(self.window)
291         dialog.show()
292         ret = dialog.run()
293         dialog.hide()
294
295         if ret == gtk.RESPONSE_OK:
296             result = dialog.get_result()
297             channel = self.add_output_channel(**result)
298             self.window.show_all()
299
300     def on_edit_input_channel(self, widget, channel):
301         print 'Editing channel "%s"' % channel.channel_name
302         channel.on_channel_properties()
303
304     def remove_channel_edit_input_menuitem_by_label(self, widget, label):
305         if (widget.get_label() == label):
306             self.channel_edit_input_menu.remove(widget)
307
308     def on_remove_input_channel(self, widget, channel):
309         print 'Removing channel "%s"' % channel.channel_name
310         self.channel_remove_input_menu.remove(widget)
311         self.channel_edit_input_menu.foreach(
312             self.remove_channel_edit_input_menuitem_by_label, 
313             channel.channel_name);
314         if self.monitored_channel is channel:
315             channel.monitor_button.set_active(False)
316         for i in range(len(self.channels)):
317             if self.channels[i] is channel:
318                 channel.unrealize()
319                 del self.channels[i]
320                 self.hbox_inputs.remove(channel.parent)
321                 break
322         if len(self.channels) == 0:
323             self.channel_remove_input_menu_item.set_sensitive(False)
324
325     def on_edit_output_channel(self, widget, channel):
326         print 'Editing channel "%s"' % channel.channel_name
327         channel.on_channel_properties()
328
329     def remove_channel_edit_output_menuitem_by_label(self, widget, label):
330         if (widget.get_label() == label):
331             self.channel_edit_output_menu.remove(widget)
332
333     def on_remove_output_channel(self, widget, channel):
334         print 'Removing channel "%s"' % channel.channel_name
335         self.channel_remove_output_menu.remove(widget)
336         self.channel_edit_output_menu.foreach(
337             self.remove_channel_edit_output_menuitem_by_label, 
338             channel.channel_name);
339         if self.monitored_channel is channel:
340             channel.monitor_button.set_active(False)
341         for i in range(len(self.channels)):
342             if self.output_channels[i] is channel:
343                 channel.unrealize()
344                 del self.output_channels[i]
345                 self.hbox_outputs.remove(channel.parent)
346                 break
347         if len(self.output_channels) == 0:
348             self.channel_remove_output_menu_item.set_sensitive(False)
349
350     def rename_channels(self, container, parameters):
351         if (container.get_label() == parameters['oldname']):
352             container.set_label(parameters['newname']) 
353
354     def on_channel_rename(self, oldname, newname):
355         rename_parameters = { 'oldname' : oldname, 'newname' : newname }
356         self.channel_edit_input_menu.foreach(self.rename_channels, 
357             rename_parameters)
358         self.channel_edit_output_menu.foreach(self.rename_channels, 
359             rename_parameters)
360         self.channel_remove_input_menu.foreach(self.rename_channels, 
361             rename_parameters)
362         self.channel_remove_output_menu.foreach(self.rename_channels, 
363             rename_parameters)
364         print "Renaming channel from %s to %s\n" % (oldname, newname)
365
366
367     def on_channels_clear(self, widget):
368         for channel in self.output_channels:
369             channel.unrealize()
370             self.hbox_outputs.remove(channel.parent)
371         for channel in self.channels:
372             channel.unrealize()
373             self.hbox_inputs.remove(channel.parent)
374         self.channels = []
375         self.output_channels = []
376         self.channel_edit_input_menu = gtk.Menu()
377         self.channel_edit_input_menu_item.set_submenu(self.channel_edit_input_menu)
378         self.channel_edit_input_menu_item.set_sensitive(False)
379         self.channel_remove_input_menu = gtk.Menu()
380         self.channel_remove_input_menu_item.set_submenu(self.channel_remove_input_menu)
381         self.channel_remove_input_menu_item.set_sensitive(False)
382         self.channel_edit_output_menu = gtk.Menu()
383         self.channel_edit_output_menu_item.set_submenu(self.channel_edit_output_menu)
384         self.channel_edit_output_menu_item.set_sensitive(False)
385         self.channel_remove_output_menu = gtk.Menu()
386         self.channel_remove_output_menu_item.set_submenu(self.channel_remove_output_menu)
387         self.channel_remove_output_menu_item.set_sensitive(False)
388
389     def add_channel(self, name, stereo, volume_cc, balance_cc):
390         try:
391             channel = InputChannel(self, name, stereo)
392             self.add_channel_precreated(channel)
393         except Exception:
394             err = gtk.MessageDialog(self.window,
395                             gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
396                             gtk.MESSAGE_ERROR,
397                             gtk.BUTTONS_OK,
398                             "Channel creation failed")
399             err.run()
400             err.destroy()
401             return
402         if volume_cc:
403             channel.channel.volume_midi_cc = int(volume_cc)
404         if balance_cc:
405             channel.channel.balance_midi_cc = int(balance_cc)
406         if not (volume_cc or balance_cc):
407             channel.channel.autoset_midi_cc()
408
409         return channel
410
411     def add_channel_precreated(self, channel):
412         frame = gtk.Frame()
413         frame.add(channel)
414         self.hbox_inputs.pack_start(frame, False)
415         channel.realize()
416
417         channel_edit_menu_item = gtk.MenuItem(channel.channel_name)
418         self.channel_edit_input_menu.append(channel_edit_menu_item)
419         channel_edit_menu_item.connect("activate", self.on_edit_input_channel, channel)
420         self.channel_edit_input_menu_item.set_sensitive(True)
421
422         channel_remove_menu_item = gtk.MenuItem(channel.channel_name)
423         self.channel_remove_input_menu.append(channel_remove_menu_item)
424         channel_remove_menu_item.connect("activate", self.on_remove_input_channel, channel)
425         self.channel_remove_input_menu_item.set_sensitive(True)
426
427         self.channels.append(channel)
428
429         for outputchannel in self.output_channels:
430             channel.add_control_group(outputchannel)
431
432         # create post fader output channel matching the input channel
433         channel.post_fader_output_channel = self.mixer.add_output_channel(
434                         channel.channel.name + ' Out', channel.channel.is_stereo, True)
435         channel.post_fader_output_channel.volume = 0
436         channel.post_fader_output_channel.set_solo(channel.channel, True)
437
438     def read_meters(self):
439         for channel in self.channels:
440             channel.read_meter()
441         self.main_mix.read_meter()
442         for channel in self.output_channels:
443             channel.read_meter()
444         return True
445
446     def add_output_channel(self, name, stereo, volume_cc, balance_cc, display_solo_buttons):
447         try:
448             channel = OutputChannel(self, name, stereo)
449             channel.display_solo_buttons = display_solo_buttons
450             self.add_output_channel_precreated(channel)
451         except Exception:
452             err = gtk.MessageDialog(self.window,
453                             gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
454                             gtk.MESSAGE_ERROR,
455                             gtk.BUTTONS_OK,
456                             "Channel creation failed")
457             err.run()
458             err.destroy()
459             return
460         if volume_cc:
461             channel.channel.volume_midi_cc = int(volume_cc)
462         if balance_cc:
463             channel.channel.balance_midi_cc = int(balance_cc)
464         return channel
465
466     def add_output_channel_precreated(self, channel):
467         frame = gtk.Frame()
468         frame.add(channel)
469         self.hbox_outputs.pack_start(frame, False)
470         channel.realize()
471
472         channel_edit_menu_item = gtk.MenuItem(channel.channel_name)
473         self.channel_edit_output_menu.append(channel_edit_menu_item)
474         channel_edit_menu_item.connect("activate", self.on_edit_output_channel, channel)
475         self.channel_edit_output_menu_item.set_sensitive(True)
476
477         channel_remove_menu_item = gtk.MenuItem(channel.channel_name)
478         self.channel_remove_output_menu.append(channel_remove_menu_item)
479         channel_remove_menu_item.connect("activate", self.on_remove_output_channel, channel)
480         self.channel_remove_output_menu_item.set_sensitive(True)
481
482         self.output_channels.append(channel)
483
484     _monitored_channel = None
485     def get_monitored_channel(self):
486         return self._monitored_channel
487
488     def set_monitored_channel(self, channel):
489         if self._monitored_channel:
490             if channel.channel.name == self._monitored_channel.channel.name:
491                 return
492         self._monitored_channel = channel
493         if type(channel) is InputChannel:
494             # reset all solo/mute settings
495             for in_channel in self.channels:
496                 self.monitor_channel.set_solo(in_channel.channel, False)
497                 self.monitor_channel.set_muted(in_channel.channel, False)
498             self.monitor_channel.set_solo(channel.channel, True)
499             self.monitor_channel.prefader = True
500         else:
501             self.monitor_channel.prefader = False
502         self.update_monitor(channel)
503     monitored_channel = property(get_monitored_channel, set_monitored_channel)
504
505     def update_monitor(self, channel):
506         if self.monitored_channel is not channel:
507             return
508         self.monitor_channel.volume = channel.channel.volume
509         self.monitor_channel.balance = channel.channel.balance
510         if type(self.monitored_channel) is OutputChannel:
511             # sync solo/muted channels
512             for input_channel in self.channels:
513                 self.monitor_channel.set_solo(input_channel.channel,
514                                 channel.channel.is_solo(input_channel.channel))
515                 self.monitor_channel.set_muted(input_channel.channel,
516                                 channel.channel.is_muted(input_channel.channel))
517         elif type(self.monitored_channel) is MainMixChannel:
518             # sync solo/muted channels
519             for input_channel in self.channels:
520                 self.monitor_channel.set_solo(input_channel.channel,
521                                 input_channel.channel.solo)
522                 self.monitor_channel.set_muted(input_channel.channel,
523                                 input_channel.channel.mute)
524
525     def get_input_channel_by_name(self, name):
526         for input_channel in self.channels:
527             if input_channel.channel.name == name:
528                 return input_channel
529         return None
530
531     def on_about(self, *args):
532         about = gtk.AboutDialog()
533         about.set_name('jack_mixer')
534         about.set_copyright('Copyright © 2006-2009\nNedko Arnaudov, Frederic Peters')
535         about.set_license('''\
536 jack_mixer is free software; you can redistribute it and/or modify it
537 under the terms of the GNU General Public License as published by the
538 Free Software Foundation; either version 2 of the License, or (at your
539 option) any later version.
540
541 jack_mixer is distributed in the hope that it will be useful, but
542 WITHOUT ANY WARRANTY; without even the implied warranty of
543 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
544 General Public License for more details.
545
546 You should have received a copy of the GNU General Public License along
547 with jack_mixer; if not, write to the Free Software Foundation, Inc., 51
548 Franklin Street, Fifth Floor, Boston, MA 02110-130159 USA''')
549         about.set_authors(['Nedko Arnaudov <nedko@arnaudov.name>',
550                            'Frederic Peters <fpeters@0d.be>'])
551         about.set_logo_icon_name('jack_mixer')
552         about.set_website('http://home.gna.org/jackmixer/')
553
554         about.run()
555         about.destroy()
556
557     def lash_check_events(self):
558         if self.save:
559             self.save = False
560             if self.current_filename:
561                 print "saving on SIGUSR1 request"
562                 self.on_save_cb()
563                 print "save done"
564             else:
565                 print "not saving because filename is not known"
566             return True
567
568         if not self.lash_client:
569             return True
570
571         while lash.lash_get_pending_event_count(self.lash_client):
572             event = lash.lash_get_event(self.lash_client)
573
574             #print repr(event)
575
576             event_type = lash.lash_event_get_type(event)
577             if event_type == lash.LASH_Quit:
578                 print "jack_mixer: LASH ordered quit."
579                 gtk.main_quit()
580                 return False
581             elif event_type == lash.LASH_Save_File:
582                 directory = lash.lash_event_get_string(event)
583                 print "jack_mixer: LASH ordered to save data in directory %s" % directory
584                 filename = directory + os.sep + "jack_mixer.xml"
585                 f = file(filename, "w")
586                 self.save_to_xml(f)
587                 f.close()
588                 lash.lash_send_event(self.lash_client, event) # we crash with double free
589             elif event_type == lash.LASH_Restore_File:
590                 directory = lash.lash_event_get_string(event)
591                 print "jack_mixer: LASH ordered to restore data from directory %s" % directory
592                 filename = directory + os.sep + "jack_mixer.xml"
593                 f = file(filename, "r")
594                 self.load_from_xml(f, silence_errors=True)
595                 f.close()
596                 lash.lash_send_event(self.lash_client, event)
597             else:
598                 print "jack_mixer: Got unhandled LASH event, type " + str(event_type)
599                 return True
600
601             #lash.lash_event_destroy(event)
602
603         return True
604
605     def save_to_xml(self, file):
606         #print "Saving to XML..."
607         b = XmlSerialization()
608         s = Serializator()
609         s.serialize(self, b)
610         b.save(file)
611
612     def load_from_xml(self, file, silence_errors=False):
613         #print "Loading from XML..."
614         self.on_channels_clear(None)
615         self.unserialized_channels = []
616         b = XmlSerialization()
617         try:
618             b.load(file)
619         except:
620             if silence_errors:
621                 return
622             raise
623         s = Serializator()
624         s.unserialize(self, b)
625         for channel in self.unserialized_channels:
626             if isinstance(channel, InputChannel):
627                 self.add_channel_precreated(channel)
628         for channel in self.unserialized_channels:
629             if isinstance(channel, OutputChannel):
630                 self.add_output_channel_precreated(channel)
631         del self.unserialized_channels
632         self.window.show_all()
633
634     def serialize(self, object_backend):
635         object_backend.add_property('geometry',
636                         '%sx%s' % (self.window.allocation.width, self.window.allocation.height))
637
638     def unserialize_property(self, name, value):
639         if name == 'geometry':
640             width, height = value.split('x')
641             self.window.resize(int(width), int(height))
642             return True
643
644     def unserialize_child(self, name):
645         if name == MainMixChannel.serialization_name():
646             return self.main_mix
647
648         if name == InputChannel.serialization_name():
649             channel = InputChannel(self, "", True)
650             self.unserialized_channels.append(channel)
651             return channel
652
653         if name == OutputChannel.serialization_name():
654             channel = OutputChannel(self, "", True)
655             self.unserialized_channels.append(channel)
656             return channel
657
658     def serialization_get_childs(self):
659         '''Get child objects tha required and support serialization'''
660         childs = self.channels[:] + self.output_channels[:]
661         childs.append(self.main_mix)
662         return childs
663
664     def serialization_name(self):
665         return "jack_mixer"
666
667     def main(self):
668         self.main_mix.realize()
669         self.main_mix.set_monitored()
670
671         if not self.mixer:
672             return
673
674         self.window.show_all()
675
676         signal.signal(signal.SIGUSR1, self.sighandler)
677         signal.signal(signal.SIGTERM, self.sighandler)
678         signal.signal(signal.SIGINT, self.sighandler)
679
680         gtk.main()
681
682         #f = file("/dev/stdout", "w")
683         #self.save_to_xml(f)
684         #f.close
685
686 def help():
687     print "Usage: %s [mixer_name]" % sys.argv[0]
688
689 def main():
690     # Connect to LASH if Python bindings are available, and the user did not
691     # pass --no-lash
692     if lash and not '--no-lash' in sys.argv:
693         # sys.argv is modified by this call
694         lash_client = lash.init(sys.argv, "jack_mixer", lash.LASH_Config_File)
695     else:
696         lash_client = None
697
698     parser = OptionParser()
699     parser.add_option('-c', '--config', dest='config',
700                       help='use a non default configuration file')
701     # --no-lash here is not acted upon, it is specified for completeness when
702     # --help is passed.
703     parser.add_option('--no-lash', dest='nolash', action='store_true',
704                       help='do not connect to LASH')
705     options, args = parser.parse_args()
706
707     # Yeah , this sounds stupid, we connected earlier, but we dont want to show this if we got --help option
708     # This issue should be fixed in pylash, there is a reason for having two functions for initialization after all
709     if lash_client:
710         print "Successfully connected to LASH server at " +  lash.lash_get_server_name(lash_client)
711
712     if len(args) == 1:
713         name = args[0]
714     else:
715         name = None
716
717     if not name:
718         name = "jack_mixer"
719
720     gtk.gdk.threads_init() # if this function is called, when SIGUSR1 is received, we enter tight loop...
721     try:
722         mixer = JackMixer(name, lash_client)
723     except Exception, e:
724         err = gtk.MessageDialog(None,
725                             gtk.DIALOG_MODAL,
726                             gtk.MESSAGE_ERROR,
727                             gtk.BUTTONS_OK,
728                             "Mixer creation failed (%s)" % str(e))
729         err.run()
730         err.destroy()
731         sys.exit(1)
732
733     if options.config:
734         f = file(options.config)
735         mixer.current_filename = options.config
736         try:
737             mixer.load_from_xml(f)
738         except:
739             err = gtk.MessageDialog(mixer.window,
740                             gtk.DIALOG_MODAL,
741                             gtk.MESSAGE_ERROR,
742                             gtk.BUTTONS_OK,
743                             "Failed loading settings.")
744             err.run()
745             err.destroy()
746         mixer.window.set_default_size(60*(1+len(mixer.channels)+len(mixer.output_channels)), 300)
747         f.close()
748
749     mixer.main()
750
751     mixer.cleanup()
752
753 if __name__ == "__main__":
754     main()