summaryrefslogtreecommitdiff
path: root/x-move-resize.c
blob: 53b39547b4fd974fc8c2a6f1acb93de5f12b2895 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* TinyWM is written by Nick Welch <mack AT incise.org>, 2005.
 *
 * This software is in the public domain
 * and is provided AS IS, with NO WARRANTY. */

#include <errno.h>
#include <sys/select.h>
#include <X11/Xlib.h>
#include <X11/cursorfont.h>

#define MAX(a, b) ((a) > (b) ? (a) : (b))

/* interruptibleXNextEvent() is taken from the Blender source and comes with
 * the following copyright notice: */

/* Copyright (c) Mark J. Kilgard, 1994, 1995, 1996. */

/* This program is freely distributable without licensing fees
 * and is provided without guarantee or warrantee expressed or
 * implied. This program is -not- in the public domain. */

/* Unlike XNextEvent, if a signal arrives, interruptibleXNextEvent will
 * return zero. */

static int interruptibleXNextEvent(Display *dpy, XEvent *event) {
	fd_set fds;
	int rc;
	int dpy_fd = ConnectionNumber(dpy);
	for (;;) {
		if (XPending(dpy)) {
			XNextEvent(dpy, event);
			return 1;
		}
		FD_ZERO(&fds);
		FD_SET(dpy_fd, &fds);
		rc = select(dpy_fd + 1, &fds, NULL, NULL, NULL);
		if (rc < 0) {
			if (errno == EINTR) {
				return 0;
			}
			//	LOG_ERROR("interruptibleXNextEvent(): select()\n");
		}
	}
}

int die(Display *dpy, int return_value) {
	XUngrabPointer(dpy, CurrentTime);
	XCloseDisplay(dpy);
	return return_value;
}

int main()
{
	Display * dpy;
	Window root;
	XWindowAttributes attr;
	XButtonEvent start;
	XEvent ev;
	Cursor cursor;
	int cursor_shape = XC_X_cursor;
	start.button = 0; start.x_root = 0; start.y_root = 0;

	if(!(dpy = XOpenDisplay(0x0))) return 1;

	root = DefaultRootWindow(dpy);

	XGrabButton(dpy, 1, 0, root, True, ButtonPressMask, GrabModeAsync,
			GrabModeAsync, None, None);
	XGrabButton(dpy, 3, 0, root, True, ButtonPressMask, GrabModeAsync,
			GrabModeAsync, None, None);


	cursor = XCreateFontCursor(dpy, cursor_shape);
	XDefineCursor(dpy, root, cursor);
	XSync (dpy, root);			/* give xterm a chance */
	if (XGrabPointer (dpy, root, False, (ButtonPressMask | PointerMotionMask | ButtonReleaseMask),
			GrabModeAsync, GrabModeAsync, None, cursor, CurrentTime) != GrabSuccess)
		return die(dpy, 1);

	for(;;)
	{
		if (! interruptibleXNextEvent(dpy, &ev))
			continue;
		switch(ev.type) {
			case ButtonPress:
				if (ev.xbutton.subwindow == None)
					break;
				if( XGrabPointer(dpy, ev.xbutton.subwindow, False,
						PointerMotionMask|ButtonReleaseMask, GrabModeAsync,
						GrabModeAsync, None, cursor, CurrentTime) != GrabSuccess)
					return die(dpy, 1);
				XGetWindowAttributes(dpy, ev.xbutton.subwindow, &attr);
				start = ev.xbutton;
				break;
			case MotionNotify: {
				int xdiff, ydiff;
				while (XCheckTypedEvent(dpy, MotionNotify, &ev));
				xdiff = ev.xbutton.x_root - start.x_root;
				ydiff = ev.xbutton.y_root - start.y_root;
				XMoveResizeWindow(dpy, ev.xmotion.window,
					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)));
				}
				break;
			case ButtonRelease:
				return die(dpy, 0);
				break;
		}
	}
}