-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxutil.c
More file actions
327 lines (226 loc) · 6.09 KB
/
xutil.c
File metadata and controls
327 lines (226 loc) · 6.09 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "xutil.h"
#include <glib.h>
#include <xcb/xcb.h>
#include <xcb/xproto.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <X11/cursorfont.h>
struct _entry {
int wid;
int level;
};
typedef struct _entry * entry;
xcb_connection_t * display = (xcb_connection_t *) NULL;
int screen_number = 0;
xcb_setup_t *setup;
xcb_screen_iterator_t screen_iter;
int theRoot;
entry create ( int wid, int level ) {
entry e;
e = (entry) malloc(sizeof(struct _entry));
// e = malloc (sizeof (*entry));
e->wid = wid;
e->level = level;
return e;
}
void init_xcb ( ) {
display = xcb_connect ( NULL, &screen_number);
if (xcb_connection_has_error(display)) {
fprintf (stderr, "Unable to open display\n");
exit (1);
}
setup = xcb_get_setup ( display );
screen_iter = xcb_setup_roots_iterator(setup);
int i;
for (i = 0; i < screen_number; i++)
xcb_screen_next(&screen_iter);
theRoot = screen_iter.data->root;
}
void initialize ( ) {
init_xcb ( );
}
void pushChildren ( int wid, int level, GQueue * queue ) {
if (display == NULL)
init_xcb ( );
xcb_query_tree_cookie_t qtcookie = xcb_query_tree (display, wid );
xcb_query_tree_reply_t * reply = xcb_query_tree_reply(display, qtcookie, NULL);
xcb_window_t * children = xcb_query_tree_children(reply);
int i;
// printf ("Children of %d\n", wid );
for (i = 0; i < reply->children_len; i++) {
// printGeometry(children[i], dpy);
g_queue_push_tail ( queue, create ( children[i], level));
}
// printf ("\n");
}
int getAncestorBelowRoot ( int wid ) {
if (display == (xcb_connection_t *) 0)
init_xcb();
if (wid == theRoot) // wid needs to be a window below root
return -1;
else {
xcb_query_tree_cookie_t qtcookie;
xcb_query_tree_reply_t *reply;
xcb_window_t *children;
int i;
qtcookie = xcb_query_tree ( display, wid );
reply = xcb_query_tree_reply ( display, qtcookie, NULL);
if (!reply) {
printf ("Invalid window id : %d\n", wid );
return -1;
}
if (reply->parent == theRoot)
return wid;
else
return getAncestorBelowRoot ( reply->parent);
}
}
void printWindowInfo (int wid ) {
xcb_get_geometry_cookie_t geomCookie =
xcb_get_geometry (display, wid);
xcb_get_geometry_reply_t *geom =
xcb_get_geometry_reply (display, geomCookie, NULL);
printf ("%d [(%d , %d) %d x %d] ", wid,
geom->x, geom->y, geom->width, geom->height);
}
void printTree ( int wid ) {
GQueue * queue = g_queue_new( );
int curlevel = 0;
entry temp;
g_queue_push_tail ( queue, create(wid,1));
while (g_queue_is_empty(queue) == FALSE) {
temp = g_queue_pop_head ( queue );
if (temp->level > curlevel) {
curlevel = temp->level;
printf ("\n");
printf ("Level %d --> ", temp->level);
}
printWindowInfo (temp->wid);
pushChildren ( temp->wid, temp->level + 1, queue );
}
printf ("\n");
}
int getRoot ( ) {
if (display == NULL)
init_xcb ( );
return theRoot;
}
void getTopLevel(int ** toplevel, int * numTopLevel)
{
if (display == NULL)
init_xcb ( );
int i;
xcb_window_t *child;
xcb_query_tree_reply_t *reply;
xcb_query_tree_cookie_t qtcookie;
xcb_connection_t * dpy;
xcb_setup_t * setup;
char *displayname = NULL;
dpy = display;
qtcookie = xcb_query_tree (dpy, theRoot );
reply = xcb_query_tree_reply(dpy, qtcookie, NULL);
// if (!reply)
// goto done;
child = xcb_query_tree_children(reply);
*numTopLevel = reply->children_len;
// printf ("Number of toplevel windows: %d\n", *numTopLevel);
*toplevel = malloc ((*numTopLevel )* sizeof(int));
for (i = 0; i < reply->children_len; i++) {
xcb_query_tree_cookie_t qtcookie =
xcb_query_tree ( dpy, child[i]);
xcb_query_tree_reply_t * qtreply =
xcb_query_tree_reply ( dpy, qtcookie, NULL);
if (!qtreply)
{
printf ("GB: Error, unable to query tree for a child %d\n",
child[i]);
exit ( 0 );
}
(*toplevel)[i] = child[i];
// topgeom[i] = geom;
}
// return toplevel;
}
void getMouseLocation ( int * windowId,
int * x,
int * y)
{
if (display == NULL)
init_xcb ( );
xcb_query_pointer_cookie_t qpcookie;
xcb_query_pointer_reply_t * qpreply;
qpcookie = xcb_query_pointer ( display, theRoot );
qpreply = xcb_query_pointer_reply ( display, qpcookie, NULL);
*windowId = qpreply->child;
*x = qpreply->win_x;
*y = qpreply->win_y;
// printf ("%d : (%d, %d)\n", *windowId, *x, *y );
// printf ("%d, %d\n", qpreply->root_x, qpreply->root_y);
}
void getLocGeom ( int windowId,
int *x,
int *y,
int *width,
int *height )
{
if (display == NULL)
init_xcb();
xcb_get_geometry_cookie_t geomCookie =
xcb_get_geometry (display, windowId);
xcb_get_geometry_reply_t *geom =
xcb_get_geometry_reply (display, geomCookie, NULL);
// printf ("%d->%d, nc= %d : %dx%d\n", child[i], qtreply->parent,
// qtreply->children_len, geom->width, geom->height);
*x = geom->x;
*y = geom->y;
*width = geom->width;
*height = geom->height;
free (geom);
}
xcb_cursor_t
Create_Font_Cursor (xcb_connection_t *dpy, uint16_t glyph)
{
static xcb_font_t cursor_font;
xcb_cursor_t cursor;
if (!cursor_font) {
cursor_font = xcb_generate_id (dpy);
xcb_open_font (dpy, cursor_font, strlen ("cursor"), "cursor");
}
cursor = xcb_generate_id (dpy);
xcb_create_glyph_cursor (dpy, cursor, cursor_font, cursor_font,
glyph, glyph + 1,
0, 0, 0, 0xffff, 0xffff, 0xffff); /* rgb, rgb */
return cursor;
}
void Fatal_Error ( char * message ) {
printf ("%s\n", message);
exit ( 0 );
}
void grabMouse ( ) {
if (display == NULL)
init_xcb ( );
xcb_cursor_t cursor;
cursor = Create_Font_Cursor (display, XC_crosshair);
xcb_grab_pointer_cookie_t gpcookie;
gpcookie = xcb_grab_pointer(
display,
0,
theRoot,
XCB_EVENT_MASK_BUTTON_PRESS |
XCB_EVENT_MASK_BUTTON_RELEASE,
XCB_GRAB_MODE_ASYNC,
XCB_GRAB_MODE_ASYNC,
theRoot,
cursor,
XCB_TIME_CURRENT_TIME);
xcb_grab_pointer_reply_t * gpreply;
xcb_generic_error_t *err;
gpreply = xcb_grab_pointer_reply (display, gpcookie, &err);
if (gpreply->status != XCB_GRAB_STATUS_SUCCESS)
Fatal_Error ("Can't grab the mouse.");
}
/*
void moveMouse (int x, int y) {
}
*/