Skip to content

Commit 20e3a0e

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 9de8e88 commit 20e3a0e

2 files changed

Lines changed: 24 additions & 8 deletions

File tree

i3ipc/aio/connection.py

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

179179

180-
async def _find_socket_path() -> Optional[str]:
180+
async def _find_socket_path(disp: Optional[str] = None) -> Optional[str]:
181181
socket_path = None
182182

183183
def exists(path):
@@ -202,12 +202,17 @@ def exists(path):
202202
return socket_path
203203

204204
# finally try the binaries
205+
if disp:
206+
env = {**os.environ, 'DISPLAY': disp}
207+
else:
208+
env = None
205209
for binary in ('i3', 'sway'):
206210
try:
207211
process = await asyncio.create_subprocess_exec(binary,
208212
'--get-socketpath',
209213
stdout=PIPE,
210-
stderr=PIPE)
214+
stderr=PIPE,
215+
env=env)
211216

212217
stdout, stderr = await process.communicate()
213218

@@ -251,17 +256,21 @@ class Connection:
251256
:param auto_reconnect: Whether to attempt to reconnect if the connection to
252257
the socket is broken when i3 restarts.
253258
:type auto_reconnect: bool
259+
:param display: A custom DISPLAY to determinate the socket_path.
260+
:type display: str
254261
255262
:raises Exception: If the connection to i3 cannot be established.
256263
"""
257-
def __init__(self, socket_path: Optional[str] = None, auto_reconnect: bool = False):
264+
def __init__(self, socket_path: Optional[str] = None, auto_reconnect: bool = False, *,
265+
display: Optional[str] = None):
258266
self._socket_path = socket_path
259267
self._auto_reconnect = auto_reconnect
260268
self._pubsub = _AIOPubSub(self)
261269
self._subscriptions = set()
262270
self._main_future = None
263271
self._reconnect_future = None
264272
self._synchronizer = None
273+
self._display = display
265274

266275
def _sync(self):
267276
if self._synchronizer is None:
@@ -372,7 +381,7 @@ async def connect(self) -> 'Connection':
372381
logger.info('using user provided socket path: {}', self._socket_path)
373382

374383
if not self._socket_path:
375-
self._socket_path = await _find_socket_path()
384+
self._socket_path = await _find_socket_path(self._display)
376385

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

i3ipc/connection.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class Connection:
4141
:param auto_reconnect: Whether to attempt to reconnect if the connection to
4242
the socket is broken when i3 restarts.
4343
:type auto_reconnect: bool
44+
:param display: A custom DISPLAY to determinate the socket_path.
45+
:type display: str
4446
4547
:raises Exception: If the connection to i3 cannot be established.
4648
"""
@@ -50,12 +52,12 @@ class Connection:
5052
_struct_header = '=%dsII' % len(_MAGIC.encode('utf-8'))
5153
_struct_header_size = struct.calcsize(_struct_header)
5254

53-
def __init__(self, socket_path=None, auto_reconnect=False):
55+
def __init__(self, socket_path=None, auto_reconnect=False, display=None):
5456

5557
if socket_path:
5658
logger.info('using user provided socket path: %s', socket_path)
5759
else:
58-
socket_path = self._find_socket_path()
60+
socket_path = self._find_socket_path(display)
5961

6062
if not socket_path:
6163
raise Exception('Failed to retrieve the i3 or sway IPC socket path')
@@ -71,8 +73,9 @@ def __init__(self, socket_path=None, auto_reconnect=False):
7173
self._auto_reconnect = auto_reconnect
7274
self._quitting = False
7375
self._synchronizer = None
76+
self._display = display
7477

75-
def _find_socket_path(self):
78+
def _find_socket_path(self, disp=None):
7679
socket_path = os.environ.get("I3SOCK")
7780
if socket_path:
7881
logger.info('got socket path from I3SOCK env variable: %s', socket_path)
@@ -83,9 +86,13 @@ def _find_socket_path(self):
8386
logger.info('got socket path from SWAYSOCK env variable: %s', socket_path)
8487
return socket_path
8588

89+
if disp:
90+
env = {**os.environ, 'DISPLAY': disp}
91+
else:
92+
env = None
8693
for binary in ('i3', 'sway'):
8794
try:
88-
process = run([binary, '--get-socketpath'], stdout=PIPE, stderr=PIPE)
95+
process = run([binary, '--get-socketpath'], stdout=PIPE, stderr=PIPE, env=env)
8996
if process.returncode == 0 and process.stdout:
9097
socket_path = process.stdout.decode().strip()
9198
logger.info('got socket path from `%s` binary: %s', binary, socket_path)

0 commit comments

Comments
 (0)