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("bspwm [-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);
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");
111 ewmh_update_wm_name();
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 = 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_NOTIFICATION,
212 ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
213 ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
214 ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
216 xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
217 ewmh_set_supporting(motion_recorder);
219 xcb_intern_atom_reply_t *iar = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen("_COMPTON_SHADOW"), "_COMPTON_SHADOW"), NULL);
222 compton_shadow = iar->atom;
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();
245 void register_events(void)
247 uint32_t values[] = {ROOT_EVENT_MASK};
248 xcb_generic_error_t *e = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, root, XCB_CW_EVENT_MASK, values));
251 err("Another window manager is already running.\n");
255 bool import_monitors(void)
257 PUTS("import monitors");
258 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);
262 int len = xcb_randr_get_screen_resources_current_outputs_length(sres);
263 xcb_randr_output_t *outputs = xcb_randr_get_screen_resources_current_outputs(sres);
265 xcb_randr_get_output_info_cookie_t cookies[len];
266 for (int i = 0; i < len; i++)
267 cookies[i] = xcb_randr_get_output_info(dpy, outputs[i], XCB_CURRENT_TIME);
269 for (monitor_t *m = mon_head; m != NULL; m = m->next)
272 monitor_t *mm = NULL;
273 unsigned int num = 0;
275 for (int i = 0; i < len; i++) {
276 xcb_randr_get_output_info_reply_t *info = xcb_randr_get_output_info_reply(dpy, cookies[i], NULL);
277 if (info != NULL && info->crtc != XCB_NONE) {
279 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);
281 xcb_rectangle_t rect = (xcb_rectangle_t) {cir->x, cir->y, cir->width, cir->height};
282 mm = get_monitor_by_id(outputs[i]);
284 mm->rectangle = rect;
285 for (desktop_t *d = mm->desk_head; d != NULL; d = d->next)
286 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
287 fit_monitor(mm, n->client);
288 arrange(mm, mm->desk);
290 PRINTF("update monitor %s (0x%X)\n", mm->name, mm->id);
292 mm = add_monitor(&rect);
293 char *name = (char *)xcb_randr_get_output_info_name(info);
294 size_t name_len = MIN(sizeof(mm->name), (size_t)xcb_randr_get_output_info_name_length(info));
295 strncpy(mm->name, name, name_len);
296 mm->name[name_len] = '\0';
298 add_desktop(mm, make_desktop(NULL));
299 PRINTF("add monitor %s (0x%X)\n", mm->name, mm->id);
308 monitor_t *m = mon_head;
310 monitor_t *next = m->next;
312 PRINTF("remove monitor %s (0x%X)\n", m->name, m->id);
313 merge_monitors(m, mm);
320 update_motion_recorder();
321 return (num_monitors > 0);
331 while (mon_head != NULL)
332 remove_monitor(mon_head);
333 while (rule_head != NULL)
334 remove_rule(rule_head);
335 free(frozen_pointer);
338 void put_status(void)
340 if (status_fifo == NULL)
342 if (status_prefix != NULL)
343 fprintf(status_fifo, "%s", status_prefix);
345 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
346 fprintf(status_fifo, "%c%s:", (mon == m ? 'M' : 'm'), m->name);
347 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, urgent = false) {
348 for (node_t *n = first_extrema(d->root); n != NULL && !urgent; n = next_leaf(n, d->root))
349 urgent |= n->client->urgent;
350 fprintf(status_fifo, "%c%s:", m->desk == d ? (urgent ? 'U' : 'D') : (d->root == NULL ? 'E' : (urgent ? 'u' : 'd')), d->name);
353 if (mon != NULL && mon->desk != NULL)
354 fprintf(status_fifo, "L%s", (mon->desk->layout == LAYOUT_TILED ? "tiled" : "monocle"));
355 fprintf(status_fifo, "\n");