Skip to content

Commit d3ee3c3

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

File tree

2 files changed

+66
-11
lines changed

2 files changed

+66
-11
lines changed

aw_watcher_window/lib.py

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,65 @@
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+
self.gnome_shell = None
14+
15+
if self.bus:
16+
import gi.repository.GLib
17+
try:
18+
self.gnome_shell = self.bus.get("org.gnome.Shell")
19+
except gi.repository.GLib.Error:
20+
self.gnome_shell = None
21+
22+
def get_current_window(self) -> dict:
23+
if self.gnome_shell:
24+
return self.get_current_window_gnome_shell()
25+
26+
return self.get_current_window_x11()
27+
28+
def get_current_window_gnome_shell(self) -> dict:
29+
"""get current app from GNOME Shell via dbus"""
30+
js_code = """
31+
var window_list = global.get_window_actors();
32+
var active_window_actor = window_list.find(window => window.meta_window.has_focus());
33+
var active_window = active_window_actor.get_meta_window()
34+
var vm_class = active_window.get_wm_class();
35+
var title = active_window.get_title()
36+
var result = {"title": title, "appname": vm_class};
37+
result
38+
"""
39+
40+
ok, result = self.gnome_shell.Eval(js_code)
41+
if ok:
42+
result_data = json.loads(result)
43+
return result_data
44+
45+
return {"appname": "unknown", "title": "unknown"}
46+
47+
def get_current_window_x11(self) -> dict:
48+
from . import xlib
49+
window = xlib.get_current_window()
50+
51+
if window is None:
52+
cls = "unknown"
53+
name = "unknown"
54+
else:
55+
cls = xlib.get_window_class(window)
56+
name = xlib.get_window_name(window)
57+
58+
return {"appname": cls, "title": name}
859

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)
1560

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

1864

1965
def get_current_window_macos() -> Optional[dict]:
@@ -41,7 +87,7 @@ def get_current_window_windows() -> Optional[dict]:
4187

4288
def get_current_window() -> Optional[dict]:
4389
if sys.platform.startswith("linux"):
44-
return get_current_window_linux()
90+
return linux.get_current_window()
4591
elif sys.platform == "darwin":
4692
return get_current_window_macos()
4793
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)