Browse Source

introduce event types

Fabian Peter Hammerle 9 years ago
parent
commit
5aa8ca956d
1 changed files with 29 additions and 23 deletions
  1. 29 23
      scripts/jack-plumber

+ 29 - 23
scripts/jack-plumber

@@ -5,9 +5,14 @@ import re
 import jack
 import jack
 from gi.repository import GLib
 from gi.repository import GLib
 
 
+class PortEventType:
+    preexisting = 'preexisting'
+    registered = 'registered'
+    renamed = 'renamed'
+
 class Instruction(object):
 class Instruction(object):
 
 
-    def execute(self, client, port):
+    def execute(self, client, port, event):
         pass
         pass
 
 
     def __repr__(self):
     def __repr__(self):
@@ -18,8 +23,9 @@ class PortRenameInstruction(Instruction):
     def __init__(self, new_port_name):
     def __init__(self, new_port_name):
         self.new_port_name = new_port_name
         self.new_port_name = new_port_name
 
 
-    def execute(self, client, port):
-        port.set_short_name(self.new_port_name)
+    def execute(self, client, port, event):
+        if event in [PortEventType.registered, PortEventType.preexisting]:
+            port.set_short_name(self.new_port_name)
 
 
 class PortConnectInstruction(Instruction):
 class PortConnectInstruction(Instruction):
 
 
@@ -27,35 +33,35 @@ class PortConnectInstruction(Instruction):
         self.other_client_pattern = other_client_pattern
         self.other_client_pattern = other_client_pattern
         self.other_port_pattern = other_port_pattern
         self.other_port_pattern = other_port_pattern
 
 
-    def execute(self, client, port):
-        for other_port in [
-                p for p in client.get_ports()
-                    if re.match(self.other_client_pattern, p.get_client_name())
-                    and re.match(self.other_port_pattern, p.get_short_name())
-                ]:
-            try:
-                if port.is_output():
-                    client.connect(port, other_port)
-                else:
-                    client.connect(other_port, port)
-            except jack.ConnectionExists:
-                pass
-
-def check_port(client, port, instructions, rename = True):
+    def execute(self, client, port, event):
+        if event in [PortEventType.registered, PortEventType.renamed, PortEventType.preexisting]:
+            for other_port in [
+                    p for p in client.get_ports()
+                        if re.match(self.other_client_pattern, p.get_client_name())
+                        and re.match(self.other_port_pattern, p.get_short_name())
+                    ]:
+                try:
+                    if port.is_output():
+                        client.connect(port, other_port)
+                    else:
+                        client.connect(other_port, port)
+                except jack.ConnectionExists:
+                    pass
+
+def check_port(client, port, event, instructions, rename = True):
     for client_pattern in instructions:
     for client_pattern in instructions:
         if re.match(client_pattern, port.get_client_name()):
         if re.match(client_pattern, port.get_client_name()):
             for port_pattern in instructions[client_pattern]:
             for port_pattern in instructions[client_pattern]:
                 if re.match(port_pattern, port.get_short_name()):
                 if re.match(port_pattern, port.get_short_name()):
                     for instruction in instructions[client_pattern][port_pattern]:
                     for instruction in instructions[client_pattern][port_pattern]:
-                        if rename or not isinstance(instruction, PortRenameInstruction):
-                            GLib.idle_add(lambda: instruction.execute(client, port))
+                        GLib.idle_add(lambda: instruction.execute(client, port, event))
 
 
 def port_registered(client, port, instructions):
 def port_registered(client, port, instructions):
-    check_port(client, port, instructions)
+    check_port(client, port, PortEventType.registered, instructions)
 
 
 def port_renamed(client, port, old_name, new_name, instructions):
 def port_renamed(client, port, old_name, new_name, instructions):
     """ Avoid recursion by skipping rename instructions. """
     """ Avoid recursion by skipping rename instructions. """
-    check_port(client, port, instructions, rename = False)
+    check_port(client, port, PortEventType.renamed, instructions, rename = False)
 
 
 def server_shutdown(client, reason, callback):
 def server_shutdown(client, reason, callback):
     print(reason)
     print(reason)
@@ -72,7 +78,7 @@ def create_client(instructions, server_shutdown_callback = None):
     jack_client.activate()
     jack_client.activate()
 
 
     for port in jack_client.get_ports():
     for port in jack_client.get_ports():
-        check_port(jack_client, port, instructions)
+        check_port(jack_client, port, PortEventType.preexisting, instructions)
 
 
     return jack_client
     return jack_client