|
| 1 | +import blueman.bluez as bluez |
| 2 | +from blueman.Functions import * |
| 3 | +from blueman.main.SignalTracker import SignalTracker |
| 4 | +from blueman.main.Device import Device |
| 5 | +from blueman.plugins.AppletPlugin import AppletPlugin |
| 6 | +from gi.repository import Gdk, GdkX11 |
| 7 | +import subprocess |
| 8 | + |
| 9 | +class GameControllerWakelock(AppletPlugin): |
| 10 | + __description__ = _("Temporarily suspends the screensaver when a bluetooth game controller is connected.") |
| 11 | + __author__ = "bwRavencl" |
| 12 | + __icon__ = "input-gaming" |
| 13 | + |
| 14 | + def on_load(self, applet): |
| 15 | + self.wake_lock = 0 |
| 16 | + self.root_window_id = "0x%x" % Gdk.Screen.get_default().get_root_window().get_xid() |
| 17 | + |
| 18 | + self.signals = SignalTracker() |
| 19 | + self.signals.Handle("bluez", bluez.Device(), self.on_device_property_changed, "PropertyChanged", path_keyword="path") |
| 20 | + |
| 21 | + def on_unload(self): |
| 22 | + if self.wake_lock: |
| 23 | + self.wake_lock = 1 |
| 24 | + self.suspend_screensaver("resume") |
| 25 | + self.signals.DisconnectAll() |
| 26 | + |
| 27 | + def on_device_property_changed(self, key, value, path): |
| 28 | + if key == "Connected": |
| 29 | + klass = Device(path).get_properties()["Class"] & 0x1fff |
| 30 | + |
| 31 | + if klass == 0x504 or klass == 0x508: |
| 32 | + if value: |
| 33 | + self.xdg_screensaver("suspend") |
| 34 | + else: |
| 35 | + self.xdg_screensaver("resume") |
| 36 | + |
| 37 | + def xdg_screensaver(self, action): |
| 38 | + if action == "resume": |
| 39 | + if self.wake_lock == 0: |
| 40 | + pass |
| 41 | + elif self.wake_lock > 1: |
| 42 | + self.wake_lock -= 1 |
| 43 | + pass |
| 44 | + elif action == "suspend" and self.wake_lock >= 1: |
| 45 | + self.wake_lock += 1 |
| 46 | + pass |
| 47 | + |
| 48 | + command = ["xdg-screensaver", action, self.root_window_id] |
| 49 | + |
| 50 | + try: |
| 51 | + process = subprocess.Popen(command, stderr=subprocess.PIPE) |
| 52 | + _, stderr = process.communicate() |
| 53 | + |
| 54 | + if process.returncode != 0: |
| 55 | + dprint(" ".join(command) + " failed with: " + stderr) |
| 56 | + pass |
| 57 | + |
| 58 | + if action == "suspend": |
| 59 | + self.wake_lock += 1 |
| 60 | + elif action == "resume": |
| 61 | + self.wake_lock = 0 |
| 62 | + except: |
| 63 | + dprint(traceback.format_exc()) |
0 commit comments