]> git.lizzy.rs Git - bspwm.git/blob - bspwm.c
Set ID of monitor when adding it
[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                     arrange(mm, mm->desk);
83                     mm->wired = true;
84                     PRINTF("update monitor %s (0x%X)\n", mm->name, mm->id);
85                 } else {
86                     mm = add_monitor(&rect);
87                     char *name = (char *)xcb_randr_get_output_info_name(info);
88                     size_t name_len = MIN(sizeof(mm->name), (size_t)xcb_randr_get_output_info_name_length(info));
89                     strncpy(mm->name, name, name_len);
90                     mm->name[name_len] = '\0';
91                     mm->id = outputs[i];
92                     add_desktop(mm, NULL);
93                     PRINTF("add monitor %s (0x%X)\n", mm->name, mm->id);
94                 }
95                 num++;
96             }
97             free(cir);
98         }
99         free(info);
100     }
101
102     monitor_t *m = mon_head;
103     while (m != NULL) {
104         monitor_t *next = m->next;
105         if (!m->wired) {
106             PRINTF("remove monitor %s (0x%X)\n", m->name, m->id);
107             transfer_desktops(mm, m);
108         }
109         m = next;
110     }
111
112     free(sres);
113     put_status();
114     return (num_monitors > 0);
115 }
116
117 void init(void)
118 {
119     num_monitors = num_desktops = num_clients = 0;
120     monitor_uid = desktop_uid = client_uid = rule_uid = 0;
121     mon = last_mon = mon_head = mon_tail = NULL;
122     rule_head = rule_tail = NULL;
123     randr_base = 0;
124     split_mode = MODE_AUTOMATIC;
125     visible = true;
126     exit_status = 0;
127 }
128
129 void setup(void)
130 {
131     init();
132     ewmh_init();
133     screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
134     if (screen == NULL)
135         err("Can't acquire the default screen.\n");
136     root = screen->root;
137     register_events();
138
139     screen_width = screen->width_in_pixels;
140     screen_height = screen->height_in_pixels;
141     root_depth = screen->root_depth;
142
143     uint32_t mask = XCB_CW_EVENT_MASK;
144     uint32_t values[] = {XCB_EVENT_MASK_POINTER_MOTION};
145     motion_recorder = xcb_generate_id(dpy);
146     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);
147
148     xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED,
149                               ewmh->_NET_DESKTOP_NAMES,
150                               ewmh->_NET_NUMBER_OF_DESKTOPS,
151                               ewmh->_NET_CURRENT_DESKTOP,
152                               ewmh->_NET_CLIENT_LIST,
153                               ewmh->_NET_ACTIVE_WINDOW,
154                               ewmh->_NET_WM_DESKTOP,
155                               ewmh->_NET_WM_STATE,
156                               ewmh->_NET_WM_STATE_FULLSCREEN,
157                               ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
158                               ewmh->_NET_WM_WINDOW_TYPE,
159                               ewmh->_NET_WM_WINDOW_TYPE_DOCK,
160                               ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
161                               ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
162                               ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
163                               ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
164
165     xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
166
167     xcb_intern_atom_reply_t *iar = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen("_COMPTON_SHADOW"), "_COMPTON_SHADOW"), NULL);
168
169     if (iar != NULL) {
170         compton_shadow = iar->atom;
171         free(iar);
172     }
173
174     const xcb_query_extension_reply_t *qep = xcb_get_extension_data(dpy, &xcb_randr_id);
175     if (qep->present && import_monitors()) {
176         randr_base = qep->first_event;
177         xcb_randr_select_input(dpy, root, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
178     } else {
179         warn("Couldn't retrieve monitors via RandR.\n");
180         xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
181         monitor_t *m = add_monitor(&rect);
182         add_desktop(m, NULL);
183     }
184
185     ewmh_update_number_of_desktops();
186     ewmh_update_desktop_names();
187     ewmh_update_current_desktop();
188     frozen_pointer = make_pointer_state();
189 }
190
191 int main(int argc, char *argv[])
192 {
193     fd_set descriptors;
194     char socket_path[MAXLEN];
195     char *fifo_path = NULL;
196     status_prefix = NULL;
197     int sock_fd, ret_fd, dpy_fd, sel, n;
198     struct sockaddr_un sock_address;
199     size_t rsplen = 0;
200     char msg[BUFSIZ] = {0};
201     char rsp[BUFSIZ] = {0};
202     xcb_generic_event_t *event;
203     char opt;
204
205     while ((opt = getopt(argc, argv, "hvs:p:")) != -1) {
206         switch (opt) {
207             case 'h':
208                 printf("bspwm [-h|-v|-s PANEL_FIFO|-p PANEL_PREFIX]\n");
209                 exit(EXIT_SUCCESS);
210                 break;
211             case 'v':
212                 printf("%s\n", VERSION);
213                 exit(EXIT_SUCCESS);
214                 break;
215             case 's':
216                 fifo_path = optarg;
217                 break;
218             case 'p':
219                 status_prefix = optarg;
220                 break;
221         }
222     }
223
224     running = true;
225     dpy = xcb_connect(NULL, &default_screen);
226
227     if (xcb_connection_has_error(dpy))
228         err("Can't open the default display.\n");
229
230     setup();
231
232     dpy_fd = xcb_get_file_descriptor(dpy);
233
234     char *sp = getenv(SOCKET_ENV_VAR);
235     strncpy(socket_path, (sp == NULL ? DEFAULT_SOCKET_PATH : sp), sizeof(socket_path));
236
237     sock_address.sun_family = AF_UNIX;
238     strncpy(sock_address.sun_path, socket_path, sizeof(sock_address.sun_path));
239     unlink(socket_path);
240
241     sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
242
243     if (sock_fd == -1)
244         err("Couldn't create the socket.\n");
245
246     if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
247         err("Couldn't bind a name to the socket.\n");
248
249     if (listen(sock_fd, SOMAXCONN) == -1)
250         err("Couldn't listen to the socket.\n");
251
252     sel = MAX(sock_fd, dpy_fd) + 1;
253
254     if (fifo_path != NULL) {
255         int fifo_fd = open(fifo_path, O_RDWR | O_NONBLOCK);
256         if (fifo_fd != -1)
257             status_fifo = fdopen(fifo_fd, "w");
258         else
259             warn("Couldn't open status fifo.\n");
260     }
261
262     load_settings();
263     run_autostart();
264     ewmh_update_wm_name();
265
266     while (running) {
267
268         xcb_flush(dpy);
269
270         FD_ZERO(&descriptors);
271         FD_SET(sock_fd, &descriptors);
272         FD_SET(dpy_fd, &descriptors);
273
274         if (select(sel, &descriptors, NULL, NULL, NULL) > 0) {
275
276             if (FD_ISSET(sock_fd, &descriptors)) {
277                 ret_fd = accept(sock_fd, NULL, 0);
278                 if (ret_fd > 0 && (n = recv(ret_fd, msg, sizeof(msg), 0)) > 0) {
279                     msg[n] = '\0';
280                     process_message(msg, rsp);
281                     rsplen = strlen(rsp);
282                     if (rsp[rsplen - 1] == '\n')
283                         rsp[--rsplen] = '\0';
284                     send(ret_fd, rsp, rsplen, 0);
285                     close(ret_fd);
286                     rsp[0] = '\0';
287                 }
288             }
289
290             if (FD_ISSET(dpy_fd, &descriptors)) {
291                 while ((event = xcb_poll_for_event(dpy)) != NULL) {
292                     handle_event(event);
293                     free(event);
294                 }
295             }
296
297         }
298
299         if (xcb_connection_has_error(dpy))
300             err("The server has closed the connection.\n");
301     }
302
303     cleanup();
304     close(sock_fd);
305     if (status_fifo != NULL)
306         fclose(status_fifo);
307     xcb_ewmh_connection_wipe(ewmh);
308     xcb_destroy_window(dpy, motion_recorder);
309     free(ewmh);
310     xcb_flush(dpy);
311     xcb_disconnect(dpy);
312     return exit_status;
313 }