2 * Copyright (c) 2015 Andy Spencer <andy753421@gmail.com>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22 #include <xcb/xinerama.h>
31 static int border = 2;
32 static int stack = 25;
33 static int no_capture = 0;
35 /* Internal structures */
38 int override; // normal vs override redirect
39 int managed; // window is currently managed by wm
43 static xcb_connection_t *conn;
44 static xcb_window_t root;
45 static list_t *screens;
52 const char *event_names[] = {
53 [XCB_KEY_PRESS ] "key_press", [XCB_KEY_RELEASE ] "key_release",
54 [XCB_BUTTON_PRESS ] "button_press", [XCB_BUTTON_RELEASE ] "button_release",
55 [XCB_MOTION_NOTIFY ] "motion_notify", [XCB_ENTER_NOTIFY ] "enter_notify",
56 [XCB_LEAVE_NOTIFY ] "leave_notify", [XCB_FOCUS_IN ] "focus_in",
57 [XCB_FOCUS_OUT ] "focus_out", [XCB_KEYMAP_NOTIFY ] "keymap_notify",
58 [XCB_EXPOSE ] "expose", [XCB_GRAPHICS_EXPOSURE] "graphics_exposure",
59 [XCB_NO_EXPOSURE ] "no_exposure", [XCB_VISIBILITY_NOTIFY] "visibility_notify",
60 [XCB_CREATE_NOTIFY ] "create_notify", [XCB_DESTROY_NOTIFY ] "destroy_notify",
61 [XCB_UNMAP_NOTIFY ] "unmap_notify", [XCB_MAP_NOTIFY ] "map_notify",
62 [XCB_MAP_REQUEST ] "map_request", [XCB_REPARENT_NOTIFY ] "reparent_notify",
63 [XCB_CONFIGURE_NOTIFY ] "configure_notify", [XCB_CONFIGURE_REQUEST] "configure_request",
64 [XCB_GRAVITY_NOTIFY ] "gravity_notify", [XCB_RESIZE_REQUEST ] "resize_request",
65 [XCB_CIRCULATE_NOTIFY ] "circulate_notify", [XCB_CIRCULATE_REQUEST] "circulate_request",
66 [XCB_PROPERTY_NOTIFY ] "property_notify", [XCB_SELECTION_CLEAR ] "selection_clear",
67 [XCB_SELECTION_REQUEST] "selection_request", [XCB_SELECTION_NOTIFY ] "selection_notify",
68 [XCB_COLORMAP_NOTIFY ] "colormap_notify", [XCB_CLIENT_MESSAGE ] "client_message",
69 [XCB_MAPPING_NOTIFY ] "mapping_notify", [XCB_GE_GENERIC ] "ge_generic",
76 static int win_cmp(const void *_a, const void *_b)
80 if (a->sys->xcb < b->sys->xcb) return -1;
81 if (a->sys->xcb > b->sys->xcb) return 1;
85 static win_t *win_get(xcb_window_t xcb)
87 win_sys_t sys = { .xcb = xcb };
88 win_t key = { .sys = &sys };
90 win_t **win = tfind(&key, &cache, win_cmp);
93 printf("Warning: no window for %u\n", xcb);
104 static xcb_query_tree_reply_t *do_query_tree(xcb_window_t win)
106 xcb_query_tree_cookie_t cookie =
107 xcb_query_tree(conn, win);
108 xcb_query_tree_reply_t *reply =
109 xcb_query_tree_reply(conn, cookie, NULL);
111 error("do_query_tree: %d - no reply", win);
112 printf("do_query_tree: %d\n", win);
116 static xcb_get_geometry_reply_t *do_get_geometry(xcb_window_t win)
118 xcb_get_geometry_cookie_t cookie =
119 xcb_get_geometry(conn, win);
120 xcb_get_geometry_reply_t *reply =
121 xcb_get_geometry_reply(conn, cookie, NULL);
123 error("do_get_geometry: %d - no reply", win);
124 printf("do_get_geometry: %d - %dx%d @ %d,%d\n",
125 win, reply->width, reply->height, reply->x, reply->y);
129 static xcb_get_window_attributes_reply_t *do_get_window_attributes(xcb_window_t win)
131 xcb_get_window_attributes_cookie_t cookie =
132 xcb_get_window_attributes(conn, win);
133 xcb_get_window_attributes_reply_t *reply =
134 xcb_get_window_attributes_reply(conn, cookie, NULL);
136 error("do_get_window_attributes: %d - no reply ", win);
137 printf("do_get_window_attributes: %d - %d\n",
138 win, reply->override_redirect);
142 static int do_xinerama_check(void)
144 const xcb_query_extension_reply_t *data =
145 xcb_get_extension_data(conn, &xcb_xinerama_id);
146 if (!data || !data->present)
147 return printf("do_xinerama_check: no ext\n"), 0;
149 xcb_xinerama_is_active_cookie_t cookie =
150 xcb_xinerama_is_active(conn);
151 if (!cookie.sequence)
152 return printf("do_xinerama_check: no cookie\n"), 0;
154 xcb_xinerama_is_active_reply_t *reply =
155 xcb_xinerama_is_active_reply(conn, cookie, NULL);
157 printf("do_xinerama_check: no reply\n"), 0;
159 printf("do_xinerama_check: %d\n", reply->state);
160 return reply && reply->state;
163 static xcb_xinerama_query_screens_reply_t *do_query_screens(void)
165 xcb_xinerama_query_screens_cookie_t cookie =
166 xcb_xinerama_query_screens(conn);
167 xcb_xinerama_query_screens_reply_t *reply =
168 xcb_xinerama_query_screens_reply(conn, cookie, NULL);
170 printf("do_query_screens: no reply\n");
172 printf("do_query_screens: %d screens\n",
173 xcb_xinerama_query_screens_screen_info_length(reply));
177 /**********************
178 * X11 Event Handlers *
179 **********************/
181 /* Specific events */
182 static void on_create_notify(xcb_create_notify_event_t *event)
184 win_t *win = new0(win_t);
185 win_sys_t *sys = new0(win_sys_t);
187 printf("on_create_notify: xcb=%u -> win=%p\n",
192 win->w = event->width;
193 win->h = event->height;
196 sys->xcb = event->window;
197 sys->override = event->override_redirect;
199 tsearch(win, &cache, win_cmp);
202 static void on_destroy_notify(win_t *win, xcb_destroy_notify_event_t *event)
204 printf("on_destroy_notify: xcb=%u -> win=%p\n",
207 tdelete(win, &cache, win_cmp);
213 static void on_map_request(win_t *win, xcb_map_request_event_t *event)
215 printf("on_map_request: xcb=%u -> win=%p\n",
218 if (!win->sys->managed) {
220 win->sys->managed = 1;
223 xcb_map_window(conn, win->sys->xcb);
224 sys_move(win, win->x, win->y, win->w, win->h);
227 static void on_configure_request(win_t *win, xcb_configure_request_event_t *event)
229 printf("on_configure_request: xcb=%u -> win=%p -- %dx%d @ %d,%d\n",
231 event->width, event->height,
236 win->w = event->width;
237 win->h = event->height;
239 xcb_configure_notify_event_t resp = {
240 .response_type = XCB_CONFIGURE_NOTIFY,
241 .event = win->sys->xcb,
242 .window = win->sys->xcb,
247 .border_width = border,
250 xcb_send_event(conn, 0, win->sys->xcb,
251 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
252 (const char *)&resp);
256 static void on_event(xcb_generic_event_t *event)
259 switch (event->response_type) {
260 case XCB_CREATE_NOTIFY:
261 on_create_notify((xcb_create_notify_event_t *)event);
263 case XCB_DESTROY_NOTIFY:
264 if ((win = win_get(((xcb_destroy_notify_event_t *)event)->window)))
265 on_destroy_notify(win, (xcb_destroy_notify_event_t *)event);
267 case XCB_MAP_REQUEST:
268 if ((win = win_get(((xcb_map_request_event_t *)event)->window)))
269 on_map_request(win, (xcb_map_request_event_t *)event);
271 case XCB_CONFIGURE_REQUEST:
272 if ((win = win_get(((xcb_configure_request_event_t *)event)->window)))
273 on_configure_request(win, (xcb_configure_request_event_t *)event);
276 printf("on_%s\n", event_names[event->response_type] ?: "unknown_event");
281 /********************
283 ********************/
285 void sys_move(win_t *win, int x, int y, int w, int h)
287 printf("sys_move: %p - %dx%d @ %d,%d\n",
295 uint16_t mask = XCB_CONFIG_WINDOW_X
296 | XCB_CONFIG_WINDOW_Y
297 | XCB_CONFIG_WINDOW_WIDTH
298 | XCB_CONFIG_WINDOW_HEIGHT;
299 uint32_t list[] = {x, y, w, h};
301 xcb_configure_window(conn, win->sys->xcb, mask, list);
304 void sys_raise(win_t *win)
306 printf("sys_raise: %p\n", win);
309 void sys_focus(win_t *win)
311 printf("sys_focus: %p\n", win);
314 void sys_show(win_t *win, state_t state)
316 printf("sys_show: %p - %d\n", win, state);
319 void sys_watch(win_t *win, event_t ev, mod_t mod)
321 printf("sys_watch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
324 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
326 printf("sys_unwatch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
329 list_t *sys_info(void)
331 printf("sys_info\n");
333 if (screens == NULL && do_xinerama_check()) {
334 /* Add Xinerama screens */
335 xcb_xinerama_query_screens_reply_t *query;
337 xcb_xinerama_screen_info_t *info;
339 query = do_query_screens();
340 ninfo = xcb_xinerama_query_screens_screen_info_length(query);
341 info = xcb_xinerama_query_screens_screen_info(query);
343 for (int i = 0; i < ninfo; i++) {
344 win_t *screen = new0(win_t);
346 screen->x = info[i].x_org;
347 screen->y = info[i].y_org;
348 screen->w = info[i].width;
349 screen->h = info[i].height;
351 screens = list_insert(NULL, screen);
353 printf("sys_info: xinerama screen - %dx%d @ %d,%d\n",
354 screen->w, screen->h,
355 screen->x, screen->y);
359 if (screens == NULL) {
360 /* No xinerama support */
361 const xcb_setup_t *setup = xcb_get_setup(conn);
362 xcb_screen_t *geom = xcb_setup_roots_iterator(setup).data;
364 win_t *screen = new0(win_t);
366 screen->w = geom->width_in_pixels;
367 screen->h = geom->height_in_pixels;
369 screens = list_insert(NULL, screen);
371 printf("sys_info: root screen - %dx%d\n",
372 screen->w, screen->h);
380 printf("sys_init\n");
382 /* Load configuration */
383 stack = conf_get_int("main.stack", stack);
384 border = conf_get_int("main.border", border);
385 no_capture = conf_get_int("main.no-capture", no_capture);
387 /* Connect to display */
388 if (!(conn = xcb_connect(NULL, NULL)))
389 error("xcb connect failed");
390 if (xcb_connection_has_error(conn))
391 error("xcb connection has errors");
393 /* Get root window */
394 const xcb_setup_t *setup = xcb_get_setup(conn);
395 xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
396 root = iter.data->root;
398 /* Request substructure redirect */
399 xcb_event_mask_t mask;
400 xcb_void_cookie_t cookie;
401 xcb_generic_error_t *err;
402 mask = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
403 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
404 cookie = xcb_change_window_attributes_checked(conn, root,
405 XCB_CW_EVENT_MASK, &mask);
406 if ((err = xcb_request_check(conn, cookie)))
407 error("Another window manager is already running");
414 /* Add each initial window */
416 xcb_query_tree_reply_t *tree;
420 tree = do_query_tree(root);
421 nkids = xcb_query_tree_children_length(tree);
422 kids = xcb_query_tree_children(tree);
424 for(int i = 0; i < nkids; i++) {
425 xcb_get_geometry_reply_t *geom;
426 xcb_get_window_attributes_reply_t *attr;
428 geom = do_get_geometry(kids[i]);
429 attr = do_get_window_attributes(kids[i]);
431 win_t *win = new0(win_t);
432 win_sys_t *sys = new0(win_sys_t);
436 win->w = geom->width;
437 win->h = geom->height;
441 sys->override = attr->override_redirect;
443 tsearch(win, &cache, win_cmp);
445 if (!attr->override_redirect) {
458 xcb_generic_event_t *event;
459 if (!(event = xcb_wait_for_event(conn)))
463 if (!(status = xcb_flush(conn)))
470 printf("sys_exit\n");
472 xcb_disconnect(conn);
478 printf("sys_free\n");
480 xcb_disconnect(conn);