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>
35 while (mon_head != NULL)
36 remove_monitor(mon_head);
37 while (rule_head != NULL)
38 remove_rule(rule_head);
42 void register_events(void)
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));
48 err("Another window manager is already running.\n");
55 screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
57 err("Can't acquire the default screen.\n");
61 screen_width = screen->width_in_pixels;
62 screen_height = screen->height_in_pixels;
63 root_depth = screen->root_depth;
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);
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,
78 ewmh->_NET_WM_STATE_FULLSCREEN,
79 ewmh->_NET_WM_WINDOW_TYPE,
80 ewmh->_NET_WM_WINDOW_TYPE_DOCK,
81 ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
82 ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
83 ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
84 ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
86 xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
88 xcb_intern_atom_reply_t *iar = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen("_COMPTON_SHADOW"), "_COMPTON_SHADOW"), NULL);
91 compton_shadow = iar->atom;
95 monitor_uid = desktop_uid = client_uid = rule_uid = 0;
96 mon = last_mon = mon_head = mon_tail = NULL;
98 bool xinerama_is_active = false;
100 if (xcb_get_extension_data(dpy, &xcb_xinerama_id)->present) {
101 xcb_xinerama_is_active_reply_t *xia = xcb_xinerama_is_active_reply(dpy, xcb_xinerama_is_active(dpy), NULL);
103 xinerama_is_active = xia->state;
108 if (xinerama_is_active) {
109 xcb_xinerama_query_screens_reply_t *xsq = xcb_xinerama_query_screens_reply(dpy, xcb_xinerama_query_screens(dpy), NULL);
110 xcb_xinerama_screen_info_t *xsi = xcb_xinerama_query_screens_screen_info(xsq);
111 int n = xcb_xinerama_query_screens_screen_info_length(xsq);
112 for (int i = 0; i < n; i++) {
113 xcb_xinerama_screen_info_t info = xsi[i];
114 xcb_rectangle_t rect = (xcb_rectangle_t) {info.x_org, info.y_org, info.width, info.height};
119 warn("Xinerama is inactive.");
120 xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
124 for (monitor_t *m = mon_head; m != NULL; m = m->next)
125 add_desktop(m, NULL);
127 ewmh_update_number_of_desktops();
128 ewmh_update_desktop_names();
129 ewmh_update_current_desktop();
130 rule_head = rule_tail = NULL;
131 frozen_pointer = make_pointer_state();
132 split_mode = MODE_AUTOMATIC;
137 int main(int argc, char *argv[])
140 char socket_path[MAXLEN];
141 char *fifo_path = NULL;
142 int sock_fd, ret_fd, dpy_fd, sel, n;
143 struct sockaddr_un sock_address;
145 char msg[BUFSIZ] = {0};
146 char rsp[BUFSIZ] = {0};
147 xcb_generic_event_t *event;
150 while ((opt = getopt(argc, argv, "vs:")) != -1) {
153 printf("%s\n", VERSION);
163 dpy = xcb_connect(NULL, &default_screen);
165 if (xcb_connection_has_error(dpy))
166 err("Can't open the default display.\n");
170 dpy_fd = xcb_get_file_descriptor(dpy);
172 char *sp = getenv(SOCKET_ENV_VAR);
173 strncpy(socket_path, (sp == NULL ? DEFAULT_SOCKET_PATH : sp), sizeof(socket_path));
175 sock_address.sun_family = AF_UNIX;
176 strncpy(sock_address.sun_path, socket_path, sizeof(sock_address.sun_path));
179 sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
182 err("Couldn't create the socket.\n");
184 if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
185 err("Couldn't bind a name to the socket.\n");
187 if (listen(sock_fd, SOMAXCONN) == -1)
188 err("Couldn't listen to the socket.\n");
190 sel = MAX(sock_fd, dpy_fd) + 1;
192 if (fifo_path != NULL) {
193 int fifo_fd = open(fifo_path, O_RDWR | O_NONBLOCK);
195 status_fifo = fdopen(fifo_fd, "w");
197 warn("Couldn't open status fifo.\n");
202 ewmh_update_wm_name();
208 FD_ZERO(&descriptors);
209 FD_SET(sock_fd, &descriptors);
210 FD_SET(dpy_fd, &descriptors);
212 if (select(sel, &descriptors, NULL, NULL, NULL) > 0) {
214 if (FD_ISSET(sock_fd, &descriptors)) {
215 ret_fd = accept(sock_fd, NULL, 0);
216 if (ret_fd > 0 && (n = recv(ret_fd, msg, sizeof(msg), 0)) > 0) {
218 process_message(msg, rsp);
219 rsplen = strlen(rsp);
220 if (rsp[rsplen - 1] == '\n')
221 rsp[--rsplen] = '\0';
222 send(ret_fd, rsp, rsplen, 0);
228 if (FD_ISSET(dpy_fd, &descriptors)) {
229 while ((event = xcb_poll_for_event(dpy)) != NULL) {
237 if (xcb_connection_has_error(dpy))
238 err("The server has closed the connection.\n");
243 if (status_fifo != NULL)
245 xcb_ewmh_connection_wipe(ewmh);
246 xcb_destroy_window(dpy, motion_recorder);