forked from luizdemilon/Xlambda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXlambda.c
More file actions
434 lines (358 loc) · 10 KB
/
Xlambda.c
File metadata and controls
434 lines (358 loc) · 10 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* see LICENSES.md for details on the licensing of this software */
#include <stdlib.h>
#include <stdio.h>
#include <libguile.h>
#include <xcb/xcb.h>
/*
* Variables used to hold the connection to the X server,
* and the first screen of this connection.
*/
xcb_connection_t *conn;
xcb_screen_t *scrn;
/* Function prototypes */
static SCM wm_list_windows();
static SCM wm_exists_p(SCM wid);
static SCM wm_get_focused_window();
static SCM wm_set_focused_window(SCM s_wid);
static SCM wm_set_border(SCM s_wid, SCM s_width, SCM s_color);
static SCM wm_get_cursor_pos();
static SCM wm_set_cursor_pos(SCM s_x, SCM s_y);
static SCM wm_teleport(SCM wid, SCM x, SCM y, SCM width, SCM s_height);
static SCM wm_window_map(SCM s_wid, SCM s_mode);
static SCM wm_window_unmap(SCM s_wid, SCM s_mode);
static SCM wm_window_x(SCM s_wid);
static SCM wm_window_y(SCM s_wid);
static SCM wm_window_w(SCM s_wid);
static SCM wm_window_h(SCM s_wid);
static SCM wm_window_b(SCM s_wid);
static SCM wm_window_m(SCM s_wid);
static SCM wm_window_i(SCM s_wid);
static void*
register_functions (void* data)
{
/* scheme-name
* args: required, optional, rest-list,
* c_name */
scm_c_define_gsubr("wm/window/x", 1, 0, 0, &wm_window_x);
scm_c_define_gsubr("wm/window/y", 1, 0, 0, &wm_window_y);
scm_c_define_gsubr("wm/window/w", 1, 0, 0, &wm_window_w);
scm_c_define_gsubr("wm/window/h", 1, 0, 0, &wm_window_h);
scm_c_define_gsubr("wm/window/b", 1, 0, 0, &wm_window_b);
scm_c_define_gsubr("wm/window/m", 1, 0, 0, &wm_window_m);
scm_c_define_gsubr("wm/window/i", 1, 0, 0, &wm_window_i);
scm_c_define_gsubr("wm/window/map", 1, 0, 0, &wm_set_focused_window);
scm_c_define_gsubr("wm/window/unmap", 1, 0, 0, &wm_set_focused_window);
scm_c_define_gsubr("wm/get-focused", 0, 0, 0, &wm_get_focused_window);
scm_c_define_gsubr("wm/set-focused", 1, 0, 0, &wm_set_focused_window);
scm_c_define_gsubr("wm/get-cursor-pos", 0, 0, 0, &wm_get_cursor_pos);
scm_c_define_gsubr("wm/set-cursor-pos!", 2, 0, 0, &wm_set_cursor_pos);
scm_c_define_gsubr("wm/list-windows", 0, 0, 0, &wm_list_windows);
scm_c_define_gsubr("wm/exists?", 1, 0, 0, &wm_exists_p);
scm_c_define_gsubr("wm/teleport!", 5, 0, 0, &wm_teleport);
return NULL;
}
int
main(int argc, char** argv)
{
/* Connect to X */
conn = xcb_connect(NULL, NULL);
if (xcb_connection_has_error(conn))
return 0;
/* Get the first screen */
scrn = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
if (scrn == NULL)
return 0;
/* Start Guile */
scm_with_guile(®ister_functions, NULL);
scm_shell(argc, argv);
/* When it's done, quit cleanly */
if (conn) {
xcb_disconnect(conn);
return 0;
}
}
/*
* Returns the list of all existing windows
* The windows are listed in stacking order, from lower to upper window.
*/
static SCM
wm_list_windows()
{
xcb_window_t x_window;
/* Initialize the window list as a Guile empty list */
SCM s_window_list = SCM_EOL;
xcb_query_tree_cookie_t c;
xcb_query_tree_reply_t *r;
c = xcb_query_tree(conn, scrn->root);
r = xcb_query_tree_reply(conn, c, NULL);
if (r == NULL)
return SCM_BOOL_F;
xcb_window_t *x_window_list = xcb_query_tree_children(r);
/* wi = window index */
for (int wi = 0; wi <= r->children_len; wi++) {
x_window = x_window_list[wi];
SCM s_window = scm_from_uint32(x_window);
/* Put the window ID into a list */
SCM s_wid = scm_list_1(s_window);
/* Append the list with the wid to the total list */
s_window_list = scm_append(scm_list_2 (s_window_list, s_wid));
}
free(r);
return s_window_list;
}
/* Check existence of a window. */
static SCM
wm_exists_p(SCM wid)
{
xcb_window_t w = scm_to_uint32(wid);
xcb_get_window_attributes_cookie_t c;
xcb_get_window_attributes_reply_t *r;
c = xcb_get_window_attributes(conn, w);
r = xcb_get_window_attributes_reply(conn, c, NULL);
if (r == NULL)
return SCM_BOOL_F;
free(r);
return SCM_BOOL_T;
}
/* Teleport a window to the given position. */
static SCM
wm_teleport(SCM wid, SCM x, SCM y, SCM width, SCM height)
{
xcb_window_t w = scm_to_uint32(wid);
uint32_t values[4];
uint32_t mask = XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
| XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT;
values[0] = scm_to_uint32(x);
values[1] = scm_to_uint32(y);
values[2] = scm_to_uint32(width);
values[3] = scm_to_uint32(height);
xcb_configure_window(conn, w, mask, values);
xcb_flush(conn);
return SCM_BOOL_T;
/* todo: return old geometry, maybe on version 0.2, so things like
fullscreening/returning to old position are easier */
}
/* Get focused window identifier */
static SCM
wm_get_focused_window()
{
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)
return SCM_BOOL_F;
SCM fwid = scm_from_uint32(r->focus);
free(r);
return fwid;
}
/* Return window's x offset position */
static SCM
wm_window_x(SCM s_wid)
{
xcb_window_t wid = scm_to_uint32(s_wid);
xcb_get_geometry_cookie_t c;
xcb_get_geometry_reply_t *r;
c = xcb_get_geometry(conn, wid);
r = xcb_get_geometry_reply(conn, c, NULL);
if (r == NULL)
return SCM_BOOL_F;
SCM x = scm_from_int(r->x);
free(r);
return x;
}
/* Return window's y offset position */
static SCM
wm_window_y(SCM s_wid)
{
xcb_window_t wid = scm_to_uint32(s_wid);
xcb_get_geometry_cookie_t c;
xcb_get_geometry_reply_t *r;
c = xcb_get_geometry(conn, wid);
r = xcb_get_geometry_reply(conn, c, NULL);
if (r == NULL)
return SCM_BOOL_F;
SCM y = scm_from_int(r->y);
free(r);
return y;
}
/* Return window's width */
static SCM
wm_window_w(SCM s_wid)
{
xcb_window_t wid = scm_to_uint32(s_wid);
xcb_get_geometry_cookie_t c;
xcb_get_geometry_reply_t *r;
c = xcb_get_geometry(conn, wid);
r = xcb_get_geometry_reply(conn, c, NULL);
if (r == NULL)
return SCM_BOOL_F;
SCM w = scm_from_int(r->width);
free(r);
return w;
}
/* Return window's height */
static SCM
wm_window_h(SCM s_wid)
{
xcb_window_t wid = scm_to_uint32(s_wid);
xcb_get_geometry_cookie_t c;
xcb_get_geometry_reply_t *r;
c = xcb_get_geometry(conn, wid);
r = xcb_get_geometry_reply(conn, c, NULL);
if (r == NULL)
return SCM_BOOL_F;
SCM h = scm_from_int(r->height);
free(r);
return h;
}
/* Return window's border width */
static SCM
wm_window_b(SCM s_wid)
{
xcb_window_t wid = scm_to_uint32(s_wid);
xcb_get_geometry_cookie_t c;
xcb_get_geometry_reply_t *r;
c = xcb_get_geometry(conn, wid);
r = xcb_get_geometry_reply(conn, c, NULL);
if (r == NULL)
return SCM_BOOL_F;
SCM b = scm_from_int(r->border_width);
free(r);
return b;
}
/* Return window's mapped status */
static SCM
wm_window_m(SCM s_wid)
{
xcb_window_t wid = scm_to_uint32(s_wid);
xcb_get_window_attributes_cookie_t c;
xcb_get_window_attributes_reply_t *r;
c = xcb_get_window_attributes(conn, wid);
r = xcb_get_window_attributes_reply(conn, c, NULL);
if (r == NULL)
return SCM_BOOL_F;
SCM m = scm_from_int(r->map_state);
free(r);
return m;
}
/* Return whether a window should be ignored or not */
static SCM
wm_window_i(SCM s_wid)
{
xcb_window_t wid = scm_to_uint32(s_wid);
xcb_get_window_attributes_cookie_t c;
xcb_get_window_attributes_reply_t *r;
c = xcb_get_window_attributes(conn, wid);
r = xcb_get_window_attributes_reply(conn, c, NULL);
if (r == NULL)
return SCM_BOOL_F; /* this is probably the root window
* in any case, it shouldn't be handled. */
/* override_redirect is 1 when true, and 2 when false */
SCM i;
switch (r->override_redirect) {
case 0:
i = SCM_BOOL_F;
case 1:
i = SCM_BOOL_T;
}
free(r);
return i;
}
/* Get the cursor position, and return its coordinates as (x y) */
static SCM
wm_get_cursor_pos()
{
xcb_query_pointer_reply_t *r;
xcb_query_pointer_cookie_t c;
c = xcb_query_pointer(conn, scrn->root);
r = xcb_query_pointer_reply(conn, c, NULL);
if (r == NULL)
return SCM_BOOL_F;
SCM xypos;
if (r->child != XCB_NONE) {
xypos = scm_list_2(scm_from_uint16(r->win_x), scm_from_uint16(r->win_y));
} else {
xypos = scm_list_2(scm_from_uint16(r->root_x), scm_from_uint16(r->root_y));
}
free(r);
return xypos;
}
/* Set a window's border.
* The color should be a hexadecimal number, eg: "#xffffff" */
static SCM
wm_set_border(SCM s_wid, SCM s_width, SCM s_color)
{
xcb_window_t wid = scm_to_uint32(s_wid);
int width = scm_to_int32(s_wid);
int color = scm_to_int32(s_color);
uint32_t values[1];
int mask, retval = 0;
/* change width if > 0 */
if (width > -1) {
values[0] = width;
mask = XCB_CONFIG_WINDOW_BORDER_WIDTH;
xcb_configure_window(conn, wid, mask, values);
retval++;
}
/* change color if > 0 */
if (color > -1) {
values[0] = color;
mask = XCB_CW_BORDER_PIXEL;
xcb_change_window_attributes(conn, wid, mask, values);
retval++;
}
xcb_flush(conn);
return scm_from_int(retval);
}
/* Change the cursor position. */
static SCM
wm_set_cursor_pos(SCM s_x, SCM s_y)
{
int x = scm_to_int(s_x);
int y = scm_to_int(s_y);
xcb_warp_pointer(conn, XCB_NONE, scrn->root, 0, 0, 0, 0, x, y);
return SCM_BOOL_T;
}
static SCM
wm_set_override(SCM s_wid, SCM s_mode)
{
xcb_window_t wid = scm_to_uint32(s_wid);
int mode = scm_to_int(s_mode);
uint32_t mask = XCB_CW_OVERRIDE_REDIRECT;
uint32_t val[] = { mode };
xcb_change_window_attributes(conn, wid, mask, val);
return SCM_BOOL_T;
}
/* Map a window */
static SCM
wm_window_map(SCM s_wid, SCM s_mode)
{
xcb_window_t wid = scm_to_uint32(s_wid);
xcb_map_window(conn, wid);
xcb_flush(conn);
return scm_list_2(s_wid, SCM_BOOL_T);
}
/* Unmap a window */
static SCM
wm_window_unmap(SCM s_wid, SCM s_mode)
{
xcb_window_t wid = scm_to_uint32(s_wid);
xcb_unmap_window(conn, wid);
xcb_flush(conn);
return scm_list_2(s_wid, SCM_BOOL_T);
}
/* Give the input focus to the specified window */
static SCM
wm_set_focused_window(SCM s_wid)
{
xcb_window_t wid = scm_to_uint32(s_wid);
xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, wid,
XCB_CURRENT_TIME);
/* Bring window to the front */
xcb_configure_window(conn, wid, XCB_STACK_MODE_ABOVE, 0);
xcb_flush(conn);
return SCM_BOOL_T;
}