-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwm.c
4867 lines (4378 loc) · 139 KB
/
dwm.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
/* See LICENSE file for copyright and license details.
*
* dynamic window manager is designed like any other X client as well. It is
* driven through handling X events. In contrast to other X clients, a window
* manager selects for SubstructureRedirectMask on the root window, to receive
* events about window (dis-)appearance. Only one X connection at a time is
* allowed to select for this event mask.
*
* The event handlers of dwm are organized in an array which is accessed
* whenever a new event has been fetched. This allows event dispatching
* in O(1) time.
*
* Each child of the root window is called a client, except windows which have
* set the override_redirect flag. Clients are organized in a linked client
* list on each monitor, the focus history is remembered through a stack list
* on each monitor. Each client contains a bit array to indicate the tags of a
* client.
*
* Keys and tagging rules are organized as arrays and defined in config.h.
*
* To understand everything else, start reading main().
*/
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#include <X11/keysym.h>
#include <errno.h>
#include <locale.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#ifdef XINERAMA
#include <X11/extensions/Xinerama.h>
#endif /* XINERAMA */
#include <X11/Xft/Xft.h>
#include "drw.h"
#include "util.h"
#include <X11/extensions/XInput2.h>
#include <math.h>
#include <signal.h>
/* macros */
#define BUTTONMASK (ButtonPressMask | ButtonReleaseMask)
#define CLEANMASK(mask) \
(mask & ~(numlockmask | LockMask) & \
(ShiftMask | ControlMask | Mod1Mask | Mod2Mask | Mod3Mask | Mod4Mask | \
Mod5Mask))
#define INTERSECT(x, y, w, h, m) \
(MAX(0, MIN((x) + (w), (m)->wx + (m)->ww) - MAX((x), (m)->wx)) * \
MAX(0, MIN((y) + (h), (m)->wy + (m)->wh) - MAX((y), (m)->wy)))
#define ISVISIBLE(C) \
((C->mon->isoverview || C->isglobal || \
C->tags & C->mon->tagset[C->mon->seltags]))
#define HIDDEN(C) ((getstate(C->win) == IconicState))
#define LENGTH(X) (sizeof X / sizeof X[0])
#define MOUSEMASK (BUTTONMASK | PointerMotionMask)
#define WIDTH(X) ((X)->w + 2 * (X)->bw)
#define HEIGHT(X) ((X)->h + 2 * (X)->bw)
#define TAGMASK ((1 << LENGTH(tags)) - 1)
#define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
#define SYSTEM_TRAY_REQUEST_DOCK 0
/* XEMBED messages */
#define XEMBED_EMBEDDED_NOTIFY 0
#define XEMBED_WINDOW_ACTIVATE 1
#define XEMBED_FOCUS_IN 4
#define XEMBED_MODALITY_ON 10
#define XEMBED_MAPPED (1 << 0)
#define XEMBED_WINDOW_ACTIVATE 1
#define XEMBED_WINDOW_DEACTIVATE 2
#define VERSION_MAJOR 0
#define VERSION_MINOR 0
#define XEMBED_EMBEDDED_VERSION (VERSION_MAJOR << 16) | VERSION_MINOR
#define OPAQUE 0xffU
/* enums */
enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
enum {
SchemeNorm, // 普通
SchemeSel, // 选中的
SchemeSelGlobal, // 全局并选中的
SchemeSelScratchpad, // 便签窗口
SchemeSelFakeFull, // 伪全屏并选中的
SchemeSelFakeFullGLObal, // 伪全屏并全局并选中
SchemeHid, // 隐藏的
SchemeSystray, // 托盘
SchemeNormTag, // 普通标签
SchemeSelTag, // 选中的标签
SchemeUnderline, // 下划线
SchemeBarEmpty, // 状态栏空白部分
SchemeStatusText, // 状态栏文本
}; /* color schemes */
enum {
NetSupported,
NetWMName,
NetWMState,
NetWMCheck,
NetSystemTray,
NetSystemTrayOP,
NetSystemTrayOrientation,
NetSystemTrayOrientationHorz,
NetWMFullscreen,
NetActiveWindow,
NetWMWindowType,
NetWMWindowTypeDialog,
NetClientList,
NetLast
}; /* EWMH atoms */
enum { Manager, Xembed, XembedInfo, XLast }; /* Xembed atoms */
enum {
WMProtocols,
WMDelete,
WMState,
WMTakeFocus,
WMLast
}; /* default atoms */
enum {
ClkTagBar,
ClkLtSymbol,
ClkStatusText,
ClkWinTitle,
ClkBarEmpty,
ClkClientWin,
ClkRootWin,
ClkLast
}; /* clicks */
enum { UP, DOWN, LEFT, RIGHT }; /* movewin */
enum { V_EXPAND, V_REDUCE, H_EXPAND, H_REDUCE }; /* resizewins */
enum {_NET_WM_STATE_REMOVE,_NET_WM_STATE_ADD,_NET_WM_STATE_TOGGLE}; //区分clientmessage是添加请求还是清除请求,第三个是切换请求
typedef struct {
int i;
unsigned int ui;
float f;
const void *v;
} Arg;
typedef struct {
unsigned int click;
unsigned int mask;
unsigned int button;
void (*func)(const Arg *arg);
const Arg arg;
} Button;
typedef struct Monitor Monitor;
typedef struct Client Client;
struct Client {
char name[256];
char icon[10];
float mina, maxa;
int x, y, w, h;
int oldx, oldy, oldw, oldh;
int bw, oldbw;
int overview_backup_x, overview_backup_y, overview_backup_w,
overview_backup_h, overview_backup_bw;
int fullscreen_backup_x, fullscreen_backup_y, fullscreen_backup_w,
fullscreen_backup_h;
int isactive;
int basew, baseh, incw, inch, maxw, maxh, minw, minh;
int taskw, no_limit_taskw;
unsigned int tags;
int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen,
isglobal, isnoborder, overview_isfullscreenbak,
overview_isfloatingbak;
Client *next;
Client *snext;
Monitor *mon;
Window win;
int is_in_scratchpad;
int is_scratchpad_show;
};
typedef struct {
unsigned int mod;
KeySym keysym;
void (*func)(const Arg *);
const Arg arg;
} Key;
typedef struct {
const char *symbol;
void (*arrange)(Monitor *);
} Layout;
typedef struct Pertag Pertag;
struct Monitor {
char ltsymbol[16];
float mfact;
int nmaster;
int num;
int by; /* bar geometry */
int bt; /* number of tasks */
int mx, my, mw, mh; /* screen size */
int wx, wy, ww, wh; /* window area */
unsigned int seltags;
unsigned int sellt;
unsigned int tagset[2];
int showbar;
int topbar;
Client *clients;
Client *sel;
Client *stack;
Monitor *next;
Window barwin;
const Layout *lt[2];
Pertag *pertag;
uint isoverview;
uint is_in_hotarea;
int status_w;
};
typedef struct {
const char *class;
const char *instance;
const char *title;
unsigned int tags;
int isfloating;
int isglobal;
int isnoborder;
int monitor;
uint floatposition;
uint width;
uint high;
} Rule;
typedef struct {
const char *class;
const char *style;
} Icon;
typedef struct Systray Systray;
struct Systray {
Window win;
Client *icons;
};
/* function declarations */
static void logtofile(const char *fmt, ...);
static void lognumtofile(unsigned int num);
static void tile(Monitor *m);
static void rtile(Monitor *m);
static void magicgrid(Monitor *m);
static void overview(Monitor *m);
static void grid(Monitor *m, uint gappo, uint uappi);
static void applyrules(Client *c);
static int applysizehints(Client *c, int *x, int *y, int *w, int *h,
int interact);
static void arrange(Monitor *m);
static void arrangemon(Monitor *m);
static void attach(Client *c, int inserhead);
static void attachstack(Client *c);
static void buttonpress(XEvent *e);
static void checkotherwm(void);
static unsigned int judge_win_contain_state(Window w, Atom prop);
static void cleanup(void);
static void cleanupmon(Monitor *mon);
static void clientmessage(XEvent *e);
static void configure(Client *c);
static void configurenotify(XEvent *e);
static unsigned long get_win_pid(Window win);
static Atom getatompropfromwin(Window w, Atom prop);
static void configurerequest(XEvent *e);
static void clickstatusbar(const Arg *arg);
static Monitor *createmon(void);
static void destroynotify(XEvent *e);
static void detach(Client *c);
static void detachstack(Client *c);
static Monitor *dirtomon(int dir);
static void drawbar(Monitor *m);
static void drawbars(void);
static int drawstatusbar(Monitor *m, int bh, char *text);
static void enternotify(XEvent *e);
static void expose(XEvent *e);
static void focusin(XEvent *e);
static void focus(Client *c);
static void focusmon(const Arg *arg);
static void focusstack(const Arg *arg);
static void pointerfocuswin(Client *c);
static Atom getatomprop(Client *c, Atom prop);
static int getrootptr(int *x, int *y);
static long getstate(Window w);
static unsigned int getsystraywidth();
static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
static void grabbuttons(Client *c, int focused);
static void grabkeys(void);
static void hide(Client *c);
static void show(Client *c);
static void showtag(Client *c);
static void hidewin(const Arg *arg);
static void hideotherwins(const Arg *arg);
static void showonlyorall(const Arg *arg);
static int issinglewin(const Arg *arg);
static void restorewin(const Arg *arg);
static void toggle_scratchpad(const Arg *arg);
static void show_scratchpad(Client *c);
static void incnmaster(const Arg *arg);
static void keypress(XEvent *e);
static void killclient(const Arg *arg);
static void forcekillclient(const Arg *arg);
static void manage(Window w, XWindowAttributes *wa);
static void mappingnotify(XEvent *e);
static void maprequest(XEvent *e);
static void motionnotify(XEvent *e);
static void movemouse(const Arg *arg);
static void movewin(const Arg *arg);
static void resizewin(const Arg *arg);
static Client *nexttiled(Client *c);
static void pop(Client *);
static void propertynotify(XEvent *e);
static void quit(const Arg *arg);
static void setup(void);
static void seturgent(Client *c, int urg);
static void sigchld(int unused);
static void spawn(const Arg *arg);
static Monitor *systraytomon(Monitor *m);
static Monitor *recttomon(int x, int y, int w, int h);
static void removesystrayicon(Client *i);
static void resize(Client *c, int x, int y, int w, int h, int interact);
static void resizebarwin(Monitor *m);
static void resizeclient(Client *c, int x, int y, int w, int h);
static void resizemouse(const Arg *arg);
static void resizerequest(XEvent *e);
static void restack(Monitor *m);
static void clear_client(Client *fc);
static void run(void);
static void runAutostart(void);
static void scan(void);
static int sendevent(Window w, Atom proto, int m, long d0, long d1, long d2,
long d3, long d4);
static void sendmon(Client *c, Monitor *m);
static void setclientstate(Client *c, long state);
static void setfocus(Client *c);
static void selectlayout(const Arg *arg);
static void setlayout(const Arg *arg);
// static void client_flag_filter(Client *c);
static void fullscreen(const Arg *arg);
static void setfullscreen(Client *c);
static void fake_fullscreen(const Arg *arg);
static void set_fake_fullscreen(Client *c);
static void setmfact(const Arg *arg);
static void tag_client(const Arg *arg,Client *target_client);
static void tag(const Arg *arg);
static void tagmon(const Arg *arg);
static void tagtoleft(const Arg *arg);
static void tagtoright(const Arg *arg);
static void togglebar(const Arg *arg);
static void togglesystray();
static void togglefloating(const Arg *arg);
static void toggleallfloating(const Arg *arg);
static void toggleview(const Arg *arg);
static void toggleoverview(const Arg *arg);
static void togglewin(const Arg *arg);
static void toggleglobal(const Arg *arg);
static void toggleborder(const Arg *arg);
static void unfocus(Client *c, int setfocus);
static void unmanage(Client *c, int destroyed);
static void unmapnotify(XEvent *e);
static void updatebarpos(Monitor *m);
static void updatebars(void);
static void updateclientlist(void);
static int updategeom(void);
static void updatenumlockmask(void);
static void updatesizehints(Client *c);
static void updatestatus(void);
static void updatesystray(void);
static void updatesystrayicongeom(Client *i, int w, int h);
static void updatesystrayiconstate(Client *i, XPropertyEvent *ev);
static void updatetitle(Client *c);
static void updateicon(Client *c);
static void updatewindowtype(Client *c);
static void updatewmhints(Client *c);
static void setgap(const Arg *arg);
static void view(const Arg *arg);
static void viewtoleft(const Arg *arg);
static void viewtoright(const Arg *arg);
static void addtoleft(const Arg *arg);
static void addtoright(const Arg *arg);
static void exchange_client(const Arg *arg);
static void focusdir(const Arg *arg);
static unsigned int get_tags_first_tag(unsigned int tags);
static Client *wintoclient(Window w);
static Monitor *wintomon(Window w);
static Client *wintosystrayicon(Window w);
static int xerror(Display *dpy, XErrorEvent *ee);
static int xerrordummy(Display *dpy, XErrorEvent *ee);
static int xerrorstart(Display *dpy, XErrorEvent *ee);
static void xinitvisual();
static void zoom(const Arg *arg);
static void inner_overvew_toggleoverview(const Arg *arg);
static void inner_overvew_killclient(const Arg *arg);
static void clear_fullscreen_flag(Client *c);
static uint get_border_type(Client *c);
static void toggle_hotarea(int x_root, int y_root);
static void xi_handler(XEvent xevent);
static void overview_restore(Client *c, const Arg *arg);
static void overview_backup(Client *c);
static void fullname_taskbar_activeitem(const Arg *arg);
static unsigned int want_restore_fullscreen(Client *target_client);
static unsigned int want_auto_fullscren(Client *c);
/* variables */
static Systray *systray = NULL;
static const char broken[] = "broken";
static char stext[1024];
static int screen;
static int sw, sh; /* X display screen geometry width, height */
static int bh, blw = 0; /* bar geometry */
static int lrpad; /* sum of left and right padding for text */
static int vp; /* vertical padding for bar */
static int sp; /* side padding for bar */
static int (*xerrorxlib)(Display *, XErrorEvent *);
static unsigned int numlockmask = 0;
static void (*handler[LASTEvent])(
XEvent *) = { // 给捕获的事件定义处理函数,左边是类型,右边是自定义函数
[ButtonPress] = buttonpress, // 按键事件
[ClientMessage] = clientmessage, //窗口给x11服务发送的请求比如全屏请求等
[ConfigureRequest] = configurerequest, //客户端窗口请求大小和位置变化和边框改变,窗口请求不是用户触发
[ConfigureNotify] = configurenotify, //窗口大小和位置变化和边框改变的时候触发
[DestroyNotify] = destroynotify, //销毁事件
[EnterNotify] = enternotify, // 鼠标移动进入窗口事件,比如聚焦的处理
[Expose] = expose, // 窗口暴露事件,由不可见变成可见的时候触发
[FocusIn] = focusin, //聚焦事件
[KeyPress] = keypress, //键盘事件
[MappingNotify] = mappingnotify, //键盘映射发生变化时触发
[MapRequest] = maprequest, //窗口映射触发,只有设置了映射窗口才能被看到
[MotionNotify] = motionnotify, // 监视器事件,比如鼠标移动到某个位置的处理
[PropertyNotify] = propertynotify, //窗口属性改变的时候触发
[ResizeRequest] = resizerequest, //客户端请求重置大小,不是用户触发的
[UnmapNotify] = unmapnotify}; //窗口取消映射的时候触发,比如隐藏和杀死都会触发
static Atom wmatom[WMLast], netatom[NetLast], xatom[XLast];
static int running = 1;
static Cur *cursor[CurLast];
static Clr **scheme;
static Display *dpy;
static int xi_opcode;
static Drw *drw;
static int useargb = 0;
static Visual *visual;
static int depth;
static Colormap cmap;
static Monitor *mons, *selmon;
static Window root, wmcheckwin;
static int hiddenWinStackTop = -1;
static Client *hiddenWinStack[100];
/* configuration, allows nested code to access above variables */
#include "config.h"
#include "icons.h"
struct Pertag {
unsigned int curtag, prevtag; /* current and previous tag */
int nmasters[LENGTH(tags) + 1]; /* number of windows in master area */
float mfacts[LENGTH(tags) + 1]; /* mfacts per tag */
unsigned int sellts[LENGTH(tags) + 1]; /* selected layouts */
const Layout
*ltidxs[LENGTH(tags) + 1][2]; /* matrix of tags and layouts indexes */
int showbars[LENGTH(tags) + 1]; /* display bar for the current tag */
};
/* function implementations */
void logtofile(const char *fmt, ...) {
char buf[256];
char cmd[256];
va_list ap;
va_start(ap, fmt);
vsprintf((char *)buf, fmt, ap);
va_end(ap);
uint i = strlen((const char *)buf);
sprintf(cmd, "echo '%.*s' >> ~/log", i, buf);
system(cmd);
}
/* function implementations */
void lognumtofile(unsigned int num) {
char cmd[256];
sprintf(cmd, "echo '%x' >> ~/log", num);
system(cmd);
}
//获取tags中最坐标的tag的tagmask
unsigned int get_tags_first_tag(unsigned int tags){
unsigned int i,target,tag;
tag = 0;
for(i=0;!(tag & 1);i++){
tag = tags >> i;
}
target = 1 << (i-1);
return target;
}
//获取窗口的进程pid
unsigned long get_win_pid(Window win) {
Atom prop_name = XInternAtom(dpy, "_NET_WM_PID", False);
Atom type;
int format;
unsigned long nitems;
unsigned long bytes_after;
unsigned char *prop = NULL;
unsigned long pid = 0;
if (XGetWindowProperty(dpy, win, prop_name, 0, 1, False, XA_CARDINAL,
&type, &format, &nitems, &bytes_after, &prop) == Success) {
if (prop) {
pid = *((unsigned long *) prop);
XFree(prop);
}
}
return pid;
}
// 扩展输入事件处理函数
static void xi_handler(XEvent xevent) {
Client *pointer_in_client; // 鼠标所在的窗口
XGetEventData(dpy, &xevent.xcookie); // 获取扩展事件cookie对应的事件数据
if (xevent.xcookie.evtype == XI_RawMotion) { // 鼠标移动事件
Window root_return, child_return;
int root_x_return, root_y_return;
int win_x_return, win_y_return;
unsigned int mask_return;
XQueryPointer(dpy, root, &root_return,
&child_return, // 获取鼠标位置和鼠标所在的窗口
&root_x_return, &root_y_return, &win_x_return, &win_y_return,
&mask_return);
toggle_hotarea(root_x_return,
root_y_return); // 判断窗口真全屏状态左下角触发热区
if (child_return != None && mouse_move_toggle_focus == 1) {
pointer_in_client =
wintoclient(child_return); // window对象转换为client对象
focus(pointer_in_client); // 聚焦到鼠标所在的窗口
}
} else if (xevent.xcookie.evtype == XI_RawButtonPress){ //鼠标按键事件
Window root_return, child_return;
int root_x_return, root_y_return;
int win_x_return, win_y_return;
unsigned int mask_return;
XQueryPointer(dpy, root, &root_return,
&child_return, // 获取鼠标位置和鼠标所在的窗口
&root_x_return, &root_y_return, &win_x_return, &win_y_return,
&mask_return);
Atom net_wm_state_skip_pager = //_NET_WM_STATE_SKIP_PAGER一些窗口小部件有这个属性比如eww面板
XInternAtom(dpy, "_NET_WM_STATE_SKIP_PAGER", False); //最后一个参数表示是否系统本来就存在该原子的时候才返回,这里表示可以自定义不管系统有没有
Atom normal_win = //_NET_WM_STATE_SKIP_PAGER一些窗口小部件有这个属性比如eww面板
XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_NORMAL", False); //最后一个参数表示是否系统本来就存在该原子的时候才返回,这里表示可以自定义不管系统有没有
unsigned int isinbacklist = judge_win_contain_state(child_return, net_wm_state_skip_pager);
Atom wtype = getatompropfromwin(child_return, netatom[NetWMWindowType]);
if(!child_return || child_return == selmon->barwin || child_return == systray->win || isinbacklist == 1 || wtype != normal_win){
goto FreeEventData;
}
pointer_in_client =
wintoclient(child_return); // window对象转换为client对象
focus(pointer_in_client); // 聚焦到鼠标所在的窗口
if(pointer_in_client && pointer_in_client->isfloating){
XRaiseWindow(dpy,pointer_in_client->win); //提升浮动窗口到顶层
}
}
FreeEventData:
XFreeEventData(dpy,
&xevent.xcookie); // 释放函数开头get到内存的数据防止内存泄露
}
void applyrules(Client *c) { // 读取config.h的窗口配置规则处理
const char *class, *instance;
unsigned int i;
const Rule *r;
Monitor *m;
XClassHint ch = {NULL, NULL};
/* rule matching */
c->isfloating = 0;
c->overview_isfloatingbak = 0;
c->isglobal = 0;
c->isfullscreen = 0;
c->overview_isfullscreenbak = 0;
c->isnoborder = 0;
c->tags = 0;
XGetClassHint(dpy, c->win, &ch);
class = ch.res_class ? ch.res_class : broken;
instance = ch.res_name ? ch.res_name : broken;
for (i = 0; i < LENGTH(rules); i++) {
r = &rules[i];
// 当rule中定义了一个或多个属性时,只要有一个属性匹配,就认为匹配成功
if ((r->title && !strcmp(c->name, r->title)) ||
(r->class && !strcmp(class, r->class)) ||
(r->instance && !strcmp(instance, r->instance))) {
c->isfloating = r->isfloating;
c->isglobal = r->isglobal;
c->isnoborder = r->isnoborder;
c->tags |= r->tags;
c->bw = c->isnoborder ? 0 : borderpx;
for (m = mons; m && m->num != r->monitor; m = m->next)
;
if (m)
c->mon = m;
// 如果设定了isfloating 且设置窗口的长和宽
if (r->isfloating && r->width != 0 && r->high != 0) {
c->w = r->width;
c->h = r->high;
}
// 如果设定了floatposition 设定窗口位置
if (r->isfloating && r->floatposition != 0) {
switch (r->floatposition) {
case 1:
c->x = selmon->wx + gappo;
c->y = selmon->wy + gappo;
break; // 左上
case 2:
c->x = selmon->wx + (selmon->ww - WIDTH(c)) / 2 - gappo;
c->y = selmon->wy + gappo;
break; // 中上
case 3:
c->x = selmon->wx + selmon->ww - WIDTH(c) - gappo;
c->y = selmon->wy + gappo;
break; // 右上
case 4:
c->x = selmon->wx + gappo;
c->y = selmon->wy + (selmon->wh - HEIGHT(c)) / 2;
break; // 左中
case 0: // 默认0,居中
case 5:
c->x = selmon->wx + (selmon->ww - WIDTH(c)) / 2;
c->y = selmon->wy + (selmon->wh - HEIGHT(c)) / 2;
break; // 中中
case 6:
c->x = selmon->wx + selmon->ww - WIDTH(c) - gappo;
c->y = selmon->wy + (selmon->wh - HEIGHT(c)) / 2;
break; // 右中
case 7:
c->x = selmon->wx + gappo;
c->y = selmon->wy + selmon->wh - HEIGHT(c) - gappo;
break; // 左下
case 8:
c->x = selmon->wx + (selmon->ww - WIDTH(c)) / 2;
c->y = selmon->wy + selmon->wh - HEIGHT(c) - gappo;
break; // 中下
case 9:
c->x = selmon->wx + selmon->ww - WIDTH(c) - gappo;
c->y = selmon->wy + selmon->wh - HEIGHT(c) - gappo;
break; // 右下
}
}
break; // 有且只会匹配一个第一个符合的rule
}
}
if (ch.res_class)
XFree(ch.res_class);
if (ch.res_name)
XFree(ch.res_name);
c->tags =
c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
}
int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact) {
int baseismin;
Monitor *m = c->mon;
/* set minimum possible */
*w = MAX(1, *w);
*h = MAX(1, *h);
if (interact) {
if (*x > sw)
*x = sw - WIDTH(c);
if (*y > sh)
*y = sh - HEIGHT(c);
if (*x + *w + 2 * c->bw < 0)
*x = 0;
if (*y + *h + 2 * c->bw < 0)
*y = 0;
} else {
if (*x >= m->wx + m->ww)
*x = m->wx + m->ww - WIDTH(c);
if (*y >= m->wy + m->wh)
*y = m->wy + m->wh - HEIGHT(c);
if (*x + *w + 2 * c->bw <= m->wx)
*x = m->wx;
if (*y + *h + 2 * c->bw <= m->wy)
*y = m->wy;
}
if (*h < bh)
*h = bh;
if (*w < bh)
*w = bh;
if (c->isfloating) {
/* see last two sentences in ICCCM 4.1.2.3 */
baseismin = c->basew == c->minw && c->baseh == c->minh;
if (!baseismin) { /* temporarily remove base dimensions */
*w -= c->basew;
*h -= c->baseh;
}
/* adjust for aspect limits */
if (c->mina > 0 && c->maxa > 0) {
if (c->maxa < (float)*w / *h)
*w = *h * c->maxa + 0.5;
else if (c->mina < (float)*h / *w)
*h = *w * c->mina + 0.5;
}
if (baseismin) { /* increment calculation requires this */
*w -= c->basew;
*h -= c->baseh;
}
/* adjust for increment value */
if (c->incw)
*w -= *w % c->incw;
if (c->inch)
*h -= *h % c->inch;
/* restore base dimensions */
*w = MAX(*w + c->basew, c->minw);
*h = MAX(*h + c->baseh, c->minh);
if (c->maxw)
*w = MIN(*w, c->maxw);
if (c->maxh)
*h = MIN(*h, c->maxh);
}
return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
}
void arrange(Monitor *m) { // 布局管理
if (m)
showtag(m->stack);
else
for (m = mons; m; m = m->next)
showtag(m->stack);
if (m) {
arrangemon(m);
restack(m);
} else
for (m = mons; m; m = m->next)
arrangemon(m);
}
void arrangemon(Monitor *m) { // 确认选用的布局
if (m->isoverview) {
strncpy(m->ltsymbol, overviewlayout.symbol, sizeof overviewlayout.symbol);
overviewlayout.arrange(m);
} else {
strncpy(m->ltsymbol, m->lt[m->sellt]->symbol,
sizeof m->lt[m->sellt]->symbol);
m->lt[m->sellt]->arrange(m);
}
}
void attach(Client *c,int inserthead) { // 新打开的窗口放入窗口链表中
Client **tc;
for (tc = &c->mon->clients; *tc; tc = &(*tc)->next){
// 如果当前的tag中有新创建的窗口,就让当前tag中的全屏窗口退出全屏参与平铺
if((*tc) && !c->isfloating && (*tc)->tags & c->tags){
clear_fullscreen_flag(*tc);
}
}
if (!inserthead) {
*tc = c;
c->next = NULL;
} else {
c->next = c->mon->clients;
c->mon->clients = c;
}
}
void attachstack(Client *c) { // 放入栈中
c->snext = c->mon->stack;
c->mon->stack = c;
}
void buttonpress(XEvent *e) { // 鼠标按键事件处理函数
unsigned int i, x, click, occ = 0;
Arg arg = {0};
Monitor *m;
XButtonPressedEvent *ev = &e->xbutton;
Client *c = wintoclient(ev->window);
// 滚轮加按键事件直接操作,不进行下面鼠标位置判断
for (i = 0; i < LENGTH(buttons); i++) {
if (buttons[i].func && buttons[i].button == ev->button &&
(ev->button == Button4 || ev->button == Button5) &&
CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state) &&
CLEANMASK(buttons[i].mask) != 0) {
buttons[i].func(&buttons[i].arg);
return;
}
}
// 判断鼠标点击的位置
click = ClkRootWin;
/* focus monitor if necessary */
if ((m = wintomon(ev->window)) && m != selmon) {
unfocus(selmon->sel, 1);
selmon = m;
focus(NULL);
}
int status_w = selmon->status_w;
int system_w = getsystraywidth();
if (ev->window == selmon->barwin ||
(!c && selmon->showbar &&
(topbar ? ev->y <= selmon->wy
: ev->y >= selmon->wy + selmon->wh))) { // 点击在bar上
i = x = 0;
blw = TEXTW(selmon->ltsymbol);
if (selmon->isoverview) {
x += TEXTW(overviewtag);
i = ~0;
if (ev->x > x)
i = LENGTH(tags);
} else {
for (c = m->clients; c; c = c->next)
occ |= c->tags == TAGMASK ? 0 : c->tags;
do {
/* do not reserve space for vacant tags */
if (!(occ & 1 << i || m->tagset[m->seltags] & 1 << i))
continue;
x += TEXTW(tags[i]);
} while (ev->x >= x && ++i < LENGTH(tags));
}
if (i < LENGTH(tags)) {
click = ClkTagBar;
arg.ui = 1 << i;
} else if (ev->x < x + blw)
click = ClkLtSymbol;
else if (ev->x > selmon->ww - status_w - 2 * sp -
(selmon == systraytomon(selmon)
? (system_w ? system_w + systraypinning + 2 : 0)
: 0)) {
click = ClkStatusText;
arg.i = ev->x - (selmon->ww - status_w - 2 * sp -
(selmon == systraytomon(selmon)
? (system_w ? system_w + systraypinning + 2 : 0)
: 0));
arg.ui = ev->button; // 1 => L,2 => M,3 => R, 5 => U, 6 => D
} else {
click = ClkBarEmpty;
x += blw;
c = m->clients;
if (m->bt != 0)
do {
if (!ISVISIBLE(c))
continue;
else
x += c->taskw;
} while (ev->x > x && (c = c->next));
if (c) {
click = ClkWinTitle;
arg.v = c;
}
}
} else if ((c = wintoclient(ev->window))) {
focus(c);
restack(selmon);
// 这句代码好像是多余的,因为不停止捕获,重新发送的事件还是会被再次拦截捕获,不会传到原本的客户端
XAllowEvents(dpy, ReplayPointer, CurrentTime);
click = ClkClientWin;
}
// 增加ClkRootWin也可以触发鼠标按键事件,这样tab无窗口才能使用鼠标中键加滚轮切换tab
for (i = 0; i < LENGTH(buttons); i++)
if (click == buttons[i].click && buttons[i].func &&
buttons[i].button == ev->button &&
CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state)) {
buttons[i].func((click == ClkTagBar || click == ClkRootWin ||
click == ClkWinTitle || click == ClkStatusText) &&
buttons[i].arg.i == 0
? &arg
: &buttons[i].arg);
}
}
void checkotherwm(void) {
xerrorxlib = XSetErrorHandler(xerrorstart);
/* this causes an error if some other window manager is running */
XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
XSync(dpy, False);
XSetErrorHandler(xerror);
XSync(dpy, False);
}
void cleanup(void) {
Arg a = {.ui = ~0};
Layout foo = {"", NULL};
Monitor *m;
size_t i;
view(&a);
selmon->lt[selmon->sellt] = &foo;
for (m = mons; m; m = m->next){
while (m->stack){
unmanage(m->stack, 0);
}
}
XUngrabKey(dpy, AnyKey, AnyModifier, root);
while (mons)
cleanupmon(mons);
if (showsystray) {
XUnmapWindow(dpy, systray->win);
XDestroyWindow(dpy, systray->win);
free(systray);
}
for (i = 0; i < CurLast; i++)
drw_cur_free(drw, cursor[i]);
for (i = 0; i < LENGTH(colors) + 1; i++)
free(scheme[i]);
XDestroyWindow(dpy, wmcheckwin);
drw_free(drw);
XSync(dpy, False);
XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
}
void cleanupmon(Monitor *mon) {
Monitor *m;
if (mon == mons)
mons = mons->next;
else {
for (m = mons; m && m->next != mon; m = m->next)
;
m->next = mon->next;
}
XUnmapWindow(dpy, mon->barwin);
XDestroyWindow(dpy, mon->barwin);
free(mon);
}
void clientmessage(XEvent *e) {
XWindowAttributes wa;
XSetWindowAttributes swa;
XClientMessageEvent *cme = &e->xclient;
Client *c = wintoclient(cme->window);
Atom atom_max_horz = XInternAtom(dpy, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
Atom atom_max_vert = XInternAtom(dpy, "_NET_WM_STATE_MAXIMIZED_VERT", False);