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