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)
133 assert(g_app == nullptr);
138 auto l = load_layer_map(
139 this->config.get_string("layers.json").value().c_str());
141 this->layers = l.unwrap();
143 logerror("%s", l.err().value());
149 load_layout(this->config.get_string("layout.json").value().c_str());
151 this->layouts = l.unwrap();
153 logerror("%s", l.err().value());
156 } catch (std::exception &e) {
157 logerror("Loading of configuration failed: %s", e.what());
161 App::~App() { g_app = nullptr; }
164 if (!this->display->ok()) {
168 if (this->layers.mapping.empty()) {
169 logerror("No surface -> layer mapping loaded");
173 this->display->add_global_handler(
174 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
175 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
178 this->display->add_global_handler(
179 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
180 this->controller = std::make_unique<genivi::controller>(r, name, v);
182 // Init controller hooks
183 this->controller->chooks = &this->chooks;
185 // XXX: This protocol needs the output, so lets just add our mapping
187 this->controller->add_proxy_to_id_mapping(
188 this->outputs.back()->proxy.get(),
189 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
190 this->outputs.back()->proxy.get())));
193 // First level objects
194 this->display->roundtrip();
195 // Second level objects
196 this->display->roundtrip();
197 // Third level objects
198 this->display->roundtrip();
200 return init_layout();
203 int App::dispatch_events() {
204 int ret = this->display->dispatch();
206 logerror("wl_display_dipatch() returned error %d",
207 this->display->get_error());
210 this->display->flush();
212 // execute pending tasks, that is layout changes etc.
213 this->execute_pending();
219 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
220 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
221 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
222 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
223 // |_____| |___/ \_\/_/
224 int App::init_layout() {
225 if (!this->controller) {
226 logerror("ivi_controller global not available");
230 if (this->outputs.empty()) {
231 logerror("no output was set up!");
235 auto &c = this->controller;
237 auto &o = this->outputs.front();
238 auto &s = c->screens.begin()->second;
239 auto &layers = c->layers;
241 // XXX: Write output dimensions to ivi controller...
242 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
250 // Quick and dirty setup of layers
251 // XXX: This likely needs to be sorted by order (note, we don't (yet?)
252 // do any zorder arrangement).
253 for (auto const &i : this->layers.mapping) {
254 c->layer_create(i.layer_id, o->width, o->height);
255 auto &l = layers[i.layer_id];
256 l->set_destination_rectangle(0, 0, o->width, o->height);
257 l->set_visibility(1);
258 logdebug("Setting up layer %s (%d) for surfaces %d-%d", i.name.c_str(),
259 i.layer_id, i.id_min, i.id_max);
262 // Add layers to screen (XXX: are they sorted correctly?)
263 s->set_render_order(this->layers.layers);
267 this->display->flush();
272 void App::surface_set_layout(uint32_t surface_id) {
273 auto o_layer_id = this->layers.get_layer_id(surface_id);
276 logerror("Surface %d is not associated with any layer!", int(surface_id));
280 if (!this->controller->surface_exists(surface_id)) {
281 logerror("Surface %d does not exist", int(surface_id));
285 uint32_t layer_id = o_layer_id.value();
286 logdebug("surface_set_layout for surface %u on layer %u", surface_id,
289 auto const &layer = this->layers.get_layer(layer_id);
290 auto rect = layer.value().rect;
291 auto &s = this->controller->surfaces[surface_id];
298 // less-than-0 values refer to MAX + 1 - $VALUE
299 // e.g. MAX is either screen width or height
301 w = this->controller->output_size.w + 1 + w;
304 h = this->controller->output_size.h + 1 + h;
307 // configure surface to wxh dimensions
308 s->set_configuration(w, h);
309 // set source reactangle, even if we should not need to set it.
310 s->set_source_rectangle(0, 0, w, h);
311 // set destination to the display rectangle
312 s->set_destination_rectangle(x, y, w, h);
314 // XXX: The main_surface will be visible regardless
316 surface_id == static_cast<unsigned>(this->layers.main_surface) ? 1 : 0);
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);
323 char const *App::activate_surface(uint32_t surface_id) {
324 if (!this->controller->surface_exists(surface_id)) {
325 return "Surface does not exist";
328 // This should involve a policy check, but as we do not (yet) have
329 // such a thing, we will just switch to this surface.
330 // XXX: input focus missing!!1
332 // Make it visible, no (or little effect) if already visible
333 auto &s = this->controller->surfaces[surface_id];
335 // Set all others invisible
336 for (auto &i : this->controller->surfaces) {
337 auto &si = this->controller->sprops[i.second->id];
338 if (si.visibility == 1 && si.id != s->id &&
339 int(si.id) != this->layers.main_surface) {
340 i.second->set_visibility(0);
343 s->set_visibility(1);
346 this->controller->commit_changes();
347 this->display->flush();
353 void App::add_task(char const *name, std::function<void()> &&f) {
354 this->pending.emplace_back(std::make_pair(name, f));
357 void App::execute_pending() {
358 if (!this->pending.empty()) {
359 for (auto &t : this->pending) {
360 logdebug("executing task '%s'", t.first);
363 this->pending.clear();
364 this->controller->commit_changes();
365 this->display->flush();
370 // _ __ _ __ _____ _(_) ___ __| | | ____|_ _____ _ __ | |_ ___
371 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | | _| \ \ / / _ \ '_ \| __/ __|
372 // | |_) | | | (_) > <| | __/ (_| | | |___ \ V / __/ | | | |_\__ \
373 // | .__/|_| \___/_/\_\_|\___|\__,_| |_____| \_/ \___|_| |_|\__|___/
375 void App::surface_created(uint32_t surface_id) {
376 logdebug("surface_id is %u", surface_id);
378 // We need to execute the surface setup after its creation.
379 this->add_task("surface_set_layout",
380 [surface_id, this] { this->surface_set_layout(surface_id); });
383 void App::surface_removed(uint32_t surface_id) {
384 logdebug("surface_id is %u", surface_id);
386 this->add_task("remove surface ID",
387 [surface_id, this] { this->id_alloc.remove_id(surface_id); });
390 result<int> App::request_surface(char const *drawing_name) {
391 auto lid = this->layers.get_layer_id(std::string(drawing_name));
393 // XXX: to we need to put these applications on the App layer?
394 return Err<int>("Drawing name does not match any role");
397 auto rname = this->id_alloc.lookup(drawing_name);
399 // name does not exist yet, allocate surface id...
400 auto id = int(this->id_alloc.generate_id(drawing_name));
401 this->layers.add_surface(id, lid.value());
403 // XXX: you should fix this!
404 if (!this->layers.main_surface_name.empty() &&
405 this->layers.main_surface_name == drawing_name) {
406 this->layers.main_surface = id;
407 this->activate_surface(id);
408 logdebug("Set main_surface id to %u", id);
414 // Check currently registered drawing names if it is already there.
415 return Err<int>("Surface already present");
418 char const *App::activate_surface(char const *drawing_name) {
419 auto osid = this->id_alloc.lookup(drawing_name);
422 logdebug("ativate surface with name %s and id %u", drawing_name,
424 this->activate_surface(osid.value());
428 logerror("surface %s unknown", drawing_name);
429 return "Surface unknown";
433 // | |__ (_)_ __ __| (_)_ __ __ _ __ _ _ __ (_) (_)_ __ ___ _ __ | |
434 // | '_ \| | '_ \ / _` | | '_ \ / _` | / _` | '_ \| | | | '_ ` _ \| '_ \| |
435 // | |_) | | | | | (_| | | | | | (_| | | (_| | |_) | | | | | | | | | |_) | |
436 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___\__,_| .__/|_| |_|_| |_| |_| .__/|_|
437 // |___/_____| |_| |_|
438 binding_api::result_type binding_api::request_surface(
439 char const *drawing_name) {
440 auto r = this->app->request_surface(drawing_name);
442 return Err<json_object *>(r.unwrap_err());
444 return Ok(json_object_new_int(r.unwrap()));
447 binding_api::result_type binding_api::activate_surface(
448 char const *drawing_name) {
449 logdebug("%s drawing_name %s", __func__, drawing_name);
450 auto r = this->app->activate_surface(drawing_name);
452 return Err<json_object *>(r);
454 return Ok(json_object_new_object());
457 binding_api::result_type binding_api::list_drawing_names() {
458 json j = this->app->id_alloc.names;
459 return Ok(json_tokener_parse(j.dump().c_str()));
462 binding_api::result_type binding_api::debug_layers() {
463 logdebug("%s", __func__);
464 return Ok(json_tokener_parse(this->app->layers.to_json().dump().c_str()));
467 binding_api::result_type binding_api::debug_surfaces() {
468 logdebug("%s", __func__);
469 return Ok(to_json(this->app->controller->sprops));
472 binding_api::result_type binding_api::debug_status() {
473 logdebug("%s", __func__);
474 json_object *jr = json_object_new_object();
475 json_object_object_add(jr, "surfaces",
476 to_json(this->app->controller->sprops));
477 json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
481 binding_api::result_type binding_api::debug_terminate() {
482 logdebug("%s", __func__);
483 raise(SIGKILL); // XXX afb-daemon kills it's pgroup using TERM, which
484 // doesn't play well with perf
485 return Ok(json_object_new_object());
488 binding_api::result_type binding_api::demo_activate_surface(
489 uint32_t surfaceid) {
490 char const *e = this->app->activate_surface(surfaceid);
492 return Err<json_object *>(e);
494 return Ok(json_object_new_object());
497 binding_api::result_type binding_api::demo_activate_all() {
498 for (auto &s : this->app->controller->surfaces) {
499 s.second->set_visibility(1);
501 this->app->controller->commit_changes();
502 this->app->display->flush();
503 return Ok(json_object_new_object());
507 // ___ ___ _ __ | |_ _ __ ___ | | | ___ _ __ | |__ ___ ___ | | _____
508 // / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
509 // | (_| (_) | | | | |_| | | (_) | | | __/ | | | | | (_) | (_) | <\__ \
510 // \___\___/|_| |_|\__|_| \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
512 void controller_hooks::surface_created(uint32_t surface_id) {
513 this->app->surface_created(surface_id);
516 void controller_hooks::surface_removed(uint32_t surface_id) {
517 this->app->surface_removed(surface_id);
520 void controller_hooks::add_task(char const *name, std::function<void()> &&f) {
521 this->app->add_task(name, std::move(f));