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 arrange(mm, mm->desk);
84 PRINTF("update monitor %s (0x%X)\n", mm->name, mm->id);
86 mm = add_monitor(&rect);
87 char *name = (char *)xcb_randr_get_output_info_name(info);
88 size_t name_len = MIN(sizeof(mm->name), (size_t)xcb_randr_get_output_info_name_length(info));
89 strncpy(mm->name, name, name_len);
90 mm->name[name_len] = '\0';
92 add_desktop(mm, NULL);
93 PRINTF("add monitor %s (0x%X)\n", mm->name, mm->id);
102 monitor_t *m = mon_head;
104 monitor_t *next = m->next;
106 PRINTF("remove monitor %s (0x%X)\n", m->name, m->id);
107 transfer_desktops(mm, m);
114 return (num_monitors > 0);
119 num_monitors = num_desktops = num_clients = 0;
120 monitor_uid = desktop_uid = client_uid = rule_uid = 0;
121 mon = last_mon = mon_head = mon_tail = NULL;
122 rule_head = rule_tail = NULL;
124 split_mode = MODE_AUTOMATIC;
133 screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
135 err("Can't acquire the default screen.\n");
139 screen_width = screen->width_in_pixels;
140 screen_height = screen->height_in_pixels;
141 root_depth = screen->root_depth;
143 uint32_t mask = XCB_CW_EVENT_MASK;
144 uint32_t values[] = {XCB_EVENT_MASK_POINTER_MOTION};
145 motion_recorder = xcb_generate_id(dpy);
146 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);
148 xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED,
149 ewmh->_NET_DESKTOP_NAMES,
150 ewmh->_NET_NUMBER_OF_DESKTOPS,
151 ewmh->_NET_CURRENT_DESKTOP,
152 ewmh->_NET_CLIENT_LIST,
153 ewmh->_NET_ACTIVE_WINDOW,
154 ewmh->_NET_WM_DESKTOP,
156 ewmh->_NET_WM_STATE_FULLSCREEN,
157 ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
158 ewmh->_NET_WM_WINDOW_TYPE,
159 ewmh->_NET_WM_WINDOW_TYPE_DOCK,
160 ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
161 ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
162 ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
163 ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
165 xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
167 xcb_intern_atom_reply_t *iar = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen("_COMPTON_SHADOW"), "_COMPTON_SHADOW"), NULL);
170 compton_shadow = iar->atom;
174 const xcb_query_extension_reply_t *qep = xcb_get_extension_data(dpy, &xcb_randr_id);
175 if (qep->present && import_monitors()) {
176 randr_base = qep->first_event;
177 xcb_randr_select_input(dpy, root, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
179 warn("Couldn't retrieve monitors via RandR.\n");
180 xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
181 monitor_t *m = add_monitor(&rect);
182 add_desktop(m, NULL);
185 ewmh_update_number_of_desktops();
186 ewmh_update_desktop_names();
187 ewmh_update_current_desktop();
188 frozen_pointer = make_pointer_state();
191 int main(int argc, char *argv[])
194 char socket_path[MAXLEN];
195 char *fifo_path = NULL;
196 status_prefix = NULL;
197 int sock_fd, ret_fd, dpy_fd, sel, n;
198 struct sockaddr_un sock_address;
200 char msg[BUFSIZ] = {0};
201 char rsp[BUFSIZ] = {0};
202 xcb_generic_event_t *event;
205 while ((opt = getopt(argc, argv, "hvs:p:")) != -1) {
208 printf("bspwm [-h|-v|-s PANEL_FIFO|-p PANEL_PREFIX]\n");
212 printf("%s\n", VERSION);
219 status_prefix = optarg;
225 dpy = xcb_connect(NULL, &default_screen);
227 if (xcb_connection_has_error(dpy))
228 err("Can't open the default display.\n");
232 dpy_fd = xcb_get_file_descriptor(dpy);
234 char *sp = getenv(SOCKET_ENV_VAR);
235 strncpy(socket_path, (sp == NULL ? DEFAULT_SOCKET_PATH : sp), sizeof(socket_path));
237 sock_address.sun_family = AF_UNIX;
238 strncpy(sock_address.sun_path, socket_path, sizeof(sock_address.sun_path));
241 sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
244 err("Couldn't create the socket.\n");
246 if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
247 err("Couldn't bind a name to the socket.\n");
249 if (listen(sock_fd, SOMAXCONN) == -1)
250 err("Couldn't listen to the socket.\n");
252 sel = MAX(sock_fd, dpy_fd) + 1;
254 if (fifo_path != NULL) {
255 int fifo_fd = open(fifo_path, O_RDWR | O_NONBLOCK);
257 status_fifo = fdopen(fifo_fd, "w");
259 warn("Couldn't open status fifo.\n");
264 ewmh_update_wm_name();
270 FD_ZERO(&descriptors);
271 FD_SET(sock_fd, &descriptors);
272 FD_SET(dpy_fd, &descriptors);
274 if (select(sel, &descriptors, NULL, NULL, NULL) > 0) {
276 if (FD_ISSET(sock_fd, &descriptors)) {
277 ret_fd = accept(sock_fd, NULL, 0);
278 if (ret_fd > 0 && (n = recv(ret_fd, msg, sizeof(msg), 0)) > 0) {
280 process_message(msg, rsp);
281 rsplen = strlen(rsp);
282 if (rsp[rsplen - 1] == '\n')
283 rsp[--rsplen] = '\0';
284 send(ret_fd, rsp, rsplen, 0);
290 if (FD_ISSET(dpy_fd, &descriptors)) {
291 while ((event = xcb_poll_for_event(dpy)) != NULL) {
299 if (xcb_connection_has_error(dpy))
300 err("The server has closed the connection.\n");
305 if (status_fifo != NULL)
307 xcb_ewmh_connection_wipe(ewmh);
308 xcb_destroy_window(dpy, motion_recorder);