]> git.lizzy.rs Git - bspwm.git/blob - bspwm.c
Keep the real wm name on the supporting window
[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("bspwm [-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     running = true;
72     dpy = xcb_connect(NULL, &default_screen);
73
74     if (xcb_connection_has_error(dpy))
75         err("Can't open the default display.\n");
76
77     setup();
78
79     dpy_fd = xcb_get_file_descriptor(dpy);
80
81     char *sp = getenv(SOCKET_ENV_VAR);
82     strncpy(socket_path, (sp == NULL ? DEFAULT_SOCKET_PATH : sp), sizeof(socket_path));
83
84     sock_address.sun_family = AF_UNIX;
85     strncpy(sock_address.sun_path, socket_path, sizeof(sock_address.sun_path));
86     unlink(socket_path);
87
88     sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
89
90     if (sock_fd == -1)
91         err("Couldn't create the socket.\n");
92
93     if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
94         err("Couldn't bind a name to the socket.\n");
95
96     if (listen(sock_fd, SOMAXCONN) == -1)
97         err("Couldn't listen to the socket.\n");
98
99     sel = MAX(sock_fd, dpy_fd) + 1;
100
101     if (fifo_path != NULL) {
102         int fifo_fd = open(fifo_path, O_RDWR | O_NONBLOCK);
103         if (fifo_fd != -1)
104             status_fifo = fdopen(fifo_fd, "w");
105         else
106             warn("Couldn't open status fifo.\n");
107     }
108
109     load_settings();
110     run_config();
111     ewmh_update_wm_name();
112
113     while (running) {
114
115         xcb_flush(dpy);
116
117         FD_ZERO(&descriptors);
118         FD_SET(sock_fd, &descriptors);
119         FD_SET(dpy_fd, &descriptors);
120
121         if (select(sel, &descriptors, NULL, NULL, NULL) > 0) {
122
123             if (FD_ISSET(sock_fd, &descriptors)) {
124                 ret_fd = accept(sock_fd, NULL, 0);
125                 if (ret_fd > 0 && (n = recv(ret_fd, msg, sizeof(msg), 0)) > 0) {
126                     msg[n] = '\0';
127                     if (handle_message(msg, n, rsp)) {
128                         rsp_len = strlen(rsp);
129                         if (rsp[rsp_len - 1] == '\n')
130                             rsp[--rsp_len] = '\0';
131                     } else {
132                         rsp[0] = MESSAGE_FAILURE;
133                         rsp_len = 1;
134                     }
135                     send(ret_fd, rsp, rsp_len, 0);
136                     close(ret_fd);
137                     rsp[0] = '\0';
138                 }
139             }
140
141             if (FD_ISSET(dpy_fd, &descriptors)) {
142                 while ((event = xcb_poll_for_event(dpy)) != NULL) {
143                     handle_event(event);
144                     free(event);
145                 }
146             }
147
148         }
149
150         if (xcb_connection_has_error(dpy))
151             err("The server has closed the connection.\n");
152     }
153
154     cleanup();
155     close(sock_fd);
156     if (status_fifo != NULL)
157         fclose(status_fifo);
158     xcb_ewmh_connection_wipe(ewmh);
159     xcb_destroy_window(dpy, motion_recorder);
160     free(ewmh);
161     xcb_flush(dpy);
162     xcb_disconnect(dpy);
163     return exit_status;
164 }
165
166 void init(void)
167 {
168     num_monitors = num_desktops = num_clients = 0;
169     monitor_uid = desktop_uid = rule_uid = 0;
170     mon = last_mon = mon_head = mon_tail = NULL;
171     rule_head = rule_tail = NULL;
172     status_fifo = NULL;
173     last_motion_time = last_motion_x = last_motion_y = 0;
174     randr_base = 0;
175     visible = auto_raise = true;
176     exit_status = 0;
177 }
178
179 void setup(void)
180 {
181     init();
182     ewmh_init();
183     screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
184     if (screen == NULL)
185         err("Can't acquire the default screen.\n");
186     root = screen->root;
187     register_events();
188
189     screen_width = screen->width_in_pixels;
190     screen_height = screen->height_in_pixels;
191     root_depth = screen->root_depth;
192
193     uint32_t mask = XCB_CW_EVENT_MASK;
194     uint32_t values[] = {XCB_EVENT_MASK_POINTER_MOTION};
195     motion_recorder = xcb_generate_id(dpy);
196     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);
197
198     xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED,
199                               ewmh->_NET_SUPPORTING_WM_CHECK,
200                               ewmh->_NET_DESKTOP_NAMES,
201                               ewmh->_NET_NUMBER_OF_DESKTOPS,
202                               ewmh->_NET_CURRENT_DESKTOP,
203                               ewmh->_NET_CLIENT_LIST,
204                               ewmh->_NET_ACTIVE_WINDOW,
205                               ewmh->_NET_WM_DESKTOP,
206                               ewmh->_NET_WM_STATE,
207                               ewmh->_NET_WM_STATE_FULLSCREEN,
208                               ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
209                               ewmh->_NET_WM_WINDOW_TYPE,
210                               ewmh->_NET_WM_WINDOW_TYPE_DOCK,
211                               ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
212                               ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
213                               ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
214                               ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
215
216     xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
217     ewmh_set_supporting(motion_recorder);
218
219     xcb_intern_atom_reply_t *iar = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen("_COMPTON_SHADOW"), "_COMPTON_SHADOW"), NULL);
220
221     if (iar != NULL) {
222         compton_shadow = iar->atom;
223         free(iar);
224     }
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 }
244
245 void register_events(void)
246 {
247     uint32_t values[] = {ROOT_EVENT_MASK};
248     xcb_generic_error_t *e = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, root, XCB_CW_EVENT_MASK, values));
249     if (e != NULL) {
250         xcb_disconnect(dpy);
251         err("Another window manager is already running.\n");
252     }
253 }
254
255 bool import_monitors(void)
256 {
257     PUTS("import monitors");
258     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);
259     if (sres == NULL)
260         return false;
261
262     int len = xcb_randr_get_screen_resources_current_outputs_length(sres);
263     xcb_randr_output_t *outputs = xcb_randr_get_screen_resources_current_outputs(sres);
264
265     xcb_randr_get_output_info_cookie_t cookies[len];
266     for (int i = 0; i < len; i++)
267         cookies[i] = xcb_randr_get_output_info(dpy, outputs[i], XCB_CURRENT_TIME);
268
269     for (monitor_t *m = mon_head; m != NULL; m = m->next)
270         m->wired = false;
271
272     monitor_t *mm = NULL;
273     unsigned int num = 0;
274
275     for (int i = 0; i < len; i++) {
276         xcb_randr_get_output_info_reply_t *info = xcb_randr_get_output_info_reply(dpy, cookies[i], NULL);
277         if (info != NULL && info->crtc != XCB_NONE) {
278
279             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);
280             if (cir != NULL) {
281                 xcb_rectangle_t rect = (xcb_rectangle_t) {cir->x, cir->y, cir->width, cir->height};
282                 mm = get_monitor_by_id(outputs[i]);
283                 if (mm != NULL) {
284                     mm->rectangle = rect;
285                     for (desktop_t *d = mm->desk_head; d != NULL; d = d->next)
286                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
287                             fit_monitor(mm, n->client);
288                     arrange(mm, mm->desk);
289                     mm->wired = true;
290                     PRINTF("update monitor %s (0x%X)\n", mm->name, mm->id);
291                 } else {
292                     mm = add_monitor(&rect);
293                     char *name = (char *)xcb_randr_get_output_info_name(info);
294                     size_t name_len = MIN(sizeof(mm->name), (size_t)xcb_randr_get_output_info_name_length(info));
295                     strncpy(mm->name, name, name_len);
296                     mm->name[name_len] = '\0';
297                     mm->id = outputs[i];
298                     add_desktop(mm, make_desktop(NULL));
299                     PRINTF("add monitor %s (0x%X)\n", mm->name, mm->id);
300                 }
301                 num++;
302             }
303             free(cir);
304         }
305         free(info);
306     }
307
308     monitor_t *m = mon_head;
309     while (m != NULL) {
310         monitor_t *next = m->next;
311         if (!m->wired) {
312             PRINTF("remove monitor %s (0x%X)\n", m->name, m->id);
313             merge_monitors(m, mm);
314             remove_monitor(m);
315         }
316         m = next;
317     }
318
319     free(sres);
320     update_motion_recorder();
321     return (num_monitors > 0);
322 }
323
324 void quit(void)
325 {
326     running = false;
327 }
328
329 void cleanup(void)
330 {
331     while (mon_head != NULL)
332         remove_monitor(mon_head);
333     while (rule_head != NULL)
334         remove_rule(rule_head);
335     free(frozen_pointer);
336 }
337
338 void put_status(void)
339 {
340     if (status_fifo == NULL)
341         return;
342     if (status_prefix != NULL)
343         fprintf(status_fifo, "%s", status_prefix);
344     bool urgent = false;
345     for (monitor_t *m = mon_head; m != NULL; m = m->next) {
346         fprintf(status_fifo, "%c%s:", (mon == m ? 'M' : 'm'), m->name);
347         for (desktop_t *d = m->desk_head; d != NULL; d = d->next, urgent = false) {
348             for (node_t *n = first_extrema(d->root); n != NULL && !urgent; n = next_leaf(n, d->root))
349                 urgent |= n->client->urgent;
350             fprintf(status_fifo, "%c%s:", m->desk == d ? (urgent ? 'U' : 'D') : (d->root == NULL ? 'E' : (urgent ? 'u' : 'd')), d->name);
351         }
352     }
353     if (mon != NULL && mon->desk != NULL)
354         fprintf(status_fifo, "L%s", (mon->desk->layout == LAYOUT_TILED ? "tiled" : "monocle"));
355     fprintf(status_fifo, "\n");
356     fflush(status_fifo);
357 }