8 #include <sys/socket.h>
10 #include <sys/select.h>
12 #include <xcb/xcb_event.h>
13 #include <xcb/xcb_ewmh.h>
14 #include <xcb/randr.h>
27 int main(int argc, char *argv[])
30 char socket_path[MAXLEN];
31 char *fifo_path = NULL;
32 config_path[0] = '\0';
34 int sock_fd, ret_fd, dpy_fd, sel, n;
35 struct sockaddr_un sock_address;
37 char msg[BUFSIZ] = {0};
38 char rsp[BUFSIZ] = {0};
39 xcb_generic_event_t *event;
42 while ((opt = getopt(argc, argv, "hvc:s:p:")) != -1) {
45 printf(WM_NAME " [-h|-v|-c CONFIG_PATH|-s PANEL_FIFO|-p PANEL_PREFIX]\n");
49 printf("%s\n", VERSION);
53 strncpy(config_path, optarg, sizeof(config_path));
59 status_prefix = optarg;
64 if (config_path[0] == '\0') {
65 char *config_home = getenv(CONFIG_HOME_ENV);
66 if (config_home != NULL)
67 snprintf(config_path, sizeof(config_path), "%s/%s/%s", config_home, WM_NAME, CONFIG_NAME);
69 snprintf(config_path, sizeof(config_path), "%s/%s/%s/%s", getenv("HOME"), ".config", WM_NAME, CONFIG_NAME);
72 dpy = xcb_connect(NULL, &default_screen);
74 if (xcb_connection_has_error(dpy))
75 err("Can't open the default display.\n");
79 dpy_fd = xcb_get_file_descriptor(dpy);
81 char *sp = getenv(SOCKET_ENV_VAR);
82 strncpy(socket_path, (sp == NULL ? DEFAULT_SOCKET_PATH : sp), sizeof(socket_path));
84 sock_address.sun_family = AF_UNIX;
85 strncpy(sock_address.sun_path, socket_path, sizeof(sock_address.sun_path));
88 sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
91 err("Couldn't create the socket.\n");
93 if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
94 err("Couldn't bind a name to the socket.\n");
96 if (listen(sock_fd, SOMAXCONN) == -1)
97 err("Couldn't listen to the socket.\n");
99 sel = MAX(sock_fd, dpy_fd) + 1;
101 if (fifo_path != NULL) {
102 int fifo_fd = open(fifo_path, O_RDWR | O_NONBLOCK);
104 status_fifo = fdopen(fifo_fd, "w");
106 warn("Couldn't open status fifo.\n");
117 FD_ZERO(&descriptors);
118 FD_SET(sock_fd, &descriptors);
119 FD_SET(dpy_fd, &descriptors);
121 if (select(sel, &descriptors, NULL, NULL, NULL) > 0) {
123 if (FD_ISSET(sock_fd, &descriptors)) {
124 ret_fd = accept(sock_fd, NULL, 0);
125 if (ret_fd > 0 && (n = recv(ret_fd, msg, sizeof(msg), 0)) > 0) {
127 if (handle_message(msg, n, rsp)) {
128 rsp_len = strlen(rsp);
129 if (rsp[rsp_len - 1] == '\n')
130 rsp[--rsp_len] = '\0';
132 rsp[0] = MESSAGE_FAILURE;
135 send(ret_fd, rsp, rsp_len, 0);
141 if (FD_ISSET(dpy_fd, &descriptors)) {
142 while ((event = xcb_poll_for_event(dpy)) != NULL) {
150 if (xcb_connection_has_error(dpy))
151 err("The server has closed the connection.\n");
156 if (status_fifo != NULL)
158 xcb_ewmh_connection_wipe(ewmh);
159 xcb_destroy_window(dpy, motion_recorder);
168 num_monitors = num_desktops = num_clients = 0;
169 monitor_uid = desktop_uid = rule_uid = 0;
170 mon = last_mon = mon_head = mon_tail = pri_mon = NULL;
171 rule_head = rule_tail = NULL;
173 last_motion_time = last_motion_x = last_motion_y = 0;
175 visible = auto_raise = true;
183 screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
185 err("Can't acquire the default screen.\n");
189 screen_width = screen->width_in_pixels;
190 screen_height = screen->height_in_pixels;
191 root_depth = screen->root_depth;
193 uint32_t mask = XCB_CW_EVENT_MASK;
194 uint32_t values[] = {XCB_EVENT_MASK_POINTER_MOTION};
195 motion_recorder = xcb_generate_id(dpy);
196 xcb_create_window(dpy, XCB_COPY_FROM_PARENT, motion_recorder, root, 0, 0, screen_width, screen_height, 0, XCB_WINDOW_CLASS_INPUT_ONLY, XCB_COPY_FROM_PARENT, mask, values);
198 xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED,
199 ewmh->_NET_SUPPORTING_WM_CHECK,
200 ewmh->_NET_DESKTOP_NAMES,
201 ewmh->_NET_NUMBER_OF_DESKTOPS,
202 ewmh->_NET_CURRENT_DESKTOP,
203 ewmh->_NET_CLIENT_LIST,
204 ewmh->_NET_ACTIVE_WINDOW,
205 ewmh->_NET_WM_DESKTOP,
207 ewmh->_NET_WM_STATE_FULLSCREEN,
208 ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
209 ewmh->_NET_WM_WINDOW_TYPE,
210 ewmh->_NET_WM_WINDOW_TYPE_DOCK,
211 ewmh->_NET_WM_WINDOW_TYPE_DESKTOP,
212 ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
213 ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
214 ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
215 ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
217 xcb_ewmh_set_wm_name(ewmh, root, strlen(WM_NAME), WM_NAME);
218 xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
219 ewmh_set_supporting(motion_recorder);
223 GETATOM(WM_DELETE_WINDOW)
224 GETATOM(WM_TAKE_FOCUS)
225 GETATOM(_BSPWM_FLOATING_WINDOW)
228 const xcb_query_extension_reply_t *qep = xcb_get_extension_data(dpy, &xcb_randr_id);
229 if (qep->present && import_monitors()) {
231 randr_base = qep->first_event;
232 xcb_randr_select_input(dpy, root, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
235 warn("Couldn't retrieve monitors via RandR.\n");
236 xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
237 monitor_t *m = add_monitor(rect);
238 add_desktop(m, make_desktop(NULL));
241 ewmh_update_number_of_desktops();
242 ewmh_update_desktop_names();
243 ewmh_update_current_desktop();
244 frozen_pointer = make_pointer_state();
245 xcb_get_input_focus_reply_t *ifo = xcb_get_input_focus_reply(dpy, xcb_get_input_focus(dpy), NULL);
246 if (ifo != NULL && (ifo->focus == XCB_INPUT_FOCUS_POINTER_ROOT || ifo->focus == XCB_NONE))
251 void register_events(void)
253 uint32_t values[] = {ROOT_EVENT_MASK};
254 xcb_generic_error_t *e = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, root, XCB_CW_EVENT_MASK, values));
257 err("Another window manager is already running.\n");
261 bool import_monitors(void)
263 PUTS("import monitors");
264 xcb_randr_get_screen_resources_current_reply_t *sres = xcb_randr_get_screen_resources_current_reply(dpy, xcb_randr_get_screen_resources_current(dpy, root), NULL);
268 int len = xcb_randr_get_screen_resources_current_outputs_length(sres);
269 xcb_randr_output_t *outputs = xcb_randr_get_screen_resources_current_outputs(sres);
271 xcb_randr_get_output_info_cookie_t cookies[len];
272 for (int i = 0; i < len; i++)
273 cookies[i] = xcb_randr_get_output_info(dpy, outputs[i], XCB_CURRENT_TIME);
275 for (monitor_t *m = mon_head; m != NULL; m = m->next)
278 monitor_t *mm = NULL;
279 unsigned int num = 0;
281 for (int i = 0; i < len; i++) {
282 xcb_randr_get_output_info_reply_t *info = xcb_randr_get_output_info_reply(dpy, cookies[i], NULL);
283 if (info != NULL && info->crtc != XCB_NONE) {
285 xcb_randr_get_crtc_info_reply_t *cir = xcb_randr_get_crtc_info_reply(dpy, xcb_randr_get_crtc_info(dpy, info->crtc, XCB_CURRENT_TIME), NULL);
287 xcb_rectangle_t rect = (xcb_rectangle_t) {cir->x, cir->y, cir->width, cir->height};
288 mm = get_monitor_by_id(outputs[i]);
290 mm->rectangle = rect;
291 for (desktop_t *d = mm->desk_head; d != NULL; d = d->next)
292 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
293 fit_monitor(mm, n->client);
294 arrange(mm, mm->desk);
296 PRINTF("update monitor %s (0x%X)\n", mm->name, mm->id);
298 mm = add_monitor(rect);
299 char *name = (char *)xcb_randr_get_output_info_name(info);
300 size_t name_len = MIN(sizeof(mm->name), (size_t)xcb_randr_get_output_info_name_length(info));
301 strncpy(mm->name, name, name_len);
302 mm->name[name_len] = '\0';
304 PRINTF("add monitor %s (0x%X)\n", mm->name, mm->id);
313 /* initially focus the primary monitor and add the first desktop to it */
314 xcb_randr_get_output_primary_reply_t *gpo = xcb_randr_get_output_primary_reply(dpy, xcb_randr_get_output_primary(dpy, root), NULL);
316 pri_mon = get_monitor_by_id(gpo->output);
317 if (!running && pri_mon != NULL) {
320 add_desktop(pri_mon, make_desktop(NULL));
321 ewmh_update_current_desktop();
326 /* add one desktop to each new monitor */
327 for (monitor_t *m = mon_head; m != NULL; m = m->next)
328 if (m->desk == NULL && (running || pri_mon == NULL || m != pri_mon))
329 add_desktop(m, make_desktop(NULL));
331 /* merge and remove disconnected monitors */
332 monitor_t *m = mon_head;
334 monitor_t *next = m->next;
336 PRINTF("remove monitor %s (0x%X)\n", m->name, m->id);
337 merge_monitors(m, mm);
344 update_motion_recorder();
345 return (num_monitors > 0);
355 while (mon_head != NULL)
356 remove_monitor(mon_head);
357 while (rule_head != NULL)
358 remove_rule(rule_head);
359 free(frozen_pointer);
362 void put_status(void)
364 if (status_fifo == NULL)
366 if (status_prefix != NULL)
367 fprintf(status_fifo, "%s", status_prefix);
369 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
370 fprintf(status_fifo, "%c%s:", (mon == m ? 'M' : 'm'), m->name);
371 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, urgent = false) {
372 for (node_t *n = first_extrema(d->root); n != NULL && !urgent; n = next_leaf(n, d->root))
373 urgent |= n->client->urgent;
374 char c = (urgent ? 'u' : (d->root == NULL ? 'f' : 'o'));
377 fprintf(status_fifo, "%c%s:", c, d->name);
380 if (mon != NULL && mon->desk != NULL)
381 fprintf(status_fifo, "L%s", (mon->desk->layout == LAYOUT_TILED ? "tiled" : "monocle"));
382 fprintf(status_fifo, "\n");