2 // Created by mfritzsc on 7/11/17.
6 #include "json_helper.hpp"
10 #include "wayland.hpp"
17 #include <json-c/json.h>
19 #include <bits/signum.h>
29 using json = nlohmann::json;
31 struct wm::area area_from_json(json const &j) {
34 j["name"].get<std::string>(),
36 get<int32_t>(j["width"]), get<int32_t>(j["height"]),
37 get<int32_t>(j["x"]), get<int32_t>(j["y"]),
39 get<uint32_t>(j["zorder"]),
43 result<struct layout> layout_from_json(json const &j) {
45 auto &ja = j["areas"];
47 auto l = layout{j["name"].get<std::string>(), uint32_t(ja.size()), {}};
49 if (ja.size() > layout::MAX_N_AREAS) {
50 return Err<struct layout>("Invalid number of areas in layout");
53 logdebug("Loading layout '%s' with %u areas", l.name.c_str(),
56 std::transform(std::cbegin(ja), std::cend(ja), std::begin(l.areas),
62 result<json> file_to_json(char const *filename) {
63 std::ifstream i(filename);
65 return Err<json>("Could not open config file");
72 // Will throw if parsing fails
73 struct result<layouts_type> load_layout(char const *filename) {
74 DB("loading layout from " << filename);
76 auto j = file_to_json(filename);
78 return Err<layouts_type>(j.unwrap_err());
80 json jlayouts = j.unwrap();
82 auto layouts = layouts_type();
83 layouts.reserve(jlayouts.size());
84 std::transform(std::cbegin(jlayouts), std::cend(jlayouts),
85 std::back_inserter(layouts), layout_from_json);
90 struct result<layer_map>
91 load_layer_map(char const *filename) {
92 DB("loading IDs from " << filename);
94 auto j = file_to_json(filename);
96 return Err<layer_map>(j.unwrap_err());
98 json jids = j.unwrap();
100 return to_layer_map(jids);
106 // ___| | __ _ ___ ___ / \ _ __ _ __ (_)_ __ ___ _ __ | |
107 // / __| |/ _` / __/ __| / _ \ | '_ \| '_ \ | | '_ ` _ \| '_ \| |
108 // | (__| | (_| \__ \__ \ / ___ \| |_) | |_) | | | | | | | | |_) | |
109 // \___|_|\__,_|___/___/ /_/ \_\ .__/| .__/ |_|_| |_| |_| .__/|_|
111 App::App(wl::display *d)
120 assert(g_app == nullptr);
125 auto l = load_layer_map(
126 this->config.get_string("layers.json").value().c_str());
128 this->layers = l.unwrap();
130 logerror("%s", l.err().value());
135 auto l = load_layout(
136 this->config.get_string("layout.json").value().c_str());
138 this->layouts = l.unwrap();
140 logerror("%s", l.err().value());
143 } catch (std::exception &e) {
144 logerror("Loading of configuration failed: %s", e.what());
148 App::~App() { g_app = nullptr; }
151 if (!this->display->ok()) {
155 if (this->layers.mapping.empty()) {
156 logerror("No surface -> layer mapping loaded");
160 this->display->add_global_handler(
161 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
162 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
165 this->display->add_global_handler(
166 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
167 this->controller = std::make_unique<genivi::controller>(r, name, v);
169 // Init controller hooks
170 this->controller->chooks = &this->chooks;
172 // XXX: This protocol needs the output, so lets just add our mapping
174 this->controller->add_proxy_to_id_mapping(
175 this->outputs.back()->proxy.get(),
176 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
177 this->outputs.back()->proxy.get())));
180 // First level objects
181 this->display->roundtrip();
182 // Second level objects
183 this->display->roundtrip();
184 // Third level objects
185 this->display->roundtrip();
187 return init_layout();
190 int App::dispatch_events() {
191 int ret = this->display->dispatch();
193 logerror("wl_display_dipatch() returned error %d",
194 this->display->get_error());
197 this->display->flush();
199 // execute pending tasks, that is layout changes etc.
200 this->controller->execute_pending();
201 this->display->roundtrip();
207 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
208 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
209 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
210 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
211 // |_____| |___/ \_\/_/
212 int App::init_layout() {
213 if (!this->controller) {
214 logerror("ivi_controller global not available");
218 if (this->outputs.empty()) {
219 logerror("no output was set up!");
223 auto &c = this->controller;
225 auto &o = this->outputs.front();
226 auto &s = c->screens.begin()->second;
227 auto &layers = c->layers;
229 // XXX: Write output dimensions to ivi controller...
230 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
238 // Quick and dirty setup of layers
239 // XXX: This likely needs to be sorted by order (note, we don't (yet?)
240 // do any zorder arrangement).
241 for (auto const &i : this->layers.mapping) {
242 c->layer_create(i.layer_id, o->width, o->height);
243 auto &l = layers[i.layer_id];
244 l->set_destination_rectangle(0, 0, o->width, o->height);
245 l->set_visibility(1);
246 logdebug("Setting up layer %s (%d) for surfaces %d-%d", i.name.c_str(),
247 i.layer_id, i.id_min, i.id_max);
250 // Add layers to screen (XXX: are they sorted correctly?)
251 s->set_render_order(this->layers.layers);
255 this->display->flush();
261 // _ __ _ __ _____ _(_) ___ __| | | ____|_ _____ _ __ | |_ ___
262 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | | _| \ \ / / _ \ '_ \| __/ __|
263 // | |_) | | | (_) > <| | __/ (_| | | |___ \ V / __/ | | | |_\__ \
264 // | .__/|_| \___/_/\_\_|\___|\__,_| |_____| \_/ \___|_| |_|\__|___/
266 void App::surface_created(uint32_t surface_id) {
267 DB("surface_id is " << surface_id);
268 int layer_id = this->layers.get_layer_id(surface_id).value_or(-1);
269 if (layer_id == -1) {
270 logerror("Surface %d (0x%x) is not part of any layer!", surface_id,
273 auto rect = this->layers.get_layer_rect(surface_id).value();
274 this->controller->add_task(
275 "fullscreen surface",
276 [layer_id, surface_id, rect](struct genivi::controller *c) {
277 auto &s = c->surfaces[surface_id];
284 // less-than-0 values refer to MAX + 1 - $VALUE
285 // e.g. MAX is either screen width or height
287 w = c->output_size.w + 1 + w;
290 h = c->output_size.h + 1 + h;
292 logdebug("Computed rect={ %d, %d, %d, %d }", x, y, w, h);
294 // configure surface to wxh dimensions
295 s->set_configuration(w, h);
296 // set source rect to "entire surface"
297 s->set_source_rectangle(0, 0, w, h);
298 // set destination to the display rectangle
299 s->set_destination_rectangle(x, y, w, h);
301 s->set_visibility(1);
302 c->layers[layer_id]->add_surface(s.get());
303 logdebug("Surface %u now on layer %u", surface_id, layer_id);
308 void App::surface_removed(uint32_t surface_id) {
309 DB("surface_id is " << surface_id);
313 // | |__ (_)_ __ __| (_)_ __ __ _ __ _ _ __ (_) (_)_ __ ___ _ __ | |
314 // | '_ \| | '_ \ / _` | | '_ \ / _` | / _` | '_ \| | | | '_ ` _ \| '_ \| |
315 // | |_) | | | | | (_| | | | | | (_| | | (_| | |_) | | | | | | | | | |_) | |
316 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___\__,_| .__/|_| |_|_| |_| |_| .__/|_|
317 // |___/_____| |_| |_|
318 binding_api::result_type binding_api::register_surface(uint32_t appid,
320 logdebug("%s appid %u surfid %u", __func__, appid, surfid);
322 return Err<json_object *>("invalid appid");
325 if (surfid > 0xffff) {
326 return Err<json_object *>("invalid surfaceid");
329 return Ok(json_object_new_int((appid << 16) + surfid));
332 binding_api::result_type binding_api::debug_layers() {
333 logdebug("%s", __func__);
334 return Ok(to_json(this->app->controller->lprops));
337 binding_api::result_type binding_api::debug_surfaces() {
338 logdebug("%s", __func__);
339 return Ok(to_json(this->app->controller->sprops));
342 binding_api::result_type binding_api::debug_status() {
343 logdebug("%s", __func__);
344 json_object *jr = json_object_new_object();
345 json_object_object_add(jr, "surfaces",
346 to_json(this->app->controller->sprops));
347 json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
351 binding_api::result_type binding_api::debug_terminate() {
352 logdebug("%s", __func__);
353 raise(SIGKILL); // XXX afb-daemon kills it's pgroup using TERM, which
354 // doesn't play well with perf
355 return Ok(json_object_new_object());
359 // ___ ___ _ __ | |_ _ __ ___ | | | ___ _ __ | |__ ___ ___ | | _____
360 // / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
361 // | (_| (_) | | | | |_| | | (_) | | | __/ | | | | | (_) | (_) | <\__ \
362 // \___\___/|_| |_|\__|_| \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
364 void controller_hooks::surface_created(uint32_t surface_id) {
365 this->app->surface_created(surface_id);
368 void controller_hooks::surface_removed(uint32_t surface_id) {
369 this->app->surface_removed(surface_id);