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>
29 int main(int argc, char *argv[])
32 char socket_path[MAXLEN];
33 char *fifo_path = NULL;
34 config_path[0] = '\0';
36 int sock_fd, ret_fd, dpy_fd, sel, n;
37 struct sockaddr_un sock_address;
39 char msg[BUFSIZ] = {0};
40 char rsp[BUFSIZ] = {0};
41 xcb_generic_event_t *event;
44 while ((opt = getopt(argc, argv, "hvc:s:p:")) != -1) {
47 printf(WM_NAME " [-h|-v|-c CONFIG_PATH|-s PANEL_FIFO|-p PANEL_PREFIX]\n");
51 printf("%s\n", VERSION);
55 strncpy(config_path, optarg, sizeof(config_path));
61 status_prefix = optarg;
66 if (config_path[0] == '\0') {
67 char *config_home = getenv(CONFIG_HOME_ENV);
68 if (config_home != NULL)
69 snprintf(config_path, sizeof(config_path), "%s/%s/%s", config_home, WM_NAME, CONFIG_NAME);
71 snprintf(config_path, sizeof(config_path), "%s/%s/%s/%s", getenv("HOME"), ".config", WM_NAME, CONFIG_NAME);
74 dpy = xcb_connect(NULL, &default_screen);
76 if (xcb_connection_has_error(dpy))
77 err("Can't open the default display.\n");
81 dpy_fd = xcb_get_file_descriptor(dpy);
83 char *sp = getenv(SOCKET_ENV_VAR);
84 strncpy(socket_path, (sp == NULL ? DEFAULT_SOCKET_PATH : sp), sizeof(socket_path));
86 sock_address.sun_family = AF_UNIX;
87 strncpy(sock_address.sun_path, socket_path, sizeof(sock_address.sun_path));
90 sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
93 err("Couldn't create the socket.\n");
95 if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
96 err("Couldn't bind a name to the socket.\n");
98 if (listen(sock_fd, SOMAXCONN) == -1)
99 err("Couldn't listen to the socket.\n");
101 sel = MAX(sock_fd, dpy_fd) + 1;
103 if (fifo_path != NULL) {
104 int fifo_fd = open(fifo_path, O_RDWR | O_NONBLOCK);
106 status_fifo = fdopen(fifo_fd, "w");
108 warn("Couldn't open status fifo.\n");
119 FD_ZERO(&descriptors);
120 FD_SET(sock_fd, &descriptors);
121 FD_SET(dpy_fd, &descriptors);
123 if (select(sel, &descriptors, NULL, NULL, NULL) > 0) {
125 if (FD_ISSET(sock_fd, &descriptors)) {
126 ret_fd = accept(sock_fd, NULL, 0);
127 if (ret_fd > 0 && (n = recv(ret_fd, msg, sizeof(msg), 0)) > 0) {
129 if (handle_message(msg, n, rsp)) {
130 rsp_len = strlen(rsp);
131 if (rsp[rsp_len - 1] == '\n')
132 rsp[--rsp_len] = '\0';
134 rsp[0] = MESSAGE_FAILURE;
137 send(ret_fd, rsp, rsp_len, 0);
143 if (FD_ISSET(dpy_fd, &descriptors)) {
144 while ((event = xcb_poll_for_event(dpy)) != NULL) {
152 if (xcb_connection_has_error(dpy))
153 err("The server has closed the connection.\n");
158 if (status_fifo != NULL)
160 xcb_ewmh_connection_wipe(ewmh);
161 xcb_destroy_window(dpy, motion_recorder);
170 num_monitors = num_desktops = num_clients = 0;
171 monitor_uid = desktop_uid = rule_uid = 0;
172 mon = last_mon = mon_head = mon_tail = pri_mon = NULL;
173 rule_head = rule_tail = NULL;
175 last_motion_time = last_motion_x = last_motion_y = 0;
177 visible = auto_raise = true;
185 screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
187 err("Can't acquire the default screen.\n");
191 screen_width = screen->width_in_pixels;
192 screen_height = screen->height_in_pixels;
193 root_depth = screen->root_depth;
195 uint32_t mask = XCB_CW_EVENT_MASK;
196 uint32_t values[] = {XCB_EVENT_MASK_POINTER_MOTION};
197 motion_recorder = xcb_generate_id(dpy);
198 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);
200 xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED,
201 ewmh->_NET_SUPPORTING_WM_CHECK,
202 ewmh->_NET_DESKTOP_NAMES,
203 ewmh->_NET_NUMBER_OF_DESKTOPS,
204 ewmh->_NET_CURRENT_DESKTOP,
205 ewmh->_NET_CLIENT_LIST,
206 ewmh->_NET_ACTIVE_WINDOW,
207 ewmh->_NET_WM_DESKTOP,
209 ewmh->_NET_WM_STATE_FULLSCREEN,
210 ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
211 ewmh->_NET_WM_WINDOW_TYPE,
212 ewmh->_NET_WM_WINDOW_TYPE_DOCK,
213 ewmh->_NET_WM_WINDOW_TYPE_DESKTOP,
214 ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
215 ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
216 ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
217 ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
219 xcb_ewmh_set_wm_name(ewmh, root, strlen(WM_NAME), WM_NAME);
220 xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
221 ewmh_set_supporting(motion_recorder);
225 GETATOM(WM_DELETE_WINDOW)
226 GETATOM(WM_TAKE_FOCUS)
227 GETATOM(_BSPWM_FLOATING_WINDOW)
230 const xcb_query_extension_reply_t *qep = xcb_get_extension_data(dpy, &xcb_randr_id);
231 if (qep->present && import_monitors()) {
233 randr_base = qep->first_event;
234 xcb_randr_select_input(dpy, root, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
237 warn("Couldn't retrieve monitors via RandR.\n");
238 xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
239 monitor_t *m = add_monitor(rect);
240 add_desktop(m, make_desktop(NULL));
243 ewmh_update_number_of_desktops();
244 ewmh_update_desktop_names();
245 ewmh_update_current_desktop();
246 frozen_pointer = make_pointer_state();
247 xcb_get_input_focus_reply_t *ifo = xcb_get_input_focus_reply(dpy, xcb_get_input_focus(dpy), NULL);
248 if (ifo != NULL && (ifo->focus == XCB_INPUT_FOCUS_POINTER_ROOT || ifo->focus == XCB_NONE))
253 void register_events(void)
255 uint32_t values[] = {ROOT_EVENT_MASK};
256 xcb_generic_error_t *e = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, root, XCB_CW_EVENT_MASK, values));
259 err("Another window manager is already running.\n");
270 while (mon_head != NULL)
271 remove_monitor(mon_head);
272 while (rule_head != NULL)
273 remove_rule(rule_head);
274 free(frozen_pointer);
277 void put_status(void)
279 if (status_fifo == NULL)
281 if (status_prefix != NULL)
282 fprintf(status_fifo, "%s", status_prefix);
284 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
285 fprintf(status_fifo, "%c%s:", (mon == m ? 'M' : 'm'), m->name);
286 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, urgent = false) {
287 for (node_t *n = first_extrema(d->root); n != NULL && !urgent; n = next_leaf(n, d->root))
288 urgent |= n->client->urgent;
289 char c = (urgent ? 'u' : (d->root == NULL ? 'f' : 'o'));
292 fprintf(status_fifo, "%c%s:", c, d->name);
295 if (mon != NULL && mon->desk != NULL)
296 fprintf(status_fifo, "L%s", (mon->desk->layout == LAYOUT_TILED ? "tiled" : "monocle"));
297 fprintf(status_fifo, "\n");