Repository navigation
Expand file tree
/
Copy pathdwm.c
More file actions
6027 lines (5310 loc) · 153 KB
/
Copy pathdwm.c
File metadata and controls
6027 lines (5310 loc) · 153 KB
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 <errno.h>
#include <locale.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdint.h>
#include <limits.h>
#include <unistd.h>
#include <dirent.h>
#include <fnmatch.h>
#include <pthread.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/sysinfo.h>
#include <sys/wait.h>
#include <X11/extensions/Xrender.h>
#include <X11/extensions/Xcomposite.h>
#include <X11/extensions/shape.h>
#include <X11/cursorfont.h>
#include <X11/keysym.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <X11/Xutil.h>
#ifdef XINERAMA
#include <X11/extensions/Xinerama.h>
#endif /* XINERAMA */
#include <X11/Xft/Xft.h>
#include "drw.h"
#include "util.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 MOUSEMASK (BUTTONMASK|PointerMotionMask)
#define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
#define TEXTWSTATUS(X) (drw_fontset_getwidth(statusdrw, (X)) + lrpad)
#define ISVISIBLE(C) ((C) && (C)->mon && (C)->mon->scrollindex && (C)->tagindex >= 0 && (C)->tagindex < LENGTH(tags) && (C)->mon->scrollindex == &(C)->mon->scrolls[(C)->tagindex])
#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
/* status bar macros */
#define NODE_NUM 100
/* enums */
enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
enum { SchemeNorm, SchemeSel, SchemeFG, SchemeBlue, SchemeGreen, SchemeOrange, SchemeRed, SchemeYellow }; /* color schemes */
enum { NetSupported, NetWMName, NetWMState, NetWMCheck,
NetSystemTray, NetSystemTrayOP, NetSystemTrayOrientation, NetSystemTrayOrientationHorz,
NetWMFullscreen, NetActiveWindow, NetWMWindowType,
NetWMWindowTypeDialog, NetWMWindowTypeDesktop, NetClientList, NetLast }; /* EWMH atoms */
enum { Manager, Xembed, XembedInfo, XRootPmap, XSetRoot, XLast }; /* Xembed/root atoms */
enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle, ClkNullWinTitle,
ClkWinClass, ClkSuperIcon, ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
/* preview layout mode enum */
enum {
PREVIEW_SCROLL,
PREVIEW_GRID,
};
/* status bar blocks enum */
enum {
Notify,
Battery,
Clock,
Net,
Mem,
Cpu,
Cores,
Temp,
More,
};
typedef union {
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];
float mina, maxa;
int floatx, floaty;
int x, y, w, h;
int oldx, oldy, oldw, oldh;
int basew, baseh, incw, inch, maxw, maxh, minw, minh, hintsvalid;
int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
int ignoreunmap;
float mfact;
int tagindex;
Client *next; /* next pointer for scroll layout linked lists */
Client *snext;
Monitor *mon;
Window win;
};
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 Scroll Scroll;
struct Scroll {
Client *head;
int x;
int singlefill; /* 1 表示单窗铺满,0 表示居中缩小 */
};
struct Monitor {
char ltsymbol[16];
int num;
int by; /* bar geometry */
int mx, my, mw, mh; /* screen size */
int wx, wy, ww, wh; /* window area */
unsigned int sellt;
int showbar;
int topbar;
Client *sel;
Client *stack;
Monitor *next;
Window barwin;
Window container;
const Layout *lt[2];
Scroll *scrolls;
Scroll *scrollindex;
int prevtag; /* 上次使用的tag索引 */
int logotitlew; /* logotitle的实际宽度 */
};
typedef struct {
const char *class;
const char *instance;
const char *title;
int tagindex;
int isfloating;
int monitor;
} Rule;
typedef struct {
const char *class;
const char *instance;
const char *title;
int override_redirect; /* -1: any, 0: normal, 1: override-redirect */
int radius;
} CornerRule;
typedef struct Systray Systray;
struct Systray {
Window win;
Client *icons;
};
/* status bar block struct*/
typedef struct Block Block;
struct Block {
int bw;
void *storage;
int (*draw)(int x, Block *block, unsigned int timer);
void (*click)(const Arg *arg);
};
typedef struct {
unsigned long user;
unsigned long nice;
unsigned long system;
unsigned long idle;
} Cpuload;
typedef struct Node Node;
struct Node {
Node *prev;
Node *next;
Cpuload *data;
};
/*status cpu block struct */
typedef struct {
Cpuload *prev;
Cpuload *curr;
Node *pointer;
} CpuBlock;
typedef struct {
Cpuload *prev;
Cpuload *curr;
} CoreBlock;
typedef struct {
int x;
int y;
int w;
int h;
} MonitorArea;
/* function declarations */
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);
static void detach(Client *c);
static void attachstack(Client *c);
static void buttonpress(XEvent *e);
static void checkotherwm(void);
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 void configurerequest(XEvent *e);
static Monitor *createmon(void);
static void destroynotify(XEvent *e);
static void detach(Client *c);
static void detachstack(Client *c);
static void drawbar(Monitor *m);
static void drawbars(void);
static void enternotify(XEvent *e);
static void expose(XEvent *e);
static void ensureclientvisible(Client *c, int minw, int minh);
static void focus(Client *c);
static void focusin(XEvent *e);
static void focusonclick(const Arg *arg);
static void focusstep(const Arg *arg);
static void focusstepvisible(const Arg *arg);
static Atom getatomprop(Client *c, Atom prop);
static int getatompropvalue(Client *c, Atom prop, Atom *value);
static int getrootptr(int *x, int *y);
static long getstate(Window w);
static unsigned int getsystraywidth(void);
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 keypress(XEvent *e);
static void killclient(const Arg *arg);
static void manage(Window w, XWindowAttributes *wa);
static void mappingnotify(XEvent *e);
static void maprequest(XEvent *e);
static void mapnotify(XEvent *e);
static void motionnotify(XEvent *e);
static void movemouse(const Arg *arg);
static void reorderbyx(Scroll *s);
static void propertynotify(XEvent *e);
static void quit(const Arg *arg);
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 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 setfullscreen(Client *c, int fullscreen);
static void setlayout(const Arg *arg);
static void setmfact(const Arg *arg);
static void scrollmove(const Arg *arg);
static void setup(void);
static void seturgent(Client *c, int urg);
static void setcontainertitle(Monitor *m);
static int clampcornerradius(int r, int w, int h);
static void clearwindowshape(Window win);
static int setwindowrounded(Window win, int w, int h, int r);
static void applyroundedcorners(Window win);
static void setroundedfromattrs(Window win, XWindowAttributes *wa);
static void drawroundedmask(Pixmap mask, GC gc, int x, int y, int w, int h, int r, int val);
static void setupborderwin(void);
static void updateborderwin(void);
static void previewscroll(const Arg *arg);
static XImage *getwindowximage(Client *c);
static XImage *getwindowximage_safe(Client *c);
static XImage *create_placeholder_image(unsigned int w, unsigned int h);
static XImage *scaleimage(XImage *src, unsigned int nw, unsigned int nh);
static void showhide(Monitor *m);
static void spawn(const Arg *arg);
static Monitor *systraytomon(Monitor *m);
static void tag(const Arg *arg);
static void togglebar(const Arg *arg);
static void togglefloating(const Arg *arg);
static void ensureselectedvisible(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 MonitorArea *buildmonitorareas(int *targetcount);
static void movelastmonclients(Monitor *src, Monitor *dst, int *dirty);
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 updatewindowtype(Client *c);
static void updatewmhints(Client *c);
static void view(const Arg *arg);
static void viewlast(const Arg *arg);
static void setdefaultfont(void);
static void setsmallfont(void);
static void setstatusdefaultfont(void);
static void setstatussmallfont(void);
static void synccontainerbg(Monitor *m);
static void synccontainerbgs(void);
static void scrolltogglesize(const Arg *arg);
static void drawsupericon(Monitor *m, int *x);
static void drawlogotitle(Monitor *m, int *x);
static void drawtags(Monitor *m, int *x);
static void drawlayout(Monitor *m, int *x);
static void drawclienttabs(Monitor *m, int x, int w, int n);
static void togglesupericon(const Arg *arg);
static void scroll(Monitor *m);
static void scrollmoveothers(const Arg *arg);
static void initshape(void);
static void initcompositor(void);
static void redirectmonitor(Monitor *m);
/* status bar functions */
static void clickmore(const Arg *arg);
static void clickmem(const Arg *arg);
static void clicktemp(const Arg *arg);
static void clicknet(const Arg *arg);
static void clicknotify(const Arg *arg);
static void clickcpu(const Arg *arg);
static void clickcores(const Arg *arg);
static int drawstatusclock(int x, Block *block, unsigned int timer);
static int drawnet(int x, Block *block, unsigned int timer);
static int drawbattery(int x, Block *block, unsigned int timer);
static int drawnotify(int x, Block *block, unsigned int timer);
static int drawcpu(int x, Block *block, unsigned int timer);
static int drawcores(int x, Block *block, unsigned int timer);
static int drawtemp(int x, Block *block, unsigned int timer);
static int drawmem(int x, Block *block, unsigned int timer);
static int drawmore(int x, Block *block, unsigned int timer);
static int getstatuswidth(void);
static int readullfromfile(const char *path, unsigned long long *value);
static int readwordfromfile(const char *path, char *buf, size_t bufsz);
static void formatspeed(double value, char *buf, size_t bufsz);
static void spawnclickcmd(const char *const cmd[]);
static void handleStatus1(const Arg *arg);
static void handleStatus2(const Arg *arg);
static void handleStatus3(const Arg *arg);
static void handleStatus4(const Arg *arg);
static void handleStatus5(const Arg *arg);
static int gettempnums(void);
static void initstatusbar(void);
static void *drawstatusbar(void *arg);
static void cleanstatuspthread(void);
static void updatestatuscache(void);
static void freestatuscache(void);
static void handlestatusclick(const Arg *arg, int button);
static void sendnotify(const char *msg, const char *urgency, int timeout);
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);
/* variables */
static Systray *systray = NULL;
static const char broken[] = "broken";
static char stext[256];
static int screen;
static int sw, sh; /* X display screen geometry width, height */
static int bh; /* bar height */
static int lrpad; /* sum of left and right padding for text */
static int supericonw;
static int systandstat; /* right padding for systray */
static int systrayw;
static int supericonflag = 1;
static int modkey_enabled = 1;
static int (*xerrorxlib)(Display *, XErrorEvent *);
static unsigned int numlockmask = 0;
/* status bar global variables */
static pthread_t drawstatusthread;
static int status_thread_started = 0;
static int status_thread_running = 0;
static pthread_mutex_t statuscache_mutex = PTHREAD_MUTEX_INITIALIZER;
static Node Nodes[NODE_NUM];
static int numCores;
static int thermalzoneindex = 0;
static int thermalzonenum = 0;
static int interfaceindex = 0;
static CoreBlock storagecores;
static CpuBlock storagecpu;
static double storagenet[2] = {0.0, 0.0};
static Block Blocks[] = {
[Notify] = {0, NULL, drawnotify, clicknotify},
[Battery] = {0, NULL, drawbattery, NULL},
[Clock] = {0, NULL, drawstatusclock, NULL},
[Net] = {0, &storagenet, drawnet, clicknet},
[Cpu] = {0, &storagecpu, drawcpu, clickcpu},
[Cores] = {0, &storagecores, drawcores, clickcores},
[Temp] = {0, NULL, drawtemp, clicktemp},
[Mem] = {0, NULL, drawmem, clickmem},
[More] = {0, NULL, drawmore, clickmore},
};
static void (*handler[LASTEvent]) (XEvent *) = {
[ButtonPress] = buttonpress,
[ClientMessage] = clientmessage,
[ConfigureRequest] = configurerequest,
[ConfigureNotify] = configurenotify,
[DestroyNotify] = destroynotify,
[EnterNotify] = enternotify,
[Expose] = expose,
[FocusIn] = focusin,
[KeyPress] = keypress,
[MappingNotify] = mappingnotify,
[MapNotify] = mapnotify,
[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 Drw *drw;
static Drw *statusdrw; /* 状态栏绘制上下文 */
static Monitor *mons, *selmon;
static Window root, wmcheckwin;
static int composite_supported = 0;
static int shape_supported = 0;
static Window borderwin = None;
/* status bar cache */
static Pixmap statuscache = None;
static int cachew = 0;
static int cacheh = 0;
static int cachevalid = 0;
static time_t lastupdate = 0;
/* preview mode */
static int previewmode = PREVIEW_SCROLL; /* will be initialized from config */
/* configuration, allows nested code to access above variables */
#include "config.h"
/* function implementations */
void
applyrules(Client *c)
{
const char *class, *instance;
unsigned int i;
const Rule *r;
Monitor *m;
XClassHint ch = { NULL, NULL };
/* rule matching */
c->isfloating = 0;
c->tagindex = -1; // 初始化为无效值
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];
if ((!r->title || strstr(c->name, r->title))
&& (!r->class || strstr(class, r->class))
&& (!r->instance || strstr(instance, r->instance)))
{
c->isfloating = r->isfloating;
c->tagindex = r->tagindex;
for (m = mons; m && m->num != r->monitor; m = m->next);
if (m)
c->mon = m;
}
}
if (ch.res_class)
XFree(ch.res_class);
if (ch.res_name)
XFree(ch.res_name);
// 如果没有匹配规则,使用当前monitor的scrollindex对应的索引
if (c->tagindex == -1) {
for (int i = 0; i < LENGTH(tags); i++) {
if (c->mon->scrollindex == &c->mon->scrolls[i]) {
c->tagindex = i;
break;
}
}
}
/* 防御越界的 tagindex,避免被 attach/ISVISIBLE 丢弃 */
if (c->tagindex < 0 || c->tagindex >= LENGTH(tags)) {
if (c->mon->scrollindex)
c->tagindex = c->mon->scrollindex - c->mon->scrolls;
if (c->tagindex < 0 || c->tagindex >= LENGTH(tags))
c->tagindex = 0;
}
}
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 - c->w;
if (*y > sh)
*y = sh - c->h;
if (*x + *w < 0)
*x = 0;
if (*y + *h < 0)
*y = 0;
} else {
if (*x >= m->wx + m->ww)
*x = m->wx + m->ww - c->w;
if (*y >= m->wy + m->wh)
*y = m->wy + m->wh - c->h;
if (*x + *w <= m->wx)
*x = m->wx;
if (*y + *h <= m->wy)
*y = m->wy;
}
if (*h < bh)
*h = bh;
if (*w < bh)
*w = bh;
if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
if (!c->hintsvalid)
updatesizehints(c);
/* 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)
showhide(m);
else for (m = mons; m; m = m->next)
showhide(m);
if (m) {
arrangemon(m);
restack(m);
} else for (m = mons; m; m = m->next)
arrangemon(m);
updateborderwin();
}
void
arrangemon(Monitor *m)
{
snprintf(m->ltsymbol, sizeof m->ltsymbol, "%s", m->lt[m->sellt]->symbol);
if (m->lt[m->sellt]->arrange)
m->lt[m->sellt]->arrange(m);
}
void
attach(Client *c)
{
Client *last;
int i = c->tagindex;
if (i < 0 || i >= LENGTH(tags) || !c->mon)
return;
/* Insert after current selection when possible (non-floating preference) */
if (!c->isfloating && c->mon->sel && c->mon->sel->tagindex == i) {
Client **pp = &c->mon->scrolls[i].head;
for (; *pp && *pp != c->mon->sel; pp = &(*pp)->next);
if (*pp == c->mon->sel) {
c->next = c->mon->sel->next;
c->mon->sel->next = c;
return;
}
}
/* Find the last client in this tagindex's scroll list */
last = NULL;
Client *curr;
for (curr = c->mon->scrolls[i].head; curr; curr = curr->next)
last = curr;
/* Append client to the appropriate tagindex list */
if (last) {
last->next = c;
} else {
c->mon->scrolls[i].head = c;
}
c->next = NULL;
}
void
attachstack(Client *c)
{
c->snext = c->mon->stack;
c->mon->stack = c;
}
void
buttonpress(XEvent *e)
{
unsigned int i, x, click;
Arg arg = {0};
Client *c;
Monitor *m;
XButtonPressedEvent *ev = &e->xbutton;
click = ClkRootWin;
/* focus monitor if necessary */
if ((m = wintomon(ev->window)) && m != selmon) {
unfocus(selmon->sel, 1);
selmon = m;
focus(NULL);
}
if (ev->window == selmon->barwin) {
i = 0;
x = supericonw + selmon->logotitlew;
do
x += TEXTW(tags[i]);
while (ev->x >= x && ++i < LENGTH(tags));
if (ev->x < supericonw) {
click = ClkSuperIcon;
} else if (ev->x < supericonw + selmon->logotitlew) {
click = ClkWinClass;
} else if (i < LENGTH(tags)) {
click = ClkTagBar;
arg.i = i;
} else if (ev->x < x + TEXTW(selmon->ltsymbol)) {
click = ClkLtSymbol;
} else if (ev->x > selmon->ww - systandstat) {
click = ClkStatusText;
/* 确定点击的是哪个status组件 */
int stbsw = 0;
int stx = selmon->ww - ev->x - systrayw;
for (i = 0; i < LENGTH(Blocks); i++) {
stbsw += Blocks[i].bw;
if (stbsw > stx) {
arg.i = i;
break;
}
}
} else {
x += TEXTW(selmon->ltsymbol);
int n = 0;
for (c = m->scrollindex->head; c; c = c->next)
n++;
if (m->scrollindex->head && n > 0) {
c = m->scrollindex->head;
int tabw = (selmon->ww - systandstat - x) / n;
do {
x += tabw;
} while (ev->x > x && (c = c->next));
click = ClkWinTitle;
arg.v = c;
} else {
click = ClkNullWinTitle;
}
}
} else if ((c = wintoclient(ev->window))) {
focus(c);
restack(selmon);
XAllowEvents(dpy, ReplayPointer, CurrentTime);
click = ClkClientWin;
}
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 == ClkWinTitle || click == ClkStatusText) &&
buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
}
static void
ensureclientvisible(Client *c, int minw, int minh)
{
if (!c || !c->mon || !c->mon->scrollindex)
return;
Monitor *m = c->mon;
if (m->lt[m->sellt]->arrange != scroll)
return;
if (minw < 1)
minw = 1;
int cw = c->w;
if (minw > cw)
minw = cw;
int ch = c->h;
if (minh > ch)
minh = ch;
/* shrink visible band by gaps so checks match layout space */
int view_left = m->wx + gappx;
int view_right = m->wx + m->ww - gappx;
int view_top = m->wy + scrollstartgap + gappx;
int view_bottom = m->wy + m->wh - scrollstartgap - gappx;
int cleft = c->x;
int cright = c->x + cw;
int ctop = c->y;
int cbottom = c->y + ch;
int visible_left = cleft > view_left ? cleft : view_left;
int visible_right = cright < view_right ? cright : view_right;
int visible_top = ctop > view_top ? ctop : view_top;
int visible_bottom = cbottom < view_bottom ? cbottom : view_bottom;
int visw = visible_right - visible_left;
int vish = visible_bottom - visible_top;
if (visw >= minw && vish >= minh)
return;
int delta = 0;
if (cleft < view_left) {
delta = cleft - view_left;
} else if (cleft + minw > view_right) {
delta = cleft + minw - view_right;
} else if (cright > view_right) {
delta = cright - view_right;
}
if (delta != 0) {
Arg a = { .i = delta };
scrollmove(&a);
}
/* Vertical visibility fix for floating clients */
if (vish < minh && c->isfloating) {
int shortage = minh - vish;
if (ctop < view_top && cbottom <= view_bottom) {
int dy = shortage;
if (cbottom + dy > view_bottom)
dy = view_bottom - cbottom;
if (dy > 0)
resize(c, c->x, c->y + dy, c->w, c->h, 0);
} else if (cbottom > view_bottom && ctop >= view_top) {
int dy = shortage;
if (ctop - dy < view_top)
dy = ctop - view_top;
if (dy > 0)
resize(c, c->x, c->y - dy, c->w, c->h, 0);
} else {
/* fallback: clamp to viewport band */
int newy = c->y;
if (newy < view_top)
newy = view_top;
if (newy + minh > view_bottom)
newy = view_bottom - minh;
if (newy != c->y)
resize(c, c->x, newy, c->w, c->h, 0);
}
}
}
void
focusonclick(const Arg *arg)
{
Client *c = (Client *)arg->v;
if (!c)
return;
if (c->mon != selmon) {
unfocus(selmon->sel, 1);
selmon = c->mon;
}
if (!c->isfullscreen)
ensureclientvisible(c, c->w, 50);
focus(c);
restack(selmon);
}
void
focusstep(const Arg *arg)
{
if (!selmon || !selmon->scrollindex)
return;
int dir = arg->i;
if (dir == 0)
return;
Client *head = selmon->scrollindex->head;
Client *sel = selmon->sel;
Client *target = NULL;
if (!head)
return;
if (dir > 0) {
Client *start = sel ? sel->next : head;
for (Client *it = start; it; it = it->next)
if (!it->isfloating) {
target = it;
break;
}
} else { /* dir < 0 */
Client *prev = NULL;
if (sel) {
for (Client *it = head; it && it != sel; it = it->next)
if (!it->isfloating)
prev = it;
target = prev;
} else {
for (Client *it = head; it; it = it->next)
if (!it->isfloating)
target = it;
}
}
if (target && target != selmon->sel) {
focus(target);
restack(selmon);
}
}
void
focusstepvisible(const Arg *arg)
{
focusstep(arg);
ensureselectedvisible(arg);
}
static XImage *
scaleimage_sw(XImage *src, unsigned int nw, unsigned int nh)
{
if (!src || nw == 0 || nh == 0)
return NULL;
/* 防止源图像尺寸为0 */
if (src->width == 0 || src->height == 0)
return NULL;
/* 防止过大的目标尺寸导致内存问题 */
const unsigned int MAX_DIM = 16384;
if (nw > MAX_DIM || nh > MAX_DIM)
return NULL;
/* 检查缩放比例是否合理,防止极端情况 */
if (nw > src->width * 10 || nh > src->height * 10)
return NULL;
XImage *dst = XCreateImage(dpy, DefaultVisual(dpy, screen),
DefaultDepth(dpy, screen), ZPixmap, 0, NULL, nw, nh, 32, 0);
if (!dst)
return NULL;
/* 检查 bytes_per_line 是否合理 */
if (dst->bytes_per_line == 0 || dst->bytes_per_line > 65536) {
XDestroyImage(dst);
return NULL;
}
/* 分配内存,检查是否成功 */
dst->data = ecalloc(1, nh * dst->bytes_per_line);
if (!dst->data) {
XDestroyImage(dst);
return NULL;
}
/* 执行缩放,添加边界检查 */
for (unsigned int y = 0; y < nh; y++) {
unsigned int sy = (unsigned int)((uint64_t)y * src->height / nh);
if (sy >= src->height)
sy = src->height - 1;
for (unsigned int x = 0; x < nw; x++) {
unsigned int sx = (unsigned int)((uint64_t)x * src->width / nw);
if (sx >= src->width)
sx = src->width - 1;
XPutPixel(dst, x, y, XGetPixel(src, sx, sy));
}
}
return dst;
}
static XImage *
scaleimage(XImage *src, unsigned int nw, unsigned int nh)
{
if (!src || nw == 0 || nh == 0)
return NULL;
/* 防止源图像尺寸为0 */
if (src->width == 0 || src->height == 0)
return NULL;