wmii/event.c

372 lines
8.5 KiB
C
Raw Normal View History

2006-10-12 18:10:57 +04:00
/* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
2005-11-18 18:54:58 +03:00
* See LICENSE file for license details.
*/
2006-10-20 12:07:55 +04:00
#include "wmii.h"
2005-11-18 18:54:58 +03:00
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <X11/keysym.h>
#include "printevent.h"
2005-11-18 18:54:58 +03:00
uint
2006-10-12 18:10:57 +04:00
flush_masked_events(long even_mask) {
XEvent ev;
uint n = 0;
2006-07-03 20:41:14 +04:00
while(XCheckMaskEvent(blz.dpy, even_mask, &ev)) n++;
return n;
}
2005-12-21 18:18:11 +03:00
static void
2006-10-12 18:10:57 +04:00
buttonrelease(XEvent *e) {
Frame *f;
2006-06-08 12:54:19 +04:00
Bar *b;
XButtonPressedEvent *ev = &e->xbutton;
if(ev->window == screen->barwin) {
for(b=screen->lbar; b; b=b->next)
2006-10-30 11:54:22 +03:00
if(ispointinrect(ev->x, ev->y, &b->brush.rect)) {
write_event("LeftBarClick %d %s\n",
ev->button, b->name);
2006-10-30 11:54:22 +03:00
return;
}
for(b=screen->rbar; b; b=b->next)
2006-10-30 11:54:22 +03:00
if(ispointinrect(ev->x, ev->y, &b->brush.rect)) {
write_event("RightBarClick %d %s\n",
ev->button, b->name);
2006-10-30 11:54:22 +03:00
return;
}
}
2007-02-10 00:42:03 +03:00
else if((f = frame_of_win(ev->window)))
write_event("ClientClick 0x%x %d\n", f->client->win, ev->button);
}
static void
2006-10-12 18:10:57 +04:00
buttonpress(XEvent *e) {
Frame *f;
2007-02-10 00:27:10 +03:00
Bool inclient;
XButtonPressedEvent *ev;
ev = &e->xbutton;
if((f = frame_of_win(ev->window))) {
2007-02-10 00:42:03 +03:00
inclient = (ev->subwindow == f->client->win);
if((ev->state & def.mod) == def.mod) {
switch(ev->button) {
case Button1:
do_mouse_resize(f->client, False, CENTER);
focus(f->client, True);
frame_to_top(f);
focus(f->client, True);
break;
case Button3:
do_mouse_resize(f->client, False,
2007-02-18 21:27:51 +03:00
quadofcoord(&f->rect, ev->x_root, ev->y_root));
frame_to_top(f);
focus(f->client, True);
2007-02-10 02:07:59 +03:00
break;
default: break;
XAllowEvents(blz.dpy, ReplayPointer, CurrentTime);
}
}else{
if(ev->button == Button1) {
if(frame_to_top(f))
restack_view(f->view);
if(ispointinrect(ev->x, ev->y, &f->grabbox))
do_mouse_resize(f->client, True, CENTER);
else if(!ev->subwindow
&& !ispointinrect(ev->x, ev->y, &f->titlebar))
do_mouse_resize(f->client, False,
2007-02-18 21:27:51 +03:00
quadofcoord(&f->rect, ev->x_root, ev->y_root));
if(f->client != sel_client())
focus(f->client, True);
}
XAllowEvents(blz.dpy, ReplayPointer, CurrentTime);
}
2007-02-10 00:27:10 +03:00
}else
2007-02-10 02:07:59 +03:00
XAllowEvents(blz.dpy, ReplayPointer, CurrentTime);
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
static void
2006-10-12 18:10:57 +04:00
configurerequest(XEvent *e) {
XConfigureRequestEvent *ev = &e->xconfigurerequest;
XWindowChanges wc;
XRectangle *frect;
Client *c;
Frame *f;
2005-12-21 18:18:11 +03:00
2006-04-12 12:44:07 +04:00
c = client_of_win(ev->window);
if(c) {
f = c->sel;
2007-02-17 20:49:22 +03:00
if(0 && verbose)
2007-02-16 21:41:23 +03:00
fprintf(stderr, "Configure: %s\n\ta: x=%d y=%d w=%d h=%d\n",
c->name, c->rect.x, c->rect.y, c->rect.width, c->rect.height);
gravitate_client(c, True);
2007-02-17 20:49:22 +03:00
if(0 && verbose)
2007-02-17 10:20:11 +03:00
fprintf(stderr, "\tb: x=%d y=%d w=%d h=%d\n",
c->rect.x, c->rect.y, c->rect.width, c->rect.height);
if(ev->value_mask & CWX)
c->rect.x = ev->x;
if(ev->value_mask & CWY)
2007-02-16 22:04:09 +03:00
c->rect.y = ev->y;
if(ev->value_mask & CWWidth)
2007-02-16 22:04:09 +03:00
c->rect.width = ev->width;
if(ev->value_mask & CWHeight)
2007-02-16 22:04:09 +03:00
c->rect.height = ev->height;
if(ev->value_mask & CWBorderWidth)
c->border = ev->border_width;
2007-02-17 20:49:22 +03:00
if(0 && verbose)
2007-02-17 10:20:11 +03:00
fprintf(stderr, "\tb: x=%d y=%d w=%d h=%d\n",
c->rect.x, c->rect.y, c->rect.width, c->rect.height);
gravitate_client(c, False);
2007-02-17 20:49:22 +03:00
if(0 && verbose)
2007-02-16 21:41:23 +03:00
fprintf(stderr, "\tb: x=%d y=%d w=%d h=%d\n",
c->rect.x, c->rect.y, c->rect.width, c->rect.height);
if(c->sel->area->floating)
frect=&c->sel->rect;
else
frect=&c->sel->revert;
*frect = c->rect;
2007-02-16 21:41:23 +03:00
frect->y -= labelh(&def.font);
frect->x -= def.border;
frect->width += 2 * def.border;
frect->height += frame_delta_h();
2007-02-17 20:49:22 +03:00
if(0 && verbose)
2007-02-17 10:20:11 +03:00
fprintf(stderr, "\tb: x=%d y=%d w=%d h=%d\n",
frect->x, frect->y, frect->width, frect->height);
if(c->sel->area->floating)
resize_client(c, frect);
else
configure_client(c);
}else{
wc.x = ev->x;
wc.y = ev->y;
wc.width = ev->width;
wc.height = ev->height;
wc.border_width = ev->border_width;
wc.sibling = ev->above;
wc.stack_mode = ev->detail;
2007-02-16 21:41:23 +03:00
ev->value_mask &= ~(CWStackMode|CWSibling);
XConfigureWindow(blz.dpy, ev->window, ev->value_mask, &wc);
XSync(blz.dpy, False);
}
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
static void
2006-10-12 18:10:57 +04:00
destroynotify(XEvent *e) {
Client *c;
XDestroyWindowEvent *ev = &e->xdestroywindow;
2006-04-12 12:44:07 +04:00
if((c = client_of_win(ev->window)))
destroy_client(c);
2005-11-18 18:54:58 +03:00
}
static void
2006-10-12 18:10:57 +04:00
enternotify(XEvent *e) {
XCrossingEvent *ev = &e->xcrossing;
Client *c;
if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
return;
if((c = client_of_win(ev->window))) {
2007-02-06 10:12:47 +03:00
if(c->sel->area->mode == Colmax)
c = c->sel->area->sel->client;
focus(c, False);
}
else if(ev->window == blz.root) {
sel_screen = True;
draw_frames();
}
}
static void
2006-10-12 18:10:57 +04:00
leavenotify(XEvent *e) {
XCrossingEvent *ev = &e->xcrossing;
if((ev->window == blz.root) && !ev->same_screen) {
sel_screen = True;
draw_frames();
}
}
2005-12-21 18:18:11 +03:00
static void
2006-10-12 18:10:57 +04:00
expose(XEvent *e) {
XExposeEvent *ev = &e->xexpose;
static Frame *f;
if(ev->count == 0) {
if(ev->window == screen->barwin)
draw_bar(screen);
else if((f = frame_of_win(ev->window)) && f->view == screen->sel)
draw_frame(f);
}
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
static void
2006-10-12 18:10:57 +04:00
keypress(XEvent *e) {
XKeyEvent *ev = &e->xkey;
KeySym k = 0;
char buf[32];
int n;
static Frame *f;
ev->state &= valid_mask;
if((f = frame_of_win(ev->window))) {
buf[0] = 0;
2006-07-03 14:22:46 +04:00
n = XLookupString(ev, buf, sizeof(buf), &k, 0);
if(IsFunctionKey(k) || IsKeypadKey(k) || IsMiscFunctionKey(k)
|| IsPFKey(k) || IsPrivateKeypadKey(k))
return;
buf[n] = 0;
}
else
kpress(blz.root, ev->state, (KeyCode) ev->keycode);
}
static void
2006-10-12 18:10:57 +04:00
mappingnotify(XEvent *e) {
XMappingEvent *ev = &e->xmapping;
XRefreshKeyboardMapping(ev);
if(ev->request == MappingKeyboard)
update_keys();
}
static void
2006-10-12 18:10:57 +04:00
maprequest(XEvent *e) {
XMapRequestEvent *ev = &e->xmaprequest;
static XWindowAttributes wa;
2005-12-21 18:18:11 +03:00
2006-07-03 20:41:14 +04:00
if(!XGetWindowAttributes(blz.dpy, ev->window, &wa))
return;
if(wa.override_redirect) {
2006-07-03 20:41:14 +04:00
XSelectInput(blz.dpy, ev->window,
(StructureNotifyMask | PropertyChangeMask));
return;
}
2006-04-12 12:44:07 +04:00
if(!client_of_win(ev->window))
manage_client(create_client(ev->window, &wa));
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
static void
2006-10-12 18:10:57 +04:00
propertynotify(XEvent *e) {
XPropertyEvent *ev = &e->xproperty;
Client *c;
2005-12-12 21:06:10 +03:00
if(ev->state == PropertyDelete)
return; /* ignore */
2006-04-12 12:44:07 +04:00
if((c = client_of_win(ev->window)))
prop_client(c, ev);
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
static void
2006-10-12 18:10:57 +04:00
unmapnotify(XEvent *e) {
Client *c;
XUnmapEvent *ev = &e->xunmap;
2006-04-12 12:44:07 +04:00
if((c = client_of_win(ev->window)))
if(!c->unmapped--)
destroy_client(c);
2005-11-18 18:54:58 +03:00
}
static void
focusin(XEvent *e) {
Client *c;
XFocusChangeEvent *ev = &e->xfocus;
if(!((ev->detail == NotifyNonlinear)
||(ev->detail == NotifyNonlinearVirtual)
||(ev->detail == NotifyInferior)
||(ev->detail == NotifyAncestor)))
return;
if(ev->mode == NotifyWhileGrabbed)
2007-02-18 20:33:17 +03:00
return;
c = client_of_win(ev->window);
if(c) {
2007-02-17 20:49:22 +03:00
if(verbose) {
fprintf(stderr, "screen->focus: %p => %p\n", screen->focus, c);
2007-02-17 20:49:22 +03:00
fprintf(stderr, "\t%s => %s\n", (screen->focus ? screen->focus->name : nil),
c->name);
}
if(ev->mode == NotifyGrab)
screen->hasgrab = c;
screen->focus = c;
update_client_grab(c);
if(c->sel)
draw_frame(c->sel);
}else if(ev->window == screen->barwin) {
2007-02-17 20:49:22 +03:00
if(verbose) {
fprintf(stderr, "screen->focus: %p => %p\n", screen->focus, c);
fprintf(stderr, "\t%s => %s\n", (screen->focus ? screen->focus->name : nil),
"<nil>");
}
screen->focus = nil;
}else if(ev->mode == NotifyGrab) {
c = screen->focus;
if(c) {
screen->focus = nil;
if(c->sel)
draw_frame(c->sel);
}
}
}
static void
focusout(XEvent *e) {
2007-02-17 20:49:22 +03:00
Client *c;
XFocusChangeEvent *ev = &e->xfocus;
if(!((ev->detail == NotifyNonlinear)
||(ev->detail == NotifyNonlinearVirtual)))
return;
if(ev->mode == NotifyUngrab)
screen->hasgrab = nil;
2007-02-18 20:33:17 +03:00
2007-02-17 20:49:22 +03:00
c = client_of_win(ev->window);
if(c) {
if(ev->mode == NotifyWhileGrabbed) {
if(screen->focus && screen->hasgrab != screen->focus)
screen->hasgrab = screen->focus;
if(screen->hasgrab == c)
return;
}
if(screen->focus == c)
screen->focus = nil;
2007-02-17 20:49:22 +03:00
update_client_grab(c);
if(c->sel)
draw_frame(c->sel);
}
}
void (*handler[LASTEvent]) (XEvent *) = {
[ButtonPress] = buttonpress,
[ButtonRelease] = buttonrelease,
[ConfigureRequest]= configurerequest,
[DestroyNotify] = destroynotify,
[EnterNotify] = enternotify,
[LeaveNotify] = leavenotify,
[Expose] = expose,
[KeyPress] = keypress,
[MappingNotify] = mappingnotify,
[MapRequest] = maprequest,
[PropertyNotify]= propertynotify,
[UnmapNotify] = unmapnotify,
[FocusIn] = focusin,
[FocusOut] = focusout
};
void
check_x_event(IXPConn *c) {
XEvent ev;
while(XPending(blz.dpy)) { /* main event loop */
XNextEvent(blz.dpy, &ev);
if(verbose)
printevent(&ev);
if(handler[ev.type])
(handler[ev.type]) (&ev); /* call handler */
}
}