2 * Copyright (C) 2017 Mentor Graphics Development (Deutschland) GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 // Created by mfritzsc on 7/11/17.
22 #include "json_helper.hpp"
26 #include "wayland.hpp"
33 #include <json-c/json.h>
35 #include <bits/signum.h>
45 using json = nlohmann::json;
47 struct wm::area area_from_json(json const &j) {
50 j["name"].get<std::string>(),
52 get<int32_t>(j["width"]), get<int32_t>(j["height"]),
53 get<int32_t>(j["x"]), get<int32_t>(j["y"]),
55 get<uint32_t>(j["zorder"]),
59 result<struct layout> layout_from_json(json const &j) {
61 auto &ja = j["areas"];
63 auto l = layout{j["name"].get<std::string>(), uint32_t(ja.size()), {}};
65 if (ja.size() > layout::MAX_N_AREAS) {
66 return Err<struct layout>("Invalid number of areas in layout");
69 logdebug("Loading layout '%s' with %u areas", l.name.c_str(),
72 std::transform(std::cbegin(ja), std::cend(ja), std::begin(l.areas),
78 result<json> file_to_json(char const *filename) {
79 std::ifstream i(filename);
81 return Err<json>("Could not open config file");
88 // Will throw if parsing fails
89 struct result<layouts_type> load_layout(char const *filename) {
90 DB("loading layout from " << filename);
92 auto j = file_to_json(filename);
94 return Err<layouts_type>(j.unwrap_err());
96 json jlayouts = j.unwrap();
98 auto layouts = layouts_type();
99 layouts.reserve(jlayouts.size());
100 std::transform(std::cbegin(jlayouts), std::cend(jlayouts),
101 std::back_inserter(layouts), layout_from_json);
106 struct result<layer_map>
107 load_layer_map(char const *filename) {
108 DB("loading IDs from " << filename);
110 auto j = file_to_json(filename);
112 return Err<layer_map>(j.unwrap_err());
114 json jids = j.unwrap();
116 return to_layer_map(jids);
122 // ___| | __ _ ___ ___ / \ _ __ _ __ (_)_ __ ___ _ __ | |
123 // / __| |/ _` / __/ __| / _ \ | '_ \| '_ \ | | '_ ` _ \| '_ \| |
124 // | (__| | (_| \__ \__ \ / ___ \| |_) | |_) | | | | | | | | |_) | |
125 // \___|_|\__,_|___/___/ /_/ \_\ .__/| .__/ |_|_| |_| |_| .__/|_|
127 App::App(wl::display *d)
136 assert(g_app == nullptr);
141 auto l = load_layer_map(
142 this->config.get_string("layers.json").value().c_str());
144 this->layers = l.unwrap();
146 logerror("%s", l.err().value());
151 auto l = load_layout(
152 this->config.get_string("layout.json").value().c_str());
154 this->layouts = l.unwrap();
156 logerror("%s", l.err().value());
159 } catch (std::exception &e) {
160 logerror("Loading of configuration failed: %s", e.what());
164 App::~App() { g_app = nullptr; }
167 if (!this->display->ok()) {
171 if (this->layers.mapping.empty()) {
172 logerror("No surface -> layer mapping loaded");
176 this->display->add_global_handler(
177 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
178 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
181 this->display->add_global_handler(
182 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
183 this->controller = std::make_unique<genivi::controller>(r, name, v);
185 // Init controller hooks
186 this->controller->chooks = &this->chooks;
188 // XXX: This protocol needs the output, so lets just add our mapping
190 this->controller->add_proxy_to_id_mapping(
191 this->outputs.back()->proxy.get(),
192 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
193 this->outputs.back()->proxy.get())));
196 // First level objects
197 this->display->roundtrip();
198 // Second level objects
199 this->display->roundtrip();
200 // Third level objects
201 this->display->roundtrip();
203 return init_layout();
206 int App::dispatch_events() {
207 int ret = this->display->dispatch();
209 logerror("wl_display_dipatch() returned error %d",
210 this->display->get_error());
213 this->display->flush();
215 // execute pending tasks, that is layout changes etc.
216 this->controller->execute_pending();
217 this->display->roundtrip();
223 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
224 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
225 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
226 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
227 // |_____| |___/ \_\/_/
228 int App::init_layout() {
229 if (!this->controller) {
230 logerror("ivi_controller global not available");
234 if (this->outputs.empty()) {
235 logerror("no output was set up!");
239 auto &c = this->controller;
241 auto &o = this->outputs.front();
242 auto &s = c->screens.begin()->second;
243 auto &layers = c->layers;
245 // XXX: Write output dimensions to ivi controller...
246 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
254 // Quick and dirty setup of layers
255 // XXX: This likely needs to be sorted by order (note, we don't (yet?)
256 // do any zorder arrangement).
257 for (auto const &i : this->layers.mapping) {
258 c->layer_create(i.layer_id, o->width, o->height);
259 auto &l = layers[i.layer_id];
260 l->set_destination_rectangle(0, 0, o->width, o->height);
261 l->set_visibility(1);
262 logdebug("Setting up layer %s (%d) for surfaces %d-%d", i.name.c_str(),
263 i.layer_id, i.id_min, i.id_max);
266 // Add layers to screen (XXX: are they sorted correctly?)
267 s->set_render_order(this->layers.layers);
271 this->display->flush();
276 void App::surface_set_layout(uint32_t surface_id) {
277 auto o_layer_id = this->layers.get_layer_id(surface_id);
280 logerror("Surface %d is not associated with any layer!", int(surface_id));
284 if (!this->controller->surface_exists(surface_id)) {
285 logerror("Surface %d does not exist", int(surface_id));
289 uint32_t layer_id = o_layer_id.value();
291 auto rect = this->layers.get_layer_rect(surface_id).value();
292 auto &s = this->controller->surfaces[surface_id];
299 // less-than-0 values refer to MAX + 1 - $VALUE
300 // e.g. MAX is either screen width or height
302 w = this->controller->output_size.w + 1 + w;
305 h = this->controller->output_size.h + 1 + h;
307 logdebug("Computed rect={ %d, %d, %d, %d }", x, y, w, h);
309 // configure surface to wxh dimensions
310 s->set_configuration(w, h);
311 // set source rect to "entire surface"
312 s->set_source_rectangle(0, 0, w, h);
313 // set destination to the display rectangle
314 s->set_destination_rectangle(x, y, w, h);
316 s->set_visibility(1);
317 this->controller->layers[layer_id]->add_surface(s.get());
319 logdebug("Surface %u now on layer %u with rect { %d, %d, %d, %d }",
320 surface_id, layer_id, x, y, w, h);
324 // _ __ _ __ _____ _(_) ___ __| | | ____|_ _____ _ __ | |_ ___
325 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | | _| \ \ / / _ \ '_ \| __/ __|
326 // | |_) | | | (_) > <| | __/ (_| | | |___ \ V / __/ | | | |_\__ \
327 // | .__/|_| \___/_/\_\_|\___|\__,_| |_____| \_/ \___|_| |_|\__|___/
329 void App::surface_created(uint32_t surface_id) {
330 DB("surface_id is " << surface_id);
332 // We need to execute the surface setup after its creation.
333 // XXX: perhaps move the late-tasks functionality to App?
334 this->controller->add_task("surface_set_layout",
335 [surface_id, this](struct genivi::controller *) {
336 this->surface_set_layout(surface_id);
340 void App::surface_removed(uint32_t surface_id) {
341 DB("surface_id is " << surface_id);
345 // | |__ (_)_ __ __| (_)_ __ __ _ __ _ _ __ (_) (_)_ __ ___ _ __ | |
346 // | '_ \| | '_ \ / _` | | '_ \ / _` | / _` | '_ \| | | | '_ ` _ \| '_ \| |
347 // | |_) | | | | | (_| | | | | | (_| | | (_| | |_) | | | | | | | | | |_) | |
348 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___\__,_| .__/|_| |_|_| |_| |_| .__/|_|
349 // |___/_____| |_| |_|
350 binding_api::result_type binding_api::register_surface(uint32_t appid,
352 logdebug("%s appid %u surfid %u", __func__, appid, surfid);
354 return Err<json_object *>("invalid appid");
357 if (surfid > 0xffff) {
358 return Err<json_object *>("invalid surfaceid");
361 return Ok(json_object_new_int((appid << 16) + surfid));
364 binding_api::result_type binding_api::debug_layers() {
365 logdebug("%s", __func__);
366 return Ok(json_tokener_parse(this->app->layers.to_json().dump().c_str()));
369 binding_api::result_type binding_api::debug_surfaces() {
370 logdebug("%s", __func__);
371 return Ok(to_json(this->app->controller->sprops));
374 binding_api::result_type binding_api::debug_status() {
375 logdebug("%s", __func__);
376 json_object *jr = json_object_new_object();
377 json_object_object_add(jr, "surfaces",
378 to_json(this->app->controller->sprops));
379 json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
383 binding_api::result_type binding_api::debug_terminate() {
384 logdebug("%s", __func__);
385 raise(SIGKILL); // XXX afb-daemon kills it's pgroup using TERM, which
386 // doesn't play well with perf
387 return Ok(json_object_new_object());
391 // ___ ___ _ __ | |_ _ __ ___ | | | ___ _ __ | |__ ___ ___ | | _____
392 // / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
393 // | (_| (_) | | | | |_| | | (_) | | | __/ | | | | | (_) | (_) | <\__ \
394 // \___\___/|_| |_|\__|_| \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
396 void controller_hooks::surface_created(uint32_t surface_id) {
397 this->app->surface_created(surface_id);
400 void controller_hooks::surface_removed(uint32_t surface_id) {
401 this->app->surface_removed(surface_id);