7 #include <sys/socket.h>
9 #include <sys/select.h>
11 #include <xcb/xcb_event.h>
12 #include <xcb/xcb_ewmh.h>
13 #include <xcb/randr.h>
26 int main(int argc, char *argv[])
29 char socket_path[MAXLEN];
30 char *fifo_path = NULL;
31 config_path[0] = '\0';
33 int sock_fd, ret_fd, dpy_fd, sel, n;
34 struct sockaddr_un sock_address;
36 char msg[BUFSIZ] = {0};
37 char rsp[BUFSIZ] = {0};
38 xcb_generic_event_t *event;
41 while ((opt = getopt(argc, argv, "hvc:s:p:")) != -1) {
44 printf(WM_NAME " [-h|-v|-c CONFIG_PATH|-s PANEL_FIFO|-p PANEL_PREFIX]\n");
48 printf("%s\n", VERSION);
52 strncpy(config_path, optarg, sizeof(config_path));
58 status_prefix = optarg;
63 if (config_path[0] == '\0') {
64 char *config_home = getenv(CONFIG_HOME_ENV);
65 if (config_home != NULL)
66 snprintf(config_path, sizeof(config_path), "%s/%s/%s", config_home, WM_NAME, CONFIG_NAME);
68 snprintf(config_path, sizeof(config_path), "%s/%s/%s/%s", getenv("HOME"), ".config", WM_NAME, CONFIG_NAME);
71 dpy = xcb_connect(NULL, &default_screen);
73 if (xcb_connection_has_error(dpy))
74 err("Can't open the default display.\n");
78 dpy_fd = xcb_get_file_descriptor(dpy);
80 char *sp = getenv(SOCKET_ENV_VAR);
81 strncpy(socket_path, (sp == NULL ? DEFAULT_SOCKET_PATH : sp), sizeof(socket_path));
83 sock_address.sun_family = AF_UNIX;
84 strncpy(sock_address.sun_path, socket_path, sizeof(sock_address.sun_path));
87 sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
90 err("Couldn't create the socket.\n");
92 if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
93 err("Couldn't bind a name to the socket.\n");
95 if (listen(sock_fd, SOMAXCONN) == -1)
96 err("Couldn't listen to the socket.\n");
98 sel = MAX(sock_fd, dpy_fd) + 1;
100 if (fifo_path != NULL) {
101 int fifo_fd = open(fifo_path, O_RDWR | O_NONBLOCK);
103 status_fifo = fdopen(fifo_fd, "w");
105 warn("Couldn't open status fifo.\n");
116 FD_ZERO(&descriptors);
117 FD_SET(sock_fd, &descriptors);
118 FD_SET(dpy_fd, &descriptors);
120 if (select(sel, &descriptors, NULL, NULL, NULL) > 0) {
122 if (FD_ISSET(sock_fd, &descriptors)) {
123 ret_fd = accept(sock_fd, NULL, 0);
124 if (ret_fd > 0 && (n = recv(ret_fd, msg, sizeof(msg), 0)) > 0) {
126 if (handle_message(msg, n, rsp)) {
127 rsp_len = strlen(rsp);
128 if (rsp[rsp_len - 1] == '\n')
129 rsp[--rsp_len] = '\0';
131 rsp[0] = MESSAGE_FAILURE;
134 send(ret_fd, rsp, rsp_len, 0);
140 if (FD_ISSET(dpy_fd, &descriptors)) {
141 while ((event = xcb_poll_for_event(dpy)) != NULL) {
149 if (xcb_connection_has_error(dpy))
150 err("The server has closed the connection.\n");
155 if (status_fifo != NULL)
157 xcb_ewmh_connection_wipe(ewmh);
158 xcb_destroy_window(dpy, motion_recorder);
167 num_monitors = num_desktops = num_clients = 0;
168 monitor_uid = desktop_uid = rule_uid = 0;
169 mon = last_mon = mon_head = mon_tail = pri_mon = NULL;
170 rule_head = rule_tail = NULL;
172 last_motion_time = last_motion_x = last_motion_y = 0;
174 visible = auto_raise = true;
182 screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
184 err("Can't acquire the default screen.\n");
188 screen_width = screen->width_in_pixels;
189 screen_height = screen->height_in_pixels;
190 root_depth = screen->root_depth;
192 uint32_t mask = XCB_CW_EVENT_MASK;
193 uint32_t values[] = {XCB_EVENT_MASK_POINTER_MOTION};
194 motion_recorder = xcb_generate_id(dpy);
195 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);
197 xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED,
198 ewmh->_NET_SUPPORTING_WM_CHECK,
199 ewmh->_NET_DESKTOP_NAMES,
200 ewmh->_NET_NUMBER_OF_DESKTOPS,
201 ewmh->_NET_CURRENT_DESKTOP,
202 ewmh->_NET_CLIENT_LIST,
203 ewmh->_NET_ACTIVE_WINDOW,
204 ewmh->_NET_WM_DESKTOP,
206 ewmh->_NET_WM_STATE_FULLSCREEN,
207 ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
208 ewmh->_NET_WM_WINDOW_TYPE,
209 ewmh->_NET_WM_WINDOW_TYPE_DOCK,
210 ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
211 ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
212 ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
213 ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
215 xcb_ewmh_set_wm_name(ewmh, root, strlen(WM_NAME), WM_NAME);
216 xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
217 ewmh_set_supporting(motion_recorder);
221 GETATOM(WM_DELETE_WINDOW)
222 GETATOM(WM_TAKE_FOCUS)
223 GETATOM(_BSPWM_FLOATING_WINDOW)
226 const xcb_query_extension_reply_t *qep = xcb_get_extension_data(dpy, &xcb_randr_id);
227 if (qep->present && import_monitors()) {
229 randr_base = qep->first_event;
230 xcb_randr_select_input(dpy, root, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
233 warn("Couldn't retrieve monitors via RandR.\n");
234 xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
235 monitor_t *m = add_monitor(&rect);
236 add_desktop(m, make_desktop(NULL));
239 ewmh_update_number_of_desktops();
240 ewmh_update_desktop_names();
241 ewmh_update_current_desktop();
242 frozen_pointer = make_pointer_state();
243 xcb_get_input_focus_reply_t *ifo = xcb_get_input_focus_reply(dpy, xcb_get_input_focus(dpy), NULL);
244 if (ifo != NULL && (ifo->focus == XCB_INPUT_FOCUS_POINTER_ROOT || ifo->focus == XCB_NONE))
249 void register_events(void)
251 uint32_t values[] = {ROOT_EVENT_MASK};
252 xcb_generic_error_t *e = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, root, XCB_CW_EVENT_MASK, values));
255 err("Another window manager is already running.\n");
259 bool import_monitors(void)
261 PUTS("import monitors");
262 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);
266 int len = xcb_randr_get_screen_resources_current_outputs_length(sres);
267 xcb_randr_output_t *outputs = xcb_randr_get_screen_resources_current_outputs(sres);
269 xcb_randr_get_output_info_cookie_t cookies[len];
270 for (int i = 0; i < len; i++)
271 cookies[i] = xcb_randr_get_output_info(dpy, outputs[i], XCB_CURRENT_TIME);
273 for (monitor_t *m = mon_head; m != NULL; m = m->next)
276 monitor_t *mm = NULL;
277 unsigned int num = 0;
279 for (int i = 0; i < len; i++) {
280 xcb_randr_get_output_info_reply_t *info = xcb_randr_get_output_info_reply(dpy, cookies[i], NULL);
281 if (info != NULL && info->crtc != XCB_NONE) {
283 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);
285 xcb_rectangle_t rect = (xcb_rectangle_t) {cir->x, cir->y, cir->width, cir->height};
286 mm = get_monitor_by_id(outputs[i]);
288 mm->rectangle = rect;
289 for (desktop_t *d = mm->desk_head; d != NULL; d = d->next)
290 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
291 fit_monitor(mm, n->client);
292 arrange(mm, mm->desk);
294 PRINTF("update monitor %s (0x%X)\n", mm->name, mm->id);
296 mm = add_monitor(&rect);
297 char *name = (char *)xcb_randr_get_output_info_name(info);
298 size_t name_len = MIN(sizeof(mm->name), (size_t)xcb_randr_get_output_info_name_length(info));
299 strncpy(mm->name, name, name_len);
300 mm->name[name_len] = '\0';
302 PRINTF("add monitor %s (0x%X)\n", mm->name, mm->id);
311 /* initially focus the primary monitor and add the first desktop to it */
312 xcb_randr_get_output_primary_reply_t *gpo = xcb_randr_get_output_primary_reply(dpy, xcb_randr_get_output_primary(dpy, root), NULL);
314 pri_mon = get_monitor_by_id(gpo->output);
315 if (!running && pri_mon != NULL) {
318 add_desktop(pri_mon, make_desktop(NULL));
319 ewmh_update_current_desktop();
324 /* add one desktop to each new monitor */
325 for (monitor_t *m = mon_head; m != NULL; m = m->next)
326 if (m->desk == NULL && (running || pri_mon == NULL || m != pri_mon))
327 add_desktop(m, make_desktop(NULL));
329 /* merge and remove disconnected monitors */
330 monitor_t *m = mon_head;
332 monitor_t *next = m->next;
334 PRINTF("remove monitor %s (0x%X)\n", m->name, m->id);
335 merge_monitors(m, mm);
342 update_motion_recorder();
343 return (num_monitors > 0);
353 while (mon_head != NULL)
354 remove_monitor(mon_head);
355 while (rule_head != NULL)
356 remove_rule(rule_head);
357 free(frozen_pointer);
360 void put_status(void)
362 if (status_fifo == NULL)
364 if (status_prefix != NULL)
365 fprintf(status_fifo, "%s", status_prefix);
367 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
368 fprintf(status_fifo, "%c%s:", (mon == m ? 'M' : 'm'), m->name);
369 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, urgent = false) {
370 for (node_t *n = first_extrema(d->root); n != NULL && !urgent; n = next_leaf(n, d->root))
371 urgent |= n->client->urgent;
372 fprintf(status_fifo, "%c%s:", m->desk == d ? (urgent ? 'U' : 'D') : (d->root == NULL ? 'E' : (urgent ? 'u' : 'd')), d->name);
375 if (mon != NULL && mon->desk != NULL)
376 fprintf(status_fifo, "L%s", (mon->desk->layout == LAYOUT_TILED ? "tiled" : "monocle"));
377 fprintf(status_fifo, "\n");