7 #include <sys/socket.h>
9 #include <sys/select.h>
13 #include <xcb/xcb_event.h>
14 #include <xcb/xcb_ewmh.h>
15 #include <xcb/xinerama.h>
33 void register_events(void)
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));
39 err("another wm is already running\n");
43 void grab_buttons(void)
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);
53 void ungrab_buttons(void)
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);
66 screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
68 err("can't acquire screen\n");
71 screen_width = screen->width_in_pixels;
72 screen_height = screen->height_in_pixels;
73 root_depth = screen->root_depth;
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,
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};
91 xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
93 monitor_uid = desktop_uid = client_uid = 0;
94 mon = last_mon = mon_head = mon_tail = NULL;
96 bool xinerama_is_active = false;
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);
101 xinerama_is_active = xia->state;
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};
118 warn("Xinerama is inactive");
119 xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
123 for (monitor_t *m = mon_head; m != NULL; m = m->next)
124 add_desktop(m, NULL);
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;
135 int main(int argc, char *argv[])
137 if (argc == 2 && strcmp(argv[1], "-v") == 0) {
138 printf("%s\n", VERSION);
143 char socket_path[MAXLEN];
144 int sock_fd, ret_fd, dpy_fd, sel, n;
145 struct sockaddr_un sock_address;
147 char msg[BUFSIZ] = {0};
148 char rsp[BUFSIZ] = {0};
150 xcb_generic_event_t *event;
154 dpy = xcb_connect(NULL, &default_screen);
156 if (xcb_connection_has_error(dpy))
157 err("can't open display\n");
161 dpy_fd = xcb_get_file_descriptor(dpy);
163 char *sp = getenv(SOCKET_ENV_VAR);
164 strncpy(socket_path, (sp == NULL ? DEFAULT_SOCKET_PATH : sp), sizeof(socket_path));
166 sock_address.sun_family = AF_UNIX;
167 strncpy(sock_address.sun_path, socket_path, sizeof(sock_address.sun_path));
170 sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
173 err("couldn't create socket\n");
175 bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address));
176 listen(sock_fd, SOMAXCONN);
178 sel = MAX(sock_fd, dpy_fd) + 1;
183 ewmh_update_wm_name();
189 FD_ZERO(&descriptors);
190 FD_SET(sock_fd, &descriptors);
191 FD_SET(dpy_fd, &descriptors);
193 if (select(sel, &descriptors, NULL, NULL, NULL)) {
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) {
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);
209 if (FD_ISSET(dpy_fd, &descriptors)) {
210 while ((event = xcb_poll_for_event(dpy)) != NULL) {
218 if (xcb_connection_has_error(dpy)) {
219 err("connection has errors\n");
224 xcb_ewmh_connection_wipe(ewmh);