@@ -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