-
Notifications
You must be signed in to change notification settings - Fork 24
/
xmrs.c
135 lines (100 loc) · 2.57 KB
/
xmrs.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
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
/* See LICENSE file for copyright and license details. */
#include <xcb/xcb.h>
#include <stdlib.h>
#include <stdio.h>
#include <err.h>
#include "util.h"
#define CLEANMASK(m) ((m & ~0x80))
#define MAX(a, b) (a > b ? a : b)
static xcb_connection_t *conn;
static xcb_screen_t *scr;
static void usage(char *);
static xcb_window_t get_focuswin(void);
static void cleanup(void);
static void
cleanup(void)
{
kill_xcb(&conn);
}
static void
usage(char *name)
{
fprintf(stderr, "usage: %s <wid>\n", name);
exit(1);
}
static xcb_window_t
get_focuswin(void) {
xcb_window_t w = 0;
xcb_get_input_focus_cookie_t c;
xcb_get_input_focus_reply_t *r;
c = xcb_get_input_focus(conn);
r = xcb_get_input_focus_reply(conn, c, NULL);
if (r == NULL)
errx(1, "failed to get focused window");
w = r->focus;
free(r);
return w;
}
static void
mresize(xcb_window_t win) {
uint32_t values[3];
xcb_get_geometry_reply_t *geom;
xcb_query_pointer_reply_t *ptr;
xcb_generic_event_t *ev = NULL;
int orig_width, orig_height;
geom = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, win), NULL);
xcb_warp_pointer(conn, XCB_NONE, win, 0, 0, 0, 0,
geom->width, geom->height);
if (!geom)
errx(1, "could not get window size");
orig_width = geom->width;
orig_height = geom->height;
if (!scr->root)
return;
xcb_grab_pointer(conn, 0, scr->root,
XCB_EVENT_MASK_BUTTON_RELEASE
| XCB_EVENT_MASK_BUTTON_PRESS,
XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
scr->root, XCB_NONE, XCB_CURRENT_TIME);
xcb_flush(conn);
for (;;) {
ev = xcb_poll_for_event(conn);
if (geom->width != orig_width || geom->height != orig_height)
if (ev != NULL) {
if (ev->response_type == XCB_MOTION_NOTIFY)
break;
exit(0);
}
geom = xcb_get_geometry_reply(conn,
xcb_get_geometry(conn, win), NULL);
if (!geom)
errx(1, "could not get window size");
ptr = xcb_query_pointer_reply(conn,
xcb_query_pointer(conn, scr->root), 0);
if (!ptr)
errx(1, "could not get pointer location");
values[0] = MAX(1, ptr->root_x - geom->x - 2 * geom->border_width);
values[1] = MAX(1, ptr->root_y - geom->y - 2 * geom->border_width);
if (values[0] >= 0 && values[1] >= 0)
xcb_configure_window(conn, win, XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT, values);
xcb_flush(conn);
}
xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
}
int
main(int argc, char **argv)
{
xcb_window_t win;
if (argc > 2)
usage(argv[0]);
init_xcb(&conn);
get_screen(conn, &scr);
atexit(cleanup);
if (argc == 2)
win = strtoul(argv[1], NULL, 16);
else
win = get_focuswin();
mresize(win);
return 0;
}