Skip to content

Commit a821e0d

Browse files
committed
util: ensure session is x11 before getting res
1 parent 516aa5e commit a821e0d

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

util.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,21 @@ def set_xml_options(
822822
return True
823823

824824

825+
def is_x_session() -> bool:
826+
"""Returns True if on the X Window System
827+
828+
Both the X11 socket and the DISPLAY need to exist in the current session
829+
"""
830+
with Path('/proc/net/unix').open(mode='r', encoding='utf-8') as socks:
831+
for sock in socks:
832+
sock = sock.split()
833+
if not sock or not sock[-1].startswith('/tmp/.X11-unix'):
834+
continue
835+
return os.environ.get('DISPLAY') is not None
836+
837+
return False
838+
839+
825840
def get_resolution() -> Union[tuple[int, int], None]:
826841
"""Returns screen res width, height using xrandr"""
827842
# Execute xrandr command and capture its output
@@ -831,6 +846,10 @@ def get_resolution() -> Union[tuple[int, int], None]:
831846
log.info('xrandr not found in PATH, skipping screen resolution determination')
832847
return None
833848

849+
if not is_x_session():
850+
log.info('X server does not exist, skipping screen resolution determination')
851+
return None
852+
834853
xrandr_output = subprocess.check_output([xrandr_bin, '--current']).decode('utf-8')
835854

836855
# Find the line that starts with 'Screen 0:' and extract the resolution

0 commit comments

Comments
 (0)