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