]> git.lizzy.rs Git - bspwm.git/blob - bspwm.c
Asynchronously parse rule command output
[bspwm.git] / bspwm.c
1 /* * Copyright (c) 2012-2013 Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  *  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *  * Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation and/or
11  * other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER 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 ON
20  * 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/socket.h>
33 #include <sys/un.h>
34 #include <signal.h>
35 #include <unistd.h>
36 #include "types.h"
37 #include "desktop.h"
38 #include "monitor.h"
39 #include "settings.h"
40 #include "messages.h"
41 #include "subscribe.h"
42 #include "events.h"
43 #include "common.h"
44 #include "window.h"
45 #include "history.h"
46 #include "stack.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     size_t rsp_len = 0;
59     char msg[BUFSIZ] = {0};
60     char rsp[BUFSIZ] = {0};
61     xcb_generic_event_t *event;
62     char opt;
63
64     while ((opt = getopt(argc, argv, "hvc:")) != -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     if (config_path[0] == '\0') {
81         char *config_home = getenv(CONFIG_HOME_ENV);
82         if (config_home != NULL)
83             snprintf(config_path, sizeof(config_path), "%s/%s/%s", config_home, WM_NAME, CONFIG_NAME);
84         else
85             snprintf(config_path, sizeof(config_path), "%s/%s/%s/%s", getenv("HOME"), ".config", WM_NAME, CONFIG_NAME);
86     }
87
88     dpy = xcb_connect(NULL, &default_screen);
89
90     if (xcb_connection_has_error(dpy))
91         err("Can't open the default display.\n");
92
93     setup();
94
95     dpy_fd = xcb_get_file_descriptor(dpy);
96
97     char *sp = getenv(SOCKET_ENV_VAR);
98     snprintf(socket_path, sizeof(socket_path), "%s", (sp == NULL ? DEFAULT_SOCKET_PATH : sp));
99
100     sock_address.sun_family = AF_UNIX;
101     snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), "%s", socket_path);
102     unlink(socket_path);
103
104     sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
105
106     if (sock_fd == -1)
107         err("Couldn't create the socket.\n");
108
109     if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
110         err("Couldn't bind a name to the socket.\n");
111
112     if (listen(sock_fd, SOMAXCONN) == -1)
113         err("Couldn't listen to the socket.\n");
114
115     signal(SIGPIPE, SIG_IGN);
116     load_settings();
117     run_config();
118     running = true;
119
120     while (running) {
121
122         xcb_flush(dpy);
123
124         FD_ZERO(&descriptors);
125         FD_SET(sock_fd, &descriptors);
126         FD_SET(dpy_fd, &descriptors);
127         max_fd = MAX(sock_fd, dpy_fd);
128         for (pending_rule_t *pr = pending_rule_head; pr != NULL; pr = pr->next) {
129             FD_SET(pr->fd, &descriptors);
130             if (pr->fd > max_fd)
131                 max_fd = pr->fd;
132         }
133
134         if (select(max_fd + 1, &descriptors, NULL, NULL, NULL) > 0) {
135
136             pending_rule_t *pr = pending_rule_head;
137             while (pr != NULL) {
138                 pending_rule_t *next = pr->next;
139                 if (FD_ISSET(pr->fd, &descriptors)) {
140                     manage_window(pr->win, pr->csq, pr->fd);
141                     remove_pending_rule(pr);
142                 }
143                 pr = next;
144             }
145
146             if (FD_ISSET(sock_fd, &descriptors)) {
147                 cli_fd = accept(sock_fd, NULL, 0);
148                 if (cli_fd > 0 && (n = recv(cli_fd, msg, sizeof(msg), 0)) > 0) {
149                     msg[n] = '\0';
150                     if (handle_message(msg, n, rsp)) {
151                         rsp_len = strlen(rsp);
152                     } else {
153                         rsp[0] = MESSAGE_FAILURE;
154                         rsp_len = 1;
155                     }
156                     if (rsp_len == 1 && rsp[0] == MESSAGE_SUBSCRIBE) {
157                         add_subscriber(cli_fd);
158                     } else {
159                         send(cli_fd, rsp, rsp_len, 0);
160                         close(cli_fd);
161                     }
162                     rsp[0] = '\0';
163                 }
164             }
165
166             if (FD_ISSET(dpy_fd, &descriptors)) {
167                 while ((event = xcb_poll_for_event(dpy)) != NULL) {
168                     handle_event(event);
169                     free(event);
170                 }
171             }
172         }
173
174         if (xcb_connection_has_error(dpy))
175             err("The server has closed the connection.\n");
176     }
177
178     cleanup();
179     close(sock_fd);
180     xcb_ewmh_connection_wipe(ewmh);
181     xcb_destroy_window(dpy, motion_recorder);
182     free(ewmh);
183     xcb_flush(dpy);
184     xcb_disconnect(dpy);
185     return exit_status;
186 }
187
188 void init(void)
189 {
190     num_monitors = num_desktops = num_clients = 0;
191     monitor_uid = desktop_uid = 0;
192     mon = mon_head = mon_tail = pri_mon = NULL;
193     history_head = history_tail = history_needle = NULL;
194     stack_head = stack_tail = NULL;
195     subscribe_head = subscribe_tail = NULL;
196     pending_rule_head = pending_rule_tail = NULL;
197     last_motion_time = last_motion_x = last_motion_y = 0;
198     visible = auto_raise = sticky_still = record_history = true;
199     randr_base = 0;
200     exit_status = 0;
201 }
202
203 void setup(void)
204 {
205     init();
206     ewmh_init();
207     screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
208     if (screen == NULL)
209         err("Can't acquire the default screen.\n");
210     root = screen->root;
211     register_events();
212
213     screen_width = screen->width_in_pixels;
214     screen_height = screen->height_in_pixels;
215     root_depth = screen->root_depth;
216
217     uint32_t mask = XCB_CW_EVENT_MASK;
218     uint32_t values[] = {XCB_EVENT_MASK_POINTER_MOTION};
219     motion_recorder = xcb_generate_id(dpy);
220     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);
221
222     xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED,
223                               ewmh->_NET_SUPPORTING_WM_CHECK,
224                               ewmh->_NET_DESKTOP_NAMES,
225                               ewmh->_NET_NUMBER_OF_DESKTOPS,
226                               ewmh->_NET_CURRENT_DESKTOP,
227                               ewmh->_NET_CLIENT_LIST,
228                               ewmh->_NET_ACTIVE_WINDOW,
229                               ewmh->_NET_CLOSE_WINDOW,
230                               ewmh->_NET_WM_DESKTOP,
231                               ewmh->_NET_WM_STATE,
232                               ewmh->_NET_WM_STATE_FULLSCREEN,
233                               ewmh->_NET_WM_STATE_STICKY,
234                               ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
235                               ewmh->_NET_WM_WINDOW_TYPE,
236                               ewmh->_NET_WM_WINDOW_TYPE_DOCK,
237                               ewmh->_NET_WM_WINDOW_TYPE_DESKTOP,
238                               ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
239                               ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
240                               ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
241                               ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
242
243     xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
244     ewmh_set_supporting(motion_recorder);
245
246 #define GETATOM(a) \
247     get_atom(#a, &a);
248     GETATOM(WM_DELETE_WINDOW)
249     GETATOM(WM_TAKE_FOCUS)
250     GETATOM(_BSPWM_FLOATING_WINDOW)
251     GETATOM(_NET_WM_WINDOW_OPACITY)
252 #undef GETATOM
253
254     const xcb_query_extension_reply_t *qep = xcb_get_extension_data(dpy, &xcb_randr_id);
255     if (qep->present && import_monitors()) {
256         randr = true;
257         randr_base = qep->first_event;
258         xcb_randr_select_input(dpy, root, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
259     } else {
260         randr = false;
261         warn("Couldn't retrieve monitors via RandR.\n");
262         xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
263         monitor_t *m = add_monitor(rect);
264         add_desktop(m, make_desktop(NULL));
265     }
266
267     ewmh_update_number_of_desktops();
268     ewmh_update_desktop_names();
269     ewmh_update_current_desktop();
270     frozen_pointer = make_pointer_state();
271     xcb_get_input_focus_reply_t *ifo = xcb_get_input_focus_reply(dpy, xcb_get_input_focus(dpy), NULL);
272     if (ifo != NULL && (ifo->focus == XCB_INPUT_FOCUS_POINTER_ROOT || ifo->focus == XCB_NONE))
273         clear_input_focus();
274     free(ifo);
275 }
276
277 void register_events(void)
278 {
279     uint32_t values[] = {ROOT_EVENT_MASK};
280     xcb_generic_error_t *e = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, root, XCB_CW_EVENT_MASK, values));
281     if (e != NULL) {
282         xcb_disconnect(dpy);
283         err("Another window manager is already running.\n");
284     }
285 }
286
287 void quit(void)
288 {
289     running = false;
290 }
291
292 void cleanup(void)
293 {
294     while (mon_head != NULL)
295         remove_monitor(mon_head);
296     while (stack_head != NULL)
297         remove_stack(stack_head);
298     while (subscribe_head != NULL)
299         remove_subscriber(subscribe_head);
300     while (pending_rule_head != NULL)
301         remove_pending_rule(pending_rule_head);
302     empty_history();
303     free(frozen_pointer);
304 }
305
306 void put_status(void)
307 {
308     subscriber_list_t *sb = subscribe_head;
309     while (sb != NULL) {
310         subscriber_list_t *next = sb->next;
311         feed_subscriber(sb);
312         sb = next;
313     }
314 }