]> git.sur5r.net Git - i3/i3/blob - docs/hacking-howto
Merge branch 'master' into next
[i3/i3] / docs / hacking-howto
1 Hacking i3: How To
2 ==================
3 Michael Stapelberg <michael+i3@stapelberg.de>
4 July 2011
5
6 This document is intended to be the first thing you read before looking and/or
7 touching i3’s source code. It should contain all important information to help
8 you understand why things are like they are. If it does not mention something
9 you find necessary, please 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
14 combination with X to facilitate some things. The window manager's job is to
15 take care of the placement of windows, to provide the user with some mechanisms
16 to change the position/size of windows and to communicate with clients to a
17 certain extent (for example handle fullscreen requests of clients such as
18 MPlayer).
19
20 There are no different contexts in which X11 clients run, so a window manager
21 is just another client, like all other X11 applications. However, it handles
22 some events which normal clients usually don’t handle.
23
24 In the case of i3, the tasks (and order of them) are the following:
25
26 . Grab the key bindings (events will be sent upon keypress/keyrelease)
27 . Iterate through all existing windows (if the window manager is not started as
28   the first client of X) and manage them (reparent them, create window
29   decorations, etc.)
30 . When new windows are created, manage them
31 . Handle the client’s `_WM_STATE` property, but only the `_WM_STATE_FULLSCREEN`
32 . Handle the client’s `WM_NAME` property
33 . Handle the client’s size hints to display them proportionally
34 . Handle the client’s urgency hint
35 . Handle enter notifications (focus follows mouse)
36 . Handle button (as in mouse buttons) presses for focus/raise on click
37 . Handle expose events to re-draw own windows such as decorations
38 . React to the user’s commands: Change focus, Move windows, Switch workspaces,
39   Change the layout mode of a container (default/stacking/tabbed), start a new
40   application, restart the window manager
41
42 In the following chapters, each of these tasks and their implementation details
43 will be discussed.
44
45 === Tiling window managers
46
47 Traditionally, there are two approaches to managing windows: The most common
48 one nowadays is floating, which means the user can freely move/resize the
49 windows. The other approach is called tiling, which means that your window
50 manager distributes windows to use as much space as possible while not
51 overlapping each other.
52
53 The idea behind tiling is that you should not need to waste your time
54 moving/resizing windows while you usually want to get some work done. After
55 all, most users sooner or later tend to lay out their windows in a way which
56 corresponds to tiling or stacking mode in i3. Therefore, why not let i3 do this
57 for you? Certainly, it’s faster than you could ever do it.
58
59 The problem with most tiling window managers is that they are too unflexible.
60 In my opinion, a window manager is just another tool, and similar to vim which
61 can edit all kinds of text files (like source code, HTML, …) and is not limited
62 to a specific file type, a window manager should not limit itself to a certain
63 layout (like dwm, awesome, …) but provide mechanisms for you to easily create
64 the layout you need at the moment.
65
66 === The layout table
67
68 *********************************************************************************
69 This section has not been updated for v4.0 yet, sorry! We wanted to release on
70 time, but we will update this soon. Please talk to us on IRC if you need to
71 know stuff *NOW* :).
72 *********************************************************************************
73
74 /////////////////////////////////////////////////////////////////////////////////
75 To accomplish flexible layouts, we decided to simply use a table. The table
76 grows and shrinks as you need it. Each cell holds a container which then holds
77 windows (see picture below). You can use different layouts for each container
78 (default layout and stacking layout).
79
80 So, when you open a terminal and immediately open another one, they reside in
81 the same container, in default layout. The layout table has exactly one column,
82 one row and therefore one cell. When you move one of the terminals to the
83 right, the table needs to grow. It will be expanded to two columns and one row.
84 This enables you to have different layouts for each container. The table then
85 looks like this:
86
87 [width="15%",cols="^,^"]
88 |========
89 | T1 | T2
90 |========
91
92 When moving terminal 2 to the bottom, the table will be expanded again.
93
94 [width="15%",cols="^,^"]
95 |========
96 | T1 |
97 |    | T2
98 |========
99
100 You can really think of the layout table like a traditional HTML table, if
101 you’ve ever designed one. Especially col- and rowspan work similarly. Below,
102 you see an example of colspan=2 for the first container (which has T1 as
103 window).
104
105 [width="15%",cols="^asciidoc"]
106 |========
107 | T1
108 |
109 [cols="^,^",frame="none"]
110 !========
111 ! T2 ! T3
112 !========
113 |========
114
115 Furthermore, you can freely resize table cells.
116 /////////////////////////////////////////////////////////////////////////////////
117
118 == Files
119
120 include/atoms.xmacro::
121 A file containing all X11 atoms which i3 uses. This file will be included
122 various times (for defining, requesting and receiving the atoms), each time
123 with a different definition of xmacro().
124
125 include/data.h::
126 Contains data definitions used by nearly all files. You really need to read
127 this first.
128
129 include/*.h::
130 Contains forward definitions for all public functions, as well as
131 doxygen-compatible comments (so if you want to get a bit more of the big
132 picture, either browse all header files or use doxygen if you prefer that).
133
134 src/cfgparse.l::
135 Contains the lexer for i3’s configuration file, written for +flex(1)+.
136
137 src/cfgparse.y::
138 Contains the parser for i3’s configuration file, written for +bison(1)+.
139
140 src/click.c::
141 Contains all functions which handle mouse button clicks (right mouse button
142 clicks initiate resizing and thus are relatively complex).
143
144 src/cmdparse.l::
145 Contains the lexer for i3 commands, written for +flex(1)+.
146
147 src/cmdparse.y::
148 Contains the parser for i3 commands, written for +bison(1)+.
149
150 src/con.c::
151 Contains all functions which deal with containers directly (creating
152 containers, searching containers, getting specific properties from containers,
153 …).
154
155 src/config.c::
156 Contains all functions handling the configuration file (calling the parser
157 (src/cfgparse.y) with the correct path, switching key bindings mode).
158
159 src/debug.c::
160 Contains debugging functions to print unhandled X events.
161
162 src/ewmh.c::
163 Functions to get/set certain EWMH properties easily.
164
165 src/floating.c::
166 Contains functions for floating mode (mostly resizing/dragging).
167
168 src/handlers.c::
169 Contains all handlers for all kinds of X events (new window title, new hints,
170 unmapping, key presses, button presses, …).
171
172 src/ipc.c::
173 Contains code for the IPC interface.
174
175 src/load_layout.c::
176 Contains code for loading layouts from JSON files.
177
178 src/log.c::
179 Handles the setting of loglevels, contains the logging functions.
180
181 src/main.c::
182 Initializes the window manager.
183
184 src/manage.c::
185 Looks at existing or new windows and decides whether to manage them. If so, it
186 reparents the window and inserts it into our data structures.
187
188 src/match.c::
189 A "match" is a data structure which acts like a mask or expression to match
190 certain windows or not. For example, when using commands, you can specify a
191 command like this: [title="*Firefox*"] kill. The title member of the match
192 data structure will then be filled and i3 will check each window using
193 match_matches_window() to find the windows affected by this command.
194
195 src/move.c::
196 Contains code to move a container in a specific direction.
197
198 src/output.c::
199 Functions to handle CT_OUTPUT cons.
200
201 src/randr.c::
202 The RandR API is used to get (and re-query) the configured outputs (monitors,
203 …).
204
205 src/render.c::
206 Renders the tree data structure by assigning coordinates to every node. These
207 values will later be pushed to X11 in +src/x.c+.
208
209 src/resize.c::
210 Contains the functions to resize containers.
211
212 src/sighandler.c::
213 Handles +SIGSEGV+, +SIGABRT+ and +SIGFPE+ by showing a dialog that i3 crashed.
214 You can chose to let it dump core, to restart it in-place or to restart it
215 in-place but forget about the layout.
216
217 src/tree.c::
218 Contains functions which open or close containers in the tree, change focus or
219 cleanup ("flatten") the tree. See also +src/move.c+ for another similar
220 function, which was moved into its own file because it is so long.
221
222 src/util.c::
223 Contains useful functions which are not really dependant on anything.
224
225 src/window.c::
226 Handlers to update X11 window properties like +WM_CLASS+, +_NET_WM_NAME+,
227 +CLIENT_LEADER+, etc.
228
229 src/workspace.c::
230 Contains all functions related to workspaces (displaying, hiding, renaming…)
231
232 src/x.c::
233 Transfers our in-memory tree (see +src/render.c+) to X11.
234
235 src/xcb.c::
236 Contains wrappers to use xcb more easily.
237
238 src/xcursor.c::
239 XCursor functions (for cursor themes).
240
241 src/xinerama.c::
242 Legacy support for Xinerama. See +src/randr.c+ for the preferred API.
243
244 == Data structures
245
246
247 See include/data.h for documented data structures. The most important ones are
248 explained right here.
249
250 /////////////////////////////////////////////////////////////////////////////////
251 // TODO: update image
252
253 image:bigpicture.png[The Big Picture]
254
255 /////////////////////////////////////////////////////////////////////////////////
256
257 So, the hierarchy is:
258
259 . *X11 root window*, the root container
260 . *Output container* (LVDS1 in this example)
261 . *Content container* (there are also containers for dock windows)
262 . *Workspaces* (Workspace 1 in this example, with horizontal orientation)
263 . *Split container* (vertically split)
264 . *X11 window containers*
265
266 The data type is +Con+, in all cases.
267
268 === X11 root window
269
270 The X11 root window is a single window per X11 display (a display is identified
271 by +:0+ or +:1+ etc.). The root window is what you draw your background image
272 on. It spans all the available outputs, e.g. +VGA1+ is a specific part of the
273 root window and +LVDS1+ is a specific part of the root window.
274
275 === Output container
276
277 Every active output obtained through RandR is represented by one output
278 container. Outputs are considered active when a mode is configured (meaning
279 something is actually displayed on the output) and the output is not a clone.
280
281 For example, if your notebook has a screen resolution of 1280x800 px and you
282 connect a video projector with a resolution of 1024x768 px, set it up in clone
283 mode (+xrandr \--output VGA1 \--mode 1024x768 \--same-as LVDS1+), i3 will
284 reduce the resolution to the lowest common resolution and disable one of the
285 cloned outputs afterwards.
286
287 However, if you configure it using +xrandr \--output VGA1 \--mode 1024x768
288 \--right-of LVDS1+, i3 will set both outputs active. For each output, a new
289 workspace will be assigned. New workspaces are created on the output you are
290 currently on.
291
292 === Content container
293
294 Each output has multiple children. Two of them are dock containers which hold
295 dock clients. The other one is the content container, which holds the actual
296 content (workspaces) of this output.
297
298 === Workspace
299
300 A workspace is identified by its name. Basically, you could think of
301 workspaces as different desks in your office, if you like the desktop
302 metaphor. They just contain different sets of windows and are completely
303 separate of each other. Other window managers also call this ``Virtual
304 desktops''.
305
306 === Split container
307
308 A split container is a container which holds an arbitrary amount of split
309 containers or X11 window containers. It has an orientation (horizontal or
310 vertical) and a layout.
311
312 Split containers (and X11 window containers, which are a subtype of split
313 containers) can have different border styles.
314
315 === X11 window container
316
317 An X11 window container holds exactly one X11 window. These are the leaf nodes
318 of the layout tree, they cannot have any children.
319
320 == List/queue macros
321
322 i3 makes heavy use of the list macros defined in BSD operating systems. To
323 ensure that the operating system on which i3 is compiled has all the expected
324 features, i3 comes with `include/queue.h`. On BSD systems, you can use man
325 `queue(3)`. On Linux, you have to use google (or read the source).
326
327 The lists used are +SLIST+ (single linked lists), +CIRCLEQ+ (circular
328 queues) and +TAILQ+ (tail queues). Usually, only forward traversal is necessary,
329 so an `SLIST` works fine. If inserting elements at arbitrary positions or at
330 the end of a list is necessary, a +TAILQ+ is used instead. However, for the
331 windows inside a container, a +CIRCLEQ+ is necessary to go from the currently
332 selected window to the window above/below.
333
334 == Naming conventions
335
336 There is a row of standard variables used in many events. The following names
337 should be chosen for those:
338
339  * ``conn'' is the xcb_connection_t
340  * ``event'' is the event of the particular type
341  * ``con'' names a container
342  * ``current'' is a loop variable when using +TAILQ_FOREACH+ etc.
343
344 == Startup (src/mainx.c, main())
345
346  * Establish the xcb connection
347  * Check for XKB extension on the separate X connection, load Xcursor
348  * Check for RandR screens (with a fall-back to Xinerama)
349  * Grab the keycodes for which bindings exist
350  * Manage all existing windows
351  * Enter the event loop
352
353 == Keybindings
354
355 === Grabbing the bindings
356
357 Grabbing the bindings is quite straight-forward. You pass X your combination of
358 modifiers and the keycode you want to grab and whether you want to grab them
359 actively or passively. Most bindings (everything except for bindings using
360 Mode_switch) are grabbed passively, that is, just the window manager gets the
361 event and cannot replay it.
362
363 We need to grab bindings that use Mode_switch actively because of a bug in X.
364 When the window manager receives the keypress/keyrelease event for an actively
365 grabbed keycode, it has to decide what to do with this event: It can either
366 replay it so that other applications get it or it can prevent other
367 applications from receiving it.
368
369 So, why do we need to grab keycodes actively? Because X does not set the
370 state-property of keypress/keyrelease events properly. The Mode_switch bit is
371 not set and we need to get it using XkbGetState. This means we cannot pass X
372 our combination of modifiers containing Mode_switch when grabbing the key and
373 therefore need to grab the keycode itself without any modifiers. This means,
374 if you bind Mode_switch + keycode 38 ("a"), i3 will grab keycode 38 ("a") and
375 check on each press of "a" if the Mode_switch bit is set using XKB. If yes, it
376 will handle the event, if not, it will replay the event.
377
378 === Handling a keypress
379
380 As mentioned in "Grabbing the bindings", upon a keypress event, i3 first gets
381 the correct state.
382
383 Then, it looks through all bindings and gets the one which matches the received
384 event.
385
386 The bound command is parsed by the cmdparse lexer/parser, see +parse_cmd+ in
387 +src/cmdparse.y+.
388
389 == Manage windows (src/main.c, manage_window() and reparent_window())
390
391 `manage_window()` does some checks to decide whether the window should be
392 managed at all:
393
394  * Windows have to be mapped, that is, visible on screen
395  * The override_redirect must not be set. Windows with override_redirect shall
396    not be managed by a window manager
397
398 Afterwards, i3 gets the intial geometry and reparents the window (see
399 `reparent_window()`) if it wasn’t already managed.
400
401 Reparenting means that for each window which is reparented, a new window,
402 slightly larger than the original one, is created. The original window is then
403 reparented to the bigger one (called "frame").
404
405 After reparenting, the window type (`_NET_WM_WINDOW_TYPE`) is checked to see
406 whether this window is a dock (`_NET_WM_WINDOW_TYPE_DOCK`), like dzen2 for
407 example. Docks are handled differently, they don’t have decorations and are not
408 assigned to a specific container. Instead, they are positioned at the bottom
409 or top of the screen (in the appropriate dock area containers). To get the
410 height which needs to be reserved for the window, the `_NET_WM_STRUT_PARTIAL`
411 property is used.
412
413 Furthermore, the list of assignments (to other workspaces, which may be on
414 other screens) is checked. If the window matches one of the user’s criteria,
415 it may either be put in floating mode or moved to a different workspace. If the
416 target workspace is not visible, the window will not be mapped.
417
418 == What happens when an application is started?
419
420 i3 does not care for applications. All it notices is when new windows are
421 mapped (see `src/handlers.c`, `handle_map_request()`). The window is then
422 reparented (see section "Manage windows").
423
424 After reparenting the window, `render_tree()` is called which renders the
425 internal layout table. The new window has been placed in the currently focused
426 container and therefore the new window and the old windows (if any) need to be
427 moved/resized so that the currently active layout (default/stacking/tabbed mode)
428 is rendered correctly. To move/resize windows, a window is ``configured'' in
429 X11-speak.
430
431 Some applications, such as MPlayer obviously assume the window manager is
432 stupid and try to configure their windows by themselves. This generates an
433 event called configurerequest. i3 handles these events and tells the window the
434 size it had before the configurerequest (with the exception of not yet mapped
435 windows, which get configured like they want to, and floating windows, which
436 can reconfigure themselves).
437
438 == _NET_WM_STATE
439
440 Only the _NET_WM_STATE_FULLSCREEN atom is handled. It calls
441 ``toggle_fullscreen()'' for the specific client which just configures the
442 client to use the whole screen on which it currently is. Also, it is set as
443 fullscreen_client for the i3Screen.
444
445 == WM_NAME
446
447 When the WM_NAME property of a window changes, its decoration (containing the
448 title) is re-rendered. Note that WM_NAME is in COMPOUND_TEXT encoding which is
449 totally uncommon and cumbersome. Therefore, the _NET_WM_NAME atom will be used
450 if present.
451
452 == _NET_WM_NAME
453
454 Like WM_NAME, this atom contains the title of a window. However, _NET_WM_NAME
455 is encoded in UTF-8. i3 will recode it to UCS-2 in order to be able to pass it
456 to X. Using an appropriate font (ISO-10646), you can see most special
457 characters (every special character contained in your font).
458
459 == Size hints
460
461 Size hints specify the minimum/maximum size for a given window as well as its
462 aspect ratio.  This is important for clients like mplayer, who only set the
463 aspect ratio and resize their window to be as small as possible (but only with
464 some video outputs, for example in Xv, while when using x11, mplayer does the
465 necessary centering for itself).
466
467 So, when an aspect ratio was specified, i3 adjusts the height of the window
468 until the size maintains the correct aspect ratio. For the code to do this, see
469 src/layout.c, function resize_client().
470
471 == Rendering (src/layout.c, render_layout() and render_container())
472
473 Rendering in i3 version 4 is the step which assigns the correct sizes for
474 borders, decoration windows, child windows and the stacking order of all
475 windows. In a separate step (+x_push_changes()+), these changes are pushed to
476 X11.
477
478 Keep in mind that all these properties (+rect+, +window_rect+ and +deco_rect+)
479 are temporary, meaning they will be overwritten by calling +render_con+.
480 Persistent position/size information is kept in +geometry+.
481
482 The entry point for every rendering operation (except for the case of moving
483 floating windows around) currently is +tree_render()+ which will re-render
484 everything that’s necessary (for every output, only the currently displayed
485 workspace is rendered). This behavior is expected to change in the future,
486 since for a lot of updates, re-rendering everything is not actually necessary.
487 Focus was on getting it working correct, not getting it work very fast.
488
489 What +tree_render()+ actually does is calling +render_con()+ on the root
490 container and then pushing the changes to X11. The following sections talk
491 about the different rendering steps, in the order of "top of the tree" (root
492 container) to the bottom.
493
494 === Rendering the root container
495
496 The i3 root container (`con->type == CT_ROOT`) represents the X11 root window.
497 It contains one child container for every output (like LVDS1, VGA1, …), which
498 is available on your computer.
499
500 Rendering the root will first render all tiling windows and then all floating
501 windows. This is necessary because a floating window can be positioned in such
502 a way that it is visible on two different outputs. Therefore, by first
503 rendering all the tiling windows (of all outputs), we make sure that floating
504 windows can never be obscured by tiling windows.
505
506 Essentially, though, this code path will just call +render_con()+ for every
507 output and +x_raise_con(); render_con()+ for every floating window.
508
509 In the special case of having a "global fullscreen" window (fullscreen mode
510 spanning all outputs), a shortcut is taken and +x_raise_con(); render_con()+ is
511 only called for the global fullscreen window.
512
513 === Rendering an output
514
515 Output containers (`con->layout == L_OUTPUT`) represent a hardware output like
516 LVDS1, VGA1, etc. An output container has three children (at the moment): One
517 content container (having workspaces as children) and the top/bottom dock area
518 containers.
519
520 The rendering happens in the function +render_l_output()+ in the following
521 steps:
522
523 1. Find the content container (`con->type == CT_CON`)
524 2. Get the currently visible workspace (+con_get_fullscreen_con(content,
525    CF_OUTPUT)+).
526 3. If there is a fullscreened window on that workspace, directly render it and
527    return, thus ignoring the dock areas.
528 4. Sum up the space used by all the dock windows (they have a variable height
529    only).
530 5. Set the workspace rects (x/y/width/height) based on the position of the
531    output (stored in `con->rect`) and the usable space
532    (`con->rect.{width,height}` without the space used for dock windows).
533 6. Recursively raise and render the output’s child containers (meaning dock
534    area containers and the content container).
535
536 === Rendering a workspace or split container
537
538 From here on, there really is no difference anymore. All containers are of
539 `con->type == CT_CON` (whether workspace or split container) and some of them
540 have a `con->window`, meaning they represent an actual window instead of a
541 split container.
542
543 ==== Default layout
544
545 In default layout, containers are placed horizontally or vertically next to
546 each other (depending on the `con->orientation`). If a child is a leaf node (as
547 opposed to a split container) and has border style "normal", appropriate space
548 will be reserved for its window decoration.
549
550 ==== Stacked layout
551
552 In stacked layout, only the focused window is actually shown (this is achieved
553 by calling +x_raise_con()+ in reverse focus order at the end of +render_con()+).
554
555 The available space for the focused window is the size of the container minus
556 the height of the window decoration for all windows inside this stacked
557 container.
558
559 If border style is "1pixel" or "none", no window decoration height will be
560 reserved (or displayed later on), unless there is more than one window inside
561 the stacked container.
562
563 ==== Tabbed layout
564
565 Tabbed layout works precisely like stacked layout, but the window decoration
566 position/size is different: They are placed next to each other on a single line
567 (fixed height).
568
569 ==== Dock area layout
570
571 This is a special case. Users cannot chose the dock area layout, but it will be
572 set for the dock area containers. In the dockarea layout (at the moment!),
573 windows will be placed above each other.
574
575 === Rendering a window
576
577 A window’s size and position will be determined in the following way:
578
579 1. Subtract the border if border style is not "none" (but "normal" or "1pixel").
580 2. Subtract the X11 border, if the window has an X11 border > 0.
581 3. Obey the aspect ratio of the window (think MPlayer).
582 4. Obey the height- and width-increments of the window (think terminal emulator
583    which can only be resized in one-line or one-character steps).
584
585 == Pushing updates to X11 / Drawing
586
587 A big problem with i3 before version 4 was that we just sent requests to X11
588 anywhere in the source code. This was bad because nobody could understand the
589 entirety of our interaction with X11, it lead to subtle bugs and a lot of edge
590 cases which we had to consider all over again.
591
592 Therefore, since version 4, we have a single file, +src/x.c+, which is
593 responsible for repeatedly transferring parts of our tree datastructure to X11.
594
595 +src/x.c+ consists of multiple parts:
596
597 1. The state pushing: +x_push_changes()+, which calls +x_push_node()+.
598 2. State modification functions: +x_con_init+, +x_reinit+,
599    +x_reparent_child+, +x_move_win+, +x_con_kill+, +x_raise_con+, +x_set_name+
600    and +x_set_warp_to+.
601 3. Expose event handling (drawing decorations): +x_deco_recurse()+ and
602    +x_draw_decoration()+.
603
604 === Pushing state to X11
605
606 In general, the function +x_push_changes+ should be called to push state
607 changes. Only when the scope of the state change is clearly defined (for
608 example only the title of a window) and its impact is known beforehand, one can
609 optimize this and call +x_push_node+ on the appropriate con directly.
610
611 +x_push_changes+ works in the following steps:
612
613 1. Clear the eventmask for all mapped windows. This leads to not getting
614    useless ConfigureNotify or EnterNotify events which are caused by our
615    requests. In general, we only want to handle user input.
616 2. Stack windows above each other, in reverse stack order (starting with the
617    most obscured/bottom window). This is relevant for floating windows which
618    can overlap each other, but also for tiling windows in stacked or tabbed
619    containers. We also update the +_NET_CLIENT_LIST_STACKING+ hint which is
620    necessary for tab drag and drop in Chromium.
621 3. +x_push_node+ will be called for the root container, recursively calling
622    itself for the container’s children. This function actually pushes the
623    state, see the next paragraph.
624 4. If the pointer needs to be warped to a different position (for example when
625    changing focus to a differnt output), it will be warped now.
626 5. The eventmask is restored for all mapped windows.
627 6. Window decorations will be rendered by calling +x_deco_recurse+ on the root
628    container, which then recursively calls itself for the children.
629 7. If the input focus needs to be changed (because the user focused a different
630    window), it will be updated now.
631 8. +x_push_node_unmaps+ will be called for the root container. This function
632    only pushes UnmapWindow requests. Separating the state pushing is necessary
633    to handle fullscreen windows (and workspace switches) in a smooth fashion:
634    The newly visible windows should be visible before the old windows are
635    unmapped.
636
637 +x_push_node+ works in the following steps:
638
639 1. Update the window’s +WM_NAME+, if changed (the +WM_NAME+ is set on i3
640    containers mainly for debugging purposes).
641 2. Reparents a child window into the i3 container if the container was created
642    for a specific managed window.
643 3. If the size/position of the i3 container changed (due to opening a new
644    window or switching layouts for example), the window will be reconfigured.
645    Also, the pixmap which is used to draw the window decoration/border on is
646    reconfigured (pixmaps are size-dependent).
647 4. Size/position for the child window is adjusted.
648 5. The i3 container is mapped if it should be visible and was not yet mapped.
649    When mapping, +WM_STATE+ is set to +WM_STATE_NORMAL+. Also, the eventmask of
650    the child window is updated and the i3 container’s contents are copied from
651    the pixmap.
652 6. +x_push_node+ is called recursively for all children of the current
653    container.
654
655 +x_push_node_unmaps+ handles the remaining case of an i3 container being
656 unmapped if it should not be visible anymore. +WM_STATE+ will be set to
657 +WM_STATE_WITHDRAWN+.
658
659
660 === Drawing window decorations/borders/backgrounds
661
662 +x_draw_decoration+ draws window decorations. It is run for every leaf
663 container (representing an actual X11 window) and for every non-leaf container
664 which is in a stacked/tabbed container (because stacked/tabbed containers
665 display a window decoration for split containers, which at the moment just says
666 "another container").
667
668 Then, parameters are collected to be able to determine whether this decoration
669 drawing is actually necessary or was already done. This saves a substantial
670 number of redraws (depending on your workload, but far over 50%).
671
672 Assuming that we need to draw this decoration, we start by filling the empty
673 space around the child window (think of MPlayer with a specific aspect ratio)
674 in the user-configured client background color.
675
676 Afterwards, we draw the appropriate border (in case of border styles "normal"
677 and "1pixel") and the top bar (in case of border style "normal").
678
679 The last step is drawing the window title on the top bar.
680
681
682 /////////////////////////////////////////////////////////////////////////////////
683
684 == Resizing containers
685
686 By clicking and dragging the border of a container, you can resize the whole
687 column (respectively row) which this container is in. This is necessary to keep
688 the table layout working and consistent.
689
690 The resizing works similarly to the resizing of floating windows or movement of
691 floating windows:
692
693 * A new, invisible window with the size of the root window is created
694   (+grabwin+)
695 * Another window, 2px width and as high as your screen (or vice versa for
696   horizontal resizing) is created. Its background color is the border color and
697   it is only there to inform the user how big the container will be (it
698   creates the impression of dragging the border out of the container).
699 * The +drag_pointer+ function of +src/floating.c+ is called to grab the pointer
700   and enter its own event loop which will pass all events (expose events) but
701   motion notify events. This function then calls the specified callback
702   (+resize_callback+) which does some boundary checking and moves the helper
703   window. As soon as the mouse button is released, this loop will be
704   terminated.
705 * The new width_factor for each involved column (respectively row) will be
706   calculated.
707
708 /////////////////////////////////////////////////////////////////////////////////
709
710 == User commands / commandmode (src/cmdparse.{l,y})
711
712 *********************************************************************************
713 This section has not been updated for v4.0 yet, sorry! We wanted to release on
714 time, but we will update this soon. Please talk to us on IRC if you need to
715 know stuff *NOW* :).
716 *********************************************************************************
717
718 /////////////////////////////////////////////////////////////////////////////////
719
720
721 Like in vim, you can control i3 using commands. They are intended to be a
722 powerful alternative to lots of shortcuts, because they can be combined. There
723 are a few special commands, which are the following:
724
725 exec <command>::
726 Starts the given command by passing it to `/bin/sh`.
727
728 restart::
729 Restarts i3 by executing `argv[0]` (the path with which you started i3) without
730 forking.
731
732 w::
733 "With". This is used to select a bunch of windows. Currently, only selecting
734 the whole container in which the window is in, is supported by specifying "w".
735
736 f, s, d::
737 Toggle fullscreen, stacking, default mode for the current window/container.
738
739 The other commands are to be combined with a direction. The directions are h,
740 j, k and l, like in vim (h = left, j = down, k = up, l = right). When you just
741 specify the direction keys, i3 will move the focus in that direction. You can
742 provide "m" or "s" before the direction to move a window respectively or snap.
743
744 /////////////////////////////////////////////////////////////////////////////////
745
746 == Moving containers
747
748 The movement code is pretty delicate. You need to consider all cases before
749 making any changes or before being able to fully understand how it works.
750
751 === Case 1: Moving inside the same container
752
753 The reference layout for this case is a single workspace in horizontal
754 orientation with two containers on it. Focus is on the left container (1).
755
756
757 [width="15%",cols="^,^"]
758 |========
759 | 1 | 2
760 |========
761
762 When moving the left window to the right (command +move right+), tree_move will
763 look for a container with horizontal orientation and finds the parent of the
764 left container, that is, the workspace. Afterwards, it runs the code branch
765 commented with "the easy case": it calls TAILQ_NEXT to get the container right
766 of the current one and swaps both containers.
767
768 === Case 2: Move a container into a split container
769
770 The reference layout for this case is a horizontal workspace with two
771 containers. The right container is a v-split with two containers. Focus is on
772 the left container (1).
773
774 [width="15%",cols="^,^"]
775 |========
776 1.2+^.^| 1 | 2
777 | 3
778 |========
779
780 When moving to the right (command +move right+), i3 will work like in case 1
781 ("the easy case"). However, as the right container is not a leaf container, but
782 a v-split, the left container (1) will be inserted at the right position (below
783 2, assuming that 2 is focused inside the v-split) by calling +insert_con_into+.
784
785 +insert_con_into+ detaches the container from its parent and inserts it
786 before/after the given target container. Afterwards, the on_remove_child
787 callback is called on the old parent container which will then be closed, if
788 empty.
789
790 Afterwards, +con_focus+ will be called to fix the focus stack and the tree will
791 be flattened.
792
793 === Case 3: Moving to non-existant top/bottom
794
795 Like in case 1, the reference layout for this case is a single workspace in
796 horizontal orientation with two containers on it. Focus is on the left
797 container:
798
799 [width="15%",cols="^,^"]
800 |========
801 | 1 | 2
802 |========
803
804 This time however, the command is +move up+ or +move down+. tree_move will look
805 for a container with vertical orientation. As it will not find any,
806 +same_orientation+ is NULL and therefore i3 will perform a forced orientation
807 change on the workspace by creating a new h-split container, moving the
808 workspace contents into it and then changing the workspace orientation to
809 vertical. Now it will again search for parent containers with vertical
810 orientation and it will find the workspace.
811
812 This time, the easy case code path will not be run as we are not moving inside
813 the same container. Instead, +insert_con_into+ will be called with the focused
814 container and the container above/below the current one (on the level of
815 +same_orientation+).
816
817 Now, +con_focus+ will be called to fix the focus stack and the tree will be
818 flattened.
819
820 === Case 4: Moving to existant top/bottom
821
822 The reference layout for this case is a vertical workspace with two containers.
823 The bottom one is a h-split containing two containers (1 and 2). Focus is on
824 the bottom left container (1).
825
826 [width="15%",cols="^,^"]
827 |========
828 2+| 3
829 | 1 | 2
830 |========
831
832 This case is very much like case 3, only this time the forced workspace
833 orientation change does not need to be performed because the workspace already
834 is in vertical orientation.
835
836 === Case 5: Moving in one-child h-split
837
838 The reference layout for this case is a horizontal workspace with two
839 containers having a v-split on the left side with a one-child h-split on the
840 bottom. Focus is on the bottom left container (2(h)):
841
842 [width="15%",cols="^,^"]
843 |========
844 | 1 1.2+^.^| 3
845 | 2(h)
846 |========
847
848 In this case, +same_orientation+ will be set to the h-split container around
849 the focused container. However, when trying the easy case, the next/previous
850 container +swap+ will be NULL. Therefore, i3 will search again for a
851 +same_orientation+ container, this time starting from the parent of the h-split
852 container.
853
854 After determining a new +same_orientation+ container (if it is NULL, the
855 orientation will be force-changed), this case is equivalent to case 2 or case
856 4.
857
858
859 === Case 6: Floating containers
860
861 The reference layout for this case is a horizontal workspace with two
862 containers plus one floating h-split container. Focus is on the floating
863 container.
864
865 TODO: nice illustration. table not possible?
866
867 When moving up/down, the container needs to leave the floating container and it
868 needs to be placed on the workspace (at workspace level). This is accomplished
869 by calling the function +attach_to_workspace+.
870
871 == Click handling
872
873 Without much ado, here is the list of cases which need to be considered:
874
875 * click to focus (tiling + floating) and raise (floating)
876 * click to focus/raise when in stacked/tabbed mode
877 * floating_modifier + left mouse button to drag a floating con
878 * floating_modifier + right mouse button to resize a floating con
879 * click on decoration in a floating con to either initiate a resize (if there
880   is more than one child in the floating con) or to drag the
881   floating con (if it’s the one at the top).
882 * click on border in a floating con to resize the floating con
883 * floating_modifier + right mouse button to resize a tiling con
884 * click on border/decoration to resize a tiling con
885
886 == Gotchas
887
888 * Forgetting to call `xcb_flush(conn);` after sending a request. This usually
889   leads to code which looks like it works fine but which does not work under
890   certain conditions.
891
892 == Using git / sending patches
893
894 For a short introduction into using git, see
895 http://www.spheredev.org/wiki/Git_for_the_lazy or, for more documentation, see
896 http://git-scm.com/documentation
897
898 When you want to send a patch because you fixed a bug or implemented a cool
899 feature (please talk to us before working on features to see whether they are
900 maybe already implemented, not possible for some some reason, or don’t fit
901 into the concept), please use git to create a patchfile.
902
903 First of all, update your working copy to the latest version of the master
904 branch:
905
906 --------
907 git pull
908 --------
909
910 Afterwards, make the necessary changes for your bugfix/feature. Then, review
911 the changes using +git diff+ (you might want to enable colors in the diff using
912 +git config diff.color auto+).  When you are definitely done, use +git commit
913 -a+ to commit all changes you’ve made.
914
915 Then, use the following command to generate a patchfile which we can directly
916 apply to the branch, preserving your commit message and name:
917
918 -----------------------
919 git format-patch origin
920 -----------------------
921
922 Just send us the generated file via email.
923
924 == Thought experiments
925
926 In this section, we collect thought experiments, so that we don’t forget our
927 thoughts about specific topics. They are not necessary to get into hacking i3,
928 but if you are interested in one of the topics they cover, you should read them
929 before asking us why things are the way they are or why we don’t implement
930 things.
931
932 === Using cgroups per workspace
933
934 cgroups (control groups) are a linux-only feature which provides the ability to
935 group multiple processes. For each group, you can individually set resource
936 limits, like allowed memory usage. Furthermore, and more importantly for our
937 purposes, they serve as a namespace, a label which you can attach to processes
938 and their children.
939
940 One interesting use for cgroups is having one cgroup per workspace (or
941 container, doesn’t really matter). That way, you could set different priorities
942 and have a workspace for important stuff (say, writing a LaTeX document or
943 programming) and a workspace for unimportant background stuff (say,
944 JDownloader). Both tasks can obviously consume a lot of I/O resources, but in
945 this example it doesn’t really matter if JDownloader unpacks the download a
946 minute earlier or not. However, your compiler should work as fast as possible.
947 Having one cgroup per workspace, you would assign more resources to the
948 programming workspace.
949
950 Another interesting feature is that an inherent problem of the workspace
951 concept could be solved by using cgroups: When starting an application on
952 workspace 1, then switching to workspace 2, you will get the application’s
953 window(s) on workspace 2 instead of the one you started it on. This is because
954 the window manager does not have any mapping between the process it starts (or
955 gets started in any way) and the window(s) which appear.
956
957 Imagine for example using dmenu: The user starts dmenu by pressing Mod+d, dmenu
958 gets started with PID 3390. The user then decides to launch Firefox, which
959 takes a long time. So he enters firefox into dmenu and presses enter. Firefox
960 gets started with PID 4001. When it finally finishes loading, it creates an X11
961 window and uses MapWindow to make it visible. This is the first time i3
962 actually gets in touch with Firefox. It decides to map the window, but it has
963 no way of knowing that this window (even though it has the _NET_WM_PID property
964 set to 4001) belongs to the dmenu the user started before.
965
966 How do cgroups help with this? Well, when pressing Mod+d to launch dmenu, i3
967 would create a new cgroup, let’s call it i3-3390-1. It launches dmenu in that
968 cgroup, which gets PID 3390. As before, the user enters firefox and Firefox
969 gets launched with PID 4001. This time, though, the Firefox process with PID
970 4001 is *also* member of the cgroup i3-3390-1 (because fork()ing in a cgroup
971 retains the cgroup property). Therefore, when mapping the window, i3 can look
972 up in which cgroup the process is and can establish a mapping between the
973 workspace and the window.
974
975 There are multiple problems with this approach:
976
977 . Every application has to properly set +_NET_WM_PID+. This is acceptable and
978   patches can be written for the few applications which don’t set the hint yet.
979 . It does only work on Linux, since cgroups are a Linux-only feature. Again,
980   this is acceptable.
981 . The main problem is that some applications create X11 windows completely
982   independent of UNIX processes. An example for this is Chromium (or
983   gnome-terminal), which, when being started a second time, communicates with
984   the first process and lets the first process open a new window. Therefore, if
985   you have a Chromium window on workspace 2 and you are currently working on
986   workspace 3, starting +chromium+ does not lead to the desired result (the
987   window will open on workspace 2).
988
989 Therefore, my conclusion is that the only proper way of fixing the "window gets
990 opened on the wrong workspace" problem is in the application itself. Most
991 modern applications support freedesktop startup-notifications  which can be
992 used for this.