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>
32 #include <bits/signum.h>
42 using json = nlohmann::json;
44 struct wm::area area_from_json(json const &j) {
48 j["width"], j["height"], j["x"], j["y"],
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)
134 assert(g_app == nullptr);
139 auto l = load_layer_map(
140 this->config.get_string("layers.json").value().c_str());
142 this->layers = l.unwrap();
144 logerror("%s", l.err().value());
150 load_layout(this->config.get_string("layout.json").value().c_str());
152 this->layouts = l.unwrap();
154 logerror("%s", l.err().value());
157 } catch (std::exception &e) {
158 logerror("Loading of configuration failed: %s", e.what());
162 App::~App() { g_app = nullptr; }
165 if (!this->display->ok()) {
169 if (this->layers.mapping.empty()) {
170 logerror("No surface -> layer mapping loaded");
174 this->display->add_global_handler(
175 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
176 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
179 this->display->add_global_handler(
180 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
181 this->controller = std::make_unique<struct genivi::controller>(r, name, v);
183 // Init controller hooks
184 this->controller->chooks = &this->chooks;
186 // XXX: This protocol needs the output, so lets just add our mapping
188 this->controller->add_proxy_to_id_mapping(
189 this->outputs.back()->proxy.get(),
190 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
191 this->outputs.back()->proxy.get())));
194 // First level objects
195 this->display->roundtrip();
196 // Second level objects
197 this->display->roundtrip();
198 // Third level objects
199 this->display->roundtrip();
201 return init_layout();
204 int App::dispatch_events() {
205 int ret = this->display->dispatch();
207 logerror("wl_display_dipatch() returned error %d",
208 this->display->get_error());
211 this->display->flush();
213 // execute pending tasks, that is layout changes etc.
214 this->execute_pending();
220 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
221 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
222 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
223 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
224 // |_____| |___/ \_\/_/
225 int App::init_layout() {
226 if (!this->controller) {
227 logerror("ivi_controller global not available");
231 if (this->outputs.empty()) {
232 logerror("no output was set up!");
236 auto &c = this->controller;
238 auto &o = this->outputs.front();
239 auto &s = c->screens.begin()->second;
240 auto &layers = c->layers;
242 // XXX: Write output dimensions to ivi controller...
243 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
251 // Quick and dirty setup of layers
252 // XXX: This likely needs to be sorted by order (note, we don't (yet?)
253 // do any zorder arrangement).
254 for (auto const &i : this->layers.mapping) {
255 c->layer_create(i.layer_id, o->width, o->height);
256 auto &l = layers[i.layer_id];
257 l->set_destination_rectangle(0, 0, o->width, o->height);
258 l->set_visibility(1);
259 logdebug("Setting up layer %s (%d) for surfaces %d-%d", i.name.c_str(),
260 i.layer_id, i.id_min, i.id_max);
263 // Add layers to screen (XXX: are they sorted correctly?)
264 s->set_render_order(this->layers.layers);
268 this->display->flush();
273 void App::surface_set_layout(uint32_t surface_id) {
274 if (!this->controller->surface_exists(surface_id)) {
275 logerror("Surface %d does not exist", int(surface_id));
279 auto o_layer_id = this->layers.get_layer_id(surface_id);
282 logerror("Surface %d is not associated with any layer!", int(surface_id));
286 uint32_t layer_id = o_layer_id.value();
287 logdebug("surface_set_layout for surface %u on layer %u", surface_id,
290 auto const &layer = this->layers.get_layer(layer_id);
291 auto rect = layer.value().rect;
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;
308 // configure surface to wxh dimensions
309 s->set_configuration(w, h);
310 // set source reactangle, even if we should not need to set it.
311 s->set_source_rectangle(0, 0, w, h);
312 // set destination to the display rectangle
313 s->set_destination_rectangle(x, y, w, h);
315 // XXX: The main_surface will be visible regardless
317 surface_id == static_cast<unsigned>(this->layers.main_surface) ? 1 : 0);
318 this->controller->layers[layer_id]->add_surface(s.get());
320 logdebug("Surface %u now on layer %u with rect { %d, %d, %d, %d }",
321 surface_id, layer_id, x, y, w, h);
324 char const *App::activate_surface(uint32_t surface_id) {
325 if (!this->controller->surface_exists(surface_id)) {
326 return "Surface does not exist";
329 // This should involve a policy check, but as we do not (yet) have
330 // such a thing, we will just switch to this surface.
331 // XXX: input focus missing!!1
333 // Make it visible, no (or little effect) if already visible
334 auto &s = this->controller->surfaces[surface_id];
336 // Set all others invisible
337 for (auto &i : this->controller->surfaces) {
338 auto &si = this->controller->sprops[i.second->id];
339 if (si.visibility == 1 && si.id != s->id &&
340 int(si.id) != this->layers.main_surface) {
341 i.second->set_visibility(0);
344 s->set_visibility(1);
347 this->controller->commit_changes();
348 this->display->flush();
350 // Current active surface is the first in last_active
351 this->last_active.push_front(s->id);
352 /// // XXX: I am not sure I even need 5 elements...
353 /// this->last_active.resize(std::min(int(this->last_active.size()), 5));
360 char const *App::deactivate_surface(uint32_t surface_id) {
361 if (surface_id == this->layers.main_surface) {
362 return "Cannot deactivate main_surface";
365 if (this->last_active.empty()) {
366 return "Cannot deactivate surface (not active)";
369 // XXX: should an active surface not alsways be front() or
370 // front+1() of last_active?!
372 // XXX: Should I really be able to deactivate a surface that is not
373 // front() of last_active?
374 auto is = std::find(this->last_active.begin(),
375 this->last_active.end(),
378 if (is == this->last_active.end()) {
379 return "Cannot deactivate surface (not active)";
382 this->last_active.erase(is);
384 if (! this->last_active.empty()) {
385 // Should be active already, shouldn't it?
386 this->activate_surface(this->last_active.front());
388 this->activate_surface(this->layers.main_surface);
394 void App::add_task(char const *name, std::function<void()> &&f) {
395 this->pending.emplace_back(std::make_pair(name, f));
398 void App::execute_pending() {
399 if (!this->pending.empty()) {
400 for (auto &t : this->pending) {
401 logdebug("executing task '%s'", t.first);
404 this->pending.clear();
405 this->controller->commit_changes();
406 this->display->flush();
411 // _ __ _ __ _____ _(_) ___ __| | | ____|_ _____ _ __ | |_ ___
412 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | | _| \ \ / / _ \ '_ \| __/ __|
413 // | |_) | | | (_) > <| | __/ (_| | | |___ \ V / __/ | | | |_\__ \
414 // | .__/|_| \___/_/\_\_|\___|\__,_| |_____| \_/ \___|_| |_|\__|___/
416 void App::surface_created(uint32_t surface_id) {
417 surface_id &= id_allocator::id_mask;
419 logdebug("surface_id is %u", surface_id);
421 // We need to execute the surface setup after its creation.
422 this->add_task("surface_set_layout",
423 [surface_id, this] { this->surface_set_layout(surface_id); });
426 void App::surface_removed(uint32_t surface_id) {
427 surface_id &= id_allocator::id_mask;
429 logdebug("surface_id is %u", surface_id);
431 this->add_task("remove surface ID",
433 this->id_alloc.remove_id(surface_id);
435 // Also remove from last_active, if found
436 auto i = std::find(this->last_active.begin(),
437 this->last_active.end(), surface_id);
438 if (i != this->last_active.end()) {
439 this->last_active.erase(i);
444 result<int> App::request_surface(char const *drawing_name) {
445 auto lid = this->layers.get_layer_id(std::string(drawing_name));
447 // XXX: to we need to put these applications on the App layer?
448 return Err<int>("Drawing name does not match any role");
451 auto rname = this->id_alloc.lookup(drawing_name);
453 // name does not exist yet, allocate surface id...
454 auto id = int(this->id_alloc.generate_id(drawing_name));
455 this->layers.add_surface(id, lid.value());
457 // XXX: you should fix this!
458 if (!this->layers.main_surface_name.empty() &&
459 this->layers.main_surface_name == drawing_name) {
460 this->layers.main_surface = id;
461 this->activate_surface(id);
462 logdebug("Set main_surface id to %u", id);
468 // Check currently registered drawing names if it is already there.
469 return Err<int>("Surface already present");
472 char const *App::activate_surface(char const *drawing_name) {
473 auto osid = this->id_alloc.lookup(drawing_name);
476 logdebug("ativate surface with name %s and id %u", drawing_name,
478 return this->activate_surface(osid.value());
481 logerror("surface %s unknown", drawing_name);
482 return "Surface unknown";
485 char const *App::deactivate_surface(char const *drawing_name) {
486 auto osid = this->id_alloc.lookup(drawing_name);
489 logdebug("deativate surface with name %s and id %u", drawing_name,
491 return this->deactivate_surface(osid.value());
494 logerror("surface %s unknown", drawing_name);
495 return "Surface unknown";
499 // | |__ (_)_ __ __| (_)_ __ __ _ __ _ _ __ (_) (_)_ __ ___ _ __ | |
500 // | '_ \| | '_ \ / _` | | '_ \ / _` | / _` | '_ \| | | | '_ ` _ \| '_ \| |
501 // | |_) | | | | | (_| | | | | | (_| | | (_| | |_) | | | | | | | | | |_) | |
502 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___\__,_| .__/|_| |_|_| |_| |_| .__/|_|
503 // |___/_____| |_| |_|
504 binding_api::result_type binding_api::request_surface(
505 char const *drawing_name) {
506 auto r = this->app->request_surface(drawing_name);
508 return Err<json_object *>(r.unwrap_err());
510 return Ok(json_object_new_int(r.unwrap()));
513 binding_api::result_type binding_api::activate_surface(
514 char const *drawing_name) {
515 logdebug("%s drawing_name %s", __func__, drawing_name);
516 auto r = this->app->activate_surface(drawing_name);
518 return Err<json_object *>(r);
520 return Ok(json_object_new_object());
523 binding_api::result_type binding_api::deactivate_surface(char const* drawing_name) {
524 logdebug("%s drawing_name %s", __func__, drawing_name);
525 auto r = this->app->deactivate_surface(drawing_name);
527 return Err<json_object *>(r);
529 return Ok(json_object_new_object());
532 binding_api::result_type binding_api::enddraw(char const* drawing_name) {
533 logdebug("%s drawing_name %s", __func__, drawing_name);
534 return Err<json_object*>("not implemented");
537 binding_api::result_type binding_api::list_drawing_names() {
538 logdebug("%s", __func__);
539 json j = this->app->id_alloc.names;
540 return Ok(json_tokener_parse(j.dump().c_str()));
543 binding_api::result_type binding_api::debug_layers() {
544 logdebug("%s", __func__);
545 return Ok(json_tokener_parse(this->app->layers.to_json().dump().c_str()));
548 binding_api::result_type binding_api::debug_surfaces() {
549 logdebug("%s", __func__);
550 return Ok(to_json(this->app->controller->sprops));
553 binding_api::result_type binding_api::debug_status() {
554 logdebug("%s", __func__);
555 json_object *jr = json_object_new_object();
556 json_object_object_add(jr, "surfaces",
557 to_json(this->app->controller->sprops));
558 json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
562 binding_api::result_type binding_api::debug_terminate() {
563 logdebug("%s", __func__);
564 raise(SIGKILL); // XXX afb-daemon kills it's pgroup using TERM, which
565 // doesn't play well with perf
566 return Ok(json_object_new_object());
569 binding_api::result_type binding_api::demo_activate_surface(
570 uint32_t surfaceid) {
571 char const *e = this->app->activate_surface(surfaceid);
573 return Err<json_object *>(e);
575 return Ok(json_object_new_object());
578 binding_api::result_type binding_api::demo_activate_all() {
579 for (auto &s : this->app->controller->surfaces) {
580 s.second->set_visibility(1);
582 this->app->controller->commit_changes();
583 this->app->display->flush();
584 return Ok(json_object_new_object());
588 // ___ ___ _ __ | |_ _ __ ___ | | | ___ _ __ | |__ ___ ___ | | _____
589 // / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
590 // | (_| (_) | | | | |_| | | (_) | | | __/ | | | | | (_) | (_) | <\__ \
591 // \___\___/|_| |_|\__|_| \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
593 void controller_hooks::surface_created(uint32_t surface_id) {
594 this->app->surface_created(surface_id);
597 void controller_hooks::surface_removed(uint32_t surface_id) {
598 this->app->surface_removed(surface_id);
601 void controller_hooks::add_task(char const *name, std::function<void()> &&f) {
602 this->app->add_task(name, std::move(f));