7 #include <sys/socket.h>
9 #include <sys/select.h>
23 int main(int argc, char *argv[])
26 char socket_path[MAXLEN];
27 char *fifo_path = NULL;
28 config_path[0] = '\0';
30 int sock_fd, ret_fd, dpy_fd, sel, n;
31 struct sockaddr_un sock_address;
33 char msg[BUFSIZ] = {0};
34 char rsp[BUFSIZ] = {0};
35 xcb_generic_event_t *event;
38 while ((opt = getopt(argc, argv, "hvc:s:p:")) != -1) {
41 printf(WM_NAME " [-h|-v|-c CONFIG_PATH|-s PANEL_FIFO|-p PANEL_PREFIX]\n");
45 printf("%s\n", VERSION);
49 snprintf(config_path, sizeof(config_path), "%s", optarg);
55 status_prefix = optarg;
60 if (config_path[0] == '\0') {
61 char *config_home = getenv(CONFIG_HOME_ENV);
62 if (config_home != NULL)
63 snprintf(config_path, sizeof(config_path), "%s/%s/%s", config_home, WM_NAME, CONFIG_NAME);
65 snprintf(config_path, sizeof(config_path), "%s/%s/%s/%s", getenv("HOME"), ".config", WM_NAME, CONFIG_NAME);
68 dpy = xcb_connect(NULL, &default_screen);
70 if (xcb_connection_has_error(dpy))
71 err("Can't open the default display.\n");
75 dpy_fd = xcb_get_file_descriptor(dpy);
77 char *sp = getenv(SOCKET_ENV_VAR);
78 snprintf(socket_path, sizeof(socket_path), "%s", (sp == NULL ? DEFAULT_SOCKET_PATH : sp));
80 sock_address.sun_family = AF_UNIX;
81 snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), "%s", socket_path);
84 sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
87 err("Couldn't create the socket.\n");
89 if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
90 err("Couldn't bind a name to the socket.\n");
92 if (listen(sock_fd, SOMAXCONN) == -1)
93 err("Couldn't listen to the socket.\n");
95 sel = MAX(sock_fd, dpy_fd) + 1;
97 if (fifo_path != NULL) {
98 int fifo_fd = open(fifo_path, O_RDWR | O_NONBLOCK);
100 status_fifo = fdopen(fifo_fd, "w");
102 warn("Couldn't open status fifo.\n");
113 FD_ZERO(&descriptors);
114 FD_SET(sock_fd, &descriptors);
115 FD_SET(dpy_fd, &descriptors);
117 if (select(sel, &descriptors, NULL, NULL, NULL) > 0) {
119 if (FD_ISSET(sock_fd, &descriptors)) {
120 ret_fd = accept(sock_fd, NULL, 0);
121 if (ret_fd > 0 && (n = recv(ret_fd, msg, sizeof(msg), 0)) > 0) {
123 if (handle_message(msg, n, rsp)) {
124 rsp_len = strlen(rsp);
125 if (rsp[rsp_len - 1] == '\n')
126 rsp[--rsp_len] = '\0';
128 rsp[0] = MESSAGE_FAILURE;
131 send(ret_fd, rsp, rsp_len, 0);
137 if (FD_ISSET(dpy_fd, &descriptors)) {
138 while ((event = xcb_poll_for_event(dpy)) != NULL) {
146 if (xcb_connection_has_error(dpy))
147 err("The server has closed the connection.\n");
152 if (status_fifo != NULL)
154 xcb_ewmh_connection_wipe(ewmh);
155 xcb_destroy_window(dpy, motion_recorder);
164 num_monitors = num_desktops = num_clients = 0;
165 monitor_uid = desktop_uid = rule_uid = 0;
166 mon = last_mon = mon_head = mon_tail = pri_mon = NULL;
167 rule_head = rule_tail = NULL;
169 last_motion_time = last_motion_x = last_motion_y = 0;
171 visible = auto_raise = true;
179 screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
181 err("Can't acquire the default screen.\n");
185 screen_width = screen->width_in_pixels;
186 screen_height = screen->height_in_pixels;
187 root_depth = screen->root_depth;
189 uint32_t mask = XCB_CW_EVENT_MASK;
190 uint32_t values[] = {XCB_EVENT_MASK_POINTER_MOTION};
191 motion_recorder = xcb_generate_id(dpy);
192 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);
194 xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED,
195 ewmh->_NET_SUPPORTING_WM_CHECK,
196 ewmh->_NET_DESKTOP_NAMES,
197 ewmh->_NET_NUMBER_OF_DESKTOPS,
198 ewmh->_NET_CURRENT_DESKTOP,
199 ewmh->_NET_CLIENT_LIST,
200 ewmh->_NET_ACTIVE_WINDOW,
201 ewmh->_NET_WM_DESKTOP,
203 ewmh->_NET_WM_STATE_FULLSCREEN,
204 ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
205 ewmh->_NET_WM_WINDOW_TYPE,
206 ewmh->_NET_WM_WINDOW_TYPE_DOCK,
207 ewmh->_NET_WM_WINDOW_TYPE_DESKTOP,
208 ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
209 ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
210 ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
211 ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
213 xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
214 ewmh_set_supporting(motion_recorder);
218 GETATOM(WM_DELETE_WINDOW)
219 GETATOM(WM_TAKE_FOCUS)
220 GETATOM(_BSPWM_FLOATING_WINDOW)
223 const xcb_query_extension_reply_t *qep = xcb_get_extension_data(dpy, &xcb_randr_id);
224 if (qep->present && import_monitors()) {
226 randr_base = qep->first_event;
227 xcb_randr_select_input(dpy, root, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
230 warn("Couldn't retrieve monitors via RandR.\n");
231 xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
232 monitor_t *m = add_monitor(rect);
233 add_desktop(m, make_desktop(NULL));
236 ewmh_update_number_of_desktops();
237 ewmh_update_desktop_names();
238 ewmh_update_current_desktop();
239 frozen_pointer = make_pointer_state();
240 xcb_get_input_focus_reply_t *ifo = xcb_get_input_focus_reply(dpy, xcb_get_input_focus(dpy), NULL);
241 if (ifo != NULL && (ifo->focus == XCB_INPUT_FOCUS_POINTER_ROOT || ifo->focus == XCB_NONE))
246 void register_events(void)
248 uint32_t values[] = {ROOT_EVENT_MASK};
249 xcb_generic_error_t *e = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, root, XCB_CW_EVENT_MASK, values));
252 err("Another window manager is already running.\n");
263 while (mon_head != NULL)
264 remove_monitor(mon_head);
265 while (rule_head != NULL)
266 remove_rule(rule_head);
267 free(frozen_pointer);
270 void put_status(void)
272 if (status_fifo == NULL)
274 if (status_prefix != NULL)
275 fprintf(status_fifo, "%s", status_prefix);
277 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
278 fprintf(status_fifo, "%c%s:", (mon == m ? 'M' : 'm'), m->name);
279 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, urgent = false) {
280 for (node_t *n = first_extrema(d->root); n != NULL && !urgent; n = next_leaf(n, d->root))
281 urgent |= n->client->urgent;
282 char c = (urgent ? 'u' : (d->root == NULL ? 'f' : 'o'));
285 fprintf(status_fifo, "%c%s:", c, d->name);
288 if (mon != NULL && mon->desk != NULL)
289 fprintf(status_fifo, "L%s", (mon->desk->layout == LAYOUT_TILED ? "tiled" : "monocle"));
290 fprintf(status_fifo, "\n");