-
-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: add gnome-shell compatibility #46
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,77 @@ | ||
import sys | ||
import json | ||
import logging | ||
from typing import Optional | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
def get_current_window_linux() -> Optional[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) | ||
class Linux: | ||
def __init__(self): | ||
try: | ||
import pydbus | ||
self.bus = pydbus.SessionBus() | ||
except ModuleNotFoundError: | ||
logger.info("pydbus not installed, GNOME-Shell Wayland support disabled") | ||
self.bus = False | ||
self.gnome_shell = None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. self.bus = None and self.gnome_shell = False would make more sense. |
||
|
||
if self.bus: | ||
import gi.repository.GLib | ||
try: | ||
self.gnome_shell = self.bus.get("org.gnome.Shell") | ||
self._setup_gnome() | ||
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 _setup_gnome(self) -> None: | ||
js_code = """ | ||
global._aw_current_window = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
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}; | ||
return result | ||
} | ||
""" | ||
ok, result = self.gnome_shell.Eval(js_code) | ||
if not ok: | ||
raise Error("failed seting up gnome-shell function: " + result) | ||
|
||
def get_current_window_gnome_shell(self) -> dict: | ||
"""get current app from GNOME Shell via dbus""" | ||
|
||
ok, result = self.gnome_shell.Eval("global._aw_current_window()") | ||
if ok: | ||
result_data = json.loads(result) | ||
return result_data | ||
|
||
return {"appname": "unknown", "title": "unknown"} | ||
|
||
def get_current_window_x11(self) -> 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} | ||
|
||
|
||
return {"appname": cls, "title": name} | ||
if sys.platform.startswith("linux"): | ||
linux = Linux() | ||
|
||
|
||
def get_current_window_macos() -> Optional[dict]: | ||
|
@@ -41,7 +99,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"]: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All other platform specific changes are in seperate files so would make more sense to create a new gnome_wayland.py file and only check for which backend to use in lib.py