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;
300 logdebug("Computed rect={ %d, %d, %d, %d }", x, y, w, h);
302 // configure surface to wxh dimensions
303 s->set_configuration(w, h);
304 // set destination to the display rectangle
305 s->set_destination_rectangle(x, y, w, h);
307 // XXX: visibility should be determined independently of our
308 // layer + geometry setup.
309 s->set_visibility(1);
310 this->controller->layers[layer_id]->add_surface(s.get());
312 logdebug("Surface %u now on layer %u with rect { %d, %d, %d, %d }",
313 surface_id, layer_id, x, y, w, h);
316 char const *App::activate_surface(uint32_t surface_id) {
317 if (!this->controller->surface_exists(surface_id)) {
318 return "Surface does not exist";
321 // This shouild involve a policy check, but as we do not (yet) have
322 // such a thing, we will just switch to this surface.
323 // XXX: input focus missing!!1
325 // Make it visible, no (or little effect) if already visible
326 auto &s = this->controller->surfaces[surface_id];
328 // Set all others invisible
329 for (auto &i : this->controller->surfaces) {
330 auto &si = this->controller->sprops[i.second->id];
331 if (si.visibility == 1 && si.id != s->id &&
332 int(si.id) != this->layers.main_surface) {
333 i.second->set_visibility(0);
336 s->set_visibility(1);
339 this->controller->commit_changes();
340 this->display->flush();
346 void App::add_task(char const *name, std::function<void()> &&f) {
347 this->pending.emplace_back(std::make_pair(name, f));
350 void App::execute_pending() {
351 if (!this->pending.empty()) {
352 for (auto &t : this->pending) {
353 logdebug("executing task '%s'", t.first);
356 this->pending.clear();
357 this->controller->commit_changes();
358 this->display->flush();
363 // _ __ _ __ _____ _(_) ___ __| | | ____|_ _____ _ __ | |_ ___
364 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | | _| \ \ / / _ \ '_ \| __/ __|
365 // | |_) | | | (_) > <| | __/ (_| | | |___ \ V / __/ | | | |_\__ \
366 // | .__/|_| \___/_/\_\_|\___|\__,_| |_____| \_/ \___|_| |_|\__|___/
368 void App::surface_created(uint32_t surface_id) {
369 logdebug("surface_id is %u", surface_id);
371 // We need to execute the surface setup after its creation.
372 this->add_task("surface_set_layout",
373 [surface_id, this] { this->surface_set_layout(surface_id); });
376 void App::surface_removed(uint32_t surface_id) {
377 logdebug("surface_id is %u", surface_id);
381 // | |__ (_)_ __ __| (_)_ __ __ _ __ _ _ __ (_) (_)_ __ ___ _ __ | |
382 // | '_ \| | '_ \ / _` | | '_ \ / _` | / _` | '_ \| | | | '_ ` _ \| '_ \| |
383 // | |_) | | | | | (_| | | | | | (_| | | (_| | |_) | | | | | | | | | |_) | |
384 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___\__,_| .__/|_| |_|_| |_| |_| .__/|_|
385 // |___/_____| |_| |_|
386 binding_api::result_type binding_api::register_surface(uint32_t appid,
388 logdebug("%s appid %u surfid %u", __func__, appid, surfid);
390 return Err<json_object *>("invalid appid");
393 if (surfid > 0xffff) {
394 return Err<json_object *>("invalid surfaceid");
397 return Ok(json_object_new_int((appid << 16) + surfid));
400 binding_api::result_type binding_api::debug_layers() {
401 logdebug("%s", __func__);
402 return Ok(json_tokener_parse(this->app->layers.to_json().dump().c_str()));
405 binding_api::result_type binding_api::debug_surfaces() {
406 logdebug("%s", __func__);
407 return Ok(to_json(this->app->controller->sprops));
410 binding_api::result_type binding_api::debug_status() {
411 logdebug("%s", __func__);
412 json_object *jr = json_object_new_object();
413 json_object_object_add(jr, "surfaces",
414 to_json(this->app->controller->sprops));
415 json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
419 binding_api::result_type binding_api::debug_terminate() {
420 logdebug("%s", __func__);
421 raise(SIGKILL); // XXX afb-daemon kills it's pgroup using TERM, which
422 // doesn't play well with perf
423 return Ok(json_object_new_object());
426 binding_api::result_type binding_api::demo_activate_surface(
427 uint32_t surfaceid) {
428 char const *e = this->app->activate_surface(surfaceid);
430 return Err<json_object *>(e);
432 return Ok(json_object_new_object());
435 binding_api::result_type binding_api::demo_activate_all() {
436 for (auto &s : this->app->controller->surfaces) {
437 s.second->set_visibility(1);
439 this->app->controller->commit_changes();
440 this->app->display->flush();
441 return Ok(json_object_new_object());
445 // ___ ___ _ __ | |_ _ __ ___ | | | ___ _ __ | |__ ___ ___ | | _____
446 // / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
447 // | (_| (_) | | | | |_| | | (_) | | | __/ | | | | | (_) | (_) | <\__ \
448 // \___\___/|_| |_|\__|_| \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
450 void controller_hooks::surface_created(uint32_t surface_id) {
451 this->app->surface_created(surface_id);
454 void controller_hooks::surface_removed(uint32_t surface_id) {
455 this->app->surface_removed(surface_id);
458 void controller_hooks::add_task(char const *name, std::function<void()> &&f) {
459 this->app->add_task(name, std::move(f));