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
|
/*
* (C)opyright MMIV-MMV Anselm R. Garbe <garbeam at gmail dot com>
* See LICENSE file for license details.
*/
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <X11/keysym.h>
#include "wmiiwm.h"
/* local functions */
static void handle_buttonpress(XEvent * e);
static void handle_configurerequest(XEvent * e);
static void handle_destroynotify(XEvent * e);
static void handle_expose(XEvent * e);
static void handle_maprequest(XEvent * e);
static void handle_motionnotify(XEvent * e);
static void handle_propertynotify(XEvent * e);
static void handle_unmapnotify(XEvent * e);
static void handle_enternotify(XEvent * e);
static void update_ignore_enternotify_hack(XEvent * e);
void (*handler[LASTEvent]) (XEvent *);
void
init_event_hander()
{
int i;
/* init event handler */
for (i = 0; i < LASTEvent; i++) {
handler[i] = 0;
}
handler[ButtonPress] = handle_buttonpress;
handler[CirculateNotify] = update_ignore_enternotify_hack;
handler[ConfigureRequest] = handle_configurerequest;
handler[DestroyNotify] = handle_destroynotify;
handler[EnterNotify] = handle_enternotify;
handler[Expose] = handle_expose;
handler[GravityNotify] = update_ignore_enternotify_hack;
handler[MapRequest] = handle_maprequest;
handler[MapNotify] = update_ignore_enternotify_hack;
handler[MotionNotify] = handle_motionnotify;
handler[PropertyNotify] = handle_propertynotify;
handler[UnmapNotify] = handle_unmapnotify;
}
void
check_event(Connection * c)
{
XEvent ev;
while (XPending(dpy)) {
XNextEvent(dpy, &ev);
/* main evet loop */
if (handler[ev.type]) {
/* call handler */
(handler[ev.type]) (&ev);
}
}
}
static void
handle_buttonpress(XEvent * e)
{
Client *c;
XButtonPressedEvent *ev = &e->xbutton;
Frame *f = win_to_frame(ev->window);
if (f) {
handle_frame_buttonpress(ev, f);
return;
}
if (ev->window == root) {
XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
XSync(dpy, False);
}
if ((c = win_to_client(ev->window))) {
if (c->frame) {
ev->state &= valid_mask;
if (ev->state & Mod1Mask) {
if (!is_managed_frame(c->frame))
XRaiseWindow(dpy, c->frame->win);
switch (ev->button) {
case Button1:
mouse_move(c->frame);
break;
case Button3:
{
Align align = xy_to_align(&c->rect, ev->x, ev->y);
if (align == CENTER)
mouse_move(c->frame);
else
mouse_resize(c->frame, align);
}
break;
default:
break;
}
}
}
}
}
static void
handle_configurerequest(XEvent * e)
{
XConfigureRequestEvent *ev = &e->xconfigurerequest;
XWindowChanges wc;
Client *c;
unsigned int bw = 0, tabh = 0;
Frame *f = 0;
update_ignore_enternotify_hack(e);
/* fprintf(stderr, "%s", "configure request\n"); */
c = win_to_client(ev->window);
ev->value_mask &= ~CWSibling;
if (c) {
/* fprintf(stderr, "%s", "configure request client\n"); */
f = c->frame;
if (f) {
bw = _strtonum(f->files[F_BORDER_W]->content, 0, 10);
tabh = _strtonum(f->files[F_TAB_H]->content, 0, 30);
}
if (ev->value_mask & CWStackMode) {
if (wc.stack_mode == Above)
XRaiseWindow(dpy, c->win);
else
ev->value_mask &= ~CWStackMode;
}
gravitate(c, tabh ? tabh : bw, bw, 1);
if (ev->value_mask & CWX)
c->rect.x = ev->x;
if (ev->value_mask & CWY)
c->rect.y = ev->y;
if (ev->value_mask & CWWidth)
c->rect.width = ev->width;
if (ev->value_mask & CWHeight)
c->rect.height = ev->height;
if (ev->value_mask & CWBorderWidth)
c->border = ev->border_width;
gravitate(c, tabh ? tabh : bw, bw, 0);
if (f) {
XRectangle *frect = rect_of_frame(f);
frect->x = wc.x = c->rect.x - bw;
frect->y = wc.y = c->rect.y - (tabh ? tabh : bw);
frect->width = wc.width = c->rect.width + 2 * bw;
frect->height = wc.height =
c->rect.height + bw + (tabh ? tabh : bw);
wc.border_width = 1;
wc.sibling = None;
wc.stack_mode = ev->detail;
XConfigureWindow(dpy, f->win, ev->value_mask, &wc);
configure_client(c);
}
}
wc.x = ev->x;
wc.y = ev->y;
if (f) {
/* if so, then bw and tabh are already initialized */
wc.x = bw;
wc.y = tabh ? tabh : bw;
}
wc.width = ev->width;
wc.height = ev->height;
wc.border_width = 0;
wc.sibling = None;
wc.stack_mode = Above;
ev->value_mask &= ~CWStackMode;
ev->value_mask |= CWBorderWidth;
XConfigureWindow(dpy, e->xconfigurerequest.window, ev->value_mask,
&wc);
XSync(dpy, False);
/*
fprintf(stderr, "%d,%d,%d,%d\n", wc.x, wc.y, wc.width, wc.height);
*/
}
static void
handle_destroynotify(XEvent * e)
{
XDestroyWindowEvent *ev = &e->xdestroywindow;
Client *c = win_to_client(ev->window);
/* fprintf(stderr, "destroy: client 0x%x\n", (int)ev->window); */
if (!c)
return;
if (sel == c)
sel = 0;
if (c->frame)
detach_client_from_frame(c, 0, 1, 0);
else if (detached && (index_item((void **) detached, c) >= 0))
detached = (Client **) detach_item((void **) detached, c, sizeof(Client *));
free_client(c);
}
static void
handle_expose(XEvent * e)
{
static Frame *f;
if (e->xexpose.count == 0) {
f = win_to_frame(e->xbutton.window);
if (f)
draw_frame(f);
}
}
static void
handle_maprequest(XEvent * e)
{
XMapRequestEvent *ev = &e->xmaprequest;
static XWindowAttributes wa;
static Client *c;
/* fprintf(stderr, "map: window 0x%x\n", (int)ev->window); */
if (!XGetWindowAttributes(dpy, ev->window, &wa))
return;
if (wa.override_redirect)
return;
/* there're clients which send map requests twice */
c = win_to_client(ev->window);
if (!c)
c = alloc_client(ev->window);
if (!c->frame) {
_init_client(c, &wa);
attach_client(c);
}
}
static void
handle_motionnotify(XEvent * e)
{
Frame *f = win_to_frame(e->xmotion.window);
Cursor cursor;
if (f) {
Frame *old = get_selected(f->page);
if (old != f) {
focus_frame(f, 0, 0, 1);
draw_frame(old);
draw_frame(f);
} else if (f->clients) {
/* multihead assumption */
XSetInputFocus(dpy, f->clients[f->sel]->win,
RevertToPointerRoot, CurrentTime);
XSync(dpy, False);
}
cursor = cursor_for_motion(f, e->xmotion.x, e->xmotion.y);
if (cursor != f->cursor) {
f->cursor = cursor;
XDefineCursor(dpy, f->win, cursor);
}
}
}
static void
handle_propertynotify(XEvent * e)
{
XPropertyEvent *ev = &e->xproperty;
Client *c = win_to_client(ev->window);
if (c) {
handle_client_property(c, ev);
return;
}
}
static void
handle_unmapnotify(XEvent * e)
{
XUnmapEvent *ev = &e->xunmap;
Client *c;
update_ignore_enternotify_hack(e);
if (ev->event == root)
return;
if ((c = win_to_client(ev->window))) {
if (c->frame) {
detach_client_from_frame(c, 1, 0, 0);
if (pages)
draw_page(pages[sel_page]);
free_client(c);
} else if (detached) {
if (index_item((void **) detached, c) == -1)
free_client(c);
}
}
}
static void
handle_enternotify(XEvent * e)
{
XCrossingEvent *ev = &e->xcrossing;
Client *c;
if (ev->mode != NotifyNormal)
return;
/* mouse is not in the focus window */
if (ev->detail == NotifyInferior)
return;
c = win_to_client(ev->window);
if (c && c->frame && (ev->serial != ignore_enternotify_hack)) {
Frame *old = get_selected(c->frame->page);
XUndefineCursor(dpy, c->frame->win);
if (old != c->frame) {
focus_frame(c->frame, 0, 0, 1);
draw_frame(old);
draw_frame(c->frame);
} else {
/* multihead assumption */
XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
XSync(dpy, False);
}
}
}
static void
update_ignore_enternotify_hack(XEvent * e)
{
ignore_enternotify_hack = e->xany.serial;
XSync(dpy, False);
}
|