forked from mackstann/tinywm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinywm.c
54 lines (47 loc) · 1.9 KB
/
tinywm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* TinyWM is written by Nick Welch <[email protected]> in 2005 & 2011.
*
* This software is in the public domain
* and is provided AS IS, with NO WARRANTY. */
#include <X11/Xlib.h>
#define MAX(a, b) ((a) > (b) ? (a) : (b))
int main(void)
{
Display * dpy;
XWindowAttributes attr;
XButtonEvent start;
XEvent ev;
if(!(dpy = XOpenDisplay(0x0))) return 1;
XGrabKey(dpy, XKeysymToKeycode(dpy, XStringToKeysym("F1")), Mod1Mask,
DefaultRootWindow(dpy), True, GrabModeAsync, GrabModeAsync);
XGrabButton(dpy, 1, Mod1Mask, DefaultRootWindow(dpy), True,
ButtonPressMask|ButtonReleaseMask|PointerMotionMask, GrabModeAsync, GrabModeAsync, None, None);
XGrabButton(dpy, 3, Mod1Mask, 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)
{
XRaiseWindow(dpy, ev.xkey.subwindow);
XSetInputFocus(dpy, ev.xkey.subwindow, RevertToParent, CurrentTime);
}
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;
}
}