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