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