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