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 #include "json_helper.hpp"
22 #include "wayland.hpp"
29 #include <json-c/json.h>
31 #include <bits/signum.h>
41 using json = nlohmann::json;
43 struct wm::area area_from_json(json const &j) {
47 j["width"], j["height"],
54 result<struct layout> layout_from_json(json const &j) {
55 auto &ja = j["areas"];
57 auto l = layout{j["name"], uint32_t(ja.size()), {}};
59 if (ja.size() > layout::MAX_N_AREAS) {
60 return Err<struct layout>("Invalid number of areas in layout");
63 logdebug("Loading layout '%s' with %u areas", l.name.c_str(),
66 std::transform(std::cbegin(ja), std::cend(ja), std::begin(l.areas),
72 result<json> file_to_json(char const *filename) {
73 std::ifstream i(filename);
75 return Err<json>("Could not open config file");
82 // Will throw if parsing fails
83 struct result<layouts_type> load_layout(char const *filename) {
84 logdebug("loading layout from %s", filename);
86 auto j = file_to_json(filename);
88 return Err<layouts_type>(j.unwrap_err());
90 json jlayouts = j.unwrap();
92 auto layouts = layouts_type();
93 layouts.reserve(jlayouts.size());
94 std::transform(std::cbegin(jlayouts), std::cend(jlayouts),
95 std::back_inserter(layouts), layout_from_json);
100 struct result<layer_map>
101 load_layer_map(char const *filename) {
102 logdebug("loading IDs from %s", filename);
104 auto j = file_to_json(filename);
106 return Err<layer_map>(j.unwrap_err());
108 json jids = j.unwrap();
110 return to_layer_map(jids);
116 // ___| | __ _ ___ ___ / \ _ __ _ __ (_)_ __ ___ _ __ | |
117 // / __| |/ _` / __/ __| / _ \ | '_ \| '_ \ | | '_ ` _ \| '_ \| |
118 // | (__| | (_| \__ \__ \ / ___ \| |_) | |_) | | | | | | | | |_) | |
119 // \___|_|\__,_|___/___/ /_/ \_\ .__/| .__/ |_|_| |_| |_| .__/|_|
121 App::App(wl::display *d)
130 assert(g_app == nullptr);
135 auto l = load_layer_map(
136 this->config.get_string("layers.json").value().c_str());
138 this->layers = l.unwrap();
140 logerror("%s", l.err().value());
146 load_layout(this->config.get_string("layout.json").value().c_str());
148 this->layouts = l.unwrap();
150 logerror("%s", l.err().value());
153 } catch (std::exception &e) {
154 logerror("Loading of configuration failed: %s", e.what());
158 App::~App() { g_app = nullptr; }
161 if (!this->display->ok()) {
165 if (this->layers.mapping.empty()) {
166 logerror("No surface -> layer mapping loaded");
170 this->display->add_global_handler(
171 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
172 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
175 this->display->add_global_handler(
176 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
177 this->controller = std::make_unique<genivi::controller>(r, name, v);
179 // Init controller hooks
180 this->controller->chooks = &this->chooks;
182 // XXX: This protocol needs the output, so lets just add our mapping
184 this->controller->add_proxy_to_id_mapping(
185 this->outputs.back()->proxy.get(),
186 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
187 this->outputs.back()->proxy.get())));
190 // First level objects
191 this->display->roundtrip();
192 // Second level objects
193 this->display->roundtrip();
194 // Third level objects
195 this->display->roundtrip();
197 return init_layout();
200 int App::dispatch_events() {
201 int ret = this->display->dispatch();
203 logerror("wl_display_dipatch() returned error %d",
204 this->display->get_error());
207 this->display->flush();
209 // execute pending tasks, that is layout changes etc.
210 this->execute_pending();
216 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
217 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
218 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
219 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
220 // |_____| |___/ \_\/_/
221 int App::init_layout() {
222 if (!this->controller) {
223 logerror("ivi_controller global not available");
227 if (this->outputs.empty()) {
228 logerror("no output was set up!");
232 auto &c = this->controller;
234 auto &o = this->outputs.front();
235 auto &s = c->screens.begin()->second;
236 auto &layers = c->layers;
238 // XXX: Write output dimensions to ivi controller...
239 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
247 // Quick and dirty setup of layers
248 // XXX: This likely needs to be sorted by order (note, we don't (yet?)
249 // do any zorder arrangement).
250 for (auto const &i : this->layers.mapping) {
251 c->layer_create(i.layer_id, o->width, o->height);
252 auto &l = layers[i.layer_id];
253 l->set_destination_rectangle(0, 0, o->width, o->height);
254 l->set_visibility(1);
255 logdebug("Setting up layer %s (%d) for surfaces %d-%d", i.name.c_str(),
256 i.layer_id, i.id_min, i.id_max);
259 // Add layers to screen (XXX: are they sorted correctly?)
260 s->set_render_order(this->layers.layers);
264 this->display->flush();
269 void App::surface_set_layout(uint32_t surface_id) {
270 auto o_layer_id = this->layers.get_layer_id(surface_id);
273 logerror("Surface %d is not associated with any layer!", int(surface_id));
277 if (!this->controller->surface_exists(surface_id)) {
278 logerror("Surface %d does not exist", int(surface_id));
282 uint32_t layer_id = o_layer_id.value();
284 auto rect = this->layers.get_layer_rect(surface_id).value();
285 auto &s = this->controller->surfaces[surface_id];
292 // less-than-0 values refer to MAX + 1 - $VALUE
293 // e.g. MAX is either screen width or height
295 w = this->controller->output_size.w + 1 + w;
298 h = this->controller->output_size.h + 1 + h;
301 // configure surface to wxh dimensions
302 s->set_configuration(w, h);
303 // set destination to the display rectangle
304 s->set_destination_rectangle(x, y, w, h);
306 // XXX: visibility should be determined independently of our
307 // layer + geometry setup.
308 s->set_visibility(1);
309 this->controller->layers[layer_id]->add_surface(s.get());
311 logdebug("Surface %u now on layer %u with rect { %d, %d, %d, %d }",
312 surface_id, layer_id, x, y, w, h);
315 char const *App::activate_surface(uint32_t surface_id) {
316 if (!this->controller->surface_exists(surface_id)) {
317 return "Surface does not exist";
320 // This shouild involve a policy check, but as we do not (yet) have
321 // such a thing, we will just switch to this surface.
322 // XXX: input focus missing!!1
324 // Make it visible, no (or little effect) if already visible
325 auto &s = this->controller->surfaces[surface_id];
327 // Set all others invisible
328 for (auto &i : this->controller->surfaces) {
329 auto &si = this->controller->sprops[i.second->id];
330 if (si.visibility == 1 && si.id != s->id &&
331 int(si.id) != this->layers.main_surface) {
332 i.second->set_visibility(0);
335 s->set_visibility(1);
338 this->controller->commit_changes();
339 this->display->flush();
345 void App::add_task(char const *name, std::function<void()> &&f) {
346 this->pending.emplace_back(std::make_pair(name, f));
349 void App::execute_pending() {
350 if (!this->pending.empty()) {
351 for (auto &t : this->pending) {
352 logdebug("executing task '%s'", t.first);
355 this->pending.clear();
356 this->controller->commit_changes();
357 this->display->flush();
362 // _ __ _ __ _____ _(_) ___ __| | | ____|_ _____ _ __ | |_ ___
363 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | | _| \ \ / / _ \ '_ \| __/ __|
364 // | |_) | | | (_) > <| | __/ (_| | | |___ \ V / __/ | | | |_\__ \
365 // | .__/|_| \___/_/\_\_|\___|\__,_| |_____| \_/ \___|_| |_|\__|___/
367 void App::surface_created(uint32_t surface_id) {
368 logdebug("surface_id is %u", surface_id);
370 // We need to execute the surface setup after its creation.
371 this->add_task("surface_set_layout",
372 [surface_id, this] { this->surface_set_layout(surface_id); });
375 void App::surface_removed(uint32_t surface_id) {
376 logdebug("surface_id is %u", surface_id);
380 // | |__ (_)_ __ __| (_)_ __ __ _ __ _ _ __ (_) (_)_ __ ___ _ __ | |
381 // | '_ \| | '_ \ / _` | | '_ \ / _` | / _` | '_ \| | | | '_ ` _ \| '_ \| |
382 // | |_) | | | | | (_| | | | | | (_| | | (_| | |_) | | | | | | | | | |_) | |
383 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___\__,_| .__/|_| |_|_| |_| |_| .__/|_|
384 // |___/_____| |_| |_|
385 binding_api::result_type binding_api::register_surface(uint32_t appid,
387 logdebug("%s appid %u surfid %u", __func__, appid, surfid);
389 return Err<json_object *>("invalid appid");
392 if (surfid > 0xffff) {
393 return Err<json_object *>("invalid surfaceid");
396 return Ok(json_object_new_int((appid << 16) + surfid));
399 binding_api::result_type binding_api::debug_layers() {
400 logdebug("%s", __func__);
401 return Ok(json_tokener_parse(this->app->layers.to_json().dump().c_str()));
404 binding_api::result_type binding_api::debug_surfaces() {
405 logdebug("%s", __func__);
406 return Ok(to_json(this->app->controller->sprops));
409 binding_api::result_type binding_api::debug_status() {
410 logdebug("%s", __func__);
411 json_object *jr = json_object_new_object();
412 json_object_object_add(jr, "surfaces",
413 to_json(this->app->controller->sprops));
414 json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
418 binding_api::result_type binding_api::debug_terminate() {
419 logdebug("%s", __func__);
420 raise(SIGKILL); // XXX afb-daemon kills it's pgroup using TERM, which
421 // doesn't play well with perf
422 return Ok(json_object_new_object());
425 binding_api::result_type binding_api::demo_activate_surface(
426 uint32_t surfaceid) {
427 char const *e = this->app->activate_surface(surfaceid);
429 return Err<json_object *>(e);
431 return Ok(json_object_new_object());
434 binding_api::result_type binding_api::demo_activate_all() {
435 for (auto &s : this->app->controller->surfaces) {
436 s.second->set_visibility(1);
438 this->app->controller->commit_changes();
439 this->app->display->flush();
440 return Ok(json_object_new_object());
444 // ___ ___ _ __ | |_ _ __ ___ | | | ___ _ __ | |__ ___ ___ | | _____
445 // / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
446 // | (_| (_) | | | | |_| | | (_) | | | __/ | | | | | (_) | (_) | <\__ \
447 // \___\___/|_| |_|\__|_| \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
449 void controller_hooks::surface_created(uint32_t surface_id) {
450 this->app->surface_created(surface_id);
453 void controller_hooks::surface_removed(uint32_t surface_id) {
454 this->app->surface_removed(surface_id);
457 void controller_hooks::add_task(char const *name, std::function<void()> &&f) {
458 this->app->add_task(name, std::move(f));