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