-
Notifications
You must be signed in to change notification settings - Fork 0
/
focus.c
131 lines (115 loc) · 3.51 KB
/
focus.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
#include "atlas.h"
#include "config.h"
#include <X11/Xatom.h>
void focus(Client *c) {
if (!c || !ISVISIBLE(c))
for (c = selectedMonitor->stack; c && !ISVISIBLE(c); c = c->nextInStack)
;
if (selectedMonitor->active && selectedMonitor->active != c)
unfocus(selectedMonitor->active, 0);
if (c) {
if (c->monitor != selectedMonitor)
selectedMonitor = c->monitor;
if (c->isUrgent)
setWindowUrgent(c, 0);
detachWindowFromStack(c);
attachWindowToStack(c);
registerMouseButtons(c, 1);
Clr borderColor;
drw_clr_create(drawContext, &borderColor, cfg.borderActiveColor);
XSetWindowBorder(display, c->win, borderColor.pixel);
setfocus(c);
} else {
XSetInputFocus(display, root, RevertToPointerRoot, CurrentTime);
XDeleteProperty(display, root, netAtoms[NET_ACTIVE_WINDOW]);
}
selectedMonitor->active = c;
}
void unfocus(Client *c, int setfocus) {
if (!c)
return;
registerMouseButtons(c, 0);
Clr borderColor;
drw_clr_create(drawContext, &borderColor, cfg.borderInactiveColor);
XSetWindowBorder(display, c->win, borderColor.pixel);
if (setfocus) {
XSetInputFocus(display, root, RevertToPointerRoot, CurrentTime);
XDeleteProperty(display, root, netAtoms[NET_ACTIVE_WINDOW]);
}
}
void focusMonitor(const Arg *arg) {
Monitor *m;
if (!monitors->next)
return;
if ((m = findMonitorInDirection(arg->i)) == selectedMonitor)
return;
unfocus(selectedMonitor->active, 0);
selectedMonitor = m;
// Check if there are any visible clients on the monitor
Client *c = NULL;
for (Client *t = m->clients; t; t = t->next) {
if (ISVISIBLE(t)) {
c = t;
break;
}
}
if (c) {
// Found a visible client, focus it and move cursor to its center
focus(c);
moveCursorToClientCenter(c);
} else {
// No clients, just focus monitor and move cursor to monitor center
focus(NULL);
// Calculate monitor center coordinates
int x = m->mx + (m->mw / 2);
int y = m->my + (m->mh / 2);
// Move cursor to monitor center
XWarpPointer(display, None, root, 0, 0, 0, 0, x, y);
XFlush(display);
}
}
void focusstack(const Arg *arg) {
Client *c = NULL, *i;
if (!selectedMonitor->active ||
(selectedMonitor->active->isFullscreen && cfg.lockFullscreen))
return;
if (arg->i > 0) {
for (c = selectedMonitor->active->next; c && !ISVISIBLE(c); c = c->next)
;
if (!c)
for (c = selectedMonitor->clients; c && !ISVISIBLE(c); c = c->next)
;
} else {
for (i = selectedMonitor->clients; i != selectedMonitor->active;
i = i->next)
if (ISVISIBLE(i))
c = i;
if (!c)
for (; i; i = i->next)
if (ISVISIBLE(i))
c = i;
}
if (c) {
focus(c);
moveCursorToClientCenter(c);
restack(selectedMonitor);
}
}
void setfocus(Client *c) {
if (!c->neverFocus) {
XSetInputFocus(display, c->win, RevertToPointerRoot, CurrentTime);
XChangeProperty(display, root, netAtoms[NET_ACTIVE_WINDOW], XA_WINDOW, 32,
PropModeReplace, (unsigned char *)&(c->win), 1);
}
sendevent(c, wmAtoms[WM_TAKE_FOCUS]);
}
void moveCursorToClientCenter(Client *c) {
if (!c || !cfg.moveCursorWithFocus)
return;
// Calculate center coordinates of the window
int x = c->x + (c->w / 2);
int y = c->y + (c->h / 2);
// Move cursor to window center
XWarpPointer(display, None, root, 0, 0, 0, 0, x, y);
XFlush(display);
}