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