]> git.lizzy.rs Git - bspwm.git/blob - src/bspwm.c
Fix windows sometimes not appearing
[bspwm.git] / src / bspwm.c
1 /* Copyright (c) 2012, Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this
8  *    list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  *    this list of conditions and the following disclaimer in the documentation
11  *    and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <sys/time.h>
30 #include <sys/wait.h>
31 #include <sys/socket.h>
32 #include <sys/un.h>
33 #include <fcntl.h>
34 #include <signal.h>
35 #include <unistd.h>
36 #include <stdbool.h>
37 #include <string.h>
38 #include <xcb/xinerama.h>
39 #include <xcb/xcb_aux.h>
40 #include "types.h"
41 #include "desktop.h"
42 #include "monitor.h"
43 #include "settings.h"
44 #include "messages.h"
45 #include "pointer.h"
46 #include "events.h"
47 #include "common.h"
48 #include "window.h"
49 #include "history.h"
50 #include "ewmh.h"
51 #include "rule.h"
52 #include "restore.h"
53 #include "query.h"
54 #include "bspwm.h"
55
56 xcb_connection_t *dpy;
57 int default_screen, screen_width, screen_height;
58 uint32_t clients_count;
59 xcb_screen_t *screen;
60 xcb_window_t root;
61 char config_path[MAXLEN];
62
63 monitor_t *mon;
64 monitor_t *mon_head;
65 monitor_t *mon_tail;
66 monitor_t *pri_mon;
67 history_t *history_head;
68 history_t *history_tail;
69 history_t *history_needle;
70 rule_t *rule_head;
71 rule_t *rule_tail;
72 stacking_list_t *stack_head;
73 stacking_list_t *stack_tail;
74 subscriber_list_t *subscribe_head;
75 subscriber_list_t *subscribe_tail;
76 pending_rule_t *pending_rule_head;
77 pending_rule_t *pending_rule_tail;
78
79 xcb_window_t meta_window;
80 motion_recorder_t motion_recorder;
81 xcb_atom_t WM_STATE;
82 xcb_atom_t WM_TAKE_FOCUS;
83 xcb_atom_t WM_DELETE_WINDOW;
84 int exit_status;
85
86 bool auto_raise;
87 bool sticky_still;
88 bool hide_sticky;
89 bool record_history;
90 bool running;
91 bool restart;
92 bool randr;
93
94 int main(int argc, char *argv[])
95 {
96         fd_set descriptors;
97         char socket_path[MAXLEN];
98         char state_path[MAXLEN] = {0};
99         int run_level = 0;
100         config_path[0] = '\0';
101         int sock_fd = -1, cli_fd, dpy_fd, max_fd, n;
102         struct sockaddr_un sock_address;
103         char msg[BUFSIZ] = {0};
104         xcb_generic_event_t *event;
105         char *end;
106         int opt;
107
108         while ((opt = getopt(argc, argv, "hvc:s:o:")) != -1) {
109                 switch (opt) {
110                         case 'h':
111                                 printf(WM_NAME " [-h|-v|-c CONFIG_PATH]\n");
112                                 exit(EXIT_SUCCESS);
113                                 break;
114                         case 'v':
115                                 printf("%s\n", VERSION);
116                                 exit(EXIT_SUCCESS);
117                                 break;
118                         case 'c':
119                                 snprintf(config_path, sizeof(config_path), "%s", optarg);
120                                 break;
121                         case 's':
122                                 run_level |= 1;
123                                 snprintf(state_path, sizeof(state_path), "%s", optarg);
124                                 break;
125                         case 'o':
126                                 run_level |= 2;
127                                 sock_fd = strtol(optarg, &end, 0);
128                                 if (*end != '\0') {
129                                         sock_fd = -1;
130                                 }
131                                 break;
132                 }
133         }
134
135         if (config_path[0] == '\0') {
136                 char *config_home = getenv(CONFIG_HOME_ENV);
137                 if (config_home != NULL) {
138                         snprintf(config_path, sizeof(config_path), "%s/%s/%s", config_home, WM_NAME, CONFIG_NAME);
139                 } else {
140                         snprintf(config_path, sizeof(config_path), "%s/%s/%s/%s", getenv("HOME"), ".config", WM_NAME, CONFIG_NAME);
141                 }
142         }
143
144         dpy = xcb_connect(NULL, &default_screen);
145
146         if (!check_connection(dpy)) {
147                 exit(EXIT_FAILURE);
148         }
149
150         load_settings();
151         setup();
152
153         if (state_path[0] != '\0') {
154                 restore_state(state_path);
155                 unlink(state_path);
156         }
157
158         dpy_fd = xcb_get_file_descriptor(dpy);
159
160         if (sock_fd == -1) {
161                 char *sp = getenv(SOCKET_ENV_VAR);
162                 if (sp != NULL) {
163                         snprintf(socket_path, sizeof(socket_path), "%s", sp);
164                 } else {
165                         char *host = NULL;
166                         int dn = 0, sn = 0;
167                         if (xcb_parse_display(NULL, &host, &dn, &sn) != 0) {
168                                 snprintf(socket_path, sizeof(socket_path), SOCKET_PATH_TPL, host, dn, sn);
169                         }
170                         free(host);
171                 }
172
173                 sock_address.sun_family = AF_UNIX;
174                 if (snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), "%s", socket_path) < 0) {
175                         err("Couldn't write the socket path.\n");
176                 }
177
178                 sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
179
180                 if (sock_fd == -1) {
181                         err("Couldn't create the socket.\n");
182                 }
183
184                 unlink(socket_path);
185
186                 if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1) {
187                         err("Couldn't bind a name to the socket.\n");
188                 }
189
190                 if (listen(sock_fd, SOMAXCONN) == -1) {
191                         err("Couldn't listen to the socket.\n");
192                 }
193         }
194
195         fcntl(sock_fd, F_SETFD, FD_CLOEXEC | fcntl(sock_fd, F_GETFD));
196
197         signal(SIGINT, sig_handler);
198         signal(SIGHUP, sig_handler);
199         signal(SIGTERM, sig_handler);
200         signal(SIGCHLD, sig_handler);
201         signal(SIGPIPE, SIG_IGN);
202         run_config(run_level);
203         running = true;
204
205         while (running) {
206
207                 xcb_flush(dpy);
208
209                 FD_ZERO(&descriptors);
210                 FD_SET(sock_fd, &descriptors);
211                 FD_SET(dpy_fd, &descriptors);
212                 max_fd = MAX(sock_fd, dpy_fd);
213
214                 for (pending_rule_t *pr = pending_rule_head; pr != NULL; pr = pr->next) {
215                         FD_SET(pr->fd, &descriptors);
216                         if (pr->fd > max_fd) {
217                                 max_fd = pr->fd;
218                         }
219                 }
220
221                 if (select(max_fd + 1, &descriptors, NULL, NULL, NULL) > 0) {
222
223                         pending_rule_t *pr = pending_rule_head;
224                         while (pr != NULL) {
225                                 pending_rule_t *next = pr->next;
226                                 if (FD_ISSET(pr->fd, &descriptors)) {
227                                         if (manage_window(pr->win, pr->csq, pr->fd)) {
228                                                 for (event_queue_t *eq = pr->event_head; eq != NULL; eq = eq->next) {
229                                                         handle_event(&eq->event);
230                                                 }
231                                         }
232                                         remove_pending_rule(pr);
233                                 }
234                                 pr = next;
235                         }
236
237                         if (FD_ISSET(sock_fd, &descriptors)) {
238                                 cli_fd = accept(sock_fd, NULL, 0);
239                                 if (cli_fd > 0 && (n = recv(cli_fd, msg, sizeof(msg)-1, 0)) > 0) {
240                                         msg[n] = '\0';
241                                         FILE *rsp = fdopen(cli_fd, "w");
242                                         if (rsp != NULL) {
243                                                 handle_message(msg, n, rsp);
244                                         } else {
245                                                 warn("Can't open the client socket as file.\n");
246                                                 close(cli_fd);
247                                         }
248                                 }
249                         }
250
251                         if (FD_ISSET(dpy_fd, &descriptors)) {
252                                 xcb_aux_sync(dpy);
253                                 while ((event = xcb_poll_for_event(dpy)) != NULL) {
254                                         handle_event(event);
255                                         free(event);
256                                 }
257                         }
258
259                 }
260
261                 if (!check_connection(dpy)) {
262                         running = false;
263                 }
264
265                 prune_dead_subscribers();
266         }
267
268         if (restart) {
269                 char *host = NULL;
270                 int dn = 0, sn = 0;
271                 if (xcb_parse_display(NULL, &host, &dn, &sn) != 0) {
272                         snprintf(state_path, sizeof(state_path), STATE_PATH_TPL, host, dn, sn);
273                 }
274                 free(host);
275                 FILE *f = fopen(state_path, "w");
276                 query_state(f);
277                 fclose(f);
278         }
279
280         cleanup();
281         ungrab_buttons();
282         xcb_ewmh_connection_wipe(ewmh);
283         xcb_destroy_window(dpy, meta_window);
284         xcb_destroy_window(dpy, motion_recorder.id);
285         free(ewmh);
286         xcb_flush(dpy);
287         xcb_disconnect(dpy);
288
289         if (restart) {
290                 fcntl(sock_fd, F_SETFD, ~FD_CLOEXEC & fcntl(sock_fd, F_GETFD));
291
292                 int rargc;
293                 for (rargc = 0; rargc < argc; rargc++) {
294                         if (streq("-s", argv[rargc])) {
295                                 break;
296                         }
297                 }
298
299                 int len = rargc + 5;
300                 char **rargv = malloc(len * sizeof(char *));
301
302                 for (int i = 0; i < rargc; i++) {
303                         rargv[i] = argv[i];
304                 }
305
306                 char sock_fd_arg[SMALEN];
307                 snprintf(sock_fd_arg, sizeof(sock_fd_arg), "%i", sock_fd);
308
309                 rargv[rargc] = "-s";
310                 rargv[rargc + 1] = state_path;
311                 rargv[rargc + 2] = "-o";
312                 rargv[rargc + 3] = sock_fd_arg;
313                 rargv[rargc + 4] = 0;
314
315                 exit_status = execvp(*rargv, rargv);
316                 free(rargv);
317         }
318
319         close(sock_fd);
320         unlink(socket_path);
321
322         return exit_status;
323 }
324
325 void init(void)
326 {
327         clients_count = 0;
328         mon = mon_head = mon_tail = pri_mon = NULL;
329         history_head = history_tail = history_needle = NULL;
330         rule_head = rule_tail = NULL;
331         stack_head = stack_tail = NULL;
332         subscribe_head = subscribe_tail = NULL;
333         pending_rule_head = pending_rule_tail = NULL;
334         auto_raise = sticky_still = hide_sticky = record_history = true;
335         randr_base = 0;
336         exit_status = 0;
337         restart = false;
338 }
339
340 void setup(void)
341 {
342         init();
343         ewmh_init();
344         pointer_init();
345
346         screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
347
348         if (screen == NULL) {
349                 err("Can't acquire the default screen.\n");
350         }
351
352         root = screen->root;
353         register_events();
354
355         screen_width = screen->width_in_pixels;
356         screen_height = screen->height_in_pixels;
357
358         meta_window = xcb_generate_id(dpy);
359         xcb_create_window(dpy, XCB_COPY_FROM_PARENT, meta_window, root, -1, -1, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_ONLY, XCB_COPY_FROM_PARENT, XCB_NONE, NULL);
360         xcb_icccm_set_wm_class(dpy, meta_window, sizeof(META_WINDOW_IC), META_WINDOW_IC);
361
362         motion_recorder.id = xcb_generate_id(dpy);
363         motion_recorder.sequence = 0;
364         motion_recorder.enabled = false;
365         uint32_t values[] = {XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_POINTER_MOTION};
366         xcb_create_window(dpy, XCB_COPY_FROM_PARENT, motion_recorder.id, root, 0, 0, 1, 1, 0,
367                           XCB_WINDOW_CLASS_INPUT_ONLY, XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, values);
368         xcb_icccm_set_wm_class(dpy, motion_recorder.id, sizeof(MOTION_RECORDER_IC), MOTION_RECORDER_IC);
369
370         xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED,
371                                   ewmh->_NET_SUPPORTING_WM_CHECK,
372                                   ewmh->_NET_DESKTOP_NAMES,
373                                   ewmh->_NET_DESKTOP_VIEWPORT,
374                                   ewmh->_NET_NUMBER_OF_DESKTOPS,
375                                   ewmh->_NET_CURRENT_DESKTOP,
376                                   ewmh->_NET_CLIENT_LIST,
377                                   ewmh->_NET_ACTIVE_WINDOW,
378                                   ewmh->_NET_CLOSE_WINDOW,
379                                   ewmh->_NET_WM_STRUT_PARTIAL,
380                                   ewmh->_NET_WM_DESKTOP,
381                                   ewmh->_NET_WM_STATE,
382                                   ewmh->_NET_WM_STATE_HIDDEN,
383                                   ewmh->_NET_WM_STATE_FULLSCREEN,
384                                   ewmh->_NET_WM_STATE_BELOW,
385                                   ewmh->_NET_WM_STATE_ABOVE,
386                                   ewmh->_NET_WM_STATE_STICKY,
387                                   ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
388                                   ewmh->_NET_WM_WINDOW_TYPE,
389                                   ewmh->_NET_WM_WINDOW_TYPE_DOCK,
390                                   ewmh->_NET_WM_WINDOW_TYPE_DESKTOP,
391                                   ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
392                                   ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
393                                   ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
394                                   ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
395
396         xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
397         ewmh_set_supporting(meta_window);
398
399 #define GETATOM(a) \
400         get_atom(#a, &a);
401         GETATOM(WM_STATE)
402         GETATOM(WM_DELETE_WINDOW)
403         GETATOM(WM_TAKE_FOCUS)
404 #undef GETATOM
405
406         const xcb_query_extension_reply_t *qep = xcb_get_extension_data(dpy, &xcb_randr_id);
407         if (qep->present && update_monitors()) {
408                 randr = true;
409                 randr_base = qep->first_event;
410                 xcb_randr_select_input(dpy, root, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
411         } else {
412                 randr = false;
413                 warn("Couldn't retrieve monitors via RandR.\n");
414                 bool xinerama_is_active = false;
415
416                 if (xcb_get_extension_data(dpy, &xcb_xinerama_id)->present) {
417                         xcb_xinerama_is_active_reply_t *xia = xcb_xinerama_is_active_reply(dpy, xcb_xinerama_is_active(dpy), NULL);
418                         if (xia != NULL) {
419                                 xinerama_is_active = xia->state;
420                                 free(xia);
421                         }
422                 }
423
424                 if (xinerama_is_active) {
425                         xcb_xinerama_query_screens_reply_t *xsq = xcb_xinerama_query_screens_reply(dpy, xcb_xinerama_query_screens(dpy), NULL);
426                         xcb_xinerama_screen_info_t *xsi = xcb_xinerama_query_screens_screen_info(xsq);
427                         int n = xcb_xinerama_query_screens_screen_info_length(xsq);
428                         for (int i = 0; i < n; i++) {
429                                 xcb_xinerama_screen_info_t info = xsi[i];
430                                 xcb_rectangle_t rect = (xcb_rectangle_t) {info.x_org, info.y_org, info.width, info.height};
431                                 monitor_t *m = make_monitor(NULL, &rect, XCB_NONE);
432                                 add_monitor(m);
433                                 add_desktop(m, make_desktop(NULL, XCB_NONE));
434                         }
435                         free(xsq);
436                 } else {
437                         warn("Xinerama is inactive.\n");
438                         xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
439                         monitor_t *m = make_monitor(NULL, &rect, XCB_NONE);
440                         add_monitor(m);
441                         add_desktop(m, make_desktop(NULL, XCB_NONE));
442                 }
443         }
444
445         ewmh_update_number_of_desktops();
446         ewmh_update_desktop_names();
447         ewmh_update_desktop_viewport();
448         ewmh_update_current_desktop();
449         xcb_get_input_focus_reply_t *ifo = xcb_get_input_focus_reply(dpy, xcb_get_input_focus(dpy), NULL);
450         if (ifo != NULL && (ifo->focus == XCB_INPUT_FOCUS_POINTER_ROOT || ifo->focus == XCB_NONE)) {
451                 clear_input_focus();
452         }
453         free(ifo);
454 }
455
456 void register_events(void)
457 {
458         uint32_t values[] = {ROOT_EVENT_MASK};
459         xcb_generic_error_t *e = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, root, XCB_CW_EVENT_MASK, values));
460         if (e != NULL) {
461                 free(e);
462                 xcb_ewmh_connection_wipe(ewmh);
463                 free(ewmh);
464                 xcb_disconnect(dpy);
465                 err("Another window manager is already running.\n");
466         }
467 }
468
469 void cleanup(void)
470 {
471         mon = NULL;
472
473         while (mon_head != NULL) {
474                 remove_monitor(mon_head);
475         }
476         while (rule_head != NULL) {
477                 remove_rule(rule_head);
478         }
479         while (subscribe_head != NULL) {
480                 remove_subscriber(subscribe_head);
481         }
482         while (pending_rule_head != NULL) {
483                 remove_pending_rule(pending_rule_head);
484         }
485
486         empty_history();
487 }
488
489 bool check_connection (xcb_connection_t *dpy)
490 {
491         int xerr;
492         if ((xerr = xcb_connection_has_error(dpy)) != 0) {
493                 warn("The server closed the connection: ");
494                 switch (xerr) {
495                         case XCB_CONN_ERROR:
496                                 warn("socket, pipe or stream error.\n");
497                                 break;
498                         case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
499                                 warn("unsupported extension.\n");
500                                 break;
501                         case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
502                                 warn("not enough memory.\n");
503                                 break;
504                         case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
505                                 warn("request length exceeded.\n");
506                                 break;
507                         case XCB_CONN_CLOSED_PARSE_ERR:
508                                 warn("can't parse display string.\n");
509                                 break;
510                         case XCB_CONN_CLOSED_INVALID_SCREEN:
511                                 warn("invalid screen.\n");
512                                 break;
513                         case XCB_CONN_CLOSED_FDPASSING_FAILED:
514                                 warn("failed to pass FD.\n");
515                                 break;
516                         default:
517                                 warn("unknown error.\n");
518                                 break;
519                 }
520                 return false;
521         } else {
522                 return true;
523         }
524 }
525
526 void sig_handler(int sig)
527 {
528         if (sig == SIGCHLD) {
529                 signal(sig, sig_handler);
530                 while (waitpid(-1, 0, WNOHANG) > 0)
531                         ;
532         } else if (sig == SIGINT || sig == SIGHUP || sig == SIGTERM) {
533                 running = false;
534         }
535 }
536
537 /* Adapted from i3wm */
538 uint32_t get_color_pixel(const char *color)
539 {
540         unsigned int red, green, blue;
541         if (sscanf(color + 1, "%02x%02x%02x", &red, &green, &blue) == 3) {
542                 /* We set the first 8 bits high to have 100% opacity in case of a 32 bit
543                  * color depth visual. */
544                 return (0xFF << 24) | (red << 16 | green << 8 | blue);
545         } else {
546                 return screen->black_pixel;
547         }
548 }