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