]> git.lizzy.rs Git - bspwm.git/blob - bspwm.c
f485ff5302997a989ead12ac33e3b6761fb1c9e9
[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 <ctype.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/select.h>
31 #ifdef __OpenBSD__
32 #include <sys/types.h>
33 #endif
34 #include <sys/socket.h>
35 #include <sys/un.h>
36 #include <unistd.h>
37 #include "types.h"
38 #include "desktop.h"
39 #include "monitor.h"
40 #include "settings.h"
41 #include "messages.h"
42 #include "events.h"
43 #include "common.h"
44 #include "tree.h"
45 #include "window.h"
46 #include "history.h"
47 #include "stack.h"
48 #include "tag.h"
49 #include "rule.h"
50 #include "ewmh.h"
51 #include "bspwm.h"
52
53 int main(int argc, char *argv[])
54 {
55     fd_set descriptors;
56     char socket_path[MAXLEN];
57     char *fifo_path = NULL;
58     config_path[0] = '\0';
59     status_prefix = NULL;
60     int sock_fd, ret_fd, dpy_fd, sel, n;
61     struct sockaddr_un sock_address;
62     size_t rsp_len = 0;
63     char msg[BUFSIZ] = {0};
64     char rsp[BUFSIZ] = {0};
65     xcb_generic_event_t *event;
66     char opt;
67
68     while ((opt = getopt(argc, argv, "hvc:s:p:")) != -1) {
69         switch (opt) {
70             case 'h':
71                 printf(WM_NAME " [-h|-v|-c CONFIG_PATH|-s PANEL_FIFO|-p PANEL_PREFIX]\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             case 's':
82                 fifo_path = optarg;
83                 break;
84             case 'p':
85                 status_prefix = optarg;
86                 break;
87         }
88     }
89
90     if (config_path[0] == '\0') {
91         char *config_home = getenv(CONFIG_HOME_ENV);
92         if (config_home != NULL)
93             snprintf(config_path, sizeof(config_path), "%s/%s/%s", config_home, WM_NAME, CONFIG_NAME);
94         else
95             snprintf(config_path, sizeof(config_path), "%s/%s/%s/%s", getenv("HOME"), ".config", WM_NAME, CONFIG_NAME);
96     }
97
98     dpy = xcb_connect(NULL, &default_screen);
99
100     if (xcb_connection_has_error(dpy))
101         err("Can't open the default display.\n");
102
103     setup();
104
105     dpy_fd = xcb_get_file_descriptor(dpy);
106
107     char *sp = getenv(SOCKET_ENV_VAR);
108     snprintf(socket_path, sizeof(socket_path), "%s", (sp == NULL ? DEFAULT_SOCKET_PATH : sp));
109
110     sock_address.sun_family = AF_UNIX;
111     snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), "%s", socket_path);
112     unlink(socket_path);
113
114     sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
115
116     if (sock_fd == -1)
117         err("Couldn't create the socket.\n");
118
119     if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
120         err("Couldn't bind a name to the socket.\n");
121
122     if (listen(sock_fd, SOMAXCONN) == -1)
123         err("Couldn't listen to the socket.\n");
124
125     sel = MAX(sock_fd, dpy_fd) + 1;
126
127     if (fifo_path != NULL) {
128         int fifo_fd = open(fifo_path, O_RDWR | O_NONBLOCK);
129         if (fifo_fd != -1)
130             status_fifo = fdopen(fifo_fd, "w");
131         else
132             warn("Couldn't open status fifo.\n");
133     }
134
135     load_settings();
136     run_config();
137     running = true;
138
139     while (running) {
140
141         xcb_flush(dpy);
142
143         FD_ZERO(&descriptors);
144         FD_SET(sock_fd, &descriptors);
145         FD_SET(dpy_fd, &descriptors);
146
147         if (select(sel, &descriptors, NULL, NULL, NULL) > 0) {
148
149             if (FD_ISSET(sock_fd, &descriptors)) {
150                 ret_fd = accept(sock_fd, NULL, 0);
151                 if (ret_fd > 0 && (n = recv(ret_fd, msg, sizeof(msg), 0)) > 0) {
152                     msg[n] = '\0';
153                     if (handle_message(msg, n, rsp)) {
154                         rsp_len = strlen(rsp);
155                         if (rsp[rsp_len - 1] == '\n')
156                             rsp[--rsp_len] = '\0';
157                     } else {
158                         rsp[0] = MESSAGE_FAILURE;
159                         rsp_len = 1;
160                     }
161                     send(ret_fd, rsp, rsp_len, 0);
162                     close(ret_fd);
163                     rsp[0] = '\0';
164                 }
165             }
166
167             if (FD_ISSET(dpy_fd, &descriptors)) {
168                 while ((event = xcb_poll_for_event(dpy)) != NULL) {
169                     handle_event(event);
170                     free(event);
171                 }
172             }
173
174         }
175
176         if (xcb_connection_has_error(dpy))
177             err("The server has closed the connection.\n");
178     }
179
180     cleanup();
181     close(sock_fd);
182     if (status_fifo != NULL)
183         fclose(status_fifo);
184     xcb_ewmh_connection_wipe(ewmh);
185     xcb_destroy_window(dpy, motion_recorder);
186     free(ewmh);
187     xcb_flush(dpy);
188     xcb_disconnect(dpy);
189     return exit_status;
190 }
191
192 void init(void)
193 {
194     num_monitors = num_desktops = num_clients = 0;
195     monitor_uid = desktop_uid = 0;
196     mon = mon_head = mon_tail = pri_mon = NULL;
197     rule_head = rule_tail = NULL;
198     history_head = history_tail = history_needle = NULL;
199     stack_head = stack_tail = NULL;
200     init_tags();
201     status_fifo = NULL;
202     last_motion_time = last_motion_x = last_motion_y = 0;
203     visible = auto_raise = sticky_still = record_history = true;
204     randr_base = 0;
205     exit_status = 0;
206 }
207
208 void setup(void)
209 {
210     init();
211     ewmh_init();
212     screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
213     if (screen == NULL)
214         err("Can't acquire the default screen.\n");
215     root = screen->root;
216     register_events();
217
218     screen_width = screen->width_in_pixels;
219     screen_height = screen->height_in_pixels;
220     root_depth = screen->root_depth;
221
222     uint32_t mask = XCB_CW_EVENT_MASK;
223     uint32_t values[] = {XCB_EVENT_MASK_POINTER_MOTION};
224     motion_recorder = xcb_generate_id(dpy);
225     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);
226
227     xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED,
228                               ewmh->_NET_SUPPORTING_WM_CHECK,
229                               ewmh->_NET_DESKTOP_NAMES,
230                               ewmh->_NET_NUMBER_OF_DESKTOPS,
231                               ewmh->_NET_CURRENT_DESKTOP,
232                               ewmh->_NET_CLIENT_LIST,
233                               ewmh->_NET_ACTIVE_WINDOW,
234                               ewmh->_NET_WM_DESKTOP,
235                               ewmh->_NET_WM_STATE,
236                               ewmh->_NET_WM_STATE_FULLSCREEN,
237                               ewmh->_NET_WM_STATE_STICKY,
238                               ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
239                               ewmh->_NET_WM_WINDOW_TYPE,
240                               ewmh->_NET_WM_WINDOW_TYPE_DOCK,
241                               ewmh->_NET_WM_WINDOW_TYPE_DESKTOP,
242                               ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
243                               ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
244                               ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
245                               ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
246
247     xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
248     ewmh_set_supporting(motion_recorder);
249
250 #define GETATOM(a) \
251     get_atom(#a, &a);
252     GETATOM(WM_DELETE_WINDOW)
253     GETATOM(WM_TAKE_FOCUS)
254     GETATOM(_BSPWM_FLOATING_WINDOW)
255 #undef GETATOM
256
257     const xcb_query_extension_reply_t *qep = xcb_get_extension_data(dpy, &xcb_randr_id);
258     if (qep->present && import_monitors()) {
259         randr = true;
260         randr_base = qep->first_event;
261         xcb_randr_select_input(dpy, root, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
262     } else {
263         randr = false;
264         warn("Couldn't retrieve monitors via RandR.\n");
265         xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
266         monitor_t *m = add_monitor(rect);
267         add_desktop(m, make_desktop(NULL));
268     }
269
270     ewmh_update_number_of_desktops();
271     ewmh_update_desktop_names();
272     ewmh_update_current_desktop();
273     frozen_pointer = make_pointer_state();
274     xcb_get_input_focus_reply_t *ifo = xcb_get_input_focus_reply(dpy, xcb_get_input_focus(dpy), NULL);
275     if (ifo != NULL && (ifo->focus == XCB_INPUT_FOCUS_POINTER_ROOT || ifo->focus == XCB_NONE))
276         clear_input_focus();
277     free(ifo);
278 }
279
280 void register_events(void)
281 {
282     uint32_t values[] = {ROOT_EVENT_MASK};
283     xcb_generic_error_t *e = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, root, XCB_CW_EVENT_MASK, values));
284     if (e != NULL) {
285         xcb_disconnect(dpy);
286         err("Another window manager is already running.\n");
287     }
288 }
289
290 void quit(void)
291 {
292     running = false;
293 }
294
295 void cleanup(void)
296 {
297     while (mon_head != NULL)
298         remove_monitor(mon_head);
299     while (rule_head != NULL)
300         remove_rule(rule_head);
301     while (stack_head != NULL)
302         remove_stack(stack_head);
303     while (num_tags > 0)
304         remove_tag_by_index(num_tags - 1);
305     empty_history();
306     free(frozen_pointer);
307 }
308
309 void put_status(void)
310 {
311     if (status_fifo == NULL)
312         return;
313     if (status_prefix != NULL)
314         fprintf(status_fifo, "%s", status_prefix);
315     bool urgent = false;
316     for (monitor_t *m = mon_head; m != NULL; m = m->next) {
317         fprintf(status_fifo, "%c%s:", (mon == m ? 'M' : 'm'), m->name);
318         for (desktop_t *d = m->desk_head; d != NULL; d = d->next, urgent = false) {
319             for (node_t *n = first_extrema(d->root); n != NULL && !urgent; n = next_leaf(n, d->root))
320                 urgent |= n->client->urgent;
321             char c = (urgent ? 'u' : (d->root == NULL ? 'f' : 'o'));
322             if (m->desk == d)
323                 c = toupper(c);
324             fprintf(status_fifo, "%c%s:", c, d->name);
325         }
326     }
327     if (mon != NULL && mon->desk != NULL) {
328         for (int i = 0; i < num_tags; i++)
329             fprintf(status_fifo, "%c%s:", (tags[i]->mask & mon->desk->tags_field) != 0 ? 'T' : 't', tags[i]->name);
330         fprintf(status_fifo, "L%s", (mon->desk->layout == LAYOUT_TILED ? "tiled" : "monocle"));
331     }
332     fprintf(status_fifo, "\n");
333     fflush(status_fifo);
334 }