]> git.0d.be Git - jack_mixer.git/blobdiff - jack_mixer.py
Set version to 14 in preparation for next release
[jack_mixer.git] / jack_mixer.py
index 01ec7a049c38832baeee6b76fd87ddec3eb70f20..3c13d4abc02f5c267cefe098b661383b439cdbf5 100755 (executable)
@@ -21,6 +21,7 @@
 
 import logging
 import os
+import re
 import signal
 import sys
 from argparse import ArgumentParser
@@ -45,20 +46,43 @@ from nsmclient import NSMClient
 from serialization_xml import XmlSerialization
 from serialization import SerializedObject, Serializator
 from preferences import PreferencesDialog
+from version import __version__
+
 
 # restore Python modules lookup path
 sys.path = old_path
 log = logging.getLogger("jack_mixer")
 
+
+def add_number_suffix(s):
+    def inc(match):
+        return str(int(match.group(0)) + 1)
+
+    new_s = re.sub('(\d+)\s*$', inc, s)
+    if new_s == s:
+        new_s = s + ' 1'
+
+    return new_s
+
+
 class JackMixer(SerializedObject):
 
     # scales suitable as meter scales
-    meter_scales = [scale.K20(), scale.K14(), scale.IEC268(), scale.Linear70dB(), scale.IEC268Minimalistic()]
+    meter_scales = [
+        scale.K20(),
+        scale.K14(),
+        scale.IEC268(),
+        scale.Linear70dB(),
+        scale.IEC268Minimalistic()
+    ]
 
     # scales suitable as volume slider scales
-    slider_scales = [scale.Linear30dB(), scale.Linear70dB()]
+    slider_scales = [
+        scale.Linear30dB(),
+        scale.Linear70dB()
+    ]
 
-    # name of settngs file that is currently open
+    # name of settings file that is currently open
     current_filename = None
 
     _init_solo_channels = None
@@ -68,36 +92,37 @@ class JackMixer(SerializedObject):
         self.nsm_client = None
 
         if os.environ.get('NSM_URL'):
-            self.nsm_client = NSMClient(prettyName = "jack_mixer",
-                                        saveCallback = self.nsm_save_cb,
-                                        openOrNewCallback = self.nsm_open_cb,
-                                        supportsSaveStatus = False,
-                                        hideGUICallback = self.nsm_hide_cb,
-                                        showGUICallback = self.nsm_show_cb,
-                                        exitProgramCallback = self.nsm_exit_cb,
-                                        loggingLevel = "error",
-                                       )
+            self.nsm_client = NSMClient(
+                prettyName="jack_mixer",
+                saveCallback=self.nsm_save_cb,
+                openOrNewCallback=self.nsm_open_cb,
+                supportsSaveStatus=False,
+                hideGUICallback=self.nsm_hide_cb,
+                showGUICallback=self.nsm_show_cb,
+                exitProgramCallback=self.nsm_exit_cb,
+                loggingLevel="error",
+            )
             self.nsm_client.announceGuiVisibility(self.visible)
         else:
-
             self.visible = True
-            self.create_mixer(client_name, with_nsm = False)
+            self.create_mixer(client_name, with_nsm=False)
 
-    def create_mixer(self, client_name, with_nsm = True):
+    def create_mixer(self, client_name, with_nsm=True):
         self.mixer = jack_mixer_c.Mixer(client_name)
-        self.create_ui(with_nsm)
         if not self.mixer:
-            sys.exit(1)
+            raise RuntimeError("Failed to create Mixer instance.")
 
+        self.create_ui(with_nsm)
         self.window.set_title(client_name)
 
         self.monitor_channel = self.mixer.add_output_channel("Monitor", True, True)
         self.save = False
 
         GLib.timeout_add(33, self.read_meters)
+        GLib.timeout_add(50, self.midi_events_check)
+
         if with_nsm:
             GLib.timeout_add(200, self.nsm_react)
-        GLib.timeout_add(50, self.midi_events_check)
 
     def new_menu_item(self, title, callback=None, accel=None, enabled=True):
         menuitem = Gtk.MenuItem.new_with_mnemonic(title)
@@ -159,7 +184,10 @@ class JackMixer(SerializedObject):
                                                       "<Shift><Control>S"))
 
         self.mixer_menu.append(Gtk.SeparatorMenuItem())
-        self.mixer_menu.append(self.new_menu_item('_Quit', self.on_quit_cb, "<Control>Q"))
+        if with_nsm:
+            self.mixer_menu.append(self.new_menu_item('_Hide', self.nsm_hide_cb, "<Control>W"))
+        else:
+            self.mixer_menu.append(self.new_menu_item('_Quit', self.on_quit_cb, "<Control>Q"))
 
         edit_menu = Gtk.Menu()
         edit_menu_item.set_submenu(edit_menu)
@@ -189,8 +217,8 @@ class JackMixer(SerializedObject):
         self.channel_remove_output_menu_item.set_submenu(self.channel_remove_output_menu)
 
         edit_menu.append(Gtk.SeparatorMenuItem())
-        edit_menu.append(self.new_menu_item('Narrow Input Channels', self.on_narrow_input_channels_cb, "<Control>A"))
-        edit_menu.append(self.new_menu_item('Widen Input Channels', self.on_widen_input_channels_cb, "<Control>W"))
+        edit_menu.append(self.new_menu_item('Shrink Channels', self.on_shrink_channels_cb, "<Control>minus"))
+        edit_menu.append(self.new_menu_item('Expand Channels', self.on_expand_channels_cb, "<Control>plus"))
         edit_menu.append(Gtk.SeparatorMenuItem())
 
         edit_menu.append(self.new_menu_item('_Clear', self.on_channels_clear, "<Control>X"))
@@ -225,16 +253,14 @@ class JackMixer(SerializedObject):
         self.hbox_top.pack_start(self.paned, True, True, 0)
         self.paned.pack1(self.scrolled_window, True, False)
         self.paned.pack2(self.scrolled_output, True, False)
-
         self.window.connect("destroy", Gtk.main_quit)
-
         self.window.connect('delete-event', self.on_delete_event)
 
     def nsm_react(self):
         self.nsm_client.reactToMessage()
         return True
 
-    def nsm_hide_cb(self):
+    def nsm_hide_cb(self, *args):
         self.window.hide()
         self.visible = False
         self.nsm_client.announceGuiVisibility(False)
@@ -248,15 +274,16 @@ class JackMixer(SerializedObject):
         self.nsm_client.announceGuiVisibility(True)
 
     def nsm_open_cb(self, path, session_name, client_name):
-        self.create_mixer(client_name, with_nsm = True)
+        self.create_mixer(client_name, with_nsm=True)
         self.current_filename = path + '.xml'
         if os.path.isfile(self.current_filename):
-            f = open(self.current_filename, 'r')
-            self.load_from_xml(f, from_nsm=True)
-            f.close()
-        else:
-            f = open(self.current_filename, 'w')
-            f.close()
+            try:
+                with open(self.current_filename, 'r') as fp:
+                    self.load_from_xml(fp, from_nsm=True)
+            except Exception as exc:
+                # Re-raise with more meaningful error message
+                raise IOError("Error loading settings file '{}': {}".format(
+                              self.current_filename, exc))
 
     def nsm_save_cb(self, path, session_name, client_name):
         self.current_filename = path + '.xml'
@@ -271,16 +298,20 @@ class JackMixer(SerializedObject):
         self.mixer.midi_behavior_mode = value
 
     def on_delete_event(self, widget, event):
-        return False
+        if self.nsm_client:
+            self.nsm_hide_cb()
+            return True
+
+        return self.on_quit_cb()
 
     def sighandler(self, signum, frame):
         log.debug("Signal %d received.", signum)
         if signum == signal.SIGUSR1:
             self.save = True
         elif signum == signal.SIGTERM:
-            Gtk.main_quit()
+            self.on_quit_cb()
         elif signum == signal.SIGINT:
-            Gtk.main_quit()
+            self.on_quit_cb()
         else:
             log.warning("Unknown signal %d received.", signum)
 
@@ -303,14 +334,12 @@ class JackMixer(SerializedObject):
         if dlg.run() == Gtk.ResponseType.OK:
             filename = dlg.get_filename()
             try:
-                f = open(filename, 'r')
-                self.load_from_xml(f)
-            except Exception as e:
-                error_dialog(self.window, "Failed loading settings (%s)", e)
+                with open(filename, 'r') as fp:
+                    self.load_from_xml(fp)
+            except Exception as exc:
+                error_dialog(self.window, "Error loading settings file '%s': %s", filename, exc)
             else:
                 self.current_filename = filename
-            finally:
-                f.close()
         dlg.destroy()
 
     def on_save_cb(self, *args):
@@ -332,14 +361,31 @@ class JackMixer(SerializedObject):
         dlg.destroy()
 
     def on_quit_cb(self, *args):
+        if not self.nsm_client and self.gui_factory.get_confirm_quit():
+            dlg = Gtk.MessageDialog(parent=self.window,
+                                    message_type=Gtk.MessageType.QUESTION,
+                                    buttons=Gtk.ButtonsType.NONE)
+            dlg.set_markup("<b>Quit application?</b>")
+            dlg.format_secondary_markup("All jack_mixer ports will be closed and connections lost,"
+                                        "\nstopping all sound going through jack_mixer.\n\n"
+                                        "Are you sure?")
+            dlg.add_buttons(
+                Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
+                Gtk.STOCK_QUIT, Gtk.ResponseType.OK
+            )
+            response = dlg.run()
+            dlg.destroy()
+            if response != Gtk.ResponseType.OK:
+                return True
+
         Gtk.main_quit()
 
-    def on_narrow_input_channels_cb(self, widget):
-        for channel in self.channels:
+    def on_shrink_channels_cb(self, widget):
+        for channel in self.channels + self.output_channels:
             channel.narrow()
 
-    def on_widen_input_channels_cb(self, widget):
-        for channel in self.channels:
+    def on_expand_channels_cb(self, widget):
+        for channel in self.channels + self.output_channels:
             channel.widen()
 
     preferences_dialog = None
@@ -349,8 +395,25 @@ class JackMixer(SerializedObject):
         self.preferences_dialog.show()
         self.preferences_dialog.present()
 
-    def on_add_input_channel(self, widget):
-        dialog = NewInputChannelDialog(app=self)
+    def on_add_channel(self, inout="input", default_name="Input"):
+        dialog = getattr(self, '_add_{}_dialog'.format(inout), None)
+        values = getattr(self, '_add_{}_values'.format(inout), {})
+
+        if dialog == None:
+            cls = NewInputChannelDialog if inout == 'input' else NewOutputChannelDialog
+            dialog = cls(app=self)
+            setattr(self, '_add_{}_dialog'.format(inout), dialog)
+
+        names = {ch.channel_name
+                 for ch in (self.channels if inout == 'input' else self.output_channels)}
+        values.setdefault('name', default_name)
+        while True:
+            if values['name'] in names:
+                values['name'] = add_number_suffix(values['name'])
+            else:
+                break
+
+        dialog.fill_ui(**values)
         dialog.set_transient_for(self.window)
         dialog.show()
         ret = dialog.run()
@@ -358,22 +421,17 @@ class JackMixer(SerializedObject):
 
         if ret == Gtk.ResponseType.OK:
             result = dialog.get_result()
-            channel = self.add_channel(**result)
+            setattr(self, '_add_{}_values'.format(inout), result)
+            method = getattr(self, 'add_channel' if inout == 'input' else 'add_output_channel')
+            channel = method(**result)
             if self.visible or self.nsm_client == None:
                 self.window.show_all()
 
-    def on_add_output_channel(self, widget):
-        dialog = NewOutputChannelDialog(app=self)
-        dialog.set_transient_for(self.window)
-        dialog.show()
-        ret = dialog.run()
-        dialog.hide()
+    def on_add_input_channel(self, widget):
+        return self.on_add_channel("input", "Input")
 
-        if ret == Gtk.ResponseType.OK:
-            result = dialog.get_result()
-            channel = self.add_output_channel(**result)
-            if self.visible or self.nsm_client == None:
-                self.window.show_all()
+    def on_add_output_channel(self, widget):
+        return self.on_add_channel("output", "Output")
 
     def on_edit_input_channel(self, widget, channel):
         log.debug('Editing input channel "%s".', channel.channel_name)
@@ -674,8 +732,9 @@ class JackMixer(SerializedObject):
     def on_about(self, *args):
         about = Gtk.AboutDialog()
         about.set_name('jack_mixer')
+        about.set_program_name('jack_mixer')
         about.set_copyright('Copyright © 2006-2020\nNedko Arnaudov, Frédéric Péters, Arnout Engelen, Daniel Sheeler')
-        about.set_license('''\
+        about.set_license("""\
 jack_mixer is free software; you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published by the
 Free Software Foundation; either version 2 of the License, or (at your
@@ -688,7 +747,7 @@ General Public License for more details.
 
 You should have received a copy of the GNU General Public License along
 with jack_mixer; if not, write to the Free Software Foundation, Inc., 51
-Franklin Street, Fifth Floor, Boston, MA 02110-130159 USA''')
+Franklin Street, Fifth Floor, Boston, MA 02110-130159 USA""")
         about.set_authors([
             'Nedko Arnaudov <nedko@arnaudov.name>',
             'Christopher Arndt <chris@chrisarndt.de>',
@@ -701,6 +760,7 @@ Franklin Street, Fifth Floor, Boston, MA 02110-130159 USA''')
             'Athanasios Silis <athanasios.silis@gmail.com>',
         ])
         about.set_logo_icon_name('jack_mixer')
+        about.set_version(__version__)
         about.set_website('https://rdio.space/jackmixer/')
 
         about.run()
@@ -739,6 +799,14 @@ Franklin Street, Fifth Floor, Boston, MA 02110-130159 USA''')
         width, height = self.window.get_size()
         if self.visible or not from_nsm:
             self.window.show_all()
+
+        if self.output_channels:
+            self.output_channels[-1].volume_digits.select_region(0,0)
+            self.output_channels[-1].slider.grab_focus()
+        elif self.channels:
+            self.channels[-1].volume_digits.select_region(0,0)
+            self.channels[-1].volume_digits.grab_focus()
+
         self.paned.set_position(self.paned_position/self.width*width)
         self.window.resize(self.width, self.height)
 
@@ -833,20 +901,19 @@ def main():
     try:
         mixer = JackMixer(args.client_name)
     except Exception as e:
-        error_dialog(None, "Mixer creation failed (%s).", e)
+        error_dialog(None, "Mixer creation failed:\n\n%s", e)
         sys.exit(1)
 
     if not mixer.nsm_client and args.config:
-        f = open(args.config)
-        mixer.current_filename = args.config
-
         try:
-            mixer.load_from_xml(f)
-        except Exception as e:
-            error_dialog(mixer.window, "Failed loading settings (%s).", e)
+            with open(args.config) as fp:
+                mixer.load_from_xml(fp)
+        except Exception as exc:
+            error_dialog(mixer.window, "Error loading settings file '%s': %s", args.config, exc)
+        else:
+            mixer.current_filename = args.config
 
         mixer.window.set_default_size(60*(1+len(mixer.channels)+len(mixer.output_channels)), 300)
-        f.close()
 
     mixer.main()