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
30 static int border = 2;
31 static int stack = 25;
32 static int no_capture = 0;
34 /* Internal structures */
37 int override; // normal vs override redirect
38 int managed; // window is currently managed by wm
42 static xcb_connection_t *conn;
43 static xcb_window_t root;
44 static list_t *screens;
51 const char *event_names[] = {
52 [XCB_KEY_PRESS ] "key_press", [XCB_KEY_RELEASE ] "key_release",
53 [XCB_BUTTON_PRESS ] "button_press", [XCB_BUTTON_RELEASE ] "button_release",
54 [XCB_MOTION_NOTIFY ] "motion_notify", [XCB_ENTER_NOTIFY ] "enter_notify",
55 [XCB_LEAVE_NOTIFY ] "leave_notify", [XCB_FOCUS_IN ] "focus_in",
56 [XCB_FOCUS_OUT ] "focus_out", [XCB_KEYMAP_NOTIFY ] "keymap_notify",
57 [XCB_EXPOSE ] "expose", [XCB_GRAPHICS_EXPOSURE] "graphics_exposure",
58 [XCB_NO_EXPOSURE ] "no_exposure", [XCB_VISIBILITY_NOTIFY] "visibility_notify",
59 [XCB_CREATE_NOTIFY ] "create_notify", [XCB_DESTROY_NOTIFY ] "destroy_notify",
60 [XCB_UNMAP_NOTIFY ] "unmap_notify", [XCB_MAP_NOTIFY ] "map_notify",
61 [XCB_MAP_REQUEST ] "map_request", [XCB_REPARENT_NOTIFY ] "reparent_notify",
62 [XCB_CONFIGURE_NOTIFY ] "configure_notify", [XCB_CONFIGURE_REQUEST] "configure_request",
63 [XCB_GRAVITY_NOTIFY ] "gravity_notify", [XCB_RESIZE_REQUEST ] "resize_request",
64 [XCB_CIRCULATE_NOTIFY ] "circulate_notify", [XCB_CIRCULATE_REQUEST] "circulate_request",
65 [XCB_PROPERTY_NOTIFY ] "property_notify", [XCB_SELECTION_CLEAR ] "selection_clear",
66 [XCB_SELECTION_REQUEST] "selection_request", [XCB_SELECTION_NOTIFY ] "selection_notify",
67 [XCB_COLORMAP_NOTIFY ] "colormap_notify", [XCB_CLIENT_MESSAGE ] "client_message",
68 [XCB_MAPPING_NOTIFY ] "mapping_notify", [XCB_GE_GENERIC ] "ge_generic",
75 static int win_cmp(const void *_a, const void *_b)
79 if (a->sys->xcb < b->sys->xcb) return -1;
80 if (a->sys->xcb > b->sys->xcb) return 1;
84 static win_t *win_get(xcb_window_t xcb)
86 win_sys_t sys = { .xcb = xcb };
87 win_t key = { .sys = &sys };
89 win_t **win = tfind(&key, &cache, win_cmp);
92 printf("Warning: no window for %u\n", xcb);
103 static xcb_query_tree_reply_t *do_query_tree(xcb_window_t win)
105 xcb_query_tree_cookie_t cookie =
106 xcb_query_tree(conn, win);
107 xcb_query_tree_reply_t *reply =
108 xcb_query_tree_reply(conn, cookie, NULL);
110 error("do_query_tree: %d - no reply", win);
111 printf("do_query_tree: %d\n", win);
115 static xcb_get_geometry_reply_t *do_get_geometry(xcb_window_t win)
117 xcb_get_geometry_cookie_t cookie =
118 xcb_get_geometry(conn, win);
119 xcb_get_geometry_reply_t *reply =
120 xcb_get_geometry_reply(conn, cookie, NULL);
122 error("do_get_geometry: %d - no reply", win);
123 printf("do_get_geometry: %d - %dx%d @ %d,%d\n",
124 win, reply->width, reply->height, reply->x, reply->y);
128 static xcb_get_window_attributes_reply_t *do_get_window_attributes(xcb_window_t win)
130 xcb_get_window_attributes_cookie_t cookie =
131 xcb_get_window_attributes(conn, win);
132 xcb_get_window_attributes_reply_t *reply =
133 xcb_get_window_attributes_reply(conn, cookie, NULL);
135 error("do_get_window_attributes: %d - no reply ", win);
136 printf("do_get_window_attributes: %d - %d\n",
137 win, reply->override_redirect);
141 /**********************
142 * X11 Event Handlers *
143 **********************/
145 /* Specific events */
146 static void on_create_notify(xcb_create_notify_event_t *event)
148 win_t *win = new0(win_t);
149 win_sys_t *sys = new0(win_sys_t);
151 printf("on_create_notify: xcb=%u -> win=%p\n",
156 win->w = event->width;
157 win->h = event->height;
160 sys->xcb = event->window;
161 sys->override = event->override_redirect;
163 tsearch(win, &cache, win_cmp);
166 static void on_destroy_notify(win_t *win, xcb_destroy_notify_event_t *event)
168 printf("on_destroy_notify: xcb=%u -> win=%p\n",
171 tdelete(win, &cache, win_cmp);
177 static void on_map_request(win_t *win, xcb_map_request_event_t *event)
179 printf("on_map_request: xcb=%u -> win=%p\n",
182 if (!win->sys->managed) {
184 win->sys->managed = 1;
187 xcb_map_window(conn, win->sys->xcb);
188 sys_move(win, win->x, win->y, win->w, win->h);
191 static void on_configure_request(win_t *win, xcb_configure_request_event_t *event)
193 printf("on_configure_request: xcb=%u -> win=%p -- %dx%d @ %d,%d\n",
195 event->width, event->height,
200 win->w = event->width;
201 win->h = event->height;
203 xcb_configure_notify_event_t resp = {
204 .response_type = XCB_CONFIGURE_NOTIFY,
205 .event = win->sys->xcb,
206 .window = win->sys->xcb,
211 .border_width = border,
214 xcb_send_event(conn, 0, win->sys->xcb,
215 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
216 (const char *)&resp);
220 static void on_event(xcb_generic_event_t *event)
223 switch (event->response_type) {
224 case XCB_CREATE_NOTIFY:
225 on_create_notify((xcb_create_notify_event_t *)event);
227 case XCB_DESTROY_NOTIFY:
228 if ((win = win_get(((xcb_destroy_notify_event_t *)event)->window)))
229 on_destroy_notify(win, (xcb_destroy_notify_event_t *)event);
231 case XCB_MAP_REQUEST:
232 if ((win = win_get(((xcb_map_request_event_t *)event)->window)))
233 on_map_request(win, (xcb_map_request_event_t *)event);
235 case XCB_CONFIGURE_REQUEST:
236 if ((win = win_get(((xcb_configure_request_event_t *)event)->window)))
237 on_configure_request(win, (xcb_configure_request_event_t *)event);
240 printf("on_%s\n", event_names[event->response_type] ?: "unknown_event");
245 /********************
247 ********************/
249 void sys_move(win_t *win, int x, int y, int w, int h)
251 printf("sys_move: %p - %dx%d @ %d,%d\n",
259 uint16_t mask = XCB_CONFIG_WINDOW_X
260 | XCB_CONFIG_WINDOW_Y
261 | XCB_CONFIG_WINDOW_WIDTH
262 | XCB_CONFIG_WINDOW_HEIGHT;
263 uint32_t list[] = {x, y, w, h};
265 xcb_configure_window(conn, win->sys->xcb, mask, list);
268 void sys_raise(win_t *win)
270 printf("sys_raise: %p\n", win);
273 void sys_focus(win_t *win)
275 printf("sys_focus: %p\n", win);
278 void sys_show(win_t *win, state_t state)
280 printf("sys_show: %p - %d\n", win, state);
283 void sys_watch(win_t *win, event_t ev, mod_t mod)
285 printf("sys_watch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
288 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
290 printf("sys_unwatch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
293 list_t *sys_info(void)
295 printf("sys_info\n");
297 if (screens == NULL) {
298 /* No xinerama support */
299 const xcb_setup_t *setup = xcb_get_setup(conn);
300 xcb_screen_t *geom = xcb_setup_roots_iterator(setup).data;
302 win_t *screen = new0(win_t);
304 screen->w = geom->width_in_pixels;
305 screen->h = geom->height_in_pixels;
307 screens = list_insert(NULL, screen);
309 printf("sys_info: root screen - %dx%d\n",
310 screen->w, screen->h);
318 printf("sys_init\n");
320 /* Load configuration */
321 stack = conf_get_int("main.stack", stack);
322 border = conf_get_int("main.border", border);
323 no_capture = conf_get_int("main.no-capture", no_capture);
325 /* Connect to display */
326 if (!(conn = xcb_connect(NULL, NULL)))
327 error("xcb connect failed");
328 if (xcb_connection_has_error(conn))
329 error("xcb connection has errors");
331 /* Get root window */
332 const xcb_setup_t *setup = xcb_get_setup(conn);
333 xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
334 root = iter.data->root;
336 /* Request substructure redirect */
337 xcb_event_mask_t mask;
338 xcb_void_cookie_t cookie;
339 xcb_generic_error_t *err;
340 mask = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
341 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
342 cookie = xcb_change_window_attributes_checked(conn, root,
343 XCB_CW_EVENT_MASK, &mask);
344 if ((err = xcb_request_check(conn, cookie)))
345 error("Another window manager is already running");
352 /* Add each initial window */
354 xcb_query_tree_reply_t *tree;
358 tree = do_query_tree(root);
359 nkids = xcb_query_tree_children_length(tree);
360 kids = xcb_query_tree_children(tree);
362 for(int i = 0; i < nkids; i++) {
363 xcb_get_geometry_reply_t *geom;
364 xcb_get_window_attributes_reply_t *attr;
366 geom = do_get_geometry(kids[i]);
367 attr = do_get_window_attributes(kids[i]);
369 win_t *win = new0(win_t);
370 win_sys_t *sys = new0(win_sys_t);
374 win->w = geom->width;
375 win->h = geom->height;
379 sys->override = attr->override_redirect;
381 tsearch(win, &cache, win_cmp);
383 if (!attr->override_redirect) {
396 xcb_generic_event_t *event;
397 if (!(event = xcb_wait_for_event(conn)))
401 if (!(status = xcb_flush(conn)))
408 printf("sys_exit\n");
410 xcb_disconnect(conn);
416 printf("sys_free\n");
418 xcb_disconnect(conn);