]> git.lizzy.rs Git - bspwm.git/blob - bspwm.c
Final specifications
[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 <sys/socket.h>
8 #include <sys/un.h>
9 #include <sys/select.h>
10 #include <sys/stat.h>
11 #include <sys/wait.h>
12 #include <xcb/xcb.h>
13 #include <xcb/xcb_event.h>
14 #include <xcb/xcb_ewmh.h>
15 #include "helpers.h"
16 #include "types.h"
17 #include "settings.h"
18 #include "messages.h"
19 #include "events.h"
20 #include "common.h"
21 #include "utils.h"
22 #include "bspwm.h"
23 #include "ewmh.h"
24  
25 void quit(void)
26 {
27     running = false;
28 }
29
30 // check for other WM and initiate events capture
31 int register_events(void)
32 {
33     xcb_generic_error_t *error;
34     unsigned int values[] = {XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_BUTTON_PRESS};
35     error = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, screen->root, XCB_CW_EVENT_MASK, values));
36     xcb_flush(dpy);
37     if (error) return 1;
38     return 0;
39 }
40  
41 /* wrapper to get atoms using xcb */
42 void get_atoms(char **names, xcb_atom_t *atoms, int count)
43 {
44     int i;
45     xcb_intern_atom_cookie_t cookies[count];
46     xcb_intern_atom_reply_t  *reply;
47
48     for (i = 0; i < count; i++)
49         cookies[i] = xcb_intern_atom(dpy, 0, strlen(names[i]), names[i]);
50     for (i = 0; i < count; i++) {
51         reply = xcb_intern_atom_reply(dpy, cookies[i], NULL);
52         if (reply) {
53             /* PRINTF("%s : %d\n", names[i], reply->atom); */
54             atoms[i] = reply->atom;
55             free(reply);
56         } else {
57             PUTS("warning: failed to register atoms");
58         }
59     }
60 }
61
62 xcb_screen_t *screen_of_display(xcb_connection_t *dpy, int default_screen)
63 {
64     xcb_screen_iterator_t iter = xcb_setup_roots_iterator(xcb_get_setup(dpy));
65     for (; iter.rem; --default_screen, xcb_screen_next(&iter))
66         if (default_screen == 0) return iter.data;
67     return NULL;
68 }
69
70 void sigchld(int sig)
71 {
72     sig = sig; /* to prevent an "ununsed parameter" warning */
73     if (signal(SIGCHLD, sigchld) == SIG_ERR)
74         die("cannot install SIGCHLD handler\n");
75     while (0 < waitpid(-1, NULL, WNOHANG))
76         ;
77 }
78
79 void setup(int default_screen)
80 {
81     sigchld(0);
82     screen = screen_of_display(dpy, default_screen);
83     if (!screen)
84         die("error: cannot aquire screen\n");
85     screen_width = screen->width_in_pixels;
86     screen_height = screen->height_in_pixels;
87
88     char *WM_ATOM_NAME[] = { "WM_PROTOCOLS", "WM_DELETE_WINDOW" };
89     char *NET_ATOM_NAME[] = { "_NET_SUPPORTED", "_NET_WM_STATE_FULLSCREEN", "_NET_WM_STATE", "_NET_ACTIVE_WINDOW" };
90
91     /* set up atoms for dialog/notification windows */
92     get_atoms(WM_ATOM_NAME, wmatoms, WM_COUNT);
93     get_atoms(NET_ATOM_NAME, netatoms, NET_COUNT);
94
95     xcb_change_property(dpy, XCB_PROP_MODE_REPLACE, screen->root, netatoms[NET_SUPPORTED], XCB_ATOM_ATOM, 32, NET_COUNT, netatoms);
96
97     ewmh_init();
98 }
99
100 int main(void)
101 {
102     fd_set descriptors;
103     int sock_fd, ret_fd, xfd, sel, nbr;
104     struct sockaddr_un sock_address;
105     char *sock_path;
106     char msg[BUFSIZ], rsp[BUFSIZ];
107
108     xcb_generic_event_t *event;
109
110     running = true;
111
112     dpy = xcb_connect(NULL, &default_screen);
113
114     if (xcb_connection_has_error(dpy))
115         die("error: cannot open display\n");
116
117     setup(default_screen);
118
119     /* if (register_events() == 1) { */
120     /*     xcb_disconnect(dpy); */
121     /*     die("another WM is already running\n"); */
122     /* } */
123
124     xfd = xcb_get_file_descriptor(dpy);
125
126     sock_path = getenv(SOCK_PATH);
127
128     if (sock_path == NULL)
129         die("BSPWM_SOCKET environment variable is not set\n");
130
131     sock_address.sun_family = AF_UNIX;
132     strcpy(sock_address.sun_path, sock_path);
133     unlink(sock_path);
134
135     sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
136
137     if (sock_fd == -1) 
138         die("error: could not create socket\n");
139
140     bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address));
141     listen(sock_fd, SOMAXCONN);
142
143     sel = MAX(sock_fd, xfd) + 1;
144
145     load_settings();
146
147     while (running) {
148
149         FD_ZERO(&descriptors);
150         FD_SET(sock_fd, &descriptors);
151         FD_SET(xfd, &descriptors);
152
153         xcb_flush(dpy);
154
155         if (select(sel, &descriptors, NULL, NULL, NULL)) {
156
157             if (FD_ISSET(xfd, &descriptors)) {
158                 while ((event = xcb_poll_for_event(dpy)) != NULL) {
159                     handle_event(event);
160                     free(event);
161                 }
162             }
163
164             if (FD_ISSET(sock_fd, &descriptors)) {
165                 ret_fd = accept(sock_fd, NULL, 0);
166                 if (ret_fd > 0 && (nbr = recv(ret_fd, msg, sizeof(msg), 0)) > 0) {
167                     msg[nbr] = '\0';
168                     strcpy(rsp, EMPTY_RESPONSE);
169                     process_message(msg, rsp);
170                     send(ret_fd, rsp, strlen(rsp), 0);
171                 }
172             }
173         }
174     }
175
176     close(sock_fd);
177     xcb_disconnect(dpy);
178     return 0;
179 }