forked from gitluin/sara
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsara.c
1695 lines (1375 loc) · 39.5 KB
/
sara.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
/*
* sara Window Manager
* ______________________________________________________________________________
*
* Copyright (c) 2010, Rinaldini Julien, [email protected]
* Copyright (c) 2020, This Fackin Guy, gitluin on github (no email for you!)
*
* Please refer to the MIT license for details on usage: https://mit-license.org/
*/
#include <stdio.h>
#include <math.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <X11/keysym.h>
#include <X11/Xatom.h>
#include <X11/XF86keysym.h>
#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <X11/Xft/Xft.h>
#ifdef XINERAMA
#include <X11/extensions/Xinerama.h>
#endif
#define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
#define MOUSEMASK (BUTTONMASK|PointerMotionMask)
#define EACHCLIENT(_I) (ic=_I;ic;ic=ic->next) /* ic is a global */
#define EACHMON(_M) (im=_M;im;im=im->next) /* im is a global */
#define ISOUTSIDE(PX,PY,X,Y,W,H) ((PX > X + W || PX < X || PY > Y + H || PY < Y))
#define ISVISIBLE(C) ((C->desks & C->mon->seldesks))
#define MAX(A,B) ((A) > (B) ? (A) : (B))
#define POSTOINT(X) ((int)(ceil(log2(X)) == floor(log2(X)) ? ceil(log2(X)) : 0))
#define TABLENGTH(X) (sizeof(X)/sizeof(*X))
#define TEXTW(M,X) (gettextwidth(M, X, slen(X)) + lrpad)
enum { SchNorm, SchSel };
enum { ColFg, ColBg };
enum { SymLeft, SymRight };
enum { AnyVis, OnlyVis };
enum { NoZoom, YesZoom };
enum { NoDetach, YesDetach };
enum { NoFocus, YesFocus };
enum { NoStay, YesStay };
enum { ClkHldr, ClkWin };
enum { WantMove, WantResize };
enum { NoFloat, YesFloat };
/* ---------------------------------------
* Structs
* ---------------------------------------
*/
typedef struct bar bar;
typedef struct client client;
typedef struct drw drw;
typedef struct desktop desktop;
typedef struct monitor monitor;
typedef union {
const int i;
const unsigned int ui;
const float f;
const void* v;
} Arg;
typedef struct {
unsigned int click;
unsigned int mask;
unsigned int btn;
void (*func)(const Arg arg);
const Arg arg;
} button;
struct key {
unsigned int mod;
KeySym keysym;
void (*function)(const Arg arg);
const Arg arg;
};
typedef struct {
const char* symbol;
void (*arrange)(monitor*);
} layout;
typedef struct {
const char* class, * instance, * title;
int desks;
int isfloat, isfull;
int monitor;
} rule;
struct bar {
int w, h;
Window win;
};
struct client {
int x, y, w, h;
unsigned int desks;
unsigned int iscur;
/* being in monocle is not considered floating */
int isfloat;
int isfull;
/* prior to togglefs */
int oldfloat;
Window win;
client* next;
monitor* mon;
};
struct drw {
int w, h;
Drawable d;
GC gc;
XftColor* scheme;
XftDraw* xd;
XftFont* xfont;
};
struct desktop {
float msize;
layout* curlayout;
};
struct monitor {
/* monitor */
int x, y, h, w;
int num;
bar* bar;
monitor* next;
/* desks */
float msize;
unsigned int seldesks;
unsigned int curdesk;
client* current;
client* head;
desktop* desks;
layout* curlayout;
};
/* ---------------------------------------
* Util Functions
* ---------------------------------------
*/
void die(const char* e, ...){
fprintf(stdout, "sara: %s\n", e);
exit(1);
}
void* ecalloc(size_t nmemb, size_t size){
void* p;
if ( !(p = calloc(nmemb,size)) ) die("ecalloc failed");
return p;
}
int slen(const char* str){
int i = 0;
while (*str){ str++; i++; }
return i;
}
/* ---------------------------------------
* Main Function Declarations
* ---------------------------------------
*/
/* X Event Processing */
static void buttonpress(XEvent* e);
static void configurenotify(XEvent* e);
static void configurerequest(XEvent* e);
static void destroynotify(XEvent* e);
static void enternotify(XEvent* e);
static void expose(XEvent* e);
static void keypress(XEvent* e);
static void maprequest(XEvent* e);
static void motionnotify(XEvent* e);
static void propertynotify(XEvent* e);
/* Client & Linked List Manipulation */
static void adjustcoords(client* c);
static void applyrules(client* c);
static void attachaside(client* c);
static void changecurrent(client* c, monitor* m, unsigned int deskmask);
static void configure(client* c);
static void detach(client* c);
static void killclient();
static void manage(Window parent, XWindowAttributes* wa);
static void manipulate(const Arg arg);
static void mapclients();
static void moveclient(const Arg arg);
static void moveclientup(client* c, int wantzoom);
static void movefocus(const Arg arg);
static void raisefloats();
static void refocus(client* n, client* p, monitor* m);
static void resizeclient(client* c, int x, int y, int w, int h);
static void sendmon(client* c, monitor* m);
static void todesktop(const Arg arg);
static void toggledesktop(const Arg arg);
static void togglefloat();
static void togglefs();
static void tomon(const Arg arg);
static void unmanage(client* c);
static void updatefocus();
static void zoom();
/* Monitor Manipulation */
static void changemon(monitor* m, int wantfocus);
static void cleanupmon(monitor* m);
static monitor* createmon(int num, int x, int y, int w, int h);
static monitor* dirtomon(int dir);
static monitor* findmon(Window w);
static void focusmon(const Arg arg);
static void updategeom();
/* Client Interfacing */
static client* findclient(Window w);
static client* findcurrent();
static client* findprevclient(client* c, int wantvis, int wantfloat);
static client* findvisclient(client* c, int wantfloat);
/* Bar */
static void drawbar(monitor* m);
static void drawbars();
static int drawbartext(monitor* m, int x, int y, int w, int h, unsigned int lpad, const char* text);
static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
static int gettextwidth(monitor* m, const char* str, int len);
static bar* initbar(monitor* m);
static void updatestatus();
/* Desktop Interfacing */
static void changemsize(const Arg arg);
static void loaddesktop(int i);
static void monocle(monitor* m);
static void setlayout(const Arg arg);
static void tile(monitor* m);
static void toggleview(const Arg arg);
static void view(const Arg arg);
/* Backend */
static void cleandrw();
static void cleanup();
static XftColor* createscheme(const char* clrnames[], size_t clrcount);
static int getptrcoords(int* x, int* y);
static void grabbuttons(client* c, int focused);
static void grabkeys();
static void initdrw();
static void setup();
static void sigchld(int unused);
static void spawn(const Arg arg);
static void start();
static int xerror(Display* dis, XErrorEvent* e);
static int xerrordummy(Display* dis, XErrorEvent* e);
static int xsendkill(Window w);
static void youviolatedmymother();
/* Make the above known */
#include "config.h"
/* ---------------------------------------
* Globals
* ---------------------------------------
*/
/* X Interfacing */
static Cursor cursor;
static Display* dis;
static Window root;
static int screen;
static int sh;
static int sw;
/* Monitor Interfacing */
static monitor* curmon;
static monitor* mhead;
/* Backend */
static client* ic; /* for EACHCLIENT iterating */
static monitor* im; /* for EACHMON iterating */
static int justmanage;
static int justmanageunder;
static int justswitch;
static int lrpad;
static int running;
static XftColor** scheme;
static drw* sdrw;
static char xsetr_text[256];
/* Events array */
static void (*events[LASTEvent])(XEvent* e) = {
[ButtonPress] = buttonpress,
[ConfigureNotify] = configurenotify,
[ConfigureRequest] = configurerequest,
[DestroyNotify] = destroynotify,
[EnterNotify] = enternotify,
[Expose] = expose,
[KeyPress] = keypress,
[MapRequest] = maprequest,
[MotionNotify] = motionnotify,
[PropertyNotify] = propertynotify
};
/* ---------------------------------------
* X Event Processing
* ---------------------------------------
*/
/* dwm copypasta */
void buttonpress(XEvent* e){
unsigned int click = 0;
int i;
client* c;
monitor* m;
XButtonPressedEvent* ev = &e->xbutton;
if ( (m = findmon(ev->window)) && m != curmon)
changemon(m, NoFocus);
if ( (c = findclient(ev->window)) ){
changecurrent(c, c->mon, c->mon->curdesk);
updatefocus();
XAllowEvents(dis, ReplayPointer, CurrentTime);
click = ClkWin;
}
for (i = 0; i < TABLENGTH(buttons); i++)
if (click == buttons[i].click && buttons[i].func
&& buttons[i].btn == ev->button
&& buttons[i].mask == ev->state)
buttons[i].func(buttons[i].arg);
justswitch = 0;
}
/* dwm copypasta */
void configurenotify(XEvent* e){
XConfigureEvent* ev = &e->xconfigure;
if (ev->window == root){
sw = ev->width; sh = ev->height;
if (sdrw && (sdrw->w != sw || sdrw->h != sh)){
cleandrw();
initdrw();
}
updategeom();
for EACHMON(mhead){
for EACHCLIENT(im->head) if (ic->isfull){
resizeclient(ic, im->x, im->y - im->bar->h, im->w, im->h + im->bar->h);
}
im->curlayout->arrange(im);
}
updatefocus();
}
}
/* dwm copypasta */
void configurerequest(XEvent* e){
client* c;
monitor* m;
XConfigureRequestEvent* ev = &e->xconfigurerequest;
XWindowChanges wc;
if ( (c = findclient(ev->window)) ){
if (c->isfloat){
m = c->mon;
if (ev->value_mask & CWX) c->x = m->x + ev->x;
if (ev->value_mask & CWY) c->y = (ev->y < m->bar->h) ? m->bar->h : ev->y;
if (ev->value_mask & CWWidth) c->w = ev->width;
if (ev->value_mask & CWHeight) c->h = ev->height;
if ISVISIBLE(c) XMoveResizeWindow(dis, c->win, c->x, c->y, c->w, c->h);
} else {
configure(c);
}
} else {
wc.x = ev->x; wc.y = ev->y;
wc.width = ev->width; wc.height = ev->height;
wc.border_width = 0;
wc.sibling = ev->above; wc.stack_mode = ev->detail;
XConfigureWindow(dis, ev->window, ev->value_mask, &wc);
}
XSync(dis, False);
}
void destroynotify(XEvent* e){
client* c;
XDestroyWindowEvent* ev = &e->xdestroywindow;
if ( (c = findclient(ev->window)) )
unmanage(c);
}
/* some dwm copypasta */
void enternotify(XEvent* e){
client* c;
monitor* m;
XCrossingEvent* ev = &e->xcrossing;
if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
return;
if ( !(c = findclient(ev->window)) )
return;
/* if focus is pulled by manage, and ptr enters that client, allow new events */
if (c == curmon->current){
if (justmanage)
justmanage = justswitch = 0;
return;
}
/* prevent focus from freaking out if you spawn a client underneath a ptr-focused float */
if (justmanageunder){
justmanageunder = justswitch = 0;
return;
}
/* if enternotify is generated by moveclient, movefocus, or view, ignore it */
if (justswitch){
justswitch = 0;
return;
}
if ( (m = c->mon) && m != curmon )
changemon(m, NoFocus);
changecurrent(c, c->mon, c->mon->curdesk);
updatefocus();
}
void expose(XEvent* e){
XExposeEvent* ev = &e->xexpose;
if (ev->count == 0 && findmon(ev->window))
drawbars();
}
void keypress(XEvent* e){
int i;
XKeyEvent ke = e->xkey;
KeySym keysym = XKeycodeToKeysym(dis, ke.keycode, 0);
for (i=0;i<TABLENGTH(keys);i++)
if (keys[i].keysym == keysym && keys[i].mod == ke.state)
keys[i].function(keys[i].arg);
}
/* dwm copypasta */
void maprequest(XEvent* e){
XWindowAttributes wa;
XMapRequestEvent* ev = &e->xmaprequest;
if (!XGetWindowAttributes(dis, ev->window, &wa))
return;
if (wa.override_redirect)
return;
if (!findclient(ev->window))
manage(ev->window, &wa);
}
void motionnotify(XEvent* e){
XMotionEvent* ev = &e->xmotion;
if (ev->window != root)
return;
if (ISOUTSIDE(ev->x_root, ev->y_root, curmon->x, curmon->y - curmon->bar->h, curmon->w, curmon->h + curmon->bar->h)){
for EACHMON(mhead){
if (im != curmon){
changemon(im, YesFocus);
return;
}
}
}
}
/* dwm copypasta */
void propertynotify(XEvent* e){
XPropertyEvent* ev = &e->xproperty;
if ((ev->window == root) && (ev->atom == XA_WM_NAME))
updatestatus();
}
/* ---------------------------------------
* Client & Linked List Manipulation
* ---------------------------------------
*/
void adjustcoords(client* c){
if (ISOUTSIDE(c->x, c->y, c->mon->x, c->mon->y - c->mon->bar->h, c->mon->w, c->mon->h + c->mon->bar->h)){
/* find which one it is inside */
for EACHMON(mhead)
if (!ISOUTSIDE(c->x, c->y, im->x, im->y - im->bar->h, im->w, im->h + im->bar->h)){
c->x += (im->x < c->mon->x) ? c->mon->x : -im->x;
c->y += (im->y < c->mon->y) ? c->mon->y : -im->y;
break;
}
}
}
/* mostly dwm copypasta */
void applyrules(client* c){
const char* class, * instance;
int i;
const rule* r;
XClassHint ch = { NULL, NULL };
XTextProperty tp;
c->isfloat = c->desks = 0;
XGetWMName(dis, c->win, &tp);
XGetClassHint(dis, c->win, &ch);
class = ch.res_class ? ch.res_class : "broken";
instance = ch.res_name ? ch.res_name : "broken";
for (i=0;i<TABLENGTH(rules);i++){
r = &rules[i];
if ((!r->title || (tp.value && strstr(r->title, (const char*) tp.value)))
&& (!r->class || strstr(class, r->class))
&& (!r->instance || strstr(instance, r->instance)))
{
c->isfloat = r->isfloat;
c->isfull = r->isfull;
c->desks |= r->desks;
for EACHMON(mhead) if (im->num == r->monitor) break;
if (im) c->mon = im;
}
}
if (ch.res_class) XFree(ch.res_class);
if (ch.res_name) XFree(ch.res_name);
c->desks = c->desks ? c->desks : c->mon->seldesks;
}
void attachaside(client* c){
client* l;
if (!c->mon->head){
c->mon->head = c;
} else {
/* If not the first on this desktop */
if (c->mon->current){
c->next = c->mon->current->next;
c->mon->current->next = c;
} else {
for (l=c->mon->head;l->next;l=l->next);
l->next = c;
}
}
if (c->mon->current && c->mon->current->isfloat) justmanageunder = 1;
changecurrent(c, c->mon, c->mon->curdesk);
}
void changecurrent(client* c, monitor* m, unsigned int deskmask){
if (c){
c->iscur ^= deskmask;
grabbuttons(c, 1);
}
for EACHCLIENT(m->head) if (ic != c && (ic->iscur & deskmask)){
ic->iscur ^= deskmask;
grabbuttons(ic, 0);
}
m->current = c;
}
void configure(client* c){
XConfigureEvent ce;
ce.type = ConfigureNotify;
ce.display = dis;
ce.event = c->win;
ce.window = c->win;
ce.x = c->x; ce.y = c->y;
ce.width = c->w; ce.height = c->h;
ce.border_width = 0;
ce.above = None;
ce.override_redirect = False;
XSendEvent(dis, c->win, False, StructureNotifyMask, (XEvent *)&ce);
}
void detach(client* c){
client* p;
/* Move the window out of the way first to hide it while it hangs around :) */
XMoveWindow(dis, c->win, 2*sw, 0);
/* before disconnecting c
* written this way so function keys don't move your focus
*/
if (c->desks & c->mon->curdesk) refocus(c->next, c, c->mon);
else changecurrent(findprevclient(c, OnlyVis, YesFloat), c->mon, c->mon->curdesk);
/* For both, if NULL, then we're still okay */
if ( (p = findprevclient(c, AnyVis, YesFloat)) )
p->next = c->next;
else
c->mon->head = c->next;
}
/* dwm copypasta */
void killclient(){
if (!curmon->current)
return;
if (!xsendkill(curmon->current->win)){
XGrabServer(dis);
XSetErrorHandler(xerrordummy);
XSetCloseDownMode(dis, DestroyAll);
XKillClient(dis, curmon->current->win);
XSync(dis, False);
XSetErrorHandler(xerror);
XUngrabServer(dis);
}
}
void manage(Window parent, XWindowAttributes* wa){
client* c, * t;
Window trans = None;
if ( !(c = ecalloc(1, sizeof(client))) )
die("Error while callocing new client!");
c->win = parent;
c->isfloat = c->oldfloat = c->isfull = c->iscur = 0;
c->x = wa->x; c->y = wa->y;
c->w = wa->width; c->h = wa->height;
if (XGetTransientForHint(dis, parent, &trans) && (t = findclient(trans))){
c->desks = t->desks;
c->mon = t->mon;
} else {
c->mon = curmon;
applyrules(c);
}
if (!c->isfloat) c->isfloat = c->oldfloat = (trans != None);
configure(c);
XSelectInput(dis, c->win, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
grabbuttons(c, 0);
attachaside(c);
adjustcoords(c);
c->y = (c->y < c->mon->y) ? c->mon->y : c->y;
/* move out of the way until told otherwise */
XMoveResizeWindow(dis, c->win, c->x + 2*sw, c->y, c->w, c->h);
XMapWindow(dis, c->win);
/* applyrules */
if (c->isfull && c == curmon->current){
c->isfull = !c->isfull;
togglefs();
}
justmanage = 1;
c->mon->curlayout->arrange(c->mon);
if (c->desks & curmon->seldesks) updatefocus();
}
void mapclients(monitor* m){
for EACHCLIENT(m->head) if ISVISIBLE(ic) XMapWindow(dis, ic->win); else XUnmapWindow(dis, ic->win);
}
void moveclient(const Arg arg){
client* c;
if (!curmon->current || curmon->current->isfull)
return;
/* Up stack */
if (arg.i > 0)
moveclientup(curmon->current, NoZoom);
/* Down stack - equivalent to moving next tiled client up */
else if ( arg.i < 0 && (c = findvisclient(curmon->current->next, NoFloat)) )
moveclientup(c, NoZoom);
justswitch = 1;
curmon->curlayout->arrange(curmon);
updatefocus();
}
void moveclientup(client* c, int wantzoom){
client* p, * target, * ptarget;
if (!c) return;
/* Go up only if not highest visible */
if (wantzoom){
if ( !(target = findvisclient(curmon->head, NoFloat)) || target == c )
return;
} else {
if ( !(target = findprevclient(c, 0, NoFloat)) )
return;
}
p = findprevclient(c, AnyVis, YesFloat);
ptarget = findprevclient(target, AnyVis, YesFloat);
/* if p == target, then we're still okay */
p->next = c->next;
c->next = target;
if (ptarget) ptarget->next = c;
else curmon->head = c;
}
/* some dwm copypasta */
void movefocus(const Arg arg){
client* j, * c = NULL;
if (!curmon->current || curmon->current->isfull)
return;
/* up stack */
if (arg.i > 0){
for (j=curmon->head;j != curmon->current;j=j->next)
if ISVISIBLE(j) c = j;
/* if curmon->current was highest, go to the bottom */
if (!c) for (;j;j=j->next) if ISVISIBLE(j) c = j;
/* down stack */
} else {
if ( !(c = findvisclient(curmon->current->next, YesFloat)) )
for (c=curmon->head;c && !ISVISIBLE(c);c=c->next);
}
justswitch = 1;
changecurrent(c, curmon, curmon->curdesk);
updatefocus();
}
/* dwm copypasta */
void manipulate(const Arg arg){
int x, y, ocx, ocy, nx, ny, nw, nh, trytoggle = 0;
client* c;
monitor* m;
XEvent ev;
Time lasttime = 0;
if ( !(c = curmon->current) || c->isfull )
return;
if (XGrabPointer(dis, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
None, cursor, CurrentTime) != GrabSuccess)
return;
if (!getptrcoords(&x, &y))
return;
if (arg.i == WantResize){
if ( !(m = c->mon) )
return;
XWarpPointer(dis, None, c->win, 0, 0, 0, 0, c->w + 1, c->h + 1);
}
ocx = c->x; ocy = c->y;
do {
XMaskEvent(dis, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
switch(ev.type){
case ConfigureRequest:
case Expose:
case MapRequest:
events[ev.type](&ev);
break;
case MotionNotify:
if ((ev.xmotion.time - lasttime) <= (1000 / 60))
continue;
lasttime = ev.xmotion.time;
if (arg.i == WantResize){
nw = MAX(ev.xmotion.x - ocx + 1, 1);
nh = MAX(ev.xmotion.y - ocy + 1, 1);
/* if c extends beyond the boundaries of its monitor, make it a float */
if (m->x + nw >= curmon->x && m->x + nw <= curmon->x + curmon->w
&& m->y + nh >= curmon->y && m->y + nh <= curmon->y + curmon->h)
trytoggle = 1;
nx = c->x; ny = c->y;
} else {
nx = ocx + (ev.xmotion.x - x);
ny = ocy + (ev.xmotion.y - y);
if (abs(curmon->x - nx) < snap)
nx = curmon->x;
else if (abs((curmon->x + curmon->w) - (nx + c->w)) < snap)
nx = curmon->x + curmon->w - c->w;
if (abs(curmon->y - ny) < snap)
ny = curmon->y;
else if (abs((curmon->y + curmon->h) - (ny + c->h)) < snap)
ny = curmon->y + curmon->h - c->h;
trytoggle = 1;
nw = c->w; nh = c->h;
}
if (trytoggle)
if (!c->isfloat && ((abs(nw - c->w) > snap || abs(nh - c->h) > snap)
|| (abs(nx - c->x) > snap || abs(ny - c->y) > snap)))
togglefloat();
if (c->isfloat)
resizeclient(c, nx, ny, nw, nh);
break;
}
} while (ev.type != ButtonRelease);
if (arg.i == WantResize)
XWarpPointer(dis, None, c->win, 0, 0, 0, 0, c->w + 1, c->h + 1);
XUngrabPointer(dis, CurrentTime);
if (arg.i == WantResize)
while (XCheckMaskEvent(dis, EnterWindowMask, &ev));
if (ISOUTSIDE(c->x, c->y, curmon->x, curmon->y - curmon->bar->h, curmon->w, curmon->h + curmon->bar->h)){
for EACHMON(mhead){
if (im != curmon && !ISOUTSIDE(c->x, c->y, im->x, im->y - im->bar->h, im->w, im->h + im->bar->h)){
sendmon(c, im);
changemon(im, YesFocus);
return;
}
}
}
}
void raisefloats(){
for EACHCLIENT(curmon->head) if (ISVISIBLE(ic) && ic->isfloat){
resizeclient(ic, ic->x, ic->y, ic->w, ic->h);
XRaiseWindow(dis, ic->win);
}
}
/* focus moves down if possible, else up */
void refocus(client* n, client* p, monitor* m){
client* vis;
vis = (vis = findvisclient(n, YesFloat)) ? vis : findprevclient(p, OnlyVis, YesFloat);
changecurrent(vis, m, m->curdesk);
}
void resizeclient(client* c, int x, int y, int w, int h){
XWindowChanges wc;
c->x = wc.x = x;
c->y = wc.y = y;
c->w = wc.width = w;
c->h = wc.height = h;
XConfigureWindow(dis, c->win, CWX|CWY|CWWidth|CWHeight, &wc);
XSync(dis, False);
}
/* mostly dwm copypasta */
void sendmon(client* c, monitor* m){
if (c->mon == m || c->isfull) return;
detach(c);
c->mon = m;
c->desks = m->seldesks;
c->next = NULL;
attachaside(c);
if (c->isfloat) adjustcoords(c);
curmon->current = findcurrent();
for EACHMON(mhead){
mapclients(im);
im->curlayout->arrange(im);
}
changemon(c->mon, YesFocus);
}
void todesktop(const Arg arg){
if (curmon->curdesk & arg.ui) return;
curmon->current->desks = arg.ui;
curmon->current->iscur = 0;
changecurrent(curmon->current, curmon, arg.ui);
XUnmapWindow(dis, curmon->current->win);
XSync(dis, False);
refocus(curmon->current->next, curmon->current, curmon);
curmon->curlayout->arrange(curmon);
updatefocus();
}
void toggledesktop(const Arg arg){
unsigned int newdesks;
if (!curmon->current)
return;
if ( (newdesks = curmon->current->desks ^ arg.ui) ){
curmon->current->desks = newdesks;
changecurrent(curmon->current, curmon, arg.ui);
if ( !(ISVISIBLE(curmon->current)) ){
XUnmapWindow(dis, curmon->current->win);
XSync(dis, False);
refocus(curmon->current->next, curmon->current, curmon);
}
curmon->curlayout->arrange(curmon);
updatefocus();
}
}
void togglefloat(){
XWindowChanges wc;
if (!curmon->current || curmon->current->isfull) return;
if ( (curmon->current->isfloat = !curmon->current->isfloat) ){
wc.sibling = curmon->current->win;
wc.stack_mode = Below;
for EACHCLIENT(curmon->head)
if (ic != curmon->current)
XConfigureWindow(dis, ic->win, CWSibling|CWStackMode, &wc);
} else {
wc.sibling = curmon->bar->win;
wc.stack_mode = Below;
XConfigureWindow(dis, curmon->current->win, CWSibling|CWStackMode, &wc);
}
curmon->curlayout->arrange(curmon);
}
void togglefs(){
if (!curmon->current) return;
if ( (curmon->current->isfull = !curmon->current->isfull) ){
curmon->current->oldfloat = curmon->current->isfloat;
curmon->current->isfloat = 0;
XMoveResizeWindow(dis, curmon->current->win, curmon->x, curmon->y - curmon->bar->h,
curmon->w, curmon->h + curmon->bar->h);
XRaiseWindow(dis, curmon->current->win);
XUnmapWindow(dis, curmon->bar->win);
} else {
justswitch = 1;
curmon->current->isfloat = curmon->current->oldfloat;
curmon->curlayout->arrange(curmon);
XMapRaised(dis, curmon->bar->win);
drawbars();
}
}
void tomon(const Arg arg){
if (!curmon->current || !mhead->next)
return;
sendmon(curmon->current, dirtomon(arg.i));
}
void unmanage(client* c){
monitor* m = c->mon;
detach(c);
XUngrabButton(dis, AnyButton, AnyModifier, c->win);
if (c) free(c);
m->curlayout->arrange(m);
updatefocus();
}
void updatefocus(){
if (curmon->current){
XSetInputFocus(dis, curmon->current->win, RevertToPointerRoot, CurrentTime);
XRaiseWindow(dis, curmon->current->win);
} else {
XSetInputFocus(dis, root, RevertToPointerRoot, CurrentTime);
}
raisefloats();
drawbars();
}
void zoom(){
moveclientup(curmon->current, YesZoom);
justswitch = 1;
curmon->curlayout->arrange(curmon);
updatefocus();
}
/* ---------------------------------------
* Monitor Manipulation
* ---------------------------------------
*/
void changemon(monitor* m, int wantfocus){