awesome/event.c

407 lines
14 KiB
C
Raw Normal View History

2007-09-12 14:29:51 +02:00
/*
* event.c - event handlers
*
* Copyright © 2007 Julien Danjou <julien@danjou.info>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
2007-09-05 20:15:00 +02:00
#include <X11/keysym.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
2007-09-10 12:06:54 +02:00
#include "awesome.h"
2007-09-05 20:15:00 +02:00
#include "event.h"
#include "layout.h"
#include "tag.h"
#include "layouts/tile.h"
#include "layouts/floating.h"
/* extern */
extern int wax, way, wah, waw; /* windowarea geometry */
extern DC dc; /* global draw context */
extern Cursor cursor[CurLast];
extern Client *clients, *sel; /* global client list */
2007-09-10 12:06:54 +02:00
#define CLEANMASK(mask) (mask & ~(awesomeconf->numlockmask | LockMask))
2007-09-05 20:15:00 +02:00
#define MOUSEMASK (BUTTONMASK | PointerMotionMask)
static Client *
getclient(Window w)
{
Client *c;
for(c = clients; c && c->win != w; c = c->next);
return c;
}
static void
2007-09-10 12:06:54 +02:00
movemouse(Client * c, awesome_config *awesomeconf)
2007-09-05 20:15:00 +02:00
{
int x1, y1, ocx, ocy, di, nx, ny;
unsigned int dui;
Window dummy;
XEvent ev;
ocx = nx = c->x;
ocy = ny = c->y;
if(XGrabPointer(c->display, DefaultRootWindow(c->display), False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
None, cursor[CurMove], CurrentTime) != GrabSuccess)
return;
XQueryPointer(c->display, DefaultRootWindow(c->display), &dummy, &dummy, &x1, &y1, &di, &di, &dui);
for(;;)
{
XMaskEvent(c->display, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
switch (ev.type)
{
case ButtonRelease:
XUngrabPointer(c->display, CurrentTime);
return;
case ConfigureRequest:
case Expose:
case MapRequest:
2007-09-10 12:06:54 +02:00
handle_event_maprequest(&ev, awesomeconf);
2007-09-05 20:15:00 +02:00
break;
case MotionNotify:
XSync(c->display, False);
nx = ocx + (ev.xmotion.x - x1);
ny = ocy + (ev.xmotion.y - y1);
2007-09-10 12:06:54 +02:00
if(abs(wax + nx) < awesomeconf->snap)
2007-09-05 20:15:00 +02:00
nx = wax;
2007-09-10 12:06:54 +02:00
else if(abs((wax + waw) - (nx + c->w + 2 * c->border)) < awesomeconf->snap)
2007-09-05 20:15:00 +02:00
nx = wax + waw - c->w - 2 * c->border;
2007-09-10 12:06:54 +02:00
if(abs(way - ny) < awesomeconf->snap)
2007-09-05 20:15:00 +02:00
ny = way;
2007-09-10 12:06:54 +02:00
else if(abs((way + wah) - (ny + c->h + 2 * c->border)) < awesomeconf->snap)
2007-09-05 20:15:00 +02:00
ny = way + wah - c->h - 2 * c->border;
resize(c, nx, ny, c->w, c->h, False);
break;
}
}
}
static void
2007-09-10 12:06:54 +02:00
resizemouse(Client * c, awesome_config *awesomeconf)
2007-09-05 20:15:00 +02:00
{
int ocx, ocy;
int nw, nh;
XEvent ev;
ocx = c->x;
ocy = c->y;
if(XGrabPointer(c->display, DefaultRootWindow(c->display), False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
None, cursor[CurResize], CurrentTime) != GrabSuccess)
return;
c->ismax = False;
XWarpPointer(c->display, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
for(;;)
{
XMaskEvent(c->display, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
switch (ev.type)
{
case ButtonRelease:
XWarpPointer(c->display, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
XUngrabPointer(c->display, CurrentTime);
while(XCheckMaskEvent(c->display, EnterWindowMask, &ev));
return;
case ConfigureRequest:
case Expose:
case MapRequest:
2007-09-10 12:06:54 +02:00
handle_event_maprequest(&ev, awesomeconf);
2007-09-05 20:15:00 +02:00
break;
case MotionNotify:
XSync(c->display, False);
if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
nw = 1;
if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
nh = 1;
resize(c, c->x, c->y, nw, nh, True);
break;
}
}
}
void
2007-09-10 12:06:54 +02:00
handle_event_buttonpress(XEvent * e, awesome_config *awesomeconf)
2007-09-05 20:15:00 +02:00
{
2007-09-11 17:51:48 +02:00
int i;
2007-09-05 20:15:00 +02:00
Client *c;
XButtonPressedEvent *ev = &e->xbutton;
2007-09-10 12:06:54 +02:00
if(awesomeconf->statusbar.window == ev->window)
2007-09-05 20:15:00 +02:00
{
2007-09-11 17:51:48 +02:00
int x = 0;
2007-09-10 12:06:54 +02:00
for(i = 0; i < awesomeconf->ntags; i++)
2007-09-05 20:15:00 +02:00
{
2007-09-10 12:06:54 +02:00
x += textw(awesomeconf->tags[i]);
2007-09-05 20:15:00 +02:00
if(ev->x < x)
{
if(ev->button == Button1)
{
2007-09-10 12:06:54 +02:00
if(ev->state & awesomeconf->modkey)
uicb_tag(e->xany.display, awesomeconf, awesomeconf->tags[i]);
2007-09-05 20:15:00 +02:00
else
2007-09-10 12:06:54 +02:00
uicb_view(e->xany.display, awesomeconf, awesomeconf->tags[i]);
2007-09-05 20:15:00 +02:00
}
else if(ev->button == Button3)
{
2007-09-10 12:06:54 +02:00
if(ev->state & awesomeconf->modkey)
uicb_toggletag(e->xany.display, awesomeconf, awesomeconf->tags[i]);
2007-09-05 20:15:00 +02:00
else
2007-09-10 12:06:54 +02:00
uicb_toggleview(e->xany.display, awesomeconf, awesomeconf->tags[i]);
2007-09-05 20:15:00 +02:00
}
return;
}
}
2007-09-10 12:06:54 +02:00
if((ev->x < x + awesomeconf->statusbar.width) && ev->button == Button1)
uicb_setlayout(e->xany.display, awesomeconf, NULL);
2007-09-05 20:15:00 +02:00
}
else if((c = getclient(ev->window)))
{
2007-09-10 12:06:54 +02:00
focus(c->display, &dc, c, ev->same_screen, awesomeconf);
if(CLEANMASK(ev->state) != awesomeconf->modkey)
2007-09-05 20:15:00 +02:00
return;
if(ev->button == Button1 && (IS_ARRANGE(floating) || c->isfloating))
{
2007-09-10 12:06:54 +02:00
restack(e->xany.display, awesomeconf);
movemouse(c, awesomeconf);
2007-09-05 20:15:00 +02:00
}
else if(ev->button == Button2)
2007-09-10 12:06:54 +02:00
uicb_zoom(e->xany.display, awesomeconf, NULL);
2007-09-05 20:15:00 +02:00
else if(ev->button == Button3 && (IS_ARRANGE(floating) || c->isfloating) && !c->isfixed)
{
2007-09-10 12:06:54 +02:00
restack(e->xany.display, awesomeconf);
resizemouse(c, awesomeconf);
2007-09-05 20:15:00 +02:00
}
}
else if(DefaultRootWindow(e->xany.display) == ev->window && !sel)
{
if(ev->button == Button4)
2007-09-10 12:06:54 +02:00
uicb_tag_viewnext(e->xany.display, awesomeconf, NULL);
2007-09-05 20:15:00 +02:00
else if(ev->button == Button5)
2007-09-10 12:06:54 +02:00
uicb_tag_viewprev(e->xany.display, awesomeconf, NULL);
2007-09-05 20:15:00 +02:00
}
}
void
2007-09-10 12:06:54 +02:00
handle_event_configurerequest(XEvent * e, awesome_config *awesomeconf __attribute__ ((unused)))
2007-09-05 20:15:00 +02:00
{
Client *c;
XConfigureRequestEvent *ev = &e->xconfigurerequest;
XWindowChanges wc;
if((c = getclient(ev->window)))
{
c->ismax = False;
if(ev->value_mask & CWBorderWidth)
c->border = ev->border_width;
if(c->isfixed || c->isfloating || IS_ARRANGE(floating))
{
if(ev->value_mask & CWX)
c->x = ev->x;
if(ev->value_mask & CWY)
c->y = ev->y;
if(ev->value_mask & CWWidth)
c->w = ev->width;
if(ev->value_mask & CWHeight)
c->h = ev->height;
2007-09-07 17:19:03 +02:00
if((c->x + c->w) > DisplayWidth(c->display, DefaultScreen(c->display)) && c->isfloating)
c->x = DisplayWidth(c->display, DefaultScreen(c->display)) / 2 - c->w / 2; /* center in x direction */
if((c->y + c->h) > DisplayHeight(c->display, DefaultScreen(c->display)) && c->isfloating)
c->y = DisplayHeight(c->display, DefaultScreen(c->display)) / 2 - c->h / 2; /* center in y direction */
2007-09-05 20:15:00 +02:00
if((ev->value_mask & (CWX | CWY)) && !(ev->value_mask & (CWWidth | CWHeight)))
configure(c);
2007-09-10 12:06:54 +02:00
if(isvisible(c, awesomeconf->selected_tags, awesomeconf->ntags))
2007-09-05 20:15:00 +02:00
XMoveResizeWindow(e->xany.display, c->win, c->x, c->y, c->w, c->h);
}
else
configure(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;
XConfigureWindow(e->xany.display, ev->window, ev->value_mask, &wc);
}
XSync(e->xany.display, False);
}
void
2007-09-10 12:06:54 +02:00
handle_event_configurenotify(XEvent * e, awesome_config *awesomeconf)
2007-09-05 20:15:00 +02:00
{
XConfigureEvent *ev = &e->xconfigure;
if(ev->window == DefaultRootWindow(e->xany.display)
&& (ev->width != DisplayWidth(e->xany.display, DefaultScreen(e->xany.display))
|| ev->height != DisplayHeight(e->xany.display, DefaultScreen(e->xany.display))))
2007-09-05 20:15:00 +02:00
{
2007-09-07 17:19:03 +02:00
DisplayWidth(e->xany.display, DefaultScreen(e->xany.display)) = ev->width;
DisplayHeight(e->xany.display, DefaultScreen(e->xany.display)) = ev->height;
2007-09-05 20:15:00 +02:00
XFreePixmap(e->xany.display, dc.drawable);
dc.drawable = XCreatePixmap(e->xany.display, DefaultRootWindow(e->xany.display),
DisplayWidth(e->xany.display, DefaultScreen(e->xany.display)),
awesomeconf->statusbar.height, DefaultDepth(e->xany.display, DefaultScreen(e->xany.display)));
XResizeWindow(e->xany.display, awesomeconf->statusbar.window,
DisplayWidth(e->xany.display, DefaultScreen(e->xany.display)), awesomeconf->statusbar.height);
2007-09-10 12:06:54 +02:00
updatebarpos(e->xany.display, awesomeconf->statusbar);
arrange(e->xany.display, awesomeconf);
2007-09-05 20:15:00 +02:00
}
}
void
2007-09-10 12:06:54 +02:00
handle_event_destroynotify(XEvent * e, awesome_config *awesomeconf)
2007-09-05 20:15:00 +02:00
{
Client *c;
XDestroyWindowEvent *ev = &e->xdestroywindow;
if((c = getclient(ev->window)))
2007-09-10 12:06:54 +02:00
unmanage(c, &dc, WithdrawnState, awesomeconf);
2007-09-05 20:15:00 +02:00
}
void
2007-09-10 12:06:54 +02:00
handle_event_enternotify(XEvent * e, awesome_config *awesomeconf)
2007-09-05 20:15:00 +02:00
{
Client *c;
XCrossingEvent *ev = &e->xcrossing;
if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
return;
if((c = getclient(ev->window)))
2007-09-10 12:06:54 +02:00
focus(c->display, &dc, c, ev->same_screen, awesomeconf);
2007-09-05 20:15:00 +02:00
else if(ev->window == DefaultRootWindow(e->xany.display))
2007-09-10 12:06:54 +02:00
focus(e->xany.display, &dc, NULL, True, awesomeconf);
2007-09-05 20:15:00 +02:00
}
void
2007-09-10 12:06:54 +02:00
handle_event_expose(XEvent * e, awesome_config *awesomeconf)
2007-09-05 20:15:00 +02:00
{
XExposeEvent *ev = &e->xexpose;
2007-09-10 12:06:54 +02:00
if(!ev->count && awesomeconf->statusbar.window == ev->window)
drawstatus(e->xany.display, awesomeconf);
2007-09-05 20:15:00 +02:00
}
void
2007-09-10 12:06:54 +02:00
handle_event_keypress(XEvent * e, awesome_config *awesomeconf)
2007-09-05 20:15:00 +02:00
{
int i;
KeySym keysym;
XKeyEvent *ev = &e->xkey;
keysym = XKeycodeToKeysym(e->xany.display, (KeyCode) ev->keycode, 0);
2007-09-10 12:06:54 +02:00
for(i = 0; i < awesomeconf->nkeys; i++)
if(keysym == awesomeconf->keys[i].keysym
&& CLEANMASK(awesomeconf->keys[i].mod) == CLEANMASK(ev->state) && awesomeconf->keys[i].func)
awesomeconf->keys[i].func(e->xany.display, awesomeconf, awesomeconf->keys[i].arg);
2007-09-05 20:15:00 +02:00
}
void
2007-09-10 12:06:54 +02:00
handle_event_leavenotify(XEvent * e, awesome_config *awesomeconf)
2007-09-05 20:15:00 +02:00
{
XCrossingEvent *ev = &e->xcrossing;
if((ev->window == DefaultRootWindow(e->xany.display)) && !ev->same_screen)
2007-09-10 12:06:54 +02:00
focus(e->xany.display, &dc, NULL, ev->same_screen, awesomeconf);
2007-09-05 20:15:00 +02:00
}
void
2007-09-10 12:06:54 +02:00
handle_event_mappingnotify(XEvent * e, awesome_config *awesomeconf)
2007-09-05 20:15:00 +02:00
{
XMappingEvent *ev = &e->xmapping;
XRefreshKeyboardMapping(ev);
if(ev->request == MappingKeyboard)
2007-09-10 12:06:54 +02:00
grabkeys(e->xany.display, awesomeconf);
2007-09-05 20:15:00 +02:00
}
void
2007-09-10 12:06:54 +02:00
handle_event_maprequest(XEvent * e, awesome_config *awesomeconf)
2007-09-05 20:15:00 +02:00
{
static XWindowAttributes wa;
XMapRequestEvent *ev = &e->xmaprequest;
if(!XGetWindowAttributes(e->xany.display, ev->window, &wa))
return;
if(wa.override_redirect)
return;
if(!getclient(ev->window))
2007-09-10 12:06:54 +02:00
manage(e->xany.display, &dc, ev->window, &wa, awesomeconf);
2007-09-05 20:15:00 +02:00
}
void
2007-09-10 12:06:54 +02:00
handle_event_propertynotify(XEvent * e, awesome_config *awesomeconf)
2007-09-05 20:15:00 +02:00
{
Client *c;
Window trans;
XPropertyEvent *ev = &e->xproperty;
if(ev->state == PropertyDelete)
return; /* ignore */
if((c = getclient(ev->window)))
{
switch (ev->atom)
{
case XA_WM_TRANSIENT_FOR:
XGetTransientForHint(e->xany.display, c->win, &trans);
if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
2007-09-10 12:06:54 +02:00
arrange(e->xany.display, awesomeconf);
2007-09-05 20:15:00 +02:00
break;
case XA_WM_NORMAL_HINTS:
updatesizehints(c);
break;
}
2007-09-10 16:49:54 +02:00
if(ev->atom == XA_WM_NAME || ev->atom == XInternAtom(c->display, "_NET_WM_NAME", False))
2007-09-05 20:15:00 +02:00
{
updatetitle(c);
if(c == sel)
2007-09-10 12:06:54 +02:00
drawstatus(e->xany.display, awesomeconf);
2007-09-05 20:15:00 +02:00
}
}
}
void
2007-09-10 12:06:54 +02:00
handle_event_unmapnotify(XEvent * e, awesome_config *awesomeconf)
2007-09-05 20:15:00 +02:00
{
Client *c;
XUnmapEvent *ev = &e->xunmap;
if((c = getclient(ev->window)) && ev->event == DefaultRootWindow(e->xany.display) && (ev->send_event || !c->unmapped--))
2007-09-10 12:06:54 +02:00
unmanage(c, &dc, WithdrawnState, awesomeconf);
2007-09-05 20:15:00 +02:00
}
void
2007-09-10 12:06:54 +02:00
grabkeys(Display *disp, awesome_config *awesomeconf)
2007-09-05 20:15:00 +02:00
{
int i;
KeyCode code;
XUngrabKey(disp, AnyKey, AnyModifier, DefaultRootWindow(disp));
2007-09-10 12:06:54 +02:00
for(i = 0; i < awesomeconf->nkeys; i++)
2007-09-05 20:15:00 +02:00
{
2007-09-10 12:06:54 +02:00
code = XKeysymToKeycode(disp, awesomeconf->keys[i].keysym);
XGrabKey(disp, code, awesomeconf->keys[i].mod, DefaultRootWindow(disp), True, GrabModeAsync, GrabModeAsync);
XGrabKey(disp, code, awesomeconf->keys[i].mod | LockMask, DefaultRootWindow(disp), True, GrabModeAsync, GrabModeAsync);
XGrabKey(disp, code, awesomeconf->keys[i].mod | awesomeconf->numlockmask, DefaultRootWindow(disp), True, GrabModeAsync, GrabModeAsync);
XGrabKey(disp, code, awesomeconf->keys[i].mod | awesomeconf->numlockmask | LockMask, DefaultRootWindow(disp), True,
2007-09-05 20:15:00 +02:00
GrabModeAsync, GrabModeAsync);
}
}