7 #include <sys/socket.h>
9 #include <sys/select.h>
13 #include <xcb/xcb_event.h>
14 #include <xcb/xcb_ewmh.h>
15 #include <xcb/randr.h>
35 while (mon_head != NULL)
36 remove_monitor(mon_head);
37 while (rule_head != NULL)
38 remove_rule(rule_head);
42 void register_events(void)
44 uint32_t values[] = {ROOT_EVENT_MASK};
45 xcb_generic_error_t *e = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, root, XCB_CW_EVENT_MASK, values));
48 err("Another window manager is already running.\n");
52 bool import_monitors(void)
54 PUTS("import monitors");
55 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);
59 int len = xcb_randr_get_screen_resources_current_outputs_length(sres);
60 xcb_randr_output_t *outputs = xcb_randr_get_screen_resources_current_outputs(sres);
62 xcb_randr_get_output_info_cookie_t cookies[len];
63 for (int i = 0; i < len; i++)
64 cookies[i] = xcb_randr_get_output_info(dpy, outputs[i], XCB_CURRENT_TIME);
66 for (monitor_t *m = mon_head; m != NULL; m = m->next)
72 for (int i = 0; i < len; i++) {
73 xcb_randr_get_output_info_reply_t *info = xcb_randr_get_output_info_reply(dpy, cookies[i], NULL);
74 if (info != NULL && info->crtc != XCB_NONE) {
76 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);
78 xcb_rectangle_t rect = (xcb_rectangle_t) {cir->x, cir->y, cir->width, cir->height};
79 mm = get_monitor_by_id(outputs[i]);
82 for (desktop_t *d = mm->desk_head; d != NULL; d = d->next)
83 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
84 fit_monitor(mm, n->client);
85 arrange(mm, mm->desk);
87 PRINTF("update monitor %s (0x%X)\n", mm->name, mm->id);
89 mm = add_monitor(&rect);
90 char *name = (char *)xcb_randr_get_output_info_name(info);
91 size_t name_len = MIN(sizeof(mm->name), (size_t)xcb_randr_get_output_info_name_length(info));
92 strncpy(mm->name, name, name_len);
93 mm->name[name_len] = '\0';
95 add_desktop(mm, make_desktop(NULL));
96 PRINTF("add monitor %s (0x%X)\n", mm->name, mm->id);
105 monitor_t *m = mon_head;
107 monitor_t *next = m->next;
109 PRINTF("remove monitor %s (0x%X)\n", m->name, m->id);
110 merge_monitors(m, mm);
117 update_motion_recorder();
118 return (num_monitors > 0);
123 num_monitors = num_desktops = num_clients = 0;
124 monitor_uid = desktop_uid = client_uid = rule_uid = 0;
125 mon = last_mon = mon_head = mon_tail = NULL;
126 rule_head = rule_tail = NULL;
137 screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
139 err("Can't acquire the default screen.\n");
143 screen_width = screen->width_in_pixels;
144 screen_height = screen->height_in_pixels;
145 root_depth = screen->root_depth;
147 uint32_t mask = XCB_CW_EVENT_MASK;
148 uint32_t values[] = {XCB_EVENT_MASK_POINTER_MOTION};
149 motion_recorder = xcb_generate_id(dpy);
150 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);
152 xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED,
153 ewmh->_NET_DESKTOP_NAMES,
154 ewmh->_NET_NUMBER_OF_DESKTOPS,
155 ewmh->_NET_CURRENT_DESKTOP,
156 ewmh->_NET_CLIENT_LIST,
157 ewmh->_NET_ACTIVE_WINDOW,
158 ewmh->_NET_WM_DESKTOP,
160 ewmh->_NET_WM_STATE_FULLSCREEN,
161 ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
162 ewmh->_NET_WM_WINDOW_TYPE,
163 ewmh->_NET_WM_WINDOW_TYPE_DOCK,
164 ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
165 ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
166 ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
167 ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
169 xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
171 xcb_intern_atom_reply_t *iar = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen("_COMPTON_SHADOW"), "_COMPTON_SHADOW"), NULL);
174 compton_shadow = iar->atom;
178 const xcb_query_extension_reply_t *qep = xcb_get_extension_data(dpy, &xcb_randr_id);
179 if (qep->present && import_monitors()) {
181 randr_base = qep->first_event;
182 xcb_randr_select_input(dpy, root, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
185 warn("Couldn't retrieve monitors via RandR.\n");
186 xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
187 monitor_t *m = add_monitor(&rect);
188 add_desktop(m, make_desktop(NULL));
191 ewmh_update_number_of_desktops();
192 ewmh_update_desktop_names();
193 ewmh_update_current_desktop();
194 frozen_pointer = make_pointer_state();
197 int main(int argc, char *argv[])
200 char socket_path[MAXLEN];
201 char *fifo_path = NULL;
202 status_prefix = NULL;
203 int sock_fd, ret_fd, dpy_fd, sel, n;
204 struct sockaddr_un sock_address;
206 char msg[BUFSIZ] = {0};
207 char rsp[BUFSIZ] = {0};
208 xcb_generic_event_t *event;
211 while ((opt = getopt(argc, argv, "hvs:p:")) != -1) {
214 printf("bspwm [-h|-v|-s PANEL_FIFO|-p PANEL_PREFIX]\n");
218 printf("%s\n", VERSION);
225 status_prefix = optarg;
231 dpy = xcb_connect(NULL, &default_screen);
233 if (xcb_connection_has_error(dpy))
234 err("Can't open the default display.\n");
238 dpy_fd = xcb_get_file_descriptor(dpy);
240 char *sp = getenv(SOCKET_ENV_VAR);
241 strncpy(socket_path, (sp == NULL ? DEFAULT_SOCKET_PATH : sp), sizeof(socket_path));
243 sock_address.sun_family = AF_UNIX;
244 strncpy(sock_address.sun_path, socket_path, sizeof(sock_address.sun_path));
247 sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
250 err("Couldn't create the socket.\n");
252 if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
253 err("Couldn't bind a name to the socket.\n");
255 if (listen(sock_fd, SOMAXCONN) == -1)
256 err("Couldn't listen to the socket.\n");
258 sel = MAX(sock_fd, dpy_fd) + 1;
260 if (fifo_path != NULL) {
261 int fifo_fd = open(fifo_path, O_RDWR | O_NONBLOCK);
263 status_fifo = fdopen(fifo_fd, "w");
265 warn("Couldn't open status fifo.\n");
270 ewmh_update_wm_name();
276 FD_ZERO(&descriptors);
277 FD_SET(sock_fd, &descriptors);
278 FD_SET(dpy_fd, &descriptors);
280 if (select(sel, &descriptors, NULL, NULL, NULL) > 0) {
282 if (FD_ISSET(sock_fd, &descriptors)) {
283 ret_fd = accept(sock_fd, NULL, 0);
284 if (ret_fd > 0 && (n = recv(ret_fd, msg, sizeof(msg), 0)) > 0) {
286 process_message(msg, rsp);
287 rsplen = strlen(rsp);
288 if (rsp[rsplen - 1] == '\n')
289 rsp[--rsplen] = '\0';
290 send(ret_fd, rsp, rsplen, 0);
296 if (FD_ISSET(dpy_fd, &descriptors)) {
297 while ((event = xcb_poll_for_event(dpy)) != NULL) {
305 if (xcb_connection_has_error(dpy))
306 err("The server has closed the connection.\n");
311 if (status_fifo != NULL)
313 xcb_ewmh_connection_wipe(ewmh);
314 xcb_destroy_window(dpy, motion_recorder);