ryudo/event.c

698 lines
16 KiB
C
Raw Normal View History

2019-12-02 18:23:00 +00:00
/*
* Copyright (c) 2019 Iris Lightshard, 2005 Rus Cox, 1994-1996 David Hogan
2019-12-02 18:23:00 +00:00
* see README for licence details
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
2019-12-02 18:23:00 +00:00
#include <X11/X.h>
#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/extensions/shape.h>
#include <X11/extensions/Xrandr.h>
#include "config.h"
2019-12-02 18:23:00 +00:00
#include "dat.h"
#include "fns.h"
#include "patchlevel.h"
2021-02-26 19:50:23 +00:00
void mainloop(int shape_event) {
static XEvent ev;
static Client* c;
static int monitor;
2021-02-26 18:01:22 +00:00
for (;;) {
getevent(&ev);
#ifdef DEBUG_EV
if (debug) {
ShowEvent(&ev);
printf("\n");
}
2019-12-02 18:23:00 +00:00
#endif
2021-02-26 18:01:22 +00:00
switch (ev.type) {
default:
#ifdef SHAPE
if (shape && ev.type == shape_event)
shapenotify((XShapeEvent*)&ev);
else
2019-12-02 18:23:00 +00:00
#endif
2021-02-26 18:01:22 +00:00
fprintf(stderr, "ryudo: unknown ev.type %d\n", ev.type);
break;
/* XRANDR related events; idk what the define for this is */
case 89:
if (XRRUpdateConfiguration(&ev)) {
fetchmonitorinfo();
monitor = getmonitorbyclient(c);
if (monitor < 0 || monitor >= nmonitors) {
break;
}
for (c = clients; c; c = c->next) {
monitor = getmonitorbyclient(c);
wrangle(c, monitorinfo[monitor]);
}
}
break;
2021-02-26 19:50:23 +00:00
case KeyPress:
keypress(&ev.xkey);
break;
case KeyRelease:
keyrelease(&ev.xkey);
break;
case ButtonPress:
button(&ev.xbutton);
XAllowEvents(dpy, ReplayPointer, ev.xbutton.time);
2021-02-26 19:50:23 +00:00
break;
case ButtonRelease:
XAllowEvents(dpy, ReplayPointer, ev.xbutton.time);
2021-02-26 19:50:23 +00:00
break;
case MapRequest:
mapreq(&ev.xmaprequest);
break;
case ConfigureRequest:
configurereq(&ev.xconfigurerequest);
break;
case CirculateRequest:
circulatereq(&ev.xcirculaterequest);
break;
case UnmapNotify:
unmap(&ev.xunmap);
break;
case CreateNotify:
newwindow(&ev.xcreatewindow);
break;
case DestroyNotify:
destroy(ev.xdestroywindow.window);
break;
case ClientMessage:
clientmesg(&ev.xclient);
break;
case ColormapNotify:
cmap(&ev.xcolormap);
break;
case PropertyNotify:
property(&ev.xproperty);
break;
2021-02-26 18:01:22 +00:00
case SelectionClear:
fprintf(stderr, "ryudo: SelectionClear (this should not happen)\n");
break;
case SelectionNotify:
fprintf(stderr, "ryudo: SelectionNotify (this should not happen)\n");
break;
case SelectionRequest:
fprintf(stderr, "ryudo: SelectionRequest (this should not happen)\n");
break;
2021-02-26 19:50:23 +00:00
case EnterNotify:
enter(&ev.xcrossing);
break;
case LeaveNotify:
leave(&ev.xcrossing);
break;
case ReparentNotify:
reparent(&ev.xreparent);
break;
case FocusIn:
focusin(&ev.xfocus);
break;
case MotionNotify:
motionnotify(&ev.xmotion);
break;
2021-02-26 18:01:22 +00:00
case Expose:
case NoExpose:
case FocusOut:
case ConfigureNotify:
case MapNotify:
case MappingNotify:
case GraphicsExpose:
/* not interested */
trace("ignore", 0, &ev);
break;
}
}
2019-12-02 18:23:00 +00:00
}
2021-02-26 19:50:23 +00:00
void configurereq(XConfigureRequestEvent* e) {
2021-02-26 18:01:22 +00:00
XWindowChanges wc;
Client* c;
XRRMonitorInfo monitor;
int m;
2021-02-26 18:01:22 +00:00
/* we don't set curtime as nothing here uses it */
c = getclient(e->window, 0);
trace("configurereq", c, e);
e->value_mask &= ~CWSibling;
if (c) {
m = getmonitorbyclient(c);
monitor = monitorinfo[m];
2021-02-26 19:50:23 +00:00
if (e->value_mask & CWX)
c->x = e->x;
if (e->value_mask & CWY)
c->y = e->y;
if (e->value_mask & CWWidth)
c->dx = e->width;
if (e->value_mask & CWHeight)
c->dy = e->height;
if (
e->value_mask & CWBorderWidth && e->width >= monitor.width &&
e->height >= monitor.height)
c->border = 0;
else
c->border = BORDER;
2021-02-26 18:01:22 +00:00
if (e->value_mask & CWStackMode) {
if (e->detail == Above)
top(c);
else
e->value_mask &= ~CWStackMode;
}
e->value_mask |= CWX | CWY | CWHeight | CWWidth;
if (c->parent != c->screen->root && c->window == e->window) {
wc.x = c->x - c->border;
wc.y = c->y - c->border;
wc.width = c->dx + c->border + c->border;
wc.height = c->dy + c->border + c->border;
wc.sibling = None;
wc.border_width = c->border;
2021-02-26 18:01:22 +00:00
wc.stack_mode = e->detail;
XConfigureWindow(dpy, c->parent, e->value_mask, &wc);
if (e->value_mask & CWStackMode) {
if (c->virt > 0 && c->virt != virt)
switch_to(c->virt);
if (hidden(c)) {
unhidec(c, 1);
}
2021-02-26 18:01:22 +00:00
top(c);
active(c);
}
}
}
if (c && c->parent != c->screen->root) {
wc.x = c->border;
wc.y = c->border;
} else {
wc.x = c->x;
wc.y = c->y;
}
wc.width = c->dx;
wc.height = c->dy;
wc.border_width = 0;
wc.sibling = None;
wc.stack_mode = Above;
e->value_mask &= ~CWStackMode;
e->value_mask |= CWBorderWidth;
XConfigureWindow(dpy, c->window, e->value_mask, &wc);
2019-12-02 18:23:00 +00:00
}
2021-02-26 19:50:23 +00:00
void mapreq(XMapRequestEvent* e) {
2021-02-26 18:01:22 +00:00
Client* c;
2024-11-29 16:52:32 +00:00
int i, m;
XRRMonitorInfo monitor;
2024-11-29 16:52:32 +00:00
#ifdef MONITORFOLLOWSMOUSE
m = getmonitorbymouse();
#else
m = getmonitorbyclient(current);
#endif
monitor = monitorinfo[m];
2021-02-26 18:01:22 +00:00
curtime = CurrentTime;
c = getclient(e->window, 0);
trace("mapreq", c, e);
if (c == 0 || c->window != e->window) {
/* workaround for stupid NCDware */
fprintf(
stderr,
"ryudo: bad mapreq c %p w %x, rescanning\n",
(void*)c,
(int)e->window);
2021-02-26 19:50:23 +00:00
for (i = 0; i < num_screens; i++)
scanwins(&screens[i]);
2021-02-26 18:01:22 +00:00
c = getclient(e->window, 0);
if (c == 0 || c->window != e->window) {
fprintf(stderr, "ryudo: window not found after rescan\n");
return;
}
}
2024-11-29 16:52:32 +00:00
if (kbLaunch && CASCADE_DENSITY < 2) {
// usleep(MICROSLEEP_DELAY);
#ifdef CENTERVMAX
centerclient(c, monitor, 1);
#else
centerclient(c, monitor, 0);
#endif
kbLaunch = 0;
2024-11-29 16:52:32 +00:00
} else if (CASCADE_DENSITY >= 2 && c->trans == None) {
getcascadecoords(c->window, m, &(c->x), &(c->y));
}
2021-02-26 18:01:22 +00:00
switch (c->state) {
case WithdrawnState:
if (c->parent == c->screen->root) {
2021-02-26 19:50:23 +00:00
if (!manage(c, 0))
return;
2021-02-26 18:01:22 +00:00
break;
}
XReparentWindow(dpy, c->window, c->parent, BORDER, BORDER);
2021-02-26 18:01:22 +00:00
XAddToSaveSet(dpy, c->window);
/* fall through... */
case NormalState:
XMapWindow(dpy, c->window);
XMapRaised(dpy, c->parent);
top(c);
setstate(c, NormalState);
2021-02-26 19:50:23 +00:00
if (c->trans != None && current && c->trans == current->window)
active(c);
break;
case IconicState:
unhidec(c, 1);
2021-02-26 18:01:22 +00:00
break;
}
2019-12-02 18:23:00 +00:00
}
2021-02-26 19:50:23 +00:00
void unmap(XUnmapEvent* e) {
Client *c, *revertc;
int m;
2021-02-26 18:01:22 +00:00
curtime = CurrentTime;
c = getclient(e->window, 0);
2021-02-26 18:01:22 +00:00
if (c) {
m = getmonitorbyclient(c);
2021-02-26 18:01:22 +00:00
switch (c->state) {
case IconicState:
if (e->send_event) {
unhidec(c, 0);
withdraw(c);
}
break;
case NormalState:
if (c == current) {
nofocus();
if (revertc = getclient(getrevert(c), 0)) {
top(revertc);
active(revertc);
} else {
shuffleonmonitor(m);
}
}
2021-02-26 19:50:23 +00:00
if (!c->reparenting)
withdraw(c);
2021-02-26 18:01:22 +00:00
break;
}
c->reparenting = 0;
}
2019-12-02 18:23:00 +00:00
}
2021-02-26 19:50:23 +00:00
void circulatereq(XCirculateRequestEvent* e) {
2021-02-26 18:01:22 +00:00
fprintf(stderr, "It must be the warlock Krill!\n"); /* ☺ */
2019-12-02 18:23:00 +00:00
}
2021-02-26 19:50:23 +00:00
void newwindow(XCreateWindowEvent* e) {
2021-02-26 18:01:22 +00:00
Client* c;
ScreenInfo* s;
/* we don't set curtime as nothing here uses it */
2021-02-26 19:50:23 +00:00
if (e->override_redirect)
return;
2021-02-26 18:01:22 +00:00
c = getclient(e->window, 1);
if (c && c->window == e->window && (s = getscreen(e->parent))) {
c->x = e->x;
c->y = e->y;
c->dx = e->width;
c->dy = e->height;
c->border = e->border_width;
c->screen = s;
2021-02-26 19:50:23 +00:00
if (c->parent == None)
c->parent = c->screen->root;
2021-02-26 18:01:22 +00:00
}
#if defined(OPACITY) && defined(TRANSPARENTLIST)
if (istransparent(c)) {
/* We need to set the atom on both the window and its parent */
const Atom alpha_atom = XInternAtom(dpy, "_NET_WM_WINDOW_OPACITY", 0);
unsigned long opacity = (unsigned long)OPACITY * 0x1010101;
XChangeProperty(
dpy,
c->window,
alpha_atom,
XA_CARDINAL,
32,
PropModeReplace,
(unsigned char*)&opacity,
1);
XChangeProperty(
dpy,
c->parent,
alpha_atom,
XA_CARDINAL,
32,
PropModeReplace,
(unsigned char*)&opacity,
1);
}
#endif
2019-12-02 18:23:00 +00:00
}
2021-02-26 19:50:23 +00:00
void destroy(Window w) {
2021-02-26 18:01:22 +00:00
int i;
Client *c, *revertc;
Window revert;
2021-02-26 18:01:22 +00:00
curtime = CurrentTime;
c = getclient(w, 0);
2021-02-26 19:50:23 +00:00
if (c == 0)
return;
2021-02-26 18:01:22 +00:00
revert = getrevert(c);
2021-02-26 18:01:22 +00:00
if (numvirtuals > 1)
for (i = 0; i < numvirtuals; i++)
2021-02-26 19:50:23 +00:00
if (currents[i] == c)
currents[i] = 0;
2021-02-26 18:01:22 +00:00
rmclient(c);
if (!current && (revertc = getclient(revert, 0))) {
top(revertc);
active(revertc);
}
2021-02-26 18:01:22 +00:00
/* flush any errors generated by the window's sudden demise */
ignore_badwindow = 1;
XSync(dpy, False);
ignore_badwindow = 0;
2019-12-02 18:23:00 +00:00
}
2021-02-26 19:50:23 +00:00
void clientmesg(XClientMessageEvent* e) {
2021-02-26 18:01:22 +00:00
Client* c;
curtime = CurrentTime;
if (e->message_type == exit_rio) {
cleanup();
exit(0);
}
if (e->message_type == restart_rio) {
fprintf(stderr, "*** ryudo restarting ***\n");
2021-02-26 18:01:22 +00:00
cleanup();
execvp(myargv[0], myargv);
perror("ryudo: exec failed");
exit(1);
}
if (e->message_type == wm_protocols) {
2021-02-26 19:50:23 +00:00
return;
}
2021-02-26 18:01:22 +00:00
if (e->message_type == wm_change_state) {
c = getclient(e->window, 0);
if (e->format == 32 && e->data.l[0] == IconicState && c != 0) {
2021-02-26 19:50:23 +00:00
if (normal(c))
hide(c);
2021-02-26 18:01:22 +00:00
} else
fprintf(
stderr,
"ryudo: WM_CHANGE_STATE: format %d data %d w 0x%x\n",
(int)e->format,
(int)e->data.l[0],
(int)e->window);
return;
}
if (e->message_type == wm_state) {
// c = getclient(e->window, 0);
// if(e->format == 32 && e->data.l[1] == wm_state_fullscreen){
// }else
fprintf(
stderr,
"ryudo: WM_STATE: format %d data %d %d w 0x%x\n",
(int)e->format,
(int)e->data.l[0],
(int)e->data.l[1],
(int)e->window);
return;
}
fprintf(
stderr,
"ryudo: strange ClientMessage, type 0x%x window 0x%x\n",
(int)e->message_type,
(int)e->window);
2019-12-02 18:23:00 +00:00
}
2021-02-26 19:50:23 +00:00
void cmap(XColormapEvent* e) {
2021-02-26 18:01:22 +00:00
Client* c;
int i;
/* we don't set curtime as nothing here uses it */
if (e->new) {
c = getclient(e->window, 0);
if (c) {
c->cmap = e->colormap;
2021-02-26 19:50:23 +00:00
if (c == current)
cmapfocus(c);
2021-02-26 18:01:22 +00:00
} else
for (c = clients; c; c = c->next) {
for (i = 0; i < c->ncmapwins; i++)
if (c->cmapwins[i] == e->window) {
c->wmcmaps[i] = e->colormap;
2021-02-26 19:50:23 +00:00
if (c == current)
cmapfocus(c);
2021-02-26 18:01:22 +00:00
return;
}
}
}
2019-12-02 18:23:00 +00:00
}
2021-02-26 19:50:23 +00:00
void property(XPropertyEvent* e) {
2021-02-26 18:01:22 +00:00
Atom a;
int delete;
Client* c;
long msize;
/* we don't set curtime as nothing here uses it */
a = e->atom;
delete = (e->state == PropertyDelete);
c = getclient(e->window, 0);
2021-02-26 19:50:23 +00:00
if (c == 0)
return;
2021-02-26 18:01:22 +00:00
switch (a) {
case XA_WM_ICON_NAME:
2021-02-26 19:50:23 +00:00
if (c->iconname != 0)
XFree((char*)c->iconname);
2021-02-26 18:01:22 +00:00
c->iconname = delete ? 0 : getprop(c->window, a);
setlabel(c);
renamec(c, c->label);
return;
case XA_WM_NAME:
2021-02-26 19:50:23 +00:00
if (c->name != 0)
XFree((char*)c->name);
2021-02-26 18:01:22 +00:00
c->name = delete ? 0 : getprop(c->window, a);
setlabel(c);
renamec(c, c->label);
return;
2021-02-26 19:50:23 +00:00
case XA_WM_TRANSIENT_FOR:
gettrans(c);
return;
2021-02-26 18:01:22 +00:00
case XA_WM_HINTS:
case XA_WM_SIZE_HINTS:
case XA_WM_ZOOM_HINTS:
/* placeholders to not forget. ignore for now. -Axel */
return;
case XA_WM_NORMAL_HINTS:
if (
XGetWMNormalHints(dpy, c->window, &c->size, &msize) == 0 ||
c->size.flags == 0)
c->size.flags = PSize; /* not specified - punt */
return;
}
if (a == _rio_hold_mode) {
c->hold = getiprop(c->window, _rio_hold_mode);
2021-02-26 19:50:23 +00:00
if (c == current)
draw_border(c, 1);
2021-02-26 18:01:22 +00:00
} else if (a == wm_colormaps) {
getcmaps(c);
2021-02-26 19:50:23 +00:00
if (c == current)
cmapfocus(c);
2021-02-26 18:01:22 +00:00
}
2019-12-02 18:23:00 +00:00
}
2021-02-26 19:50:23 +00:00
void reparent(XReparentEvent* e) {
2021-02-26 18:01:22 +00:00
Client* c;
XWindowAttributes attr;
ScreenInfo* s;
/* we don't set curtime as nothing here uses it */
2021-02-26 19:50:23 +00:00
if (!getscreen(e->event) || e->override_redirect)
return;
2021-02-26 18:01:22 +00:00
if ((s = getscreen(e->parent)) != 0) {
c = getclient(e->window, 1);
if (c != 0 && (c->dx == 0 || c->dy == 0)) {
/* flush any errors */
ignore_badwindow = 1;
XGetWindowAttributes(dpy, c->window, &attr);
XSync(dpy, False);
ignore_badwindow = 0;
c->x = attr.x;
c->y = attr.y;
c->dx = attr.width;
c->dy = attr.height;
c->border = attr.border_width;
c->screen = s;
2021-02-26 19:50:23 +00:00
if (c->parent == None)
c->parent = c->screen->root;
2021-02-26 18:01:22 +00:00
}
} else {
c = getclient(e->window, 0);
2021-02-26 19:50:23 +00:00
if (c != 0 && (c->parent == c->screen->root || withdrawn(c)))
rmclient(c);
2021-02-26 18:01:22 +00:00
}
2019-12-02 18:23:00 +00:00
}
2021-02-26 18:01:22 +00:00
#ifdef SHAPE
2021-02-26 19:50:23 +00:00
void shapenotify(XShapeEvent* e) {
2021-02-26 18:01:22 +00:00
Client* c;
2019-12-02 18:23:00 +00:00
2021-02-26 18:01:22 +00:00
/* we don't set curtime as nothing here uses it */
c = getclient(e->window, 0);
2021-02-26 19:50:23 +00:00
if (c == 0)
return;
2019-12-02 18:23:00 +00:00
2021-02-26 18:01:22 +00:00
setshape(c);
2019-12-02 18:23:00 +00:00
}
#endif
2021-02-26 19:50:23 +00:00
void enter(XCrossingEvent* e) {
2021-02-26 18:01:22 +00:00
Client* c;
curtime = e->time;
2024-09-30 02:58:18 +00:00
if (e->mode != NotifyGrab || e->detail != NotifyNonlinearVirtual)
return;
2021-02-26 18:01:22 +00:00
c = getclient(e->window, 0);
if (c != 0 && c != current) {
/* someone grabbed the pointer; make them current */
2024-09-30 02:58:18 +00:00
XMapRaised(dpy, c->parent);
2021-02-26 18:01:22 +00:00
top(c);
active(c);
}
2019-12-02 18:23:00 +00:00
}
2021-02-26 19:50:23 +00:00
void leave(XCrossingEvent* e) {
2021-02-26 18:01:22 +00:00
Client* c;
c = getclient(e->window, 0);
2021-02-26 19:50:23 +00:00
if (c)
XUndefineCursor(dpy, c->parent);
2021-02-26 18:01:22 +00:00
/* XDefineCursor(dpy, c->parent, c->screen->arrow); */
2019-12-02 18:23:00 +00:00
}
2021-02-26 19:50:23 +00:00
void focusin(XFocusChangeEvent* e) {
2021-02-26 18:01:22 +00:00
Client* c;
curtime = CurrentTime;
2021-02-26 19:50:23 +00:00
if (e->detail != NotifyNonlinearVirtual)
return;
2021-02-26 18:01:22 +00:00
c = getclient(e->window, 0);
if (c != 0 && c->window == e->window && c != current) {
#ifdef AUTOSTICK
if (!isautostick(c)) {
2021-02-26 18:01:22 +00:00
#endif
/* someone grabbed keyboard or seized focus; make them current */
XMapRaised(dpy, c->parent);
top(c);
active(c);
#ifdef AUTOSTICK
}
#endif
2021-02-26 18:01:22 +00:00
}
2019-12-02 18:23:00 +00:00
}
2021-02-26 19:50:23 +00:00
BorderOrient borderorient(Client* c, int x, int y) {
2021-02-26 18:01:22 +00:00
if (x <= BORDER) {
if (y <= CORNER) {
2021-02-26 19:50:23 +00:00
if (debug)
fprintf(stderr, "topleft\n");
2021-02-26 18:01:22 +00:00
return BorderWNW;
}
if (y >= (c->dy + 2 * BORDER) - CORNER) {
2021-02-26 19:50:23 +00:00
if (debug)
fprintf(stderr, "botleft\n");
2021-02-26 18:01:22 +00:00
return BorderWSW;
}
if (y > CORNER && y < (c->dy + 2 * BORDER) - CORNER) {
2021-02-26 19:50:23 +00:00
if (debug)
fprintf(stderr, "left\n");
2021-02-26 18:01:22 +00:00
return BorderW;
}
} else if (x <= CORNER) {
if (y <= BORDER) {
2021-02-26 19:50:23 +00:00
if (debug)
fprintf(stderr, "topleft\n");
2021-02-26 18:01:22 +00:00
return BorderNNW;
}
if (y >= (c->dy + BORDER)) {
2021-02-26 19:50:23 +00:00
if (debug)
fprintf(stderr, "botleft\n");
2021-02-26 18:01:22 +00:00
return BorderSSW;
}
} else if (x >= (c->dx + BORDER)) {
if (y <= CORNER) {
2021-02-26 19:50:23 +00:00
if (debug)
fprintf(stderr, "topright\n");
2021-02-26 18:01:22 +00:00
return BorderENE;
}
if (y >= (c->dy + 2 * BORDER) - CORNER) {
2021-02-26 19:50:23 +00:00
if (debug)
fprintf(stderr, "botright\n");
2021-02-26 18:01:22 +00:00
return BorderESE;
}
if (y > CORNER && y < (c->dy + 2 * BORDER) - CORNER) {
2021-02-26 19:50:23 +00:00
if (debug)
fprintf(stderr, "right\n");
2021-02-26 18:01:22 +00:00
return BorderE;
}
} else if (x >= (c->dx + 2 * BORDER) - CORNER) {
if (y <= BORDER) {
2021-02-26 19:50:23 +00:00
if (debug)
fprintf(stderr, "topright\n");
2021-02-26 18:01:22 +00:00
return BorderNNE;
}
if (y >= (c->dy + BORDER)) {
2021-02-26 19:50:23 +00:00
if (debug)
fprintf(stderr, "botright\n");
2021-02-26 18:01:22 +00:00
return BorderSSE;
}
} else if (x > CORNER && x < (c->dx + 2 * BORDER) - CORNER) {
if (y <= BORDER) {
2021-02-26 19:50:23 +00:00
if (debug)
fprintf(stderr, "top\n");
2021-02-26 18:01:22 +00:00
return BorderN;
}
if (y >= (c->dy + BORDER)) {
2021-02-26 19:50:23 +00:00
if (debug)
fprintf(stderr, "bot\n");
2021-02-26 18:01:22 +00:00
return BorderS;
}
}
return BorderUnknown;
2019-12-02 18:23:00 +00:00
}
2021-02-26 19:50:23 +00:00
void motionnotify(XMotionEvent* e) {
2021-02-26 18:01:22 +00:00
Client* c;
BorderOrient bl;
c = getclient(e->window, 0);
if (c) {
bl = borderorient(c, e->x, e->y);
if (bl == BorderUnknown)
XUndefineCursor(dpy, c->parent);
else
XDefineCursor(dpy, c->parent, c->screen->bordcurs[bl]);
}
2019-12-02 18:23:00 +00:00
}