Skip to content

Commit e9c4f3d

Browse files
committed
add gnome-shell compatibility
1 parent fbec7ed commit e9c4f3d

File tree

2 files changed

+65
-11
lines changed

2 files changed

+65
-11
lines changed

aw_watcher_window/lib.py

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,64 @@
11
import sys
2+
import json
23
from typing import Optional
34

45

5-
def get_current_window_linux() -> Optional[dict]:
6-
from . import xlib
7-
window = xlib.get_current_window()
6+
class Linux:
7+
def __init__(self):
8+
try:
9+
import pydbus
10+
self.bus = pydbus.SessionBus()
11+
except ModuleNotFoundError:
12+
self.bus = False
13+
14+
if self.bus:
15+
import gi.repository.GLib
16+
try:
17+
self.gnome_shell = self.bus.get("org.gnome.Shell")
18+
except gi.repository.GLib.Error:
19+
self.gnome_shell = None
20+
21+
def get_current_window(self) -> dict:
22+
if self.gnome_shell:
23+
return self.get_current_window_gnome_shell()
24+
25+
return self.get_current_window_x11()
26+
27+
def get_current_window_gnome_shell(self) -> dict:
28+
"""get current app from GNOME Shell via dbus"""
29+
js_code = """
30+
var window_list = global.get_window_actors();
31+
var active_window_actor = window_list.find(window => window.meta_window.has_focus());
32+
var active_window = active_window_actor.get_meta_window()
33+
var vm_class = active_window.get_wm_class();
34+
var title = active_window.get_title()
35+
var result = {"title": title, "appname": vm_class};
36+
result
37+
"""
38+
39+
ok, result = self.gnome_shell.Eval(js_code)
40+
if ok:
41+
result_data = json.loads(result)
42+
return result_data
43+
44+
return {"appname": "unknown", "title": "unknown"}
45+
46+
def get_current_window_x11() -> dict:
47+
from . import xlib
48+
window = xlib.get_current_window()
49+
50+
if window is None:
51+
cls = "unknown"
52+
name = "unknown"
53+
else:
54+
cls = xlib.get_window_class(window)
55+
name = xlib.get_window_name(window)
56+
57+
return {"appname": cls, "title": name}
858

9-
if window is None:
10-
cls = "unknown"
11-
name = "unknown"
12-
else:
13-
cls = xlib.get_window_class(window)
14-
name = xlib.get_window_name(window)
1559

16-
return {"appname": cls, "title": name}
60+
if sys.platform.startswith("linux"):
61+
linux = Linux()
1762

1863

1964
def get_current_window_macos() -> Optional[dict]:
@@ -41,7 +86,7 @@ def get_current_window_windows() -> Optional[dict]:
4186

4287
def get_current_window() -> Optional[dict]:
4388
if sys.platform.startswith("linux"):
44-
return get_current_window_linux()
89+
return linux.get_current_window()
4590
elif sys.platform == "darwin":
4691
return get_current_window_macos()
4792
elif sys.platform in ["win32", "cygwin"]:

pyproject.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,20 @@ python-xlib = {version = "^0.26", platform = "linux"}
1515
pypiwin32 = {version = "223", platform = "win32"}
1616
wmi = {version = "^1.4.9", platform = "win32"}
1717

18+
# pydbus depends on PyGObject, which needs several system libs installed,
19+
# to be able to be compiled:
20+
# * gobject-introspection-devel
21+
# * cairo-gobject-devel
22+
pydbus = { version = "^0.6.0", platform = "linux", optional = true }
23+
1824
[tool.poetry.dev-dependencies]
1925
pytest = "^5.3.2"
2026
mypy = "^0.761"
2127
macholib = {version = "^1.13", platform = "darwin"} # Needed for pyinstaller
2228

29+
[tool.poetry.extras]
30+
gnome = ["pydbus"]
31+
2332
[build-system]
2433
requires = ["poetry>=0.12"]
2534
build-backend = "poetry.masonry.api"

0 commit comments

Comments
 (0)