app/wayland: move surface setup as is to app controller hook
[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 "layers.hpp"
8 #include "layout.hpp"
9 #include "util.hpp"
10 #include "wayland.hpp"
11
12 #include <cstdio>
13 #include <memory>
14
15 #include <cassert>
16
17 #include <json-c/json.h>
18
19 #include <fstream>
20 #include <json.hpp>
21
22 namespace wm {
23
24 namespace {
25 App *g_app;
26
27 using json = nlohmann::json;
28
29 struct wm::area area_from_json(json const &j) {
30    DB(j);
31    return wm::area{
32       j["name"].get<std::string>(),
33       {
34          get<uint32_t>(j["width"]), get<uint32_t>(j["height"]),
35          get<int32_t>(j["x"]), get<int32_t>(j["y"]),
36       },
37       get<uint32_t>(j["zorder"]),
38    };
39 }
40
41 result<struct layout> layout_from_json(json const &j) {
42    DB(j);
43    auto &ja = j["areas"];
44
45    auto l = wm::layout{j["name"].get<std::string>(), uint32_t(ja.size()), {}};
46
47    if (ja.size() > layout::MAX_N_AREAS) {
48       return Err<struct layout>("Invalid number of areas in layout");
49    }
50
51    logdebug("Loading layout '%s' with %u areas", l.name.c_str(),
52             unsigned(ja.size()));
53
54    std::transform(std::cbegin(ja), std::cend(ja), std::begin(l.areas),
55                   area_from_json);
56
57    return Ok(l);
58 }
59
60 struct result<layouts_type> load_layout(char const *filename) {
61    DB("loading layout from " << filename);
62
63    json jlayouts;
64    std::ifstream i(filename);
65    i >> jlayouts;
66
67    auto layouts = layouts_type();
68    std::transform(std::cbegin(jlayouts), std::cend(jlayouts),
69                   std::back_inserter(layouts), layout_from_json);
70
71    return Ok(layouts);
72 }
73
74 struct result<surface_id_to_layer_map>
75    load_layer_ids(char const *filename) {
76    DB("loading IDs from " << filename);
77
78    json jids;
79    std::ifstream i(filename);
80    i >> jids;
81
82    return to_surface_id_to_layer_map(jids);
83 }
84
85 }  // namespace
86
87 //       _                    _                  _                 _
88 //   ___| | __ _ ___ ___     / \   _ __  _ __   (_)_ __ ___  _ __ | |
89 //  / __| |/ _` / __/ __|   / _ \ | '_ \| '_ \  | | '_ ` _ \| '_ \| |
90 // | (__| | (_| \__ \__ \  / ___ \| |_) | |_) | | | | | | | | |_) | |
91 //  \___|_|\__,_|___/___/ /_/   \_\ .__/| .__/  |_|_| |_| |_| .__/|_|
92 //                                |_|   |_|                 |_|
93 App::App(wl::display *d)
94    : api{this}, chooks{this}, display{d}, controller{} {
95    // layouts(load_layout("../layout.json").unwrap()) {
96    assert(g_app == nullptr);
97    g_app = this;
98
99    auto a = load_layout("../layout.json");
100    auto b = a.map_err([](char const *e) -> char const * {
101       DB("Could not load layout: " << e);
102       return e;
103    });
104
105    auto c = load_layer_ids("../ids.json");
106    auto e = c.map_err([](char const *e) -> char const * {
107       DB("Could not load ids: " << e);
108       return e;
109    });
110 }
111
112 App::~App() { g_app = nullptr; }
113
114 int App::init() {
115    if (!this->display->ok()) {
116       return -1;
117    }
118
119    this->display->add_global_handler(
120       "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
121          this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
122       });
123
124    this->display->add_global_handler(
125       "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
126          this->controller = std::make_unique<genivi::controller>(r, name, v);
127
128          // Init controller hooks
129          this->controller->chooks = &this->chooks;
130
131          // XXX: This protocol needs the output, so lets just add our mapping
132          // here...
133          this->controller->add_proxy_to_id_mapping(
134             this->outputs.back()->proxy.get(),
135             wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
136                this->outputs.back()->proxy.get())));
137       });
138
139    // First level objects
140    this->display->roundtrip();
141    // Second level objects
142    this->display->roundtrip();
143    // Third level objects
144    this->display->roundtrip();
145
146    return init_layout();
147 }
148
149 int App::dispatch_events() {
150    int ret = this->display->dispatch();
151    if (ret == -1) {
152       logerror("wl_display_dipatch() returned error %d",
153                this->display->get_error());
154       return -1;
155    }
156    this->display->flush();
157
158    // execute pending tasks, that is layout changes etc.
159    this->controller->execute_pending();
160    this->display->roundtrip();
161
162    return 0;
163 }
164
165 //  _       _ _       _                         _    ____
166 // (_)_ __ (_) |_    | | __ _ _   _  ___  _   _| |_ / /\ \
167 // | | '_ \| | __|   | |/ _` | | | |/ _ \| | | | __| |  | |
168 // | | | | | | |_    | | (_| | |_| | (_) | |_| | |_| |  | |
169 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| |  | |
170 //              |_____|       |___/                 \_\/_/
171 int App::init_layout() {
172    if (!this->controller) {
173       logerror("ivi_controller global not available");
174       return -1;
175    }
176
177    if (this->outputs.empty()) {
178       logerror("no output was set up!");
179       return -1;
180    }
181
182    auto &c = this->controller;
183
184    auto &o = this->outputs.front();
185    auto &s = c->screens.begin()->second;
186    auto &layers = c->layers;
187
188    // XXX: Write output dimensions to ivi controller...
189    c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
190
191    // Clear scene
192    layers.clear();
193
194    // Clear screen
195    s->clear();
196
197    // Setup our dummy scene...
198    c->layer_create(100, 0, 0);   // bottom layer, anything else
199    c->layer_create(1000, 0, 0);  // top layer, mandelbrot
200
201    auto &l100 = c->layers[100];
202    auto &l1k = c->layers[1000];
203
204    // Set layers fullscreen
205    l100->set_destination_rectangle(0, 0, o->width, o->height);
206    l1k->set_destination_rectangle(0, 0, o->width, o->height);
207    l100->set_visibility(1);
208    l1k->set_visibility(1);
209
210    // Add layers to screen
211    s->set_render_order({100, 1000});
212
213    c->commit_changes();
214
215    this->display->flush();
216
217    return 0;
218 }
219
220 void App::surface_created(uint32_t surface_id) {
221    DB("surface_id is " << surface_id);
222    this->controller->add_task("fullscreen surface", [surface_id](struct genivi::controller *c) {
223       auto &s = c->surfaces[surface_id];
224       s->set_destination_rectangle(0, 0, c->output_size.w, c->output_size.h);
225       s->set_visibility(1);
226       uint32_t lid = surface_id == 0x16180 ? 1000 : 100;
227       c->layers[lid]->add_surface(s.get());
228       logdebug("Surface %u now fullscreen on layer %u", surface_id, lid);
229    });
230 }
231
232 void App::surface_removed(uint32_t surface_id) {
233    DB("surface_id is " << surface_id);
234 }
235
236 //  _     _           _ _                            _   _                 _
237 // | |__ (_)_ __   __| (_)_ __   __ _     __ _ _ __ (_) (_)_ __ ___  _ __ | |
238 // | '_ \| | '_ \ / _` | | '_ \ / _` |   / _` | '_ \| | | | '_ ` _ \| '_ \| |
239 // | |_) | | | | | (_| | | | | | (_| |  | (_| | |_) | | | | | | | | | |_) | |
240 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___\__,_| .__/|_| |_|_| |_| |_| .__/|_|
241 //                              |___/_____|   |_|                   |_|
242 binding_api::result_type binding_api::register_surface(uint32_t appid,
243                                                        uint32_t surfid) {
244    logdebug("%s appid %u surfid %u", __func__, appid, surfid);
245    if (appid > 0xff) {
246       return Err<json_object *>("invalid appid");
247    }
248
249    if (surfid > 0xffff) {
250       return Err<json_object *>("invalid surfaceid");
251    }
252
253    return Ok(json_object_new_int((appid << 16) + surfid));
254 }
255
256 binding_api::result_type binding_api::debug_layers() {
257    logdebug("%s", __func__);
258    return Ok(to_json(this->app->controller->lprops));
259 }
260
261 binding_api::result_type binding_api::debug_surfaces() {
262    logdebug("%s", __func__);
263    return Ok(to_json(this->app->controller->sprops));
264 }
265
266 binding_api::result_type binding_api::debug_status() {
267    logdebug("%s", __func__);
268    json_object *jr = json_object_new_object();
269    json_object_object_add(jr, "surfaces",
270                           to_json(this->app->controller->sprops));
271    json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
272    return Ok(jr);
273 }
274
275 //                  _             _ _            _                 _
276 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __ | |__   ___   ___ | | _____
277 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
278 // | (_| (_) | | | | |_| | | (_) | | |  __/ |   | | | | (_) | (_) |   <\__ \
279 //  \___\___/|_| |_|\__|_|  \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
280 //                                         |_____|
281 void controller_hooks::surface_created(uint32_t surface_id) {
282    this->app->surface_created(surface_id);
283 }
284
285 void controller_hooks::surface_removed(uint32_t surface_id) {
286    this->app->surface_removed(surface_id);
287 }
288
289 }  // namespace wm