summaryrefslogtreecommitdiff
path: root/xsnap.c
blob: df5d55cb169578c5a497aa8bbc054db0a8980e29 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <X11/Xlib.h>

/* MAJE_LDLIB=-lX11 */

#define WM_STATE_FORMAT	32

static Bool is_normal(Display *dpy, Window win)
{
	static Atom state = None;
	if (state == None) {
		state = XInternAtom(dpy, "WM_STATE", True);
		if (state == None) {
			return False;
		}
	}

	Atom type = AnyPropertyType;
	int format = WM_STATE_FORMAT;
	unsigned long nitems = 0;
	unsigned long bytes = 0;
	unsigned char *prop = NULL;

	int r = XGetWindowProperty(dpy,
		win,
		state,
		0,
		0,
		False,
		type,
		&type,
		&format,
		&nitems,
		&bytes,
		&prop);

	if (r == Success && prop != NULL) {
		XFree(prop);
		return True;
	}

	return False;
}

static Window find_normal_parent(Display *dpy, Window win)
{
	if (win == PointerRoot || win == None || is_normal(dpy, win)) {
		return win;
	}

	Window root = win, parent = win, *children = NULL;
	unsigned int nchildren = 0;
	if (!XQueryTree(dpy, win, &root, &parent, &children, &nchildren)) {
		return None;
	}

	if (parent == win || is_normal(dpy, parent)) {
		return parent;
	}

	return find_normal_parent(dpy, win);
}

static int xsnap_error(Display *dpy, XErrorEvent *e)
{
	char buf[BUFSIZ];
	XGetErrorText(dpy, e->error_code, buf, sizeof(buf));
	fprintf(stderr, "xsnap: %s\n", buf);
	exit(1);
}

int main(int argc, char *argv[])
{
	enum { NONE, LEFT, RIGHT, UP, DOWN } dir = NONE;

	int c = 0;
	while ((c = getopt(argc, argv, "lrud")) != -1) {
		switch (c) {
		case 'l':
			dir = LEFT;
			break;

		case 'r':
			dir = RIGHT;
			break;

		case 'u':
			dir = UP;
			break;

		case 'd':
			dir = DOWN;
			break;

		default:
			return 1;
		}
	}

	if (dir == NONE) {
		fprintf(stderr, "xsnap: missing direction\n");
		return 1;
	}

	/* TODO: if operand, use that, otherwise find focused */

	Display *dpy = XOpenDisplay(NULL);
	XSetErrorHandler(xsnap_error);

	Window root = XDefaultRootWindow(dpy);

	Window focused = root;
	int revert = RevertToNone;
	XGetInputFocus(dpy, &focused, &revert);
	focused = find_normal_parent(dpy, focused);

	XWindowAttributes rootattr = { 0 };
	XGetWindowAttributes(dpy, root, &rootattr);

	XWindowAttributes attributes = { 0 };
	XGetWindowAttributes(dpy, focused, &attributes);

	if (!attributes.override_redirect) {
		fprintf(stderr, "can't override redirect\n");
		exit(1);
	}

	int x = 0;
	int y = 0;
	unsigned w = 0;
	unsigned h = 0;

	switch (dir) {
	case LEFT:
		x = 0;
		y = 0;
		w = rootattr.width / 2;
		h = rootattr.height;
		break;

	case RIGHT:
		x = rootattr.width / 2;
		y = 0;
		w = rootattr.width / 2;
		h = rootattr.height;
		break;

	case UP:
		x = 0;
		y = 0;
		w = rootattr.width;
		h = rootattr.height / 2;
		break;

	case DOWN:
		x = 0;
		y = rootattr.height / 2;
		w = rootattr.width;
		h = rootattr.height / 2;
		break;

	default:
		fprintf(stderr, "somehow missing direction\n");
		return 1;
		break;
	}

	int r = XMoveResizeWindow(dpy, focused, x, y, w ? w : 1, h ? h : 1);
	if (r != 0) {
		XErrorEvent e = { .error_code = r };
		xsnap_error(dpy, &e);
	}

	return 0;
}