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