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