]> git.lizzy.rs Git - bspwm.git/blob - bspwm.c
New setting: `split_ratio`
[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/xinerama.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 void setup(void)
53 {
54     ewmh_init();
55     screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
56     if (screen == NULL)
57         err("Can't acquire the default screen.\n");
58     root = screen->root;
59     register_events();
60
61     screen_width = screen->width_in_pixels;
62     screen_height = screen->height_in_pixels;
63     root_depth = screen->root_depth;
64
65     uint32_t mask = XCB_CW_EVENT_MASK;
66     uint32_t values[] = {XCB_EVENT_MASK_POINTER_MOTION};
67     motion_recorder = xcb_generate_id(dpy);
68     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);
69
70     xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED,
71                               ewmh->_NET_DESKTOP_NAMES,
72                               ewmh->_NET_NUMBER_OF_DESKTOPS,
73                               ewmh->_NET_CURRENT_DESKTOP,
74                               ewmh->_NET_CLIENT_LIST,
75                               ewmh->_NET_ACTIVE_WINDOW,
76                               ewmh->_NET_WM_DESKTOP,
77                               ewmh->_NET_WM_STATE,
78                               ewmh->_NET_WM_STATE_FULLSCREEN,
79                               ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
80                               ewmh->_NET_WM_WINDOW_TYPE,
81                               ewmh->_NET_WM_WINDOW_TYPE_DOCK,
82                               ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
83                               ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
84                               ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
85                               ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
86
87     xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
88
89     xcb_intern_atom_reply_t *iar = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen("_COMPTON_SHADOW"), "_COMPTON_SHADOW"), NULL);
90
91     if (iar != NULL) {
92         compton_shadow = iar->atom;
93         free(iar);
94     }
95
96     monitor_uid = desktop_uid = client_uid = rule_uid = 0;
97     mon = last_mon = mon_head = mon_tail = NULL;
98
99     bool xinerama_is_active = false;
100
101     if (xcb_get_extension_data(dpy, &xcb_xinerama_id)->present) {
102         xcb_xinerama_is_active_reply_t *xia = xcb_xinerama_is_active_reply(dpy, xcb_xinerama_is_active(dpy), NULL);
103         if (xia != NULL) {
104             xinerama_is_active = xia->state;
105             free(xia);
106         }
107     }
108
109     if (xinerama_is_active) {
110         xcb_xinerama_query_screens_reply_t *xsq = xcb_xinerama_query_screens_reply(dpy, xcb_xinerama_query_screens(dpy), NULL);
111         xcb_xinerama_screen_info_t *xsi = xcb_xinerama_query_screens_screen_info(xsq);
112         int n = xcb_xinerama_query_screens_screen_info_length(xsq);
113         for (int i = 0; i < n; i++) {
114             xcb_xinerama_screen_info_t info = xsi[i];
115             xcb_rectangle_t rect = (xcb_rectangle_t) {info.x_org, info.y_org, info.width, info.height};
116             add_monitor(&rect);
117         }
118         free(xsq);
119     } else {
120         warn("Xinerama is inactive.");
121         xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
122         add_monitor(&rect);
123     }
124
125     for (monitor_t *m = mon_head; m != NULL; m = m->next)
126         add_desktop(m, NULL);
127
128     ewmh_update_number_of_desktops();
129     ewmh_update_desktop_names();
130     ewmh_update_current_desktop();
131     rule_head = rule_tail = NULL;
132     frozen_pointer = make_pointer_state();
133     split_mode = MODE_AUTOMATIC;
134     visible = true;
135     exit_status = 0;
136 }
137
138 int main(int argc, char *argv[])
139 {
140     fd_set descriptors;
141     char socket_path[MAXLEN];
142     char *fifo_path = NULL;
143     status_prefix = NULL;
144     int sock_fd, ret_fd, dpy_fd, sel, n;
145     struct sockaddr_un sock_address;
146     size_t rsplen = 0;
147     char msg[BUFSIZ] = {0};
148     char rsp[BUFSIZ] = {0};
149     xcb_generic_event_t *event;
150     char opt;
151
152     while ((opt = getopt(argc, argv, "hvs:p:")) != -1) {
153         switch (opt) {
154             case 'h':
155                 printf("bspwm [-h|-v|-s PANEL_FIFO|-p PANEL_PREFIX]\n");
156                 exit(EXIT_SUCCESS);
157                 break;
158             case 'v':
159                 printf("%s\n", VERSION);
160                 exit(EXIT_SUCCESS);
161                 break;
162             case 's':
163                 fifo_path = optarg;
164                 break;
165             case 'p':
166                 status_prefix = optarg;
167                 break;
168         }
169     }
170
171     running = true;
172     dpy = xcb_connect(NULL, &default_screen);
173
174     if (xcb_connection_has_error(dpy))
175         err("Can't open the default display.\n");
176
177     setup();
178
179     dpy_fd = xcb_get_file_descriptor(dpy);
180
181     char *sp = getenv(SOCKET_ENV_VAR);
182     strncpy(socket_path, (sp == NULL ? DEFAULT_SOCKET_PATH : sp), sizeof(socket_path));
183
184     sock_address.sun_family = AF_UNIX;
185     strncpy(sock_address.sun_path, socket_path, sizeof(sock_address.sun_path));
186     unlink(socket_path);
187
188     sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
189
190     if (sock_fd == -1)
191         err("Couldn't create the socket.\n");
192
193     if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
194         err("Couldn't bind a name to the socket.\n");
195
196     if (listen(sock_fd, SOMAXCONN) == -1)
197         err("Couldn't listen to the socket.\n");
198
199     sel = MAX(sock_fd, dpy_fd) + 1;
200
201     if (fifo_path != NULL) {
202         int fifo_fd = open(fifo_path, O_RDWR | O_NONBLOCK);
203         if (fifo_fd != -1)
204             status_fifo = fdopen(fifo_fd, "w");
205         else
206             warn("Couldn't open status fifo.\n");
207     }
208
209     load_settings();
210     run_autostart();
211     ewmh_update_wm_name();
212
213     while (running) {
214
215         xcb_flush(dpy);
216
217         FD_ZERO(&descriptors);
218         FD_SET(sock_fd, &descriptors);
219         FD_SET(dpy_fd, &descriptors);
220
221         if (select(sel, &descriptors, NULL, NULL, NULL) > 0) {
222
223             if (FD_ISSET(sock_fd, &descriptors)) {
224                 ret_fd = accept(sock_fd, NULL, 0);
225                 if (ret_fd > 0 && (n = recv(ret_fd, msg, sizeof(msg), 0)) > 0) {
226                     msg[n] = '\0';
227                     process_message(msg, rsp);
228                     rsplen = strlen(rsp);
229                     if (rsp[rsplen - 1] == '\n')
230                         rsp[--rsplen] = '\0';
231                     send(ret_fd, rsp, rsplen, 0);
232                     close(ret_fd);
233                     rsp[0] = '\0';
234                 }
235             }
236
237             if (FD_ISSET(dpy_fd, &descriptors)) {
238                 while ((event = xcb_poll_for_event(dpy)) != NULL) {
239                     handle_event(event);
240                     free(event);
241                 }
242             }
243
244         }
245
246         if (xcb_connection_has_error(dpy))
247             err("The server has closed the connection.\n");
248     }
249
250     cleanup();
251     close(sock_fd);
252     if (status_fifo != NULL)
253         fclose(status_fifo);
254     xcb_ewmh_connection_wipe(ewmh);
255     xcb_destroy_window(dpy, motion_recorder);
256     free(ewmh);
257     xcb_flush(dpy);
258     xcb_disconnect(dpy);
259     return exit_status;
260 }