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>
45 result<json> file_to_json(char const *filename) {
46 std::ifstream i(filename);
48 return Err<json>("Could not open config file");
55 struct result<layer_map>
56 load_layer_map(char const *filename) {
57 logdebug("loading IDs from %s", filename);
59 auto j = file_to_json(filename);
61 return Err<layer_map>(j.unwrap_err());
63 json jids = j.unwrap();
65 return to_layer_map(jids);
71 // ___| | __ _ ___ ___ / \ _ __ _ __ (_)_ __ ___ _ __ | |
72 // / __| |/ _` / __/ __| / _ \ | '_ \| '_ \ | | '_ ` _ \| '_ \| |
73 // | (__| | (_| \__ \__ \ / ___ \| |_) | |_) | | | | | | | | |_) | |
74 // \___|_|\__,_|___/___/ /_/ \_\ .__/| .__/ |_|_| |_| |_| .__/|_|
76 App::App(wl::display *d)
86 assert(g_app == nullptr);
91 auto l = load_layer_map(
92 this->config.get_string("layers.json").value().c_str());
94 this->layers = l.unwrap();
96 logerror("%s", l.err().value());
99 } catch (std::exception &e) {
100 logerror("Loading of configuration failed: %s", e.what());
104 App::~App() { g_app = nullptr; }
107 if (!this->display->ok()) {
111 if (this->layers.mapping.empty()) {
112 logerror("No surface -> layer mapping loaded");
116 this->display->add_global_handler(
117 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
118 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
121 this->display->add_global_handler(
122 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
123 this->controller = std::make_unique<struct genivi::controller>(r, name, v);
125 // Init controller hooks
126 this->controller->chooks = &this->chooks;
128 // XXX: This protocol needs the output, so lets just add our mapping
130 this->controller->add_proxy_to_id_mapping(
131 this->outputs.back()->proxy.get(),
132 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
133 this->outputs.back()->proxy.get())));
136 // First level objects
137 this->display->roundtrip();
138 // Second level objects
139 this->display->roundtrip();
140 // Third level objects
141 this->display->roundtrip();
143 return init_layers();
146 int App::dispatch_events() {
147 int ret = this->display->dispatch();
149 logerror("wl_display_dipatch() returned error %d",
150 this->display->get_error());
153 this->display->flush();
155 // execute pending tasks, that is layout changes etc.
156 //this->execute_pending();
162 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
163 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
164 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
165 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
166 // |_____| |___/ \_\/_/
167 int App::init_layers() {
168 if (!this->controller) {
169 logerror("ivi_controller global not available");
173 if (this->outputs.empty()) {
174 logerror("no output was set up!");
178 auto &c = this->controller;
180 auto &o = this->outputs.front();
181 auto &s = c->screens.begin()->second;
182 auto &layers = c->layers;
184 // XXX: Write output dimensions to ivi controller...
185 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
193 // Quick and dirty setup of layers
194 // XXX: This likely needs to be sorted by order (note, we don't (yet?)
195 // do any zorder arrangement).
196 for (auto const &i : this->layers.mapping) {
197 c->layer_create(i.layer_id, o->width, o->height);
198 auto &l = layers[i.layer_id];
199 l->set_destination_rectangle(0, 0, o->width, o->height);
200 l->set_visibility(1);
201 logdebug("Setting up layer %s (%d) for surfaces %d-%d", i.name.c_str(),
202 i.layer_id, i.id_min, i.id_max);
205 // Add layers to screen (XXX: are they sorted correctly?)
206 s->set_render_order(this->layers.layers);
210 this->display->flush();
217 // This can fix the HomeScreen...
218 void redraw_fix(App *app, std::unique_ptr<genivi::surface> &s, int x, int y, int w, int h) {
219 { // XXX: Work around weston redraw issues
220 // trigger an update by changing the source dimensions!
221 s->set_configuration(w + 1, h);
222 s->set_source_rectangle(0, 0, w + 1, h);
223 s->set_destination_rectangle(x, y, w + 1, h);
224 app->controller->commit_changes();
225 app->display->roundtrip();
227 // wait some time, for the process to do its thing...
228 using namespace std::chrono_literals;
229 std::this_thread::sleep_for(100ms);
231 // Set a different size then what we actually want.
232 s->set_configuration(w, h);
233 s->set_source_rectangle(0, 0, w, h);
234 s->set_destination_rectangle(x, y, w, h);
235 app->controller->commit_changes();
236 app->display->roundtrip();
242 void App::surface_init_layout(uint32_t surface_id) {
243 if (!this->controller->surface_exists(surface_id)) {
244 logerror("Surface %d does not exist", int(surface_id));
248 auto o_layer_id = this->layers.get_layer_id(surface_id);
251 logerror("Surface %d is not associated with any layer!", int(surface_id));
255 uint32_t layer_id = o_layer_id.value();
256 logdebug("surface_set_layout for surface %u on layer %u", surface_id,
259 auto const &layer = this->layers.get_layer(layer_id);
260 auto rect = layer.value().rect;
261 auto &s = this->controller->surfaces[surface_id];
268 // less-than-0 values refer to MAX + 1 - $VALUE
269 // e.g. MAX is either screen width or height
271 w = this->controller->output_size.w + 1 + w;
274 h = this->controller->output_size.h + 1 + h;
277 // configure surface to wxh dimensions
278 s->set_configuration(w, h);
280 // set source reactangle, even if we should not need to set it.
281 s->set_source_rectangle(0, 0, w, h);
283 // set destination to the display rectangle
284 s->set_destination_rectangle(x, y, w, h);
286 s->set_visibility(0);
289 this->controller->commit_changes();
290 this->display->roundtrip();
292 redraw_fix(this, s, x, y, w, h);
294 this->controller->layers[layer_id]->add_surface(s.get());
296 // activate the main_surface right away
297 if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
298 logdebug("Activating main_surface (%d)", surface_id);
300 this->activate_surface(this->lookup_name(surface_id).value_or("unknown-name").c_str());
303 logdebug("Surface %u now on layer %u with rect { %d, %d, %d, %d }",
304 surface_id, layer_id, x, y, w, h);
307 char const *App::activate_surface(char const *drawing_name) {
311 auto oid = this->lookup_id(drawing_name);
313 surface_id = oid.value();
315 return "Surface does not exist";
319 if (!this->controller->surface_exists(surface_id)) {
320 return "Surface does not exist";
323 // This should involve a policy check, but as we do not (yet) have
324 // such a thing, we will just switch to this surface.
325 // XXX: input focus missing!!1
327 // Make it visible, no (or little effect) if already visible
328 auto &s = this->controller->surfaces[surface_id];
330 // Set all others invisible
331 for (auto &i : this->controller->surfaces) {
332 auto &si = this->controller->sprops[i.second->id];
333 if (si.id != s->id && si.visibility != 0 &&
334 int(si.id) != this->layers.main_surface) {
335 this->deactivate(si.id);
338 this->activate(s->id);
341 this->controller->commit_changes();
342 this->display->flush();
348 char const *App::deactivate_surface(char const *drawing_name) {
352 auto oid = this->lookup_id(drawing_name);
354 surface_id = oid.value();
356 return "Surface does not exist";
360 if (surface_id == this->layers.main_surface) {
361 return "Cannot deactivate main_surface";
364 this->deactivate(surface_id);
366 this->controller->commit_changes();
367 this->display->flush();
373 // _ __ _ __ _____ _(_) ___ __| | | ____|_ _____ _ __ | |_ ___
374 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | | _| \ \ / / _ \ '_ \| __/ __|
375 // | |_) | | | (_) > <| | __/ (_| | | |___ \ V / __/ | | | |_\__ \
376 // | .__/|_| \___/_/\_\_|\___|\__,_| |_____| \_/ \___|_| |_|\__|___/
378 void App::surface_created(uint32_t surface_id) {
379 logdebug("surface_id is %u", surface_id);
381 this->surface_init_layout(surface_id);
384 void App::surface_removed(uint32_t surface_id) {
385 logdebug("surface_id is %u", surface_id);
387 this->id_alloc.remove_id(surface_id);
390 void App::emit_activated(char const *label) {
391 this->api.send_event("activated", label);
394 void App::emit_deactivated(char const *label) {
395 this->api.send_event("deactivated", label);
398 void App::emit_syncdraw(char const *label) {
399 this->api.send_event("syncdraw", label);
402 void App::emit_flushdraw(char const *label) {
403 this->api.send_event("syncdraw", label);
406 void App::emit_visible(char const *label, bool is_visible) {
407 this->api.send_event(is_visible ? "visible" : "invisible", label);
410 void App::emit_invisible(char const *label) {
411 return emit_visible(label, 0);
414 void App::emit_visible(char const *label) {
415 return emit_visible(label, 1);
418 result<int> App::request_surface(char const *drawing_name) {
419 auto lid = this->layers.get_layer_id(std::string(drawing_name));
421 // XXX: to we need to put these applications on the App layer?
422 return Err<int>("Drawing name does not match any role");
425 auto rname = this->lookup_id(drawing_name);
427 // name does not exist yet, allocate surface id...
428 auto id = int(this->id_alloc.generate_id(drawing_name));
429 this->layers.add_surface(id, lid.value());
431 // XXX: we set the main_surface[_name] here and now,
432 // not sure if we want this, but it worked so far.
433 if (!this->layers.main_surface_name.empty() &&
434 this->layers.main_surface_name == drawing_name) {
435 this->layers.main_surface = id;
436 logdebug("Set main_surface id to %u", id);
442 // Check currently registered drawing names if it is already there.
443 return Err<int>("Surface already present");
446 void App::activate(unsigned id) {
447 if (this->controller->sprops[id].visibility == 0) {
448 this->controller->surfaces[id]->set_visibility(1);
449 char const *label = this->lookup_name(id).value_or("unknown-name").c_str();
450 this->emit_activated(label);
451 this->emit_visible(label);
455 void App::deactivate(unsigned id) {
456 if (this->controller->sprops[id].visibility != 0) {
457 this->controller->surfaces[id]->set_visibility(0);
458 char const *label = this->lookup_name(id).value_or("unknown-name").c_str();
459 this->emit_deactivated(label);
460 this->emit_invisible(label);
464 bool App::can_split(unsigned new_id) {
465 if (this->state.state == LayoutState::LayoutSingle) {
466 auto new_id_layer = this->layers.get_layer_id(new_id).value();
467 auto current_id_layer = this->layers.get_layer_id(this->state.main).value();
469 if (new_id_layer != current_id_layer) {
473 std::string const &new_id_str = this->lookup_name(new_id).value();
474 std::string const &cur_id_str = this->lookup_name(this->state.main).value();
481 // ___ ___ _ __ | |_ _ __ ___ | | | ___ _ __ | |__ ___ ___ | | _____
482 // / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
483 // | (_| (_) | | | | |_| | | (_) | | | __/ | | | | | (_) | (_) | <\__ \
484 // \___\___/|_| |_|\__|_| \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
486 void controller_hooks::surface_created(uint32_t surface_id) {
487 this->app->surface_created(surface_id);
490 void controller_hooks::surface_removed(uint32_t surface_id) {
491 this->app->surface_removed(surface_id);