syphon.server_directory
1import threading 2from dataclasses import dataclass 3from enum import Enum 4from typing import Any, Callable, List, Optional 5 6import objc 7from Cocoa import NSDate, NSDefaultRunLoopMode, NSImage, NSRunLoop 8 9 10class SyphonServerNotification(Enum): 11 """ 12 Enum representing Syphon server notifications. 13 14 Enum Values: 15 - Announce: A new SyphonServer is available on the system. 16 - Update: An existing SyphonServer instance has changed its description. 17 - Retire: A SyphonServer instance will no longer be available. 18 """ 19 20 Announce = "SyphonServerAnnounceNotification" 21 Update = "SyphonServerUpdateNotification" 22 Retire = "SyphonServerRetireNotification" 23 24 25@dataclass 26class SyphonServerDescription: 27 """ 28 Data class representing the description of a Syphon server. 29 30 Attributes: 31 - uuid (str): The UUID of the Syphon server. 32 - name (str): The name of the Syphon server. 33 - app_name (str): The name of the application associated with the Syphon server. 34 - icon (NSImage): The icon image of the Syphon server. 35 - raw (Any): The raw server information. 36 """ 37 38 uuid: str 39 name: str 40 app_name: str 41 icon: Optional[NSImage] 42 raw: Any 43 44 @classmethod 45 def from_native(cls, raw): 46 """Wrap a complete native description without requiring optional display metadata.""" 47 return cls( 48 uuid=str(raw.get("SyphonServerDescriptionUUIDKey") or ""), 49 name=str(raw.get("SyphonServerDescriptionNameKey") or ""), 50 app_name=str(raw.get("SyphonServerDescriptionAppNameKey") or ""), 51 icon=raw.get("SyphonServerDescriptionIconKey"), 52 raw=raw, 53 ) 54 55 56class SyphonServerDirectory: 57 """ 58 Class for interacting with the Syphon server directory. 59 60 Attributes: 61 - run_loop_interval (float): The interval for the run loop in seconds. 62 """ 63 64 def __init__(self): 65 """ 66 Initialize a SyphonServerDirectory. 67 """ 68 self._syphonServerDirectoryObjC = objc.lookUpClass("SyphonServerDirectory") 69 self._notification_center = objc.lookUpClass("NSNotificationCenter").defaultCenter() 70 71 self.run_loop_interval: float = 1.0 72 self._observer_lock = threading.RLock() 73 self._observers = [] 74 self._closed = False 75 76 def add_observer(self, notification: SyphonServerNotification, handler: Callable[[Any], None]): 77 """ 78 Add an observer for a Syphon server notification. 79 80 Parameters: 81 - notification (SyphonServerNotification): The notification to observe. 82 - handler (Callable[[Any], None]): The handler function to be called when the notification is received. 83 84 Returns the native observer token. Pass it to remove_observer(), or close this 85 directory to remove all tokens it owns. Callback threading follows NSNotificationCenter. 86 """ 87 with self._observer_lock: 88 if self._closed: 89 raise RuntimeError("Syphon server directory is closed") 90 token = self._notification_center.addObserverForName_object_queue_usingBlock_( 91 notification.value, None, None, handler 92 ) 93 self._observers.append(token) 94 return token 95 96 def remove_observer(self, token): 97 """Remove a token returned by this directory; unknown/removed tokens are ignored.""" 98 with self._observer_lock: 99 for index, owned in enumerate(self._observers): 100 if owned is token: 101 self._observers.pop(index) 102 break 103 else: 104 return 105 self._notification_center.removeObserver_(token) 106 107 def close(self): 108 """Remove this wrapper's observers once, without stopping the shared native directory.""" 109 with self._observer_lock: 110 self._closed = True 111 tokens, self._observers = self._observers, [] 112 for token in tokens: 113 self._notification_center.removeObserver_(token) 114 115 def __enter__(self): 116 with self._observer_lock: 117 if self._closed: 118 raise RuntimeError("Syphon server directory is closed") 119 return self 120 121 def __exit__(self, exc_type, exc_value, traceback): 122 self.close() 123 124 @property 125 def servers(self) -> List[SyphonServerDescription]: 126 """ 127 Get a list of Syphon servers in the directory. 128 129 Returns: 130 - List[SyphonServerDescription]: A list of SyphonServerDescription objects. 131 """ 132 self.update_run_loop() 133 directory = self._syphonServerDirectoryObjC.sharedDirectory() 134 servers = directory.servers() 135 136 return [SyphonServerDescription.from_native(raw) for raw in servers] 137 138 def update_run_loop(self): 139 """ 140 Update the run loop to process events. 141 """ 142 NSRunLoop.currentRunLoop().runMode_beforeDate_( 143 NSDefaultRunLoopMode, NSDate.dateWithTimeIntervalSinceNow_(self.run_loop_interval) 144 ) 145 146 def servers_matching_name( 147 self, name: Optional[str] = None, app_name: Optional[str] = None 148 ) -> List[SyphonServerDescription]: 149 """ 150 Get a list of Syphon servers that match the specified name or application name. 151 152 Parameters: 153 - name (Optional[str]): The name to match. 154 - app_name (Optional[str]): The application name to match. 155 156 Returns: 157 - List[SyphonServerDescription]: A list of SyphonServerDescription objects that match the criteria. 158 """ 159 filtered_servers = [] 160 161 for server in self.servers: 162 if name is not None and name == server.name: 163 filtered_servers.append(server) 164 continue 165 166 if app_name is not None and app_name == server.app_name: 167 filtered_servers.append(server) 168 continue 169 170 return filtered_servers
11class SyphonServerNotification(Enum): 12 """ 13 Enum representing Syphon server notifications. 14 15 Enum Values: 16 - Announce: A new SyphonServer is available on the system. 17 - Update: An existing SyphonServer instance has changed its description. 18 - Retire: A SyphonServer instance will no longer be available. 19 """ 20 21 Announce = "SyphonServerAnnounceNotification" 22 Update = "SyphonServerUpdateNotification" 23 Retire = "SyphonServerRetireNotification"
Enum representing Syphon server notifications.
Enum Values:
- Announce: A new SyphonServer is available on the system.
- Update: An existing SyphonServer instance has changed its description.
- Retire: A SyphonServer instance will no longer be available.
26@dataclass 27class SyphonServerDescription: 28 """ 29 Data class representing the description of a Syphon server. 30 31 Attributes: 32 - uuid (str): The UUID of the Syphon server. 33 - name (str): The name of the Syphon server. 34 - app_name (str): The name of the application associated with the Syphon server. 35 - icon (NSImage): The icon image of the Syphon server. 36 - raw (Any): The raw server information. 37 """ 38 39 uuid: str 40 name: str 41 app_name: str 42 icon: Optional[NSImage] 43 raw: Any 44 45 @classmethod 46 def from_native(cls, raw): 47 """Wrap a complete native description without requiring optional display metadata.""" 48 return cls( 49 uuid=str(raw.get("SyphonServerDescriptionUUIDKey") or ""), 50 name=str(raw.get("SyphonServerDescriptionNameKey") or ""), 51 app_name=str(raw.get("SyphonServerDescriptionAppNameKey") or ""), 52 icon=raw.get("SyphonServerDescriptionIconKey"), 53 raw=raw, 54 )
Data class representing the description of a Syphon server.
Attributes:
- uuid (str): The UUID of the Syphon server.
- name (str): The name of the Syphon server.
- app_name (str): The name of the application associated with the Syphon server.
- icon (NSImage): The icon image of the Syphon server.
- raw (Any): The raw server information.
45 @classmethod 46 def from_native(cls, raw): 47 """Wrap a complete native description without requiring optional display metadata.""" 48 return cls( 49 uuid=str(raw.get("SyphonServerDescriptionUUIDKey") or ""), 50 name=str(raw.get("SyphonServerDescriptionNameKey") or ""), 51 app_name=str(raw.get("SyphonServerDescriptionAppNameKey") or ""), 52 icon=raw.get("SyphonServerDescriptionIconKey"), 53 raw=raw, 54 )
Wrap a complete native description without requiring optional display metadata.
57class SyphonServerDirectory: 58 """ 59 Class for interacting with the Syphon server directory. 60 61 Attributes: 62 - run_loop_interval (float): The interval for the run loop in seconds. 63 """ 64 65 def __init__(self): 66 """ 67 Initialize a SyphonServerDirectory. 68 """ 69 self._syphonServerDirectoryObjC = objc.lookUpClass("SyphonServerDirectory") 70 self._notification_center = objc.lookUpClass("NSNotificationCenter").defaultCenter() 71 72 self.run_loop_interval: float = 1.0 73 self._observer_lock = threading.RLock() 74 self._observers = [] 75 self._closed = False 76 77 def add_observer(self, notification: SyphonServerNotification, handler: Callable[[Any], None]): 78 """ 79 Add an observer for a Syphon server notification. 80 81 Parameters: 82 - notification (SyphonServerNotification): The notification to observe. 83 - handler (Callable[[Any], None]): The handler function to be called when the notification is received. 84 85 Returns the native observer token. Pass it to remove_observer(), or close this 86 directory to remove all tokens it owns. Callback threading follows NSNotificationCenter. 87 """ 88 with self._observer_lock: 89 if self._closed: 90 raise RuntimeError("Syphon server directory is closed") 91 token = self._notification_center.addObserverForName_object_queue_usingBlock_( 92 notification.value, None, None, handler 93 ) 94 self._observers.append(token) 95 return token 96 97 def remove_observer(self, token): 98 """Remove a token returned by this directory; unknown/removed tokens are ignored.""" 99 with self._observer_lock: 100 for index, owned in enumerate(self._observers): 101 if owned is token: 102 self._observers.pop(index) 103 break 104 else: 105 return 106 self._notification_center.removeObserver_(token) 107 108 def close(self): 109 """Remove this wrapper's observers once, without stopping the shared native directory.""" 110 with self._observer_lock: 111 self._closed = True 112 tokens, self._observers = self._observers, [] 113 for token in tokens: 114 self._notification_center.removeObserver_(token) 115 116 def __enter__(self): 117 with self._observer_lock: 118 if self._closed: 119 raise RuntimeError("Syphon server directory is closed") 120 return self 121 122 def __exit__(self, exc_type, exc_value, traceback): 123 self.close() 124 125 @property 126 def servers(self) -> List[SyphonServerDescription]: 127 """ 128 Get a list of Syphon servers in the directory. 129 130 Returns: 131 - List[SyphonServerDescription]: A list of SyphonServerDescription objects. 132 """ 133 self.update_run_loop() 134 directory = self._syphonServerDirectoryObjC.sharedDirectory() 135 servers = directory.servers() 136 137 return [SyphonServerDescription.from_native(raw) for raw in servers] 138 139 def update_run_loop(self): 140 """ 141 Update the run loop to process events. 142 """ 143 NSRunLoop.currentRunLoop().runMode_beforeDate_( 144 NSDefaultRunLoopMode, NSDate.dateWithTimeIntervalSinceNow_(self.run_loop_interval) 145 ) 146 147 def servers_matching_name( 148 self, name: Optional[str] = None, app_name: Optional[str] = None 149 ) -> List[SyphonServerDescription]: 150 """ 151 Get a list of Syphon servers that match the specified name or application name. 152 153 Parameters: 154 - name (Optional[str]): The name to match. 155 - app_name (Optional[str]): The application name to match. 156 157 Returns: 158 - List[SyphonServerDescription]: A list of SyphonServerDescription objects that match the criteria. 159 """ 160 filtered_servers = [] 161 162 for server in self.servers: 163 if name is not None and name == server.name: 164 filtered_servers.append(server) 165 continue 166 167 if app_name is not None and app_name == server.app_name: 168 filtered_servers.append(server) 169 continue 170 171 return filtered_servers
Class for interacting with the Syphon server directory.
Attributes:
- run_loop_interval (float): The interval for the run loop in seconds.
65 def __init__(self): 66 """ 67 Initialize a SyphonServerDirectory. 68 """ 69 self._syphonServerDirectoryObjC = objc.lookUpClass("SyphonServerDirectory") 70 self._notification_center = objc.lookUpClass("NSNotificationCenter").defaultCenter() 71 72 self.run_loop_interval: float = 1.0 73 self._observer_lock = threading.RLock() 74 self._observers = [] 75 self._closed = False
Initialize a SyphonServerDirectory.
77 def add_observer(self, notification: SyphonServerNotification, handler: Callable[[Any], None]): 78 """ 79 Add an observer for a Syphon server notification. 80 81 Parameters: 82 - notification (SyphonServerNotification): The notification to observe. 83 - handler (Callable[[Any], None]): The handler function to be called when the notification is received. 84 85 Returns the native observer token. Pass it to remove_observer(), or close this 86 directory to remove all tokens it owns. Callback threading follows NSNotificationCenter. 87 """ 88 with self._observer_lock: 89 if self._closed: 90 raise RuntimeError("Syphon server directory is closed") 91 token = self._notification_center.addObserverForName_object_queue_usingBlock_( 92 notification.value, None, None, handler 93 ) 94 self._observers.append(token) 95 return token
Add an observer for a Syphon server notification.
Parameters:
- notification (SyphonServerNotification): The notification to observe.
- handler (Callable[[Any], None]): The handler function to be called when the notification is received.
Returns the native observer token. Pass it to remove_observer(), or close this directory to remove all tokens it owns. Callback threading follows NSNotificationCenter.
97 def remove_observer(self, token): 98 """Remove a token returned by this directory; unknown/removed tokens are ignored.""" 99 with self._observer_lock: 100 for index, owned in enumerate(self._observers): 101 if owned is token: 102 self._observers.pop(index) 103 break 104 else: 105 return 106 self._notification_center.removeObserver_(token)
Remove a token returned by this directory; unknown/removed tokens are ignored.
108 def close(self): 109 """Remove this wrapper's observers once, without stopping the shared native directory.""" 110 with self._observer_lock: 111 self._closed = True 112 tokens, self._observers = self._observers, [] 113 for token in tokens: 114 self._notification_center.removeObserver_(token)
Remove this wrapper's observers once, without stopping the shared native directory.
125 @property 126 def servers(self) -> List[SyphonServerDescription]: 127 """ 128 Get a list of Syphon servers in the directory. 129 130 Returns: 131 - List[SyphonServerDescription]: A list of SyphonServerDescription objects. 132 """ 133 self.update_run_loop() 134 directory = self._syphonServerDirectoryObjC.sharedDirectory() 135 servers = directory.servers() 136 137 return [SyphonServerDescription.from_native(raw) for raw in servers]
Get a list of Syphon servers in the directory.
Returns:
- List[SyphonServerDescription]: A list of SyphonServerDescription objects.
139 def update_run_loop(self): 140 """ 141 Update the run loop to process events. 142 """ 143 NSRunLoop.currentRunLoop().runMode_beforeDate_( 144 NSDefaultRunLoopMode, NSDate.dateWithTimeIntervalSinceNow_(self.run_loop_interval) 145 )
Update the run loop to process events.
147 def servers_matching_name( 148 self, name: Optional[str] = None, app_name: Optional[str] = None 149 ) -> List[SyphonServerDescription]: 150 """ 151 Get a list of Syphon servers that match the specified name or application name. 152 153 Parameters: 154 - name (Optional[str]): The name to match. 155 - app_name (Optional[str]): The application name to match. 156 157 Returns: 158 - List[SyphonServerDescription]: A list of SyphonServerDescription objects that match the criteria. 159 """ 160 filtered_servers = [] 161 162 for server in self.servers: 163 if name is not None and name == server.name: 164 filtered_servers.append(server) 165 continue 166 167 if app_name is not None and app_name == server.app_name: 168 filtered_servers.append(server) 169 continue 170 171 return filtered_servers
Get a list of Syphon servers that match the specified name or application name.
Parameters:
- name (Optional[str]): The name to match.
- app_name (Optional[str]): The application name to match.
Returns:
- List[SyphonServerDescription]: A list of SyphonServerDescription objects that match the criteria.