1 /* * Copyright (c) 2012-2013 Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation and/or
11 * other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/select.h>
30 #include <sys/types.h>
33 #include <sys/socket.h>
42 #include "subscribe.h"
52 int main(int argc, char *argv[])
55 char socket_path[MAXLEN];
56 config_path[0] = '\0';
57 int sock_fd, cli_fd, dpy_fd, max_fd, n;
58 struct sockaddr_un sock_address;
60 char msg[BUFSIZ] = {0};
61 char rsp[BUFSIZ] = {0};
62 xcb_generic_event_t *event;
65 while ((opt = getopt(argc, argv, "hvc:")) != -1) {
68 printf(WM_NAME " [-h|-v|-c CONFIG_PATH]\n");
72 printf("%s\n", VERSION);
76 snprintf(config_path, sizeof(config_path), "%s", optarg);
81 if (config_path[0] == '\0') {
82 char *config_home = getenv(CONFIG_HOME_ENV);
83 if (config_home != NULL)
84 snprintf(config_path, sizeof(config_path), "%s/%s/%s", config_home, WM_NAME, CONFIG_NAME);
86 snprintf(config_path, sizeof(config_path), "%s/%s/%s/%s", getenv("HOME"), ".config", WM_NAME, CONFIG_NAME);
89 dpy = xcb_connect(NULL, &default_screen);
91 if (xcb_connection_has_error(dpy))
92 err("Can't open the default display.\n");
96 dpy_fd = xcb_get_file_descriptor(dpy);
98 char *sp = getenv(SOCKET_ENV_VAR);
99 snprintf(socket_path, sizeof(socket_path), "%s", (sp == NULL ? DEFAULT_SOCKET_PATH : sp));
101 sock_address.sun_family = AF_UNIX;
102 snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), "%s", socket_path);
105 sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
108 err("Couldn't create the socket.\n");
110 if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
111 err("Couldn't bind a name to the socket.\n");
113 if (listen(sock_fd, SOMAXCONN) == -1)
114 err("Couldn't listen to the socket.\n");
116 signal(SIGCHLD, sig_handler);
117 signal(SIGPIPE, SIG_IGN);
126 FD_ZERO(&descriptors);
127 FD_SET(sock_fd, &descriptors);
128 FD_SET(dpy_fd, &descriptors);
129 max_fd = MAX(sock_fd, dpy_fd);
130 for (pending_rule_t *pr = pending_rule_head; pr != NULL; pr = pr->next) {
131 FD_SET(pr->fd, &descriptors);
136 if (select(max_fd + 1, &descriptors, NULL, NULL, NULL) > 0) {
138 pending_rule_t *pr = pending_rule_head;
140 pending_rule_t *next = pr->next;
141 if (FD_ISSET(pr->fd, &descriptors)) {
142 manage_window(pr->win, pr->csq, pr->fd);
143 remove_pending_rule(pr);
148 if (FD_ISSET(sock_fd, &descriptors)) {
149 cli_fd = accept(sock_fd, NULL, 0);
150 if (cli_fd > 0 && (n = recv(cli_fd, msg, sizeof(msg), 0)) > 0) {
152 if (handle_message(msg, n, rsp)) {
153 rsp_len = strlen(rsp);
155 rsp[0] = MESSAGE_FAILURE;
158 if (rsp_len == 1 && rsp[0] == MESSAGE_SUBSCRIBE) {
159 add_subscriber(cli_fd);
161 send(cli_fd, rsp, rsp_len, 0);
168 if (FD_ISSET(dpy_fd, &descriptors)) {
169 while ((event = xcb_poll_for_event(dpy)) != NULL) {
176 if (xcb_connection_has_error(dpy))
177 err("The server has closed the connection.\n");
182 xcb_ewmh_connection_wipe(ewmh);
183 xcb_destroy_window(dpy, motion_recorder);
192 num_monitors = num_desktops = num_clients = 0;
193 monitor_uid = desktop_uid = 0;
194 mon = mon_head = mon_tail = pri_mon = NULL;
195 history_head = history_tail = history_needle = NULL;
196 stack_head = stack_tail = NULL;
197 subscribe_head = subscribe_tail = NULL;
198 pending_rule_head = pending_rule_tail = NULL;
199 last_motion_time = last_motion_x = last_motion_y = 0;
200 visible = auto_raise = sticky_still = record_history = true;
209 screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
211 err("Can't acquire the default screen.\n");
215 screen_width = screen->width_in_pixels;
216 screen_height = screen->height_in_pixels;
217 root_depth = screen->root_depth;
219 uint32_t mask = XCB_CW_EVENT_MASK;
220 uint32_t values[] = {XCB_EVENT_MASK_POINTER_MOTION};
221 motion_recorder = xcb_generate_id(dpy);
222 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);
224 xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED,
225 ewmh->_NET_SUPPORTING_WM_CHECK,
226 ewmh->_NET_DESKTOP_NAMES,
227 ewmh->_NET_NUMBER_OF_DESKTOPS,
228 ewmh->_NET_CURRENT_DESKTOP,
229 ewmh->_NET_CLIENT_LIST,
230 ewmh->_NET_ACTIVE_WINDOW,
231 ewmh->_NET_CLOSE_WINDOW,
232 ewmh->_NET_WM_DESKTOP,
234 ewmh->_NET_WM_STATE_FULLSCREEN,
235 ewmh->_NET_WM_STATE_STICKY,
236 ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
237 ewmh->_NET_WM_WINDOW_TYPE,
238 ewmh->_NET_WM_WINDOW_TYPE_DOCK,
239 ewmh->_NET_WM_WINDOW_TYPE_DESKTOP,
240 ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
241 ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
242 ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
243 ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
245 xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
246 ewmh_set_supporting(motion_recorder);
250 GETATOM(WM_DELETE_WINDOW)
251 GETATOM(WM_TAKE_FOCUS)
252 GETATOM(_BSPWM_FLOATING_WINDOW)
253 GETATOM(_NET_WM_WINDOW_OPACITY)
256 const xcb_query_extension_reply_t *qep = xcb_get_extension_data(dpy, &xcb_randr_id);
257 if (qep->present && import_monitors()) {
259 randr_base = qep->first_event;
260 xcb_randr_select_input(dpy, root, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
263 warn("Couldn't retrieve monitors via RandR.\n");
264 xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
265 monitor_t *m = add_monitor(rect);
266 add_desktop(m, make_desktop(NULL));
269 ewmh_update_number_of_desktops();
270 ewmh_update_desktop_names();
271 ewmh_update_current_desktop();
272 frozen_pointer = make_pointer_state();
273 xcb_get_input_focus_reply_t *ifo = xcb_get_input_focus_reply(dpy, xcb_get_input_focus(dpy), NULL);
274 if (ifo != NULL && (ifo->focus == XCB_INPUT_FOCUS_POINTER_ROOT || ifo->focus == XCB_NONE))
279 void register_events(void)
281 uint32_t values[] = {ROOT_EVENT_MASK};
282 xcb_generic_error_t *e = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, root, XCB_CW_EVENT_MASK, values));
285 err("Another window manager is already running.\n");
296 while (mon_head != NULL)
297 remove_monitor(mon_head);
298 while (stack_head != NULL)
299 remove_stack(stack_head);
300 while (subscribe_head != NULL)
301 remove_subscriber(subscribe_head);
302 while (pending_rule_head != NULL)
303 remove_pending_rule(pending_rule_head);
305 free(frozen_pointer);
308 void put_status(void)
310 subscriber_list_t *sb = subscribe_head;
312 subscriber_list_t *next = sb->next;
318 void sig_handler(int sig)
320 signal(sig, sig_handler);
322 while (waitpid(-1, 0, WNOHANG) > 0)