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