Skip to content

Instantly share code, notes, and snippets.

@qiwichupa
Created January 17, 2024 20:51
Show Gist options
  • Save qiwichupa/f1816add49b707231763e5506181fb8a to your computer and use it in GitHub Desktop.
Save qiwichupa/f1816add49b707231763e5506181fb8a to your computer and use it in GitHub Desktop.
script to automatically disable the bluetooth gamepad when there is no activity for a specified time. Written for Steam and SteamDeck
#!/usr/bin/env python3
'''
script to automatically disable the bluetooth gamepad when there is no activity for a specified time.
Written for Steam and SteamDeck (requires "/dev/input/js0" device).
Change maxidletime and devid variables.
Мake the script executable and add it to autorun in desktop mode.
'''
import struct
from datetime import datetime as dt
import sys,os
import select
import time
from threading import *
maxidletime=600 # seconds
devid = '84:17:66:F1:09:B7' # gamepad ID
def input_checker (dev="/dev/input/js0"):
while True:
while True:
time.sleep(1)
if os.path.exists(dev):
EVENT_SIZE = struct.calcsize("llHHI")
file = open(dev, "rb")
break
while True:
r, w, e = select.select([ file ], [], [], 0)
if file in r:
try:
event = file.read(EVENT_SIZE)
print(struct.unpack("llHHI", event))
(tv_sec, tv_usec, type, code, value) = struct.unpack("llHHI", event)
movement.set()
print("movement detected")
except:
break
else:
pass
def timer (devid, maxidletime=600, dev="/dev/input/js0"):
currtime=dt.now()
prevtime=currtime
while True:
time.sleep(1)
currtime=dt.now()
if os.path.exists(dev):
if movement.is_set() is True:
print("date updated")
prevtime=currtime
movement.clear()
if (currtime-prevtime).total_seconds() >= maxidletime:
time.sleep(1)
os.system("echo disconnect "+devid+"| bluetoothctl")
time.sleep(1)
os.system("echo exit | bluetoothctl")
else:
prevtime=currtime
dev="/dev/input/js0"
movement=Event()
t1=Thread(target=input_checker, args = (dev, ))
t2=Thread(target=timer, args = (devid, maxidletime))
t1.start()
t2.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment