]> git.0d.be Git - jack_mixer.git/blob - jack_mixer.py
6a00cf626d7b615925f3cbed75b4a528671d8aff
[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 from optparse import OptionParser
23
24 import gi
25 gi.require_version('Gtk', '3.0')
26 from gi.repository import Gtk
27 from gi.repository import GObject
28 from gi.repository import GLib
29 import sys
30 import os
31 import signal
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
39 import jack_mixer_c
40
41 import scale
42 from channel import *
43
44 import gui
45 from preferences import PreferencesDialog
46
47 from serialization_xml import XmlSerialization
48 from serialization import SerializedObject, Serializator
49
50 from nsmclient import NSMClient
51
52 # restore Python modules lookup path
53 sys.path = old_path
54
55 class JackMixer(SerializedObject):
56
57     # scales suitable as meter scales
58     meter_scales = [scale.IEC268(), scale.Linear70dB(), scale.IEC268Minimalistic()]
59
60     # scales suitable as volume slider scales
61     slider_scales = [scale.Linear30dB(), scale.Linear70dB()]
62
63     # name of settngs file that is currently open
64     current_filename = None
65
66     _init_solo_channels = None
67
68     def __init__(self):
69         self.visible = False
70         self.nsm_client = None
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             self.visible = True
84             self.create_mixer('jack_mixer', with_nsm = False)
85
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         self.window.set_title(client_name)
93
94         self.monitor_channel = self.mixer.add_output_channel("Monitor", True, True)
95         self.save = False
96
97         GLib.timeout_add(80, self.read_meters)
98         if with_nsm:
99             GLib.timeout_add(200, self.nsm_react)
100         GLib.timeout_add(50, self.midi_events_check)
101
102     def create_ui(self, with_nsm):
103         self.channels = []
104         self.output_channels = []
105         self.window = Gtk.Window(type=Gtk.WindowType.TOPLEVEL)
106         self.window.set_icon_name('jack_mixer')
107         self.gui_factory = gui.Factory(self.window, self.meter_scales, self.slider_scales)
108
109         self.vbox_top = Gtk.VBox()
110         self.window.add(self.vbox_top)
111
112         self.menubar = Gtk.MenuBar()
113         self.vbox_top.pack_start(self.menubar, False, True, 0)
114
115         mixer_menu_item = Gtk.MenuItem.new_with_mnemonic("_Mixer")
116         self.menubar.append(mixer_menu_item)
117         edit_menu_item = Gtk.MenuItem.new_with_mnemonic('_Edit')
118         self.menubar.append(edit_menu_item)
119         help_menu_item = Gtk.MenuItem.new_with_mnemonic('_Help')
120         self.menubar.append(help_menu_item)
121
122         self.window.set_default_size(120, 300)
123
124         self.mixer_menu = Gtk.Menu()
125         mixer_menu_item.set_submenu(self.mixer_menu)
126
127         add_input_channel = Gtk.MenuItem.new_with_mnemonic('New _Input Channel')
128         self.mixer_menu.append(add_input_channel)
129         add_input_channel.connect("activate", self.on_add_input_channel)
130
131         add_output_channel = Gtk.MenuItem.new_with_mnemonic('New _Output Channel')
132         self.mixer_menu.append(add_output_channel)
133         add_output_channel.connect("activate", self.on_add_output_channel)
134
135         self.mixer_menu.append(Gtk.SeparatorMenuItem())
136         if not with_nsm:
137             open = Gtk.MenuItem.new_with_mnemonic('_Open')
138             self.mixer_menu.append(open)
139             open.connect('activate', self.on_open_cb)
140         save = Gtk.MenuItem.new_with_mnemonic('_Save')
141         self.mixer_menu.append(save)
142         save.connect('activate', self.on_save_cb)
143         if not with_nsm:
144             save_as = Gtk.MenuItem.new_with_mnemonic('Save_As')
145             self.mixer_menu.append(save_as)
146             save_as.connect('activate', self.on_save_as_cb)
147
148         self.mixer_menu.append(Gtk.SeparatorMenuItem())
149
150         quit = Gtk.MenuItem.new_with_mnemonic('_Quit')
151         self.mixer_menu.append(quit)
152         quit.connect('activate', self.on_quit_cb)
153
154         edit_menu = Gtk.Menu()
155         edit_menu_item.set_submenu(edit_menu)
156
157         self.channel_edit_input_menu_item = Gtk.MenuItem.new_with_mnemonic('_Edit Input Channel')
158         edit_menu.append(self.channel_edit_input_menu_item)
159         self.channel_edit_input_menu = Gtk.Menu()
160         self.channel_edit_input_menu_item.set_submenu(self.channel_edit_input_menu)
161
162         self.channel_edit_output_menu_item = Gtk.MenuItem.new_with_mnemonic('Edit _Output Channel')
163         edit_menu.append(self.channel_edit_output_menu_item)
164         self.channel_edit_output_menu = Gtk.Menu()
165         self.channel_edit_output_menu_item.set_submenu(self.channel_edit_output_menu)
166
167         self.channel_remove_input_menu_item = Gtk.MenuItem.new_with_mnemonic('Remove _Input Channel')
168         edit_menu.append(self.channel_remove_input_menu_item)
169         self.channel_remove_input_menu = Gtk.Menu()
170         self.channel_remove_input_menu_item.set_submenu(self.channel_remove_input_menu)
171
172         self.channel_remove_output_menu_item = Gtk.MenuItem.new_with_mnemonic('_Remove Output Channel')
173         edit_menu.append(self.channel_remove_output_menu_item)
174         self.channel_remove_output_menu = Gtk.Menu()
175         self.channel_remove_output_menu_item.set_submenu(self.channel_remove_output_menu)
176
177         channel_remove_all_menu_item = Gtk.MenuItem.new_with_mnemonic('Clear')
178         edit_menu.append(channel_remove_all_menu_item)
179         channel_remove_all_menu_item.connect("activate", self.on_channels_clear)
180
181         edit_menu.append(Gtk.SeparatorMenuItem())
182
183         preferences = Gtk.MenuItem.new_with_mnemonic('_Preferences')
184         preferences.connect('activate', self.on_preferences_cb)
185         edit_menu.append(preferences)
186
187         help_menu = Gtk.Menu()
188         help_menu_item.set_submenu(help_menu)
189
190         about = Gtk.MenuItem.new_with_mnemonic('_About')
191         help_menu.append(about)
192         about.connect("activate", self.on_about)
193
194         self.hbox_top = Gtk.HBox()
195         self.vbox_top.pack_start(self.hbox_top, True, True, 0)
196
197         self.scrolled_window = Gtk.ScrolledWindow()
198         self.hbox_top.pack_start(self.scrolled_window, True, True, 0)
199
200         self.hbox_inputs = Gtk.HBox()
201         self.hbox_inputs.set_spacing(0)
202         self.hbox_inputs.set_border_width(0)
203         self.hbox_top.set_spacing(0)
204         self.hbox_top.set_border_width(0)
205
206         self.scrolled_window.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
207         self.scrolled_window.add(self.hbox_inputs)
208
209         self.hbox_outputs = Gtk.HBox()
210         self.hbox_outputs.set_spacing(0)
211         self.hbox_outputs.set_border_width(0)
212         frame = Gtk.Frame()
213         self.hbox_outputs.pack_start(frame, False, True, 0)
214         self.hbox_top.pack_start(self.hbox_outputs, False, True, 0)
215
216         self.window.connect("destroy", Gtk.main_quit)
217
218         self.window.connect('delete-event', self.on_delete_event)
219
220     def nsm_react(self):
221         self.nsm_client.reactToMessage()
222         return True
223
224     def nsm_hide_cb(self):
225         self.window.hide()
226         self.visible = False
227         self.nsm_client.announceGuiVisibility(False)
228
229     def nsm_show_cb(self):
230         self.window.show_all()
231         self.visible = True
232         self.nsm_client.announceGuiVisibility(True)
233
234     def nsm_open_cb(self, path, session_name, client_name):
235         self.create_mixer(client_name, with_nsm = True)
236
237         if os.path.isfile(path):
238             f = open(path, 'r')
239             self.load_from_xml(f)
240             f.close()
241         else:
242             f = open(path, 'w')
243             f.close()
244         self.current_filename = path
245
246     def nsm_save_cb(self, path, session_name, client_name):
247         f = open(path, 'w')
248         self.save_to_xml(f)
249         f.close()
250
251     def nsm_exit_cb(self, path, session_name, client_name):
252         Gtk.main_quit()
253
254     def on_delete_event(self, widget, event):
255         return False
256
257     def sighandler(self, signum, frame):
258         #print "Signal %d received" % signum
259         if signum == signal.SIGUSR1:
260             self.save = True
261         elif signum == signal.SIGTERM:
262             Gtk.main_quit()
263         elif signum == signal.SIGINT:
264             Gtk.main_quit()
265         else:
266             print("Unknown signal %d received" % signum)
267
268     def cleanup(self):
269         print("Cleaning jack_mixer")
270         if not self.mixer:
271             return
272
273         for channel in self.channels:
274             channel.unrealize()
275
276         self.mixer.destroy()
277
278     def on_open_cb(self, *args):
279         dlg = Gtk.FileChooserDialog(title='Open', parent=self.window,
280                         action=Gtk.FileChooserAction.OPEN,
281                         buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
282                                  Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
283         dlg.set_default_response(Gtk.ResponseType.OK)
284         if dlg.run() == Gtk.ResponseType.OK:
285             filename = dlg.get_filename()
286             try:
287                 f = open(filename, 'r')
288                 self.load_from_xml(f)
289             except:
290                 err = Gtk.MessageDialog(self.window,
291                             Gtk.DialogFlags.MODAL,
292                             Gtk.MessageType.ERROR,
293                             Gtk.ButtonsType.OK,
294                             "Failed loading settings.")
295                 err.run()
296                 err.destroy()
297             else:
298                 self.current_filename = filename
299             finally:
300                 f.close()
301         dlg.destroy()
302
303     def on_save_cb(self, *args):
304         if not self.current_filename:
305             return self.on_save_as_cb()
306         f = open(self.current_filename, 'w')
307         self.save_to_xml(f)
308         f.close()
309
310     def on_save_as_cb(self, *args):
311         dlg = Gtk.FileChooserDialog(title='Save', parent=self.window,
312                         action=Gtk.FileChooserAction.SAVE,
313                         buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
314                                  Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
315         dlg.set_default_response(Gtk.ResponseType.OK)
316         if dlg.run() == Gtk.ResponseType.OK:
317             self.current_filename = dlg.get_filename()
318             self.on_save_cb()
319         dlg.destroy()
320
321     def on_quit_cb(self, *args):
322         Gtk.main_quit()
323
324     preferences_dialog = None
325     def on_preferences_cb(self, widget):
326         if not self.preferences_dialog:
327             self.preferences_dialog = PreferencesDialog(self)
328         self.preferences_dialog.show()
329         self.preferences_dialog.present()
330
331     def on_add_input_channel(self, widget):
332         dialog = NewChannelDialog(app=self)
333         dialog.set_transient_for(self.window)
334         dialog.show()
335         ret = dialog.run()
336         dialog.hide()
337
338         if ret == Gtk.ResponseType.OK:
339             result = dialog.get_result()
340             channel = self.add_channel(**result)
341             if self.visible:
342                 self.window.show_all()
343
344     def on_add_output_channel(self, widget):
345         dialog = NewOutputChannelDialog(app=self)
346         dialog.set_transient_for(self.window)
347         dialog.show()
348         ret = dialog.run()
349         dialog.hide()
350
351         if ret == Gtk.ResponseType.OK:
352             result = dialog.get_result()
353             channel = self.add_output_channel(**result)
354             if self.visible:
355                 self.window.show_all()
356
357     def on_edit_input_channel(self, widget, channel):
358         print('Editing channel "%s"' % channel.channel_name)
359         channel.on_channel_properties()
360
361     def remove_channel_edit_input_menuitem_by_label(self, widget, label):
362         if (widget.get_label() == label):
363             self.channel_edit_input_menu.remove(widget)
364
365     def on_remove_input_channel(self, widget, channel):
366         print('Removing channel "%s"' % channel.channel_name)
367         self.channel_remove_input_menu.remove(widget)
368         self.channel_edit_input_menu.foreach(
369             self.remove_channel_edit_input_menuitem_by_label,
370             channel.channel_name);
371         if self.monitored_channel is channel:
372             channel.monitor_button.set_active(False)
373         for i in range(len(self.channels)):
374             if self.channels[i] is channel:
375                 channel.unrealize()
376                 del self.channels[i]
377                 self.hbox_inputs.remove(channel.get_parent())
378                 break
379         if len(self.channels) == 0:
380             self.channel_remove_input_menu_item.set_sensitive(False)
381
382     def on_edit_output_channel(self, widget, channel):
383         print('Editing channel "%s"' % channel.channel_name)
384         channel.on_channel_properties()
385
386     def remove_channel_edit_output_menuitem_by_label(self, widget, label):
387         if (widget.get_label() == label):
388             self.channel_edit_output_menu.remove(widget)
389
390     def on_remove_output_channel(self, widget, channel):
391         print('Removing channel "%s"' % channel.channel_name)
392         self.channel_remove_output_menu.remove(widget)
393         self.channel_edit_output_menu.foreach(
394             self.remove_channel_edit_output_menuitem_by_label,
395             channel.channel_name);
396         if self.monitored_channel is channel:
397             channel.monitor_button.set_active(False)
398         for i in range(len(self.channels)):
399             if self.output_channels[i] is channel:
400                 channel.unrealize()
401                 del self.output_channels[i]
402                 self.hbox_outputs.remove(channel.get_parent())
403                 break
404         if len(self.output_channels) == 0:
405             self.channel_remove_output_menu_item.set_sensitive(False)
406
407     def rename_channels(self, container, parameters):
408         if (container.get_label() == parameters['oldname']):
409             container.set_label(parameters['newname'])
410
411     def on_channel_rename(self, oldname, newname):
412         rename_parameters = { 'oldname' : oldname, 'newname' : newname }
413         self.channel_edit_input_menu.foreach(self.rename_channels,
414             rename_parameters)
415         self.channel_edit_output_menu.foreach(self.rename_channels,
416             rename_parameters)
417         self.channel_remove_input_menu.foreach(self.rename_channels,
418             rename_parameters)
419         self.channel_remove_output_menu.foreach(self.rename_channels,
420             rename_parameters)
421         print("Renaming channel from %s to %s\n" % (oldname, newname))
422
423
424     def on_channels_clear(self, widget):
425         for channel in self.output_channels:
426             channel.unrealize()
427             self.hbox_outputs.remove(channel.get_parent())
428         for channel in self.channels:
429             channel.unrealize()
430             self.hbox_inputs.remove(channel.get_parent())
431         self.channels = []
432         self.output_channels = []
433         self.channel_edit_input_menu = Gtk.Menu()
434         self.channel_edit_input_menu_item.set_submenu(self.channel_edit_input_menu)
435         self.channel_edit_input_menu_item.set_sensitive(False)
436         self.channel_remove_input_menu = Gtk.Menu()
437         self.channel_remove_input_menu_item.set_submenu(self.channel_remove_input_menu)
438         self.channel_remove_input_menu_item.set_sensitive(False)
439         self.channel_edit_output_menu = Gtk.Menu()
440         self.channel_edit_output_menu_item.set_submenu(self.channel_edit_output_menu)
441         self.channel_edit_output_menu_item.set_sensitive(False)
442         self.channel_remove_output_menu = Gtk.Menu()
443         self.channel_remove_output_menu_item.set_submenu(self.channel_remove_output_menu)
444         self.channel_remove_output_menu_item.set_sensitive(False)
445
446     def add_channel(self, name, stereo, volume_cc, balance_cc, mute_cc, solo_cc):
447         try:
448             channel = InputChannel(self, name, stereo)
449             self.add_channel_precreated(channel)
450         except Exception:
451             e = sys.exc_info()[0]
452             err = Gtk.MessageDialog(self.window,
453                             Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
454                             Gtk.MessageType.ERROR,
455                             Gtk.ButtonsType.OK,
456                             "Channel creation failed")
457             err.run()
458             err.destroy()
459             return
460         if volume_cc != -1:
461             channel.channel.volume_midi_cc = volume_cc
462         else:
463             channel.channel.autoset_volume_midi_cc()
464         if balance_cc != -1:
465             channel.channel.balance_midi_cc = balance_cc
466         else:
467             channel.channel.autoset_balance_midi_cc()
468         if mute_cc != -1:
469             channel.channel.mute_midi_cc = mute_cc
470         else:
471             channel.channel.autoset_mute_midi_cc()
472         if solo_cc != -1:
473             channel.channel.solo_midi_cc = solo_cc
474         else:
475             channel.channel.autoset_solo_midi_cc()
476         return channel
477
478     def add_channel_precreated(self, channel):
479         frame = Gtk.Frame()
480         frame.add(channel)
481         self.hbox_inputs.pack_start(frame, False, True, 0)
482         channel.realize()
483
484         channel_edit_menu_item = Gtk.MenuItem(label=channel.channel_name)
485         self.channel_edit_input_menu.append(channel_edit_menu_item)
486         channel_edit_menu_item.connect("activate", self.on_edit_input_channel, channel)
487         self.channel_edit_input_menu_item.set_sensitive(True)
488
489         channel_remove_menu_item = Gtk.MenuItem(label=channel.channel_name)
490         self.channel_remove_input_menu.append(channel_remove_menu_item)
491         channel_remove_menu_item.connect("activate", self.on_remove_input_channel, channel)
492         self.channel_remove_input_menu_item.set_sensitive(True)
493
494         self.channels.append(channel)
495
496         for outputchannel in self.output_channels:
497             channel.add_control_group(outputchannel)
498
499         # create post fader output channel matching the input channel
500         channel.post_fader_output_channel = self.mixer.add_output_channel(
501                         channel.channel.name + ' Out', channel.channel.is_stereo, True)
502         channel.post_fader_output_channel.volume = 0
503         channel.post_fader_output_channel.set_solo(channel.channel, True)
504
505     def read_meters(self):
506         for channel in self.channels:
507             channel.read_meter()
508         for channel in self.output_channels:
509             channel.read_meter()
510         return True
511
512     def midi_events_check(self):
513         for channel in self.channels + self.output_channels:
514             channel.midi_events_check()
515         return True
516
517     def add_output_channel(self, name, stereo, volume_cc, balance_cc, mute_cc,
518             display_solo_buttons, color):
519         try:
520             channel = OutputChannel(self, name, stereo)
521             channel.display_solo_buttons = display_solo_buttons
522             channel.color = color
523             self.add_output_channel_precreated(channel)
524         except Exception:
525             err = Gtk.MessageDialog(self.window,
526                             Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
527                             Gtk.MessageType.ERROR,
528                             Gtk.ButtonsType.OK,
529                             "Channel creation failed")
530             err.run()
531             err.destroy()
532             return
533
534         if volume_cc != -1:
535             channel.channel.volume_midi_cc = volume_cc
536         else:
537             channel.channel.autoset_volume_midi_cc()
538         if balance_cc != -1:
539             channel.channel.balance_midi_cc = balance_cc
540         else:
541             channel.channel.autoset_balance_midi_cc()
542         if mute_cc != -1:
543             channel.channel.mute_midi_cc = mute_cc
544         else:
545             channel.channel.autoset_mute_midi_cc()
546
547         return channel
548
549     def add_output_channel_precreated(self, channel):
550         frame = Gtk.Frame()
551         frame.add(channel)
552         self.hbox_outputs.pack_start(frame, False, True, 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, Frederic Peters, 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(['Nedko Arnaudov <nedko@arnaudov.name>',
627                            'Frederic Peters <fpeters@0d.be>',
628                            'Daniel Sheeler <dsheeler@pobox.com>'])
629         about.set_logo_icon_name('jack_mixer')
630         about.set_website('https://rdio.space/jackmixer/')
631
632         about.run()
633         about.destroy()
634
635     def save_to_xml(self, file):
636         #print "Saving to XML..."
637         b = XmlSerialization()
638         s = Serializator()
639         s.serialize(self, b)
640         b.save(file)
641
642     def load_from_xml(self, file, silence_errors=False):
643         #print "Loading from XML..."
644         self.on_channels_clear(None)
645         self.unserialized_channels = []
646         b = XmlSerialization()
647         try:
648             b.load(file)
649         except:
650             if silence_errors:
651                 return
652             raise
653         s = Serializator()
654         s.unserialize(self, b)
655         for channel in self.unserialized_channels:
656             if isinstance(channel, InputChannel):
657                 if self._init_solo_channels and channel.channel_name in self._init_solo_channels:
658                     channel.solo = True
659                 self.add_channel_precreated(channel)
660         self._init_solo_channels = None
661         for channel in self.unserialized_channels:
662             if isinstance(channel, OutputChannel):
663                 self.add_output_channel_precreated(channel)
664         del self.unserialized_channels
665         if self.visible:
666             self.window.show_all()
667
668     def serialize(self, object_backend):
669         width, height = self.window.get_size()
670         object_backend.add_property('geometry',
671                         '%sx%s' % (width, height))
672         solo_channels = []
673         for input_channel in self.channels:
674             if input_channel.channel.solo:
675                 solo_channels.append(input_channel)
676         if solo_channels:
677             object_backend.add_property('solo_channels', '|'.join([x.channel.name for x in solo_channels]))
678         object_backend.add_property('visible', '%s' % str(self.visible))
679
680     def unserialize_property(self, name, value):
681         if name == 'geometry':
682             width, height = value.split('x')
683             self.window.resize(int(width), int(height))
684             return True
685         if name == 'solo_channels':
686             self._init_solo_channels = value.split('|')
687             return True
688         if name == 'visible':
689             self.visible = value == 'True'
690             return True
691
692     def unserialize_child(self, name):
693         if name == InputChannel.serialization_name():
694             channel = InputChannel(self, "", True)
695             self.unserialized_channels.append(channel)
696             return channel
697
698         if name == OutputChannel.serialization_name():
699             channel = OutputChannel(self, "", True)
700             self.unserialized_channels.append(channel)
701             return channel
702
703     def serialization_get_childs(self):
704         '''Get child objects tha required and support serialization'''
705         childs = self.channels[:] + self.output_channels[:]
706         return childs
707
708     def serialization_name(self):
709         return "jack_mixer"
710
711     def main(self):
712         if not self.mixer:
713             return
714
715         if self.visible:
716             self.window.show_all()
717
718         signal.signal(signal.SIGUSR1, self.sighandler)
719         signal.signal(signal.SIGTERM, self.sighandler)
720         signal.signal(signal.SIGINT, self.sighandler)
721         signal.signal(signal.SIGHUP, signal.SIG_IGN)
722
723         Gtk.main()
724
725         #f = file("/dev/stdout", "w")
726         #self.save_to_xml(f)
727         #f.close
728
729 def help():
730     print("Usage: %s [mixer_name]" % sys.argv[0])
731
732 def main():
733     parser = OptionParser(usage='usage: %prog [options] [jack_client_name]')
734     parser.add_option('-c', '--config', dest='config',
735                       help='use a non default configuration file')
736     # --help is passed.
737     options, args = parser.parse_args()
738
739     mixer = None
740
741     if len(args) == 1:
742         name = args[0]
743     else:
744         name = None
745
746         try:
747             mixer = JackMixer()
748         except Exception as e:
749             err = Gtk.MessageDialog(None,
750                                     Gtk.DialogFlags.MODAL,
751                                     Gtk.MessageType.ERROR,
752                                     Gtk.ButtonsType.OK,
753                                     "Mixer creation failed (%s)" % str(e))
754             err.run()
755             err.destroy()
756             sys.exit(1)
757
758     if not mixer.nsm_client and options.config:
759         f = open(options.config)
760         mixer.current_filename = options.config
761         try:
762             mixer.load_from_xml(f)
763         except:
764             err = Gtk.MessageDialog(mixer.window,
765                             Gtk.DialogFlags.MODAL,
766                             Gtk.MessageType.ERROR,
767                             Gtk.ButtonsType.OK,
768                             "Failed loading settings.")
769             err.run()
770             err.destroy()
771         mixer.window.set_default_size(60*(1+len(mixer.channels)+len(mixer.output_channels)), 300)
772         f.close()
773
774     mixer.main()
775
776     mixer.cleanup()
777
778 if __name__ == "__main__":
779     main()