]> git.lizzy.rs Git - bspwm.git/blob - bspwm.c
Print status informations via control --subscribe
[bspwm.git] / bspwm.c
1 /* * Copyright (c) 2012-2013 Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
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.
12  *
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.
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/select.h>
29 #ifdef __OpenBSD__
30 #include <sys/types.h>
31 #endif
32 #include <sys/socket.h>
33 #include <sys/un.h>
34 #include <signal.h>
35 #include <unistd.h>
36 #include "types.h"
37 #include "desktop.h"
38 #include "monitor.h"
39 #include "settings.h"
40 #include "messages.h"
41 #include "subscribe.h"
42 #include "events.h"
43 #include "common.h"
44 #include "window.h"
45 #include "history.h"
46 #include "stack.h"
47 #include "ewmh.h"
48 #include "bspwm.h"
49
50 int main(int argc, char *argv[])
51 {
52     fd_set descriptors;
53     char socket_path[MAXLEN];
54     config_path[0] = '\0';
55     int sock_fd, ret_fd, dpy_fd, sel, n;
56     struct sockaddr_un sock_address;
57     size_t rsp_len = 0;
58     char msg[BUFSIZ] = {0};
59     char rsp[BUFSIZ] = {0};
60     xcb_generic_event_t *event;
61     char opt;
62
63     while ((opt = getopt(argc, argv, "hvc:")) != -1) {
64         switch (opt) {
65             case 'h':
66                 printf(WM_NAME " [-h|-v|-c CONFIG_PATH]\n");
67                 exit(EXIT_SUCCESS);
68                 break;
69             case 'v':
70                 printf("%s\n", VERSION);
71                 exit(EXIT_SUCCESS);
72                 break;
73             case 'c':
74                 snprintf(config_path, sizeof(config_path), "%s", optarg);
75                 break;
76         }
77     }
78
79     if (config_path[0] == '\0') {
80         char *config_home = getenv(CONFIG_HOME_ENV);
81         if (config_home != NULL)
82             snprintf(config_path, sizeof(config_path), "%s/%s/%s", config_home, WM_NAME, CONFIG_NAME);
83         else
84             snprintf(config_path, sizeof(config_path), "%s/%s/%s/%s", getenv("HOME"), ".config", WM_NAME, CONFIG_NAME);
85     }
86
87     dpy = xcb_connect(NULL, &default_screen);
88
89     if (xcb_connection_has_error(dpy))
90         err("Can't open the default display.\n");
91
92     setup();
93
94     dpy_fd = xcb_get_file_descriptor(dpy);
95
96     char *sp = getenv(SOCKET_ENV_VAR);
97     snprintf(socket_path, sizeof(socket_path), "%s", (sp == NULL ? DEFAULT_SOCKET_PATH : sp));
98
99     sock_address.sun_family = AF_UNIX;
100     snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), "%s", socket_path);
101     unlink(socket_path);
102
103     sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
104
105     if (sock_fd == -1)
106         err("Couldn't create the socket.\n");
107
108     if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
109         err("Couldn't bind a name to the socket.\n");
110
111     if (listen(sock_fd, SOMAXCONN) == -1)
112         err("Couldn't listen to the socket.\n");
113
114     sel = MAX(sock_fd, dpy_fd) + 1;
115
116     signal(SIGPIPE, SIG_IGN);
117     load_settings();
118     run_config();
119     running = true;
120
121     while (running) {
122
123         xcb_flush(dpy);
124
125         FD_ZERO(&descriptors);
126         FD_SET(sock_fd, &descriptors);
127         FD_SET(dpy_fd, &descriptors);
128
129         if (select(sel, &descriptors, NULL, NULL, NULL) > 0) {
130
131             if (FD_ISSET(sock_fd, &descriptors)) {
132                 ret_fd = accept(sock_fd, NULL, 0);
133                 if (ret_fd > 0 && (n = recv(ret_fd, msg, sizeof(msg), 0)) > 0) {
134                     msg[n] = '\0';
135                     if (handle_message(msg, n, rsp)) {
136                         rsp_len = strlen(rsp);
137                         if (rsp[rsp_len - 1] == '\n')
138                             rsp[--rsp_len] = '\0';
139                     } else {
140                         rsp[0] = MESSAGE_FAILURE;
141                         rsp_len = 1;
142                     }
143                     if (rsp_len == 1 && rsp[0] == MESSAGE_SUBSCRIBE) {
144                         add_subscriber(ret_fd);
145                     } else {
146                         send(ret_fd, rsp, rsp_len, 0);
147                         close(ret_fd);
148                     }
149                     rsp[0] = '\0';
150                 }
151             }
152
153             if (FD_ISSET(dpy_fd, &descriptors)) {
154                 while ((event = xcb_poll_for_event(dpy)) != NULL) {
155                     handle_event(event);
156                     free(event);
157                 }
158             }
159
160         }
161
162         if (xcb_connection_has_error(dpy))
163             err("The server has closed the connection.\n");
164     }
165
166     cleanup();
167     close(sock_fd);
168     xcb_ewmh_connection_wipe(ewmh);
169     xcb_destroy_window(dpy, motion_recorder);
170     free(ewmh);
171     xcb_flush(dpy);
172     xcb_disconnect(dpy);
173     return exit_status;
174 }
175
176 void init(void)
177 {
178     num_monitors = num_desktops = num_clients = 0;
179     monitor_uid = desktop_uid = 0;
180     mon = mon_head = mon_tail = pri_mon = NULL;
181     history_head = history_tail = history_needle = NULL;
182     stack_head = stack_tail = NULL;
183     subscribe_head = subscribe_tail = NULL;
184     last_motion_time = last_motion_x = last_motion_y = 0;
185     visible = auto_raise = sticky_still = record_history = true;
186     randr_base = 0;
187     exit_status = 0;
188 }
189
190 void setup(void)
191 {
192     init();
193     ewmh_init();
194     screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
195     if (screen == NULL)
196         err("Can't acquire the default screen.\n");
197     root = screen->root;
198     register_events();
199
200     screen_width = screen->width_in_pixels;
201     screen_height = screen->height_in_pixels;
202     root_depth = screen->root_depth;
203
204     uint32_t mask = XCB_CW_EVENT_MASK;
205     uint32_t values[] = {XCB_EVENT_MASK_POINTER_MOTION};
206     motion_recorder = xcb_generate_id(dpy);
207     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);
208
209     xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED,
210                               ewmh->_NET_SUPPORTING_WM_CHECK,
211                               ewmh->_NET_DESKTOP_NAMES,
212                               ewmh->_NET_NUMBER_OF_DESKTOPS,
213                               ewmh->_NET_CURRENT_DESKTOP,
214                               ewmh->_NET_CLIENT_LIST,
215                               ewmh->_NET_ACTIVE_WINDOW,
216                               ewmh->_NET_CLOSE_WINDOW,
217                               ewmh->_NET_WM_DESKTOP,
218                               ewmh->_NET_WM_STATE,
219                               ewmh->_NET_WM_STATE_FULLSCREEN,
220                               ewmh->_NET_WM_STATE_STICKY,
221                               ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
222                               ewmh->_NET_WM_WINDOW_TYPE,
223                               ewmh->_NET_WM_WINDOW_TYPE_DOCK,
224                               ewmh->_NET_WM_WINDOW_TYPE_DESKTOP,
225                               ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
226                               ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
227                               ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
228                               ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
229
230     xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
231     ewmh_set_supporting(motion_recorder);
232
233 #define GETATOM(a) \
234     get_atom(#a, &a);
235     GETATOM(WM_DELETE_WINDOW)
236     GETATOM(WM_TAKE_FOCUS)
237     GETATOM(_BSPWM_FLOATING_WINDOW)
238     GETATOM(_NET_WM_WINDOW_OPACITY)
239 #undef GETATOM
240
241     const xcb_query_extension_reply_t *qep = xcb_get_extension_data(dpy, &xcb_randr_id);
242     if (qep->present && import_monitors()) {
243         randr = true;
244         randr_base = qep->first_event;
245         xcb_randr_select_input(dpy, root, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
246     } else {
247         randr = false;
248         warn("Couldn't retrieve monitors via RandR.\n");
249         xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
250         monitor_t *m = add_monitor(rect);
251         add_desktop(m, make_desktop(NULL));
252     }
253
254     ewmh_update_number_of_desktops();
255     ewmh_update_desktop_names();
256     ewmh_update_current_desktop();
257     frozen_pointer = make_pointer_state();
258     xcb_get_input_focus_reply_t *ifo = xcb_get_input_focus_reply(dpy, xcb_get_input_focus(dpy), NULL);
259     if (ifo != NULL && (ifo->focus == XCB_INPUT_FOCUS_POINTER_ROOT || ifo->focus == XCB_NONE))
260         clear_input_focus();
261     free(ifo);
262 }
263
264 void register_events(void)
265 {
266     uint32_t values[] = {ROOT_EVENT_MASK};
267     xcb_generic_error_t *e = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, root, XCB_CW_EVENT_MASK, values));
268     if (e != NULL) {
269         xcb_disconnect(dpy);
270         err("Another window manager is already running.\n");
271     }
272 }
273
274 void quit(void)
275 {
276     running = false;
277 }
278
279 void cleanup(void)
280 {
281     while (mon_head != NULL)
282         remove_monitor(mon_head);
283     while (stack_head != NULL)
284         remove_stack(stack_head);
285     while (subscribe_head != NULL)
286         remove_subscriber(subscribe_head);
287     empty_history();
288     free(frozen_pointer);
289 }
290
291 void put_status(void)
292 {
293     subscriber_list_t *sb = subscribe_head;
294     while (sb != NULL) {
295         subscriber_list_t *next = sb->next;
296         feed_subscriber(sb);
297         sb = next;
298     }
299 }