]> git.sur5r.net Git - i3/i3/blob - src/mainx.c
Optimization: Get the colorpixels when loading configuration, make use of the new...
[i3/i3] / src / mainx.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  */
11 #include <stdio.h>
12 #include <assert.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/types.h>
16 #include <unistd.h>
17 #include <stdbool.h>
18 #include <assert.h>
19 #include <limits.h>
20 #include <locale.h>
21
22 #include <X11/XKBlib.h>
23 #include <X11/extensions/XKB.h>
24
25 #include <xcb/xcb.h>
26 #include <xcb/xcb_atom.h>
27 #include <xcb/xcb_aux.h>
28 #include <xcb/xcb_event.h>
29 #include <xcb/xcb_property.h>
30 #include <xcb/xcb_keysyms.h>
31 #include <xcb/xcb_icccm.h>
32 #include <xcb/xinerama.h>
33
34 #include "config.h"
35 #include "data.h"
36 #include "debug.h"
37 #include "handlers.h"
38 #include "i3.h"
39 #include "layout.h"
40 #include "queue.h"
41 #include "table.h"
42 #include "util.h"
43 #include "xcb.h"
44 #include "xinerama.h"
45 #include "manage.h"
46
47 /* This is the path to i3, copied from argv[0] when starting up */
48 char **start_argv;
49
50 /* This is our connection to X11 for use with XKB */
51 Display *xkbdpy;
52
53 /* The list of key bindings */
54 struct bindings_head bindings = TAILQ_HEAD_INITIALIZER(bindings);
55
56 /* The list of exec-lines */
57 struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
58
59 /* The list of assignments */
60 struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments);
61
62 /* This is a list of Stack_Windows, global, for easier/faster access on expose events */
63 struct stack_wins_head stack_wins = SLIST_HEAD_INITIALIZER(stack_wins);
64
65 /* The event handlers need to be global because they are accessed by our custom event handler
66    in handle_button_press(), needed for graphical resizing */
67 xcb_event_handlers_t evenths;
68 xcb_atom_t atoms[NUM_ATOMS];
69
70 int num_screens = 0;
71
72 int main(int argc, char *argv[], char *env[]) {
73         int i, screens, opt;
74         char *override_configpath = NULL;
75         bool autostart = true;
76         xcb_connection_t *conn;
77         xcb_property_handlers_t prophs;
78         xcb_window_t root;
79         xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
80
81         setlocale(LC_ALL, "");
82
83         /* Disable output buffering to make redirects in .xsession actually useful for debugging */
84         if (!isatty(fileno(stdout)))
85                 setbuf(stdout, NULL);
86
87         start_argv = argv;
88
89         while ((opt = getopt(argc, argv, "c:va")) != -1) {
90                 switch (opt) {
91                         case 'a':
92                                 LOG("Autostart disabled using -a\n");
93                                 autostart = false;
94                                 break;
95                         case 'c':
96                                 override_configpath = sstrdup(optarg);
97                                 break;
98                         case 'v':
99                                 printf("i3 version " I3_VERSION " © 2009 Michael Stapelberg and contributors\n");
100                                 exit(EXIT_SUCCESS);
101                         default:
102                                 fprintf(stderr, "Usage: %s [-c configfile]\n", argv[0]);
103                                 exit(EXIT_FAILURE);
104                 }
105         }
106
107         LOG("i3 version " I3_VERSION " starting\n");
108
109         /* Initialize the table data structures for each workspace */
110         init_table();
111
112         memset(&evenths, 0, sizeof(xcb_event_handlers_t));
113         memset(&prophs, 0, sizeof(xcb_property_handlers_t));
114
115         conn = xcb_connect(NULL, &screens);
116
117         load_configuration(conn, override_configpath);
118
119         /* Place requests for the atoms we need as soon as possible */
120         #define REQUEST_ATOM(name) atom_cookies[name] = xcb_intern_atom(conn, 0, strlen(#name), #name);
121
122         REQUEST_ATOM(_NET_SUPPORTED);
123         REQUEST_ATOM(_NET_WM_STATE_FULLSCREEN);
124         REQUEST_ATOM(_NET_SUPPORTING_WM_CHECK);
125         REQUEST_ATOM(_NET_WM_NAME);
126         REQUEST_ATOM(_NET_WM_STATE);
127         REQUEST_ATOM(_NET_WM_WINDOW_TYPE);
128         REQUEST_ATOM(_NET_WM_DESKTOP);
129         REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
130         REQUEST_ATOM(_NET_WM_STRUT_PARTIAL);
131         REQUEST_ATOM(WM_PROTOCOLS);
132         REQUEST_ATOM(WM_DELETE_WINDOW);
133         REQUEST_ATOM(UTF8_STRING);
134         REQUEST_ATOM(WM_STATE);
135
136         /* TODO: this has to be more beautiful somewhen */
137         int major, minor, error;
138
139         major = XkbMajorVersion;
140         minor = XkbMinorVersion;
141
142         int evBase, errBase;
143
144         if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &evBase, &errBase, &major, &minor, &error)) == NULL) {
145                 fprintf(stderr, "XkbOpenDisplay() failed\n");
146                 return 1;
147         }
148
149         int i1;
150         if (!XkbQueryExtension(xkbdpy,&i1,&evBase,&errBase,&major,&minor)) {
151                 fprintf(stderr, "XKB not supported by X-server\n");
152                 return 1;
153         }
154         /* end of ugliness */
155
156         xcb_event_handlers_init(conn, &evenths);
157
158         /* DEBUG: Trap all events and print them */
159         for (i = 2; i < 128; ++i)
160                 xcb_event_set_handler(&evenths, i, handle_event, 0);
161
162         for (i = 0; i < 256; ++i)
163                 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t)handle_event, 0);
164
165         /* Expose = an Application should redraw itself, in this case it’s our titlebars. */
166         xcb_event_set_expose_handler(&evenths, handle_expose_event, NULL);
167
168         /* Key presses/releases are pretty obvious, I think */
169         xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL);
170         xcb_event_set_key_release_handler(&evenths, handle_key_release, NULL);
171
172         /* Enter window = user moved his mouse over the window */
173         xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, NULL);
174
175         /* Button press = user pushed a mouse button over one of our windows */
176         xcb_event_set_button_press_handler(&evenths, handle_button_press, NULL);
177
178         /* Map notify = there is a new window */
179         xcb_event_set_map_request_handler(&evenths, handle_map_request, &prophs);
180
181         /* Unmap notify = window disappeared. When sent from a client, we don’t manage
182            it any longer. Usually, the client destroys the window shortly afterwards. */
183         xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, NULL);
184
185         /* Configure notify = window’s configuration (geometry, stacking, …). We only need
186            it to set up ignore the following enter_notify events */
187         xcb_event_set_configure_notify_handler(&evenths, handle_configure_event, NULL);
188
189         /* Configure request = window tried to change size on its own */
190         xcb_event_set_configure_request_handler(&evenths, handle_configure_request, NULL);
191
192         /* Client message are sent to the root window. The only interesting client message
193            for us is _NET_WM_STATE, we honour _NET_WM_STATE_FULLSCREEN */
194         xcb_event_set_client_message_handler(&evenths, handle_client_message, NULL);
195
196         /* Initialize the property handlers */
197         xcb_property_handlers_init(&prophs, &evenths);
198
199         /* Watch size hints (to obey correct aspect ratio) */
200         xcb_property_set_handler(&prophs, WM_NORMAL_HINTS, UINT_MAX, handle_normal_hints, NULL);
201
202         /* Get the root window and set the event mask */
203         root = xcb_aux_get_screen(conn, screens)->root;
204
205         uint32_t mask = XCB_CW_EVENT_MASK;
206         uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
207                               XCB_EVENT_MASK_STRUCTURE_NOTIFY |         /* when the user adds a screen (e.g. video
208                                                                            projector), the root window gets a
209                                                                            ConfigureNotify */
210                               XCB_EVENT_MASK_PROPERTY_CHANGE |
211                               XCB_EVENT_MASK_ENTER_WINDOW };
212         xcb_change_window_attributes(conn, root, mask, values);
213
214         /* Setup NetWM atoms */
215         #define GET_ATOM(name) { \
216                 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, atom_cookies[name], NULL); \
217                 if (!reply) { \
218                         LOG("Could not get atom " #name "\n"); \
219                         exit(-1); \
220                 } \
221                 atoms[name] = reply->atom; \
222                 free(reply); \
223         }
224
225         GET_ATOM(_NET_SUPPORTED);
226         GET_ATOM(_NET_WM_STATE_FULLSCREEN);
227         GET_ATOM(_NET_SUPPORTING_WM_CHECK);
228         GET_ATOM(_NET_WM_NAME);
229         GET_ATOM(_NET_WM_STATE);
230         GET_ATOM(_NET_WM_WINDOW_TYPE);
231         GET_ATOM(_NET_WM_DESKTOP);
232         GET_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
233         GET_ATOM(_NET_WM_STRUT_PARTIAL);
234         GET_ATOM(WM_PROTOCOLS);
235         GET_ATOM(WM_DELETE_WINDOW);
236         GET_ATOM(UTF8_STRING);
237         GET_ATOM(WM_STATE);
238
239         xcb_property_set_handler(&prophs, atoms[_NET_WM_WINDOW_TYPE], UINT_MAX, handle_window_type, NULL);
240         /* TODO: In order to comply with EWMH, we have to watch _NET_WM_STRUT_PARTIAL */
241
242         /* Watch _NET_WM_NAME (= title of the window in UTF-8) property */
243         xcb_property_set_handler(&prophs, atoms[_NET_WM_NAME], 128, handle_windowname_change, NULL);
244
245         /* Watch WM_NAME (= title of the window in compound text) property for legacy applications */
246         xcb_watch_wm_name(&prophs, 128, handle_windowname_change_legacy, NULL);
247
248         /* Watch WM_CLASS (= class of the window) */
249         xcb_property_set_handler(&prophs, WM_CLASS, 128, handle_windowclass_change, NULL);
250
251         /* Set up the atoms we support */
252         check_error(conn, xcb_change_property_checked(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTED],
253                        ATOM, 32, 7, atoms), "Could not set _NET_SUPPORTED");
254         /* Set up the window manager’s name */
255         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTING_WM_CHECK], WINDOW, 32, 1, &root);
256         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_WM_NAME], atoms[UTF8_STRING], 8, strlen("i3"), "i3");
257
258         xcb_get_numlock_mask(conn);
259
260         /* Grab the bound keys */
261         Binding *bind;
262         TAILQ_FOREACH(bind, &bindings, bindings) {
263                 LOG("Grabbing %d\n", bind->keycode);
264                 if (bind->mods & BIND_MODE_SWITCH)
265                         xcb_grab_key(conn, 0, root, 0, bind->keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
266                 else {
267                         /* Grab the key in all combinations */
268                         #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, bind->keycode, \
269                                                                 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
270                         GRAB_KEY(bind->mods);
271                         GRAB_KEY(bind->mods | xcb_numlock_mask);
272                         GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
273                 }
274         }
275
276         /* Autostarting exec-lines */
277         struct Autostart *exec;
278         if (autostart) {
279                 TAILQ_FOREACH(exec, &autostarts, autostarts) {
280                         LOG("auto-starting %s\n", exec->command);
281                         start_application(exec->command);
282                 }
283         }
284
285         /* check for Xinerama */
286         LOG("Checking for Xinerama...\n");
287         initialize_xinerama(conn);
288
289         xcb_flush(conn);
290
291         manage_existing_windows(conn, &prophs, root);
292
293         /* Get pointer position to see on which screen we’re starting */
294         xcb_query_pointer_reply_t *reply;
295         if ((reply = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL)) == NULL) {
296                 LOG("Could not get pointer position\n");
297                 return 1;
298         }
299
300         i3Screen *screen = get_screen_containing(reply->root_x, reply->root_y);
301         if (screen == NULL) {
302                 LOG("ERROR: No screen at %d x %d\n", reply->root_x, reply->root_y);
303                 return 0;
304         }
305         if (screen->current_workspace != 0) {
306                 LOG("Ok, I need to go to the other workspace\n");
307                 c_ws = &workspaces[screen->current_workspace];
308         }
309
310         /* Enter xcb’s event handler */
311         xcb_event_wait_for_event_loop(&evenths);
312
313         /* not reached */
314         return 0;
315 }