clang-format, mostly indenting.
[staging/windowmanager.git] / src / app.cpp
1 //
2 // Created by mfritzsc on 7/11/17.
3 //
4
5 #include "app.hpp"
6 #include "json_helper.hpp"
7 #include "util.hpp"
8 #include "wayland.hpp"
9
10 #include <cassert>
11
12 #include <json-c/json.h>
13
14 namespace wm {
15
16 namespace {
17 App *g_app;
18 }  // namespace
19
20 App::App(wl::display *d) : api{this}, display{d}, controller{}, outputs() {
21    assert(g_app == nullptr);
22    g_app = this;
23 }
24
25 int App::init() {
26    if (!this->display->ok()) {
27       return -1;
28    }
29
30    this->display->r.add_global_handler(
31       "wl_output", [](wl_registry *r, uint32_t name, uint32_t v) {
32          g_app->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
33       });
34
35    this->display->r.add_global_handler(
36       "ivi_controller", [](wl_registry *r, uint32_t name, uint32_t v) {
37          g_app->controller = std::make_unique<genivi::controller>(r, name, v);
38
39          // XXX: This protocol needs the output, so lets just add our mapping
40          // here...
41          g_app->controller->add_proxy_to_id_mapping(
42             g_app->outputs.back()->proxy.get(),
43             wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
44                g_app->outputs.back()->proxy.get())));
45       });
46
47    // First level objects
48    this->display->roundtrip();
49    // Second level objects
50    this->display->roundtrip();
51    // Third level objects
52    this->display->roundtrip();
53
54    return init_layout();
55 }
56
57 int App::dispatch_events() {
58    int ret = this->display->dispatch();
59    if (ret == -1) {
60       logerror("wl_display_dipatch() returned error %d",
61                this->display->get_error());
62       return -1;
63    }
64    this->display->flush();
65
66    // execute pending tasks, that is layout changes etc.
67    this->controller->execute_pending();
68    this->display->roundtrip();
69
70    return 0;
71 }
72
73 //  _       _ _       _                         _    ____
74 // (_)_ __ (_) |_    | | __ _ _   _  ___  _   _| |_ / /\ \
75 // | | '_ \| | __|   | |/ _` | | | |/ _ \| | | | __| |  | |
76 // | | | | | | |_    | | (_| | |_| | (_) | |_| | |_| |  | |
77 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| |  | |
78 //              |_____|       |___/                 \_\/_/
79 int App::init_layout() {
80    if (!this->controller) {
81       logerror("ivi_controller global not available");
82       return -1;
83    }
84
85    if (this->outputs.empty()) {
86       logerror("no output was set up!");
87       return -1;
88    }
89
90    auto &c = this->controller;
91
92    auto &o = this->outputs.front();
93    auto &s = c->screens.begin()->second;
94    auto &layers = c->layers;
95
96    // XXX: Write output dimensions to ivi controller...
97    c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
98
99    // Clear scene
100    layers.clear();
101
102    // Clear screen
103    s->clear();
104
105    // Setup our dummy scene...
106    c->layer_create(100, 0, 0);   // bottom layer, anything else
107    c->layer_create(1000, 0, 0);  // top layer, mandelbrot
108
109    auto &l100 = c->layers[100];
110    auto &l1k = c->layers[1000];
111
112    // Set layers fullscreen
113    l100->set_destination_rectangle(0, 0, o->width, o->height);
114    l1k->set_destination_rectangle(0, 0, o->width, o->height);
115    l100->set_visibility(1);
116    l1k->set_visibility(1);
117
118    // Add layers to screen
119    s->set_render_order({100, 1000});
120
121    c->commit_changes();
122
123    this->display->flush();
124
125    return 0;
126 }
127
128 binding_api::result_type binding_api::register_surface(uint32_t appid,
129                                                        uint32_t surfid) {
130    logdebug("%s appid %u surfid %u", __func__, appid, surfid);
131    if (appid > 0xff) {
132       return Err<json_object *>("invalid appid");
133    }
134
135    if (surfid > 0xffff) {
136       return Err<json_object *>("invalid surfaceid");
137    }
138
139    return Ok(json_object_new_int((appid << 16) + surfid));
140 }
141
142 binding_api::result_type binding_api::debug_layers() {
143    logdebug("%s", __func__);
144    return Ok(to_json(this->app->controller->lprops));
145 }
146
147 binding_api::result_type binding_api::debug_surfaces() {
148    logdebug("%s", __func__);
149    return Ok(to_json(this->app->controller->sprops));
150 }
151
152 binding_api::result_type binding_api::debug_status() {
153    logdebug("%s", __func__);
154    json_object *jr = json_object_new_object();
155    json_object_object_add(jr, "surfaces",
156                           to_json(this->app->controller->sprops));
157    json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
158    return Ok(jr);
159 }
160
161 }  // namespace wm