-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.c
1643 lines (1451 loc) · 54.5 KB
/
main.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 {{{ */
/*
wmctrl
A command line tool to interact with an EWMH/NetWM compatible X Window Manager.
Author, current maintainer: Tomas Styblo <[email protected]>
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 <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>
#include <X11/Xmu/WinUtil.h>
#include <glib.h>
#define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
#define _NET_WM_STATE_ADD 1 /* add/set property */
#define _NET_WM_STATE_TOGGLE 2 /* toggle property */
/* help {{{ */
#define HELP "wmctrl " VERSION "\n" \
"Usage: wmctrl [OPTION]...\n" \
"Actions:\n" \
" -m Show information about the window manager and\n" \
" about the environment.\n" \
" -l List windows managed by the window manager.\n" \
" -d List desktops. The current desktop is marked\n" \
" with an asterisk.\n" \
" -j List current desktop.\n" \
" -s <DESK> Switch to the specified desktop.\n" \
" -a <WIN> Activate the window by switching to its desktop and\n" \
" raising it.\n" \
" -c <WIN> Close the window gracefully.\n" \
" -R <WIN> Move the window to the current desktop and\n" \
" activate it.\n" \
" -Y <WIN> Iconify (minimize) the window.\n" \
" -r <WIN> -t <DESK> Move the window to the specified desktop.\n" \
" -r <WIN> -e <MVARG> Resize and move the window around the desktop.\n" \
" The format of the <MVARG> argument is described below.\n" \
" -r <WIN> -y <MVARG> Resize and move like above, then reactivate.\n" \
" -r <WIN> -b <STARG> Change the state of the window. Using this option it's\n" \
" possible for example to make the window maximized,\n" \
" shaded or fullscreen. The format of the <STARG>\n" \
" argument and list of possible states is given below.\n" \
" -r <WIN> -N <STR> Set the name (long title) of the window.\n" \
" -r <WIN> -I <STR> Set the icon name (short title) of the window.\n" \
" -r <WIN> -T <STR> Set both the name and the icon name of the window.\n" \
" -k (on|off|toggle) Activate or deactivate window manager's\n" \
" \"showing the desktop\" mode. Many window managers\n" \
" do not implement this mode.\n" \
" -o <X>,<Y> Change the viewport for the current desktop.\n" \
" The X and Y values are separated with a comma.\n" \
" They define the top left corner of the viewport.\n" \
" The window manager may ignore the request.\n" \
" -n <NUM> Change number of desktops.\n" \
" The window manager may ignore the request.\n" \
" -g <W>,<H> Change geometry (common size) of all desktops.\n" \
" The window manager may ignore the request.\n" \
" -h Print help.\n" \
"\n" \
"Options:\n" \
" -S List windows in stacking order (bottom to top).\n" \
" -i Interpret <WIN> as a numerical window ID.\n" \
" -p Include PIDs in the window list. Very few\n" \
" X applications support this feature.\n" \
" -G Include geometry in the window list.\n" \
" -x Include WM_CLASS in the window list or\n" \
" interpret <WIN> as the WM_CLASS name.\n" \
" -u Override auto-detection and force UTF-8 mode.\n" \
" -F Modifies the behavior of the window title matching\n" \
" algorithm. It will match only the full window title\n" \
" instead of a substring, when this option is used.\n" \
" Furthermore it makes the matching case sensitive.\n" \
" -v Be verbose. Useful for debugging.\n" \
" -w <WA> Use a workaround. The option may appear multiple\n" \
" times. List of available workarounds is given below.\n" \
"\n" \
"Arguments:\n" \
" <WIN> This argument specifies the window. By default it's\n" \
" interpreted as a string. The string is matched\n" \
" against the window titles and the first matching\n" \
" window is used. The matching isn't case sensitive\n" \
" and the string may appear in any position\n" \
" of the title.\n" \
"\n" \
" The -i option may be used to interpret the argument\n" \
" as a numerical window ID represented as a decimal\n" \
" number. If it starts with \"0x\", then\n" \
" it will be interpreted as a hexadecimal number.\n" \
"\n" \
" The -x option may be used to interpret the argument\n" \
" as a string, which is matched against the window's\n" \
" class name (WM_CLASS property). The first matching\n" \
" window is used. The matching isn't case sensitive\n" \
" and the string may appear in any position\n" \
" of the class name. So it's recommended to always use\n" \
" the -F option in conjunction with the -x option.\n" \
"\n" \
" The special string \":SELECT:\" (without the quotes)\n" \
" may be used to instruct wmctrl to let you select the\n" \
" window by clicking on it.\n" \
"\n" \
" The special string \":ACTIVE:\" (without the quotes)\n" \
" may be used to instruct wmctrl to use the currently\n" \
" active window for the action.\n" \
"\n" \
" <DESK> A desktop number. Desktops are counted from zero.\n" \
"\n" \
" <MVARG> Specifies a change to the position and size\n" \
" of the window. The format of the argument is:\n" \
"\n" \
" <G>,<X>,<Y>,<W>,<H>\n" \
"\n" \
" <G>: Gravity specified as a number. The numbers are\n" \
" defined in the EWMH specification. The value of\n" \
" zero is particularly useful, it means \"use the\n" \
" default gravity of the window\".\n" \
" <X>,<Y>: Coordinates of new position of the window.\n" \
" <W>,<H>: New width and height of the window.\n" \
"\n" \
" The value of -1 may appear in place of\n" \
" any of the <X>, <Y>, <W> and <H> properties\n" \
" to left the property unchanged.\n" \
"\n" \
" <STARG> Specifies a change to the state of the window\n" \
" by the means of _NET_WM_STATE request.\n" \
" This option allows two properties to be changed\n" \
" simultaneously, specifically to allow both\n" \
" horizontal and vertical maximization to be\n" \
" altered together.\n" \
"\n" \
" The format of the argument is:\n" \
"\n" \
" (remove|add|toggle),<PROP1>[,<PROP2>]\n" \
"\n" \
" The EWMH specification defines the\n" \
" following properties:\n" \
"\n" \
" modal, sticky, maximized_vert, maximized_horz,\n" \
" shaded, skip_taskbar, skip_pager, hidden,\n" \
" fullscreen, above, below\n" \
"\n" \
"Workarounds:\n" \
"\n" \
" DESKTOP_TITLES_INVALID_UTF8 Print non-ASCII desktop titles correctly\n" \
" when using Window Maker.\n" \
"\n" \
"The format of the window list:\n" \
"\n" \
" <window ID> <desktop ID> <client machine> <window title>\n" \
"\n" \
"The format of the desktop list:\n" \
"\n" \
" <desktop ID> [-*] <geometry> <viewport> <workarea> <title>\n" \
"\n" \
"\n" \
"Author, current maintainer: Tomas Styblo <[email protected]>\n" \
"Released under the GNU General Public License.\n" \
"Copyright (C) 2003\n"
/* }}} */
#define MAX_PROPERTY_VALUE_LEN 4096
#define SELECT_WINDOW_MAGIC ":SELECT:"
#define ACTIVE_WINDOW_MAGIC ":ACTIVE:"
#define p_verbose(...) if (options.verbose) { \
fprintf(stderr, __VA_ARGS__); \
}
/* declarations of static functions *//*{{{*/
static gboolean wm_supports (Display *disp, const gchar *prop);
static Window *get_client_list (Display *disp, unsigned long *size);
static int client_msg(Display *disp, Window win, char *msg,
unsigned long data0, unsigned long data1,
unsigned long data2, unsigned long data3,
unsigned long data4);
static int list_windows (Display *disp);
static int list_current_desktop (Display *disp);
static int list_desktops (Display *disp);
static int showing_desktop (Display *disp);
static int change_viewport (Display *disp);
static int change_geometry (Display *disp);
static int change_number_of_desktops (Display *disp);
static int switch_desktop (Display *disp);
static int wm_info (Display *disp);
static gchar *get_output_str (gchar *str, gboolean is_utf8);
static int action_window (Display *disp, Window win, char mode);
static int action_window_pid (Display *disp, char mode);
static int action_window_str (Display *disp, char mode);
static int activate_window (Display *disp, Window win,
gboolean switch_desktop);
static int close_window (Display *disp, Window win);
static int longest_str (gchar **strv);
static int window_to_desktop (Display *disp, Window win, int desktop);
static void window_set_title (Display *disp, Window win, char *str, char mode);
static gchar *get_window_title (Display *disp, Window win);
static gchar *get_window_class (Display *disp, Window win);
static gchar *get_property (Display *disp, Window win,
Atom xa_prop_type, gchar *prop_name, unsigned long *size);
static void init_charset(void);
static int window_move_resize (Display *disp, Window win, char *arg);
static int window_state (Display *disp, Window win, char *arg);
static Window Select_Window(Display *dpy);
static Window get_active_window(Display *dpy);
/*}}}*/
static struct {
int verbose;
int force_utf8;
int show_class;
int show_pid;
int show_geometry;
int stacking_order;
int match_by_id;
int match_by_cls;
int full_window_title_match;
int wa_desktop_titles_invalid_utf8;
char *param_window;
char *param;
} options;
static gboolean envir_utf8;
int main (int argc, char **argv) { /* {{{ */
int opt;
int action = 0;
int ret = EXIT_SUCCESS;
int missing_option = 1;
Display *disp;
memset(&options, 0, sizeof(options)); /* just for sure */
/* necessary to make g_get_charset() and g_locale_*() work */
setlocale(LC_ALL, "");
/* make "--help" and "--version" work. I don't want to use
* getopt_long for portability reasons */
if (argc == 2 && argv[1]) {
if (strcmp(argv[1], "--help") == 0) {
fputs(HELP, stdout);
return EXIT_SUCCESS;
}
else if (strcmp(argv[1], "--version") == 0) {
puts(VERSION);
return EXIT_SUCCESS;
}
}
while ((opt = getopt(argc, argv, "FGVvhSlupidjmxa:r:s:c:t:w:k:o:n:g:e:y:b:z:E:N:I:T:R:Y:")) != -1) {
missing_option = 0;
switch (opt) {
case 'F':
options.full_window_title_match = 1;
break;
case 'S':
options.stacking_order = 1;
break;
case 'G':
options.show_geometry = 1;
break;
case 'i':
options.match_by_id = 1;
break;
case 'v':
options.verbose = 1;
break;
case 'u':
options.force_utf8 = 1;
break;
case 'x':
options.match_by_cls = 1;
options.show_class = 1;
break;
case 'p':
options.show_pid = 1;
break;
case 'a': case 'c': case 'R': case 'z': case 'Y': case 'E':
options.param_window = optarg;
action = opt;
break;
case 'r':
options.param_window = optarg;
break;
case 't': case 'e': case 'b': case 'N': case 'I': case 'T': case 'y':
options.param = optarg;
action = opt;
break;
case 's':
options.param = optarg;
action = opt;
break;
case 'w':
if (strcmp(optarg, "DESKTOP_TITLES_INVALID_UTF8") == 0) {
options.wa_desktop_titles_invalid_utf8 = 1;
}
else {
fprintf(stderr, "Unknown workaround: %s\n", optarg);
return EXIT_FAILURE;
}
break;
case 'k': case 'o': case 'n': case 'g':
options.param = optarg;
action = opt;
break;
case '?':
return EXIT_FAILURE;
default:
action = opt;
}
}
if (missing_option) {
fputs(HELP, stderr);
return EXIT_FAILURE;
}
init_charset();
if (! (disp = XOpenDisplay(NULL))) {
fputs("Cannot open display.\n", stderr);
return EXIT_FAILURE;
}
switch (action) {
case 'V':
puts(VERSION);
break;
case 'h':
fputs(HELP, stdout);
break;
case 'l':
ret = list_windows(disp);
break;
case 'j':
ret = list_current_desktop(disp);
break;
case 'd':
ret = list_desktops(disp);
break;
case 's':
ret = switch_desktop(disp);
break;
case 'm':
ret = wm_info(disp);
break;
case 'a': case 'c': case 'R': case 'z': case 'E':
case 't': case 'e': case 'b': case 'N': case 'I': case 'T': case 'y':
case 'Y':
if (! options.param_window) {
fputs("No window was specified.\n", stderr);
return EXIT_FAILURE;
}
if (options.match_by_id) {
ret = action_window_pid(disp, action);
}
else {
ret = action_window_str(disp, action);
}
break;
case 'k':
ret = showing_desktop(disp);
break;
case 'o':
ret = change_viewport(disp);
break;
case 'n':
ret = change_number_of_desktops(disp);
break;
case 'g':
ret = change_geometry(disp);
break;
}
XCloseDisplay(disp);
return ret;
}
/* }}} */
static 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);
}/*}}}*/
static 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;
}
}/*}}}*/
static 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;
}/*}}}*/
static 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;
}/*}}}*/
static int showing_desktop (Display *disp) {/*{{{*/
unsigned long state;
unsigned long *showing_desktop = NULL;
/* _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");
}
if (strcmp(options.param, "on") == 0) {
state = 1;
}
else if (strcmp(options.param, "off") == 0) {
state = 0;
}
else if (strcmp(options.param, "toggle") == 0) {
if(*showing_desktop == 0) { state = 1; }
if(*showing_desktop == 1) { state = 0; }
}
else {
fputs("The argument to the -k option must be either \"on\" or \"off\" or \"toggle\"\n", stderr);
return EXIT_FAILURE;
}
return client_msg(disp, DefaultRootWindow(disp), "_NET_SHOWING_DESKTOP",
state, 0, 0, 0, 0);
}/*}}}*/
static 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;
}
}/*}}}*/
static 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;
}
}/*}}}*/
static 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);
}/*}}}*/
static 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);
}/*}}}*/
static 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,
(guchar *) 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,
(guchar *) 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,
(guchar *) 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,
(guchar *) title_utf8, strlen(title_utf8));
}
g_free(title_utf8);
g_free(title_local);
}/*}}}*/
static 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);
}/*}}}*/
static 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");
}
}
if (switch_desktop && desktop) {
if (client_msg(disp, DefaultRootWindow(disp),
"_NET_CURRENT_DESKTOP",
*desktop, 0, 0, 0, 0) != EXIT_SUCCESS) {
p_verbose("Cannot switch desktop.\n");
}
g_free(desktop);
}
client_msg(disp, win, "_NET_ACTIVE_WINDOW",
0, 0, 0, 0, 0);
XMapRaised(disp, win);
return EXIT_SUCCESS;
}/*}}}*/
static int iconify_window (Display *disp, Window win) {/* {{{ */
return !XIconifyWindow(disp, win, DefaultScreen(disp));
}/*}}}*/
static int close_window (Display *disp, Window win) {/*{{{*/
return client_msg(disp, win, "_NET_CLOSE_WINDOW",
0, 0, 0, 0, 0);
}/*}}}*/
static gchar * normalize_wm_state_name(const char * name)
{
char * short_names[] = {
"modal", "sticky", "maximized_vert", "maximized_horz",
"shaded", "skip_taskbar", "skip_pager", "hidden",
"fullscreen", "above", "below", 0};
int i;
for (i = 0; short_names[i]; i++)
{
if (strcmp(short_names[i], name) == 0)
{
gchar * upcase = g_ascii_strup(name, -1);
gchar * result = g_strdup_printf("_NET_WM_STATE_%s", upcase);
g_free(upcase);
return result;
}
}
if (strcmp("undecorated", name) == 0)
{
return g_strdup("_OB_WM_STATE_UNDECORATED");
}
return g_strdup(name);
}
static 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;
*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;
*p2 = '\0';
p2++;
if (strlen(p2) == 0) {
fputs("Invalid zero length property.\n", stderr);
return EXIT_FAILURE;
}
tmp_prop2 = normalize_wm_state_name(p2);
p_verbose("State 2: %s\n", tmp_prop2);
prop2 = XInternAtom(disp, tmp_prop2, False);
g_free(tmp_prop2);
}
/* the first property */
if (strlen(p1) == 0) {
fputs("Invalid zero length property.\n", stderr);
return EXIT_FAILURE;
}
tmp_prop1 = normalize_wm_state_name(p1);
p_verbose("State 1: %s\n", tmp_prop1);
prop1 = XInternAtom(disp, tmp_prop1, False);
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;
}
}/*}}}*/
static gboolean wm_supports (Display *disp, const gchar *prop) {/*{{{*/
Atom xa_prop = XInternAtom(disp, prop, False);
Atom *list;
unsigned long size;
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;
}/*}}}*/
static 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;
}
}/*}}}*/
static int window_say_title (Display *disp, Window win) {
gchar *title_utf8 = get_window_title(disp, win);
printf("%s\n", title_utf8);
g_free(title_utf8);
return EXIT_SUCCESS;
}
static int action_window (Display *disp, Window win, char mode) {/*{{{*/
int rv;
p_verbose("Using window: 0x%.8lx\n", win);
switch (mode) {
case 'a':
return activate_window(disp, win, TRUE);
case 'Y':
return iconify_window(disp, win);
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 'y':
/* resize/move the window, then activate it */
rv = window_move_resize(disp, win, options.param);
activate_window(disp, win, TRUE);
return rv;
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;
case 'z':
// iconify
return XLowerWindow(disp, win);
case 'E':
return window_say_title(disp, win);
default:
fprintf(stderr, "Unknown action: '%c'\n", mode);
return EXIT_FAILURE;
}
}/*}}}*/
static 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);