forked from kohsuke/rubywm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wmlib.c
1027 lines (799 loc) · 24.4 KB
/
wmlib.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
/*
Window Management Library for Ruby
SegPhault (Ryan Paul) - 02/13/05
NOTES
------
I used the source code of Tomas Styblo's wmctrl utility as a starting point
to help me figure out some of the X stuff. You can find his excellent command
line utility here: http://www.sweb.cz/tripie/utils/wmctrl/
XGrabKey Resources:
* http://mail.gnome.org/archives/gtk-app-devel-list/2005-August/msg00280.html
* http://sunsolve.sun.com/search/document.do?assetkey=1-9-15043-1
LICENSE
-------
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 <string.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>
#include <X11/Xmu/WinUtil.h>
#include <glib.h>
#include "ruby.h"
// Window management stuff
#define wm_root DefaultRootWindow(disp)
#define open_disp Display *disp = XOpenDisplay(NULL)
#define close_disp XCloseDisplay(disp);
#define WM_TOP 0
#define WM_BOTTOM 1
#define WM_LEFT 2
#define WM_RIGHT 3
#define WM_WIDTH 4
#define WM_HEIGHT 5
char* xwm_get_win_title(Display *disp, Window win);
VALUE new_window(Window win);
VALUE window_desktop(VALUE self);
VALUE desktop_index(VALUE self);
static int xwm_long_msg(Display *disp, Window win, char *msg,
long d0, long d1, long d2, long d3, long d4)
{
XEvent e;
long mask = SubstructureRedirectMask | SubstructureNotifyMask;
e.xclient.type = ClientMessage;
e.xclient.serial = 0;
e.xclient.send_event = True;
e.xclient.message_type = XInternAtom(disp, msg, False);
e.xclient.window = win;
e.xclient.format = 32;
e.xclient.data.l[0] = d0;
e.xclient.data.l[1] = d1;
e.xclient.data.l[2] = d2;
e.xclient.data.l[3] = d3;
e.xclient.data.l[4] = d4;
return XSendEvent(disp, DefaultRootWindow(disp),
False, mask, &e) ? EXIT_SUCCESS : EXIT_FAILURE;
}
static int xwm_msg(Display *disp, Window win, char *msg, long d0)
{
return xwm_long_msg(disp,win,msg,d0,0,0,0,0);
}
static gchar *get_property(Display *disp, Window win,
Atom xa_prop_type, gchar *prop_name, unsigned long *size)
{
unsigned long ret_nitems, ret_bytes_after, tmp_size;
Atom xa_prop_name, xa_ret_type;
unsigned char *ret_prop;
int ret_format;
gchar *ret;
int size_in_byte;
xa_prop_name = XInternAtom(disp, prop_name, False);
if (XGetWindowProperty(disp, win, xa_prop_name, 0, 4096 / 4, False,
xa_prop_type, &xa_ret_type, &ret_format, &ret_nitems,
&ret_bytes_after, &ret_prop) != Success)
return NULL;
if (xa_ret_type != xa_prop_type)
{
XFree(ret_prop);
return NULL;
}
switch(ret_format) {
case 8: size_in_byte = sizeof(char); break;
case 16: size_in_byte = sizeof(short); break;
case 32: size_in_byte = sizeof(long); break;
}
tmp_size = size_in_byte * ret_nitems;
ret = g_malloc(tmp_size + 1);
memcpy(ret, ret_prop, tmp_size);
ret[tmp_size] = '\0';
if (size) *size = tmp_size;
XFree(ret_prop);
return ret;
}
int xwm_has_state(Display* disp, Window win, gchar* state_name)
{
long size=0;
Atom state = XInternAtom(disp,state_name,False);
Atom* list = (Atom*)get_property(disp,win,XA_CARDINAL,"_NET_WM_STATE",&size);
int r = 0;
int i;
for (i=0; i<size/sizeof(Atom); i++) {
if (list[i]==state)
r = 1;
}
g_free(list);
return r;
}
int xwm_get_win_desk(Display *disp, Window win)
{
long *wdesk = (long*)get_property(disp,win, XA_CARDINAL,"_NET_WM_DESKTOP", NULL);
long *nwdesk = (long*)get_property(disp, win, XA_CARDINAL,"_WIN_WORKSPACE", NULL);
return nwdesk ? (int)*nwdesk : (wdesk ? (int)*wdesk : -1);
}
int xwm_get_win_pid(Display *disp, Window win)
{
long *pid = (long*)get_property(disp,win, XA_CARDINAL,"_NET_WM_PID", NULL);
if (pid==NULL) return -1;
return *pid;
}
void xwm_set_win_desk(Display *disp, Window win, int d)
{
xwm_msg(disp, win, "_NET_WM_DESKTOP", d);
}
char *xwm_get_win_class(Display *disp, Window win)
{
unsigned long size;
return get_property(disp,win,XA_STRING, "WM_CLASS", &size);
}
char *xwm_get_win_title(Display *disp, Window win)
{
char *wname = (char*)get_property(disp,win, XA_STRING, "WM_NAME", NULL);
char *nwname = (char*)get_property(disp,win, XInternAtom(disp,
"UTF8_STRING", False), "_NET_WM_NAME", NULL);
return nwname ? nwname : (wname ? wname : NULL);
}
Window xwm_get_win_active(Display *disp)
{
char *prop;
unsigned long s;
prop = get_property(disp, wm_root, XA_WINDOW,
"_NET_ACTIVE_WINDOW", &s);
return prop ? *((Window*)prop) : ((Window)0);
}
Window *xwm_window_list(Display *disp, gchar* prop1, gchar* prop2, long *size)
{
Window *nlst = (Window*)get_property(disp, wm_root, XA_WINDOW,
prop1 /*"_NET_CLIENT_LIST"*/, size);
Window *lst = (Window *)get_property(disp, wm_root, XA_CARDINAL,
prop2 /*"_WIN_CLIENT_LIST"*/, size);
return nlst ? nlst : (lst ? lst : NULL);
}
Window* xwm_window_list_byage(Display* disp, long* size) {
return xwm_window_list(disp,"_NET_CLIENT_LIST","_WIN_CLIENT_LIST",size);
}
Window* xwm_window_list_bystack(Display* disp, long* size) {
return xwm_window_list(disp,"_NET_CLIENT_LIST_STACKING","_WIN_CLIENT_LIST_STACKING",size);
}
void xwm_desk_activate(Display *disp, int desk)
{
xwm_msg(disp, wm_root, "_NET_CURRENT_DESKTOP", desk);
}
int xwm_desk_count(Display *disp)
{
long *ndcount = (long*)get_property(disp, wm_root, XA_CARDINAL,
"_NET_NUMBER_OF_DESKTOPS", NULL);
long *dcount = (long*)get_property(disp, wm_root, XA_CARDINAL,
"_WIN_WORKSPACE_COUNT", NULL);
return ndcount ? (int)*ndcount : (dcount ? (int)*dcount : -1);
}
void xwm_win_close(Display *disp, Window win)
{
xwm_msg(disp, win, "_NET_CLOSE_WINDOW",0);
}
void xwm_win_activate(Display *disp, Window win)
{
int d = (int)xwm_get_win_desk(disp, win);
if (d != -1) xwm_desk_activate(disp, d);
xwm_msg(disp, win, "_NET_ACTIVE_WINDOW",0);
}
int xwm_get_win_size(Display *disp, Window win, int stype)
{
XWindowAttributes a; int rx,ry; Window junkwin;
if (!XGetWindowAttributes(disp, win, &a))
return -1;
int bw = a.border_width;
// translate the top-left boundary of this windo into the coordinates of the root widnow
XTranslateCoordinates (disp, win, a.root,
-bw,-bw,
&rx, &ry, &junkwin);
if (stype == WM_TOP) return ry;
if (stype == WM_BOTTOM) return ry + a.height+bw*2;
if (stype == WM_LEFT) return rx;
if (stype == WM_RIGHT) return rx + a.width+bw*2;
if (stype == WM_WIDTH) return a.width;
if (stype == WM_HEIGHT) return a.height;
}
static gboolean xwm_supports(Display *disp, const char *prop)
{
unsigned long size;
Atom p = XInternAtom(disp, prop, False);
Atom *list;
int i;
if (! (list = (Atom *)get_property(disp, wm_root, XA_ATOM,
"_NET_SUPPORTED", &size)))
return FALSE;
for (i = 0; i < size / sizeof(Atom); i++)
if (list[i] == p)
return TRUE;
}
void xwm_win_move(Display *disp, Window win, int x, int y)
{
XMoveWindow(disp, win, x, y);
}
void xwm_win_resize(Display *disp, Window win, int w, int h)
{
XResizeWindow(disp, win, w, h);
}
void xwm_win_remove_decor(Display *disp, Window win)
{
xwm_msg(disp, win, "_OB_WM_STATE_UNDECORATED", 0);
}
// --- Ruby Stuff ---
#define rb_cmdef rb_define_singleton_method
#define rb_imdef rb_define_method
#define rb_wmclass(n) rb_define_class_under(mWM, n, rb_cObject)
#define RSTR rb_str_new2
#define ISA rb_obj_is_instance_of
#define rb_struct(n) n *data; Data_Get_Struct(self, n, data)
#define rbwm_init(n) open_disp; rb_struct(n)
#define WM_FILTER_CLASS 1
#define WM_FILTER_TITLE 2
#define same_win(w1,w2) RTEST(window_op_eq(w1,w2))
#define same_desk(w1,w2) xwm_get_win_desk(disp,w1) == xwm_get_win_desk(disp,w2)
VALUE Obj_filter(VALUE self)
{
open_disp;
VALUE lst = rb_ary_new();
VALUE iter(VALUE x)
{
if (RTEST(rb_yield(x))) rb_ary_push(lst,x);
return Qnil;
}
rb_iterate(rb_each, self, iter, 0);
return lst;
}
VALUE Obj_find(VALUE self)
{
open_disp;
VALUE wnd = Qnil;
VALUE iter(VALUE x)
{
if (RTEST(rb_yield(x)))
{
wnd = x;
rb_iter_break();
}
return Qnil;
}
rb_iterate(rb_each, self, iter, 0);
return wnd;
}
VALUE mWM;
VALUE cWindow;
VALUE cDesktop;
typedef struct desktop
{
int index;
} DESKTOP;
typedef struct window
{
Window win;
} WINDOW;
VALUE Desktop_new(VALUE self, VALUE ind)
{
DESKTOP *data;
VALUE d = Data_Make_Struct(self, DESKTOP, 0, free, data);
data->index = FIX2INT(ind);
rb_obj_call_init(d, 0, 0);
return d;
}
VALUE new_desktop(int ind)
{
return Desktop_new(cDesktop, INT2FIX(ind));
}
VALUE Desktop_current(VALUE self)
{
open_disp;
Window w = xwm_get_win_active(disp);
close_disp;
return window_desktop(new_window(w));
}
VALUE Desktop_set_current(VALUE self, VALUE d)
{
open_disp;
if (TYPE(d) == T_FIXNUM)
xwm_desk_activate(disp, FIX2INT(d));
if (ISA(d, cDesktop))
xwm_desk_activate(disp, FIX2INT(desktop_index(d)));
close_disp;
return self;
}
VALUE desktop_op_add(VALUE self, VALUE n)
{
open_disp;
int dval = -1;
if (TYPE(n) == T_FIXNUM)
dval = FIX2INT(desktop_index(self)) + FIX2INT(n);
if (ISA(n, cDesktop))
dval = FIX2INT(desktop_index(self)) + FIX2INT(desktop_index(n));
close_disp;
return new_desktop(dval != -1 ? dval : 0);
}
VALUE desktop_op_sub(VALUE self, VALUE n)
{
open_disp;
int dval = -1;
if (TYPE(n) == T_FIXNUM)
dval = FIX2INT(desktop_index(self)) - FIX2INT(n);
if (ISA(n, cDesktop))
dval = FIX2INT(desktop_index(self)) - FIX2INT(desktop_index(n));
close_disp;
return new_desktop(dval != -1 ? dval : 0);
}
VALUE desktop_op_eq(VALUE self, VALUE d)
{
open_disp;
if (ISA(d,cDesktop))
return rb_equal(desktop_index(self), desktop_index(d));
if (TYPE(d) == T_FIXNUM)
return rb_equal(desktop_index(self), d);
close_disp;
return Qfalse;
}
VALUE Desktop_last(VALUE self)
{
open_disp;
int cnt = xwm_desk_count(disp);
close_disp;
return new_desktop(cnt - 1);
}
VALUE Desktop_first(VALUE self)
{
return new_desktop(0);
}
VALUE Desktop_each(VALUE self)
{
open_disp;
int cnt = xwm_desk_count(disp);
int i;
for (i = 0; i <= cnt; i++)
rb_yield(new_desktop(i));
close_disp;
return self;
}
VALUE Desktop_count(VALUE self)
{
open_disp;
int cnt = xwm_desk_count(disp);
close_disp;
return INT2FIX(cnt);
}
VALUE Desktop_root_window(VALUE self)
{
open_disp;
VALUE v = new_window(DefaultRootWindow(disp));
close_disp;
return v;
}
VALUE desktop_index(VALUE self)
{
rb_struct(DESKTOP);
return INT2FIX(data->index);
}
VALUE desktop_to_s(VALUE self)
{
rb_struct(DESKTOP);
char *txt;
sprintf(txt, "%d", data->index);
return RSTR(txt);
}
VALUE desktop_each(VALUE self)
{
rbwm_init(DESKTOP);
long cnt;
int i;
Window *lst = xwm_window_list_byage(disp, &cnt);
for (i = 0; i < cnt / sizeof(Window); i++)
if (xwm_get_win_desk(disp, lst[i]) == data->index)
rb_yield(new_window(lst[i]));
close_disp;
return Qnil;
}
VALUE Window_new(VALUE self)
{
WINDOW *data;
VALUE w = Data_Make_Struct(self, WINDOW, 0, free, data);
rb_obj_call_init(w, 0, 0);
return w;
}
VALUE new_window(Window win)
{
VALUE w = Window_new(cWindow);
WINDOW *data;
Data_Get_Struct(w, WINDOW, data);
data->win = win;
return w;
}
VALUE Window_current(VALUE self)
{
open_disp;
Window w = xwm_get_win_active(disp);
close_disp;
return w ? new_window(w) : Qnil;
}
VALUE Window_on_press(VALUE self, VALUE keys)
{
open_disp;
KeyCode mykey = 0;
KeySym mysym;
Window w = xwm_get_win_active(disp);
mysym = XStringToKeysym(STR2CSTR(keys));
mykey = XKeysymToKeycode(disp, mysym);
XGrabKey(disp, mykey, AnyModifier, w, True, GrabModeAsync, GrabModeAsync);
close_disp;
return Qnil;
}
VALUE Window_key_loop(VALUE self)
{
open_disp;
XEvent ev;
KeySym grabbed_key;
for (;;)
{
XNextEvent(disp, &ev);
switch (ev.type)
{
case KeyPress:
grabbed_key = XKeycodeToKeysym(disp, ev.xkey.keycode, 0);
printf("Test: %s", grabbed_key);
default:
break;
}
}
return Qnil;
}
VALUE Window_each(VALUE self)
{
open_disp;
long cnt;
int i;
Window *lst = xwm_window_list_bystack(disp, &cnt);
// printf("Found %d\n",cnt/sizeof(Window));
for (i = 0; i < cnt / sizeof(Window); i++)
rb_yield(new_window(lst[i]));
close_disp;
return Qnil;
}
VALUE window_root(VALUE self)
{
rbwm_init(WINDOW);
Window root,parent;
int i; Window* children;
XQueryTree(disp,data->win,&root,&parent,&children,&i);
if (children!=NULL) XFree(children);
close_disp;
return new_window(root);
}
VALUE Window_eachChild(VALUE self)
{
rbwm_init(WINDOW);
Window root,parent;
int i;
int nChild=0;
Window* children=NULL;
XQueryTree(disp,data->win,&root,&parent,&children,&nChild);
for (i=0; i<nChild; i++)
rb_yield(new_window(children[i]));
if (children!=NULL)
XFree(children);
close_disp;
return Qnil;
}
VALUE window_title(VALUE self)
{
rbwm_init(WINDOW);
char *t = xwm_get_win_title(disp, data->win);
close_disp;
return t ? RSTR(t) : Qnil;
}
VALUE window_class(VALUE self)
{
rbwm_init(WINDOW);
char *t = xwm_get_win_class(disp, data->win);
close_disp;
return t ? RSTR(t) : Qnil;
}
VALUE windim(VALUE self, int dt)
{
rbwm_init(WINDOW);
int s = xwm_get_win_size(disp, data->win, dt);
close_disp;
return INT2FIX(s);
}
VALUE window_top(VALUE self) { return windim(self, WM_TOP); }
VALUE window_bottom(VALUE self) { return windim(self, WM_BOTTOM); }
VALUE window_left(VALUE self) { return windim(self, WM_LEFT); }
VALUE window_right(VALUE self) { return windim(self, WM_RIGHT); }
VALUE window_width(VALUE self) { return windim(self, WM_WIDTH); }
VALUE window_height(VALUE self) { return windim(self, WM_HEIGHT); }
/*
* Is the window visible?
*
* This excludes minimized windows. Not sure exactly what else would fit
* in this category.
*/
VALUE window_visible(VALUE self)
{
XWindowAttributes a;
rbwm_init(WINDOW);
if (!XGetWindowAttributes(disp, data->win, &a))
return -1;
close_disp;
return a.map_state==IsViewable ? Qtrue : Qfalse;
}
VALUE window_id(VALUE self)
{
rbwm_init(WINDOW);
VALUE v = LONG2FIX(data->win);
close_disp;
return v;
}
VALUE window_set_desktop(VALUE self, VALUE dsk)
{
rbwm_init(WINDOW);
xwm_set_win_desk(disp, data->win, FIX2INT(dsk));
close_disp;
return Qnil;
}
VALUE window_set_left(VALUE self, VALUE x)
{
rbwm_init(WINDOW);
xwm_win_move(disp, data->win, FIX2INT(x), FIX2INT(window_top(self)));
close_disp;
return Qnil;
}
VALUE window_set_top(VALUE self, VALUE x)
{
rbwm_init(WINDOW);
xwm_win_move(disp, data->win, FIX2INT(window_left(self)), FIX2INT(x));
close_disp;
return Qnil;
}
VALUE window_set_width(VALUE self, VALUE x)
{
rbwm_init(WINDOW);
xwm_win_resize(disp, data->win, FIX2INT(x), FIX2INT(window_height(self)));
close_disp;
return Qnil;
}
VALUE window_set_height(VALUE self, VALUE x)
{
rbwm_init(WINDOW);
xwm_win_resize(disp, data->win, FIX2INT(window_width(self)), FIX2INT(x));
close_disp;
return Qnil;
}
VALUE window_op_eq(VALUE self, VALUE other)
{
// Match window title
if (ISA(other,cWindow))
return rb_equal(window_id(self), window_id(other));
if (TYPE(other) == T_REGEXP)
return rb_reg_match(other, window_title(self));
if (TYPE(other) == T_STRING)
return rb_equal(window_title(self), other);
if (TYPE(other) == T_FIXNUM)
return rb_equal(desktop_index(window_desktop(self)), other);
return Qfalse;
}
VALUE window_op_teq(VALUE self, VALUE other)
{
// Match window class rather than title
if (ISA(other,cWindow))
return rb_equal(window_class(self), window_class(other));
if (TYPE(other) == T_REGEXP)
return rb_reg_match(other, window_class(self));
if (TYPE(other) == T_STRING)
return rb_equal(window_class(self), other);
if (TYPE(other) == T_FIXNUM)
return rb_equal(desktop_index(window_desktop(self)), other);
return Qfalse;
}
VALUE wm_filter(VALUE self, VALUE other, int ftype)
{
open_disp;
VALUE lst = rb_ary_new();
VALUE iter(VALUE x)
{
if (ftype == WM_FILTER_CLASS)
if (RTEST(window_op_teq(x, other))) rb_ary_push(lst, x);
if (ftype == WM_FILTER_TITLE)
if (RTEST(window_op_eq(x, other))) rb_ary_push(lst, x);
return Qnil;
}
rb_iterate(rb_each, self, iter, 0);
close_disp;
return lst;
}
VALUE wm_find(VALUE self, VALUE other, int ftype)
{
open_disp;
VALUE fnd = Qnil;
VALUE iter(VALUE x)
{
if (ftype == WM_FILTER_CLASS)
if (RTEST(window_op_teq(x, other)))
{
fnd = x;
rb_iter_break();
}
if (ftype == WM_FILTER_TITLE)
if (RTEST(window_op_eq(x, other)))
{
fnd = x;
rb_iter_break();
}
return Qnil;
}
rb_iterate(rb_each, cWindow, iter, 0);
close_disp;
return fnd;
}
VALUE Window_op_div(VALUE self, VALUE other)
{
return wm_filter(self,other,WM_FILTER_TITLE);
}
VALUE Window_op_gi(VALUE self, VALUE other)
{
return wm_filter(self,other,WM_FILTER_CLASS);
}
VALUE Window_window(VALUE self, VALUE x)
{
return wm_find(self, x, WM_FILTER_CLASS);
}
VALUE Window_op_mul(VALUE self, VALUE x)
{
return wm_find(self, x, WM_FILTER_TITLE);
}
VALUE window_desktop(VALUE self)
{
rbwm_init(WINDOW);
int d = xwm_get_win_desk(disp, data->win);
close_disp;
return new_desktop(d);
}
VALUE window_pid(VALUE self)
{
rbwm_init(WINDOW);
int pid = xwm_get_win_pid(disp, data->win);
close_disp;
return INT2FIX(pid);
}
VALUE window_activate(VALUE self)
{
rbwm_init(WINDOW);
xwm_win_activate(disp, data->win);
close_disp;
return self;
}
VALUE window_remove_decor(VALUE self)
{
rbwm_init(WINDOW);
xwm_win_remove_decor(disp, data->win);
close_disp;
return Qnil;
}
// This is where it starts to get ugly
// I'm still debugging/reworking all this. It doesnt really work yet.
VALUE window_dir(VALUE self, int dir)
{
rbwm_init(WINDOW);
int i,junkx,junky,bw,depth;
int wwidth,wheight,wleft,wtop,wright,wbottom;
int owidth,oheight,oleft,otop,oright,obottom;
Window junkroot;
long cnt;
XGetGeometry(disp, data->win, &junkroot, &junkx, &junky,
&wwidth, &wheight, &bw, &depth);
XTranslateCoordinates(disp, data->win, junkroot, junkx, junky,
&wleft, &wtop, &junkroot);
wright = wleft + wwidth;
wbottom = wtop + wheight;
VALUE ret = self;
Window *lst = xwm_window_list_byage(disp, &cnt);
for (i = 0; i < cnt / sizeof(Window); i++)
{
XGetGeometry(disp, lst[i], &junkroot, &junkx, &junky,
&owidth, &oheight, &bw, &depth);
XTranslateCoordinates(disp, lst[i], junkroot, junkx, junky,
&oleft, &otop, &junkroot);
oright = oleft + owidth;
obottom = otop + oheight;
if (same_desk(data->win, lst[i]))
{
if (dir == WM_LEFT)
if (oright < wleft && (obottom > wtop && otop < wbottom))
if (!same_win(ret,self) && (oright > FIX2INT(window_right(ret))))
ret = new_window(lst[i]);
else
ret = new_window(lst[i]);
if (dir == WM_RIGHT)
if (oleft > wright && (obottom > wtop && otop < wbottom))
if (!same_win(ret,self) && (oleft < FIX2INT(window_left(ret))))
ret = new_window(lst[i]);
else
{
printf("%s - %d - %d\n", xwm_get_win_title(disp, lst[i]), oleft, FIX2INT(window_left(ret)));
ret = new_window(lst[i]);
}
}
}
close_disp;
return RTEST(window_op_eq(ret,self)) ? Qnil : ret;
}
// this is where the ugliness stops
// ugliness will eventually be purged
VALUE window_dir_left(VALUE self) { return window_dir(self, WM_LEFT); }
VALUE window_dir_right(VALUE self) { return window_dir(self, WM_RIGHT); }
VALUE window_dir_top(VALUE self) { return window_dir(self, WM_TOP); }
VALUE window_dir_bottom(VALUE self) { return window_dir(self, WM_BOTTOM); }
void Init_wmlib()
{
mWM = rb_define_module("WM");
rb_imdef(mWM, "Window", Window_window, 1);
cDesktop = rb_wmclass("Desktop");
rb_cmdef(cDesktop, "new", Desktop_new, 1);
rb_cmdef(cDesktop, "current", Desktop_current, 0);
rb_cmdef(cDesktop, "current=", Desktop_set_current, 1);
rb_cmdef(cDesktop, "each", Desktop_each, 0);
rb_cmdef(cDesktop, "count", Desktop_count, 0);
rb_cmdef(cDesktop, "last", Desktop_last, 0);
rb_cmdef(cDesktop, "first", Desktop_first, 0);
rb_imdef(cDesktop, "index", desktop_index, 0);
rb_imdef(cDesktop, "each", desktop_each, 0);
rb_imdef(cDesktop, "filter", Obj_filter, 0);
rb_imdef(cDesktop, "find", Obj_find, 0);
rb_imdef(cDesktop, "to_s", desktop_to_s, 0);
rb_imdef(cDesktop, "+", desktop_op_add, 1);
rb_imdef(cDesktop, "-", desktop_op_sub, 1);
rb_imdef(cDesktop, "==", desktop_op_eq, 1);
rb_imdef(cDesktop, "root_window", Desktop_root_window,0);
cWindow = rb_wmclass("Window");
rb_cmdef(cWindow, "new", Window_new, 0);
rb_cmdef(cWindow, "current", Window_current, 0);
rb_cmdef(cWindow, "on_press", Window_on_press, 1);
rb_cmdef(cWindow, "key_loop", Window_key_loop, 0);
rb_cmdef(cWindow, "each", Window_each, 0);
rb_imdef(cWindow, "eachChild", Window_eachChild, 0);
rb_cmdef(cWindow, "filter", Obj_filter, 0);
rb_cmdef(cWindow, "find", Obj_find, 0);
rb_cmdef(cWindow, "*", Window_op_mul, 1);
rb_cmdef(cWindow, "/", Window_op_div, 1);
rb_cmdef(cWindow, "[]", Window_op_gi, 1);
rb_imdef(cWindow, "==", window_op_eq, 1);
rb_imdef(cWindow, "=~", window_op_teq, 1);
rb_imdef(cWindow, "to_s", window_title, 0);
rb_imdef(cWindow, "title", window_title, 0);
rb_imdef(cWindow, "id", window_id, 0);