69182fd5e986441c8214a877cae51f997a708351
[staging/windowmanager.git] / doc / WindowManagerTMC.txt
1 = WindowManagerTMC
2 :doctype: book
3 :toc:
4 :icons:
5 :data-uri:
6 :lang: en
7 :encoding: utf-8
8
9 == Introduction
10 This WindowManager implements simple layout switching of applications on
11 multiple layers and with different layer layouts.
12
13 === Intended audience
14 This documentation is intended for developers and system integrators
15 that need to know how the window manager works and how it is to be used.
16
17 === Scope of this Document
18 This document covers the window manager that was implemented for TMC and
19 delivered to the Automotive Grade Linux (AGL) project. It includes its
20 implementation details, concepts of operation, configuration and usage.
21
22 === Known Issues
23 Currently there are a couple of known issues:
24
25 * Weston seems not to redraw the screen correctly. When the window
26   manager makes scene changes in quick succession, Weston seems not to
27   redraw the screen correctly and also not send wl_surface::enter
28   events, which in turn leaves applications "dead" - i.e. not rendering
29   or showing up. We developed a simple secondary ivi-controller client
30   application *redraw_fixer* (See <<_redraw_fixer,redraw_fixer>> for more)
31   that listens for specific scene-change events and issues other commands
32   that should prompt a correct redraw - however, this does not work in
33   all instances.
34 * Only single-surface Qt applications are support through the AFBClient
35   library. This is a limitation of how Qt creates surface IDs for the
36   ivi-application interface.
37
38 == Concepts
39 The window manager implements a couple of concepts in order to allow
40 efficient implementation.
41
42 === Layers
43 Layers are entities that are stacked on top of each other. Each layer
44 has an ID which is used for the ivi-controller interface, but this ID
45 also implicitly specifies its stacking order. That is, the screen render
46 order will be set according to the layer stacking which is determined by
47 the layer IDs.
48
49 Layers are always full-screen. We do not use layer dimensions as a way
50 to setup the scene, rather - each layer has a layout attached to it,
51 which specifies an area that is used by surfaces to draw on.
52
53 Additionally, layers will generally leave surfaces on below layers
54 activated, and only disable surfaces on layers the are above the
55 currently used layer.
56
57 In order to deactivate surfaces on lower layer, it is possible to
58 deactivate these surfaces explicitly using the `deactivate_surface` API
59 call.
60
61 === Surfaces
62 Surfaces are _placed_ on layers according to their name. The surface
63 will then be resized to dimensions, according to the layer's layout
64 configuration.
65
66 == Configuration
67 The window manager is configured with the _layers.json_ configuration
68 file, by default it is searched in `/etc/layers.json` but through the
69 use of the environment variable `LAYERS_JSON` the WM can be instructed
70 to use different file. Note, that the WM will not run unless this
71 configuration is found and valid.
72
73 === Configuration Items
74 This section describes configuration items available through
75 `layers.json`. It will do this, by first providing an example, and then
76 going into its components.
77
78 Configuration items can contain `comment` objects, these are unused and
79 only for documentation purposes in the json, they can safely be removed
80 from the JSON file.
81
82 ==== main_surface
83 ------
84 "main_surface": {
85    "surface_role": "HomeScreen",
86 },
87 ------
88
89 The `main_surface` object describes a surface that will internally be
90 treated as the main surface - usually this mean _HomeScreen_. The only
91 special handling this surface receives, is that it is not allowed to
92 deactivate it. Placement of this surface on an layer is done by the
93 other configuration described below.
94
95 * `surface_role` this configuration item specifies the name of the main
96   surface. Set this to e.g. `HomeScreen` if the main surface is called
97   `HomeScreen`.
98
99 ==== mappings
100 This configuration item is a list of surface-name to layer mappings.
101
102 ===== surface to layer mapping
103 ------
104 "mappings": [
105    {
106       "role": "^HomeScreen$",
107       "name": "HomeScreen",
108       "layer_id": 1000,
109       "area": { "type": "full" },
110    },
111    {
112       "role": "^App.*",
113       "name": "apps",
114       "layer_id": 1001,
115       "area": { "type": "rect",
116                 "rect": { "x": 0,
117                           "y": 100,
118                           "width": -1,
119                           "height": -201 } },
120       "split_layouts": []
121    }
122 ]
123 ------
124
125 Each mapping defines the following items to map corresponding surfaces
126 to a layer.
127
128 * `role` defines a regular expression that application drawing names are
129   matched against. If applications match tis regular expression, the
130   surface will be visible on this layer.
131 * `name` is just a name definition for this layer, it has no functional use
132   apart from identifying a layer with a name.
133 * `layer_id` specifies which ID this layer will use.
134 * `area` is an object that defines the area assigned to surfaces.
135 * `split_layouts` is an optional item, that - if present - defines a
136   number of possible split-screen layouts for this layer.
137
138 ===== Area
139 Areas can be either `full` or `rect`, whereas `full` means a fullscreen
140 layer, this is mostly useful for the main_surface or HomeScreen layer.
141 `rect` declares a layer drawing area specified as a rectangle with
142 start coordinates `x` and `y` as well as its dimensions `width` and
143 `height`.
144
145 The dimensions can be specified relative to the screen dimensions. For
146 this negative values for width and height mus be used.
147
148 For example, a fullscreen surface can have the following `rect`
149 definition:
150
151 ------
152 "rect": { "x": 0,
153           "y": 0,
154           "width": -1,
155           "height": -1 }
156 ------
157
158 So the expression for the actual surface dimensions when using
159 screen-size-relative values will be:
160
161 ------
162 actual_width = screen_width + 1 + width
163 actual_height = screen_height + 1 + height
164 ------
165
166 Or in other words, to leave an `N` wide border around a surface, the
167 actual value in the configuration needs to be `-N - 1`.
168
169 ===== split_layouts
170 this configuration item allows the specification of split-screen layouts
171 on layers for certain surfaces. A split screen layout always has a
172 _main_ surface and a _sub_ surface. In order to enter a split screen
173 layout, first the _main_ surface of the layout must be activated, and
174 then the _sub_ surface. In order to disable the split layout, one of the
175 two participating surface must be deactivate (or a surface on a layer
176 below the current one must be activated).
177
178 ------
179 "split_layouts": [
180    {
181       "name": "Media Player",
182       "main_match": "^App MPlayer Main$",
183       "sub_match": "^App MPlayer Sub",
184    }
185 ]
186 ------
187
188 A split layout object has the following attributes:
189
190 * `name` defines its name, it has no actual function other then a way to
191   identify this split layout.
192 * `main_match` is a regular expression that matches for the _main_
193   surface of this split layout.
194 * `sub_match` is a regular expression that matches for the _sub_ surface
195   of this layout.
196
197 In the above example only the surface with drawing name
198 `App MPlayer Main` will be used as the _main_ surface, but all surfaces
199 that begin with `App MPlayer Sub` can be used as a _sub_ surface for
200 this layout.
201
202 .Note
203 ******
204 The names must still match the layer's role match!
205 ******
206
207 == Binding API
208 The binding API consists of a couple of AFB _verbs_ - that is function
209 calls to the Window Manager.
210
211 === Verbs (Functions)
212 * `RequestSurface(drawing_name: string): int`
213   Request a surface ID for the given name. This name and ID association
214   will live until the surface is destroyed (or e.g. the application
215   exits). Each surface that is managed by the window manager needs to
216   call this function first!
217 * `ActivateSurface(drawing_name: string)`
218   This function requests the activation of a surface. It usually is not
219   called by the application, but rather by the application framework or
220   the HomeScreen.
221 * `DeactivateSurface(drawing_name: string)`
222   Request deactivation of a surface. This function is not usually called
223   by applications themselves, but rather by the application framework or
224   the HomeScreen.
225 * `EndDraw(drawing_name: string)`
226   Signals the window manager, that the surface is finished drawing. This
227   is useful for consistent flicker-free layout switches, see the
228   Architecture document for details.
229
230 There are a couple of non-essential (mostly for debugging and
231 development) API calls:
232
233 * `list_drawing_names(): json`
234   List known surface _name_ to _ID_ associations.
235 * `ping()`
236   Ping the window manager. Does also dispatch pending events if any.
237 * `debug_status(): json`
238   Returns a json representation of the current layers and surfaces known
239   to the window manager. This represents the wayland-ivi-extension
240   object's properties.
241 * `debug_surfaces(): json`
242   Returns a json representation of all surfaces known to the window
243   manager. This represents the wayland-ivi-extension properties of the
244   surfaces.
245 * `debug_layers(): json`
246   Returns the current layer configuration, as configured through
247   _layers.json_.
248 * `debug_terminate()`
249   Terminates the afb-daemon running the window manager binding, if the
250   environment variable `WINMAN_DEBUG_TERMINATE` is set.
251
252 === Events
253 The window manager broadcasts certain events (to all applications) that
254 signal information on the state of the surface regarding the current
255 layout.
256
257 * `Active(drawing_name: string)`
258   Signal that the surface with the name `drawing_name` is now active.
259 * `Inactive(drawing_name: string)`
260   Signal that the surface with the name `drawing_name` is now inactive.
261   This usually means, the layout got changed, and the surface is now
262   considered inactive (or sleeping).
263 * `Visible(drawing_name: string)`
264   Signal applications, that the surface with name `drawing_name` is now
265   visible.
266 * `Invisible(drawing_name: string)`
267   Signal applications that the surface with name `drawing_name` is now
268   invisible.
269 * `SyncDraw(drawing_name: string)`
270   Signal applications, that the surface with name `drawing_name` needs
271   to redraw its content - this usually is sent when the surface geometry
272   changed.
273 * `FlushDraw(drawing_name: string)`
274   Signal to applications, that the surface with name `drawing_name` can
275   now be swapped to its newly drawn content as the window manager is
276   ready to activate a new layout (i.e. a new surface geomtry).
277
278 === Binding API Usage
279 For a detailed description on how the binding API is supposed to be
280 used, refer to the Architecture document.
281
282 == Building and Running
283
284 === Dependencies
285 This project is intended to be build with the 4.0 release of AGL.
286
287 Build dependencies are as follows:
288
289 * afb-daemon >= 1.0
290 * libsystemd >= 2222
291 * wayland-client >= 1.11
292
293 === Build Configuration
294 Use cmake to configure a build tree:
295
296 --------
297 mkdir build
298 cd build
299 cmake ..
300 make
301 [sudo] make install
302 --------
303
304 == Utilities
305 With the actual window manager implementation, two general utilities are
306 provided.
307
308 === wm-request
309 A shell script, that wraps `afb-client-demo` and issues commands to the
310 window manager using the AFB exposed API. It will call synchronously to
311 the WM, and output any events that are happening in the meantime.
312 Replies are printed to stdout using an failed/success annotation and a
313 dump of the actual json reply from the AFB.
314
315 ==== Commands
316 * `wm-request requestsurface $NAME`: request the surface with $NAME,
317   this will succeed only if the surface name is not yet (or anymore)
318   known. The actual call return is in the "response" json object of the
319   reply.
320 * `wm-request activatesurface $NAME`: activate the surface with $NAME.
321   This call will only succeed of the surface is available and not
322   currently active. The reply just contains failure/success information,
323   but no actual return value from the API.
324 * `wm-request deactivatesurface $NAME`: deactivate the surface with
325   $NAME. This call will only succeed if the surface is currently active,
326   no return value is provided by the API.
327 * `wm-request ping`: Just send a *ping* to the window manager, can be
328   sued to check whether the WindowManager is alive or not.
329
330 === redraw_fixer
331 This utility is intended to be ran alongside the compositor, it will
332 listen for certain events regarding surfaces, and issue a couple of
333 other commands, to hopefully trigger a redraw of the surface in the
334 compositor.
335
336 It will print messages for each acted-upon event, and exit when the
337 compositor exits.
338
339 == Implementation Notes
340 The window manager is implemented as a app-framework-binder binding. That
341 means, the build produces one shared object that exports a binding
342 interface.
343
344 === Binding code generation
345 The binding API is rather simple; functions receive a json object
346 describing arguments and return a json object describing the result or
347 an error. In order to simplify development, the
348 `generate-binding-glue.py` script was added, that contains a description
349 of the API as a python dictionary. This script generates the header
350 `afb_binding_api.hpp` and the afb binding functions as
351 `afb_binding_glue.inl`. Where the latter is included in `main.cpp`.
352
353 Each function for the AFB binding that is generated does the following:
354
355 * Lock the binding mutex, so that we serialize all access to the
356   binding.
357 * Do some debug logging (if wanted).
358 * Check the binding state, i.e. the compositor might have exited
359   unexpectedly at which point it would not make sense to continue.
360 * Extract the arguments from the json object that is provided (doing
361   some primitive type checking).
362 * Call the afb_binding_api method corresponding to this binding function
363 * Check the afb_binding_api's function return value, log an error state
364   and return the result to the afb request.
365
366 The generated functions do also check for any "loose" exception that
367 comes out of the afb_binding_api call (which in turn might call the
368 actual non-trivial implementation in `App`). However, *IF* an exception
369 is thrown and not handled inside the afb_binding_call, that internal
370 state of the window manager might be broken at this time (hence the
371 talkative error log).
372
373 === Structure
374 The implementation is loosely split across the following source files:
375
376 * `main.cpp`: The program entry point as used by the afb-daemon. This
377   file defines the afbBindingV2 symbol tat is used by the afb-daemon in
378   order to load a binding. It also defines the wayland fd event
379   dispatcher and some globals to be used (as context for the afb calls
380   we receive).
381 * `afb_binding_api.cpp`: The implementation of the afb binding
382   functions. The actual functions are generated by
383   `generate-binding-glue.py` which generates a *.inl* file that is
384   included by `main.cpp`.
385 * `app.cpp` / `app.hpp`: This is the main application logic
386   implementation.
387 * `config.cpp` / `config.hpp`: Very simple configuration item interface.
388 * `controller_hooks.hpp`: hook functions called by the wayland
389   controller to call into the App instance. Only a very limited number
390   of events are passed to the Application, which allowed the usage of
391   such a simple interface.
392 * `json_helper.cpp` / `json_helper.hpp`: Smaller json related helper
393   functions.
394 * `layers.cpp` / `layers.hpp`: Actually hold all the data from
395   layers.json configuration, do some transformations and service the App
396   implementation.
397 * `layout.cpp` / `layout.hpp`: Very simple layout state for the
398   implementation of split layouts and tracking of the surfaces involved.
399 * `policy.hpp`: PolicyManager implementation stub. Gets passed the
400   current and new layout on layout switch and can decide upon it being
401   valid or not.
402 * `result.hpp`: Simple result class around `std::experimental::optional`
403   that additionally can hold a `char const *` to describe the error.
404 * `util.cpp` / `util.hpp`: general utility functions and structs - and
405   preprocessor definitions (e.g. `log*()` to AFB logging functions.
406 * `wayland.cpp` / `wayland.hpp`: A C++ object-oriented libwayland-client
407   wrapper. It is instanced in `main.cpp` and handles all our wayland
408   needs.
409
410 === XXX
411 a
412
413 ---------------------------
414 Some code
415 ---------------------------
416
417 The `token` parameter is a string consisting of only alphanumeric
418 characters, and with a maximum length of 20 characters. If these
419 conditions are not met, the AFBClient instance will not initialize,
420 i.e. this call will return `-EINVAL`.
421
422 .Note
423 ******************
424 The timeout should be small in order to not block too long, but also a
425 0 timeout will not dispatch anything and return immediately (see
426 https://linux.die.net/man/2/epoll_wait[epoll_wait(2)]).
427 ******************
428
429 // vim:set ft=asciidoc tw=72 spell spelllang=en_US: