-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto-big-picture.py.template
More file actions
86 lines (69 loc) · 2.59 KB
/
auto-big-picture.py.template
File metadata and controls
86 lines (69 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
import os
import subprocess
import time
import glob
CONTROLLER_MAC_ADDRESS = "__CONTROLLER_MAC_ADDRESS__"
LAUNCH_IF_CLOSED = __LAUNCH_PREFERENCE__
# Core
def is_steam_running():
return subprocess.run(["pgrep", "-x", "steam"], capture_output=True).returncode == 0
def is_game_running():
cmd = "pgrep -af 'steamapps/common' | grep -v 'd3ddriverquery64.exe'"
return subprocess.run(cmd, shell=True, capture_output=True).returncode == 0
def handle_connect():
steam_running = is_steam_running()
if LAUNCH_IF_CLOSED or steam_running:
env = os.environ.copy()
if 'WAYLAND_DISPLAY' in env:
env['GBM_BACKEND'] = 'nvidia-drm'
env['__GLX_VENDOR_LIBRARY_NAME'] = 'nvidia'
else:
env['DISPLAY'] = ':0'
cmd = ["steam", "steam://open/bigpicture"] if steam_running else ["steam", "-bigpicture"]
subprocess.Popen(cmd, env=env)
def handle_disconnect():
if not is_steam_running():
return
if is_game_running():
return
subprocess.Popen(["steam", "steam://close/bigpicture"])
# Monitoring
def is_bt_connected(mac):
if not mac or mac == "DISABLED":
return False
cmd = f"bluetoothctl info {mac} | grep 'Connected: yes'"
return subprocess.run(cmd, shell=True, capture_output=True).returncode == 0
# Main
if __name__ == "__main__":
joystick_path = "/dev/input/by-id/*-event-joystick"
try:
prev_joysticks = set(glob.glob(joystick_path))
except OSError:
prev_joysticks = set()
prev_bt_connected = False
if CONTROLLER_MAC_ADDRESS and CONTROLLER_MAC_ADDRESS != "DISABLED":
prev_bt_connected = is_bt_connected(CONTROLLER_MAC_ADDRESS)
try:
while True:
try:
curr_joysticks = set(glob.glob(joystick_path))
except OSError:
curr_joysticks = set()
added = curr_joysticks - prev_joysticks
removed = prev_joysticks - curr_joysticks
if added:
handle_connect()
if removed:
handle_disconnect()
prev_joysticks = curr_joysticks
if CONTROLLER_MAC_ADDRESS and CONTROLLER_MAC_ADDRESS != "DISABLED":
curr_bt_connected = is_bt_connected(CONTROLLER_MAC_ADDRESS)
if curr_bt_connected and not prev_bt_connected:
handle_connect()
if not curr_bt_connected and prev_bt_connected:
handle_disconnect()
prev_bt_connected = curr_bt_connected
time.sleep(1)
except KeyboardInterrupt:
pass