[go: up one dir, main page]

Menu

[f59a1b]: / root.c  Maximize  Restore  History

Download this file

241 lines (215 with data), 5.6 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
/*
* Copyright 2010 Johan Veenhuizen
*/
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include "wind.h"
static void fnkey(KeySym, unsigned, Time, int);
static void configurerequest(XConfigureRequestEvent *);
static void maprequest(XMapRequestEvent *);
static void keypress(XKeyEvent *);
static void focusin(XFocusChangeEvent *);
static void clientmessage(XClientMessageEvent *);
static void unmapnotify(XUnmapEvent *);
static void enternotify(XCrossingEvent *);
static void leavenotify(XCrossingEvent *);
static void event(void *, XEvent *);
// True if the pointer is on this screen
static Bool pointerhere;
static const struct listener listener = {
.function = event,
.pointer = NULL,
};
static struct keybind {
KeySym keysym;
unsigned modifiers;
void (*function)(KeySym key, unsigned state, Time t, int arg);
int arg;
KeyCode keycode;
} keymap[] = {
{ XK_F1, Mod1Mask, fnkey, 1 },
{ XK_F2, Mod1Mask, fnkey, 2 },
{ XK_F3, Mod1Mask, fnkey, 3 },
{ XK_F4, Mod1Mask, fnkey, 4 },
{ XK_F5, Mod1Mask, fnkey, 5 },
{ XK_F6, Mod1Mask, fnkey, 6 },
{ XK_F7, Mod1Mask, fnkey, 7 },
{ XK_F8, Mod1Mask, fnkey, 8 },
{ XK_F9, Mod1Mask, fnkey, 9 },
{ XK_F10, Mod1Mask, fnkey, 10 },
{ XK_F11, Mod1Mask, fnkey, 11 },
{ XK_F12, Mod1Mask, fnkey, 12 },
{ XK_F1, ShiftMask | Mod1Mask, fnkey, 1 },
{ XK_F2, ShiftMask | Mod1Mask, fnkey, 2 },
{ XK_F3, ShiftMask | Mod1Mask, fnkey, 3 },
{ XK_F4, ShiftMask | Mod1Mask, fnkey, 4 },
{ XK_F5, ShiftMask | Mod1Mask, fnkey, 5 },
{ XK_F6, ShiftMask | Mod1Mask, fnkey, 6 },
{ XK_F7, ShiftMask | Mod1Mask, fnkey, 7 },
{ XK_F8, ShiftMask | Mod1Mask, fnkey, 8 },
{ XK_F9, ShiftMask | Mod1Mask, fnkey, 9 },
{ XK_F10, ShiftMask | Mod1Mask, fnkey, 10 },
{ XK_F11, ShiftMask | Mod1Mask, fnkey, 11 },
{ XK_F12, ShiftMask | Mod1Mask, fnkey, 12 },
};
static void fnkey(KeySym keysym, unsigned state, Time time, int arg)
{
if (state & ShiftMask)
setndesk(arg);
gotodesk(arg - 1);
refocus(time);
}
static void configurerequest(XConfigureRequestEvent *e)
{
// First try to redirect the event.
if (redirect((XEvent *)e, e->window) == 0)
return;
// Nobody listens to this window so we'll just
// do whatever it wants us to do.
// Ignore stacking requests for now.
e->value_mask &= ~(CWSibling | CWStackMode);
XConfigureWindow(dpy, e->window, e->value_mask,
&(XWindowChanges){
.x = e->x,
.y = e->y,
.width = e->width,
.height = e->height,
.border_width = e->border_width,
.sibling = e->above,
.stack_mode = e->detail });
}
static void maprequest(XMapRequestEvent *e)
{
// Already managed?
if (redirect((XEvent *)e, e->window) == 0)
return;
// Try to manage it, otherwise just map it.
if (manage(e->window) == NULL)
XMapWindow(dpy, e->window);
}
static void keypress(XKeyEvent *e)
{
for (int i = 0; i < NELEM(keymap); i++)
if (keymap[i].keycode == e->keycode) {
keymap[i].function(keymap[i].keysym, e->state,
e->time, keymap[i].arg);
break;
}
}
/*
* Pick a new focus window whenever input focus reverts
* to our root window (unless caused by our own key grab).
*
* By checking for NotifyPointer we avoid refocusing
* if the pointer is on another screen.
*/
static void focusin(XFocusChangeEvent *e)
{
if (e->window == root && e->mode != NotifyGrab)
switch (e->detail) {
case NotifyPointer:
case NotifyInferior:
refocus(CurrentTime);
break;
case NotifyDetailNone:
if (pointerhere)
refocus(CurrentTime);
break;
}
}
static void clientmessage(XClientMessageEvent *e)
{
ewmh_rootclientmessage(e);
}
/*
* Refer to the ICCCM section 4.1.4, "Changing Window State",
* for information on the synthetic UnmapNotify event sent
* by clients to the root window on withdrawal.
*/
static void unmapnotify(XUnmapEvent *e)
{
if (e->send_event)
redirect((XEvent *)e, e->window);
}
/*
* Refocus whenever the pointer enters our root window from
* another screen.
*/
static void enternotify(XCrossingEvent *e)
{
if (e->detail == NotifyNonlinear ||
e->detail == NotifyNonlinearVirtual) {
pointerhere = True;
refocus(e->time);
}
}
/*
* Give up focus if the pointer leaves our screen.
*/
static void leavenotify(XCrossingEvent *e)
{
if (e->detail == NotifyNonlinear ||
e->detail == NotifyNonlinearVirtual) {
pointerhere = False;
XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, e->time);
}
}
static void event(void *self, XEvent *e)
{
switch (e->type) {
case MapRequest:
maprequest(&e->xmaprequest);
break;
case ConfigureRequest:
configurerequest(&e->xconfigurerequest);
break;
case KeyPress:
keypress(&e->xkey);
break;
case FocusIn:
focusin(&e->xfocus);
break;
case ClientMessage:
clientmessage(&e->xclient);
break;
case UnmapNotify:
unmapnotify(&e->xunmap);
break;
case EnterNotify:
enternotify(&e->xcrossing);
break;
case LeaveNotify:
leavenotify(&e->xcrossing);
break;
}
}
void initroot(void)
{
setlistener(root, &listener);
XSync(dpy, False);
xerror = NULL;
XSelectInput(dpy, root,
FocusChangeMask |
EnterWindowMask |
LeaveWindowMask |
SubstructureRedirectMask |
SubstructureNotifyMask);
XSync(dpy, False);
if (xerror != NULL) {
errorf("display \"%s\" already has a window manager",
XDisplayName(displayname));
exit(1);
}
for (int i = 0; i < NELEM(keymap); i++) {
struct keybind *k = &keymap[i];
k->keycode = XKeysymToKeycode(dpy, k->keysym);
grabkey(k->keycode, k->modifiers, root, True,
GrabModeAsync, GrabModeAsync);
}
Window r, c;
int rx, ry, x, y;
unsigned m;
XQueryPointer(dpy, root, &r, &c, &rx, &ry, &x, &y, &m);
pointerhere = (r == root);
}