]> git.0d.be Git - jack_mixer.git/blob - jack_mixer.py
Fix Gtk deprecation warning for save dialog as well
[jack_mixer.git] / jack_mixer.py
1 #!/usr/bin/env python3
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 import os
23 import signal
24 import sys
25 from argparse import ArgumentParser
26
27 import gi
28 gi.require_version('Gtk', '3.0')
29 from gi.repository import Gtk
30 from gi.repository import GObject
31 from gi.repository import GLib
32
33 # temporary change Python modules lookup path to look into installation
34 # directory ($prefix/share/jack_mixer/)
35 old_path = sys.path
36 sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), '..', 'share', 'jack_mixer'))
37
38 import jack_mixer_c
39
40 import scale
41 from channel import *
42
43 import gui
44 from preferences import PreferencesDialog
45
46 from serialization_xml import XmlSerialization
47 from serialization import SerializedObject, Serializator
48
49 from nsmclient import NSMClient
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     _init_solo_channels = None
66
67     def __init__(self, client_name='jack_mixer'):
68         self.visible = False
69         self.nsm_client = None
70
71         if os.environ.get('NSM_URL'):
72             self.nsm_client = NSMClient(prettyName = "jack_mixer",
73                                         saveCallback = self.nsm_save_cb,
74                                         openOrNewCallback = self.nsm_open_cb,
75                                         supportsSaveStatus = False,
76                                         hideGUICallback = self.nsm_hide_cb,
77                                         showGUICallback = self.nsm_show_cb,
78                                         exitProgramCallback = self.nsm_exit_cb,
79                                         loggingLevel = "error",
80                                        )
81             self.nsm_client.announceGuiVisibility(self.visible)
82         else:
83
84             self.visible = True
85             self.create_mixer(client_name, with_nsm = False)
86
87     def create_mixer(self, client_name, with_nsm = True):
88         self.create_ui(with_nsm)
89         self.mixer = jack_mixer_c.Mixer(client_name)
90         if not self.mixer:
91             sys.exit(1)
92
93         #self.mixer.midi_behavior_mode = self.gui_factory.midi_behavior_modes[self.gui_factory.get_midi_behavior_mode()]
94
95         self.window.set_title(client_name)
96
97         self.monitor_channel = self.mixer.add_output_channel("Monitor", True, True)
98         self.save = False
99
100         GLib.timeout_add(80, self.read_meters)
101         if with_nsm:
102             GLib.timeout_add(200, self.nsm_react)
103         GLib.timeout_add(50, self.midi_events_check)
104
105     def create_ui(self, with_nsm):
106         self.channels = []
107         self.output_channels = []
108         self.window = Gtk.Window(type=Gtk.WindowType.TOPLEVEL)
109         self.window.set_icon_name('jack_mixer')
110         self.gui_factory = gui.Factory(self.window, self.meter_scales, self.slider_scales)
111         self.gui_factory.connect('midi-behavior-mode-changed', self.on_midi_behavior_mode_changed)
112         self.vbox_top = Gtk.VBox()
113         self.window.add(self.vbox_top)
114
115         self.menubar = Gtk.MenuBar()
116         self.vbox_top.pack_start(self.menubar, False, True, 0)
117
118         mixer_menu_item = Gtk.MenuItem.new_with_mnemonic("_Mixer")
119         self.menubar.append(mixer_menu_item)
120         edit_menu_item = Gtk.MenuItem.new_with_mnemonic('_Edit')
121         self.menubar.append(edit_menu_item)
122         help_menu_item = Gtk.MenuItem.new_with_mnemonic('_Help')
123         self.menubar.append(help_menu_item)
124
125         self.window.set_default_size(120, 300)
126
127         self.mixer_menu = Gtk.Menu()
128         mixer_menu_item.set_submenu(self.mixer_menu)
129
130         add_input_channel = Gtk.MenuItem.new_with_mnemonic('New _Input Channel')
131         self.mixer_menu.append(add_input_channel)
132         add_input_channel.connect("activate", self.on_add_input_channel)
133
134         add_output_channel = Gtk.MenuItem.new_with_mnemonic('New _Output Channel')
135         self.mixer_menu.append(add_output_channel)
136         add_output_channel.connect("activate", self.on_add_output_channel)
137
138         self.mixer_menu.append(Gtk.SeparatorMenuItem())
139         if not with_nsm:
140             open = Gtk.MenuItem.new_with_mnemonic('_Open')
141             self.mixer_menu.append(open)
142             open.connect('activate', self.on_open_cb)
143         save = Gtk.MenuItem.new_with_mnemonic('_Save')
144         self.mixer_menu.append(save)
145         save.connect('activate', self.on_save_cb)
146         if not with_nsm:
147             save_as = Gtk.MenuItem.new_with_mnemonic('Save_As')
148             self.mixer_menu.append(save_as)
149             save_as.connect('activate', self.on_save_as_cb)
150
151         self.mixer_menu.append(Gtk.SeparatorMenuItem())
152
153         quit = Gtk.MenuItem.new_with_mnemonic('_Quit')
154         self.mixer_menu.append(quit)
155         quit.connect('activate', self.on_quit_cb)
156
157         edit_menu = Gtk.Menu()
158         edit_menu_item.set_submenu(edit_menu)
159
160         self.channel_edit_input_menu_item = Gtk.MenuItem.new_with_mnemonic('_Edit Input Channel')
161         edit_menu.append(self.channel_edit_input_menu_item)
162         self.channel_edit_input_menu = Gtk.Menu()
163         self.channel_edit_input_menu_item.set_submenu(self.channel_edit_input_menu)
164
165         self.channel_edit_output_menu_item = Gtk.MenuItem.new_with_mnemonic('Edit _Output Channel')
166         edit_menu.append(self.channel_edit_output_menu_item)
167         self.channel_edit_output_menu = Gtk.Menu()
168         self.channel_edit_output_menu_item.set_submenu(self.channel_edit_output_menu)
169
170         self.channel_remove_input_menu_item = Gtk.MenuItem.new_with_mnemonic('Remove _Input Channel')
171         edit_menu.append(self.channel_remove_input_menu_item)
172         self.channel_remove_input_menu = Gtk.Menu()
173         self.channel_remove_input_menu_item.set_submenu(self.channel_remove_input_menu)
174
175         self.channel_remove_output_menu_item = Gtk.MenuItem.new_with_mnemonic('_Remove Output Channel')
176         edit_menu.append(self.channel_remove_output_menu_item)
177         self.channel_remove_output_menu = Gtk.Menu()
178         self.channel_remove_output_menu_item.set_submenu(self.channel_remove_output_menu)
179
180         channel_remove_all_menu_item = Gtk.MenuItem.new_with_mnemonic('Clear')
181         edit_menu.append(channel_remove_all_menu_item)
182         channel_remove_all_menu_item.connect("activate", self.on_channels_clear)
183
184         edit_menu.append(Gtk.SeparatorMenuItem())
185
186         preferences = Gtk.MenuItem.new_with_mnemonic('_Preferences')
187         preferences.connect('activate', self.on_preferences_cb)
188         edit_menu.append(preferences)
189
190         help_menu = Gtk.Menu()
191         help_menu_item.set_submenu(help_menu)
192
193         about = Gtk.MenuItem.new_with_mnemonic('_About')
194         help_menu.append(about)
195         about.connect("activate", self.on_about)
196
197         self.hbox_top = Gtk.HBox()
198         self.vbox_top.pack_start(self.hbox_top, True, True, 0)
199
200         self.scrolled_window = Gtk.ScrolledWindow()
201         self.hbox_top.pack_start(self.scrolled_window, True, True, 0)
202
203         self.hbox_inputs = Gtk.HBox()
204         self.hbox_inputs.set_spacing(0)
205         self.hbox_inputs.set_border_width(0)
206         self.hbox_top.set_spacing(0)
207         self.hbox_top.set_border_width(0)
208
209         self.scrolled_window.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
210         self.scrolled_window.add(self.hbox_inputs)
211
212         self.hbox_outputs = Gtk.HBox()
213         self.hbox_outputs.set_spacing(0)
214         self.hbox_outputs.set_border_width(0)
215         frame = Gtk.Frame()
216         self.hbox_outputs.pack_start(frame, False, True, 0)
217         self.hbox_top.pack_start(self.hbox_outputs, False, True, 0)
218
219         self.window.connect("destroy", Gtk.main_quit)
220
221         self.window.connect('delete-event', self.on_delete_event)
222
223     def nsm_react(self):
224         self.nsm_client.reactToMessage()
225         return True
226
227     def nsm_hide_cb(self):
228         self.window.hide()
229         self.visible = False
230         self.nsm_client.announceGuiVisibility(False)
231
232     def nsm_show_cb(self):
233         self.window.show_all()
234         self.visible = True
235         self.nsm_client.announceGuiVisibility(True)
236
237     def nsm_open_cb(self, path, session_name, client_name):
238         self.create_mixer(client_name, with_nsm = True)
239         self.current_filename = path + '.xml'
240         if os.path.isfile(self.current_filename):
241             f = open(self.current_filename, 'r')
242             self.load_from_xml(f)
243             f.close()
244         else:
245             f = open(self.current_filename, 'w')
246             f.close()
247
248     def nsm_save_cb(self, path, session_name, client_name):
249         self.current_filename = path + '.xml'
250         f = open(self.current_filename, 'w')
251         self.save_to_xml(f)
252         f.close()
253
254     def nsm_exit_cb(self, path, session_name, client_name):
255         Gtk.main_quit()
256
257     def on_midi_behavior_mode_changed(self, gui_factory, value):
258         self.mixer.midi_behavior_mode = value
259
260     def on_delete_event(self, widget, event):
261         return False
262
263     def sighandler(self, signum, frame):
264         #print "Signal %d received" % signum
265         if signum == signal.SIGUSR1:
266             self.save = True
267         elif signum == signal.SIGTERM:
268             Gtk.main_quit()
269         elif signum == signal.SIGINT:
270             Gtk.main_quit()
271         else:
272             print("Unknown signal %d received" % signum)
273
274     def cleanup(self):
275         print("Cleaning jack_mixer")
276         if not self.mixer:
277             return
278
279         for channel in self.channels:
280             channel.unrealize()
281
282         self.mixer.destroy()
283
284     def on_open_cb(self, *args):
285         dlg = Gtk.FileChooserDialog(title='Open', parent=self.window,
286                         action=Gtk.FileChooserAction.OPEN)
287         dlg.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
288                         Gtk.STOCK_OPEN, Gtk.ResponseType.OK)
289         dlg.set_default_response(Gtk.ResponseType.OK)
290         if dlg.run() == Gtk.ResponseType.OK:
291             filename = dlg.get_filename()
292             try:
293                 f = open(filename, 'r')
294                 self.load_from_xml(f)
295             except Exception as e:
296                 err = Gtk.MessageDialog(parent = self.window,
297                             modal = True,
298                             message_type = Gtk.MessageType.ERROR,
299                             buttons = Gtk.ButtonsType.OK,
300                             text = "Failed loading settings (%s)." % str(e))
301                 err.run()
302                 err.destroy()
303             else:
304                 self.current_filename = filename
305             finally:
306                 f.close()
307         dlg.destroy()
308
309     def on_save_cb(self, *args):
310         if not self.current_filename:
311             return self.on_save_as_cb()
312         f = open(self.current_filename, 'w')
313         self.save_to_xml(f)
314         f.close()
315
316     def on_save_as_cb(self, *args):
317         dlg = Gtk.FileChooserDialog(title='Save', parent=self.window,
318                         action=Gtk.FileChooserAction.SAVE)
319         dlg.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
320                         Gtk.STOCK_SAVE, Gtk.ResponseType.OK)
321         dlg.set_default_response(Gtk.ResponseType.OK)
322         if dlg.run() == Gtk.ResponseType.OK:
323             self.current_filename = dlg.get_filename()
324             self.on_save_cb()
325         dlg.destroy()
326
327     def on_quit_cb(self, *args):
328         Gtk.main_quit()
329
330     preferences_dialog = None
331     def on_preferences_cb(self, widget):
332         if not self.preferences_dialog:
333             self.preferences_dialog = PreferencesDialog(self)
334         self.preferences_dialog.show()
335         self.preferences_dialog.present()
336
337     def on_add_input_channel(self, widget):
338         dialog = NewChannelDialog(app=self)
339         dialog.set_transient_for(self.window)
340         dialog.show()
341         ret = dialog.run()
342         dialog.hide()
343
344         if ret == Gtk.ResponseType.OK:
345             result = dialog.get_result()
346             channel = self.add_channel(**result)
347             if self.visible:
348                 self.window.show_all()
349
350     def on_add_output_channel(self, widget):
351         dialog = NewOutputChannelDialog(app=self)
352         dialog.set_transient_for(self.window)
353         dialog.show()
354         ret = dialog.run()
355         dialog.hide()
356
357         if ret == Gtk.ResponseType.OK:
358             result = dialog.get_result()
359             channel = self.add_output_channel(**result)
360             if self.visible:
361                 self.window.show_all()
362
363     def on_edit_input_channel(self, widget, channel):
364         print('Editing channel "%s"' % channel.channel_name)
365         channel.on_channel_properties()
366
367     def remove_channel_edit_input_menuitem_by_label(self, widget, label):
368         if (widget.get_label() == label):
369             self.channel_edit_input_menu.remove(widget)
370
371     def on_remove_input_channel(self, widget, channel):
372         print('Removing channel "%s"' % channel.channel_name)
373         self.channel_remove_input_menu.remove(widget)
374         self.channel_edit_input_menu.foreach(
375             self.remove_channel_edit_input_menuitem_by_label,
376             channel.channel_name);
377         if self.monitored_channel is channel:
378             channel.monitor_button.set_active(False)
379         for i in range(len(self.channels)):
380             if self.channels[i] is channel:
381                 channel.unrealize()
382                 del self.channels[i]
383                 self.hbox_inputs.remove(channel.get_parent())
384                 break
385         if len(self.channels) == 0:
386             self.channel_remove_input_menu_item.set_sensitive(False)
387
388     def on_edit_output_channel(self, widget, channel):
389         print('Editing channel "%s"' % channel.channel_name)
390         channel.on_channel_properties()
391
392     def remove_channel_edit_output_menuitem_by_label(self, widget, label):
393         if (widget.get_label() == label):
394             self.channel_edit_output_menu.remove(widget)
395
396     def on_remove_output_channel(self, widget, channel):
397         print('Removing channel "%s"' % channel.channel_name)
398         self.channel_remove_output_menu.remove(widget)
399         self.channel_edit_output_menu.foreach(
400             self.remove_channel_edit_output_menuitem_by_label,
401             channel.channel_name);
402         if self.monitored_channel is channel:
403             channel.monitor_button.set_active(False)
404         for i in range(len(self.channels)):
405             if self.output_channels[i] is channel:
406                 channel.unrealize()
407                 del self.output_channels[i]
408                 self.hbox_outputs.remove(channel.get_parent())
409                 break
410         if len(self.output_channels) == 0:
411             self.channel_remove_output_menu_item.set_sensitive(False)
412
413     def rename_channels(self, container, parameters):
414         if (container.get_label() == parameters['oldname']):
415             container.set_label(parameters['newname'])
416
417     def on_channel_rename(self, oldname, newname):
418         rename_parameters = { 'oldname' : oldname, 'newname' : newname }
419         self.channel_edit_input_menu.foreach(self.rename_channels,
420             rename_parameters)
421         self.channel_edit_output_menu.foreach(self.rename_channels,
422             rename_parameters)
423         self.channel_remove_input_menu.foreach(self.rename_channels,
424             rename_parameters)
425         self.channel_remove_output_menu.foreach(self.rename_channels,
426             rename_parameters)
427         print("Renaming channel from %s to %s\n" % (oldname, newname))
428
429
430     def on_channels_clear(self, widget):
431         for channel in self.output_channels:
432             channel.unrealize()
433             self.hbox_outputs.remove(channel.get_parent())
434         for channel in self.channels:
435             channel.unrealize()
436             self.hbox_inputs.remove(channel.get_parent())
437         self.channels = []
438         self.output_channels = []
439         self.channel_edit_input_menu = Gtk.Menu()
440         self.channel_edit_input_menu_item.set_submenu(self.channel_edit_input_menu)
441         self.channel_edit_input_menu_item.set_sensitive(False)
442         self.channel_remove_input_menu = Gtk.Menu()
443         self.channel_remove_input_menu_item.set_submenu(self.channel_remove_input_menu)
444         self.channel_remove_input_menu_item.set_sensitive(False)
445         self.channel_edit_output_menu = Gtk.Menu()
446         self.channel_edit_output_menu_item.set_submenu(self.channel_edit_output_menu)
447         self.channel_edit_output_menu_item.set_sensitive(False)
448         self.channel_remove_output_menu = Gtk.Menu()
449         self.channel_remove_output_menu_item.set_submenu(self.channel_remove_output_menu)
450         self.channel_remove_output_menu_item.set_sensitive(False)
451
452     def add_channel(self, name, stereo, volume_cc, balance_cc, mute_cc, solo_cc):
453         try:
454             channel = InputChannel(self, name, stereo)
455             self.add_channel_precreated(channel)
456         except Exception:
457             e = sys.exc_info()[0]
458             err = Gtk.MessageDialog(self.window,
459                             Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
460                             Gtk.MessageType.ERROR,
461                             Gtk.ButtonsType.OK,
462                             "Channel creation failed")
463             err.run()
464             err.destroy()
465             return
466         if volume_cc != -1:
467             channel.channel.volume_midi_cc = volume_cc
468         else:
469             channel.channel.autoset_volume_midi_cc()
470         if balance_cc != -1:
471             channel.channel.balance_midi_cc = balance_cc
472         else:
473             channel.channel.autoset_balance_midi_cc()
474         if mute_cc != -1:
475             channel.channel.mute_midi_cc = mute_cc
476         else:
477             channel.channel.autoset_mute_midi_cc()
478         if solo_cc != -1:
479             channel.channel.solo_midi_cc = solo_cc
480         else:
481             channel.channel.autoset_solo_midi_cc()
482         return channel
483
484     def add_channel_precreated(self, channel):
485         frame = Gtk.Frame()
486         frame.add(channel)
487         self.hbox_inputs.pack_start(frame, False, True, 0)
488         channel.realize()
489
490         channel_edit_menu_item = Gtk.MenuItem(label=channel.channel_name)
491         self.channel_edit_input_menu.append(channel_edit_menu_item)
492         channel_edit_menu_item.connect("activate", self.on_edit_input_channel, channel)
493         self.channel_edit_input_menu_item.set_sensitive(True)
494
495         channel_remove_menu_item = Gtk.MenuItem(label=channel.channel_name)
496         self.channel_remove_input_menu.append(channel_remove_menu_item)
497         channel_remove_menu_item.connect("activate", self.on_remove_input_channel, channel)
498         self.channel_remove_input_menu_item.set_sensitive(True)
499
500         self.channels.append(channel)
501
502         for outputchannel in self.output_channels:
503             channel.add_control_group(outputchannel)
504
505         # create post fader output channel matching the input channel
506         channel.post_fader_output_channel = self.mixer.add_output_channel(
507                         channel.channel.name + ' Out', channel.channel.is_stereo, True)
508         channel.post_fader_output_channel.volume = 0
509         channel.post_fader_output_channel.set_solo(channel.channel, True)
510
511     def read_meters(self):
512         for channel in self.channels:
513             channel.read_meter()
514         for channel in self.output_channels:
515             channel.read_meter()
516         return True
517
518     def midi_events_check(self):
519         for channel in self.channels + self.output_channels:
520             channel.midi_events_check()
521         return True
522
523     def add_output_channel(self, name, stereo, volume_cc, balance_cc, mute_cc,
524             display_solo_buttons, color):
525         try:
526             channel = OutputChannel(self, name, stereo)
527             channel.display_solo_buttons = display_solo_buttons
528             channel.color = color
529             self.add_output_channel_precreated(channel)
530         except Exception:
531             err = Gtk.MessageDialog(self.window,
532                             Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
533                             Gtk.MessageType.ERROR,
534                             Gtk.ButtonsType.OK,
535                             "Channel creation failed")
536             err.run()
537             err.destroy()
538             return
539
540         if volume_cc != -1:
541             channel.channel.volume_midi_cc = volume_cc
542         else:
543             channel.channel.autoset_volume_midi_cc()
544         if balance_cc != -1:
545             channel.channel.balance_midi_cc = balance_cc
546         else:
547             channel.channel.autoset_balance_midi_cc()
548         if mute_cc != -1:
549             channel.channel.mute_midi_cc = mute_cc
550         else:
551             channel.channel.autoset_mute_midi_cc()
552
553         return channel
554
555     def add_output_channel_precreated(self, channel):
556         frame = Gtk.Frame()
557         frame.add(channel)
558         self.hbox_outputs.pack_start(frame, False, True, 0)
559         channel.realize()
560
561         channel_edit_menu_item = Gtk.MenuItem(label=channel.channel_name)
562         self.channel_edit_output_menu.append(channel_edit_menu_item)
563         channel_edit_menu_item.connect("activate", self.on_edit_output_channel, channel)
564         self.channel_edit_output_menu_item.set_sensitive(True)
565
566         channel_remove_menu_item = Gtk.MenuItem(label=channel.channel_name)
567         self.channel_remove_output_menu.append(channel_remove_menu_item)
568         channel_remove_menu_item.connect("activate", self.on_remove_output_channel, channel)
569         self.channel_remove_output_menu_item.set_sensitive(True)
570
571         self.output_channels.append(channel)
572
573     _monitored_channel = None
574     def get_monitored_channel(self):
575         return self._monitored_channel
576
577     def set_monitored_channel(self, channel):
578         if self._monitored_channel:
579             if channel.channel.name == self._monitored_channel.channel.name:
580                 return
581         self._monitored_channel = channel
582         if type(channel) is InputChannel:
583             # reset all solo/mute settings
584             for in_channel in self.channels:
585                 self.monitor_channel.set_solo(in_channel.channel, False)
586                 self.monitor_channel.set_muted(in_channel.channel, False)
587             self.monitor_channel.set_solo(channel.channel, True)
588             self.monitor_channel.prefader = True
589         else:
590             self.monitor_channel.prefader = False
591         self.update_monitor(channel)
592     monitored_channel = property(get_monitored_channel, set_monitored_channel)
593
594     def update_monitor(self, channel):
595         if self.monitored_channel is not channel:
596             return
597         self.monitor_channel.volume = channel.channel.volume
598         self.monitor_channel.balance = channel.channel.balance
599         self.monitor_channel.out_mute = channel.channel.out_mute
600         if type(self.monitored_channel) is OutputChannel:
601             # sync solo/muted channels
602             for input_channel in self.channels:
603                 self.monitor_channel.set_solo(input_channel.channel,
604                                 channel.channel.is_solo(input_channel.channel))
605                 self.monitor_channel.set_muted(input_channel.channel,
606                                 channel.channel.is_muted(input_channel.channel))
607
608     def get_input_channel_by_name(self, name):
609         for input_channel in self.channels:
610             if input_channel.channel.name == name:
611                 return input_channel
612         return None
613
614     def on_about(self, *args):
615         about = Gtk.AboutDialog()
616         about.set_name('jack_mixer')
617         about.set_copyright('Copyright © 2006-2020\nNedko Arnaudov, Frederic Peters, Arnout Engelen, Daniel Sheeler')
618         about.set_license('''\
619 jack_mixer is free software; you can redistribute it and/or modify it
620 under the terms of the GNU General Public License as published by the
621 Free Software Foundation; either version 2 of the License, or (at your
622 option) any later version.
623
624 jack_mixer is distributed in the hope that it will be useful, but
625 WITHOUT ANY WARRANTY; without even the implied warranty of
626 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
627 General Public License for more details.
628
629 You should have received a copy of the GNU General Public License along
630 with jack_mixer; if not, write to the Free Software Foundation, Inc., 51
631 Franklin Street, Fifth Floor, Boston, MA 02110-130159 USA''')
632         about.set_authors(['Nedko Arnaudov <nedko@arnaudov.name>',
633                            'Frederic Peters <fpeters@0d.be>',
634                            'Daniel Sheeler <dsheeler@pobox.com>'])
635         about.set_logo_icon_name('jack_mixer')
636         about.set_website('https://rdio.space/jackmixer/')
637
638         about.run()
639         about.destroy()
640
641     def save_to_xml(self, file):
642         #print "Saving to XML..."
643         b = XmlSerialization()
644         s = Serializator()
645         s.serialize(self, b)
646         b.save(file)
647
648     def load_from_xml(self, file, silence_errors=False):
649         #print "Loading from XML..."
650         self.on_channels_clear(None)
651         self.unserialized_channels = []
652         b = XmlSerialization()
653         try:
654             b.load(file)
655         except:
656             if silence_errors:
657                 return
658             raise
659         s = Serializator()
660         s.unserialize(self, b)
661         for channel in self.unserialized_channels:
662             if isinstance(channel, InputChannel):
663                 if self._init_solo_channels and channel.channel_name in self._init_solo_channels:
664                     channel.solo = True
665                 self.add_channel_precreated(channel)
666         self._init_solo_channels = None
667         for channel in self.unserialized_channels:
668             if isinstance(channel, OutputChannel):
669                 self.add_output_channel_precreated(channel)
670         del self.unserialized_channels
671         if self.visible:
672             self.window.show_all()
673
674     def serialize(self, object_backend):
675         width, height = self.window.get_size()
676         object_backend.add_property('geometry',
677                         '%sx%s' % (width, height))
678         solo_channels = []
679         for input_channel in self.channels:
680             if input_channel.channel.solo:
681                 solo_channels.append(input_channel)
682         if solo_channels:
683             object_backend.add_property('solo_channels', '|'.join([x.channel.name for x in solo_channels]))
684         object_backend.add_property('visible', '%s' % str(self.visible))
685
686     def unserialize_property(self, name, value):
687         if name == 'geometry':
688             width, height = value.split('x')
689             self.window.resize(int(width), int(height))
690             return True
691         if name == 'solo_channels':
692             self._init_solo_channels = value.split('|')
693             return True
694         if name == 'visible':
695             self.visible = value == 'True'
696             return True
697
698     def unserialize_child(self, name):
699         if name == InputChannel.serialization_name():
700             channel = InputChannel(self, "", True)
701             self.unserialized_channels.append(channel)
702             return channel
703
704         if name == OutputChannel.serialization_name():
705             channel = OutputChannel(self, "", True)
706             self.unserialized_channels.append(channel)
707             return channel
708
709         if name == gui.Factory.serialization_name():
710             self.gui_factory = gui.Factory(self.window, self.meter_scales, self.slider_scales)
711             self.gui_factory.connect('midi-behavior-mode-changed', self.on_midi_behavior_mode_changed)
712             return self.gui_factory
713
714     def serialization_get_childs(self):
715         '''Get child objects that required and support serialization'''
716         childs = self.channels[:] + self.output_channels[:] + [self.gui_factory]
717         return childs
718
719     def serialization_name(self):
720         return "jack_mixer"
721
722     def main(self):
723         if not self.mixer:
724             return
725
726         if self.visible:
727             self.window.show_all()
728
729         signal.signal(signal.SIGUSR1, self.sighandler)
730         signal.signal(signal.SIGTERM, self.sighandler)
731         signal.signal(signal.SIGINT, self.sighandler)
732         signal.signal(signal.SIGHUP, signal.SIG_IGN)
733
734         Gtk.main()
735
736         #f = file("/dev/stdout", "w")
737         #self.save_to_xml(f)
738         #f.close
739
740 def main():
741     parser = ArgumentParser()
742     parser.add_argument('-c', '--config', help='use a non default configuration file')
743     parser.add_argument('client_name', metavar='NAME', nargs='?', default='jack_mixer',
744                         help='set JACK client name')
745     args = parser.parse_args()
746
747     try:
748         mixer = JackMixer(args.client_name)
749     except Exception as e:
750         err = Gtk.MessageDialog(parent = None,
751                                 modal = True,
752                                 message_type = Gtk.MessageType.ERROR,
753                                 buttons = Gtk.ButtonsType.OK,
754                                 text = "Mixer creation failed (%s)" % str(e))
755         err.run()
756         err.destroy()
757         sys.exit(1)
758
759     if not mixer.nsm_client and args.config:
760         f = open(args.config)
761         mixer.current_filename = args.config
762
763         try:
764             mixer.load_from_xml(f)
765         except Exception as e:
766             err = Gtk.MessageDialog(parent = mixer.window,
767                             modal =  True,
768                             message_type = Gtk.MessageType.ERROR,
769                             buttons = Gtk.ButtonsType.OK,
770                             text = "Failed loading settings (%s)." % str(e))
771             err.run()
772             err.destroy()
773         mixer.window.set_default_size(60*(1+len(mixer.channels)+len(mixer.output_channels)), 300)
774         f.close()
775
776     mixer.main()
777
778     mixer.cleanup()
779
780 if __name__ == "__main__":
781     main()