wmii/cmd/wm/event.c

272 lines
6.7 KiB
C
Raw Normal View History

2005-11-18 18:54:58 +03:00
/*
* (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 "wm.h"
/* local functions */
2005-12-05 01:45:59 +03:00
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);
2005-12-06 15:44:36 +03:00
static void handle_ignore_enternotify_crap(XEvent *e);
2005-11-18 18:54:58 +03:00
2005-12-06 15:44:36 +03:00
static unsigned int ignore_enternotify_crap = 0;
2005-11-18 18:54:58 +03:00
2005-12-05 01:45:59 +03:00
void (*handler[LASTEvent]) (XEvent *);
2005-11-18 18:54:58 +03:00
2005-12-05 01:45:59 +03:00
void init_event_hander()
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
int i;
2005-11-18 18:54:58 +03:00
/* init event handler */
for (i = 0; i < LASTEvent; i++) {
handler[i] = 0;
}
handler[ButtonPress] = handle_buttonpress;
handler[ConfigureRequest] = handle_configurerequest;
2005-12-06 15:44:36 +03:00
handler[ConfigureNotify] = handle_ignore_enternotify_crap;
handler[CirculateNotify] = handle_ignore_enternotify_crap;
2005-11-18 18:54:58 +03:00
handler[DestroyNotify] = handle_destroynotify;
handler[EnterNotify] = handle_enternotify;
handler[Expose] = handle_expose;
2005-12-06 15:44:36 +03:00
handler[GravityNotify] = handle_ignore_enternotify_crap;
2005-11-18 18:54:58 +03:00
handler[MapRequest] = handle_maprequest;
handler[MotionNotify] = handle_motionnotify;
handler[PropertyNotify] = handle_propertynotify;
2005-12-06 15:44:36 +03:00
handler[MapNotify] = handle_ignore_enternotify_crap;
2005-11-18 18:54:58 +03:00
handler[UnmapNotify] = handle_unmapnotify;
}
2005-12-05 01:45:59 +03:00
void check_event(Connection * c)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
XEvent ev;
2005-12-06 15:44:36 +03:00
while (XPending(dpy)) { /* main evet loop */
2005-11-18 18:54:58 +03:00
XNextEvent(dpy, &ev);
2005-12-06 15:44:36 +03:00
if (handler[ev.type])
(handler[ev.type]) (&ev); /* call handler */
2005-11-18 18:54:58 +03:00
}
}
2005-12-05 01:45:59 +03:00
static void handle_buttonpress(XEvent * e)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
Client *c;
2005-11-18 18:54:58 +03:00
XButtonPressedEvent *ev = &e->xbutton;
2005-12-05 01:45:59 +03:00
Frame *f = win_to_frame(ev->window);
2005-12-11 17:47:23 +03:00
fprintf(stderr, "%s\n", "handle_buttonpress");
2005-12-06 16:22:58 +03:00
2005-11-18 18:54:58 +03:00
if (f) {
handle_frame_buttonpress(ev, f);
return;
}
if (ev->window == root) {
XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
XSync(dpy, False);
2005-12-06 15:44:36 +03:00
return;
2005-11-18 18:54:58 +03:00
}
if ((c = win_to_client(ev->window))) {
2005-12-06 15:44:36 +03:00
if (c->frame) { /* client is attached */
2005-11-18 18:54:58 +03:00
ev->state &= valid_mask;
if (ev->state & Mod1Mask) {
2005-12-06 16:22:58 +03:00
Align align;
2005-12-06 21:09:23 +03:00
XRaiseWindow(dpy, c->frame->win);
2005-11-18 18:54:58 +03:00
switch (ev->button) {
case Button1:
mouse_move(c->frame);
break;
case Button3:
2005-12-06 16:22:58 +03:00
align = xy_to_align(&c->rect, ev->x, ev->y);
if (align == CENTER) mouse_move(c->frame);
else mouse_resize(c->frame, align);
2005-11-18 18:54:58 +03:00
break;
default:
break;
}
}
}
}
}
2005-12-05 01:45:59 +03:00
static void handle_configurerequest(XEvent * e)
2005-11-18 18:54:58 +03:00
{
XConfigureRequestEvent *ev = &e->xconfigurerequest;
2005-12-05 01:45:59 +03:00
XWindowChanges wc;
Client *c;
unsigned int bw = 0, tabh = 0;
Frame *f = 0;
2005-11-18 18:54:58 +03:00
2005-12-11 17:47:23 +03:00
fprintf(stderr, "%s\n", "handle_configurerequest");
2005-11-18 18:54:58 +03:00
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 = border_width(f);
tabh = tab_height(f);
}
if (ev->value_mask & CWStackMode) {
2005-12-06 16:22:58 +03:00
if (wc.stack_mode == Above) XRaiseWindow(dpy, c->win);
else ev->value_mask &= ~CWStackMode;
2005-11-18 18:54:58 +03:00
}
gravitate(c, tabh ? tabh : bw, bw, 1);
2005-12-06 16:22:58 +03:00
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;
2005-11-18 18:54:58 +03:00
gravitate(c, tabh ? tabh : bw, bw, 0);
if (f) {
2005-12-03 20:02:39 +03:00
f->rect.x = wc.x = c->rect.x - bw;
f->rect.y = wc.y = c->rect.y - (tabh ? tabh : bw);
f->rect.width = wc.width = c->rect.width + 2 * bw;
2005-12-06 16:22:58 +03:00
f->rect.height = wc.height = c->rect.height + bw + (tabh ? tabh : bw);
2005-11-18 18:54:58 +03:00
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;
2005-12-06 15:44:36 +03:00
XConfigureWindow(dpy, e->xconfigurerequest.window, ev->value_mask, &wc);
2005-11-18 18:54:58 +03:00
XSync(dpy, False);
2005-12-06 15:44:36 +03:00
/* fprintf(stderr, "%d,%d,%d,%d\n", wc.x, wc.y, wc.width, wc.height); */
2005-11-18 18:54:58 +03:00
}
2005-12-05 01:45:59 +03:00
static void handle_destroynotify(XEvent * e)
2005-11-18 18:54:58 +03:00
{
XDestroyWindowEvent *ev = &e->xdestroywindow;
2005-12-05 01:45:59 +03:00
Client *c = win_to_client(ev->window);
2005-12-11 17:47:23 +03:00
fprintf(stderr, "%s\n", "handle_destroynotify");
2005-12-06 00:22:24 +03:00
if (c) {
c->destroyed = True;
2005-12-11 18:24:52 +03:00
detach_client(c, False);
2005-12-06 00:22:24 +03:00
}
2005-11-18 18:54:58 +03:00
}
2005-12-05 01:45:59 +03:00
static void handle_expose(XEvent * e)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
static Frame *f;
2005-12-11 17:47:23 +03:00
fprintf(stderr, "%s\n", "handle_expose");
2005-11-18 18:54:58 +03:00
if (e->xexpose.count == 0) {
f = win_to_frame(e->xbutton.window);
if (f)
draw_frame(f, nil);
2005-11-18 18:54:58 +03:00
}
}
2005-12-05 01:45:59 +03:00
static void handle_maprequest(XEvent * e)
2005-11-18 18:54:58 +03:00
{
XMapRequestEvent *ev = &e->xmaprequest;
static XWindowAttributes wa;
2005-12-05 01:45:59 +03:00
static Client *c;
2005-11-18 18:54:58 +03:00
2005-12-11 17:47:23 +03:00
fprintf(stderr, "%s\n", "handle_maprequest");
2005-11-18 18:54:58 +03:00
if (!XGetWindowAttributes(dpy, ev->window, &wa))
return;
if (wa.override_redirect)
return;
2005-12-05 04:50:02 +03:00
/* there're client which send map requests twice */
2005-11-18 18:54:58 +03:00
c = win_to_client(ev->window);
if (!c)
c = alloc_client(ev->window);
if (!c->frame) {
2005-12-05 22:38:03 +03:00
init_client(c, &wa);
2005-11-18 18:54:58 +03:00
attach_client(c);
}
}
2005-12-05 01:45:59 +03:00
static void handle_motionnotify(XEvent * e)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
Frame *f = win_to_frame(e->xmotion.window);
Cursor cursor;
2005-12-11 17:47:23 +03:00
fprintf(stderr, "%s\n", "handle_motionnotify");
2005-11-18 18:54:58 +03:00
if (f) {
cursor = cursor_for_motion(f, e->xmotion.x, e->xmotion.y);
if (cursor != f->cursor) {
f->cursor = cursor;
XDefineCursor(dpy, f->win, cursor);
}
}
}
2005-12-05 01:45:59 +03:00
static void handle_propertynotify(XEvent * e)
2005-11-18 18:54:58 +03:00
{
XPropertyEvent *ev = &e->xproperty;
2005-12-05 01:45:59 +03:00
Client *c = win_to_client(ev->window);
2005-11-18 18:54:58 +03:00
2005-12-11 17:47:23 +03:00
fprintf(stderr, "%s\n", "handle_propertynotify");
2005-12-06 15:44:36 +03:00
if (c)
2005-11-18 18:54:58 +03:00
handle_client_property(c, ev);
}
2005-12-05 01:45:59 +03:00
static void handle_unmapnotify(XEvent * e)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
XUnmapEvent *ev = &e->xunmap;
Client *c;
2005-12-11 17:47:23 +03:00
fprintf(stderr, "%s\n", "handle_unmapnotify");
2005-12-06 15:44:36 +03:00
handle_ignore_enternotify_crap(e);
2005-12-06 00:22:24 +03:00
if ((c = win_to_client(ev->window)))
2005-12-11 18:24:52 +03:00
detach_client(c, True);
2005-11-18 18:54:58 +03:00
}
2005-12-05 01:45:59 +03:00
static void handle_enternotify(XEvent * e)
2005-11-18 18:54:58 +03:00
{
XCrossingEvent *ev = &e->xcrossing;
2005-12-05 01:45:59 +03:00
Client *c;
2005-11-18 18:54:58 +03:00
if ((ev->mode != NotifyNormal) || (ev->detail == NotifyInferior) || (ev->serial == ignore_enternotify_crap))
2005-11-18 18:54:58 +03:00
return;
2005-12-11 17:47:23 +03:00
fprintf(stderr, "%s\n", "handle_enternotify");
2005-11-18 18:54:58 +03:00
c = win_to_client(ev->window);
if (c && c->frame) {
2005-12-06 21:09:23 +03:00
Frame *old = get_sel_frame();
2005-11-18 18:54:58 +03:00
if (old != c->frame) {
2005-12-11 01:43:45 +03:00
c->frame->area->layout->select(c->frame, False);
draw_frame(old, nil);
draw_frame(c->frame, nil);
2005-11-18 18:54:58 +03:00
}
}
}
2005-12-06 15:44:36 +03:00
static void handle_ignore_enternotify_crap(XEvent *e)
{
2005-12-11 17:47:23 +03:00
fprintf(stderr, "%s\n", "handle_ignore_enternotify_crap");
2005-12-06 15:44:36 +03:00
ignore_enternotify_crap = e->xany.serial;
XSync(dpy, False);
}