]> git.0d.be Git - jack_mixer.git/blob - jack_mixer.py
Ignore SIGHUP
[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         gobject.timeout_add(50, self.midi_events_check)
205
206     def sighandler(self, signum, frame):
207         #print "Signal %d received" % signum
208         if signum == signal.SIGUSR1:
209             self.save = True
210         elif signum == signal.SIGTERM:
211             gtk.main_quit()
212         elif signum == signal.SIGINT:
213             gtk.main_quit()
214         else:
215             print "Unknown signal %d received" % signum
216
217     def cleanup(self):
218         print "Cleaning jack_mixer"
219         if not self.mixer:
220             return
221
222         for channel in self.channels:
223             channel.unrealize()
224
225         self.mixer.destroy()
226
227     def on_open_cb(self, *args):
228         dlg = gtk.FileChooserDialog(title='Open', parent=self.window,
229                         action=gtk.FILE_CHOOSER_ACTION_OPEN,
230                         buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
231                                  gtk.STOCK_OPEN, gtk.RESPONSE_OK))
232         dlg.set_default_response(gtk.RESPONSE_OK)
233         if dlg.run() == gtk.RESPONSE_OK:
234             filename = dlg.get_filename()
235             try:
236                 f = file(filename, 'r')
237                 self.load_from_xml(f)
238             except:
239                 err = gtk.MessageDialog(self.window,
240                             gtk.DIALOG_MODAL,
241                             gtk.MESSAGE_ERROR,
242                             gtk.BUTTONS_OK,
243                             "Failed loading settings.")
244                 err.run()
245                 err.destroy()
246             else:
247                 self.current_filename = filename
248             finally:
249                 f.close()
250         dlg.destroy()
251
252     def on_save_cb(self, *args):
253         if not self.current_filename:
254             return self.on_save_as_cb()
255         f = file(self.current_filename, 'w')
256         self.save_to_xml(f)
257         f.close()
258
259     def on_save_as_cb(self, *args):
260         dlg = gtk.FileChooserDialog(title='Save', parent=self.window,
261                         action=gtk.FILE_CHOOSER_ACTION_SAVE,
262                         buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
263                                  gtk.STOCK_SAVE, gtk.RESPONSE_OK))
264         dlg.set_default_response(gtk.RESPONSE_OK)
265         if dlg.run() == gtk.RESPONSE_OK:
266             self.current_filename = dlg.get_filename()
267             self.on_save_cb()
268         dlg.destroy()
269
270     def on_quit_cb(self, *args):
271         gtk.main_quit()
272
273     preferences_dialog = None
274     def on_preferences_cb(self, widget):
275         if not self.preferences_dialog:
276             self.preferences_dialog = PreferencesDialog(self)
277         self.preferences_dialog.show()
278         self.preferences_dialog.present()
279
280     def on_add_input_channel(self, widget):
281         dialog = NewChannelDialog(app=self)
282         dialog.set_transient_for(self.window)
283         dialog.show()
284         ret = dialog.run()
285         dialog.hide()
286
287         if ret == gtk.RESPONSE_OK:
288             result = dialog.get_result()
289             channel = self.add_channel(**result)
290             self.window.show_all()
291
292     def on_add_output_channel(self, widget):
293         dialog = NewOutputChannelDialog(app=self)
294         dialog.set_transient_for(self.window)
295         dialog.show()
296         ret = dialog.run()
297         dialog.hide()
298
299         if ret == gtk.RESPONSE_OK:
300             result = dialog.get_result()
301             channel = self.add_output_channel(**result)
302             self.window.show_all()
303
304     def on_edit_input_channel(self, widget, channel):
305         print 'Editing channel "%s"' % channel.channel_name
306         channel.on_channel_properties()
307
308     def remove_channel_edit_input_menuitem_by_label(self, widget, label):
309         if (widget.get_label() == label):
310             self.channel_edit_input_menu.remove(widget)
311
312     def on_remove_input_channel(self, widget, channel):
313         print 'Removing channel "%s"' % channel.channel_name
314         self.channel_remove_input_menu.remove(widget)
315         self.channel_edit_input_menu.foreach(
316             self.remove_channel_edit_input_menuitem_by_label, 
317             channel.channel_name);
318         if self.monitored_channel is channel:
319             channel.monitor_button.set_active(False)
320         for i in range(len(self.channels)):
321             if self.channels[i] is channel:
322                 channel.unrealize()
323                 del self.channels[i]
324                 self.hbox_inputs.remove(channel.parent)
325                 break
326         if len(self.channels) == 0:
327             self.channel_remove_input_menu_item.set_sensitive(False)
328
329     def on_edit_output_channel(self, widget, channel):
330         print 'Editing channel "%s"' % channel.channel_name
331         channel.on_channel_properties()
332
333     def remove_channel_edit_output_menuitem_by_label(self, widget, label):
334         if (widget.get_label() == label):
335             self.channel_edit_output_menu.remove(widget)
336
337     def on_remove_output_channel(self, widget, channel):
338         print 'Removing channel "%s"' % channel.channel_name
339         self.channel_remove_output_menu.remove(widget)
340         self.channel_edit_output_menu.foreach(
341             self.remove_channel_edit_output_menuitem_by_label, 
342             channel.channel_name);
343         if self.monitored_channel is channel:
344             channel.monitor_button.set_active(False)
345         for i in range(len(self.channels)):
346             if self.output_channels[i] is channel:
347                 channel.unrealize()
348                 del self.output_channels[i]
349                 self.hbox_outputs.remove(channel.parent)
350                 break
351         if len(self.output_channels) == 0:
352             self.channel_remove_output_menu_item.set_sensitive(False)
353
354     def rename_channels(self, container, parameters):
355         if (container.get_label() == parameters['oldname']):
356             container.set_label(parameters['newname']) 
357
358     def on_channel_rename(self, oldname, newname):
359         rename_parameters = { 'oldname' : oldname, 'newname' : newname }
360         self.channel_edit_input_menu.foreach(self.rename_channels, 
361             rename_parameters)
362         self.channel_edit_output_menu.foreach(self.rename_channels, 
363             rename_parameters)
364         self.channel_remove_input_menu.foreach(self.rename_channels, 
365             rename_parameters)
366         self.channel_remove_output_menu.foreach(self.rename_channels, 
367             rename_parameters)
368         print "Renaming channel from %s to %s\n" % (oldname, newname)
369
370
371     def on_channels_clear(self, widget):
372         for channel in self.output_channels:
373             channel.unrealize()
374             self.hbox_outputs.remove(channel.parent)
375         for channel in self.channels:
376             channel.unrealize()
377             self.hbox_inputs.remove(channel.parent)
378         self.channels = []
379         self.output_channels = []
380         self.channel_edit_input_menu = gtk.Menu()
381         self.channel_edit_input_menu_item.set_submenu(self.channel_edit_input_menu)
382         self.channel_edit_input_menu_item.set_sensitive(False)
383         self.channel_remove_input_menu = gtk.Menu()
384         self.channel_remove_input_menu_item.set_submenu(self.channel_remove_input_menu)
385         self.channel_remove_input_menu_item.set_sensitive(False)
386         self.channel_edit_output_menu = gtk.Menu()
387         self.channel_edit_output_menu_item.set_submenu(self.channel_edit_output_menu)
388         self.channel_edit_output_menu_item.set_sensitive(False)
389         self.channel_remove_output_menu = gtk.Menu()
390         self.channel_remove_output_menu_item.set_submenu(self.channel_remove_output_menu)
391         self.channel_remove_output_menu_item.set_sensitive(False)
392
393     def add_channel(self, name, stereo, volume_cc, balance_cc):
394         try:
395             channel = InputChannel(self, name, stereo)
396             self.add_channel_precreated(channel)
397         except Exception:
398             err = gtk.MessageDialog(self.window,
399                             gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
400                             gtk.MESSAGE_ERROR,
401                             gtk.BUTTONS_OK,
402                             "Channel creation failed")
403             err.run()
404             err.destroy()
405             return
406         if volume_cc:
407             channel.channel.volume_midi_cc = int(volume_cc)
408         if balance_cc:
409             channel.channel.balance_midi_cc = int(balance_cc)
410         if not (volume_cc or balance_cc):
411             channel.channel.autoset_midi_cc()
412
413         return channel
414
415     def add_channel_precreated(self, channel):
416         frame = gtk.Frame()
417         frame.add(channel)
418         self.hbox_inputs.pack_start(frame, False)
419         channel.realize()
420
421         channel_edit_menu_item = gtk.MenuItem(channel.channel_name)
422         self.channel_edit_input_menu.append(channel_edit_menu_item)
423         channel_edit_menu_item.connect("activate", self.on_edit_input_channel, channel)
424         self.channel_edit_input_menu_item.set_sensitive(True)
425
426         channel_remove_menu_item = gtk.MenuItem(channel.channel_name)
427         self.channel_remove_input_menu.append(channel_remove_menu_item)
428         channel_remove_menu_item.connect("activate", self.on_remove_input_channel, channel)
429         self.channel_remove_input_menu_item.set_sensitive(True)
430
431         self.channels.append(channel)
432
433         for outputchannel in self.output_channels:
434             channel.add_control_group(outputchannel)
435
436         # create post fader output channel matching the input channel
437         channel.post_fader_output_channel = self.mixer.add_output_channel(
438                         channel.channel.name + ' Out', channel.channel.is_stereo, True)
439         channel.post_fader_output_channel.volume = 0
440         channel.post_fader_output_channel.set_solo(channel.channel, True)
441
442     def read_meters(self):
443         for channel in self.channels:
444             channel.read_meter()
445         self.main_mix.read_meter()
446         for channel in self.output_channels:
447             channel.read_meter()
448         return True
449
450     def midi_events_check(self):
451         for channel in self.channels + [self.main_mix] + self.output_channels:
452             channel.midi_events_check()
453         return True
454
455     def add_output_channel(self, name, stereo, volume_cc, balance_cc, display_solo_buttons):
456         try:
457             channel = OutputChannel(self, name, stereo)
458             channel.display_solo_buttons = display_solo_buttons
459             self.add_output_channel_precreated(channel)
460         except Exception:
461             err = gtk.MessageDialog(self.window,
462                             gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
463                             gtk.MESSAGE_ERROR,
464                             gtk.BUTTONS_OK,
465                             "Channel creation failed")
466             err.run()
467             err.destroy()
468             return
469         if volume_cc:
470             channel.channel.volume_midi_cc = int(volume_cc)
471         if balance_cc:
472             channel.channel.balance_midi_cc = int(balance_cc)
473         return channel
474
475     def add_output_channel_precreated(self, channel):
476         frame = gtk.Frame()
477         frame.add(channel)
478         self.hbox_outputs.pack_start(frame, False)
479         channel.realize()
480
481         channel_edit_menu_item = gtk.MenuItem(channel.channel_name)
482         self.channel_edit_output_menu.append(channel_edit_menu_item)
483         channel_edit_menu_item.connect("activate", self.on_edit_output_channel, channel)
484         self.channel_edit_output_menu_item.set_sensitive(True)
485
486         channel_remove_menu_item = gtk.MenuItem(channel.channel_name)
487         self.channel_remove_output_menu.append(channel_remove_menu_item)
488         channel_remove_menu_item.connect("activate", self.on_remove_output_channel, channel)
489         self.channel_remove_output_menu_item.set_sensitive(True)
490
491         self.output_channels.append(channel)
492
493     _monitored_channel = None
494     def get_monitored_channel(self):
495         return self._monitored_channel
496
497     def set_monitored_channel(self, channel):
498         if self._monitored_channel:
499             if channel.channel.name == self._monitored_channel.channel.name:
500                 return
501         self._monitored_channel = channel
502         if type(channel) is InputChannel:
503             # reset all solo/mute settings
504             for in_channel in self.channels:
505                 self.monitor_channel.set_solo(in_channel.channel, False)
506                 self.monitor_channel.set_muted(in_channel.channel, False)
507             self.monitor_channel.set_solo(channel.channel, True)
508             self.monitor_channel.prefader = True
509         else:
510             self.monitor_channel.prefader = False
511         self.update_monitor(channel)
512     monitored_channel = property(get_monitored_channel, set_monitored_channel)
513
514     def update_monitor(self, channel):
515         if self.monitored_channel is not channel:
516             return
517         self.monitor_channel.volume = channel.channel.volume
518         self.monitor_channel.balance = channel.channel.balance
519         if type(self.monitored_channel) is OutputChannel:
520             # sync solo/muted channels
521             for input_channel in self.channels:
522                 self.monitor_channel.set_solo(input_channel.channel,
523                                 channel.channel.is_solo(input_channel.channel))
524                 self.monitor_channel.set_muted(input_channel.channel,
525                                 channel.channel.is_muted(input_channel.channel))
526         elif type(self.monitored_channel) is MainMixChannel:
527             # sync solo/muted channels
528             for input_channel in self.channels:
529                 self.monitor_channel.set_solo(input_channel.channel,
530                                 input_channel.channel.solo)
531                 self.monitor_channel.set_muted(input_channel.channel,
532                                 input_channel.channel.mute)
533
534     def get_input_channel_by_name(self, name):
535         for input_channel in self.channels:
536             if input_channel.channel.name == name:
537                 return input_channel
538         return None
539
540     def on_about(self, *args):
541         about = gtk.AboutDialog()
542         about.set_name('jack_mixer')
543         about.set_copyright('Copyright Â© 2006-2010\nNedko Arnaudov, Frederic Peters, Arnout Engelen')
544         about.set_license('''\
545 jack_mixer is free software; you can redistribute it and/or modify it
546 under the terms of the GNU General Public License as published by the
547 Free Software Foundation; either version 2 of the License, or (at your
548 option) any later version.
549
550 jack_mixer is distributed in the hope that it will be useful, but
551 WITHOUT ANY WARRANTY; without even the implied warranty of
552 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
553 General Public License for more details.
554
555 You should have received a copy of the GNU General Public License along
556 with jack_mixer; if not, write to the Free Software Foundation, Inc., 51
557 Franklin Street, Fifth Floor, Boston, MA 02110-130159 USA''')
558         about.set_authors(['Nedko Arnaudov <nedko@arnaudov.name>',
559                            'Frederic Peters <fpeters@0d.be>'])
560         about.set_logo_icon_name('jack_mixer')
561         about.set_website('http://home.gna.org/jackmixer/')
562
563         about.run()
564         about.destroy()
565
566     def lash_check_events(self):
567         if self.save:
568             self.save = False
569             if self.current_filename:
570                 print "saving on SIGUSR1 request"
571                 self.on_save_cb()
572                 print "save done"
573             else:
574                 print "not saving because filename is not known"
575             return True
576
577         if not self.lash_client:
578             return True
579
580         while lash.lash_get_pending_event_count(self.lash_client):
581             event = lash.lash_get_event(self.lash_client)
582
583             #print repr(event)
584
585             event_type = lash.lash_event_get_type(event)
586             if event_type == lash.LASH_Quit:
587                 print "jack_mixer: LASH ordered quit."
588                 gtk.main_quit()
589                 return False
590             elif event_type == lash.LASH_Save_File:
591                 directory = lash.lash_event_get_string(event)
592                 print "jack_mixer: LASH ordered to save data in directory %s" % directory
593                 filename = directory + os.sep + "jack_mixer.xml"
594                 f = file(filename, "w")
595                 self.save_to_xml(f)
596                 f.close()
597                 lash.lash_send_event(self.lash_client, event) # we crash with double free
598             elif event_type == lash.LASH_Restore_File:
599                 directory = lash.lash_event_get_string(event)
600                 print "jack_mixer: LASH ordered to restore data from directory %s" % directory
601                 filename = directory + os.sep + "jack_mixer.xml"
602                 f = file(filename, "r")
603                 self.load_from_xml(f, silence_errors=True)
604                 f.close()
605                 lash.lash_send_event(self.lash_client, event)
606             else:
607                 print "jack_mixer: Got unhandled LASH event, type " + str(event_type)
608                 return True
609
610             #lash.lash_event_destroy(event)
611
612         return True
613
614     def save_to_xml(self, file):
615         #print "Saving to XML..."
616         b = XmlSerialization()
617         s = Serializator()
618         s.serialize(self, b)
619         b.save(file)
620
621     def load_from_xml(self, file, silence_errors=False):
622         #print "Loading from XML..."
623         self.on_channels_clear(None)
624         self.unserialized_channels = []
625         b = XmlSerialization()
626         try:
627             b.load(file)
628         except:
629             if silence_errors:
630                 return
631             raise
632         s = Serializator()
633         s.unserialize(self, b)
634         for channel in self.unserialized_channels:
635             if isinstance(channel, InputChannel):
636                 self.add_channel_precreated(channel)
637         for channel in self.unserialized_channels:
638             if isinstance(channel, OutputChannel):
639                 self.add_output_channel_precreated(channel)
640         del self.unserialized_channels
641         self.window.show_all()
642
643     def serialize(self, object_backend):
644         object_backend.add_property('geometry',
645                         '%sx%s' % (self.window.allocation.width, self.window.allocation.height))
646
647     def unserialize_property(self, name, value):
648         if name == 'geometry':
649             width, height = value.split('x')
650             self.window.resize(int(width), int(height))
651             return True
652
653     def unserialize_child(self, name):
654         if name == MainMixChannel.serialization_name():
655             return self.main_mix
656
657         if name == InputChannel.serialization_name():
658             channel = InputChannel(self, "", True)
659             self.unserialized_channels.append(channel)
660             return channel
661
662         if name == OutputChannel.serialization_name():
663             channel = OutputChannel(self, "", True)
664             self.unserialized_channels.append(channel)
665             return channel
666
667     def serialization_get_childs(self):
668         '''Get child objects tha required and support serialization'''
669         childs = self.channels[:] + self.output_channels[:]
670         childs.append(self.main_mix)
671         return childs
672
673     def serialization_name(self):
674         return "jack_mixer"
675
676     def main(self):
677         self.main_mix.realize()
678         self.main_mix.set_monitored()
679
680         if not self.mixer:
681             return
682
683         self.window.show_all()
684
685         signal.signal(signal.SIGUSR1, self.sighandler)
686         signal.signal(signal.SIGTERM, self.sighandler)
687         signal.signal(signal.SIGINT, self.sighandler)
688         signal.signal(signal.SIGHUP, signal.SIG_IGN)
689
690         gtk.main()
691
692         #f = file("/dev/stdout", "w")
693         #self.save_to_xml(f)
694         #f.close
695
696 def help():
697     print "Usage: %s [mixer_name]" % sys.argv[0]
698
699 def main():
700     # Connect to LASH if Python bindings are available, and the user did not
701     # pass --no-lash
702     if lash and not '--no-lash' in sys.argv:
703         # sys.argv is modified by this call
704         lash_client = lash.init(sys.argv, "jack_mixer", lash.LASH_Config_File)
705     else:
706         lash_client = None
707
708     parser = OptionParser(usage='usage: %prog [options] [jack_client_name]')
709     parser.add_option('-c', '--config', dest='config',
710                       help='use a non default configuration file')
711     # --no-lash here is not acted upon, it is specified for completeness when
712     # --help is passed.
713     parser.add_option('--no-lash', dest='nolash', action='store_true',
714                       help='do not connect to LASH')
715     options, args = parser.parse_args()
716
717     # Yeah , this sounds stupid, we connected earlier, but we dont want to show this if we got --help option
718     # This issue should be fixed in pylash, there is a reason for having two functions for initialization after all
719     if lash_client:
720         server_name = lash.lash_get_server_name(lash_client)
721         if server_name:
722             print "Successfully connected to LASH server at " + server_name
723         else:
724             # getting the server name failed, probably not worth trying to do
725             # further things with as a lash client.
726             lash_client = None
727
728     if len(args) == 1:
729         name = args[0]
730     else:
731         name = None
732
733     if not name:
734         name = "jack_mixer"
735
736     try:
737         mixer = JackMixer(name, lash_client)
738     except Exception, e:
739         err = gtk.MessageDialog(None,
740                             gtk.DIALOG_MODAL,
741                             gtk.MESSAGE_ERROR,
742                             gtk.BUTTONS_OK,
743                             "Mixer creation failed (%s)" % str(e))
744         err.run()
745         err.destroy()
746         sys.exit(1)
747
748     if options.config:
749         f = file(options.config)
750         mixer.current_filename = options.config
751         try:
752             mixer.load_from_xml(f)
753         except:
754             err = gtk.MessageDialog(mixer.window,
755                             gtk.DIALOG_MODAL,
756                             gtk.MESSAGE_ERROR,
757                             gtk.BUTTONS_OK,
758                             "Failed loading settings.")
759             err.run()
760             err.destroy()
761         mixer.window.set_default_size(60*(1+len(mixer.channels)+len(mixer.output_channels)), 300)
762         f.close()
763
764     mixer.main()
765
766     mixer.cleanup()
767
768 if __name__ == "__main__":
769     main()