-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwintoggle.c
More file actions
426 lines (363 loc) · 11 KB
/
Copy pathwintoggle.c
File metadata and controls
426 lines (363 loc) · 11 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#define WT_VERSION "3.0.1"
#define WT_HELP_MESSAGE "\
This is a short usage summary. See wintoggle(1) for more information. \n\
\n\
Usage: wintoggle [--help] [--version] class [executable] \n\
\n\
Positional arguments: \n\
class The window class to match. \n\
executable The executable to launch. It defaults to the window class. \n\
\n\
Optional arguments: \n\
-h, --help List available options and exit. \n\
-v, --version Print the program version and exit."
#define SOURCE_INDICATION 2L // Pager
#define DEFAULT_WT_WINDOW_INFO (WTWindowInfo) { .window = 0, .screen = -1, .class_name = NULL };
typedef struct
{
const Window window;
const char* class_name;
const int screen;
} WTWindowInfo;
typedef struct
{
const char* class_name;
const char* executable;
} WTParams;
typedef enum
{
WT_PARAMS_OK,
WT_PARAMS_INVALID,
WT_PARAMS_EXIT
} WTParamsStatus;
long
wt_get_long_window_property(Display* display, Window window, const char* property)
{
long result;
unsigned char* value = NULL;
Atom atom = XInternAtom(display, property, false);
// Assume that the result is an int and ignore the following properties
Atom actual_type;
int actual_format;
unsigned long n_items;
unsigned long bytes_after;
if (XGetWindowProperty(
display,
window,
atom,
0,
1,
false,
AnyPropertyType,
&actual_type,
&actual_format,
&n_items,
&bytes_after,
&value
) != Success) {
fprintf(stderr, "Could not acquire property '%s' for window %lu.\n", property, window);
return -1;
}
if (value == NULL) {
return -1;
}
result = *(long*)value;
XFree(value);
return result;
}
int
wt_get_desktop(Display* display, Window window)
{
return wt_get_long_window_property(display, window, "_NET_WM_DESKTOP");
}
bool
wt_get_window_name(Display* display, Window window, char** class_name_return)
{
XClassHint class_hint;
if (XGetClassHint(display, window, &class_hint) == 0)
{
// This condition fails for most windows so don't print an error message
// fprintf(stderr, "Could not get the window class hint of window %lu.\n", window);
return false;
}
int name_length = strlen(class_hint.res_name);
class_name_return[0] = (char*) malloc(name_length + 1);
memcpy(*class_name_return, class_hint.res_name, name_length);
class_name_return[0][name_length] = '\0';
XFree(class_hint.res_name);
XFree(class_hint.res_class);
return true;
}
bool
wt_window_matches(Display* display, Window window, const char* class_name)
{
char* class_name_buffer = NULL;
if (!wt_get_window_name(display, window, &class_name_buffer))
return false;
bool result = strcmp(class_name_buffer, class_name) == 0;
free(class_name_buffer);
return result;
}
WTWindowInfo
wt_search_by_class_name_recursively(
Display* display,
int screen,
const char* class_name,
Window root,
Window window
)
{
Window parent; // This variable is not actually used.
Window* children = NULL;
unsigned int child_count;
if (wt_window_matches(display, window, class_name))
return (WTWindowInfo) { .window = window, .screen = screen, .class_name = class_name };
if (XQueryTree(display, window, &root, &parent, &children, &child_count) == 0)
fprintf(stderr, "Error: Could not acquire the children of window %lu\n.", parent);
for (size_t i = 0; i < child_count; i++)
{
WTWindowInfo match = wt_search_by_class_name_recursively(
display,
screen,
class_name,
root,
children[i]
);
if (match.window > 0)
{
XFree(children);
return match;
}
}
XFree(children);
return DEFAULT_WT_WINDOW_INFO;
}
WTWindowInfo
wt_find_window_by_class_name(Display* display, const char* class_name)
{
for (int screen = 0; screen < XScreenCount(display); screen++)
{
Window root = XRootWindow(display, screen);
WTWindowInfo match = wt_search_by_class_name_recursively(
display,
screen,
class_name,
root,
root
);
if (match.window > 0)
return match;
}
return DEFAULT_WT_WINDOW_INFO;
}
bool
wt_send_event(
Display* display,
Window target,
WTWindowInfo info,
const char* message_type,
size_t data_size,
const long* data
)
{
XEvent event;
memset(&event, 0, sizeof(event));
memcpy(event.xclient.data.l, data, data_size * sizeof(long) / sizeof(char));
event.type = ClientMessage;
event.xclient.format = 32;
event.xclient.window = info.window;
event.xclient.display = display;
event.xclient.message_type = XInternAtom(display, message_type, false);
if (XSendEvent(
display,
target,
false,
SubstructureNotifyMask | SubstructureRedirectMask,
&event
) == 0)
{
fprintf(stderr, "Could not send a '%s' event to window %lu (%s).\n", message_type, info.window, info.class_name);
return false;
}
return true;
}
bool
wt_minimize_window(Display* display, WTWindowInfo info)
{
if (XIconifyWindow(display, info.window, info.screen) == 0)
{
fprintf(stderr, "Could not iconify window %lu (%s).\n", info.window, info.class_name);
return false;
}
XSync(display, false);
return true;
}
bool
wt_focus_window(Display* display, WTWindowInfo info)
{
Window root = XDefaultRootWindow(display);
long active_desktop = wt_get_long_window_property(display, root, "_NET_CURRENT_DESKTOP");
if (
!wt_send_event(
display, root, info, "_NET_WM_DESKTOP", 2,
(long[]) { active_desktop, SOURCE_INDICATION }
) ||
!wt_send_event(
display, root, info, "_NET_ACTIVE_WINDOW", 2,
(long[]) { SOURCE_INDICATION, CurrentTime }
)
)
return false;
XSync(display, false);
return true;
}
WTWindowInfo
wt_active_window_info(Display* display, char** class_name_return)
{
Window root = XDefaultRootWindow(display);
class_name_return[0] = NULL;
long active_window_id = wt_get_long_window_property(display, root, "_NET_ACTIVE_WINDOW");
int screen = 0;
if (active_window_id <= 0)
return DEFAULT_WT_WINDOW_INFO;
Window active = (Window)active_window_id;
XWindowAttributes attributes;
memset(&attributes, 0, sizeof(attributes));
XGetWindowAttributes(display, active, &attributes);
screen = XScreenNumberOfScreen(attributes.screen);
wt_get_window_name(display, active, class_name_return);
return (WTWindowInfo) {
.window = active,
.screen = screen,
.class_name = *class_name_return
};
}
WTParamsStatus
wt_params_populate(size_t arg_count, const char** args, WTParams* params)
{
WTParams result; // Use a working copy and copy it to <params> only on success
memset(&result, 0, sizeof(result));
bool after_split = false;
for (size_t i = 0; i < arg_count; i++)
{
const char* arg = args[i];
if (after_split)
goto notopt;
if (strcmp(arg, "--") == 0)
after_split = true;
else if (arg[0] == '-')
{
if (arg[1] == '\0')
continue;
else if (strcmp(arg, "-v") == 0 || strcmp(arg, "--version") == 0)
{
puts(WT_VERSION);
return WT_PARAMS_EXIT;
}
else if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0)
{
puts(WT_HELP_MESSAGE);
return WT_PARAMS_EXIT;
}
else
{
fprintf(stderr, "Invalid option '%s'.\n", arg);
return WT_PARAMS_INVALID;
}
}
else
goto notopt;
continue;
notopt:
if (result.class_name == NULL)
result.class_name = arg;
else if (result.executable == NULL)
result.executable = arg;
else
{
fprintf(stderr, "Excess argument '%s'.\n", arg);
return WT_PARAMS_INVALID;
}
}
if (result.class_name == NULL)
{
fputs("No class name specified.\n", stderr);
return WT_PARAMS_INVALID;
}
if (result.executable == NULL)
result.executable = result.class_name;
memcpy(params, &result, sizeof(WTParams));
return WT_PARAMS_OK;
}
int
main(int argc, const char** argv)
{
WTParams params;
switch (wt_params_populate(argc - 1, &argv[1], ¶ms))
{
case WT_PARAMS_INVALID: return 1;
case WT_PARAMS_EXIT: return 0;
case WT_PARAMS_OK: break;
}
Display* display = XOpenDisplay(NULL); // Open the default display
if (display == NULL)
{
fputs("Error: Could not open the default display.\n", stderr);
return 1;
}
WTWindowInfo match = wt_find_window_by_class_name(display, params.class_name);
// Step 1
{
char* class_name_buffer = NULL;
Window root = XDefaultRootWindow(display);
long active_window_id = wt_get_long_window_property(display, root, "_NET_ACTIVE_WINDOW");
// The initial implementation compared the active window class name with the class_name param
// but sometimes, after closing the last window, OpenBox did not update _NET_SHOWING_DESKTOP
// but left the root window with an invalid _NET_ACTIVE_WINDOW.
if (match.window > 0 && match.window == (unsigned long)active_window_id)
{
WTWindowInfo active_info = wt_active_window_info(display, &class_name_buffer);
printf("[Step 1] Minimizing window %lu (%s).\n", active_info.window, active_info.class_name);
bool status = wt_minimize_window(display, active_info);
free(class_name_buffer);
XCloseDisplay(display);
return status;
}
else
{
printf("[Step 1] The active window does not match class '%s'.\n", params.class_name);
free(class_name_buffer);
}
}
// Step 2
{
if (match.window > 0)
{
printf("[Step 2] Presenting window %lu (%s) to the current workspace.\n", match.window, params.class_name);
bool status = wt_focus_window(display, match);
XCloseDisplay(display);
return status;
}
else
printf("[Step 2] The currently mapped windows do not match class '%s'.\n", params.class_name);
}
// Step 3
{
XCloseDisplay(display);
printf("[Step 3] Executing '%s'.\n", params.executable);
fflush(stdout);
if (execlp(params.executable, params.executable, NULL) == -1)
{
perror("Could not execute the specified program");
return 1;
}
return 0;
}
}