]> git.sur5r.net Git - i3/i3/blob - docs/hacking-howto
website: add download link for OpenBSD
[i3/i3] / docs / hacking-howto
1 Hacking i3: How To
2 ==================
3 Michael Stapelberg <michael+i3@stapelberg.de>
4 May 2009
5
6 This document is intended to be the first thing you read before looking and/or touching
7 i3’s source code. It should contain all important information to help you understand
8 why things are like they are. If it does not mention something you find necessary, please
9 do not hesitate to contact me.
10
11 == Window Managers
12
13 A window manager is not necessarily needed to run X, but it is usually used in combination
14 to facilitate some things. The window manager's job is to take care of the placement of
15 windows, to provide the user some mechanisms to change the position/size of windows and
16 to communicate with clients to a certain extent (for example handle fullscreen requests
17 of clients such as MPlayer).
18
19 There are no different contexts in which X11 clients run, so a window manager is just another
20 client, like all other X11 applications. However, it handles some events which normal clients
21 usually don’t handle.
22
23 In the case of i3, the tasks (and order of them) are the following:
24
25 . Grab the key bindings (events will be sent upon keypress/keyrelease)
26 . Iterate through all existing windows (if the window manager is not started as the first
27   client of X) and manage them (= reparent them, create window decorations)
28 . When new windows are created, manage them
29 . Handle the client’s `_WM_STATE` property, but only the `_WM_STATE_FULLSCREEN`
30 . Handle the client’s `WM_NAME` property
31 . Handle the client’s size hints to display them proportionally
32 . Handle enter notifications (focus follows mouse)
33 . Handle button (as in mouse buttons) presses for focus/raise on click
34 . Handle expose events to re-draw own windows such as decorations
35 . React to the user’s commands: Change focus, Move windows, Switch workspaces,
36 Change the layout mode of a container (default/stacking), Start a new application,
37 Restart the window manager
38
39 In the following chapters, each of these tasks and their implementation details will be discussed.
40
41 === Tiling window managers
42
43 Traditionally, there are two approaches to managing windows: The most common one nowadays is
44 floating, which means the user can freely move/resize the windows. The other approach is called
45 tiling, which means that your window manager distributing windows to use as much space as
46 possible while not overlapping.
47
48 The idea behind tiling is that you should not need to waste your time moving/resizing windows
49 while you usually want to get some work done. After all, most users sooner or later tend to
50 lay out their windows in a way which corresponds to tiling or stacking mode in i3. Therefore,
51 why not let i3 do this for you? Certainly, it’s faster than you could ever do it.
52
53 The problem with most tiling window managers is that they are too unflexible. In my opinion, a
54 window manager is just another tool, and similar to vim which can edit all kinds of text files
55 (like source code, HTML, …) and is not limited to a specific file type, a window manager should
56 not limit itself to a certain layout (like dwm, awesome, …) but provide mechanisms for you to
57 easily create the layout you need at the moment.
58
59 === The layout table
60
61 To accomplish flexible layouts, we decided to simply use a table. The table grows and shrinks
62 as you need it. Each cell holds a container which then holds windows (see picture below). You
63 can use different layouts for each container (default layout and stacking layout).
64
65 So, when you open a terminal and immediately open another one, they reside in the same container,
66 in default layout. The layout table has exactly one column, one row and therefore one cell.
67 When you move one of the terminals to the right, the table needs to grow. It will be expanded
68 to two columns and one row. This enables you to have different layouts for each container.
69 The table then looks like this:
70
71 [width="15%",cols="^,^"]
72 |========
73 | T1 | T2
74 |========
75
76 When moving terminal 2 to the bottom, the table will be expanded again.
77
78 [width="15%",cols="^,^"]
79 |========
80 | T1 |
81 |    | T2
82 |========
83
84 You can really think of the layout table like a traditional HTML table, if you’ve ever
85 designed one. Especially col- and rowspan work equally. Below you see an example of
86 colspan=2 for the first container (which has T1 as window).
87
88 [width="15%",cols="^asciidoc"]
89 |========
90 | T1
91 |
92 [cols="^,^",frame="none"]
93 !========
94 ! T2 ! T3
95 !========
96 |========
97
98 Furthermore, you can freely resize table cells.
99
100 == Files
101
102 include/data.h::
103 Contains data definitions used by nearly all files. You really need to read this first.
104
105 include/*.h::
106 Contains forward definitions for all public functions, aswell as doxygen-compatible
107 comments (so if you want to get a bit more of the big picture, either browse all
108 header files or use doxygen if you prefer that).
109
110 src/client.c::
111 Contains all functions which are specific to a certain client (make it
112 fullscreen, see if its class/name matches a pattern, kill it, …).
113
114 src/commands.c::
115 Parsing commands and actually execute them (focussing, moving, …).
116
117 src/config.c::
118 Parses the configuration file.
119
120 src/debug.c::
121 Contains debugging functions to print unhandled X events.
122
123 src/floating.c::
124 Contains functions for floating mode (mostly resizing/dragging).
125
126 src/handlers.c::
127 Contains all handlers for all kind of X events (new window title, new hints,
128 unmapping, key presses, button presses, …).
129
130 src/ipc.c::
131 Contains code for the IPC interface.
132
133 src/layout.c::
134 Renders your layout (screens, workspaces, containers).
135
136 src/mainx.c::
137 Initializes the window manager.
138
139 src/manage.c::
140 Looks at existing or new windows and decides whether to manage them. If so, it
141 reparents the window and inserts it into our data structures.
142
143 src/resize.c::
144 Contains the functions to resize columns/rows in the table.
145
146 src/resize.c::
147 Contains the functions to resize columns/rows in the table.
148
149 src/table.c::
150 Manages the most important internal data structure, the design table.
151
152 src/util.c::
153 Contains useful functions which are not really dependant on anything.
154
155 src/workspace.c::
156 Contains all functions related to workspaces (displaying, hiding, renaming…)
157
158 src/xcb.c::
159 Contains wrappers to use xcb more easily.
160
161 src/xinerama.c::
162 (Re-)initializes the available screens and converts them to virtual screens (see below).
163
164 == Data structures
165
166 See include/data.h for documented data structures. The most important ones are explained
167 right here.
168
169 image:bigpicture.png[The Big Picture]
170
171 So, the hierarchy is:
172
173 . *Virtual screens* (Screen 0 in this example)
174 . *Workspaces* (Workspace 1 in this example)
175 . *Table* (There can only be one table per Workspace)
176 . *Container* (left and right in this example)
177 . *Client* (The two clients in the left container)
178
179 === Virtual screens
180
181 A virtual screen (type `i3Screen`) is generated from the connected screens obtained
182 through Xinerama. The difference to the raw Xinerama monitors as seen when using +xrandr(1)+
183 is that it falls back to the lowest common resolution of the logical screens.
184
185 For example, if your notebook has 1280x800 and you connect a video projector with
186 1024x768, set up in clone mode (+xrandr \--output VGA \--mode 1024x768 \--same-as LVDS+),
187 i3 will have one virtual screen.
188
189 However, if you configure it using +xrandr \--output VGA \--mode 1024x768 \--right-of LVDS+,
190 i3 will generate two virtual screens. For each virtual screen, a new workspace will be
191 assigned. New workspaces are created on the screen you are currently on.
192
193 === Workspace
194
195 A workspace is identified by its number. Basically, you could think of workspaces
196 as different desks in your bureau, if you like the desktop methaphor. They just contain
197 different sets of windows and are completely separate of each other. Other window
198 managers also call this ``Virtual desktops''.
199
200 === The layout table
201
202 Each workspace has a table, which is just a two-dimensional dynamic array containing
203 Containers (see below). This table grows and shrinks as you need it (by moving windows
204 to the right you can create a new column in the table, by moving them to the bottom
205 you create a new row).
206
207 === Container
208
209 A container is the content of a table’s cell. It holds an arbitrary amount of windows
210 and has a specific layout (default layout or stack layout). Containers can consume
211 multiple table cells by modifying their colspan/rowspan attribute.
212
213 === Client
214
215 A client is x11-speak for a window.
216
217 == List/queue macros
218
219 i3 makes heavy use of the list macros defined in BSD operating systems. To ensure
220 that the operating system on which i3 is compiled has all the awaited features,
221 i3 comes with `include/queue.h`. On BSD systems, you can use man `queue(3)`. On Linux,
222 you have to use google.
223
224 The lists used are `SLIST` (single linked lists) and `CIRCLEQ` (circular queues).
225 Usually, only forward traversal is necessary, so an `SLIST` works fine. However,
226 for the windows inside a container, a `CIRCLEQ` is necessary to go from the currently
227 selected window to the window above/below.
228
229 == Naming conventions
230
231 There is a row of standard variables used in many events. The following names should be
232 chosen for those:
233
234  * ``conn'' is the xcb_connection_t
235  * ``event'' is the event of the particular type
236  * ``container'' names a container
237  * ``client'' names a client, for example when using a +CIRCLEQ_FOREACH+
238
239 == Startup (src/mainx.c, main())
240
241  * Establish the xcb connection
242  * Check for XKB extension on the separate X connection
243  * Check for Xinerama screens
244  * Grab the keycodes for which bindings exist
245  * Manage all existing windows
246  * Enter the event loop
247
248 == Keybindings
249
250 === Grabbing the bindings
251
252 Grabbing the bindings is quite straight-forward. You pass X your combination of modifiers and
253 the keycode you want to grab and whether you want to grab them actively or passively. Most
254 bindings (everything except for bindings using Mode_switch) are grabbed passively, that is,
255 just the window manager gets the event and cannot replay it.
256
257 We need to grab bindings that use Mode_switch actively because of a bug in X. When the window
258 manager receives the keypress/keyrelease event for an actively grabbed keycode, it has to decide
259 what to do with this event: It can either replay it so that other applications get it or it
260 can prevent other applications from receiving it.
261
262 So, why do we need to grab keycodes actively? Because X does not set the state-property of
263 keypress/keyrelease events properly. The Mode_switch bit is not set and we need to get it
264 using XkbGetState. This means we cannot pass X our combination of modifiers containing Mode_switch
265 when grabbing the key and therefore need to grab the keycode itself without any modiffiers.
266 This means, if you bind Mode_switch + keycode 38 ("a"), i3 will grab keycode 38 ("a") and
267 check on each press of "a" if the Mode_switch bit is set using XKB. If yes, it will handle
268 the event, if not, it will replay the event.
269
270 === Handling a keypress
271
272 As mentioned in "Grabbing the bindings", upon a keypress event, i3 first gets the correct state.
273
274 Then, it looks through all bindings and gets the one which matches the received event.
275
276 The bound command is parsed directly in command mode.
277
278 == Manage windows (src/mainx.c, manage_window() and reparent_window())
279
280 `manage_window()` does some checks to decide whether the window should be managed at all:
281
282  * Windows have to be mapped, that is, visible on screen
283  * The override_redirect must not be set. Windows with override_redirect shall not be
284    managed by a window manager
285
286 Afterwards, i3 gets the intial geometry and reparents the window if it wasn’t already
287 managed.
288
289 Reparenting means that for each window which is reparented, a new window, slightly larger
290 than the original one, is created. The original window is then reparented to the bigger one
291 (called "frame").
292
293 After reparenting, the window type (`_NET_WM_WINDOW_TYPE`) is checked to see whether this
294 window is a dock (`_NET_WM_WINDOW_TYPE_DOCK`), like dzen2 for example. Docks are handled
295 differently, they don’t have decorations and are not assigned to a specific container.
296 Instead, they are positioned at the bottom of the screen. To get the height which needsd
297 to be reserved for the window, the `_NET_WM_STRUT_PARTIAL` property is used.
298
299 == What happens when an application is started?
300
301 i3 does not care for applications. All it notices is when new windows are mapped (see
302 `src/handlers.c`, `handle_map_notify_event()`). The window is then reparented (see section
303 "Manage windows").
304
305 After reparenting the window, `render_layout()` is called which renders the internal
306 layout table. The window was placed in the currently focused container and
307 therefore the new window and the old windows (if any) need te be moved/resized
308 so that the currently active layout (default mode/stacking mode) is rendered
309 correctly. To move/resize windows, a window is ``configured'' in X11-speak.
310
311 Some applications, such as MPlayer obivously assume the window manager is stupid
312 and therefore configure their windows by themselves. This generates an event called
313 configurenotify. i3 handles these events and pushes the window back to its position/size.
314
315 == _NET_WM_STATE
316
317 Only the _NET_WM_STATE_FULLSCREEN atom is handled. It calls ``toggle_fullscreen()'' for the
318 specific client which just configures the client to use the whole screen on which it
319 currently is. Also, it is set as fullscreen_client for the i3Screen.
320
321 == WM_NAME
322
323 When the WM_NAME property of a window changes, its decoration (containing the title)
324 is re-rendered.
325
326 == Size hints
327
328 Size hints specify the minimum/maximum size for a given window aswell as its aspect ratio.
329 At the moment, as i3 does not have a floating mode yet, only the aspect ratio is parsed.
330 This is important for clients like mplayer, who only set the aspect ratio and resize their
331 window to be as small as possible (but only with some video outputs, for example in Xv,
332 while when using x11, mplayer does the necessary centering for itself).
333
334 So, when an aspect ratio was specified, i3 adjusts the height of the window until the
335 size maintains the correct aspect ratio. For the code to do this, see src/layout.c,
336 function resize_client().
337
338 == Rendering (src/layout.c, render_layout() and render_container())
339
340 There are two entry points to rendering: render_layout() and render_container(). The
341 former one renders all virtual screens, the currently active workspace of each virtual
342 screen and all containers (inside the table cells) of these workspaces using
343 render_container(). Therefore, if you need to render only a single container, for
344 example because a window was removed, added or changed its title, you should directly
345 call render_container().
346
347 Rendering consists of two steps: In the first one, in render_layout(), each container
348 gets its position (screen offset + offset in the table) and size (container's width
349 times colspan/rowspan). Then, render_container() is called:
350
351 render_container() then takes different approaches, depending on the mode the container
352 is in.
353
354 === Common parts
355
356 On the frame (the window which was created around the client’s window for the decorations),
357 a black rectangle is drawn as a background for windows like MPlayer, which don’t completely
358 fit into the frame.
359
360 === Default mode
361
362 Each clients gets the container’s width and an equal amount of height.
363
364 === Stack mode
365
366 In stack mode, a window containing the decorations of all windows inside the container
367 is placed at the top. The currently focused window is then given the whole remaining
368 space.
369
370 === Window decorations
371
372 The window decorations consist of a rectangle in the appropriate color (depends on whether
373 this window is the currently focused one or the last focused one in a not focused container
374 or not focused at all) forming the background. Afterwards, two lighter lines are drawn
375 and the last step is drawing the window’s title (see WM_NAME) onto it.
376
377 === Fullscreen windows
378
379 For fullscreen windows, the `rect` (x, y, width, height) is not changed to allow the client
380 to easily go back to its previous position. Instead, fullscreen windows are skipped
381 when rendering.
382
383 === Resizing containers
384
385 By clicking and dragging the border of a container, you can resize the whole column
386 (respectively row) which this container is in. This is necessary to keep the table
387 layout working and consistent.
388
389 Currently, only vertical resizing is implemented.
390
391 The resizing works similarly to the resizing of floating windows or movement of floating
392 windows:
393
394 * A new, invisible window with the size of the root window is created (+grabwin+)
395 * Another window, 2px width and as high as your screen (or vice versa for horizontal
396   resizing) is created. Its background color is the border color and it is only
397   there to signalize the user how big the container will be (it creates the impression
398   of dragging the border out of the container).
399 * The +drag_pointer+ function of +src/floating.c+ is called to grab the pointer and
400   enter an own event loop which will pass all events (expose events) but motion notify
401   events. This function then calls the specified callback (+resize_callback+) which
402   does some boundary checking and moves the helper window. As soon as the mouse
403   button is released, this loop will be terminated.
404 * The new width_factor for each involved column (respectively row) will be calculated.
405
406 == User commands / commandmode (src/commands.c)
407
408 Like in vim, you can control i3 using commands. They are intended to be a powerful
409 alternative to lots of shortcuts, because they can be combined. There are a few special
410 commands, which are the following:
411
412 exec <command>::
413 Starts the given command by passing it to `/bin/sh`.
414
415 restart::
416 Restarts i3 by executing `argv[0]` (the path with which you started i3) without forking.
417
418 w::
419 "With". This is used to select a bunch of windows. Currently, only selecting the whole
420 container in which the window is in, is supported by specifying "w".
421
422 f, s, d::
423 Toggle fullscreen, stacking, default mode for the current window/container.
424
425 The other commands are to be combined with a direction. The directions are h, j, k and l,
426 like in vim (h = left, j = down, k = up, l = right). When you just specify the direction
427 keys, i3 will move the focus in that direction. You can provide "m" or "s" before the
428 direction to move a window respectively or snap.
429
430 == Gotchas
431
432 * Forgetting to call `xcb_flush(conn);` after sending a request. This usually leads to
433   code which looks like it works fine but which does not work under certain conditions.
434
435 == Using git / sending patches
436
437 For a short introduction into using git, see http://www.spheredev.org/wiki/Git_for_the_lazy
438 or, for more documentation, see http://git-scm.com/documentation
439
440 When you want to send a patch because you fixed a bug or implemented a cool feature (please
441 talk to us before working on features to see whether they are maybe already implemented, not
442 possible because of some reason or don’t fit into the concept), please use git to create
443 a patchfile.
444
445 First of all, update your working copy to the latest version of the master branch:
446
447 --------
448 git pull
449 --------
450
451 Afterwards, make the necessary changes for your bugfix/feature. Then, review the changes
452 using +git diff+ (you might want to enable colors in the diff using +git config diff.color auto+).
453 When you are definitely done, use +git commit -a+ to commit all changes you’ve made.
454
455 Then, use the following command to generate a patchfile which we can directly apply to
456 the branch, preserving your commit message and name:
457
458 -----------------------
459 git format-patch origin
460 -----------------------
461
462 Just send us the generated file via mail.