-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
WindowTracker.vala
320 lines (255 loc) · 10.5 KB
/
WindowTracker.vala
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Copyright 2021 elementary, Inc. <https://elementary.io>
* Copyright 2021 Corentin Noël <tintou@noel.tf>
* SPDX-License-Identifier: GPL-3.0-or-later
*/
public class Gala.WindowTracker : GLib.Object {
private GLib.HashTable<unowned Meta.Window, Gala.App> window_to_app;
public signal void windows_changed ();
construct {
window_to_app = new GLib.HashTable<unowned Meta.Window, Gala.App> (direct_hash, direct_equal);
}
public void init (Meta.Display display) {
unowned Meta.StartupNotification sn = display.get_startup_notification ();
sn.changed.connect (on_startup_sequence_changed);
load_initial_windows (display);
init_window_tracking (display);
}
private void load_initial_windows (Meta.Display display) {
GLib.List<weak Meta.Window> windows = display.list_all_windows ();
foreach (weak Meta.Window window in windows) {
track_window (window);
}
}
private void init_window_tracking (Meta.Display display) {
display.notify["focus-window"].connect (() => {});
display.window_created.connect ((window) => track_window (window));
}
private void on_startup_sequence_changed (Meta.StartupSequence sequence) {
unowned Gala.App? app = Gala.App.new_from_startup_sequence (sequence);
if (app != null) {
app.handle_startup_sequence (sequence);
}
}
private static unowned Gala.App? get_app_from_id (string id) {
var desktop_file = id.concat (".desktop");
return Gala.AppSystem.get_default ().lookup_app (desktop_file);
}
private static unowned Gala.App? get_app_from_gapplication_id (Meta.Window window) {
unowned string? id = window.get_gtk_application_id ();
if (id == null) {
return null;
}
return get_app_from_id (id);
}
private static unowned Gala.App? get_app_from_pid (Posix.pid_t pid) {
var running_apps = Gala.AppSystem.get_default ().get_running_apps ();
foreach (unowned Gala.App app in running_apps) {
var app_pids = app.get_pids ();
foreach (var app_pid in app_pids) {
if (app_pid == pid) {
return app;
}
}
}
return null;
}
private unowned Gala.App? get_app_from_window_pid (Meta.Window window) {
if (window.is_remote ()) {
return null;
}
var pid = window.get_pid ();
if (pid < 1) {
return null;
}
return get_app_from_pid (pid);
}
private static bool check_app_id_prefix (Gala.App app, string? prefix) {
if (prefix == null) {
return true;
}
return app.id.has_prefix (prefix);
}
private unowned Gala.App? get_app_from_window_wmclass (Meta.Window window) {
string? app_prefix = null;
unowned string? sandbox_id = window.get_sandboxed_app_id ();
if (sandbox_id != null) {
app_prefix = "%s.".printf (sandbox_id);
}
/* Notes on the heuristics used here:
much of the complexity here comes from the desire to support
Chrome apps.
From https://bugzilla.gnome.org/show_bug.cgi?id=673657#c13
Currently chrome sets WM_CLASS as follows (the first string is the 'instance',
the second one is the 'class':
For the normal browser:
WM_CLASS(STRING) = "chromium", "Chromium"
For a bookmarked page (through 'Tools -> Create application shortcuts')
WM_CLASS(STRING) = "wiki.gnome.org__GnomeShell_ApplicationBased", "Chromium"
For an application from the chrome store (with a .desktop file created through
right click, "Create shortcuts" from Chrome's apps overview)
WM_CLASS(STRING) = "crx_blpcfgokakmgnkcojhhkbfbldkacnbeo", "Chromium"
The .desktop file has a matching StartupWMClass, but the name differs, e.g. for
the store app (youtube) there is
.local/share/applications/chrome-blpcfgokakmgnkcojhhkbfbldkacnbeo-Default.desktop
with
StartupWMClass=crx_blpcfgokakmgnkcojhhkbfbldkacnbeo
Note that chromium (but not google-chrome!) includes a StartupWMClass=chromium
in their .desktop file, so we must match the instance first.
Also note that in the good case (regular gtk+ app without hacks), instance and
class are the same except for case and there is no StartupWMClass at all.
*/
/* first try a match from WM_CLASS (instance part) to StartupWMClass */
unowned string wm_instance = window.get_wm_class_instance ();
unowned var appsys = Gala.AppSystem.get_default ();
unowned Gala.App? app = appsys.lookup_startup_wmclass (wm_instance);
if (app != null && check_app_id_prefix (app, app_prefix)) {
return app;
}
/* then try a match from WM_CLASS to StartupWMClass */
unowned string wm_class = window.get_wm_class ();
app = appsys.lookup_startup_wmclass (wm_class);
if (app != null && check_app_id_prefix (app, app_prefix)) {
return app;
}
/* then try a match from WM_CLASS (instance part) to .desktop */
app = appsys.lookup_desktop_wmclass (wm_instance);
if (app != null && check_app_id_prefix (app, app_prefix)) {
return app;
}
/* finally, try a match from WM_CLASS to .desktop */
app = appsys.lookup_desktop_wmclass (wm_class);
if (app != null && check_app_id_prefix (app, app_prefix)) {
return app;
}
return null;
}
private unowned Gala.App? get_app_from_sandboxed_app_id (Meta.Window window) {
unowned string? id = window.get_sandboxed_app_id ();
if (id == null) {
return null;
}
return get_app_from_id (id);
}
private unowned Gala.App? get_app_from_window_group (Meta.Window window) {
unowned Meta.Group? group = window.get_group ();
if (group == null) {
return null;
}
GLib.SList<weak Meta.Window> group_windows = group.list_windows ();
foreach (weak Meta.Window group_window in group_windows) {
if (group_window.window_type != Meta.WindowType.NORMAL) {
continue;
}
unowned Gala.App? result = window_to_app.lookup (group_window);
if (result != null) {
return result;
}
}
return null;
}
private Gala.App get_app_for_window (Meta.Window window) {
unowned Meta.Window? transient_for = window.get_transient_for ();
if (transient_for != null) {
return get_app_for_window (transient_for);
}
/* First, we check whether we already know about this window,
* if so, just return that.
*/
unowned Gala.App? result;
if (window.window_type == Meta.WindowType.NORMAL || window.is_remote ()) {
result = window_to_app.lookup (window);
if (result != null) {
return result;
}
}
if (window.is_remote ()) {
return new Gala.App.for_window (window);
}
/* Check if the app's WM_CLASS specifies an app; this is
* canonical if it does.
*/
result = get_app_from_window_wmclass (window);
if (result != null) {
return result;
}
/* Check if the window was opened from within a sandbox; if this
* is the case, a corresponding .desktop file is guaranteed to match;
*/
result = get_app_from_sandboxed_app_id (window);
if (result != null) {
return result;
}
/* Check if the window has a GApplication ID attached; this is
* canonical if it does
*/
result = get_app_from_gapplication_id (window);
if (result != null) {
return result;
}
result = get_app_from_window_pid (window);
if (result != null) {
return result;
}
/* Now we check whether we have a match through startup-notification */
unowned string? startup_id = window.get_startup_id ();
if (startup_id != null) {
unowned Meta.StartupNotification sn = window.get_display ().get_startup_notification ();
unowned GLib.SList<Meta.StartupSequence> sequences = sn.get_sequences ();
foreach (unowned var sequence in sequences) {
unowned string id = sequence.get_id ();
if (id != startup_id) {
continue;
}
unowned string? appid = sequence.get_application_id ();
if (appid != null) {
result = AppSystem.get_default ().lookup_app (GLib.Path.get_basename (appid));
if (result != null) {
return result;
}
}
}
}
/* If we didn't get a startup-notification match, see if we matched
* any other windows in the group.
*/
result = get_app_from_window_group (window);
if (result != null) {
return result;
}
/* Our last resort - we create a fake app from the window */
return new Gala.App.for_window (window);
}
private void tracked_window_changed (Meta.Window window) {
/* It's simplest to just treat this as a remove + add. */
disassociate_window (window);
track_window (window);
}
private void tracked_window_notified (GLib.Object object, GLib.ParamSpec pspec) {
tracked_window_changed ((Meta.Window) object);
}
private void track_window (Meta.Window window) {
var app = get_app_for_window (window);
if (app == null) {
return;
}
window_to_app.insert (window, app);
window.notify["wm-class"].connect (tracked_window_notified);
window.notify["gtk-application-id"].connect (tracked_window_notified);
window.unmanaged.connect (disassociate_window);
app.add_window (window);
windows_changed ();
}
private void disassociate_window (Meta.Window window) {
var app = get_app_for_window (window);
if (app == null) {
return;
}
window.unmanaged.disconnect (disassociate_window);
window.notify["wm-class"].disconnect (tracked_window_notified);
window.notify["gtk-application-id"].disconnect (tracked_window_notified);
app.remove_window (window);
window_to_app.remove (window);
windows_changed ();
}
}