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