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