Created
December 13, 2021 22:19
-
-
Save jairovsky/2a1133f90d53852f9b1d481c229f3943 to your computer and use it in GitHub Desktop.
switch_window - alternative version for Gnome on Wayland
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import dbus | |
from pprint import pprint | |
import json | |
import sys | |
import time; | |
def focus_wnd(proxy, id): | |
(ok, res) = proxy.Eval( | |
""" | |
global.get_window_actors() | |
.map(a=>a.meta_window) | |
.find(w=> w.get_id() == %s) | |
.activate(%s) | |
"""%(target_wnd_id, int(time.time())), | |
dbus_interface='org.gnome.Shell') | |
if ok: | |
print('ok') | |
else: | |
print('couldnt focus on window') | |
pass | |
if __name__ == "__main__": | |
which_wnd_class = sys.argv[1] | |
sessbus = dbus.SessionBus() | |
proxy = sessbus.get_object( 'org.gnome.Shell', '/org/gnome/Shell') | |
(ok, res) = proxy.Eval( | |
""" | |
global.get_window_actors() | |
.map(a=>a.meta_window) | |
.filter(w => w.has_focus() == false) | |
.filter(w => w.get_wm_class() != 'Gnome-shell') | |
.map(w=>({id: w.get_id(), class: w.get_wm_class(), title: w.get_title()})) | |
""", | |
dbus_interface='org.gnome.Shell') | |
if ok: | |
windows = json.loads(str(res)) | |
if which_wnd_class == 'debug': | |
for w in windows: | |
print("%s\t%s\t%s"%(w['id'], w['class'], w['title'])) | |
exit(0) | |
by_class = list(filter( | |
lambda x: x['class'] == which_wnd_class, | |
windows | |
)) | |
if len(by_class) > 0: | |
target_wnd_id = by_class[-1]['id'] | |
focus_wnd(proxy, target_wnd_id) | |
else: | |
# if no unfocused window of the requested class is found, | |
# focus on the second topmost from the stack regardless of class. | |
# this is equivalent of hitting alt+tab | |
target_wnd_id = windows[-1]['id'] | |
focus_wnd(proxy, target_wnd_id) | |
else: | |
print('error while communicating via dbus') | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment