]> git.0d.be Git - jack_mixer.git/blob - jack_mixer.py
Add midi behavior modes: "pick up" and "jump to value"
[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         print('on_midi_behavior_mode_changed', value)
259         self.mixer.midi_behavior_mode = value
260
261     def on_delete_event(self, widget, event):
262         return False
263
264     def sighandler(self, signum, frame):
265         #print "Signal %d received" % signum
266         if signum == signal.SIGUSR1:
267             self.save = True
268         elif signum == signal.SIGTERM:
269             Gtk.main_quit()
270         elif signum == signal.SIGINT:
271             Gtk.main_quit()
272         else:
273             print("Unknown signal %d received" % signum)
274
275     def cleanup(self):
276         print("Cleaning jack_mixer")
277         if not self.mixer:
278             return
279
280         for channel in self.channels:
281             channel.unrealize()
282
283         self.mixer.destroy()
284
285     def on_open_cb(self, *args):
286         dlg = Gtk.FileChooserDialog(title='Open', parent=self.window,
287                         action=Gtk.FileChooserAction.OPEN,
288                         buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
289                                  Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
290         dlg.set_default_response(Gtk.ResponseType.OK)
291         if dlg.run() == Gtk.ResponseType.OK:
292             filename = dlg.get_filename()
293             try:
294                 f = open(filename, 'r')
295                 self.load_from_xml(f)
296             except:
297                 err = Gtk.MessageDialog(self.window,
298                             Gtk.DialogFlags.MODAL,
299                             Gtk.MessageType.ERROR,
300                             Gtk.ButtonsType.OK,
301                             "Failed loading settings.")
302                 err.run()
303                 err.destroy()
304             else:
305                 self.current_filename = filename
306             finally:
307                 f.close()
308         dlg.destroy()
309
310     def on_save_cb(self, *args):
311         if not self.current_filename:
312             return self.on_save_as_cb()
313         f = open(self.current_filename, 'w')
314         self.save_to_xml(f)
315         f.close()
316
317     def on_save_as_cb(self, *args):
318         dlg = Gtk.FileChooserDialog(title='Save', parent=self.window,
319                         action=Gtk.FileChooserAction.SAVE,
320                         buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
321                                  Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
322         dlg.set_default_response(Gtk.ResponseType.OK)
323         if dlg.run() == Gtk.ResponseType.OK:
324             self.current_filename = dlg.get_filename()
325             self.on_save_cb()
326         dlg.destroy()
327
328     def on_quit_cb(self, *args):
329         Gtk.main_quit()
330
331     preferences_dialog = None
332     def on_preferences_cb(self, widget):
333         if not self.preferences_dialog:
334             self.preferences_dialog = PreferencesDialog(self)
335         self.preferences_dialog.show()
336         self.preferences_dialog.present()
337
338     def on_add_input_channel(self, widget):
339         dialog = NewChannelDialog(app=self)
340         dialog.set_transient_for(self.window)
341         dialog.show()
342         ret = dialog.run()
343         dialog.hide()
344
345         if ret == Gtk.ResponseType.OK:
346             result = dialog.get_result()
347             channel = self.add_channel(**result)
348             if self.visible:
349                 self.window.show_all()
350
351     def on_add_output_channel(self, widget):
352         dialog = NewOutputChannelDialog(app=self)
353         dialog.set_transient_for(self.window)
354         dialog.show()
355         ret = dialog.run()
356         dialog.hide()
357
358         if ret == Gtk.ResponseType.OK:
359             result = dialog.get_result()
360             channel = self.add_output_channel(**result)
361             if self.visible:
362                 self.window.show_all()
363
364     def on_edit_input_channel(self, widget, channel):
365         print('Editing channel "%s"' % channel.channel_name)
366         channel.on_channel_properties()
367
368     def remove_channel_edit_input_menuitem_by_label(self, widget, label):
369         if (widget.get_label() == label):
370             self.channel_edit_input_menu.remove(widget)
371
372     def on_remove_input_channel(self, widget, channel):
373         print('Removing channel "%s"' % channel.channel_name)
374         self.channel_remove_input_menu.remove(widget)
375         self.channel_edit_input_menu.foreach(
376             self.remove_channel_edit_input_menuitem_by_label,
377             channel.channel_name);
378         if self.monitored_channel is channel:
379             channel.monitor_button.set_active(False)
380         for i in range(len(self.channels)):
381             if self.channels[i] is channel:
382                 channel.unrealize()
383                 del self.channels[i]
384                 self.hbox_inputs.remove(channel.get_parent())
385                 break
386         if len(self.channels) == 0:
387             self.channel_remove_input_menu_item.set_sensitive(False)
388
389     def on_edit_output_channel(self, widget, channel):
390         print('Editing channel "%s"' % channel.channel_name)
391         channel.on_channel_properties()
392
393     def remove_channel_edit_output_menuitem_by_label(self, widget, label):
394         if (widget.get_label() == label):
395             self.channel_edit_output_menu.remove(widget)
396
397     def on_remove_output_channel(self, widget, channel):
398         print('Removing channel "%s"' % channel.channel_name)
399         self.channel_remove_output_menu.remove(widget)
400         self.channel_edit_output_menu.foreach(
401             self.remove_channel_edit_output_menuitem_by_label,
402             channel.channel_name);
403         if self.monitored_channel is channel:
404             channel.monitor_button.set_active(False)
405         for i in range(len(self.channels)):
406             if self.output_channels[i] is channel:
407                 channel.unrealize()
408                 del self.output_channels[i]
409                 self.hbox_outputs.remove(channel.get_parent())
410                 break
411         if len(self.output_channels) == 0:
412             self.channel_remove_output_menu_item.set_sensitive(False)
413
414     def rename_channels(self, container, parameters):
415         if (container.get_label() == parameters['oldname']):
416             container.set_label(parameters['newname'])
417
418     def on_channel_rename(self, oldname, newname):
419         rename_parameters = { 'oldname' : oldname, 'newname' : newname }
420         self.channel_edit_input_menu.foreach(self.rename_channels,
421             rename_parameters)
422         self.channel_edit_output_menu.foreach(self.rename_channels,
423             rename_parameters)
424         self.channel_remove_input_menu.foreach(self.rename_channels,
425             rename_parameters)
426         self.channel_remove_output_menu.foreach(self.rename_channels,
427             rename_parameters)
428         print("Renaming channel from %s to %s\n" % (oldname, newname))
429
430
431     def on_channels_clear(self, widget):
432         for channel in self.output_channels:
433             channel.unrealize()
434             self.hbox_outputs.remove(channel.get_parent())
435         for channel in self.channels:
436             channel.unrealize()
437             self.hbox_inputs.remove(channel.get_parent())
438         self.channels = []
439         self.output_channels = []
440         self.channel_edit_input_menu = Gtk.Menu()
441         self.channel_edit_input_menu_item.set_submenu(self.channel_edit_input_menu)
442         self.channel_edit_input_menu_item.set_sensitive(False)
443         self.channel_remove_input_menu = Gtk.Menu()
444         self.channel_remove_input_menu_item.set_submenu(self.channel_remove_input_menu)
445         self.channel_remove_input_menu_item.set_sensitive(False)
446         self.channel_edit_output_menu = Gtk.Menu()
447         self.channel_edit_output_menu_item.set_submenu(self.channel_edit_output_menu)
448         self.channel_edit_output_menu_item.set_sensitive(False)
449         self.channel_remove_output_menu = Gtk.Menu()
450         self.channel_remove_output_menu_item.set_submenu(self.channel_remove_output_menu)
451         self.channel_remove_output_menu_item.set_sensitive(False)
452
453     def add_channel(self, name, stereo, volume_cc, balance_cc, mute_cc, solo_cc):
454         try:
455             channel = InputChannel(self, name, stereo)
456             self.add_channel_precreated(channel)
457         except Exception:
458             e = sys.exc_info()[0]
459             err = Gtk.MessageDialog(self.window,
460                             Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
461                             Gtk.MessageType.ERROR,
462                             Gtk.ButtonsType.OK,
463                             "Channel creation failed")
464             err.run()
465             err.destroy()
466             return
467         if volume_cc != -1:
468             channel.channel.volume_midi_cc = volume_cc
469         else:
470             channel.channel.autoset_volume_midi_cc()
471         if balance_cc != -1:
472             channel.channel.balance_midi_cc = balance_cc
473         else:
474             channel.channel.autoset_balance_midi_cc()
475         if mute_cc != -1:
476             channel.channel.mute_midi_cc = mute_cc
477         else:
478             channel.channel.autoset_mute_midi_cc()
479         if solo_cc != -1:
480             channel.channel.solo_midi_cc = solo_cc
481         else:
482             channel.channel.autoset_solo_midi_cc()
483         return channel
484
485     def add_channel_precreated(self, channel):
486         frame = Gtk.Frame()
487         frame.add(channel)
488         self.hbox_inputs.pack_start(frame, False, True, 0)
489         channel.realize()
490
491         channel_edit_menu_item = Gtk.MenuItem(label=channel.channel_name)
492         self.channel_edit_input_menu.append(channel_edit_menu_item)
493         channel_edit_menu_item.connect("activate", self.on_edit_input_channel, channel)
494         self.channel_edit_input_menu_item.set_sensitive(True)
495
496         channel_remove_menu_item = Gtk.MenuItem(label=channel.channel_name)
497         self.channel_remove_input_menu.append(channel_remove_menu_item)
498         channel_remove_menu_item.connect("activate", self.on_remove_input_channel, channel)
499         self.channel_remove_input_menu_item.set_sensitive(True)
500
501         self.channels.append(channel)
502
503         for outputchannel in self.output_channels:
504             channel.add_control_group(outputchannel)
505
506         # create post fader output channel matching the input channel
507         channel.post_fader_output_channel = self.mixer.add_output_channel(
508                         channel.channel.name + ' Out', channel.channel.is_stereo, True)
509         channel.post_fader_output_channel.volume = 0
510         channel.post_fader_output_channel.set_solo(channel.channel, True)
511
512     def read_meters(self):
513         for channel in self.channels:
514             channel.read_meter()
515         for channel in self.output_channels:
516             channel.read_meter()
517         return True
518
519     def midi_events_check(self):
520         for channel in self.channels + self.output_channels:
521             channel.midi_events_check()
522         return True
523
524     def add_output_channel(self, name, stereo, volume_cc, balance_cc, mute_cc,
525             display_solo_buttons, color):
526         try:
527             channel = OutputChannel(self, name, stereo)
528             channel.display_solo_buttons = display_solo_buttons
529             channel.color = color
530             self.add_output_channel_precreated(channel)
531         except Exception:
532             err = Gtk.MessageDialog(self.window,
533                             Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
534                             Gtk.MessageType.ERROR,
535                             Gtk.ButtonsType.OK,
536                             "Channel creation failed")
537             err.run()
538             err.destroy()
539             return
540
541         if volume_cc != -1:
542             channel.channel.volume_midi_cc = volume_cc
543         else:
544             channel.channel.autoset_volume_midi_cc()
545         if balance_cc != -1:
546             channel.channel.balance_midi_cc = balance_cc
547         else:
548             channel.channel.autoset_balance_midi_cc()
549         if mute_cc != -1:
550             channel.channel.mute_midi_cc = mute_cc
551         else:
552             channel.channel.autoset_mute_midi_cc()
553
554         return channel
555
556     def add_output_channel_precreated(self, channel):
557         frame = Gtk.Frame()
558         frame.add(channel)
559         self.hbox_outputs.pack_start(frame, False, True, 0)
560         channel.realize()
561
562         channel_edit_menu_item = Gtk.MenuItem(label=channel.channel_name)
563         self.channel_edit_output_menu.append(channel_edit_menu_item)
564         channel_edit_menu_item.connect("activate", self.on_edit_output_channel, channel)
565         self.channel_edit_output_menu_item.set_sensitive(True)
566
567         channel_remove_menu_item = Gtk.MenuItem(label=channel.channel_name)
568         self.channel_remove_output_menu.append(channel_remove_menu_item)
569         channel_remove_menu_item.connect("activate", self.on_remove_output_channel, channel)
570         self.channel_remove_output_menu_item.set_sensitive(True)
571
572         self.output_channels.append(channel)
573
574     _monitored_channel = None
575     def get_monitored_channel(self):
576         return self._monitored_channel
577
578     def set_monitored_channel(self, channel):
579         if self._monitored_channel:
580             if channel.channel.name == self._monitored_channel.channel.name:
581                 return
582         self._monitored_channel = channel
583         if type(channel) is InputChannel:
584             # reset all solo/mute settings
585             for in_channel in self.channels:
586                 self.monitor_channel.set_solo(in_channel.channel, False)
587                 self.monitor_channel.set_muted(in_channel.channel, False)
588             self.monitor_channel.set_solo(channel.channel, True)
589             self.monitor_channel.prefader = True
590         else:
591             self.monitor_channel.prefader = False
592         self.update_monitor(channel)
593     monitored_channel = property(get_monitored_channel, set_monitored_channel)
594
595     def update_monitor(self, channel):
596         if self.monitored_channel is not channel:
597             return
598         self.monitor_channel.volume = channel.channel.volume
599         self.monitor_channel.balance = channel.channel.balance
600         self.monitor_channel.out_mute = channel.channel.out_mute
601         if type(self.monitored_channel) is OutputChannel:
602             # sync solo/muted channels
603             for input_channel in self.channels:
604                 self.monitor_channel.set_solo(input_channel.channel,
605                                 channel.channel.is_solo(input_channel.channel))
606                 self.monitor_channel.set_muted(input_channel.channel,
607                                 channel.channel.is_muted(input_channel.channel))
608
609     def get_input_channel_by_name(self, name):
610         for input_channel in self.channels:
611             if input_channel.channel.name == name:
612                 return input_channel
613         return None
614
615     def on_about(self, *args):
616         about = Gtk.AboutDialog()
617         about.set_name('jack_mixer')
618         about.set_copyright('Copyright © 2006-2020\nNedko Arnaudov, Frederic Peters, Arnout Engelen, Daniel Sheeler')
619         about.set_license('''\
620 jack_mixer is free software; you can redistribute it and/or modify it
621 under the terms of the GNU General Public License as published by the
622 Free Software Foundation; either version 2 of the License, or (at your
623 option) any later version.
624
625 jack_mixer is distributed in the hope that it will be useful, but
626 WITHOUT ANY WARRANTY; without even the implied warranty of
627 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
628 General Public License for more details.
629
630 You should have received a copy of the GNU General Public License along
631 with jack_mixer; if not, write to the Free Software Foundation, Inc., 51
632 Franklin Street, Fifth Floor, Boston, MA 02110-130159 USA''')
633         about.set_authors(['Nedko Arnaudov <nedko@arnaudov.name>',
634                            'Frederic Peters <fpeters@0d.be>',
635                            'Daniel Sheeler <dsheeler@pobox.com>'])
636         about.set_logo_icon_name('jack_mixer')
637         about.set_website('https://rdio.space/jackmixer/')
638
639         about.run()
640         about.destroy()
641
642     def save_to_xml(self, file):
643         #print "Saving to XML..."
644         b = XmlSerialization()
645         s = Serializator()
646         s.serialize(self, b)
647         b.save(file)
648
649     def load_from_xml(self, file, silence_errors=False):
650         #print "Loading from XML..."
651         self.on_channels_clear(None)
652         self.unserialized_channels = []
653         b = XmlSerialization()
654         try:
655             b.load(file)
656         except:
657             if silence_errors:
658                 return
659             raise
660         s = Serializator()
661         s.unserialize(self, b)
662         for channel in self.unserialized_channels:
663             if isinstance(channel, InputChannel):
664                 if self._init_solo_channels and channel.channel_name in self._init_solo_channels:
665                     channel.solo = True
666                 self.add_channel_precreated(channel)
667         self._init_solo_channels = None
668         for channel in self.unserialized_channels:
669             if isinstance(channel, OutputChannel):
670                 self.add_output_channel_precreated(channel)
671         del self.unserialized_channels
672         if self.visible:
673             self.window.show_all()
674
675     def serialize(self, object_backend):
676         width, height = self.window.get_size()
677         object_backend.add_property('geometry',
678                         '%sx%s' % (width, height))
679         solo_channels = []
680         for input_channel in self.channels:
681             if input_channel.channel.solo:
682                 solo_channels.append(input_channel)
683         if solo_channels:
684             object_backend.add_property('solo_channels', '|'.join([x.channel.name for x in solo_channels]))
685         object_backend.add_property('visible', '%s' % str(self.visible))
686
687     def unserialize_property(self, name, value):
688         if name == 'geometry':
689             width, height = value.split('x')
690             self.window.resize(int(width), int(height))
691             return True
692         if name == 'solo_channels':
693             self._init_solo_channels = value.split('|')
694             return True
695         if name == 'visible':
696             self.visible = value == 'True'
697             return True
698
699     def unserialize_child(self, name):
700         if name == InputChannel.serialization_name():
701             channel = InputChannel(self, "", True)
702             self.unserialized_channels.append(channel)
703             return channel
704
705         if name == OutputChannel.serialization_name():
706             channel = OutputChannel(self, "", True)
707             self.unserialized_channels.append(channel)
708             return channel
709
710         if name == gui.Factory.serialization_name():
711             self.gui_factory = gui.Factory(self.window, self.meter_scales, self.slider_scales)
712             self.gui_factory.connect('midi-behavior-mode-changed', self.on_midi_behavior_mode_changed)
713             return self.gui_factory
714
715     def serialization_get_childs(self):
716         '''Get child objects that required and support serialization'''
717         childs = self.channels[:] + self.output_channels[:] + [self.gui_factory]
718         return childs
719
720     def serialization_name(self):
721         return "jack_mixer"
722
723     def main(self):
724         if not self.mixer:
725             return
726
727         if self.visible:
728             self.window.show_all()
729
730         signal.signal(signal.SIGUSR1, self.sighandler)
731         signal.signal(signal.SIGTERM, self.sighandler)
732         signal.signal(signal.SIGINT, self.sighandler)
733         signal.signal(signal.SIGHUP, signal.SIG_IGN)
734
735         Gtk.main()
736
737         #f = file("/dev/stdout", "w")
738         #self.save_to_xml(f)
739         #f.close
740
741 def main():
742     parser = ArgumentParser()
743     parser.add_argument('-c', '--config', help='use a non default configuration file')
744     parser.add_argument('client_name', metavar='NAME', nargs='?', default='jack_mixer',
745                         help='set JACK client name')
746     args = parser.parse_args()
747
748     try:
749         mixer = JackMixer(args.client_name)
750     except Exception as e:
751         err = Gtk.MessageDialog(None,
752                                 Gtk.DialogFlags.MODAL,
753                                 Gtk.MessageType.ERROR,
754                                 Gtk.ButtonsType.OK,
755                                 "Mixer creation failed (%s)" % str(e))
756         err.run()
757         err.destroy()
758         sys.exit(1)
759
760     if not mixer.nsm_client and args.config:
761         f = open(args.config)
762         mixer.current_filename = args.config
763
764         try:
765             mixer.load_from_xml(f)
766         except:
767             err = Gtk.MessageDialog(mixer.window,
768                             Gtk.DialogFlags.MODAL,
769                             Gtk.MessageType.ERROR,
770                             Gtk.ButtonsType.OK,
771                             "Failed loading settings.")
772             err.run()
773             err.destroy()
774         mixer.window.set_default_size(60*(1+len(mixer.channels)+len(mixer.output_channels)), 300)
775         f.close()
776
777     mixer.main()
778
779     mixer.cleanup()
780
781 if __name__ == "__main__":
782     main()