app: remove last remnants of obsolete layout config
[staging/windowmanager.git] / src / app.cpp
1 /*
2  * Copyright (C) 2017 Mentor Graphics Development (Deutschland) GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "app.hpp"
18 #include "json_helper.hpp"
19 #include "layers.hpp"
20 #include "layout.hpp"
21 #include "util.hpp"
22 #include "wayland.hpp"
23
24 #include <cstdio>
25 #include <memory>
26
27 #include <cassert>
28
29 #include <json-c/json.h>
30
31 #include <algorithm>
32 #include <bits/signum.h>
33 #include <csignal>
34 #include <fstream>
35 #include <thread>
36 #include <json.hpp>
37
38 namespace wm {
39
40 namespace {
41 App *g_app;
42
43 using nlohmann::json;
44
45 result<json> file_to_json(char const *filename) {
46    std::ifstream i(filename);
47    if (i.fail()) {
48       return Err<json>("Could not open config file");
49    }
50    json j;
51    i >> j;
52    return Ok(j);
53 }
54
55 struct result<layer_map>
56    load_layer_map(char const *filename) {
57    logdebug("loading IDs from %s", filename);
58
59    auto j = file_to_json(filename);
60    if (j.is_err()) {
61       return Err<layer_map>(j.unwrap_err());
62    }
63    json jids = j.unwrap();
64
65    return to_layer_map(jids);
66 }
67
68 }  // namespace
69
70 //       _                    _                  _                 _
71 //   ___| | __ _ ___ ___     / \   _ __  _ __   (_)_ __ ___  _ __ | |
72 //  / __| |/ _` / __/ __|   / _ \ | '_ \| '_ \  | | '_ ` _ \| '_ \| |
73 // | (__| | (_| \__ \__ \  / ___ \| |_) | |_) | | | | | | | | |_) | |
74 //  \___|_|\__,_|___/___/ /_/   \_\ .__/| .__/  |_|_| |_| |_| .__/|_|
75 //                                |_|   |_|                 |_|
76 App::App(wl::display *d)
77    : api{this},
78      chooks{this},
79      display{d},
80      controller{},
81      outputs(),
82      config(),
83      layouts(),
84      layers(),
85      id_alloc{} {
86    assert(g_app == nullptr);
87    g_app = this;
88
89    try {
90       {
91          auto l = load_layer_map(
92             this->config.get_string("layers.json").value().c_str());
93          if (l.is_ok()) {
94             this->layers = l.unwrap();
95          } else {
96             logerror("%s", l.err().value());
97          }
98       }
99    } catch (std::exception &e) {
100       logerror("Loading of configuration failed: %s", e.what());
101    }
102 }
103
104 App::~App() { g_app = nullptr; }
105
106 int App::init() {
107    if (!this->display->ok()) {
108       return -1;
109    }
110
111    if (this->layers.mapping.empty()) {
112       logerror("No surface -> layer mapping loaded");
113       return -1;
114    }
115
116    this->display->add_global_handler(
117       "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
118          this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
119       });
120
121    this->display->add_global_handler(
122       "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
123          this->controller = std::make_unique<struct genivi::controller>(r, name, v);
124
125          // Init controller hooks
126          this->controller->chooks = &this->chooks;
127
128          // XXX: This protocol needs the output, so lets just add our mapping
129          // here...
130          this->controller->add_proxy_to_id_mapping(
131             this->outputs.back()->proxy.get(),
132             wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
133                this->outputs.back()->proxy.get())));
134       });
135
136    // First level objects
137    this->display->roundtrip();
138    // Second level objects
139    this->display->roundtrip();
140    // Third level objects
141    this->display->roundtrip();
142
143    return init_layers();
144 }
145
146 int App::dispatch_events() {
147    int ret = this->display->dispatch();
148    if (ret == -1) {
149       logerror("wl_display_dipatch() returned error %d",
150                this->display->get_error());
151       return -1;
152    }
153    this->display->flush();
154
155    // execute pending tasks, that is layout changes etc.
156    //this->execute_pending();
157
158    return 0;
159 }
160
161 //  _       _ _       _                         _    ____
162 // (_)_ __ (_) |_    | | __ _ _   _  ___  _   _| |_ / /\ \
163 // | | '_ \| | __|   | |/ _` | | | |/ _ \| | | | __| |  | |
164 // | | | | | | |_    | | (_| | |_| | (_) | |_| | |_| |  | |
165 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| |  | |
166 //              |_____|       |___/                 \_\/_/
167 int App::init_layers() {
168    if (!this->controller) {
169       logerror("ivi_controller global not available");
170       return -1;
171    }
172
173    if (this->outputs.empty()) {
174       logerror("no output was set up!");
175       return -1;
176    }
177
178    auto &c = this->controller;
179
180    auto &o = this->outputs.front();
181    auto &s = c->screens.begin()->second;
182    auto &layers = c->layers;
183
184    // XXX: Write output dimensions to ivi controller...
185    c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
186
187    // Clear scene
188    layers.clear();
189
190    // Clear screen
191    s->clear();
192
193    // Quick and dirty setup of layers
194    // XXX: This likely needs to be sorted by order (note, we don't (yet?)
195    // do any zorder arrangement).
196    for (auto const &i : this->layers.mapping) {
197       c->layer_create(i.layer_id, o->width, o->height);
198       auto &l = layers[i.layer_id];
199       l->set_destination_rectangle(0, 0, o->width, o->height);
200       l->set_visibility(1);
201       logdebug("Setting up layer %s (%d) for surfaces %d-%d", i.name.c_str(),
202                i.layer_id, i.id_min, i.id_max);
203    }
204
205    // Add layers to screen (XXX: are they sorted correctly?)
206    s->set_render_order(this->layers.layers);
207
208    c->commit_changes();
209
210    this->display->flush();
211
212    return 0;
213 }
214
215 namespace {
216
217 // This can fix the HomeScreen...
218 void redraw_fix(App *app, std::unique_ptr<genivi::surface> &s, int x, int y, int w, int h) {
219    { // XXX: Work around weston redraw issues
220       // trigger an update by changing the source dimensions!
221       s->set_configuration(w + 1, h);
222       s->set_source_rectangle(0, 0, w + 1, h);
223       s->set_destination_rectangle(x, y, w + 1, h);
224       app->controller->commit_changes();
225       app->display->roundtrip();
226
227       // wait some time, for the process to do its thing...
228       using namespace std::chrono_literals;
229       std::this_thread::sleep_for(100ms);
230
231       // Set a different size then what we actually want.
232       s->set_configuration(w, h);
233       s->set_source_rectangle(0, 0, w, h);
234       s->set_destination_rectangle(x, y, w, h);
235       app->controller->commit_changes();
236       app->display->roundtrip();
237    }
238 }
239
240 }  // namespace
241
242 void App::surface_init_layout(uint32_t surface_id) {
243    if (!this->controller->surface_exists(surface_id)) {
244       logerror("Surface %d does not exist", int(surface_id));
245       return;
246    }
247
248    auto o_layer_id = this->layers.get_layer_id(surface_id);
249
250    if (!o_layer_id) {
251       logerror("Surface %d is not associated with any layer!", int(surface_id));
252       return;
253    }
254
255    uint32_t layer_id = o_layer_id.value();
256    logdebug("surface_set_layout for surface %u on layer %u", surface_id,
257             layer_id);
258
259    auto const &layer = this->layers.get_layer(layer_id);
260    auto rect = layer.value().rect;
261    auto &s = this->controller->surfaces[surface_id];
262
263    int x = rect.x;
264    int y = rect.y;
265    int w = rect.w;
266    int h = rect.h;
267
268    // less-than-0 values refer to MAX + 1 - $VALUE
269    // e.g. MAX is either screen width or height
270    if (w < 0) {
271       w = this->controller->output_size.w + 1 + w;
272    }
273    if (h < 0) {
274       h = this->controller->output_size.h + 1 + h;
275    }
276
277    // configure surface to wxh dimensions
278    s->set_configuration(w, h);
279
280    // set source reactangle, even if we should not need to set it.
281    s->set_source_rectangle(0, 0, w, h);
282
283    // set destination to the display rectangle
284    s->set_destination_rectangle(x, y, w, h);
285
286    s->set_visibility(0);
287    s->set_opacity(256);
288
289    this->controller->commit_changes();
290    this->display->roundtrip();
291
292    redraw_fix(this, s, x, y, w, h);
293
294    this->controller->layers[layer_id]->add_surface(s.get());
295
296    // activate the main_surface right away
297    if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
298       logdebug("Activating main_surface (%d)", surface_id);
299
300       this->activate_surface(this->lookup_name(surface_id).value_or("unknown-name").c_str());
301    }
302
303    logdebug("Surface %u now on layer %u with rect { %d, %d, %d, %d }",
304             surface_id, layer_id, x, y, w, h);
305 }
306
307 char const *App::activate_surface(char const *drawing_name) {
308    int surface_id = -1;
309
310    {
311       auto oid = this->lookup_id(drawing_name);
312       if (oid) {
313          surface_id = oid.value();
314       } else {
315          return "Surface does not exist";
316       }
317    }
318
319    if (!this->controller->surface_exists(surface_id)) {
320       return "Surface does not exist";
321    }
322
323    // This should involve a policy check, but as we do not (yet) have
324    // such a thing, we will just switch to this surface.
325    // XXX: input focus missing!!1
326
327    // Make it visible, no (or little effect) if already visible
328    auto &s = this->controller->surfaces[surface_id];
329
330    // Set all others invisible
331    for (auto &i : this->controller->surfaces) {
332       auto &si = this->controller->sprops[i.second->id];
333       if (si.id != s->id && si.visibility != 0 &&
334           int(si.id) != this->layers.main_surface) {
335          this->deactivate(si.id);
336       }
337    }
338    this->activate(s->id);
339
340    // commit changes
341    this->controller->commit_changes();
342    this->display->flush();
343
344    // no error
345    return nullptr;
346 }
347
348 char const *App::deactivate_surface(char const *drawing_name) {
349    int surface_id = -1;
350
351    {
352       auto oid = this->lookup_id(drawing_name);
353       if (oid) {
354          surface_id = oid.value();
355       } else {
356          return "Surface does not exist";
357       }
358    }
359
360    if (surface_id == this->layers.main_surface) {
361       return "Cannot deactivate main_surface";
362    }
363
364    this->deactivate(surface_id);
365
366    this->controller->commit_changes();
367    this->display->flush();
368
369    return nullptr;
370 }
371
372 //                      _          _   _____                 _
373 //  _ __  _ __ _____  _(_) ___  __| | | ____|_   _____ _ __ | |_ ___
374 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | |  _| \ \ / / _ \ '_ \| __/ __|
375 // | |_) | | | (_) >  <| |  __/ (_| | | |___ \ V /  __/ | | | |_\__ \
376 // | .__/|_|  \___/_/\_\_|\___|\__,_| |_____| \_/ \___|_| |_|\__|___/
377 // |_|
378 void App::surface_created(uint32_t surface_id) {
379    logdebug("surface_id is %u", surface_id);
380
381    this->surface_init_layout(surface_id);
382 }
383
384 void App::surface_removed(uint32_t surface_id) {
385    logdebug("surface_id is %u", surface_id);
386
387    this->id_alloc.remove_id(surface_id);
388 }
389
390 void App::emit_activated(char const *label) {
391    this->api.send_event("activated", label);
392 }
393
394 void App::emit_deactivated(char const *label) {
395    this->api.send_event("deactivated", label);
396 }
397
398 void App::emit_syncdraw(char const *label) {
399    this->api.send_event("syncdraw", label);
400 }
401
402 void App::emit_flushdraw(char const *label) {
403    this->api.send_event("syncdraw", label);
404 }
405
406 void App::emit_visible(char const *label, bool is_visible) {
407    this->api.send_event(is_visible ? "visible" : "invisible", label);
408 }
409
410 void App::emit_invisible(char const *label) {
411    return emit_visible(label, 0);
412 }
413
414 void App::emit_visible(char const *label) {
415    return emit_visible(label, 1);
416 }
417
418 result<int> App::request_surface(char const *drawing_name) {
419    auto lid = this->layers.get_layer_id(std::string(drawing_name));
420    if (!lid) {
421       // XXX: to we need to put these applications on the App layer?
422       return Err<int>("Drawing name does not match any role");
423    }
424
425    auto rname = this->lookup_id(drawing_name);
426    if (!rname) {
427       // name does not exist yet, allocate surface id...
428       auto id = int(this->id_alloc.generate_id(drawing_name));
429       this->layers.add_surface(id, lid.value());
430
431       // XXX: we set the main_surface[_name] here and now,
432       // not sure if we want this, but it worked so far.
433       if (!this->layers.main_surface_name.empty() &&
434           this->layers.main_surface_name == drawing_name) {
435          this->layers.main_surface = id;
436          logdebug("Set main_surface id to %u", id);
437       }
438
439       return Ok<int>(id);
440    }
441
442    // Check currently registered drawing names if it is already there.
443    return Err<int>("Surface already present");
444 }
445
446 void App::activate(unsigned id) {
447    if (this->controller->sprops[id].visibility == 0) {
448       this->controller->surfaces[id]->set_visibility(1);
449       char const *label = this->lookup_name(id).value_or("unknown-name").c_str();
450       this->emit_activated(label);
451       this->emit_visible(label);
452    }
453 }
454
455 void App::deactivate(unsigned id) {
456    if (this->controller->sprops[id].visibility != 0) {
457       this->controller->surfaces[id]->set_visibility(0);
458       char const *label = this->lookup_name(id).value_or("unknown-name").c_str();
459       this->emit_deactivated(label);
460       this->emit_invisible(label);
461    }
462 }
463
464 bool App::can_split(unsigned new_id) {
465    if (this->state.state == LayoutState::LayoutSingle) {
466       auto new_id_layer = this->layers.get_layer_id(new_id).value();
467       auto current_id_layer = this->layers.get_layer_id(this->state.main).value();
468
469       if (new_id_layer != current_id_layer) {
470          return false;
471       }
472
473       std::string const &new_id_str = this->lookup_name(new_id).value();
474       std::string const &cur_id_str = this->lookup_name(this->state.main).value();
475
476
477    }
478 }
479
480 //                  _             _ _            _                 _
481 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __ | |__   ___   ___ | | _____
482 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
483 // | (_| (_) | | | | |_| | | (_) | | |  __/ |   | | | | (_) | (_) |   <\__ \
484 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
485 //                                         |_____|
486 void controller_hooks::surface_created(uint32_t surface_id) {
487    this->app->surface_created(surface_id);
488 }
489
490 void controller_hooks::surface_removed(uint32_t surface_id) {
491    this->app->surface_removed(surface_id);
492 }
493
494 }  // namespace wm