wmii/cmd/wm/event.c

291 lines
6.5 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-11-18 18:54:58 +03:00
static unsigned int ignore_enternotify_hack = 0;
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;
handler[DestroyNotify] = handle_destroynotify;
handler[EnterNotify] = handle_enternotify;
handler[Expose] = handle_expose;
handler[MapRequest] = handle_maprequest;
handler[MotionNotify] = handle_motionnotify;
handler[PropertyNotify] = handle_propertynotify;
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-11-18 18:54:58 +03:00
while (XPending(dpy)) {
XNextEvent(dpy, &ev);
/* main evet loop */
if (handler[ev.type]) {
/* call handler */
(handler[ev.type]) (&ev);
}
}
}
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-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);
}
if ((c = win_to_client(ev->window))) {
if (c->frame) {
ev->state &= valid_mask;
if (ev->state & Mod1Mask) {
if (!c->frame->area->page->sel)
2005-11-18 18:54:58 +03:00
XRaiseWindow(dpy, c->frame->win);
switch (ev->button) {
case Button1:
mouse_move(c->frame);
break;
case Button3:
{
2005-12-05 01:45:59 +03:00
Align align = xy_to_align(&c->rect, ev->x, ev->y);
2005-11-18 18:54:58 +03:00
if (align == CENTER)
mouse_move(c->frame);
else
mouse_resize(c->frame, align);
}
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
/* 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 = border_width(f);
tabh = tab_height(f);
}
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) {
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-05 01:45:59 +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;
XConfigureWindow(dpy, e->xconfigurerequest.window, ev->value_mask,
2005-12-05 01:45:59 +03:00
&wc);
2005-11-18 18:54:58 +03:00
XSync(dpy, False);
/*
fprintf(stderr, "%d,%d,%d,%d\n", wc.x, wc.y, wc.width, wc.height);
*/
}
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-11-18 18:54:58 +03:00
/* fprintf(stderr, "destroy: client 0x%x\n", (int)ev->window); */
2005-12-06 00:22:24 +03:00
if (c) {
c->destroyed = True;
detach_client(c);
}
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-11-18 18:54:58 +03:00
if (e->xexpose.count == 0) {
f = win_to_frame(e->xbutton.window);
if (f)
draw_frame(f);
}
}
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
/* fprintf(stderr, "map: window 0x%x\n", (int)ev->window); */
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-11-18 18:54:58 +03:00
if (f) {
2005-12-05 04:50:02 +03:00
Frame *old = SELFRAME(page[sel]);
2005-11-18 18:54:58 +03:00
if (old != f) {
2005-12-06 00:22:24 +03:00
sel_frame(f, 1);
2005-11-18 18:54:58 +03:00
draw_frame(old);
draw_frame(f);
2005-12-05 04:50:02 +03:00
} else if (f->client) {
2005-11-18 18:54:58 +03:00
/* multihead assumption */
2005-12-06 00:22:24 +03:00
XSetInputFocus(dpy, f->client[f->sel]->win, RevertToPointerRoot, CurrentTime);
2005-11-18 18:54:58 +03:00
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);
}
}
}
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
if (c) {
handle_client_property(c, ev);
return;
}
}
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-11-18 18:54:58 +03:00
if (ev->event == root)
return;
2005-12-06 00:22:24 +03:00
if ((c = win_to_client(ev->window)))
detach_client(c);
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)
return;
2005-12-05 22:38:03 +03:00
/* mouse is not in the sel window */
2005-11-18 18:54:58 +03:00
if (ev->detail == NotifyInferior)
return;
c = win_to_client(ev->window);
if (c && c->frame && (ev->serial != ignore_enternotify_hack)) {
2005-12-05 04:50:02 +03:00
Frame *old = SELFRAME(page[sel]);
2005-11-18 18:54:58 +03:00
XUndefineCursor(dpy, c->frame->win);
if (old != c->frame) {
2005-12-06 00:22:24 +03:00
sel_frame(c->frame, 1);
2005-11-18 18:54:58 +03:00
draw_frame(old);
draw_frame(c->frame);
} else {
/* multihead assumption */
XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
XSync(dpy, False);
}
}
}