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