-
Notifications
You must be signed in to change notification settings - Fork 24
/
xmmv.c
150 lines (114 loc) · 2.9 KB
/
xmmv.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* 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))
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
mmove(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_x, orig_y;
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/2, geom->height/2);
if (!geom)
errx(1, "could not get window size");
orig_x = geom->x;
orig_y = geom->y;
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->x != orig_x || geom->y != orig_y)
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] = ptr->root_x + geom->width/2;
values[1] = ptr->root_y + geom->height/2;
values[0] = (ptr->root_x + geom->width / 2
> scr->width_in_pixels
- (geom->border_width*2))
? scr->width_in_pixels - geom->width
- (geom->border_width*2)
: ptr->root_x - geom->width / 2;
values[1] = (ptr->root_y + geom->height / 2
> scr->height_in_pixels
- (geom->border_width*2))
? (scr->height_in_pixels - geom->height
- (geom->border_width*2))
: ptr->root_y - geom->height / 2;
if (ptr->root_x < geom->width/2)
values[0] = 0;
if (ptr->root_y < geom->height/2)
values[1] = 0;
xcb_configure_window(conn, win, XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y, 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();
mmove(win);
return 0;
}