]> git.sur5r.net Git - i3/i3/blob - docs/hacking-howto
More work on hacking-howto
[i3/i3] / docs / hacking-howto
1 Hacking i3: How To
2 ==================
3 Michael Stapelberg <michael+i3@stapelberg.de>
4 March 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.
107
108 src/commands.c::
109 Parsing commands
110
111 src/config.c::
112 Parses the configuration file
113
114 src/debug.c::
115 Contains debugging functions to print unhandled X events
116
117 src/handlers.c::
118 Contains all handlers for all kind of X events
119
120 src/layout.c::
121 Renders your layout (screens, workspaces, containers)
122
123 src/mainx.c::
124 Initializes the window manager
125
126 src/table.c::
127 Manages the most important internal data structure, the design table.
128
129 src/util.c::
130 Contains useful functions which are not really dependant on anything.
131
132 src/xcb.c::
133 Contains wrappers to use xcb more easily.
134
135 src/xinerama.c::
136 (Re-)initializes the available screens and converts them to virtual screens (see below).
137
138 == Data structures
139
140 See include/data.h for documented data structures. The most important ones are explained
141 right here.
142
143 TODO: We need a slick graphic here
144
145 === Virtual screens
146
147 A virtual screen (type `i3Screen`) is generated from the connected screens obtained
148 through Xinerama. The difference to the raw Xinerama monitors as seen when using xrandr(1)
149 is that it falls back to the lowest common resolution of the logical screens.
150
151 For example, if your notebook has 1280x800 and you connect a video projector with
152 1024x768, set up in clone mode (xrandr \--output VGA \--mode 1024x768 \--same-as LVDS),
153 i3 will have one virtual screen.
154
155 However, if you configure it using xrandr \--output VGA \--mode 1024x768 \--right-of LVDS,
156 i3 will generate two virtual screens. For each virtual screen, a new workspace will be
157 assigned. New workspaces are created on the screen you are currently on.
158
159 === Workspace
160
161 A workspace is identified by its number. Basically, you could think of workspaces
162 as different desks in your bureau, if you like the desktop methaphor. They just contain
163 different sets of windows and are completely separate of each other. Other window
164 managers also call this ``Virtual desktops''.
165
166 === The layout table
167
168 Each workspace has a table, which is just a two-dimensional dynamic array containing
169 Containers (see below). This table grows and shrinks as you need it (by moving windows
170 to the right you can create a new column in the table, by moving them to the bottom
171 you create a new row).
172
173 === Container
174
175 A container is the content of a table’s cell. It holds an arbitrary amount of windows
176 and has a specific layout (default layout or stack layout). Containers can consume
177 multiple table cells by modifying their colspan/rowspan attribute.
178
179 === Client
180
181 A client is x11-speak for a window.
182
183 == List/queue macros
184
185 i3 makes heavy use of the list macros defined in BSD operating systems. To ensure
186 that the operating system on which i3 is compiled has all the awaited features,
187 i3 comes with `include/queue.h`. On BSD systems, you can use man `queue(3)`. On Linux,
188 you have to use google.
189
190 The lists used are `SLIST` (single linked lists) and `CIRCLEQ` (circular queues).
191 Usually, only forward traversal is necessary, so an `SLIST` works fine. However,
192 for the windows inside a container, a `CIRCLEQ` is necessary to go from the currently
193 selected window to the window above/below.
194
195 == Naming conventions
196
197 There is a row of standard variables used in many events. The following names should be
198 chosen for those:
199
200  * ``conn'' is the xcb_connection_t
201  * ``event'' is the event of the particular type
202  * ``container'' names a container
203  * ``client'' names a client, for example when using a `CIRCLEQ_FOREACH`
204
205 == Startup (src/mainx.c, main())
206
207  * Establish the xcb connection
208  * Check for XKB extension on the separate X connection
209  * Check for Xinerama screens
210  * Grab the keycodes for which bindings exist
211  * Manage all existing windows
212  * Enter the event loop
213
214 == Keybindings
215
216 === Grabbing the bindings
217
218 Grabbing the bindings is quite straight-forward. You pass X your combination of modifiers and
219 the keycode you want to grab and whether you want to grab them actively or passively. Most
220 bindings (everything except for bindings using Mode_switch) are grabbed passively, that is,
221 just the window manager gets the event and cannot replay it.
222
223 We need to grab bindings that use Mode_switch actively because of a bug in X. When the window
224 manager receives the keypress/keyrelease event for an actively grabbed keycode, it has to decide
225 what to do with this event: It can either replay it so that other applications get it or it
226 can prevent other applications from receiving it.
227
228 So, why do we need to grab keycodes actively? Because X does not set the state-property of
229 keypress/keyrelease events properly. The Mode_switch bit is not set and we need to get it
230 using XkbGetState. This means we cannot pass X our combination of modifiers containing Mode_switch
231 when grabbing the key and therefore need to grab the keycode itself without any modiffiers.
232 This means, if you bind Mode_switch + keycode 38 ("a"), i3 will grab keycode 38 ("a") and
233 check on each press of "a" if the Mode_switch bit is set using XKB. If yes, it will handle
234 the event, if not, it will replay the event.
235
236 === Handling a keypress
237
238 As mentioned in "Grabbing the bindings", upon a keypress event, i3 first gets the correct state.
239
240 Then, it looks through all bindings and gets the one which matches the received event.
241
242 The bound command is parsed directly in command mode.
243
244 == Manage windows (src/mainx.c, manage_window() and reparent_window())
245
246 `manage_window()` does some checks to decide whether the window should be managed at all:
247
248  * Windows have to be mapped, that is, visible on screen
249  * The override_redirect must not be set. Windows with override_redirect shall not be
250    managed by a window manager
251
252 Afterwards, i3 gets the intial geometry and reparents the window if it wasn’t already
253 managed.
254
255 Reparenting means that for each window which is reparented, a new window, slightly larger
256 than the original one, is created. The original window is then reparented to the bigger one
257 (called "frame").
258
259 After reparenting, the window type (`_NET_WM_WINDOW_TYPE`) is checked to see whether this
260 window is a dock (`_NET_WM_WINDOW_TYPE_DOCK`), like dzen2 for example. Docks are handled
261 differently, they don’t have decorations and are not assigned to a specific container.
262 Instead, they are positioned at the bottom of the screen. To get the height which needsd
263 to be reserved for the window, the `_NET_WM_STRUT_PARTIAL` property is used.
264
265 == What happens when an application is started?
266
267 i3 does not care for applications. All it notices is when new windows are mapped (see
268 `src/handlers.c`, `handle_map_notify_event()`). The window is then reparented (see section
269 "Manage windows").
270
271 After reparenting the window, `render_layout()` is called which renders the internal
272 layout table. The window was placed in the currently focused container and
273 therefore the new window and the old windows (if any) need te be moved/resized
274 so that the currently active layout (default mode/stacking mode) is rendered
275 correctly. To move/resize windows, a window is ``configured'' in X11-speak.
276
277 Some applications, such as MPlayer obivously assume the window manager is stupid
278 and therefore configure their windows by themselves. This generates an event called
279 configurenotify. i3 handles these events and pushes the window back to its position/size.
280
281 == _NET_WM_STATE
282
283 Only the _NET_WM_STATE_FULLSCREEN atom is handled. It calls ``toggle_fullscreen()'' for the
284 specific client which just configures the client to use the whole screen on which it
285 currently is. Also, it is set as fullscreen_client for the i3Screen.
286
287 == WM_NAME
288
289 When the WM_NAME property of a window changes, its decoration (containing the title)
290 is re-rendered.
291
292 == Size hints
293
294 TODO
295
296 == Rendering (src/layout.c, render_layout() and render_container())
297
298 There are two entry points to rendering: render_layout() and render_container(). The
299 former one renders all virtual screens, the currently active workspace of each virtual
300 screen and all containers (inside the table cells) of these workspaces using
301 render_container(). Therefore, if you need to render only a single container, for
302 example because a window was removed, added or changed its title, you should directly
303 call render_container().
304
305 Rendering consists of two steps: In the first one, in render_layout(), each container
306 gets its position (screen offset + offset in the table) and size (container's width
307 times colspan/rowspan). Then, render_container() is called:
308
309 render_container() then takes different approaches, depending on the mode the container
310 is in.
311
312 === Common parts
313
314 On the frame (the window which was created around the client’s window for the decorations),
315 a black rectangle is drawn as a background for windows like MPlayer, which don’t completely
316 fit into the frame.
317
318 === Default mode
319
320 Each clients gets the container’s width and an equal amount of height.
321
322 === Stack mode
323
324 In stack mode, a window containing the decorations of all windows inside the container
325 is placed at the top. The currently focused window is then given the whole remaining
326 space.
327
328 === Window decorations
329
330 The window decorations consist of a rectangle in the appropriate color (depends on whether
331 this window is the currently focused one or the last focused one in a not focused container
332 or not focused at all) forming the background. Afterwards, two lighter lines are drawn
333 and the last step is drawing the window’s title (see WM_NAME) onto it.
334
335 === Fullscreen windows
336
337 For fullscreen windows, the `rect` (x, y, width, height) is not changed to allow the client
338 to easily go back to its previous position. Instead, fullscreen windows are skipped
339 when rendering.
340
341 === Resizing containers
342
343 By clicking and dragging the border of a container, you can resize it freely.
344
345 TODO
346
347 == User commands / commandmode (src/commands.c)
348
349 Like in vim, you can control i3 using commands. They are intended to be a powerful
350 alternative to lots of shortcuts, because they can be combined. There are a few special
351 commands, which are the following:
352
353 exec <command>::
354 Starts the given command by passing it to `/bin/sh`.
355
356 restart::
357 Restarts i3 by executing `argv[0]` (the path with which you started i3) without forking.
358
359 w::
360 "With". This is used to select a bunch of windows. Currently, only selecting the whole
361 container in which the window is in, is supported by specifying "w".
362
363 f, s, d::
364 Toggle fullscreen, stacking, default mode for the current window/container.
365
366 The other commands are to be combined with a direction. The directions are h, j, k and l,
367 like in vim (h = left, j = down, k = up, l = right). When you just specify the direction
368 keys, i3 will move the focus in that direction. You can provide "m" or "s" before the
369 direction to move a window respectively or snap.
370
371 == Gotchas
372
373 * Forgetting to call `xcb_flush(conn);` after sending a request. This usually leads to
374   code which looks like it works fine but which does not work under certain conditions.
375