]> git.sur5r.net Git - i3/i3.github.io/blob - docs/hacking-howto.html
15e6c593a3cd998d3aed7f83e5e1ab3ba66be1d5
[i3/i3.github.io] / docs / hacking-howto.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"\r
2     "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\r
3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">\r
4 <head>\r
5 <link rel="icon" type="image/x-icon" href="/favicon.ico">\r
6 <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />\r
7 <meta name="generator" content="AsciiDoc 8.6.10" />\r
8 <title>i3: Hacking i3: How To</title>\r
9 <link rel="stylesheet" href="/css/style.css" type="text/css" />\r
10 <link rel="stylesheet" href="/css/xhtml11.css" type="text/css" />\r
11 <script type="text/javascript">\r
12 /*<![CDATA[*/\r
13 document.addEventListener("DOMContentLoaded", function(){asciidoc.footnotes(); asciidoc.toc(2);}, false);\r
14 /*]]>*/\r
15 </script>\r
16 <script type="text/javascript" src="/js/asciidoc-xhtml11.js"></script>\r
17 </head>\r
18 <body class="article">\r
19 \r
20         <div id="main">\r
21             <a href="/"><h1 id="title">i3 - improved tiling WM</h1></a>\r
22                         <ul id="nav">\r
23                                 <li><a style="border-bottom: 2px solid #fff" href="/docs">Docs</a></li>\r
24                                 <li><a href="/screenshots">Screens</a></li>\r
25                                 <li><a href="https://www.reddit.com/r/i3wm/">FAQ</a></li>\r
26                                 <li><a href="/contact">Contact</a></li>\r
27                                 <li><a href="https://github.com/i3/i3/issues">Bugs</a></li>\r
28                         </ul>\r
29         <br style="clear: both">\r
30 <div id="content">\r
31 <div id="header">\r
32 <h1>Hacking i3: How To</h1>\r
33 <span id="author">Michael Stapelberg</span><br />\r
34 <span id="email"><tt>&lt;<a href="mailto:michael@i3wm.org">michael@i3wm.org</a>&gt;</tt></span><br />\r
35 <span id="revdate">February 2013</span>\r
36 <div id="toc">
37   <div id="toctitle">Table of Contents</div>
38   <noscript><p><b>JavaScript must be enabled in your browser to display the table of contents.</b></p></noscript>
39 </div>\r
40 </div>\r
41 <div id="preamble">\r
42 <div class="sectionbody">\r
43 <div class="paragraph"><p>This document is intended to be the first thing you read before looking and/or\r
44 touching i3’s source code. It should contain all important information to help\r
45 you understand why things are like they are. If it does not mention something\r
46 you find necessary, please do not hesitate to contact me.</p></div>\r
47 </div>\r
48 </div>\r
49 <div class="sect1">\r
50 <h2 id="_building_i3">1. Building i3</h2>\r
51 <div class="sectionbody">\r
52 <div class="paragraph"><p>You can build i3 like you build any other software package which uses autotools.\r
53 Here’s a memory refresher:</p></div>\r
54 <div class="literalblock">\r
55 <div class="content">\r
56 <pre><tt>$ autoreconf -fi\r
57 $ mkdir -p build &amp;&amp; cd build\r
58 $ ../configure\r
59 $ make -j8</tt></pre>\r
60 </div></div>\r
61 <div class="paragraph"><p>(The autoreconf -fi step is unnecessary if you are building from a release tarball,\r
62  but shouldn’t hurt either.)</p></div>\r
63 <div class="sect2">\r
64 <h3 id="_build_system_features">1.1. Build system features</h3>\r
65 <div class="ulist"><ul>\r
66 <li>\r
67 <p>\r
68 We use the AX_ENABLE_BUILDDIR macro to enforce builds happening in a separate\r
69   directory. This is a prerequisite for the AX_EXTEND_SRCDIR macro and building\r
70   in a separate directory is common practice anyway. In case this causes any\r
71   trouble when packaging i3 for your distribution, please open an issue.\r
72 </p>\r
73 </li>\r
74 <li>\r
75 <p>\r
76 “make check” runs the i3 testsuite. See docs/testsuite for details.\r
77 </p>\r
78 </li>\r
79 <li>\r
80 <p>\r
81 “make distcheck” (runs testsuite on “make dist” result, tiny bit quicker\r
82   feedback cycle than waiting for the travis build to catch the issue).\r
83 </p>\r
84 </li>\r
85 <li>\r
86 <p>\r
87 “make uninstall” (occasionally requested by users who compile from source)\r
88 </p>\r
89 </li>\r
90 <li>\r
91 <p>\r
92 “make” will build manpages/docs by default if the tools are installed.\r
93   Conversely, manpages/docs are not tried to be built for users who don’t want\r
94   to install all these dependencies to get started hacking on i3.\r
95 </p>\r
96 </li>\r
97 <li>\r
98 <p>\r
99 non-release builds will enable address sanitizer by default. Use the\r
100   --disable-sanitizers configure option to turn off all sanitizers, and see\r
101   --help for available sanitizers.\r
102 </p>\r
103 </li>\r
104 <li>\r
105 <p>\r
106 Support for pre-compiled headers (PCH) has been dropped for now in the\r
107   interest of simplicity. If you need support for PCH, please open an issue.\r
108 </p>\r
109 </li>\r
110 <li>\r
111 <p>\r
112 Coverage reports are now generated using “make check-code-coverage”, which\r
113   requires specifying --enable-code-coverage when calling configure.\r
114 </p>\r
115 </li>\r
116 </ul></div>\r
117 </div>\r
118 </div>\r
119 </div>\r
120 <div class="sect1">\r
121 <h2 id="_using_git_sending_patches">2. Using git / sending patches</h2>\r
122 <div class="sectionbody">\r
123 <div class="paragraph"><p>For a short introduction into using git, see\r
124 <a href="https://web.archive.org/web/20121024222556/http://www.spheredev.org/wiki/Git_for_the_lazy">https://web.archive.org/web/20121024222556/http://www.spheredev.org/wiki/Git_for_the_lazy</a>\r
125 or, for more documentation, see <a href="https://git-scm.com/documentation">https://git-scm.com/documentation</a></p></div>\r
126 <div class="paragraph"><p>Please talk to us before working on new features to see whether they will be\r
127 accepted. A good way for this is to open an issue and asking for opinions on it.\r
128 Even for accepted features, this can be a good way to refine an idea upfront. However,\r
129 we don&#8217;t want to see certain features in i3, e.g., switching window focus in an\r
130 Alt+Tab like way.</p></div>\r
131 <div class="paragraph"><p>When working on bugfixes, please make sure you mention that you are working on\r
132 it in the corresponding bug report at <a href="https://github.com/i3/i3/issues">https://github.com/i3/i3/issues</a>. In case\r
133 there is no bug report yet, please create one.</p></div>\r
134 <div class="paragraph"><p>After you are done, please submit your work for review as a pull request at\r
135 <a href="https://github.com/i3/i3">https://github.com/i3/i3</a>.</p></div>\r
136 <div class="paragraph"><p>Do not send emails to the mailing list or any author directly, and don’t submit\r
137 them in the bugtracker, since all reviews should be done in public at\r
138 <a href="https://github.com/i3/i3">https://github.com/i3/i3</a>. In order to make your review go as fast as possible, you\r
139 could have a look at previous reviews and see what the common mistakes are.</p></div>\r
140 <div class="sect2">\r
141 <h3 id="_which_branch_to_use">2.1. Which branch to use?</h3>\r
142 <div class="paragraph"><p>Work on i3 generally happens in two branches: “master” and “next” (the latter\r
143 being the default branch, the one that people get when they check out the git\r
144 repository).</p></div>\r
145 <div class="paragraph"><p>The contents of “master” are always stable. That is, it contains the source code\r
146 of the latest release, plus any bugfixes that were applied since that release.</p></div>\r
147 <div class="paragraph"><p>New features are only found in the “next” branch. Therefore, if you are working\r
148 on a new feature, use the “next” branch. If you are working on a bugfix, use the\r
149 “next” branch, too, but make sure your code also works on “master”.</p></div>\r
150 </div>\r
151 </div>\r
152 </div>\r
153 <div class="sect1">\r
154 <h2 id="_window_managers">3. Window Managers</h2>\r
155 <div class="sectionbody">\r
156 <div class="paragraph"><p>A window manager is not necessarily needed to run X, but it is usually used in\r
157 combination with X to facilitate some things. The window manager&#8217;s job is to\r
158 take care of the placement of windows, to provide the user with some mechanisms\r
159 to change the position/size of windows and to communicate with clients to a\r
160 certain extent (for example handle fullscreen requests of clients such as\r
161 MPlayer).</p></div>\r
162 <div class="paragraph"><p>There are no different contexts in which X11 clients run, so a window manager\r
163 is just another client, like all other X11 applications. However, it handles\r
164 some events which normal clients usually don’t handle.</p></div>\r
165 <div class="paragraph"><p>In the case of i3, the tasks (and order of them) are the following:</p></div>\r
166 <div class="olist arabic"><ol class="arabic">\r
167 <li>\r
168 <p>\r
169 Grab the key bindings (events will be sent upon keypress/keyrelease)\r
170 </p>\r
171 </li>\r
172 <li>\r
173 <p>\r
174 Iterate through all existing windows (if the window manager is not started as\r
175   the first client of X) and manage them (reparent them, create window\r
176   decorations, etc.)\r
177 </p>\r
178 </li>\r
179 <li>\r
180 <p>\r
181 When new windows are created, manage them\r
182 </p>\r
183 </li>\r
184 <li>\r
185 <p>\r
186 Handle the client’s <tt>_WM_STATE</tt> property, but only <tt>_WM_STATE_FULLSCREEN</tt> and\r
187   <tt>_NET_WM_STATE_DEMANDS_ATTENTION</tt>\r
188 </p>\r
189 </li>\r
190 <li>\r
191 <p>\r
192 Handle the client’s <tt>WM_NAME</tt> property\r
193 </p>\r
194 </li>\r
195 <li>\r
196 <p>\r
197 Handle the client’s size hints to display them proportionally\r
198 </p>\r
199 </li>\r
200 <li>\r
201 <p>\r
202 Handle the client’s urgency hint\r
203 </p>\r
204 </li>\r
205 <li>\r
206 <p>\r
207 Handle enter notifications (focus follows mouse)\r
208 </p>\r
209 </li>\r
210 <li>\r
211 <p>\r
212 Handle button (as in mouse buttons) presses for focus/raise on click\r
213 </p>\r
214 </li>\r
215 <li>\r
216 <p>\r
217 Handle expose events to re-draw own windows such as decorations\r
218 </p>\r
219 </li>\r
220 <li>\r
221 <p>\r
222 React to the user’s commands: Change focus, Move windows, Switch workspaces,\r
223   Change the layout mode of a container (default/stacking/tabbed), start a new\r
224   application, restart the window manager\r
225 </p>\r
226 </li>\r
227 </ol></div>\r
228 <div class="paragraph"><p>In the following chapters, each of these tasks and their implementation details\r
229 will be discussed.</p></div>\r
230 <div class="sect2">\r
231 <h3 id="_tiling_window_managers">3.1. Tiling window managers</h3>\r
232 <div class="paragraph"><p>Traditionally, there are two approaches to managing windows: The most common\r
233 one nowadays is floating, which means the user can freely move/resize the\r
234 windows. The other approach is called tiling, which means that your window\r
235 manager distributes windows to use as much space as possible while not\r
236 overlapping each other.</p></div>\r
237 <div class="paragraph"><p>The idea behind tiling is that you should not need to waste your time\r
238 moving/resizing windows while you usually want to get some work done. After\r
239 all, most users sooner or later tend to lay out their windows in a way which\r
240 corresponds to tiling or stacking mode in i3. Therefore, why not let i3 do this\r
241 for you? Certainly, it’s faster than you could ever do it.</p></div>\r
242 <div class="paragraph"><p>The problem with most tiling window managers is that they are too inflexible.\r
243 In my opinion, a window manager is just another tool, and similar to vim which\r
244 can edit all kinds of text files (like source code, HTML, …) and is not limited\r
245 to a specific file type, a window manager should not limit itself to a certain\r
246 layout (like dwm, awesome, …) but provide mechanisms for you to easily create\r
247 the layout you need at the moment.</p></div>\r
248 </div>\r
249 <div class="sect2">\r
250 <h3 id="_the_layout_tree">3.2. The layout tree</h3>\r
251 <div class="paragraph"><p>The data structure which i3 uses to keep track of your windows is a tree. Every\r
252 node in the tree is a container (type <tt>Con</tt>). Some containers represent actual\r
253 windows (every container with a <tt>window != NULL</tt>), some represent split\r
254 containers and a few have special purposes: they represent workspaces, outputs\r
255 (like VGA1, LVDS1, …) or the X11 root window.</p></div>\r
256 <div class="paragraph"><p>So, when you open a terminal and immediately open another one, they reside in\r
257 the same split container, which uses the default layout. In case of an empty\r
258 workspace, the split container we are talking about is the workspace.</p></div>\r
259 <div class="paragraph"><p>To get an impression of how different layouts are represented, just play around\r
260 and look at the data structures&#8201;&#8212;&#8201;they are exposed as a JSON hash. See\r
261 <a href="https://i3wm.org/docs/ipc.html#_tree_reply">https://i3wm.org/docs/ipc.html#_tree_reply</a> for documentation on that and an\r
262 example.</p></div>\r
263 </div>\r
264 </div>\r
265 </div>\r
266 <div class="sect1">\r
267 <h2 id="_files">4. Files</h2>\r
268 <div class="sectionbody">\r
269 <div class="dlist"><dl>\r
270 <dt class="hdlist1">\r
271 include/atoms.xmacro\r
272 </dt>\r
273 <dd>\r
274 <p>\r
275 A file containing all X11 atoms which i3 uses. This file will be included\r
276 various times (for defining, requesting and receiving the atoms), each time\r
277 with a different definition of xmacro().\r
278 </p>\r
279 </dd>\r
280 <dt class="hdlist1">\r
281 include/data.h\r
282 </dt>\r
283 <dd>\r
284 <p>\r
285 Contains data definitions used by nearly all files. You really need to read\r
286 this first.\r
287 </p>\r
288 </dd>\r
289 <dt class="hdlist1">\r
290 include/*.h\r
291 </dt>\r
292 <dd>\r
293 <p>\r
294 Contains forward definitions for all public functions, as well as\r
295 doxygen-compatible comments (so if you want to get a bit more of the big\r
296 picture, either browse all header files or use doxygen if you prefer that).\r
297 </p>\r
298 </dd>\r
299 <dt class="hdlist1">\r
300 src/config_parser.c\r
301 </dt>\r
302 <dd>\r
303 <p>\r
304 Contains a custom configuration parser. See src/command_parser.c for rationale\r
305  on why we use a custom parser.\r
306 </p>\r
307 </dd>\r
308 <dt class="hdlist1">\r
309 src/click.c\r
310 </dt>\r
311 <dd>\r
312 <p>\r
313 Contains all functions which handle mouse button clicks (right mouse button\r
314 clicks initiate resizing and thus are relatively complex).\r
315 </p>\r
316 </dd>\r
317 <dt class="hdlist1">\r
318 src/command_parser.c\r
319 </dt>\r
320 <dd>\r
321 <p>\r
322 Contains a hand-written parser to parse commands (commands are what\r
323 you bind on keys and what you can send to i3 using the IPC interface, like\r
324 <em>move left</em> or <em>workspace 4</em>).\r
325 </p>\r
326 </dd>\r
327 <dt class="hdlist1">\r
328 src/con.c\r
329 </dt>\r
330 <dd>\r
331 <p>\r
332 Contains all functions which deal with containers directly (creating\r
333 containers, searching containers, getting specific properties from containers,\r
334 …).\r
335 </p>\r
336 </dd>\r
337 <dt class="hdlist1">\r
338 src/config.c\r
339 </dt>\r
340 <dd>\r
341 <p>\r
342 Contains all functions handling the configuration file (calling the parser\r
343 src/config_parser.c) with the correct path, switching key bindings mode).\r
344 </p>\r
345 </dd>\r
346 <dt class="hdlist1">\r
347 src/ewmh.c\r
348 </dt>\r
349 <dd>\r
350 <p>\r
351 Functions to get/set certain EWMH properties easily.\r
352 </p>\r
353 </dd>\r
354 <dt class="hdlist1">\r
355 src/floating.c\r
356 </dt>\r
357 <dd>\r
358 <p>\r
359 Contains functions for floating mode (mostly resizing/dragging).\r
360 </p>\r
361 </dd>\r
362 <dt class="hdlist1">\r
363 src/handlers.c\r
364 </dt>\r
365 <dd>\r
366 <p>\r
367 Contains all handlers for all kinds of X events (new window title, new hints,\r
368 unmapping, key presses, button presses, …).\r
369 </p>\r
370 </dd>\r
371 <dt class="hdlist1">\r
372 src/ipc.c\r
373 </dt>\r
374 <dd>\r
375 <p>\r
376 Contains code for the IPC interface.\r
377 </p>\r
378 </dd>\r
379 <dt class="hdlist1">\r
380 src/load_layout.c\r
381 </dt>\r
382 <dd>\r
383 <p>\r
384 Contains code for loading layouts from JSON files.\r
385 </p>\r
386 </dd>\r
387 <dt class="hdlist1">\r
388 src/log.c\r
389 </dt>\r
390 <dd>\r
391 <p>\r
392 Contains the logging functions.\r
393 </p>\r
394 </dd>\r
395 <dt class="hdlist1">\r
396 src/main.c\r
397 </dt>\r
398 <dd>\r
399 <p>\r
400 Initializes the window manager.\r
401 </p>\r
402 </dd>\r
403 <dt class="hdlist1">\r
404 src/manage.c\r
405 </dt>\r
406 <dd>\r
407 <p>\r
408 Looks at existing or new windows and decides whether to manage them. If so, it\r
409 reparents the window and inserts it into our data structures.\r
410 </p>\r
411 </dd>\r
412 <dt class="hdlist1">\r
413 src/match.c\r
414 </dt>\r
415 <dd>\r
416 <p>\r
417 A "match" is a data structure which acts like a mask or expression to match\r
418 certain windows or not. For example, when using commands, you can specify a\r
419 command like this: [title="<strong>Firefox</strong>"] kill. The title member of the match\r
420 data structure will then be filled and i3 will check each window using\r
421 match_matches_window() to find the windows affected by this command.\r
422 </p>\r
423 </dd>\r
424 <dt class="hdlist1">\r
425 src/move.c\r
426 </dt>\r
427 <dd>\r
428 <p>\r
429 Contains code to move a container in a specific direction.\r
430 </p>\r
431 </dd>\r
432 <dt class="hdlist1">\r
433 src/output.c\r
434 </dt>\r
435 <dd>\r
436 <p>\r
437 Functions to handle CT_OUTPUT cons.\r
438 </p>\r
439 </dd>\r
440 <dt class="hdlist1">\r
441 src/randr.c\r
442 </dt>\r
443 <dd>\r
444 <p>\r
445 The RandR API is used to get (and re-query) the configured outputs (monitors,\r
446 …).\r
447 </p>\r
448 </dd>\r
449 <dt class="hdlist1">\r
450 src/render.c\r
451 </dt>\r
452 <dd>\r
453 <p>\r
454 Renders the tree data structure by assigning coordinates to every node. These\r
455 values will later be pushed to X11 in <tt>src/x.c</tt>.\r
456 </p>\r
457 </dd>\r
458 <dt class="hdlist1">\r
459 src/resize.c\r
460 </dt>\r
461 <dd>\r
462 <p>\r
463 Contains the functions to resize containers.\r
464 </p>\r
465 </dd>\r
466 <dt class="hdlist1">\r
467 src/restore_layout.c\r
468 </dt>\r
469 <dd>\r
470 <p>\r
471 Everything for restored containers that is not pure state parsing (which can be\r
472 found in load_layout.c).\r
473 </p>\r
474 </dd>\r
475 <dt class="hdlist1">\r
476 src/sighandler.c\r
477 </dt>\r
478 <dd>\r
479 <p>\r
480 Handles <tt>SIGSEGV</tt>, <tt>SIGABRT</tt> and <tt>SIGFPE</tt> by showing a dialog that i3 crashed.\r
481 You can chose to let it dump core, to restart it in-place or to restart it\r
482 in-place but forget about the layout.\r
483 </p>\r
484 </dd>\r
485 <dt class="hdlist1">\r
486 src/tree.c\r
487 </dt>\r
488 <dd>\r
489 <p>\r
490 Contains functions which open or close containers in the tree, change focus or\r
491 cleanup ("flatten") the tree. See also <tt>src/move.c</tt> for another similar\r
492 function, which was moved into its own file because it is so long.\r
493 </p>\r
494 </dd>\r
495 <dt class="hdlist1">\r
496 src/util.c\r
497 </dt>\r
498 <dd>\r
499 <p>\r
500 Contains useful functions which are not really dependent on anything.\r
501 </p>\r
502 </dd>\r
503 <dt class="hdlist1">\r
504 src/window.c\r
505 </dt>\r
506 <dd>\r
507 <p>\r
508 Handlers to update X11 window properties like <tt>WM_CLASS</tt>, <tt>_NET_WM_NAME</tt>,\r
509 <tt>CLIENT_LEADER</tt>, etc.\r
510 </p>\r
511 </dd>\r
512 <dt class="hdlist1">\r
513 src/workspace.c\r
514 </dt>\r
515 <dd>\r
516 <p>\r
517 Contains all functions related to workspaces (displaying, hiding, renaming…)\r
518 </p>\r
519 </dd>\r
520 <dt class="hdlist1">\r
521 src/x.c\r
522 </dt>\r
523 <dd>\r
524 <p>\r
525 Transfers our in-memory tree (see <tt>src/render.c</tt>) to X11.\r
526 </p>\r
527 </dd>\r
528 <dt class="hdlist1">\r
529 src/xcb.c\r
530 </dt>\r
531 <dd>\r
532 <p>\r
533 Contains wrappers to use xcb more easily.\r
534 </p>\r
535 </dd>\r
536 <dt class="hdlist1">\r
537 src/xcursor.c\r
538 </dt>\r
539 <dd>\r
540 <p>\r
541 XCursor functions (for cursor themes).\r
542 </p>\r
543 </dd>\r
544 <dt class="hdlist1">\r
545 src/xinerama.c\r
546 </dt>\r
547 <dd>\r
548 <p>\r
549 Legacy support for Xinerama. See <tt>src/randr.c</tt> for the preferred API.\r
550 </p>\r
551 </dd>\r
552 </dl></div>\r
553 </div>\r
554 </div>\r
555 <div class="sect1">\r
556 <h2 id="_data_structures">5. Data structures</h2>\r
557 <div class="sectionbody">\r
558 <div class="paragraph"><p>See include/data.h for documented data structures. The most important ones are\r
559 explained right here.</p></div>\r
560 <div class="paragraph"><p>So, the hierarchy is:</p></div>\r
561 <div class="olist arabic"><ol class="arabic">\r
562 <li>\r
563 <p>\r
564 <strong>X11 root window</strong>, the root container\r
565 </p>\r
566 </li>\r
567 <li>\r
568 <p>\r
569 <strong>Output container</strong> (LVDS1 in this example)\r
570 </p>\r
571 </li>\r
572 <li>\r
573 <p>\r
574 <strong>Content container</strong> (there are also containers for dock windows)\r
575 </p>\r
576 </li>\r
577 <li>\r
578 <p>\r
579 <strong>Workspaces</strong> (Workspace 1 in this example, with horizontal orientation)\r
580 </p>\r
581 </li>\r
582 <li>\r
583 <p>\r
584 <strong>Split container</strong> (vertically split)\r
585 </p>\r
586 </li>\r
587 <li>\r
588 <p>\r
589 <strong>X11 window containers</strong>\r
590 </p>\r
591 </li>\r
592 </ol></div>\r
593 <div class="paragraph"><p>The data type is <tt>Con</tt>, in all cases.</p></div>\r
594 <div class="sect2">\r
595 <h3 id="_x11_root_window">5.1. X11 root window</h3>\r
596 <div class="paragraph"><p>The X11 root window is a single window per X11 display (a display is identified\r
597 by <tt>:0</tt> or <tt>:1</tt> etc.). The root window is what you draw your background image\r
598 on. It spans all the available outputs, e.g. <tt>VGA1</tt> is a specific part of the\r
599 root window and <tt>LVDS1</tt> is a specific part of the root window.</p></div>\r
600 </div>\r
601 <div class="sect2">\r
602 <h3 id="_output_container">5.2. Output container</h3>\r
603 <div class="paragraph"><p>Every active output obtained through RandR is represented by one output\r
604 container. Outputs are considered active when a mode is configured (meaning\r
605 something is actually displayed on the output) and the output is not a clone.</p></div>\r
606 <div class="paragraph"><p>For example, if your notebook has a screen resolution of 1280x800 px and you\r
607 connect a video projector with a resolution of 1024x768 px, set it up in clone\r
608 mode (<tt>xrandr --output VGA1 --mode 1024x768 --same-as LVDS1</tt>), i3 will\r
609 reduce the resolution to the lowest common resolution and disable one of the\r
610 cloned outputs afterwards.</p></div>\r
611 <div class="paragraph"><p>However, if you configure it using <tt>xrandr --output VGA1 --mode 1024x768\r
612 --right-of LVDS1</tt>, i3 will set both outputs active. For each output, a new\r
613 workspace will be assigned. New workspaces are created on the output you are\r
614 currently on.</p></div>\r
615 </div>\r
616 <div class="sect2">\r
617 <h3 id="_content_container">5.3. Content container</h3>\r
618 <div class="paragraph"><p>Each output has multiple children. Two of them are dock containers which hold\r
619 dock clients. The other one is the content container, which holds the actual\r
620 content (workspaces) of this output.</p></div>\r
621 </div>\r
622 <div class="sect2">\r
623 <h3 id="_workspace">5.4. Workspace</h3>\r
624 <div class="paragraph"><p>A workspace is identified by its name. Basically, you could think of\r
625 workspaces as different desks in your office, if you like the desktop\r
626 metaphor. They just contain different sets of windows and are completely\r
627 separate of each other. Other window managers also call this &#8220;Virtual\r
628 desktops&#8221;.</p></div>\r
629 </div>\r
630 <div class="sect2">\r
631 <h3 id="_split_container">5.5. Split container</h3>\r
632 <div class="paragraph"><p>A split container is a container which holds an arbitrary amount of split\r
633 containers or X11 window containers. It has an orientation (horizontal or\r
634 vertical) and a layout.</p></div>\r
635 <div class="paragraph"><p>Split containers (and X11 window containers, which are a subtype of split\r
636 containers) can have different border styles.</p></div>\r
637 </div>\r
638 <div class="sect2">\r
639 <h3 id="_x11_window_container">5.6. X11 window container</h3>\r
640 <div class="paragraph"><p>An X11 window container holds exactly one X11 window. These are the leaf nodes\r
641 of the layout tree, they cannot have any children.</p></div>\r
642 </div>\r
643 </div>\r
644 </div>\r
645 <div class="sect1">\r
646 <h2 id="_list_queue_macros">6. List/queue macros</h2>\r
647 <div class="sectionbody">\r
648 <div class="paragraph"><p>i3 makes heavy use of the list macros defined in BSD operating systems. To\r
649 ensure that the operating system on which i3 is compiled has all the expected\r
650 features, i3 comes with <tt>include/queue.h</tt>. On BSD systems, you can use man\r
651 <tt>queue(3)</tt>. On Linux, you have to use google (or read the source).</p></div>\r
652 <div class="paragraph"><p>The lists used are <tt>SLIST</tt> (single linked lists), <tt>CIRCLEQ</tt> (circular\r
653 queues) and <tt>TAILQ</tt> (tail queues). Usually, only forward traversal is necessary,\r
654 so an <tt>SLIST</tt> works fine. If inserting elements at arbitrary positions or at\r
655 the end of a list is necessary, a <tt>TAILQ</tt> is used instead. However, for the\r
656 windows inside a container, a <tt>CIRCLEQ</tt> is necessary to go from the currently\r
657 selected window to the window above/below.</p></div>\r
658 </div>\r
659 </div>\r
660 <div class="sect1">\r
661 <h2 id="_naming_conventions">7. Naming conventions</h2>\r
662 <div class="sectionbody">\r
663 <div class="paragraph"><p>There is a row of standard variables used in many events. The following names\r
664 should be chosen for those:</p></div>\r
665 <div class="ulist"><ul>\r
666 <li>\r
667 <p>\r
668 &#8220;conn&#8221; is the xcb_connection_t\r
669 </p>\r
670 </li>\r
671 <li>\r
672 <p>\r
673 &#8220;event&#8221; is the event of the particular type\r
674 </p>\r
675 </li>\r
676 <li>\r
677 <p>\r
678 &#8220;con&#8221; names a container\r
679 </p>\r
680 </li>\r
681 <li>\r
682 <p>\r
683 &#8220;current&#8221; is a loop variable when using <tt>TAILQ_FOREACH</tt> etc.\r
684 </p>\r
685 </li>\r
686 </ul></div>\r
687 </div>\r
688 </div>\r
689 <div class="sect1">\r
690 <h2 id="_startup_src_mainx_c_main">8. Startup (src/mainx.c, main())</h2>\r
691 <div class="sectionbody">\r
692 <div class="ulist"><ul>\r
693 <li>\r
694 <p>\r
695 Establish the xcb connection\r
696 </p>\r
697 </li>\r
698 <li>\r
699 <p>\r
700 Check for XKB extension on the separate X connection, load Xcursor\r
701 </p>\r
702 </li>\r
703 <li>\r
704 <p>\r
705 Check for RandR screens (with a fall-back to Xinerama)\r
706 </p>\r
707 </li>\r
708 <li>\r
709 <p>\r
710 Grab the keycodes for which bindings exist\r
711 </p>\r
712 </li>\r
713 <li>\r
714 <p>\r
715 Manage all existing windows\r
716 </p>\r
717 </li>\r
718 <li>\r
719 <p>\r
720 Enter the event loop\r
721 </p>\r
722 </li>\r
723 </ul></div>\r
724 </div>\r
725 </div>\r
726 <div class="sect1">\r
727 <h2 id="_keybindings">9. Keybindings</h2>\r
728 <div class="sectionbody">\r
729 <div class="sect2">\r
730 <h3 id="_grabbing_the_bindings">9.1. Grabbing the bindings</h3>\r
731 <div class="paragraph"><p>Grabbing the bindings is quite straight-forward. You pass X your combination of\r
732 modifiers and the keycode you want to grab and whether you want to grab them\r
733 actively or passively. Most bindings (everything except for bindings using\r
734 Mode_switch) are grabbed passively, that is, just the window manager gets the\r
735 event and cannot replay it.</p></div>\r
736 <div class="paragraph"><p>We need to grab bindings that use Mode_switch actively because of a bug in X.\r
737 When the window manager receives the keypress/keyrelease event for an actively\r
738 grabbed keycode, it has to decide what to do with this event: It can either\r
739 replay it so that other applications get it or it can prevent other\r
740 applications from receiving it.</p></div>\r
741 <div class="paragraph"><p>So, why do we need to grab keycodes actively? Because X does not set the\r
742 state-property of keypress/keyrelease events properly. The Mode_switch bit is\r
743 not set and we need to get it using XkbGetState. This means we cannot pass X\r
744 our combination of modifiers containing Mode_switch when grabbing the key and\r
745 therefore need to grab the keycode itself without any modifiers. This means,\r
746 if you bind Mode_switch + keycode 38 ("a"), i3 will grab keycode 38 ("a") and\r
747 check on each press of "a" if the Mode_switch bit is set using XKB. If yes, it\r
748 will handle the event, if not, it will replay the event.</p></div>\r
749 </div>\r
750 <div class="sect2">\r
751 <h3 id="_handling_a_keypress">9.2. Handling a keypress</h3>\r
752 <div class="paragraph"><p>As mentioned in "Grabbing the bindings", upon a keypress event, i3 first gets\r
753 the correct state.</p></div>\r
754 <div class="paragraph"><p>Then, it looks through all bindings and gets the one which matches the received\r
755 event.</p></div>\r
756 <div class="paragraph"><p>The bound command is parsed by the cmdparse lexer/parser, see <tt>parse_cmd</tt> in\r
757 <tt>src/cmdparse.y</tt>.</p></div>\r
758 </div>\r
759 </div>\r
760 </div>\r
761 <div class="sect1">\r
762 <h2 id="_manage_windows_src_main_c_manage_window_and_reparent_window">10. Manage windows (src/main.c, manage_window() and reparent_window())</h2>\r
763 <div class="sectionbody">\r
764 <div class="paragraph"><p><tt>manage_window()</tt> does some checks to decide whether the window should be\r
765 managed at all:</p></div>\r
766 <div class="ulist"><ul>\r
767 <li>\r
768 <p>\r
769 Windows have to be mapped, that is, visible on screen\r
770 </p>\r
771 </li>\r
772 <li>\r
773 <p>\r
774 The override_redirect must not be set. Windows with override_redirect shall\r
775    not be managed by a window manager\r
776 </p>\r
777 </li>\r
778 </ul></div>\r
779 <div class="paragraph"><p>Afterwards, i3 gets the initial geometry and reparents the window (see\r
780 <tt>reparent_window()</tt>) if it wasn’t already managed.</p></div>\r
781 <div class="paragraph"><p>Reparenting means that for each window which is reparented, a new window,\r
782 slightly larger than the original one, is created. The original window is then\r
783 reparented to the bigger one (called "frame").</p></div>\r
784 <div class="paragraph"><p>After reparenting, the window type (<tt>_NET_WM_WINDOW_TYPE</tt>) is checked to see\r
785 whether this window is a dock (<tt>_NET_WM_WINDOW_TYPE_DOCK</tt>), like dzen2 for\r
786 example. Docks are handled differently, they don’t have decorations and are not\r
787 assigned to a specific container. Instead, they are positioned at the bottom\r
788 or top of the screen (in the appropriate dock area containers). To get the\r
789 height which needs to be reserved for the window, the <tt>_NET_WM_STRUT_PARTIAL</tt>\r
790 property is used.</p></div>\r
791 <div class="paragraph"><p>Furthermore, the list of assignments (to other workspaces, which may be on\r
792 other screens) is checked. If the window matches one of the user’s criteria,\r
793 it may either be put in floating mode or moved to a different workspace. If the\r
794 target workspace is not visible, the window will not be mapped.</p></div>\r
795 </div>\r
796 </div>\r
797 <div class="sect1">\r
798 <h2 id="_what_happens_when_an_application_is_started">11. What happens when an application is started?</h2>\r
799 <div class="sectionbody">\r
800 <div class="paragraph"><p>i3 does not care about applications. All it notices is when new windows are\r
801 mapped (see <tt>src/handlers.c</tt>, <tt>handle_map_request()</tt>). The window is then\r
802 reparented (see section "Manage windows").</p></div>\r
803 <div class="paragraph"><p>After reparenting the window, <tt>render_tree()</tt> is called which renders the\r
804 internal layout table. The new window has been placed in the currently focused\r
805 container and therefore the new window and the old windows (if any) need to be\r
806 moved/resized so that the currently active layout (default/stacking/tabbed mode)\r
807 is rendered correctly. To move/resize windows, a window is &#8220;configured&#8221; in\r
808 X11-speak.</p></div>\r
809 <div class="paragraph"><p>Some applications, such as MPlayer obviously assume the window manager is\r
810 stupid and try to configure their windows by themselves. This generates an\r
811 event called configurerequest. i3 handles these events and tells the window the\r
812 size it had before the configurerequest (with the exception of not yet mapped\r
813 windows, which get configured like they want to, and floating windows, which\r
814 can reconfigure themselves).</p></div>\r
815 </div>\r
816 </div>\r
817 <div class="sect1">\r
818 <h2 id="_net_wm_state">12. _NET_WM_STATE</h2>\r
819 <div class="sectionbody">\r
820 <div class="paragraph"><p>Only the _NET_WM_STATE_FULLSCREEN and _NET_WM_STATE_DEMANDS_ATTENTION atoms\r
821 are handled.</p></div>\r
822 <div class="paragraph"><p>The former calls &#8220;toggle_fullscreen()&#8221; for the specific client which just\r
823 configures the client to use the whole screen on which it currently is.\r
824 Also, it is set as fullscreen_client for the i3Screen.</p></div>\r
825 <div class="paragraph"><p>The latter is used to set, read and display urgency hints.</p></div>\r
826 </div>\r
827 </div>\r
828 <div class="sect1">\r
829 <h2 id="_wm_name">13. WM_NAME</h2>\r
830 <div class="sectionbody">\r
831 <div class="paragraph"><p>When the WM_NAME property of a window changes, its decoration (containing the\r
832 title) is re-rendered. Note that WM_NAME is in COMPOUND_TEXT encoding which is\r
833 totally uncommon and cumbersome. Therefore, the _NET_WM_NAME atom will be used\r
834 if present.</p></div>\r
835 </div>\r
836 </div>\r
837 <div class="sect1">\r
838 <h2 id="_net_wm_name">14. _NET_WM_NAME</h2>\r
839 <div class="sectionbody">\r
840 <div class="paragraph"><p>Like WM_NAME, this atom contains the title of a window. However, _NET_WM_NAME\r
841 is encoded in UTF-8. i3 will recode it to UCS-2 in order to be able to pass it\r
842 to X. Using an appropriate font (ISO-10646), you can see most special\r
843 characters (every special character contained in your font).</p></div>\r
844 </div>\r
845 </div>\r
846 <div class="sect1">\r
847 <h2 id="_size_hints">15. Size hints</h2>\r
848 <div class="sectionbody">\r
849 <div class="paragraph"><p>Size hints specify the minimum/maximum size for a given window as well as its\r
850 aspect ratio.  This is important for clients like mplayer, who only set the\r
851 aspect ratio and resize their window to be as small as possible (but only with\r
852 some video outputs, for example in Xv, while when using x11, mplayer does the\r
853 necessary centering for itself).</p></div>\r
854 <div class="paragraph"><p>So, when an aspect ratio was specified, i3 adjusts the height of the window\r
855 until the size maintains the correct aspect ratio. For the code to do this, see\r
856 src/layout.c, function resize_client().</p></div>\r
857 </div>\r
858 </div>\r
859 <div class="sect1">\r
860 <h2 id="_rendering_src_layout_c_render_layout_and_render_container">16. Rendering (src/layout.c, render_layout() and render_container())</h2>\r
861 <div class="sectionbody">\r
862 <div class="paragraph"><p>Rendering in i3 version 4 is the step which assigns the correct sizes for\r
863 borders, decoration windows, child windows and the stacking order of all\r
864 windows. In a separate step (<tt>x_push_changes()</tt>), these changes are pushed to\r
865 X11.</p></div>\r
866 <div class="paragraph"><p>Keep in mind that all these properties (<tt>rect</tt>, <tt>window_rect</tt> and <tt>deco_rect</tt>)\r
867 are temporary, meaning they will be overwritten by calling <tt>render_con</tt>.\r
868 Persistent position/size information is kept in <tt>geometry</tt>.</p></div>\r
869 <div class="paragraph"><p>The entry point for every rendering operation (except for the case of moving\r
870 floating windows around) currently is <tt>tree_render()</tt> which will re-render\r
871 everything that’s necessary (for every output, only the currently displayed\r
872 workspace is rendered). This behavior is expected to change in the future,\r
873 since for a lot of updates, re-rendering everything is not actually necessary.\r
874 Focus was on getting it working correct, not getting it work very fast.</p></div>\r
875 <div class="paragraph"><p>What <tt>tree_render()</tt> actually does is calling <tt>render_con()</tt> on the root\r
876 container and then pushing the changes to X11. The following sections talk\r
877 about the different rendering steps, in the order of "top of the tree" (root\r
878 container) to the bottom.</p></div>\r
879 <div class="sect2">\r
880 <h3 id="_rendering_the_root_container">16.1. Rendering the root container</h3>\r
881 <div class="paragraph"><p>The i3 root container (<tt>con-&gt;type == CT_ROOT</tt>) represents the X11 root window.\r
882 It contains one child container for every output (like LVDS1, VGA1, …), which\r
883 is available on your computer.</p></div>\r
884 <div class="paragraph"><p>Rendering the root will first render all tiling windows and then all floating\r
885 windows. This is necessary because a floating window can be positioned in such\r
886 a way that it is visible on two different outputs. Therefore, by first\r
887 rendering all the tiling windows (of all outputs), we make sure that floating\r
888 windows can never be obscured by tiling windows.</p></div>\r
889 <div class="paragraph"><p>Essentially, though, this code path will just call <tt>render_con()</tt> for every\r
890 output and <tt>x_raise_con(); render_con()</tt> for every floating window.</p></div>\r
891 <div class="paragraph"><p>In the special case of having a "global fullscreen" window (fullscreen mode\r
892 spanning all outputs), a shortcut is taken and <tt>x_raise_con(); render_con()</tt> is\r
893 only called for the global fullscreen window.</p></div>\r
894 </div>\r
895 <div class="sect2">\r
896 <h3 id="_rendering_an_output">16.2. Rendering an output</h3>\r
897 <div class="paragraph"><p>Output containers (<tt>con-&gt;layout == L_OUTPUT</tt>) represent a hardware output like\r
898 LVDS1, VGA1, etc. An output container has three children (at the moment): One\r
899 content container (having workspaces as children) and the top/bottom dock area\r
900 containers.</p></div>\r
901 <div class="paragraph"><p>The rendering happens in the function <tt>render_l_output()</tt> in the following\r
902 steps:</p></div>\r
903 <div class="olist arabic"><ol class="arabic">\r
904 <li>\r
905 <p>\r
906 Find the content container (<tt>con-&gt;type == CT_CON</tt>)\r
907 </p>\r
908 </li>\r
909 <li>\r
910 <p>\r
911 Get the currently visible workspace (<tt>con_get_fullscreen_con(content,\r
912    CF_OUTPUT)</tt>).\r
913 </p>\r
914 </li>\r
915 <li>\r
916 <p>\r
917 If there is a fullscreened window on that workspace, directly render it and\r
918    return, thus ignoring the dock areas.\r
919 </p>\r
920 </li>\r
921 <li>\r
922 <p>\r
923 Sum up the space used by all the dock windows (they have a variable height\r
924    only).\r
925 </p>\r
926 </li>\r
927 <li>\r
928 <p>\r
929 Set the workspace rects (x/y/width/height) based on the position of the\r
930    output (stored in <tt>con-&gt;rect</tt>) and the usable space\r
931    (<tt>con-&gt;rect.{width,height}</tt> without the space used for dock windows).\r
932 </p>\r
933 </li>\r
934 <li>\r
935 <p>\r
936 Recursively raise and render the output’s child containers (meaning dock\r
937    area containers and the content container).\r
938 </p>\r
939 </li>\r
940 </ol></div>\r
941 </div>\r
942 <div class="sect2">\r
943 <h3 id="_rendering_a_workspace_or_split_container">16.3. Rendering a workspace or split container</h3>\r
944 <div class="paragraph"><p>From here on, there really is no difference anymore. All containers are of\r
945 <tt>con-&gt;type == CT_CON</tt> (whether workspace or split container) and some of them\r
946 have a <tt>con-&gt;window</tt>, meaning they represent an actual window instead of a\r
947 split container.</p></div>\r
948 <div class="sect3">\r
949 <h4 id="_default_layout">16.3.1. Default layout</h4>\r
950 <div class="paragraph"><p>In default layout, containers are placed horizontally or vertically next to\r
951 each other (depending on the <tt>con-&gt;orientation</tt>). If a child is a leaf node (as\r
952 opposed to a split container) and has border style "normal", appropriate space\r
953 will be reserved for its window decoration.</p></div>\r
954 </div>\r
955 <div class="sect3">\r
956 <h4 id="_stacked_layout">16.3.2. Stacked layout</h4>\r
957 <div class="paragraph"><p>In stacked layout, only the focused window is actually shown (this is achieved\r
958 by calling <tt>x_raise_con()</tt> in reverse focus order at the end of <tt>render_con()</tt>).</p></div>\r
959 <div class="paragraph"><p>The available space for the focused window is the size of the container minus\r
960 the height of the window decoration for all windows inside this stacked\r
961 container.</p></div>\r
962 <div class="paragraph"><p>If border style is "1pixel" or "none", no window decoration height will be\r
963 reserved (or displayed later on), unless there is more than one window inside\r
964 the stacked container.</p></div>\r
965 </div>\r
966 <div class="sect3">\r
967 <h4 id="_tabbed_layout">16.3.3. Tabbed layout</h4>\r
968 <div class="paragraph"><p>Tabbed layout works precisely like stacked layout, but the window decoration\r
969 position/size is different: They are placed next to each other on a single line\r
970 (fixed height).</p></div>\r
971 </div>\r
972 <div class="sect3">\r
973 <h4 id="_dock_area_layout">16.3.4. Dock area layout</h4>\r
974 <div class="paragraph"><p>This is a special case. Users cannot choose the dock area layout, but it will be\r
975 set for the dock area containers. In the dockarea layout (at the moment!),\r
976 windows will be placed above each other.</p></div>\r
977 </div>\r
978 </div>\r
979 <div class="sect2">\r
980 <h3 id="_rendering_a_window">16.4. Rendering a window</h3>\r
981 <div class="paragraph"><p>A window’s size and position will be determined in the following way:</p></div>\r
982 <div class="olist arabic"><ol class="arabic">\r
983 <li>\r
984 <p>\r
985 Subtract the border if border style is not "none" (but "normal" or "1pixel").\r
986 </p>\r
987 </li>\r
988 <li>\r
989 <p>\r
990 Subtract the X11 border, if the window has an X11 border &gt; 0.\r
991 </p>\r
992 </li>\r
993 <li>\r
994 <p>\r
995 Obey the aspect ratio of the window (think MPlayer).\r
996 </p>\r
997 </li>\r
998 <li>\r
999 <p>\r
1000 Obey the height- and width-increments of the window (think terminal emulator\r
1001    which can only be resized in one-line or one-character steps).\r
1002 </p>\r
1003 </li>\r
1004 </ol></div>\r
1005 </div>\r
1006 </div>\r
1007 </div>\r
1008 <div class="sect1">\r
1009 <h2 id="_pushing_updates_to_x11_drawing">17. Pushing updates to X11 / Drawing</h2>\r
1010 <div class="sectionbody">\r
1011 <div class="paragraph"><p>A big problem with i3 before version 4 was that we just sent requests to X11\r
1012 anywhere in the source code. This was bad because nobody could understand the\r
1013 entirety of our interaction with X11, it lead to subtle bugs and a lot of edge\r
1014 cases which we had to consider all over again.</p></div>\r
1015 <div class="paragraph"><p>Therefore, since version 4, we have a single file, <tt>src/x.c</tt>, which is\r
1016 responsible for repeatedly transferring parts of our tree datastructure to X11.</p></div>\r
1017 <div class="paragraph"><p><tt>src/x.c</tt> consists of multiple parts:</p></div>\r
1018 <div class="olist arabic"><ol class="arabic">\r
1019 <li>\r
1020 <p>\r
1021 The state pushing: <tt>x_push_changes()</tt>, which calls <tt>x_push_node()</tt>.\r
1022 </p>\r
1023 </li>\r
1024 <li>\r
1025 <p>\r
1026 State modification functions: <tt>x_con_init</tt>, <tt>x_reinit</tt>,\r
1027    <tt>x_reparent_child</tt>, <tt>x_move_win</tt>, <tt>x_con_kill</tt>, <tt>x_raise_con</tt>, <tt>x_set_name</tt>\r
1028    and <tt>x_set_warp_to</tt>.\r
1029 </p>\r
1030 </li>\r
1031 <li>\r
1032 <p>\r
1033 Expose event handling (drawing decorations): <tt>x_deco_recurse()</tt> and\r
1034    <tt>x_draw_decoration()</tt>.\r
1035 </p>\r
1036 </li>\r
1037 </ol></div>\r
1038 <div class="sect2">\r
1039 <h3 id="_pushing_state_to_x11">17.1. Pushing state to X11</h3>\r
1040 <div class="paragraph"><p>In general, the function <tt>x_push_changes</tt> should be called to push state\r
1041 changes. Only when the scope of the state change is clearly defined (for\r
1042 example only the title of a window) and its impact is known beforehand, one can\r
1043 optimize this and call <tt>x_push_node</tt> on the appropriate con directly.</p></div>\r
1044 <div class="paragraph"><p><tt>x_push_changes</tt> works in the following steps:</p></div>\r
1045 <div class="olist arabic"><ol class="arabic">\r
1046 <li>\r
1047 <p>\r
1048 Clear the eventmask for all mapped windows. This leads to not getting\r
1049    useless ConfigureNotify or EnterNotify events which are caused by our\r
1050    requests. In general, we only want to handle user input.\r
1051 </p>\r
1052 </li>\r
1053 <li>\r
1054 <p>\r
1055 Stack windows above each other, in reverse stack order (starting with the\r
1056    most obscured/bottom window). This is relevant for floating windows which\r
1057    can overlap each other, but also for tiling windows in stacked or tabbed\r
1058    containers. We also update the <tt>_NET_CLIENT_LIST_STACKING</tt> hint which is\r
1059    necessary for tab drag and drop in Chromium.\r
1060 </p>\r
1061 </li>\r
1062 <li>\r
1063 <p>\r
1064 <tt>x_push_node</tt> will be called for the root container, recursively calling\r
1065    itself for the container’s children. This function actually pushes the\r
1066    state, see the next paragraph.\r
1067 </p>\r
1068 </li>\r
1069 <li>\r
1070 <p>\r
1071 If the pointer needs to be warped to a different position (for example when\r
1072    changing focus to a different output), it will be warped now.\r
1073 </p>\r
1074 </li>\r
1075 <li>\r
1076 <p>\r
1077 The eventmask is restored for all mapped windows.\r
1078 </p>\r
1079 </li>\r
1080 <li>\r
1081 <p>\r
1082 Window decorations will be rendered by calling <tt>x_deco_recurse</tt> on the root\r
1083    container, which then recursively calls itself for the children.\r
1084 </p>\r
1085 </li>\r
1086 <li>\r
1087 <p>\r
1088 If the input focus needs to be changed (because the user focused a different\r
1089    window), it will be updated now.\r
1090 </p>\r
1091 </li>\r
1092 <li>\r
1093 <p>\r
1094 <tt>x_push_node_unmaps</tt> will be called for the root container. This function\r
1095    only pushes UnmapWindow requests. Separating the state pushing is necessary\r
1096    to handle fullscreen windows (and workspace switches) in a smooth fashion:\r
1097    The newly visible windows should be visible before the old windows are\r
1098    unmapped.\r
1099 </p>\r
1100 </li>\r
1101 </ol></div>\r
1102 <div class="paragraph"><p><tt>x_push_node</tt> works in the following steps:</p></div>\r
1103 <div class="olist arabic"><ol class="arabic">\r
1104 <li>\r
1105 <p>\r
1106 Update the window’s <tt>WM_NAME</tt>, if changed (the <tt>WM_NAME</tt> is set on i3\r
1107    containers mainly for debugging purposes).\r
1108 </p>\r
1109 </li>\r
1110 <li>\r
1111 <p>\r
1112 Reparents a child window into the i3 container if the container was created\r
1113    for a specific managed window.\r
1114 </p>\r
1115 </li>\r
1116 <li>\r
1117 <p>\r
1118 If the size/position of the i3 container changed (due to opening a new\r
1119    window or switching layouts for example), the window will be reconfigured.\r
1120    Also, the pixmap which is used to draw the window decoration/border on is\r
1121    reconfigured (pixmaps are size-dependent).\r
1122 </p>\r
1123 </li>\r
1124 <li>\r
1125 <p>\r
1126 Size/position for the child window is adjusted.\r
1127 </p>\r
1128 </li>\r
1129 <li>\r
1130 <p>\r
1131 The i3 container is mapped if it should be visible and was not yet mapped.\r
1132    When mapping, <tt>WM_STATE</tt> is set to <tt>WM_STATE_NORMAL</tt>. Also, the eventmask of\r
1133    the child window is updated and the i3 container’s contents are copied from\r
1134    the pixmap.\r
1135 </p>\r
1136 </li>\r
1137 <li>\r
1138 <p>\r
1139 <tt>x_push_node</tt> is called recursively for all children of the current\r
1140    container.\r
1141 </p>\r
1142 </li>\r
1143 </ol></div>\r
1144 <div class="paragraph"><p><tt>x_push_node_unmaps</tt> handles the remaining case of an i3 container being\r
1145 unmapped if it should not be visible anymore. <tt>WM_STATE</tt> will be set to\r
1146 <tt>WM_STATE_WITHDRAWN</tt>.</p></div>\r
1147 </div>\r
1148 <div class="sect2">\r
1149 <h3 id="_drawing_window_decorations_borders_backgrounds">17.2. Drawing window decorations/borders/backgrounds</h3>\r
1150 <div class="paragraph"><p><tt>x_draw_decoration</tt> draws window decorations. It is run for every leaf\r
1151 container (representing an actual X11 window) and for every non-leaf container\r
1152 which is in a stacked/tabbed container (because stacked/tabbed containers\r
1153 display a window decoration for split containers, which consists of a representation\r
1154 of the child container&#8217;s names.</p></div>\r
1155 <div class="paragraph"><p>Then, parameters are collected to be able to determine whether this decoration\r
1156 drawing is actually necessary or was already done. This saves a substantial\r
1157 number of redraws (depending on your workload, but far over 50%).</p></div>\r
1158 <div class="paragraph"><p>Assuming that we need to draw this decoration, we start by filling the empty\r
1159 space around the child window (think of MPlayer with a specific aspect ratio)\r
1160 in the user-configured client background color.</p></div>\r
1161 <div class="paragraph"><p>Afterwards, we draw the appropriate border (in case of border styles "normal"\r
1162 and "1pixel") and the top bar (in case of border style "normal").</p></div>\r
1163 <div class="paragraph"><p>The last step is drawing the window title on the top bar.</p></div>\r
1164 </div>\r
1165 </div>\r
1166 </div>\r
1167 <div class="sect1">\r
1168 <h2 id="_user_commands_parser_specs_commands_spec">18. User commands (parser-specs/commands.spec)</h2>\r
1169 <div class="sectionbody">\r
1170 <div class="paragraph"><p>In the configuration file and when using i3 interactively (with <tt>i3-msg</tt>, for\r
1171 example), you use commands to make i3 do things, like focus a different window,\r
1172 set a window to fullscreen, and so on. An example command is <tt>floating enable</tt>,\r
1173 which enables floating mode for the currently focused window. See the\r
1174 appropriate section in the <a href="userguide.html">User’s Guide</a> for a reference of\r
1175 all commands.</p></div>\r
1176 <div class="paragraph"><p>In earlier versions of i3, interpreting these commands was done using lex and\r
1177 yacc, but experience has shown that lex and yacc are not well suited for our\r
1178 command language. Therefore, starting from version 4.2, we use a custom parser\r
1179 for user commands and the configuration file.\r
1180 The input specification for this parser can be found in the file\r
1181 <tt>parser-specs/*.spec</tt>. Should you happen to use Vim as an editor, use\r
1182 :source parser-specs/highlighting.vim to get syntax highlighting for this file\r
1183 (highlighting files for other editors are welcome).</p></div>\r
1184 <div class="listingblock">\r
1185 <div class="title">Excerpt from commands.spec</div>\r
1186 <div class="content">\r
1187 <pre><tt>state INITIAL:\r
1188   '[' -&gt; call cmd_criteria_init(); CRITERIA\r
1189   'move' -&gt; MOVE\r
1190   'exec' -&gt; EXEC\r
1191   'workspace' -&gt; WORKSPACE\r
1192   'exit' -&gt; call cmd_exit()\r
1193   'restart' -&gt; call cmd_restart()\r
1194   'reload' -&gt; call cmd_reload()</tt></pre>\r
1195 </div></div>\r
1196 <div class="paragraph"><p>The input specification is written in an extremely simple format. The\r
1197 specification is then converted into C code by the Perl script\r
1198 generate-commands-parser.pl (the output file names begin with GENERATED and the\r
1199 files are stored in the <tt>include</tt> directory). The parser implementation\r
1200 <tt>src/commands_parser.c</tt> includes the generated C code at compile-time.</p></div>\r
1201 <div class="paragraph"><p>The above excerpt from commands.spec illustrates nearly all features of our\r
1202 specification format: You describe different states and what can happen within\r
1203 each state. State names are all-caps; the state in the above excerpt is called\r
1204 INITIAL. A list of tokens and their actions (separated by an ASCII arrow)\r
1205 follows. In the excerpt, all tokens are literals, that is, simple text strings\r
1206 which will be compared with the input. An action is either the name of a state\r
1207 in which the parser will transition into, or the keyword <em>call</em>, followed by\r
1208 the name of a function (and optionally a state).</p></div>\r
1209 <div class="sect2">\r
1210 <h3 id="_example_the_workspace_state">18.1. Example: The WORKSPACE state</h3>\r
1211 <div class="paragraph"><p>Let’s have a look at the WORKSPACE state, which is a good example of all\r
1212 features. This is its definition:</p></div>\r
1213 <div class="listingblock">\r
1214 <div class="title">WORKSPACE state (commands.spec)</div>\r
1215 <div class="content">\r
1216 <pre><tt># workspace next|prev|next_on_output|prev_on_output\r
1217 # workspace back_and_forth\r
1218 # workspace &lt;name&gt;\r
1219 # workspace number &lt;number&gt;\r
1220 state WORKSPACE:\r
1221   direction = 'next_on_output', 'prev_on_output', 'next', 'prev'\r
1222       -&gt; call cmd_workspace($direction)\r
1223   'back_and_forth'\r
1224       -&gt; call cmd_workspace_back_and_forth()\r
1225   'number'\r
1226       -&gt; WORKSPACE_NUMBER\r
1227   workspace = string\r
1228       -&gt; call cmd_workspace_name($workspace)</tt></pre>\r
1229 </div></div>\r
1230 <div class="paragraph"><p>As you can see from the commands, there are multiple different valid variants\r
1231 of the workspace command:</p></div>\r
1232 <div class="dlist"><dl>\r
1233 <dt class="hdlist1">\r
1234 workspace &lt;direction&gt;\r
1235 </dt>\r
1236 <dd>\r
1237 <p>\r
1238         The word <em>workspace</em> can be followed by any of the tokens <em>next</em>,\r
1239         <em>prev</em>, <em>next_on_output</em> or <em>prev_on_output</em>. This command will\r
1240         switch to the next or previous workspace (optionally on the same\r
1241         output).<br />\r
1242         There is one function called <tt>cmd_workspace</tt>, which is defined\r
1243         in <tt>src/commands.c</tt>. It will handle this kind of command. To know which\r
1244         direction was specified, the direction token is stored on the stack\r
1245         with the name "direction", which is what the "direction = " means in\r
1246         the beginning.<br />\r
1247 </p>\r
1248 </dd>\r
1249 </dl></div>\r
1250 <div class="admonitionblock">\r
1251 <table><tr>\r
1252 <td class="icon">\r
1253 <div class="title">Note</div>\r
1254 </td>\r
1255 <td class="content">Note that you can specify multiple literals in the same line. This has\r
1256         exactly the same effect as if you specified <tt>direction =\r
1257         'next_on_output' -&gt; call cmd_workspace($direction)</tt> and so forth.<br /></td>\r
1258 </tr></table>\r
1259 </div>\r
1260 <div class="admonitionblock">\r
1261 <table><tr>\r
1262 <td class="icon">\r
1263 <div class="title">Note</div>\r
1264 </td>\r
1265 <td class="content">Also note that the order of literals is important here: If <em>next</em> were\r
1266         ordered before <em>next_on_output</em>, then <em>next_on_output</em> would never\r
1267         match.</td>\r
1268 </tr></table>\r
1269 </div>\r
1270 <div class="dlist"><dl>\r
1271 <dt class="hdlist1">\r
1272 workspace back_and_forth\r
1273 </dt>\r
1274 <dd>\r
1275 <p>\r
1276         This is a very simple case: When the literal <em>back_and_forth</em> is found\r
1277         in the input, the function <tt>cmd_workspace_back_and_forth</tt> will be\r
1278         called without parameters and the parser will return to the INITIAL\r
1279         state (since no other state was specified).\r
1280 </p>\r
1281 </dd>\r
1282 <dt class="hdlist1">\r
1283 workspace &lt;name&gt;\r
1284 </dt>\r
1285 <dd>\r
1286 <p>\r
1287         In this case, the workspace command is followed by an arbitrary string,\r
1288         possibly in quotes, for example "workspace 3" or "workspace bleh".<br />\r
1289         This is the first time that the token is actually not a literal (not in\r
1290         single quotes), but just called string. Other possible tokens are word\r
1291         (the same as string, but stops matching at a whitespace) and end\r
1292         (matches the end of the input).\r
1293 </p>\r
1294 </dd>\r
1295 <dt class="hdlist1">\r
1296 workspace number &lt;number&gt;\r
1297 </dt>\r
1298 <dd>\r
1299 <p>\r
1300         The workspace command has to be followed by the keyword <tt>number</tt>. It\r
1301         then transitions into the state <tt>WORKSPACE_NUMBER</tt>, where the actual\r
1302         parameter will be read.\r
1303 </p>\r
1304 </dd>\r
1305 </dl></div>\r
1306 </div>\r
1307 <div class="sect2">\r
1308 <h3 id="_introducing_a_new_command">18.2. Introducing a new command</h3>\r
1309 <div class="paragraph"><p>The following steps have to be taken in order to properly introduce a new\r
1310 command (or possibly extend an existing command):</p></div>\r
1311 <div class="olist arabic"><ol class="arabic">\r
1312 <li>\r
1313 <p>\r
1314 Define a function beginning with <tt>cmd_</tt> in the file <tt>src/commands.c</tt>. Copy\r
1315    the prototype of an existing function.\r
1316 </p>\r
1317 </li>\r
1318 <li>\r
1319 <p>\r
1320 After adding a comment on what the function does, copy the comment and\r
1321    function definition to <tt>include/commands.h</tt>. Make the comment in the header\r
1322    file use double asterisks to make doxygen pick it up.\r
1323 </p>\r
1324 </li>\r
1325 <li>\r
1326 <p>\r
1327 Write a test case (or extend an existing test case) for your feature, see\r
1328    <a href="testsuite.html">i3 testsuite</a>. For now, it is sufficient to simply call\r
1329    your command in all the various possible ways.\r
1330 </p>\r
1331 </li>\r
1332 <li>\r
1333 <p>\r
1334 Extend the parser specification in <tt>parser-specs/commands.spec</tt>. Run the\r
1335    testsuite and see if your new function gets called with the appropriate\r
1336    arguments for the appropriate input.\r
1337 </p>\r
1338 </li>\r
1339 <li>\r
1340 <p>\r
1341 Actually implement the feature.\r
1342 </p>\r
1343 </li>\r
1344 <li>\r
1345 <p>\r
1346 Document the feature in the <a href="userguide.html">User’s Guide</a>.\r
1347 </p>\r
1348 </li>\r
1349 </ol></div>\r
1350 </div>\r
1351 </div>\r
1352 </div>\r
1353 <div class="sect1">\r
1354 <h2 id="_moving_containers">19. Moving containers</h2>\r
1355 <div class="sectionbody">\r
1356 <div class="paragraph"><p>The movement code is pretty delicate. You need to consider all cases before\r
1357 making any changes or before being able to fully understand how it works.</p></div>\r
1358 <div class="sect2">\r
1359 <h3 id="_case_1_moving_inside_the_same_container">19.1. Case 1: Moving inside the same container</h3>\r
1360 <div class="paragraph"><p>The reference layout for this case is a single workspace in horizontal\r
1361 orientation with two containers on it. Focus is on the left container (1).</p></div>\r
1362 <div class="tableblock">\r
1363 <table rules="all"\r
1364 width="15%"\r
1365 frame="border"\r
1366 cellspacing="0" cellpadding="4">\r
1367 <col width="50%" />\r
1368 <col width="50%" />\r
1369 <tbody>\r
1370 <tr>\r
1371 <td align="center" valign="top"><p class="table">1</p></td>\r
1372 <td align="center" valign="top"><p class="table">2</p></td>\r
1373 </tr>\r
1374 </tbody>\r
1375 </table>\r
1376 </div>\r
1377 <div class="paragraph"><p>When moving the left window to the right (command <tt>move right</tt>), tree_move will\r
1378 look for a container with horizontal orientation and finds the parent of the\r
1379 left container, that is, the workspace. Afterwards, it runs the code branch\r
1380 commented with "the easy case": it calls TAILQ_NEXT to get the container right\r
1381 of the current one and swaps both containers.</p></div>\r
1382 </div>\r
1383 <div class="sect2">\r
1384 <h3 id="_case_2_move_a_container_into_a_split_container">19.2. Case 2: Move a container into a split container</h3>\r
1385 <div class="paragraph"><p>The reference layout for this case is a horizontal workspace with two\r
1386 containers. The right container is a v-split with two containers. Focus is on\r
1387 the left container (1).</p></div>\r
1388 <div class="tableblock">\r
1389 <table rules="all"\r
1390 width="15%"\r
1391 frame="border"\r
1392 cellspacing="0" cellpadding="4">\r
1393 <col width="50%" />\r
1394 <col width="50%" />\r
1395 <tbody>\r
1396 <tr>\r
1397 <td rowspan="2" align="center" valign="middle"><p class="table">1</p></td>\r
1398 <td align="center" valign="top"><p class="table">2</p></td>\r
1399 </tr>\r
1400 <tr>\r
1401 <td align="center" valign="top"><p class="table">3</p></td>\r
1402 </tr>\r
1403 </tbody>\r
1404 </table>\r
1405 </div>\r
1406 <div class="paragraph"><p>When moving to the right (command <tt>move right</tt>), i3 will work like in case 1\r
1407 ("the easy case"). However, as the right container is not a leaf container, but\r
1408 a v-split, the left container (1) will be inserted at the right position (below\r
1409 2, assuming that 2 is focused inside the v-split) by calling <tt>insert_con_into</tt>.</p></div>\r
1410 <div class="paragraph"><p><tt>insert_con_into</tt> detaches the container from its parent and inserts it\r
1411 before/after the given target container. Afterwards, the on_remove_child\r
1412 callback is called on the old parent container which will then be closed, if\r
1413 empty.</p></div>\r
1414 <div class="paragraph"><p>Afterwards, <tt>con_focus</tt> will be called to fix the focus stack and the tree will\r
1415 be flattened.</p></div>\r
1416 </div>\r
1417 <div class="sect2">\r
1418 <h3 id="_case_3_moving_to_non_existent_top_bottom">19.3. Case 3: Moving to non-existent top/bottom</h3>\r
1419 <div class="paragraph"><p>Like in case 1, the reference layout for this case is a single workspace in\r
1420 horizontal orientation with two containers on it. Focus is on the left\r
1421 container:</p></div>\r
1422 <div class="tableblock">\r
1423 <table rules="all"\r
1424 width="15%"\r
1425 frame="border"\r
1426 cellspacing="0" cellpadding="4">\r
1427 <col width="50%" />\r
1428 <col width="50%" />\r
1429 <tbody>\r
1430 <tr>\r
1431 <td align="center" valign="top"><p class="table">1</p></td>\r
1432 <td align="center" valign="top"><p class="table">2</p></td>\r
1433 </tr>\r
1434 </tbody>\r
1435 </table>\r
1436 </div>\r
1437 <div class="paragraph"><p>This time however, the command is <tt>move up</tt> or <tt>move down</tt>. tree_move will look\r
1438 for a container with vertical orientation. As it will not find any,\r
1439 <tt>same_orientation</tt> is NULL and therefore i3 will perform a forced orientation\r
1440 change on the workspace by creating a new h-split container, moving the\r
1441 workspace contents into it and then changing the workspace orientation to\r
1442 vertical. Now it will again search for parent containers with vertical\r
1443 orientation and it will find the workspace.</p></div>\r
1444 <div class="paragraph"><p>This time, the easy case code path will not be run as we are not moving inside\r
1445 the same container. Instead, <tt>insert_con_into</tt> will be called with the focused\r
1446 container and the container above/below the current one (on the level of\r
1447 <tt>same_orientation</tt>).</p></div>\r
1448 <div class="paragraph"><p>Now, <tt>con_focus</tt> will be called to fix the focus stack and the tree will be\r
1449 flattened.</p></div>\r
1450 </div>\r
1451 <div class="sect2">\r
1452 <h3 id="_case_4_moving_to_existent_top_bottom">19.4. Case 4: Moving to existent top/bottom</h3>\r
1453 <div class="paragraph"><p>The reference layout for this case is a vertical workspace with two containers.\r
1454 The bottom one is a h-split containing two containers (1 and 2). Focus is on\r
1455 the bottom left container (1).</p></div>\r
1456 <div class="tableblock">\r
1457 <table rules="all"\r
1458 width="15%"\r
1459 frame="border"\r
1460 cellspacing="0" cellpadding="4">\r
1461 <col width="50%" />\r
1462 <col width="50%" />\r
1463 <tbody>\r
1464 <tr>\r
1465 <td colspan="2" align="center" valign="top"><p class="table">3</p></td>\r
1466 </tr>\r
1467 <tr>\r
1468 <td align="center" valign="top"><p class="table">1</p></td>\r
1469 <td align="center" valign="top"><p class="table">2</p></td>\r
1470 </tr>\r
1471 </tbody>\r
1472 </table>\r
1473 </div>\r
1474 <div class="paragraph"><p>This case is very much like case 3, only this time the forced workspace\r
1475 orientation change does not need to be performed because the workspace already\r
1476 is in vertical orientation.</p></div>\r
1477 </div>\r
1478 <div class="sect2">\r
1479 <h3 id="_case_5_moving_in_one_child_h_split">19.5. Case 5: Moving in one-child h-split</h3>\r
1480 <div class="paragraph"><p>The reference layout for this case is a horizontal workspace with two\r
1481 containers having a v-split on the left side with a one-child h-split on the\r
1482 bottom. Focus is on the bottom left container (2(h)):</p></div>\r
1483 <div class="tableblock">\r
1484 <table rules="all"\r
1485 width="15%"\r
1486 frame="border"\r
1487 cellspacing="0" cellpadding="4">\r
1488 <col width="50%" />\r
1489 <col width="50%" />\r
1490 <tbody>\r
1491 <tr>\r
1492 <td align="center" valign="top"><p class="table">1</p></td>\r
1493 <td rowspan="2" align="center" valign="middle"><p class="table">3</p></td>\r
1494 </tr>\r
1495 <tr>\r
1496 <td align="center" valign="top"><p class="table">2(h)</p></td>\r
1497 </tr>\r
1498 </tbody>\r
1499 </table>\r
1500 </div>\r
1501 <div class="paragraph"><p>In this case, <tt>same_orientation</tt> will be set to the h-split container around\r
1502 the focused container. However, when trying the easy case, the next/previous\r
1503 container <tt>swap</tt> will be NULL. Therefore, i3 will search again for a\r
1504 <tt>same_orientation</tt> container, this time starting from the parent of the h-split\r
1505 container.</p></div>\r
1506 <div class="paragraph"><p>After determining a new <tt>same_orientation</tt> container (if it is NULL, the\r
1507 orientation will be force-changed), this case is equivalent to case 2 or case\r
1508 4.</p></div>\r
1509 </div>\r
1510 <div class="sect2">\r
1511 <h3 id="_case_6_floating_containers">19.6. Case 6: Floating containers</h3>\r
1512 <div class="paragraph"><p>The reference layout for this case is a horizontal workspace with two\r
1513 containers plus one floating h-split container. Focus is on the floating\r
1514 container.</p></div>\r
1515 <div class="paragraph"><p>TODO: nice illustration. table not possible?</p></div>\r
1516 <div class="paragraph"><p>When moving up/down, the container needs to leave the floating container and it\r
1517 needs to be placed on the workspace (at workspace level). This is accomplished\r
1518 by calling the function <tt>attach_to_workspace</tt>.</p></div>\r
1519 </div>\r
1520 </div>\r
1521 </div>\r
1522 <div class="sect1">\r
1523 <h2 id="_click_handling">20. Click handling</h2>\r
1524 <div class="sectionbody">\r
1525 <div class="paragraph"><p>Without much ado, here is the list of cases which need to be considered:</p></div>\r
1526 <div class="ulist"><ul>\r
1527 <li>\r
1528 <p>\r
1529 click to focus (tiling + floating) and raise (floating)\r
1530 </p>\r
1531 </li>\r
1532 <li>\r
1533 <p>\r
1534 click to focus/raise when in stacked/tabbed mode\r
1535 </p>\r
1536 </li>\r
1537 <li>\r
1538 <p>\r
1539 floating_modifier + left mouse button to drag a floating con\r
1540 </p>\r
1541 </li>\r
1542 <li>\r
1543 <p>\r
1544 floating_modifier + right mouse button to resize a floating con\r
1545 </p>\r
1546 </li>\r
1547 <li>\r
1548 <p>\r
1549 click on decoration in a floating con to either initiate a resize (if there\r
1550   is more than one child in the floating con) or to drag the\r
1551   floating con (if it’s the one at the top).\r
1552 </p>\r
1553 </li>\r
1554 <li>\r
1555 <p>\r
1556 click on border in a floating con to resize the floating con\r
1557 </p>\r
1558 </li>\r
1559 <li>\r
1560 <p>\r
1561 floating_modifier + right mouse button to resize a tiling con\r
1562 </p>\r
1563 </li>\r
1564 <li>\r
1565 <p>\r
1566 click on border/decoration to resize a tiling con\r
1567 </p>\r
1568 </li>\r
1569 </ul></div>\r
1570 </div>\r
1571 </div>\r
1572 <div class="sect1">\r
1573 <h2 id="_gotchas">21. Gotchas</h2>\r
1574 <div class="sectionbody">\r
1575 <div class="ulist"><ul>\r
1576 <li>\r
1577 <p>\r
1578 Forgetting to call <tt>xcb_flush(conn);</tt> after sending a request. This usually\r
1579   leads to code which looks like it works fine but which does not work under\r
1580   certain conditions.\r
1581 </p>\r
1582 </li>\r
1583 <li>\r
1584 <p>\r
1585 Forgetting to call <tt>floating_fix_coordinates(con, old_rect, new_rect)</tt> after\r
1586   moving workspaces across outputs. Coordinates for floating containers are\r
1587   not relative to workspace boundaries, so you must correct their coordinates\r
1588   or those containers will show up in the wrong workspace or not at all.\r
1589 </p>\r
1590 </li>\r
1591 </ul></div>\r
1592 </div>\r
1593 </div>\r
1594 <div class="sect1">\r
1595 <h2 id="_thought_experiments">22. Thought experiments</h2>\r
1596 <div class="sectionbody">\r
1597 <div class="paragraph"><p>In this section, we collect thought experiments, so that we don’t forget our\r
1598 thoughts about specific topics. They are not necessary to get into hacking i3,\r
1599 but if you are interested in one of the topics they cover, you should read them\r
1600 before asking us why things are the way they are or why we don’t implement\r
1601 things.</p></div>\r
1602 <div class="sect2">\r
1603 <h3 id="_using_cgroups_per_workspace">22.1. Using cgroups per workspace</h3>\r
1604 <div class="paragraph"><p>cgroups (control groups) are a linux-only feature which provides the ability to\r
1605 group multiple processes. For each group, you can individually set resource\r
1606 limits, like allowed memory usage. Furthermore, and more importantly for our\r
1607 purposes, they serve as a namespace, a label which you can attach to processes\r
1608 and their children.</p></div>\r
1609 <div class="paragraph"><p>One interesting use for cgroups is having one cgroup per workspace (or\r
1610 container, doesn’t really matter). That way, you could set different priorities\r
1611 and have a workspace for important stuff (say, writing a LaTeX document or\r
1612 programming) and a workspace for unimportant background stuff (say,\r
1613 JDownloader). Both tasks can obviously consume a lot of I/O resources, but in\r
1614 this example it doesn’t really matter if JDownloader unpacks the download a\r
1615 minute earlier or not. However, your compiler should work as fast as possible.\r
1616 Having one cgroup per workspace, you would assign more resources to the\r
1617 programming workspace.</p></div>\r
1618 <div class="paragraph"><p>Another interesting feature is that an inherent problem of the workspace\r
1619 concept could be solved by using cgroups: When starting an application on\r
1620 workspace 1, then switching to workspace 2, you will get the application’s\r
1621 window(s) on workspace 2 instead of the one you started it on. This is because\r
1622 the window manager does not have any mapping between the process it starts (or\r
1623 gets started in any way) and the window(s) which appear.</p></div>\r
1624 <div class="paragraph"><p>Imagine for example using dmenu: The user starts dmenu by pressing Mod+d, dmenu\r
1625 gets started with PID 3390. The user then decides to launch Firefox, which\r
1626 takes a long time. So they enter firefox into dmenu and press enter. Firefox\r
1627 gets started with PID 4001. When it finally finishes loading, it creates an X11\r
1628 window and uses MapWindow to make it visible. This is the first time i3\r
1629 actually gets in touch with Firefox. It decides to map the window, but it has\r
1630 no way of knowing that this window (even though it has the _NET_WM_PID property\r
1631 set to 4001) belongs to the dmenu the user started before.</p></div>\r
1632 <div class="paragraph"><p>How do cgroups help with this? Well, when pressing Mod+d to launch dmenu, i3\r
1633 would create a new cgroup, let’s call it i3-3390-1. It launches dmenu in that\r
1634 cgroup, which gets PID 3390. As before, the user enters firefox and Firefox\r
1635 gets launched with PID 4001. This time, though, the Firefox process with PID\r
1636 4001 is <strong>also</strong> member of the cgroup i3-3390-1 (because fork()ing in a cgroup\r
1637 retains the cgroup property). Therefore, when mapping the window, i3 can look\r
1638 up in which cgroup the process is and can establish a mapping between the\r
1639 workspace and the window.</p></div>\r
1640 <div class="paragraph"><p>There are multiple problems with this approach:</p></div>\r
1641 <div class="olist arabic"><ol class="arabic">\r
1642 <li>\r
1643 <p>\r
1644 Every application has to properly set <tt>_NET_WM_PID</tt>. This is acceptable and\r
1645   patches can be written for the few applications which don’t set the hint yet.\r
1646 </p>\r
1647 </li>\r
1648 <li>\r
1649 <p>\r
1650 It does only work on Linux, since cgroups are a Linux-only feature. Again,\r
1651   this is acceptable.\r
1652 </p>\r
1653 </li>\r
1654 <li>\r
1655 <p>\r
1656 The main problem is that some applications create X11 windows completely\r
1657   independent of UNIX processes. An example for this is Chromium (or\r
1658   gnome-terminal), which, when being started a second time, communicates with\r
1659   the first process and lets the first process open a new window. Therefore, if\r
1660   you have a Chromium window on workspace 2 and you are currently working on\r
1661   workspace 3, starting <tt>chromium</tt> does not lead to the desired result (the\r
1662   window will open on workspace 2).\r
1663 </p>\r
1664 </li>\r
1665 </ol></div>\r
1666 <div class="paragraph"><p>Therefore, my conclusion is that the only proper way of fixing the "window gets\r
1667 opened on the wrong workspace" problem is in the application itself. Most\r
1668 modern applications support freedesktop startup-notifications  which can be\r
1669 used for this.</p></div>\r
1670 </div>\r
1671 </div>\r
1672 </div>\r
1673 </div>\r
1674 <div id="footnotes"><hr /></div>\r
1675 <div id="footer" lang="de">\r
1676 © 2009-2011 Michael Stapelberg, <a href="/impress.html">Impressum</a>\r
1677 </div>\r
1678 </body>\r
1679 </html>\r