Skip to content

Commit 178a6be

Browse files
committed
A custom DISPLAY to determinate the socket_path.
Add `display` argument to sync and async `Connetion()` classes to determineate the socket_path. Useful when you work with many Xephyr servers.
1 parent a670f24 commit 178a6be

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

i3ipc/aio/connection.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def _unpack_header(data: bytes) -> Tuple[bytes, int, int]:
179179
return struct.unpack(_struct_header, data[:_struct_header_size])
180180

181181

182-
async def _find_socket_path() -> Optional[str]:
182+
async def _find_socket_path(disp: Optional[str] = None) -> Optional[str]:
183183
socket_path = None
184184

185185
def exists(path):
@@ -205,7 +205,7 @@ def exists(path):
205205

206206
# next try the root window property
207207
try:
208-
d = display.Display()
208+
d = display.Display(disp)
209209
atom = d.get_atom('I3_SOCKET_PATH')
210210
root = d.screen().root
211211
prop = root.get_full_property(atom, X.AnyPropertyType)
@@ -269,17 +269,21 @@ class Connection:
269269
:param auto_reconnect: Whether to attempt to reconnect if the connection to
270270
the socket is broken when i3 restarts.
271271
:type auto_reconnect: bool
272+
:param display: A custom DISPLAY to determinate the socket_path.
273+
:type display: str
272274
273275
:raises Exception: If the connection to i3 cannot be established.
274276
"""
275-
def __init__(self, socket_path: Optional[str] = None, auto_reconnect: bool = False):
277+
def __init__(self, socket_path: Optional[str] = None, auto_reconnect: bool = False, *,
278+
display: Optional[str] = None):
276279
self._socket_path = socket_path
277280
self._auto_reconnect = auto_reconnect
278281
self._pubsub = _AIOPubSub(self)
279282
self._subscriptions = set()
280283
self._main_future = None
281284
self._reconnect_future = None
282285
self._synchronizer = None
286+
self._display = display
283287

284288
def _sync(self):
285289
if self._synchronizer is None:
@@ -390,7 +394,7 @@ async def connect(self) -> 'Connection':
390394
logger.info('using user provided socket path: {}', self._socket_path)
391395

392396
if not self._socket_path:
393-
self._socket_path = await _find_socket_path()
397+
self._socket_path = await _find_socket_path(self._display)
394398

395399
if not self.socket_path:
396400
raise Exception('Failed to retrieve the i3 or sway IPC socket path')

i3ipc/connection.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class Connection:
4444
:param auto_reconnect: Whether to attempt to reconnect if the connection to
4545
the socket is broken when i3 restarts.
4646
:type auto_reconnect: bool
47+
:param display: A custom DISPLAY to determinate the socket_path.
48+
:type display: str
4749
4850
:raises Exception: If the connection to i3 cannot be established.
4951
"""
@@ -53,7 +55,7 @@ class Connection:
5355
_struct_header = '=%dsII' % len(_MAGIC.encode('utf-8'))
5456
_struct_header_size = struct.calcsize(_struct_header)
5557

56-
def __init__(self, socket_path=None, auto_reconnect=False):
58+
def __init__(self, socket_path=None, auto_reconnect=False, display=None):
5759

5860
if socket_path:
5961
logger.info('using user provided socket path: %s', socket_path)
@@ -74,6 +76,7 @@ def __init__(self, socket_path=None, auto_reconnect=False):
7476
self._auto_reconnect = auto_reconnect
7577
self._quitting = False
7678
self._synchronizer = None
79+
self._display = display
7780

7881
def _find_socket_path(self):
7982
socket_path = os.environ.get("I3SOCK")
@@ -87,7 +90,7 @@ def _find_socket_path(self):
8790
return socket_path
8891

8992
try:
90-
disp = Xlib.display.Display()
93+
disp = Xlib.display.Display(self._display)
9194
root = disp.screen().root
9295
i3atom = disp.intern_atom("I3_SOCKET_PATH")
9396
prop = root.get_full_property(i3atom, Xlib.X.AnyPropertyType)

0 commit comments

Comments
 (0)