Browse Source

use KWin to find fullscreen window and pause adjustments if found

Thomas B 2 months ago
parent
commit
f91a68bc6d
4 changed files with 75 additions and 1 deletions
  1. 4
    0
      README.md
  2. 15
    1
      client/brightness.py
  3. 5
    0
      client/kwin_check.js
  4. 51
    0
      client/window.py

+ 4
- 0
README.md View File

@@ -31,6 +31,10 @@ Prepare udev rules for our new device.
31 31
 
32 32
 ## Client
33 33
 
34
+Uses [KWin scripting](https://develop.kde.org/docs/plasma/kwin/) to get the active window from the KDE window manager.
35
+This is used to pause adjustments while a fullscreen window is focused (eg. while gaming).
36
+Also sends status updates to a local InfluxDB.
37
+
34 38
 ### Quick Start
35 39
 
36 40
 Install dependency and run the client.

+ 15
- 1
client/brightness.py View File

@@ -4,6 +4,7 @@ import lux
4 4
 import ddc
5 5
 import time
6 6
 import influx
7
+import window
7 8
 
8 9
 filter_fact = 0.90
9 10
 
@@ -62,6 +63,9 @@ if __name__ == "__main__":
62 63
 
63 64
     time_brightness = time.time()
64 65
     time_displays = time.time()
66
+    time_window = time.time()
67
+
68
+    is_active = True
65 69
 
66 70
     while True:
67 71
         # read brightness at approx. 1Hz with low-pass filtering
@@ -85,8 +89,18 @@ if __name__ == "__main__":
85 89
             except:
86 90
                 pass
87 91
 
92
+        # check for fullscreen windows every 20s
93
+        if (time.time() - time_window) > 20.0:
94
+            time_window = time.time()
95
+            info = window.query()
96
+            if info["fullscreen"] and is_active:
97
+                print("App \"{}\" is now fullscreen! Pausing.".format(info["name"]))
98
+            if (not info["fullscreen"]) and (not is_active):
99
+                print("No longer fullscreen. Continuing.")
100
+            is_active = not info["fullscreen"]
101
+
88 102
         # set displays at most every 10s
89
-        if (time.time() - time_displays) > 10.0:
103
+        if is_active and ((time.time() - time_displays) > 10.0):
90 104
             time_displays = time.time()
91 105
 
92 106
             for d in disps:

+ 5
- 0
client/kwin_check.js View File

@@ -0,0 +1,5 @@
1
+var win = workspace.activeWindow;
2
+var name = win.caption;
3
+var pid = win.pid;
4
+var state = (win.bufferGeometry == win.output.geometry);
5
+print('{ "name": "' + name + '", "pid": ' + pid + ', "fullscreen": ' + state + ' }');

+ 51
- 0
client/window.py View File

@@ -0,0 +1,51 @@
1
+#!/usr/bin/env python
2
+
3
+import os
4
+import subprocess
5
+from datetime import datetime
6
+import json
7
+
8
+# https://unix.stackexchange.com/a/776620
9
+def query(verbose=False):
10
+    dir_path = os.path.abspath(os.path.dirname(__file__))
11
+    file_path = os.path.join(dir_path, "kwin_check.js")
12
+    datetime_now = datetime.now()
13
+
14
+    result = subprocess.run("dbus-send --print-reply --dest=org.kde.KWin /Scripting org.kde.kwin.Scripting.loadScript string:" + file_path, capture_output=True, shell=True)
15
+    if verbose and result.stdout:
16
+        print("Output 1", result.stdout.decode("utf-8"))
17
+    if verbose and result.stderr:
18
+        print("Errs 1", result.stderr.decode("utf-8"))
19
+
20
+    n = result.stdout.decode("utf-8").split("\n")[1].split()[1]
21
+    if verbose:
22
+        print("Script ID", n)
23
+
24
+    result = subprocess.run("dbus-send --print-reply --dest=org.kde.KWin /Scripting/Script" + n + " org.kde.kwin.Script.run", capture_output=True, shell=True)
25
+    if verbose and result.stdout:
26
+        print("Output 2", result.stdout.decode("utf-8"))
27
+    if verbose and result.stderr:
28
+        print("Errs 2", result.stderr.decode("utf-8"))
29
+
30
+    result = subprocess.run("dbus-send --print-reply --dest=org.kde.KWin /Scripting/Script" + n + " org.kde.kwin.Script.stop", capture_output=True, shell=True)
31
+    if verbose and result.stdout:
32
+        print("Output 3", result.stdout.decode("utf-8"))
33
+    if verbose and result.stderr:
34
+        print("Errs 3", result.stderr.decode("utf-8"))
35
+
36
+    since = str(datetime_now)
37
+
38
+    result = subprocess.run("journalctl _COMM=kwin_wayland -o cat --since \"" + since + "\"", capture_output=True, shell=True)
39
+    if verbose and result.stdout:
40
+        print("Output 4", result.stdout.decode("utf-8"))
41
+    if verbose and result.stderr:
42
+        print("Errs 4", result.stderr.decode("utf-8"))
43
+
44
+    msg = result.stdout.decode().rstrip().split("\n")[0][4:]
45
+    return json.loads(msg)
46
+
47
+if __name__ == "__main__":
48
+    info = query()
49
+    print("Name: \"{}\"".format(info["name"]))
50
+    print("PID: {}".format(info["pid"]))
51
+    print("Fullscreen: {}".format(info["fullscreen"]))

Loading…
Cancel
Save