-
Notifications
You must be signed in to change notification settings - Fork 25
/
single_window_expo.py
executable file
·138 lines (115 loc) · 4.2 KB
/
single_window_expo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python3
# Author: Serg Kolo
# Date: Sept 28, 2016
# Purpose: activates
# Depends: python3-gi
# xdotool
# Written for: http://askubuntu.com/q/651188/295286
# just in case user runs this with python 2
from __future__ import print_function
import gi
gi.require_version('Gdk', '3.0')
from gi.repository import Gdk,Gio
import sys
import dbus
import subprocess
def run_cmd(cmdlist):
""" Reusable function for running shell commands"""
try:
stdout = subprocess.check_output(cmdlist)
except subprocess.CalledProcessError:
print(">>> subprocess:",cmdlist)
sys.exit(1)
else:
if stdout:
return stdout
def gsettings_get(schema,path,key):
"""Get value of gsettings schema"""
if path is None:
gsettings = Gio.Settings.new(schema)
else:
gsettings = Gio.Settings.new_with_path(schema,path)
return gsettings.get_value(key)
def get_launcher_object(screen):
# Unity allows launcher to be on multiple
# monitors, so we need to account for all
# window objects of the launcher
launchers = []
for window in screen.get_window_stack():
xid = window.get_xid()
command = ['xprop','-notype',
'WM_NAME','-id',str(xid)
]
xprop = run_cmd(command).decode()
title = xprop.replace("WM_NAME =","")
if title.strip() == '"unity-launcher"':
launchers.append(window)
#return window
return launchers
def get_dbus(bus_type,obj,path,interface,method,arg):
# Reusable function for accessing dbus
# This basically works the same as
# dbus-send or qdbus. Just give it
# all the info, and it will spit out output
if bus_type == "session":
bus = dbus.SessionBus()
if bus_type == "system":
bus = dbus.SystemBus()
proxy = bus.get_object(obj,path)
method = proxy.get_dbus_method(method,interface)
if arg:
return method(arg)
else:
return method()
def main():
previous_xid = int()
screen = Gdk.Screen.get_default()
while True:
current_xid = screen.get_active_window().get_xid()
if int(current_xid) == previous_xid:
continue
previous_xid = int(current_xid)
icon_size = gsettings_get(
'org.compiz.unityshell',
'/org/compiz/profiles/unity/plugins/unityshell/',
'icon-size')
icon_size = int(str(icon_size))
position = str(gsettings_get(
'com.canonical.Unity.Launcher',
None,
'launcher-position'))
screen = Gdk.Screen.get_default()
launcher_objs = get_launcher_object(screen)
# for faster processing,figure out which launcher is used
# first before running xdotool command. We also need
# to account for different launcher positions (available since 16.04)
pointer_on_launcher = None
for launcher in launcher_objs:
if 'Left' in position and \
abs(launcher.get_pointer().x) <= icon_size:
pointer_on_launcher = True
elif 'Bottom' in position and \
abs(launcher.get_pointer().y) <= icon_size:
pointer_on_launcher = True
else:
continue
active_xid = int(screen.get_active_window().get_xid())
application = get_dbus('session',
'org.ayatana.bamf',
'/org/ayatana/bamf/matcher',
'org.ayatana.bamf.matcher',
'ApplicationForXid',
active_xid)
# Apparently desktop window returns empty application
# we need to account for that as well
if application:
xids = list(get_dbus('session',
'org.ayatana.bamf',
application,
'org.ayatana.bamf.application',
'Xids',None))
if pointer_on_launcher and\
len(xids) == 1:
run_cmd(['xdotool','key','Ctrl+Super+W'])
if __name__ == '__main__':
main()