]> git.lizzy.rs Git - bspwm.git/blob - bspwm.c
Add new contributor
[bspwm.git] / bspwm.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <signal.h>
6 #include <fcntl.h>
7 #include <sys/socket.h>
8 #include <sys/un.h>
9 #include <sys/select.h>
10 #include <sys/stat.h>
11 #include <sys/wait.h>
12 #include <xcb/xcb.h>
13 #include <xcb/xcb_event.h>
14 #include <xcb/xcb_ewmh.h>
15 #include <xcb/randr.h>
16 #include "types.h"
17 #include "settings.h"
18 #include "messages.h"
19 #include "rules.h"
20 #include "events.h"
21 #include "common.h"
22 #include "helpers.h"
23 #include "window.h"
24 #include "bspwm.h"
25 #include "tree.h"
26 #include "ewmh.h"
27
28 void quit(void)
29 {
30     running = false;
31 }
32
33 void cleanup(void)
34 {
35     while (mon_head != NULL)
36         remove_monitor(mon_head);
37     while (rule_head != NULL)
38         remove_rule(rule_head);
39     free(frozen_pointer);
40 }
41
42 void register_events(void)
43 {
44     uint32_t values[] = {ROOT_EVENT_MASK};
45     xcb_generic_error_t *e = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, root, XCB_CW_EVENT_MASK, values));
46     if (e != NULL) {
47         xcb_disconnect(dpy);
48         err("Another window manager is already running.\n");
49     }
50 }
51
52 bool import_monitors(void)
53 {
54     PUTS("import monitors");
55     xcb_randr_get_screen_resources_current_reply_t *sres = xcb_randr_get_screen_resources_current_reply(dpy, xcb_randr_get_screen_resources_current(dpy, root), NULL);
56     if (sres == NULL)
57         return false;
58
59     int len = xcb_randr_get_screen_resources_current_outputs_length(sres);
60     xcb_randr_output_t *outputs = xcb_randr_get_screen_resources_current_outputs(sres);
61
62     xcb_randr_get_output_info_cookie_t cookies[len];
63     for (int i = 0; i < len; i++)
64         cookies[i] = xcb_randr_get_output_info(dpy, outputs[i], XCB_CURRENT_TIME);
65
66     for (monitor_t *m = mon_head; m != NULL; m = m->next)
67         m->wired = false;
68
69     monitor_t *mm = NULL;
70     unsigned int num = 0;
71
72     for (int i = 0; i < len; i++) {
73         xcb_randr_get_output_info_reply_t *info = xcb_randr_get_output_info_reply(dpy, cookies[i], NULL);
74         if (info != NULL && info->crtc != XCB_NONE) {
75
76             xcb_randr_get_crtc_info_reply_t *cir = xcb_randr_get_crtc_info_reply(dpy, xcb_randr_get_crtc_info(dpy, info->crtc, XCB_CURRENT_TIME), NULL);
77             if (cir != NULL) {
78                 xcb_rectangle_t rect = (xcb_rectangle_t) {cir->x, cir->y, cir->width, cir->height};
79                 mm = get_monitor_by_id(outputs[i]);
80                 if (mm != NULL) {
81                     mm->rectangle = rect;
82                     for (desktop_t *d = mm->desk_head; d != NULL; d = d->next)
83                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
84                             fit_monitor(mm, n->client);
85                     arrange(mm, mm->desk);
86                     mm->wired = true;
87                     PRINTF("update monitor %s (0x%X)\n", mm->name, mm->id);
88                 } else {
89                     mm = add_monitor(&rect);
90                     char *name = (char *)xcb_randr_get_output_info_name(info);
91                     size_t name_len = MIN(sizeof(mm->name), (size_t)xcb_randr_get_output_info_name_length(info));
92                     strncpy(mm->name, name, name_len);
93                     mm->name[name_len] = '\0';
94                     mm->id = outputs[i];
95                     add_desktop(mm, make_desktop(NULL));
96                     PRINTF("add monitor %s (0x%X)\n", mm->name, mm->id);
97                 }
98                 num++;
99             }
100             free(cir);
101         }
102         free(info);
103     }
104
105     monitor_t *m = mon_head;
106     while (m != NULL) {
107         monitor_t *next = m->next;
108         if (!m->wired) {
109             PRINTF("remove monitor %s (0x%X)\n", m->name, m->id);
110             merge_monitors(m, mm);
111             remove_monitor(m);
112         }
113         m = next;
114     }
115
116     free(sres);
117     update_motion_recorder();
118     return (num_monitors > 0);
119 }
120
121 void init(void)
122 {
123     num_monitors = num_desktops = num_clients = 0;
124     monitor_uid = desktop_uid = client_uid = rule_uid = 0;
125     mon = last_mon = mon_head = mon_tail = NULL;
126     rule_head = rule_tail = NULL;
127     status_fifo = NULL;
128     randr_base = 0;
129     visible = true;
130     exit_status = 0;
131 }
132
133 void setup(void)
134 {
135     init();
136     ewmh_init();
137     screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
138     if (screen == NULL)
139         err("Can't acquire the default screen.\n");
140     root = screen->root;
141     register_events();
142
143     screen_width = screen->width_in_pixels;
144     screen_height = screen->height_in_pixels;
145     root_depth = screen->root_depth;
146
147     uint32_t mask = XCB_CW_EVENT_MASK;
148     uint32_t values[] = {XCB_EVENT_MASK_POINTER_MOTION};
149     motion_recorder = xcb_generate_id(dpy);
150     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);
151
152     xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED,
153                               ewmh->_NET_DESKTOP_NAMES,
154                               ewmh->_NET_NUMBER_OF_DESKTOPS,
155                               ewmh->_NET_CURRENT_DESKTOP,
156                               ewmh->_NET_CLIENT_LIST,
157                               ewmh->_NET_ACTIVE_WINDOW,
158                               ewmh->_NET_WM_DESKTOP,
159                               ewmh->_NET_WM_STATE,
160                               ewmh->_NET_WM_STATE_FULLSCREEN,
161                               ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
162                               ewmh->_NET_WM_WINDOW_TYPE,
163                               ewmh->_NET_WM_WINDOW_TYPE_DOCK,
164                               ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
165                               ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
166                               ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
167                               ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
168
169     xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
170
171     xcb_intern_atom_reply_t *iar = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen("_COMPTON_SHADOW"), "_COMPTON_SHADOW"), NULL);
172
173     if (iar != NULL) {
174         compton_shadow = iar->atom;
175         free(iar);
176     }
177
178     const xcb_query_extension_reply_t *qep = xcb_get_extension_data(dpy, &xcb_randr_id);
179     if (qep->present && import_monitors()) {
180         randr = true;
181         randr_base = qep->first_event;
182         xcb_randr_select_input(dpy, root, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
183     } else {
184         randr = false;
185         warn("Couldn't retrieve monitors via RandR.\n");
186         xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
187         monitor_t *m = add_monitor(&rect);
188         add_desktop(m, make_desktop(NULL));
189     }
190
191     ewmh_update_number_of_desktops();
192     ewmh_update_desktop_names();
193     ewmh_update_current_desktop();
194     frozen_pointer = make_pointer_state();
195 }
196
197 int main(int argc, char *argv[])
198 {
199     fd_set descriptors;
200     char socket_path[MAXLEN];
201     char *fifo_path = NULL;
202     status_prefix = NULL;
203     int sock_fd, ret_fd, dpy_fd, sel, n;
204     struct sockaddr_un sock_address;
205     size_t rsplen = 0;
206     char msg[BUFSIZ] = {0};
207     char rsp[BUFSIZ] = {0};
208     xcb_generic_event_t *event;
209     char opt;
210
211     while ((opt = getopt(argc, argv, "hvs:p:")) != -1) {
212         switch (opt) {
213             case 'h':
214                 printf("bspwm [-h|-v|-s PANEL_FIFO|-p PANEL_PREFIX]\n");
215                 exit(EXIT_SUCCESS);
216                 break;
217             case 'v':
218                 printf("%s\n", VERSION);
219                 exit(EXIT_SUCCESS);
220                 break;
221             case 's':
222                 fifo_path = optarg;
223                 break;
224             case 'p':
225                 status_prefix = optarg;
226                 break;
227         }
228     }
229
230     running = true;
231     dpy = xcb_connect(NULL, &default_screen);
232
233     if (xcb_connection_has_error(dpy))
234         err("Can't open the default display.\n");
235
236     setup();
237
238     dpy_fd = xcb_get_file_descriptor(dpy);
239
240     char *sp = getenv(SOCKET_ENV_VAR);
241     strncpy(socket_path, (sp == NULL ? DEFAULT_SOCKET_PATH : sp), sizeof(socket_path));
242
243     sock_address.sun_family = AF_UNIX;
244     strncpy(sock_address.sun_path, socket_path, sizeof(sock_address.sun_path));
245     unlink(socket_path);
246
247     sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
248
249     if (sock_fd == -1)
250         err("Couldn't create the socket.\n");
251
252     if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
253         err("Couldn't bind a name to the socket.\n");
254
255     if (listen(sock_fd, SOMAXCONN) == -1)
256         err("Couldn't listen to the socket.\n");
257
258     sel = MAX(sock_fd, dpy_fd) + 1;
259
260     if (fifo_path != NULL) {
261         int fifo_fd = open(fifo_path, O_RDWR | O_NONBLOCK);
262         if (fifo_fd != -1)
263             status_fifo = fdopen(fifo_fd, "w");
264         else
265             warn("Couldn't open status fifo.\n");
266     }
267
268     load_settings();
269     run_autostart();
270     ewmh_update_wm_name();
271
272     while (running) {
273
274         xcb_flush(dpy);
275
276         FD_ZERO(&descriptors);
277         FD_SET(sock_fd, &descriptors);
278         FD_SET(dpy_fd, &descriptors);
279
280         if (select(sel, &descriptors, NULL, NULL, NULL) > 0) {
281
282             if (FD_ISSET(sock_fd, &descriptors)) {
283                 ret_fd = accept(sock_fd, NULL, 0);
284                 if (ret_fd > 0 && (n = recv(ret_fd, msg, sizeof(msg), 0)) > 0) {
285                     msg[n] = '\0';
286                     process_message(msg, rsp);
287                     rsplen = strlen(rsp);
288                     if (rsp[rsplen - 1] == '\n')
289                         rsp[--rsplen] = '\0';
290                     send(ret_fd, rsp, rsplen, 0);
291                     close(ret_fd);
292                     rsp[0] = '\0';
293                 }
294             }
295
296             if (FD_ISSET(dpy_fd, &descriptors)) {
297                 while ((event = xcb_poll_for_event(dpy)) != NULL) {
298                     handle_event(event);
299                     free(event);
300                 }
301             }
302
303         }
304
305         if (xcb_connection_has_error(dpy))
306             err("The server has closed the connection.\n");
307     }
308
309     cleanup();
310     close(sock_fd);
311     if (status_fifo != NULL)
312         fclose(status_fifo);
313     xcb_ewmh_connection_wipe(ewmh);
314     xcb_destroy_window(dpy, motion_recorder);
315     free(ewmh);
316     xcb_flush(dpy);
317     xcb_disconnect(dpy);
318     return exit_status;
319 }