-
Notifications
You must be signed in to change notification settings - Fork 0
/
wmctrl.c
1228 lines (1054 loc) · 39.7 KB
/
wmctrl.c
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* license
wmjump
a program for the keyboard navigation of the desktop;
use the keyboard to switch between windows and workspaces
Author: Andrei Mikhailov <a.mkhlv@gmail.com>
Copyright (C) 2008
This program is free software which I release under the GNU General Public
License. You may redistribute and/or modify this program under the terms
of that license as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
To get a copy of the GNU General Puplic License, write to the
Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* license {{{ */
/*
wmctrl
A command line tool to interact with an EWMH/NetWM compatible X Window Manager.
Author, current maintainer: Tomas Styblo <tripie@cpan.org>
Copyright (C) 2003
This program is free software which I release under the GNU General Public
License. You may redistribute and/or modify this program under the terms
of that license as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
To get a copy of the GNU General Puplic License, write to the
Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* }}} */
#include "wmctrl.h"
#define p_verbose(...) if (options.verbose) { \
fprintf(stderr, __VA_ARGS__); \
}
gboolean envir_utf8;
struct optstruct options;
unsigned long get_timestamp (Display* disp) {
unsigned long * tstump;
if ( (tstump = (unsigned long *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_NET_WM_USER_TIME", NULL)) == NULL)
{return 0L ; } else { return * tstump;}
g_free(tstump); tstump = NULL ;
}
void init_charset (void) {/*{{{*/
const gchar *charset; /* unused */
gchar *lang = getenv("LANG") ? g_ascii_strup(getenv("LANG"), -1) : NULL;
gchar *lc_ctype = getenv("LC_CTYPE") ? g_ascii_strup(getenv("LC_CTYPE"), -1) : NULL;
/* this glib function doesn't work on my system ... */
envir_utf8 = g_get_charset(&charset);
/* ... therefore we will examine the environment variables */
if (lc_ctype && (strstr(lc_ctype, "UTF8") || strstr(lc_ctype, "UTF-8"))) {
envir_utf8 = TRUE;
}
else if (lang && (strstr(lang, "UTF8") || strstr(lang, "UTF-8"))) {
envir_utf8 = TRUE;
}
g_free(lang);
g_free(lc_ctype);
if (options.force_utf8) {
envir_utf8 = TRUE;
}
p_verbose("envir_utf8: %d\n", envir_utf8);
}/*}}}*/
int client_msg(Display *disp, Window win, char *msg, /* {{{ */
unsigned long data0, unsigned long data1,
unsigned long data2, unsigned long data3,
unsigned long data4) {
XEvent event;
long mask = SubstructureRedirectMask | SubstructureNotifyMask;
event.xclient.type = ClientMessage;
event.xclient.serial = 0;
event.xclient.send_event = True;
event.xclient.message_type = XInternAtom(disp, msg, False);
event.xclient.window = win;
event.xclient.format = 32;
event.xclient.data.l[0] = data0;
event.xclient.data.l[1] = data1;
event.xclient.data.l[2] = data2;
event.xclient.data.l[3] = data3;
event.xclient.data.l[4] = data4;
if (XSendEvent(disp, DefaultRootWindow(disp), False, mask, &event)) {
return EXIT_SUCCESS;
}
else {
fprintf(stderr, "Cannot send %s event.\n", msg);
return EXIT_FAILURE;
}
}/*}}}*/
gchar *get_output_str (gchar *str, gboolean is_utf8) {/*{{{*/
gchar *out;
if (str == NULL) {
return NULL;
}
if (envir_utf8) {
if (is_utf8) {
out = g_strdup(str);
}
else {
if (! (out = g_locale_to_utf8(str, -1, NULL, NULL, NULL))) {
p_verbose("Cannot convert string from locale charset to UTF-8.\n");
out = g_strdup(str);
}
}
}
else {
if (is_utf8) {
if (! (out = g_locale_from_utf8(str, -1, NULL, NULL, NULL))) {
p_verbose("Cannot convert string from UTF-8 to locale charset.\n");
out = g_strdup(str);
}
}
else {
out = g_strdup(str);
}
}
return out;
}/*}}}*/
int wm_info (Display *disp) {/*{{{*/
Window *sup_window = NULL;
gchar *wm_name = NULL;
gchar *wm_class = NULL;
unsigned long *wm_pid = NULL;
unsigned long *showing_desktop = NULL;
gboolean name_is_utf8 = TRUE;
gchar *name_out;
gchar *class_out;
if (! (sup_window = (Window *)get_property(disp, DefaultRootWindow(disp),
XA_WINDOW, "_NET_SUPPORTING_WM_CHECK", NULL))) {
if (! (sup_window = (Window *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_WIN_SUPPORTING_WM_CHECK", NULL))) {
fputs("Cannot get window manager info properties.\n"
"(_NET_SUPPORTING_WM_CHECK or _WIN_SUPPORTING_WM_CHECK)\n", stderr);
return EXIT_FAILURE;
}
}
/* WM_NAME */
if (! (wm_name = get_property(disp, *sup_window,
XInternAtom(disp, "UTF8_STRING", False), "_NET_WM_NAME", NULL))) {
name_is_utf8 = FALSE;
if (! (wm_name = get_property(disp, *sup_window,
XA_STRING, "_NET_WM_NAME", NULL))) {
p_verbose("Cannot get name of the window manager (_NET_WM_NAME).\n");
}
}
name_out = get_output_str(wm_name, name_is_utf8);
/* WM_CLASS */
if (! (wm_class = get_property(disp, *sup_window,
XInternAtom(disp, "UTF8_STRING", False), "WM_CLASS", NULL))) {
name_is_utf8 = FALSE;
if (! (wm_class = get_property(disp, *sup_window,
XA_STRING, "WM_CLASS", NULL))) {
p_verbose("Cannot get class of the window manager (WM_CLASS).\n");
}
}
class_out = get_output_str(wm_class, name_is_utf8);
/* WM_PID */
if (! (wm_pid = (unsigned long *)get_property(disp, *sup_window,
XA_CARDINAL, "_NET_WM_PID", NULL))) {
p_verbose("Cannot get pid of the window manager (_NET_WM_PID).\n");
}
/* _NET_SHOWING_DESKTOP */
if (! (showing_desktop = (unsigned long *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_NET_SHOWING_DESKTOP", NULL))) {
p_verbose("Cannot get the _NET_SHOWING_DESKTOP property.\n");
}
/* print out the info */
printf("Name: %s\n", name_out ? name_out : "N/A");
printf("Class: %s\n", class_out ? class_out : "N/A");
if (wm_pid) {
printf("PID: %lu\n", *wm_pid);
}
else {
printf("PID: N/A\n");
}
if (showing_desktop) {
printf("Window manager's \"showing the desktop\" mode: %s\n",
*showing_desktop == 1 ? "ON" : "OFF");
}
else {
printf("Window manager's \"showing the desktop\" mode: N/A\n");
}
g_free(name_out);
g_free(sup_window);
g_free(wm_name);
g_free(wm_class);
g_free(wm_pid);
g_free(showing_desktop);
return EXIT_SUCCESS;
}/*}}}*/
int showing_desktop (Display *disp) {/*{{{*/
unsigned long state;
if (strcmp(options.param, "on") == 0) {
state = 1;
}
else if (strcmp(options.param, "off") == 0) {
state = 0;
}
else {
fputs("The argument to the -k option must be either \"on\" or \"off\"\n", stderr);
return EXIT_FAILURE;
}
return client_msg(disp, DefaultRootWindow(disp), "_NET_SHOWING_DESKTOP",
state, 0, 0, 0, 0);
}/*}}}*/
int change_viewport (Display *disp) {/*{{{*/
unsigned long x, y;
const char *argerr = "The -o option expects two integers separated with a comma.\n";
if (sscanf(options.param, "%lu,%lu", &x, &y) == 2) {
return client_msg(disp, DefaultRootWindow(disp), "_NET_DESKTOP_VIEWPORT",
x, y, 0, 0, 0);
}
else {
fputs(argerr, stderr);
return EXIT_FAILURE;
}
}/*}}}*/
int change_geometry (Display *disp) {/*{{{*/
unsigned long x, y;
const char *argerr = "The -g option expects two integers separated with a comma.\n";
if (sscanf(options.param, "%lu,%lu", &x, &y) == 2) {
return client_msg(disp, DefaultRootWindow(disp), "_NET_DESKTOP_GEOMETRY",
x, y, 0, 0, 0);
}
else {
fputs(argerr, stderr);
return EXIT_FAILURE;
}
}/*}}}*/
int change_number_of_desktops (Display *disp) {/*{{{*/
unsigned long n;
if (sscanf(options.param, "%lu", &n) != 1) {
fputs("The -n option expects an integer.\n", stderr);
return EXIT_FAILURE;
}
return client_msg(disp, DefaultRootWindow(disp), "_NET_NUMBER_OF_DESKTOPS",
n, 0, 0, 0, 0);
}/*}}}*/
int switch_desktop (Display *disp) {/*{{{*/
int target = -1;
target = atoi(options.param);
if (target == -1) {
fputs("Invalid desktop ID.\n", stderr);
return EXIT_FAILURE;
}
return client_msg(disp, DefaultRootWindow(disp), "_NET_CURRENT_DESKTOP",
(unsigned long)target, 0, 0, 0, 0);
}/*}}}*/
void window_set_title (Display *disp, Window win, /* {{{ */
char *title, char mode) {
gchar *title_utf8;
gchar *title_local;
if (envir_utf8) {
title_utf8 = g_strdup(title);
title_local = NULL;
}
else {
if (! (title_utf8 = g_locale_to_utf8(title, -1, NULL, NULL, NULL))) {
title_utf8 = g_strdup(title);
}
title_local = g_strdup(title);
}
if (mode == 'T' || mode == 'N') {
/* set name */
if (title_local) {
XChangeProperty(disp, win, XA_WM_NAME, XA_STRING, 8, PropModeReplace,
(unsigned char *)title_local, strlen(title_local));
}
else {
XDeleteProperty(disp, win, XA_WM_NAME);
}
XChangeProperty(disp, win, XInternAtom(disp, "_NET_WM_NAME", False),
XInternAtom(disp, "UTF8_STRING", False), 8, PropModeReplace,
(unsigned char *)title_utf8, strlen(title_utf8));
}
if (mode == 'T' || mode == 'I') {
/* set icon name */
if (title_local) {
XChangeProperty(disp, win, XA_WM_ICON_NAME, XA_STRING, 8, PropModeReplace,
(unsigned char *)title_local, strlen(title_local));
}
else {
XDeleteProperty(disp, win, XA_WM_ICON_NAME);
}
XChangeProperty(disp, win, XInternAtom(disp, "_NET_WM_ICON_NAME", False),
XInternAtom(disp, "UTF8_STRING", False), 8, PropModeReplace,
(unsigned char *)title_utf8, strlen(title_utf8));
}
g_free(title_utf8);
g_free(title_local);
}/*}}}*/
int window_to_desktop (Display *disp, Window win, int desktop) {/*{{{*/
unsigned long *cur_desktop = NULL;
Window root = DefaultRootWindow(disp);
if (desktop == -1) {
if (! (cur_desktop = (unsigned long *)get_property(disp, root,
XA_CARDINAL, "_NET_CURRENT_DESKTOP", NULL))) {
if (! (cur_desktop = (unsigned long *)get_property(disp, root,
XA_CARDINAL, "_WIN_WORKSPACE", NULL))) {
fputs("Cannot get current desktop properties. "
"(_NET_CURRENT_DESKTOP or _WIN_WORKSPACE property)"
"\n", stderr);
return EXIT_FAILURE;
}
}
desktop = *cur_desktop;
}
g_free(cur_desktop);
return client_msg(disp, win, "_NET_WM_DESKTOP", (unsigned long)desktop,
0, 0, 0, 0);
}/*}}}*/
int activate_window (Display *disp, Window win, /* {{{ */
gboolean switch_desktop) {
unsigned long *desktop;
/* desktop ID */
if ((desktop = (unsigned long *)get_property(disp, win,
XA_CARDINAL, "_NET_WM_DESKTOP", NULL)) == NULL) {
if ((desktop = (unsigned long *)get_property(disp, win,
XA_CARDINAL, "_WIN_WORKSPACE", NULL)) == NULL) {
p_verbose("Cannot find desktop ID of the window.\n");
}
}
unsigned long tstamp = 0;
tstamp = get_timestamp(disp);
if (switch_desktop && desktop) {
if (client_msg(disp, DefaultRootWindow(disp),
"_NET_CURRENT_DESKTOP",
*desktop, tstamp, 0, 0, 0) != EXIT_SUCCESS) {
p_verbose("Cannot switch desktop.\n");
}
XSync(disp,FALSE);
nanosleep(&wait_time_short,NULL);
g_free(desktop); desktop = NULL ;
}
client_msg(disp, win, "_NET_ACTIVE_WINDOW", 0, tstamp, 0, 0, 0);
XMapRaised(disp, win);
/*XSync(disp,FALSE); */
return EXIT_SUCCESS;
}/*}}}*/
int close_window (Display *disp, Window win) {/*{{{*/
return client_msg(disp, win, "_NET_CLOSE_WINDOW",
0, 0, 0, 0, 0);
}/*}}}*/
int window_state (Display *disp, Window win, char *arg) {/*{{{*/
unsigned long action;
Atom prop1 = 0;
Atom prop2 = 0;
char *p1, *p2;
const char *argerr = "The -b option expects a list of comma separated parameters: \"(remove|add|toggle),<PROP1>[,<PROP2>]\"\n";
if (!arg || strlen(arg) == 0) {
fputs(argerr, stderr);
return EXIT_FAILURE;
}
if ((p1 = strchr(arg, ','))) {
gchar *tmp_prop1, *tmp1;
*p1 = '\0';
/* action */
if (strcmp(arg, "remove") == 0) {
action = _NET_WM_STATE_REMOVE;
}
else if (strcmp(arg, "add") == 0) {
action = _NET_WM_STATE_ADD;
}
else if (strcmp(arg, "toggle") == 0) {
action = _NET_WM_STATE_TOGGLE;
}
else {
fputs("Invalid action. Use either remove, add or toggle.\n", stderr);
return EXIT_FAILURE;
}
p1++;
/* the second property */
if ((p2 = strchr(p1, ','))) {
gchar *tmp_prop2, *tmp2;
*p2 = '\0';
p2++;
if (strlen(p2) == 0) {
fputs("Invalid zero length property.\n", stderr);
return EXIT_FAILURE;
}
tmp_prop2 = g_strdup_printf("_NET_WM_STATE_%s", tmp2 = g_ascii_strup(p2, -1));
p_verbose("State 2: %s\n", tmp_prop2);
prop2 = XInternAtom(disp, tmp_prop2, False);
g_free(tmp2);
g_free(tmp_prop2);
}
/* the first property */
if (strlen(p1) == 0) {
fputs("Invalid zero length property.\n", stderr);
return EXIT_FAILURE;
}
tmp_prop1 = g_strdup_printf("_NET_WM_STATE_%s", tmp1 = g_ascii_strup(p1, -1));
p_verbose("State 1: %s\n", tmp_prop1);
prop1 = XInternAtom(disp, tmp_prop1, False);
g_free(tmp1);
g_free(tmp_prop1);
return client_msg(disp, win, "_NET_WM_STATE",
action, (unsigned long)prop1, (unsigned long)prop2, 0, 0);
}
else {
fputs(argerr, stderr);
return EXIT_FAILURE;
}
}/*}}}*/
gboolean wm_supports (Display *disp, const gchar *prop) {/*{{{*/
Atom xa_prop = XInternAtom(disp, prop, False);
Atom *list;
unsigned long size;
unsigned int i;
if (! (list = (Atom *)get_property(disp, DefaultRootWindow(disp),
XA_ATOM, "_NET_SUPPORTED", &size))) {
p_verbose("Cannot get _NET_SUPPORTED property.\n");
return FALSE;
}
for (i = 0; i < size / sizeof(Atom); i++) {
if (list[i] == xa_prop) {
g_free(list);
return TRUE;
}
}
g_free(list);
return FALSE;
}/*}}}*/
int window_move_resize (Display *disp, Window win, char *arg) {/*{{{*/
signed long grav, x, y, w, h;
unsigned long grflags;
const char *argerr = "The -e option expects a list of comma separated integers: \"gravity,X,Y,width,height\"\n";
if (!arg || strlen(arg) == 0) {
fputs(argerr, stderr);
return EXIT_FAILURE;
}
if (sscanf(arg, "%ld,%ld,%ld,%ld,%ld", &grav, &x, &y, &w, &h) != 5) {
fputs(argerr, stderr);
return EXIT_FAILURE;
}
if (grav < 0) {
fputs("Value of gravity mustn't be negative. Use zero to use the default gravity of the window.\n", stderr);
return EXIT_FAILURE;
}
grflags = grav;
if (x != -1) grflags |= (1 << 8);
if (y != -1) grflags |= (1 << 9);
if (w != -1) grflags |= (1 << 10);
if (h != -1) grflags |= (1 << 11);
p_verbose("grflags: %lu\n", grflags);
if (wm_supports(disp, "_NET_MOVERESIZE_WINDOW")){
return client_msg(disp, win, "_NET_MOVERESIZE_WINDOW",
grflags, (unsigned long)x, (unsigned long)y, (unsigned long)w, (unsigned long)h);
}
else {
p_verbose("WM doesn't support _NET_MOVERESIZE_WINDOW. Gravity will be ignored.\n");
if ((w < 1 || h < 1) && (x >= 0 && y >= 0)) {
XMoveWindow(disp, win, x, y);
}
else if ((x < 0 || y < 0) && (w >= 1 && h >= -1)) {
XResizeWindow(disp, win, w, h);
}
else if (x >= 0 && y >= 0 && w >= 1 && h >= 1) {
XMoveResizeWindow(disp, win, x, y, w, h);
}
return EXIT_SUCCESS;
}
}/*}}}*/
int action_window (Display *disp, Window win, char mode) {/*{{{*/
p_verbose("Using window: 0x%.8lx\n", win);
switch (mode) {
case 'a':
return activate_window(disp, win, TRUE);
case 'c':
return close_window(disp, win);
case 'e':
/* resize/move the window around the desktop => -r -e */
return window_move_resize(disp, win, options.param);
case 'b':
/* change state of a window => -r -b */
return window_state(disp, win, options.param);
case 't':
/* move the window to the specified desktop => -r -t */
return window_to_desktop(disp, win, atoi(options.param));
case 'R':
/* move the window to the current desktop and activate it => -r */
if (window_to_desktop(disp, win, -1) == EXIT_SUCCESS) {
usleep(100000); /* 100 ms - make sure the WM has enough
time to move the window, before we activate it */
return activate_window(disp, win, FALSE);
}
else {
return EXIT_FAILURE;
}
case 'N': case 'I': case 'T':
window_set_title(disp, win, options.param, mode);
return EXIT_SUCCESS;
default:
fprintf(stderr, "Unknown action: '%c'\n", mode);
return EXIT_FAILURE;
}
}/*}}}*/
int action_window_pid (Display *disp, char mode) {/*{{{*/
unsigned long wid;
if (sscanf(options.param_window, "0x%lx", &wid) != 1 &&
sscanf(options.param_window, "0X%lx", &wid) != 1 &&
sscanf(options.param_window, "%lu", &wid) != 1) {
fputs("Cannot convert argument to number.\n", stderr);
return EXIT_FAILURE;
}
return action_window(disp, (Window)wid, mode);
}/*}}}*/
int action_window_str (Display *disp, char mode) {/*{{{*/
Window activate = 0;
Window *client_list;
unsigned long client_list_size;
unsigned int i;
if (strcmp(SELECT_WINDOW_MAGIC, options.param_window) == 0) {
activate = Select_Window(disp);
if (activate) {
return action_window(disp, activate, mode);
}
else {
return EXIT_FAILURE;
}
}
if (strcmp(ACTIVE_WINDOW_MAGIC, options.param_window) == 0) {
activate = get_active_window(disp);
if (activate)
{
return action_window(disp, activate, mode);
}
else
{
return EXIT_FAILURE;
}
}
else {
if ((client_list = get_client_list(disp, &client_list_size)) == NULL) {
return EXIT_FAILURE;
}
for (i = 0; i < client_list_size / sizeof(Window); i++) {
gchar *match_utf8;
if (options.show_class) {
match_utf8 = get_window_class(disp, client_list[i]); /* UTF8 */
}
else {
match_utf8 = get_window_title(disp, client_list[i]); /* UTF8 */
}
if (match_utf8) {
gchar *match;
gchar *match_cf;
gchar *match_utf8_cf = NULL;
if (envir_utf8) {
match = g_strdup(options.param_window);
match_cf = g_utf8_casefold(options.param_window, -1);
}
else {
if (! (match = g_locale_to_utf8(options.param_window, -1, NULL, NULL, NULL))) {
match = g_strdup(options.param_window);
}
match_cf = g_utf8_casefold(match, -1);
}
if (!match || !match_cf) {
continue;
}
match_utf8_cf = g_utf8_casefold(match_utf8, -1);
if ((options.full_window_title_match && strcmp(match_utf8, match) == 0) ||
(!options.full_window_title_match && strstr(match_utf8_cf, match_cf))) {
activate = client_list[i];
g_free(match);
g_free(match_cf);
g_free(match_utf8);
g_free(match_utf8_cf);
break;
}
g_free(match);
g_free(match_cf);
g_free(match_utf8);
g_free(match_utf8_cf);
}
}
g_free(client_list);
if (activate) {
return action_window(disp, activate, mode);
}
else {
return EXIT_FAILURE;
}
}
}/*}}}*/
int list_desktops (Display *disp) {/*{{{*/
unsigned long *num_desktops = NULL;
unsigned long *cur_desktop = NULL;
unsigned long desktop_list_size = 0;
unsigned long *desktop_geometry = NULL;
unsigned long desktop_geometry_size = 0;
gchar **desktop_geometry_str = NULL;
unsigned long *desktop_viewport = NULL;
unsigned long desktop_viewport_size = 0;
gchar **desktop_viewport_str = NULL;
unsigned long *desktop_workarea = NULL;
unsigned long desktop_workarea_size = 0;
gchar **desktop_workarea_str = NULL;
gchar *list = NULL;
unsigned int i;
unsigned int id;
Window root = DefaultRootWindow(disp);
int ret = EXIT_FAILURE;
gchar **names = NULL;
gboolean names_are_utf8 = TRUE;
if (! (num_desktops = (unsigned long *)get_property(disp, root,
XA_CARDINAL, "_NET_NUMBER_OF_DESKTOPS", NULL))) {
if (! (num_desktops = (unsigned long *)get_property(disp, root,
XA_CARDINAL, "_WIN_WORKSPACE_COUNT", NULL))) {
fputs("Cannot get number of desktops properties. "
"(_NET_NUMBER_OF_DESKTOPS or _WIN_WORKSPACE_COUNT)"
"\n", stderr);
goto cleanup;
}
}
if (! (cur_desktop = (unsigned long *)get_property(disp, root,
XA_CARDINAL, "_NET_CURRENT_DESKTOP", NULL))) {
if (! (cur_desktop = (unsigned long *)get_property(disp, root,
XA_CARDINAL, "_WIN_WORKSPACE", NULL))) {
fputs("Cannot get current desktop properties. "
"(_NET_CURRENT_DESKTOP or _WIN_WORKSPACE property)"
"\n", stderr);
goto cleanup;
}
}
if (options.wa_desktop_titles_invalid_utf8 ||
(list = get_property(disp, root,
XInternAtom(disp, "UTF8_STRING", False),
"_NET_DESKTOP_NAMES", &desktop_list_size)) == NULL) {
names_are_utf8 = FALSE;
if ((list = get_property(disp, root,
XA_STRING,
"_WIN_WORKSPACE_NAMES", &desktop_list_size)) == NULL) {
p_verbose("Cannot get desktop names properties. "
"(_NET_DESKTOP_NAMES or _WIN_WORKSPACE_NAMES)"
"\n");
/* ignore the error - list the desktops without names */
}
}
/* common size of all desktops */
if (! (desktop_geometry = (unsigned long *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_NET_DESKTOP_GEOMETRY", &desktop_geometry_size))) {
p_verbose("Cannot get common size of all desktops (_NET_DESKTOP_GEOMETRY).\n");
}
/* desktop viewport */
if (! (desktop_viewport = (unsigned long *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_NET_DESKTOP_VIEWPORT", &desktop_viewport_size))) {
p_verbose("Cannot get common size of all desktops (_NET_DESKTOP_VIEWPORT).\n");
}
/* desktop workarea */
if (! (desktop_workarea = (unsigned long *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_NET_WORKAREA", &desktop_workarea_size))) {
if (! (desktop_workarea = (unsigned long *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_WIN_WORKAREA", &desktop_workarea_size))) {
p_verbose("Cannot get _NET_WORKAREA property.\n");
}
}
/* prepare the array of desktop names */
names = g_malloc0(*num_desktops * sizeof(char *));
if (list) {
id = 0;
names[id++] = list;
for (i = 0; i < desktop_list_size; i++) {
if (list[i] == '\0') {
if (id >= *num_desktops) {
break;
}
names[id++] = list + i + 1;
}
}
}
/* prepare desktop geometry strings */
desktop_geometry_str = g_malloc0((*num_desktops + 1) * sizeof(char *));
if (desktop_geometry && desktop_geometry_size > 0) {
if (desktop_geometry_size == 2 * sizeof(*desktop_geometry)) {
/* only one value - use it for all desktops */
p_verbose("WM provides _NET_DESKTOP_GEOMETRY value common for all desktops.\n");
for (i = 0; i < *num_desktops; i++) {
desktop_geometry_str[i] = g_strdup_printf("%lux%lu",
desktop_geometry[0], desktop_geometry[1]);
}
}
else {
/* seperate values for desktops of different size */
p_verbose("WM provides separate _NET_DESKTOP_GEOMETRY value for each desktop.\n");
for (i = 0; i < *num_desktops; i++) {
if (i < desktop_geometry_size / sizeof(*desktop_geometry) / 2) {
desktop_geometry_str[i] = g_strdup_printf("%lux%lu",
desktop_geometry[i*2], desktop_geometry[i*2+1]);
}
else {
desktop_geometry_str[i] = g_strdup("N/A");
}
}
}
}
else {
for (i = 0; i < *num_desktops; i++) {
desktop_geometry_str[i] = g_strdup("N/A");
}
}
/* prepare desktop viewport strings */
desktop_viewport_str = g_malloc0((*num_desktops + 1) * sizeof(char *));
if (desktop_viewport && desktop_viewport_size > 0) {
if (desktop_viewport_size == 2 * sizeof(*desktop_viewport)) {
/* only one value - use it for current desktop */
p_verbose("WM provides _NET_DESKTOP_VIEWPORT value only for the current desktop.\n");
for (i = 0; i < *num_desktops; i++) {
if (i == *cur_desktop) {
desktop_viewport_str[i] = g_strdup_printf("%lu,%lu",
desktop_viewport[0], desktop_viewport[1]);
}
else {
desktop_viewport_str[i] = g_strdup("N/A");
}
}
}
else {
/* seperate values for each of desktops */
for (i = 0; i < *num_desktops; i++) {
if (i < desktop_viewport_size / sizeof(*desktop_viewport) / 2) {
desktop_viewport_str[i] = g_strdup_printf("%lu,%lu",
desktop_viewport[i*2], desktop_viewport[i*2+1]);
}
else {
desktop_viewport_str[i] = g_strdup("N/A");
}
}
}
}
else {
for (i = 0; i < *num_desktops; i++) {
desktop_viewport_str[i] = g_strdup("N/A");
}
}
/* prepare desktop workarea strings */
desktop_workarea_str = g_malloc0((*num_desktops + 1) * sizeof(char *));
if (desktop_workarea && desktop_workarea_size > 0) {
if (desktop_workarea_size == 4 * sizeof(*desktop_workarea)) {
/* only one value - use it for current desktop */
p_verbose("WM provides _NET_WORKAREA value only for the current desktop.\n");
for (i = 0; i < *num_desktops; i++) {
if (i == *cur_desktop) {
desktop_workarea_str[i] = g_strdup_printf("%lu,%lu %lux%lu",
desktop_workarea[0], desktop_workarea[1],
desktop_workarea[2], desktop_workarea[3]);
}
else {
desktop_workarea_str[i] = g_strdup("N/A");
}
}
}
else {
/* seperate values for each of desktops */
for (i = 0; i < *num_desktops; i++) {
if (i < desktop_workarea_size / sizeof(*desktop_workarea) / 4) {
desktop_workarea_str[i] = g_strdup_printf("%lu,%lu %lux%lu",
desktop_workarea[i*4], desktop_workarea[i*4+1],
desktop_workarea[i*4+2], desktop_workarea[i*4+3]);
}
else {
desktop_workarea_str[i] = g_strdup("N/A");
}
}
}
}
else {
for (i = 0; i < *num_desktops; i++) {
desktop_workarea_str[i] = g_strdup("N/A");
}
}
/* print the list */
for (i = 0; i < *num_desktops; i++) {
gchar *out = get_output_str(names[i], names_are_utf8);
printf("%-2d %c DG: %-*s VP: %-*s WA: %-*s %s\n", i, i == *cur_desktop ? '*' : '-',
longest_str(desktop_geometry_str), desktop_geometry_str[i],
longest_str(desktop_viewport_str), desktop_viewport_str[i],
longest_str(desktop_workarea_str), desktop_workarea_str[i],
out ? out : "N/A");
g_free(out);
}
p_verbose("Total number of desktops: %lu\n", *num_desktops);
p_verbose("Current desktop ID (counted from zero): %lu\n", *cur_desktop);
ret = EXIT_SUCCESS;
goto cleanup;
cleanup:
g_free(names);
g_free(num_desktops);
g_free(cur_desktop);
g_free(desktop_geometry);
g_strfreev(desktop_geometry_str);
g_free(desktop_viewport);
g_strfreev(desktop_viewport_str);
g_free(desktop_workarea);
g_strfreev(desktop_workarea_str);
g_free(list);
return ret;
}/*}}}*/
int longest_str (gchar **strv) {/*{{{*/
unsigned int max = 0;
unsigned int i = 0;
while (strv && strv[i]) {
if (strlen(strv[i]) > max) {
max = strlen(strv[i]);
}
i++;
}
return max;
}/*}}}*/
Window *get_client_list (Display *disp, unsigned long *size) {/*{{{*/
Window *client_list;
if ((client_list = (Window *)get_property(disp, DefaultRootWindow(disp),
XA_WINDOW, "_NET_CLIENT_LIST", size)) == NULL) {
if ((client_list = (Window *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_WIN_CLIENT_LIST", size)) == NULL) {
fputs("Cannot get client list properties. \n"
"(_NET_CLIENT_LIST or _WIN_CLIENT_LIST)"
"\n", stderr);
return NULL;
}
}
return client_list;
}/*}}}*/
int list_windows (Display *disp) {/*{{{*/
Window *client_list;
unsigned long client_list_size;
unsigned int i;
int max_client_machine_len = 0;
if ((client_list = get_client_list(disp, &client_list_size)) == NULL) {
return EXIT_FAILURE;
}
/* find the longest client_machine name */
for (i = 0; i < client_list_size / sizeof(Window); i++) {
gchar *client_machine;
if ((client_machine = get_property(disp, client_list[i],
XA_STRING, "WM_CLIENT_MACHINE", NULL))) {
max_client_machine_len = strlen(client_machine);
}
g_free(client_machine);
}
/* print the list */
for (i = 0; i < client_list_size / sizeof(Window); i++) {
gchar *title_utf8 = get_window_title(disp, client_list[i]); /* UTF8 */
gchar *title_out = get_output_str(title_utf8, TRUE);
gchar *client_machine;
gchar *class_out = get_window_class(disp, client_list[i]); /* UTF8 */
unsigned long *pid;
unsigned long *desktop;
int x, y, junkx, junky;
unsigned int wwidth, wheight, bw, depth;
Window junkroot;
/* desktop ID */
if ((desktop = (unsigned long *)get_property(disp, client_list[i],
XA_CARDINAL, "_NET_WM_DESKTOP", NULL)) == NULL) {
desktop = (unsigned long *)get_property(disp, client_list[i],
XA_CARDINAL, "_WIN_WORKSPACE", NULL);