]> git.lizzy.rs Git - bspwm.git/blob - bspwm.c
Set the WM_STATE atom
[bspwm.git] / bspwm.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <signal.h>
6 #include <fcntl.h>
7 #include <ctype.h>
8 #include <sys/socket.h>
9 #include <sys/un.h>
10 #include <sys/select.h>
11 #include <xcb/xcb.h>
12 #include <xcb/xcb_event.h>
13 #include <xcb/xcb_ewmh.h>
14 #include <xcb/randr.h>
15 #include "types.h"
16 #include "settings.h"
17 #include "messages.h"
18 #include "rules.h"
19 #include "events.h"
20 #include "common.h"
21 #include "helpers.h"
22 #include "window.h"
23 #include "bspwm.h"
24 #include "tree.h"
25 #include "ewmh.h"
26
27 int main(int argc, char *argv[])
28 {
29     fd_set descriptors;
30     char socket_path[MAXLEN];
31     char *fifo_path = NULL;
32     config_path[0] = '\0';
33     status_prefix = NULL;
34     int sock_fd, ret_fd, dpy_fd, sel, n;
35     struct sockaddr_un sock_address;
36     size_t rsp_len = 0;
37     char msg[BUFSIZ] = {0};
38     char rsp[BUFSIZ] = {0};
39     xcb_generic_event_t *event;
40     char opt;
41
42     while ((opt = getopt(argc, argv, "hvc:s:p:")) != -1) {
43         switch (opt) {
44             case 'h':
45                 printf(WM_NAME " [-h|-v|-c CONFIG_PATH|-s PANEL_FIFO|-p PANEL_PREFIX]\n");
46                 exit(EXIT_SUCCESS);
47                 break;
48             case 'v':
49                 printf("%s\n", VERSION);
50                 exit(EXIT_SUCCESS);
51                 break;
52             case 'c':
53                 strncpy(config_path, optarg, sizeof(config_path));
54                 break;
55             case 's':
56                 fifo_path = optarg;
57                 break;
58             case 'p':
59                 status_prefix = optarg;
60                 break;
61         }
62     }
63
64     if (config_path[0] == '\0') {
65         char *config_home = getenv(CONFIG_HOME_ENV);
66         if (config_home != NULL)
67             snprintf(config_path, sizeof(config_path), "%s/%s/%s", config_home, WM_NAME, CONFIG_NAME);
68         else
69             snprintf(config_path, sizeof(config_path), "%s/%s/%s/%s", getenv("HOME"), ".config", WM_NAME, CONFIG_NAME);
70     }
71
72     dpy = xcb_connect(NULL, &default_screen);
73
74     if (xcb_connection_has_error(dpy))
75         err("Can't open the default display.\n");
76
77     setup();
78
79     dpy_fd = xcb_get_file_descriptor(dpy);
80
81     char *sp = getenv(SOCKET_ENV_VAR);
82     strncpy(socket_path, (sp == NULL ? DEFAULT_SOCKET_PATH : sp), sizeof(socket_path));
83
84     sock_address.sun_family = AF_UNIX;
85     strncpy(sock_address.sun_path, socket_path, sizeof(sock_address.sun_path));
86     unlink(socket_path);
87
88     sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
89
90     if (sock_fd == -1)
91         err("Couldn't create the socket.\n");
92
93     if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
94         err("Couldn't bind a name to the socket.\n");
95
96     if (listen(sock_fd, SOMAXCONN) == -1)
97         err("Couldn't listen to the socket.\n");
98
99     sel = MAX(sock_fd, dpy_fd) + 1;
100
101     if (fifo_path != NULL) {
102         int fifo_fd = open(fifo_path, O_RDWR | O_NONBLOCK);
103         if (fifo_fd != -1)
104             status_fifo = fdopen(fifo_fd, "w");
105         else
106             warn("Couldn't open status fifo.\n");
107     }
108
109     load_settings();
110     run_config();
111     running = true;
112
113     while (running) {
114
115         xcb_flush(dpy);
116
117         FD_ZERO(&descriptors);
118         FD_SET(sock_fd, &descriptors);
119         FD_SET(dpy_fd, &descriptors);
120
121         if (select(sel, &descriptors, NULL, NULL, NULL) > 0) {
122
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) {
126                     msg[n] = '\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';
131                     } else {
132                         rsp[0] = MESSAGE_FAILURE;
133                         rsp_len = 1;
134                     }
135                     send(ret_fd, rsp, rsp_len, 0);
136                     close(ret_fd);
137                     rsp[0] = '\0';
138                 }
139             }
140
141             if (FD_ISSET(dpy_fd, &descriptors)) {
142                 while ((event = xcb_poll_for_event(dpy)) != NULL) {
143                     handle_event(event);
144                     free(event);
145                 }
146             }
147
148         }
149
150         if (xcb_connection_has_error(dpy))
151             err("The server has closed the connection.\n");
152     }
153
154     cleanup();
155     close(sock_fd);
156     if (status_fifo != NULL)
157         fclose(status_fifo);
158     xcb_ewmh_connection_wipe(ewmh);
159     xcb_destroy_window(dpy, motion_recorder);
160     free(ewmh);
161     xcb_flush(dpy);
162     xcb_disconnect(dpy);
163     return exit_status;
164 }
165
166 void init(void)
167 {
168     num_monitors = num_desktops = num_clients = 0;
169     monitor_uid = desktop_uid = rule_uid = 0;
170     mon = last_mon = mon_head = mon_tail = pri_mon = NULL;
171     rule_head = rule_tail = NULL;
172     status_fifo = NULL;
173     last_motion_time = last_motion_x = last_motion_y = 0;
174     randr_base = 0;
175     visible = auto_raise = true;
176     exit_status = 0;
177 }
178
179 void setup(void)
180 {
181     init();
182     ewmh_init();
183     screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
184     if (screen == NULL)
185         err("Can't acquire the default screen.\n");
186     root = screen->root;
187     register_events();
188
189     screen_width = screen->width_in_pixels;
190     screen_height = screen->height_in_pixels;
191     root_depth = screen->root_depth;
192
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);
197
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,
206                               ewmh->_NET_WM_STATE,
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_DESKTOP,
212                               ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
213                               ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
214                               ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
215                               ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
216
217     xcb_ewmh_set_wm_name(ewmh, root, strlen(WM_NAME), WM_NAME);
218     xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
219     ewmh_set_supporting(motion_recorder);
220
221 #define GETATOM(a) \
222     get_atom(#a, &a);
223     GETATOM(WM_STATE)
224     GETATOM(WM_DELETE_WINDOW)
225     GETATOM(WM_TAKE_FOCUS)
226     GETATOM(_BSPWM_FLOATING_WINDOW)
227 #undef GETATOM
228
229     const xcb_query_extension_reply_t *qep = xcb_get_extension_data(dpy, &xcb_randr_id);
230     if (qep->present && import_monitors()) {
231         randr = true;
232         randr_base = qep->first_event;
233         xcb_randr_select_input(dpy, root, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
234     } else {
235         randr = false;
236         warn("Couldn't retrieve monitors via RandR.\n");
237         xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
238         monitor_t *m = add_monitor(rect);
239         add_desktop(m, make_desktop(NULL));
240     }
241
242     ewmh_update_number_of_desktops();
243     ewmh_update_desktop_names();
244     ewmh_update_current_desktop();
245     frozen_pointer = make_pointer_state();
246     xcb_get_input_focus_reply_t *ifo = xcb_get_input_focus_reply(dpy, xcb_get_input_focus(dpy), NULL);
247     if (ifo != NULL && (ifo->focus == XCB_INPUT_FOCUS_POINTER_ROOT || ifo->focus == XCB_NONE))
248         clear_input_focus();
249     free(ifo);
250 }
251
252 void register_events(void)
253 {
254     uint32_t values[] = {ROOT_EVENT_MASK};
255     xcb_generic_error_t *e = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, root, XCB_CW_EVENT_MASK, values));
256     if (e != NULL) {
257         xcb_disconnect(dpy);
258         err("Another window manager is already running.\n");
259     }
260 }
261
262 bool import_monitors(void)
263 {
264     PUTS("import monitors");
265     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);
266     if (sres == NULL)
267         return false;
268
269     int len = xcb_randr_get_screen_resources_current_outputs_length(sres);
270     xcb_randr_output_t *outputs = xcb_randr_get_screen_resources_current_outputs(sres);
271
272     xcb_randr_get_output_info_cookie_t cookies[len];
273     for (int i = 0; i < len; i++)
274         cookies[i] = xcb_randr_get_output_info(dpy, outputs[i], XCB_CURRENT_TIME);
275
276     for (monitor_t *m = mon_head; m != NULL; m = m->next)
277         m->wired = false;
278
279     monitor_t *mm = NULL;
280     unsigned int num = 0;
281
282     for (int i = 0; i < len; i++) {
283         xcb_randr_get_output_info_reply_t *info = xcb_randr_get_output_info_reply(dpy, cookies[i], NULL);
284         if (info != NULL && info->crtc != XCB_NONE) {
285
286             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);
287             if (cir != NULL) {
288                 xcb_rectangle_t rect = (xcb_rectangle_t) {cir->x, cir->y, cir->width, cir->height};
289                 mm = get_monitor_by_id(outputs[i]);
290                 if (mm != NULL) {
291                     mm->rectangle = rect;
292                     for (desktop_t *d = mm->desk_head; d != NULL; d = d->next)
293                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
294                             fit_monitor(mm, n->client);
295                     arrange(mm, mm->desk);
296                     mm->wired = true;
297                     PRINTF("update monitor %s (0x%X)\n", mm->name, mm->id);
298                 } else {
299                     mm = add_monitor(rect);
300                     char *name = (char *)xcb_randr_get_output_info_name(info);
301                     size_t name_len = MIN(sizeof(mm->name), (size_t)xcb_randr_get_output_info_name_length(info));
302                     strncpy(mm->name, name, name_len);
303                     mm->name[name_len] = '\0';
304                     mm->id = outputs[i];
305                     PRINTF("add monitor %s (0x%X)\n", mm->name, mm->id);
306                 }
307                 num++;
308             }
309             free(cir);
310         }
311         free(info);
312     }
313
314     /* initially focus the primary monitor and add the first desktop to it */
315     xcb_randr_get_output_primary_reply_t *gpo = xcb_randr_get_output_primary_reply(dpy, xcb_randr_get_output_primary(dpy, root), NULL);
316     if (gpo != NULL) {
317         pri_mon = get_monitor_by_id(gpo->output);
318         if (!running && pri_mon != NULL) {
319             if (mon != pri_mon)
320                 mon = pri_mon;
321             add_desktop(pri_mon, make_desktop(NULL));
322             ewmh_update_current_desktop();
323         }
324     }
325     free(gpo);
326
327     /* add one desktop to each new monitor */
328     for (monitor_t *m = mon_head; m != NULL; m = m->next)
329         if (m->desk == NULL && (running || pri_mon == NULL || m != pri_mon))
330             add_desktop(m, make_desktop(NULL));
331
332     /* merge and remove disconnected monitors */
333     monitor_t *m = mon_head;
334     while (m != NULL) {
335         monitor_t *next = m->next;
336         if (!m->wired) {
337             PRINTF("remove monitor %s (0x%X)\n", m->name, m->id);
338             merge_monitors(m, mm);
339             remove_monitor(m);
340         }
341         m = next;
342     }
343
344     free(sres);
345     update_motion_recorder();
346     return (num_monitors > 0);
347 }
348
349 void quit(void)
350 {
351     running = false;
352 }
353
354 void cleanup(void)
355 {
356     while (mon_head != NULL)
357         remove_monitor(mon_head);
358     while (rule_head != NULL)
359         remove_rule(rule_head);
360     free(frozen_pointer);
361 }
362
363 void put_status(void)
364 {
365     if (status_fifo == NULL)
366         return;
367     if (status_prefix != NULL)
368         fprintf(status_fifo, "%s", status_prefix);
369     bool urgent = false;
370     for (monitor_t *m = mon_head; m != NULL; m = m->next) {
371         fprintf(status_fifo, "%c%s:", (mon == m ? 'M' : 'm'), m->name);
372         for (desktop_t *d = m->desk_head; d != NULL; d = d->next, urgent = false) {
373             for (node_t *n = first_extrema(d->root); n != NULL && !urgent; n = next_leaf(n, d->root))
374                 urgent |= n->client->urgent;
375             char c = (urgent ? 'u' : (d->root == NULL ? 'f' : 'o'));
376             if (m->desk == d)
377                 c = toupper(c);
378             fprintf(status_fifo, "%c%s:", c, d->name);
379         }
380     }
381     if (mon != NULL && mon->desk != NULL)
382         fprintf(status_fifo, "L%s", (mon->desk->layout == LAYOUT_TILED ? "tiled" : "monocle"));
383     fprintf(status_fifo, "\n");
384     fflush(status_fifo);
385 }