Skip to content

Commit

Permalink
add gnome-shell compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianLudwig committed Jun 10, 2020
1 parent fbec7ed commit e9c4f3d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
67 changes: 56 additions & 11 deletions aw_watcher_window/lib.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,64 @@
import sys
import json
from typing import Optional


def get_current_window_linux() -> Optional[dict]:
from . import xlib
window = xlib.get_current_window()
class Linux:
def __init__(self):
try:
import pydbus
self.bus = pydbus.SessionBus()
except ModuleNotFoundError:
self.bus = False

if self.bus:
import gi.repository.GLib
try:
self.gnome_shell = self.bus.get("org.gnome.Shell")
except gi.repository.GLib.Error:
self.gnome_shell = None

def get_current_window(self) -> dict:
if self.gnome_shell:
return self.get_current_window_gnome_shell()

return self.get_current_window_x11()

def get_current_window_gnome_shell(self) -> dict:
"""get current app from GNOME Shell via dbus"""
js_code = """
var window_list = global.get_window_actors();
var active_window_actor = window_list.find(window => window.meta_window.has_focus());
var active_window = active_window_actor.get_meta_window()
var vm_class = active_window.get_wm_class();
var title = active_window.get_title()
var result = {"title": title, "appname": vm_class};
result
"""

ok, result = self.gnome_shell.Eval(js_code)
if ok:
result_data = json.loads(result)
return result_data

return {"appname": "unknown", "title": "unknown"}

def get_current_window_x11() -> dict:
from . import xlib
window = xlib.get_current_window()

if window is None:
cls = "unknown"
name = "unknown"
else:
cls = xlib.get_window_class(window)
name = xlib.get_window_name(window)

return {"appname": cls, "title": name}

if window is None:
cls = "unknown"
name = "unknown"
else:
cls = xlib.get_window_class(window)
name = xlib.get_window_name(window)

return {"appname": cls, "title": name}
if sys.platform.startswith("linux"):
linux = Linux()


def get_current_window_macos() -> Optional[dict]:
Expand Down Expand Up @@ -41,7 +86,7 @@ def get_current_window_windows() -> Optional[dict]:

def get_current_window() -> Optional[dict]:
if sys.platform.startswith("linux"):
return get_current_window_linux()
return linux.get_current_window()
elif sys.platform == "darwin":
return get_current_window_macos()
elif sys.platform in ["win32", "cygwin"]:
Expand Down
9 changes: 9 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,20 @@ python-xlib = {version = "^0.26", platform = "linux"}
pypiwin32 = {version = "223", platform = "win32"}
wmi = {version = "^1.4.9", platform = "win32"}

# pydbus depends on PyGObject, which needs several system libs installed,
# to be able to be compiled:
# * gobject-introspection-devel
# * cairo-gobject-devel
pydbus = { version = "^0.6.0", platform = "linux", optional = true }

[tool.poetry.dev-dependencies]
pytest = "^5.3.2"
mypy = "^0.761"
macholib = {version = "^1.13", platform = "darwin"} # Needed for pyinstaller

[tool.poetry.extras]
gnome = ["pydbus"]

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

0 comments on commit e9c4f3d

Please sign in to comment.