]> git.lizzy.rs Git - bspwm.git/blob - bspwm.c
Handle the primary monitor
[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 <xcb/xcb.h>
11 #include <xcb/xcb_event.h>
12 #include <xcb/xcb_ewmh.h>
13 #include <xcb/randr.h>
14 #include "types.h"
15 #include "settings.h"
16 #include "messages.h"
17 #include "rules.h"
18 #include "events.h"
19 #include "common.h"
20 #include "helpers.h"
21 #include "window.h"
22 #include "bspwm.h"
23 #include "tree.h"
24 #include "ewmh.h"
25
26 int main(int argc, char *argv[])
27 {
28     fd_set descriptors;
29     char socket_path[MAXLEN];
30     char *fifo_path = NULL;
31     config_path[0] = '\0';
32     status_prefix = NULL;
33     int sock_fd, ret_fd, dpy_fd, sel, n;
34     struct sockaddr_un sock_address;
35     size_t rsp_len = 0;
36     char msg[BUFSIZ] = {0};
37     char rsp[BUFSIZ] = {0};
38     xcb_generic_event_t *event;
39     char opt;
40
41     while ((opt = getopt(argc, argv, "hvc:s:p:")) != -1) {
42         switch (opt) {
43             case 'h':
44                 printf(WM_NAME " [-h|-v|-c CONFIG_PATH|-s PANEL_FIFO|-p PANEL_PREFIX]\n");
45                 exit(EXIT_SUCCESS);
46                 break;
47             case 'v':
48                 printf("%s\n", VERSION);
49                 exit(EXIT_SUCCESS);
50                 break;
51             case 'c':
52                 strncpy(config_path, optarg, sizeof(config_path));
53                 break;
54             case 's':
55                 fifo_path = optarg;
56                 break;
57             case 'p':
58                 status_prefix = optarg;
59                 break;
60         }
61     }
62
63     if (config_path[0] == '\0') {
64         char *config_home = getenv(CONFIG_HOME_ENV);
65         if (config_home != NULL)
66             snprintf(config_path, sizeof(config_path), "%s/%s/%s", config_home, WM_NAME, CONFIG_NAME);
67         else
68             snprintf(config_path, sizeof(config_path), "%s/%s/%s/%s", getenv("HOME"), ".config", WM_NAME, CONFIG_NAME);
69     }
70
71     dpy = xcb_connect(NULL, &default_screen);
72
73     if (xcb_connection_has_error(dpy))
74         err("Can't open the default display.\n");
75
76     setup();
77
78     dpy_fd = xcb_get_file_descriptor(dpy);
79
80     char *sp = getenv(SOCKET_ENV_VAR);
81     strncpy(socket_path, (sp == NULL ? DEFAULT_SOCKET_PATH : sp), sizeof(socket_path));
82
83     sock_address.sun_family = AF_UNIX;
84     strncpy(sock_address.sun_path, socket_path, sizeof(sock_address.sun_path));
85     unlink(socket_path);
86
87     sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
88
89     if (sock_fd == -1)
90         err("Couldn't create the socket.\n");
91
92     if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
93         err("Couldn't bind a name to the socket.\n");
94
95     if (listen(sock_fd, SOMAXCONN) == -1)
96         err("Couldn't listen to the socket.\n");
97
98     sel = MAX(sock_fd, dpy_fd) + 1;
99
100     if (fifo_path != NULL) {
101         int fifo_fd = open(fifo_path, O_RDWR | O_NONBLOCK);
102         if (fifo_fd != -1)
103             status_fifo = fdopen(fifo_fd, "w");
104         else
105             warn("Couldn't open status fifo.\n");
106     }
107
108     load_settings();
109     run_config();
110     running = true;
111
112     while (running) {
113
114         xcb_flush(dpy);
115
116         FD_ZERO(&descriptors);
117         FD_SET(sock_fd, &descriptors);
118         FD_SET(dpy_fd, &descriptors);
119
120         if (select(sel, &descriptors, NULL, NULL, NULL) > 0) {
121
122             if (FD_ISSET(sock_fd, &descriptors)) {
123                 ret_fd = accept(sock_fd, NULL, 0);
124                 if (ret_fd > 0 && (n = recv(ret_fd, msg, sizeof(msg), 0)) > 0) {
125                     msg[n] = '\0';
126                     if (handle_message(msg, n, rsp)) {
127                         rsp_len = strlen(rsp);
128                         if (rsp[rsp_len - 1] == '\n')
129                             rsp[--rsp_len] = '\0';
130                     } else {
131                         rsp[0] = MESSAGE_FAILURE;
132                         rsp_len = 1;
133                     }
134                     send(ret_fd, rsp, rsp_len, 0);
135                     close(ret_fd);
136                     rsp[0] = '\0';
137                 }
138             }
139
140             if (FD_ISSET(dpy_fd, &descriptors)) {
141                 while ((event = xcb_poll_for_event(dpy)) != NULL) {
142                     handle_event(event);
143                     free(event);
144                 }
145             }
146
147         }
148
149         if (xcb_connection_has_error(dpy))
150             err("The server has closed the connection.\n");
151     }
152
153     cleanup();
154     close(sock_fd);
155     if (status_fifo != NULL)
156         fclose(status_fifo);
157     xcb_ewmh_connection_wipe(ewmh);
158     xcb_destroy_window(dpy, motion_recorder);
159     free(ewmh);
160     xcb_flush(dpy);
161     xcb_disconnect(dpy);
162     return exit_status;
163 }
164
165 void init(void)
166 {
167     num_monitors = num_desktops = num_clients = 0;
168     monitor_uid = desktop_uid = rule_uid = 0;
169     mon = last_mon = mon_head = mon_tail = pri_mon = NULL;
170     rule_head = rule_tail = NULL;
171     status_fifo = NULL;
172     last_motion_time = last_motion_x = last_motion_y = 0;
173     randr_base = 0;
174     visible = auto_raise = true;
175     exit_status = 0;
176 }
177
178 void setup(void)
179 {
180     init();
181     ewmh_init();
182     screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
183     if (screen == NULL)
184         err("Can't acquire the default screen.\n");
185     root = screen->root;
186     register_events();
187
188     screen_width = screen->width_in_pixels;
189     screen_height = screen->height_in_pixels;
190     root_depth = screen->root_depth;
191
192     uint32_t mask = XCB_CW_EVENT_MASK;
193     uint32_t values[] = {XCB_EVENT_MASK_POINTER_MOTION};
194     motion_recorder = xcb_generate_id(dpy);
195     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);
196
197     xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED,
198                               ewmh->_NET_SUPPORTING_WM_CHECK,
199                               ewmh->_NET_DESKTOP_NAMES,
200                               ewmh->_NET_NUMBER_OF_DESKTOPS,
201                               ewmh->_NET_CURRENT_DESKTOP,
202                               ewmh->_NET_CLIENT_LIST,
203                               ewmh->_NET_ACTIVE_WINDOW,
204                               ewmh->_NET_WM_DESKTOP,
205                               ewmh->_NET_WM_STATE,
206                               ewmh->_NET_WM_STATE_FULLSCREEN,
207                               ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
208                               ewmh->_NET_WM_WINDOW_TYPE,
209                               ewmh->_NET_WM_WINDOW_TYPE_DOCK,
210                               ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
211                               ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
212                               ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
213                               ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
214
215     xcb_ewmh_set_wm_name(ewmh, root, strlen(WM_NAME), WM_NAME);
216     xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
217     ewmh_set_supporting(motion_recorder);
218
219 #define GETATOM(a) \
220     get_atom(#a, &a);
221     GETATOM(WM_DELETE_WINDOW)
222     GETATOM(WM_TAKE_FOCUS)
223     GETATOM(_BSPWM_FLOATING_WINDOW)
224 #undef GETATOM
225
226     const xcb_query_extension_reply_t *qep = xcb_get_extension_data(dpy, &xcb_randr_id);
227     if (qep->present && import_monitors()) {
228         randr = true;
229         randr_base = qep->first_event;
230         xcb_randr_select_input(dpy, root, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
231     } else {
232         randr = false;
233         warn("Couldn't retrieve monitors via RandR.\n");
234         xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
235         monitor_t *m = add_monitor(&rect);
236         add_desktop(m, make_desktop(NULL));
237     }
238
239     ewmh_update_number_of_desktops();
240     ewmh_update_desktop_names();
241     ewmh_update_current_desktop();
242     frozen_pointer = make_pointer_state();
243     xcb_get_input_focus_reply_t *ifo = xcb_get_input_focus_reply(dpy, xcb_get_input_focus(dpy), NULL);
244     if (ifo != NULL && (ifo->focus == XCB_INPUT_FOCUS_POINTER_ROOT || ifo->focus == XCB_NONE))
245         clear_input_focus();
246     free(ifo);
247 }
248
249 void register_events(void)
250 {
251     uint32_t values[] = {ROOT_EVENT_MASK};
252     xcb_generic_error_t *e = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, root, XCB_CW_EVENT_MASK, values));
253     if (e != NULL) {
254         xcb_disconnect(dpy);
255         err("Another window manager is already running.\n");
256     }
257 }
258
259 bool import_monitors(void)
260 {
261     PUTS("import monitors");
262     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);
263     if (sres == NULL)
264         return false;
265
266     int len = xcb_randr_get_screen_resources_current_outputs_length(sres);
267     xcb_randr_output_t *outputs = xcb_randr_get_screen_resources_current_outputs(sres);
268
269     xcb_randr_get_output_info_cookie_t cookies[len];
270     for (int i = 0; i < len; i++)
271         cookies[i] = xcb_randr_get_output_info(dpy, outputs[i], XCB_CURRENT_TIME);
272
273     for (monitor_t *m = mon_head; m != NULL; m = m->next)
274         m->wired = false;
275
276     monitor_t *mm = NULL;
277     unsigned int num = 0;
278
279     for (int i = 0; i < len; i++) {
280         xcb_randr_get_output_info_reply_t *info = xcb_randr_get_output_info_reply(dpy, cookies[i], NULL);
281         if (info != NULL && info->crtc != XCB_NONE) {
282
283             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);
284             if (cir != NULL) {
285                 xcb_rectangle_t rect = (xcb_rectangle_t) {cir->x, cir->y, cir->width, cir->height};
286                 mm = get_monitor_by_id(outputs[i]);
287                 if (mm != NULL) {
288                     mm->rectangle = rect;
289                     for (desktop_t *d = mm->desk_head; d != NULL; d = d->next)
290                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
291                             fit_monitor(mm, n->client);
292                     arrange(mm, mm->desk);
293                     mm->wired = true;
294                     PRINTF("update monitor %s (0x%X)\n", mm->name, mm->id);
295                 } else {
296                     mm = add_monitor(&rect);
297                     char *name = (char *)xcb_randr_get_output_info_name(info);
298                     size_t name_len = MIN(sizeof(mm->name), (size_t)xcb_randr_get_output_info_name_length(info));
299                     strncpy(mm->name, name, name_len);
300                     mm->name[name_len] = '\0';
301                     mm->id = outputs[i];
302                     PRINTF("add monitor %s (0x%X)\n", mm->name, mm->id);
303                 }
304                 num++;
305             }
306             free(cir);
307         }
308         free(info);
309     }
310
311     /* initially focus the primary monitor and add the first desktop to it */
312     xcb_randr_get_output_primary_reply_t *gpo = xcb_randr_get_output_primary_reply(dpy, xcb_randr_get_output_primary(dpy, root), NULL);
313     if (gpo != NULL) {
314         pri_mon = get_monitor_by_id(gpo->output);
315         if (!running && pri_mon != NULL) {
316             if (mon != pri_mon)
317                 mon = pri_mon;
318             add_desktop(pri_mon, make_desktop(NULL));
319             ewmh_update_current_desktop();
320         }
321     }
322     free(gpo);
323
324     /* add one desktop to each new monitor */
325     for (monitor_t *m = mon_head; m != NULL; m = m->next)
326         if (m->desk == NULL && (running || pri_mon == NULL || m != pri_mon))
327             add_desktop(m, make_desktop(NULL));
328
329     /* merge and remove disconnected monitors */
330     monitor_t *m = mon_head;
331     while (m != NULL) {
332         monitor_t *next = m->next;
333         if (!m->wired) {
334             PRINTF("remove monitor %s (0x%X)\n", m->name, m->id);
335             merge_monitors(m, mm);
336             remove_monitor(m);
337         }
338         m = next;
339     }
340
341     free(sres);
342     update_motion_recorder();
343     return (num_monitors > 0);
344 }
345
346 void quit(void)
347 {
348     running = false;
349 }
350
351 void cleanup(void)
352 {
353     while (mon_head != NULL)
354         remove_monitor(mon_head);
355     while (rule_head != NULL)
356         remove_rule(rule_head);
357     free(frozen_pointer);
358 }
359
360 void put_status(void)
361 {
362     if (status_fifo == NULL)
363         return;
364     if (status_prefix != NULL)
365         fprintf(status_fifo, "%s", status_prefix);
366     bool urgent = false;
367     for (monitor_t *m = mon_head; m != NULL; m = m->next) {
368         fprintf(status_fifo, "%c%s:", (mon == m ? 'M' : 'm'), m->name);
369         for (desktop_t *d = m->desk_head; d != NULL; d = d->next, urgent = false) {
370             for (node_t *n = first_extrema(d->root); n != NULL && !urgent; n = next_leaf(n, d->root))
371                 urgent |= n->client->urgent;
372             fprintf(status_fifo, "%c%s:", m->desk == d ? (urgent ? 'U' : 'D') : (d->root == NULL ? 'E' : (urgent ? 'u' : 'd')), d->name);
373         }
374     }
375     if (mon != NULL && mon->desk != NULL)
376         fprintf(status_fifo, "L%s", (mon->desk->layout == LAYOUT_TILED ? "tiled" : "monocle"));
377     fprintf(status_fifo, "\n");
378     fflush(status_fifo);
379 }