55 lines
No EOL
2.1 KiB
C
55 lines
No EOL
2.1 KiB
C
// Taken from https://github.com/mackstann/tinywm
|
|
|
|
#include <X11/Xlib.h>
|
|
#include <X11/keysym.h>
|
|
|
|
#define MODMASK Mod1Mask
|
|
|
|
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
|
|
|
int main() {
|
|
Display* dpy;
|
|
XWindowAttributes attr;
|
|
|
|
// Used to store the state of the cursor when resizing
|
|
XButtonEvent start;
|
|
|
|
XEvent ev;
|
|
|
|
if(!(dpy = XOpenDisplay(0x0))) return 1;
|
|
|
|
XGrabKey(dpy, XKeysymToKeycode(dpy, XStringToKeysym("F1")), MODMASK, DefaultRootWindow(dpy), True, GrabModeAsync, GrabModeAsync);
|
|
XGrabKey(dpy, XKeysymToKeycode(dpy, XStringToKeysym("Q")), MODMASK, DefaultRootWindow(dpy), True, GrabModeAsync, GrabModeAsync);
|
|
XGrabButton(dpy, 1, MODMASK, DefaultRootWindow(dpy), True, ButtonPressMask|ButtonReleaseMask|PointerMotionMask, GrabModeAsync, GrabModeAsync, None, None);
|
|
XGrabButton(dpy, 3, MODMASK, DefaultRootWindow(dpy), True, ButtonPressMask|ButtonReleaseMask|PointerMotionMask, GrabModeAsync, GrabModeAsync, None, None);
|
|
|
|
start.subwindow = None;
|
|
|
|
for (;;) {
|
|
XNextEvent(dpy, &ev);
|
|
|
|
if (ev.type == KeyPress && ev.xkey.subwindow != None) {
|
|
KeySym keysym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0);
|
|
if (keysym == XK_q) {
|
|
XKillClient(dpy, ev.xkey.subwindow);
|
|
} else {
|
|
XRaiseWindow(dpy, ev.xkey.subwindow);
|
|
}
|
|
} else if (ev.type == ButtonPress && ev.xbutton.subwindow != None) {
|
|
XGetWindowAttributes(dpy, ev.xbutton.subwindow, &attr);
|
|
start = ev.xbutton;
|
|
} else if (ev.type == MotionNotify && start.subwindow != None) {
|
|
int xdiff = ev.xbutton.x_root - start.x_root;
|
|
int ydiff = ev.xbutton.y_root - start.y_root;
|
|
XMoveResizeWindow(dpy, start.subwindow,
|
|
attr.x + (start.button==1 ? xdiff : 0),
|
|
attr.y + (start.button==1 ? ydiff : 0),
|
|
MAX(1, attr.width + (start.button==3 ? xdiff : 0)),
|
|
MAX(1, attr.height + (start.button==3 ? ydiff : 0)));
|
|
} else if (ev.type == ButtonRelease) {
|
|
start.subwindow = None;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
} |