]> git.lizzy.rs Git - bspwm.git/blob - bspwm.c
1a1c432253652ef80f57b0d96a534818a4715e34
[bspwm.git] / bspwm.c
1 /* Copyright (c) 2012-2014, 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  * The views and conclusions contained in the software and documentation are those
25  * of the authors and should not be interpreted as representing official policies,
26  * either expressed or implied, of the FreeBSD Project.
27  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/select.h>
33 #ifdef __OpenBSD__
34 #include <sys/types.h>
35 #endif
36 #include <sys/wait.h>
37 #include <sys/socket.h>
38 #include <sys/un.h>
39 #include <signal.h>
40 #include <unistd.h>
41 #include <xcb/xinerama.h>
42 #include "types.h"
43 #include "desktop.h"
44 #include "monitor.h"
45 #include "settings.h"
46 #include "messages.h"
47 #include "subscribe.h"
48 #include "events.h"
49 #include "common.h"
50 #include "window.h"
51 #include "history.h"
52 #include "stack.h"
53 #include "ewmh.h"
54 #include "rule.h"
55 #include "bspwm.h"
56
57 int main(int argc, char *argv[])
58 {
59         fd_set descriptors;
60         char socket_path[MAXLEN];
61         config_path[0] = '\0';
62         int sock_fd, cli_fd, dpy_fd, max_fd, n;
63         struct sockaddr_un sock_address;
64         char msg[BUFSIZ] = {0};
65         xcb_generic_event_t *event;
66         char opt;
67
68         while ((opt = getopt(argc, argv, "hvc:")) != (char)-1) {
69                 switch (opt) {
70                         case 'h':
71                                 printf(WM_NAME " [-h|-v|-c CONFIG_PATH]\n");
72                                 exit(EXIT_SUCCESS);
73                                 break;
74                         case 'v':
75                                 printf("%s\n", VERSION);
76                                 exit(EXIT_SUCCESS);
77                                 break;
78                         case 'c':
79                                 snprintf(config_path, sizeof(config_path), "%s", optarg);
80                                 break;
81                 }
82         }
83
84         if (config_path[0] == '\0') {
85                 char *config_home = getenv(CONFIG_HOME_ENV);
86                 if (config_home != NULL)
87                         snprintf(config_path, sizeof(config_path), "%s/%s/%s", config_home, WM_NAME, CONFIG_NAME);
88                 else
89                         snprintf(config_path, sizeof(config_path), "%s/%s/%s/%s", getenv("HOME"), ".config", WM_NAME, CONFIG_NAME);
90         }
91
92         dpy = xcb_connect(NULL, &default_screen);
93
94         if (xcb_connection_has_error(dpy))
95                 err("Can't open the default display.\n");
96
97         setup();
98
99         dpy_fd = xcb_get_file_descriptor(dpy);
100
101         char *sp = getenv(SOCKET_ENV_VAR);
102         if (sp != NULL)
103                 snprintf(socket_path, sizeof(socket_path), "%s", sp);
104         else
105                 snprintf(socket_path, sizeof(socket_path), SOCKET_PATH_TPL, getenv("DISPLAY"));
106
107         sock_address.sun_family = AF_UNIX;
108         snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), "%s", socket_path);
109
110         sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
111
112         if (sock_fd == -1)
113                 err("Couldn't create the socket.\n");
114
115         if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
116                 err("Couldn't bind a name to the socket.\n");
117
118         if (listen(sock_fd, SOMAXCONN) == -1)
119                 err("Couldn't listen to the socket.\n");
120
121         signal(SIGINT, sig_handler);
122         signal(SIGHUP, sig_handler);
123         signal(SIGTERM, sig_handler);
124         signal(SIGCHLD, sig_handler);
125         signal(SIGPIPE, SIG_IGN);
126         load_settings();
127         run_config();
128         running = true;
129
130         while (running) {
131
132                 xcb_flush(dpy);
133
134                 FD_ZERO(&descriptors);
135                 FD_SET(sock_fd, &descriptors);
136                 FD_SET(dpy_fd, &descriptors);
137                 max_fd = MAX(sock_fd, dpy_fd);
138                 for (pending_rule_t *pr = pending_rule_head; pr != NULL; pr = pr->next) {
139                         FD_SET(pr->fd, &descriptors);
140                         if (pr->fd > max_fd)
141                                 max_fd = pr->fd;
142                 }
143
144                 if (select(max_fd + 1, &descriptors, NULL, NULL, NULL) > 0) {
145
146                         pending_rule_t *pr = pending_rule_head;
147                         while (pr != NULL) {
148                                 pending_rule_t *next = pr->next;
149                                 if (FD_ISSET(pr->fd, &descriptors)) {
150                                         manage_window(pr->win, pr->csq, pr->fd);
151                                         remove_pending_rule(pr);
152                                 }
153                                 pr = next;
154                         }
155
156                         if (FD_ISSET(sock_fd, &descriptors)) {
157                                 cli_fd = accept(sock_fd, NULL, 0);
158                                 if (cli_fd > 0 && (n = recv(cli_fd, msg, sizeof(msg), 0)) > 0) {
159                                         msg[n] = '\0';
160                                         FILE *rsp = fdopen(cli_fd, "w");
161                                         if (rsp != NULL) {
162                                                 int ret = handle_message(msg, n, rsp);
163                                                 if (ret == MSG_SUBSCRIBE) {
164                                                         add_subscriber(rsp);
165                                                 } else {
166                                                         if (ret != MSG_SUCCESS)
167                                                                 fprintf(rsp, "%c", ret);
168                                                         fflush(rsp);
169                                                         fclose(rsp);
170                                                 }
171                                         } else {
172                                                 warn("Can't open the client socket as file.\n");
173                                                 close(cli_fd);
174                                         }
175                                 }
176                         }
177
178                         if (FD_ISSET(dpy_fd, &descriptors)) {
179                                 while ((event = xcb_poll_for_event(dpy)) != NULL) {
180                                         handle_event(event);
181                                         free(event);
182                                 }
183                         }
184                 }
185
186                 if (xcb_connection_has_error(dpy)) {
187                         warn("The server closed the connection.\n");
188                         running = false;
189                 }
190         }
191
192         cleanup();
193         close(sock_fd);
194         unlink(socket_path);
195         xcb_ewmh_connection_wipe(ewmh);
196         xcb_destroy_window(dpy, motion_recorder);
197         free(ewmh);
198         xcb_flush(dpy);
199         xcb_disconnect(dpy);
200         return exit_status;
201 }
202
203 void init(void)
204 {
205         num_monitors = num_desktops = num_clients = 0;
206         monitor_uid = desktop_uid = 0;
207         mon = mon_head = mon_tail = pri_mon = NULL;
208         history_head = history_tail = history_needle = NULL;
209         rule_head = rule_tail = NULL;
210         stack_head = stack_tail = NULL;
211         subscribe_head = subscribe_tail = NULL;
212         pending_rule_head = pending_rule_tail = NULL;
213         last_motion_time = last_motion_x = last_motion_y = 0;
214         visible = auto_raise = sticky_still = record_history = true;
215         randr_base = 0;
216         exit_status = 0;
217 }
218
219 void setup(void)
220 {
221         init();
222         ewmh_init();
223         screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
224         if (screen == NULL)
225                 err("Can't acquire the default screen.\n");
226         root = screen->root;
227         register_events();
228
229         screen_width = screen->width_in_pixels;
230         screen_height = screen->height_in_pixels;
231         root_depth = screen->root_depth;
232
233         uint32_t mask = XCB_CW_EVENT_MASK;
234         uint32_t values[] = {XCB_EVENT_MASK_POINTER_MOTION};
235         motion_recorder = xcb_generate_id(dpy);
236         xcb_create_window(dpy, XCB_COPY_FROM_PARENT, motion_recorder, root, 0, 0, screen_width, screen_height, 0, XCB_WINDOW_CLASS_INPUT_ONLY, XCB_COPY_FROM_PARENT, mask, values);
237
238         xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED,
239                                   ewmh->_NET_SUPPORTING_WM_CHECK,
240                                   ewmh->_NET_DESKTOP_NAMES,
241                                   ewmh->_NET_NUMBER_OF_DESKTOPS,
242                                   ewmh->_NET_CURRENT_DESKTOP,
243                                   ewmh->_NET_CLIENT_LIST,
244                                   ewmh->_NET_ACTIVE_WINDOW,
245                                   ewmh->_NET_CLOSE_WINDOW,
246                                   ewmh->_NET_WM_DESKTOP,
247                                   ewmh->_NET_WM_STATE,
248                                   ewmh->_NET_WM_STATE_FULLSCREEN,
249                                   ewmh->_NET_WM_STATE_STICKY,
250                                   ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
251                                   ewmh->_NET_WM_WINDOW_TYPE,
252                                   ewmh->_NET_WM_WINDOW_TYPE_DOCK,
253                                   ewmh->_NET_WM_WINDOW_TYPE_DESKTOP,
254                                   ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
255                                   ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
256                                   ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
257                                   ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
258
259         xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
260         ewmh_set_supporting(motion_recorder);
261
262 #define GETATOM(a) \
263         get_atom(#a, &a);
264         GETATOM(WM_DELETE_WINDOW)
265         GETATOM(WM_TAKE_FOCUS)
266         GETATOM(_BSPWM_FLOATING_WINDOW)
267 #undef GETATOM
268
269         const xcb_query_extension_reply_t *qep = xcb_get_extension_data(dpy, &xcb_randr_id);
270         if (qep->present && update_monitors()) {
271                 randr = true;
272                 randr_base = qep->first_event;
273                 xcb_randr_select_input(dpy, root, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
274         } else {
275                 randr = false;
276                 warn("Couldn't retrieve monitors via RandR.\n");
277                 bool xinerama_is_active = false;
278                 if (xcb_get_extension_data(dpy, &xcb_xinerama_id)->present) {
279                         xcb_xinerama_is_active_reply_t *xia = xcb_xinerama_is_active_reply(dpy, xcb_xinerama_is_active(dpy), NULL);
280                         if (xia != NULL) {
281                                 xinerama_is_active = xia->state;
282                                 free(xia);
283                         }
284                 }
285
286                 if (xinerama_is_active) {
287                         xcb_xinerama_query_screens_reply_t *xsq = xcb_xinerama_query_screens_reply(dpy, xcb_xinerama_query_screens(dpy), NULL);
288                         xcb_xinerama_screen_info_t *xsi = xcb_xinerama_query_screens_screen_info(xsq);
289                         int n = xcb_xinerama_query_screens_screen_info_length(xsq);
290                         for (int i = 0; i < n; i++) {
291                                 xcb_xinerama_screen_info_t info = xsi[i];
292                                 xcb_rectangle_t rect = (xcb_rectangle_t) {info.x_org, info.y_org, info.width, info.height};
293                                 monitor_t *m = add_monitor(rect);
294                                 add_desktop(m, make_desktop(NULL));
295                         }
296                         free(xsq);
297                 } else {
298                         warn("Xinerama is inactive.\n");
299                         xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
300                         monitor_t *m = add_monitor(rect);
301                         add_desktop(m, make_desktop(NULL));
302                 }
303         }
304
305         ewmh_update_number_of_desktops();
306         ewmh_update_desktop_names();
307         ewmh_update_current_desktop();
308         frozen_pointer = make_pointer_state();
309         xcb_get_input_focus_reply_t *ifo = xcb_get_input_focus_reply(dpy, xcb_get_input_focus(dpy), NULL);
310         if (ifo != NULL && (ifo->focus == XCB_INPUT_FOCUS_POINTER_ROOT || ifo->focus == XCB_NONE))
311                 clear_input_focus();
312         free(ifo);
313 }
314
315 void register_events(void)
316 {
317         uint32_t values[] = {ROOT_EVENT_MASK};
318         xcb_generic_error_t *e = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, root, XCB_CW_EVENT_MASK, values));
319         if (e != NULL) {
320                 xcb_disconnect(dpy);
321                 err("Another window manager is already running.\n");
322         }
323 }
324
325 void cleanup(void)
326 {
327         while (mon_head != NULL)
328                 remove_monitor(mon_head);
329         while (rule_head != NULL)
330                 remove_rule(rule_head);
331         while (stack_head != NULL)
332                 remove_stack(stack_head);
333         while (subscribe_head != NULL)
334                 remove_subscriber(subscribe_head);
335         while (pending_rule_head != NULL)
336                 remove_pending_rule(pending_rule_head);
337         empty_history();
338         free(frozen_pointer);
339 }
340
341 void put_status(void)
342 {
343         subscriber_list_t *sb = subscribe_head;
344         while (sb != NULL) {
345                 subscriber_list_t *next = sb->next;
346                 if (print_status(sb->stream) != 0)
347                         remove_subscriber(sb);
348                 sb = next;
349         }
350 }
351
352 void sig_handler(int sig)
353 {
354         if (sig == SIGCHLD) {
355                 signal(sig, sig_handler);
356                 while (waitpid(-1, 0, WNOHANG) > 0)
357                         ;
358         } else if (sig == SIGINT || sig == SIGHUP || sig == SIGTERM) {
359                 running = false;
360         }
361 }