2 // Created by mfritzsc on 7/11/17.
6 #include "json_helper.hpp"
17 #include <json-c/json.h>
27 using json = nlohmann::json;
29 struct wm::area area_from_json(json const &j) {
32 j["name"].get<std::string>(),
34 get<uint32_t>(j["width"]), get<uint32_t>(j["height"]),
35 get<int32_t>(j["x"]), get<int32_t>(j["y"]),
37 get<uint32_t>(j["zorder"]),
41 result<struct layout> layout_from_json(json const &j) {
43 auto &ja = j["areas"];
45 auto l = wm::layout{j["name"].get<std::string>(), uint32_t(ja.size()), {}};
47 if (ja.size() > layout::MAX_N_AREAS) {
48 return Err<struct layout>("Invalid number of areas in layout");
51 logdebug("Loading layout '%s' with %u areas", l.name.c_str(),
54 std::transform(std::cbegin(ja), std::cend(ja), std::begin(l.areas),
60 struct result<layouts_type> load_layout(char const *filename) {
61 DB("loading layout from " << filename);
64 std::ifstream i(filename);
67 auto layouts = layouts_type();
68 std::transform(std::cbegin(jlayouts), std::cend(jlayouts),
69 std::back_inserter(layouts), layout_from_json);
74 struct result<surface_id_to_layer_map>
75 load_layer_ids(char const *filename) {
76 DB("loading IDs from " << filename);
79 std::ifstream i(filename);
82 auto m = to_surface_id_to_layer_map(jids["mappings"]);
87 auto jtests = jids.value("tests", json());
89 std::vector<std::pair<int, int>> tests;
90 std::transform(std::cbegin(jtests), std::cend(jtests),
91 std::back_inserter(tests), [](json const &j) {
92 return std::make_pair(get<int>(j["surface_id"]),
93 get<int>(j["expect_layer_id"]));
96 for (auto sid : tests) {
97 if (i.get_layer_for_surface(sid.first).value_or(-1) != sid.second) {
98 return Err<surface_id_to_layer_map>("ID Map embedded test failed!");
108 App::App(wl::display *d)
109 : api{this}, display{d}, controller{}, outputs(), layouts() {
110 // layouts(load_layout("../layout.json").unwrap()) {
111 assert(g_app == nullptr);
114 auto a = load_layout("../layout.json");
115 auto b = a.map_err([](char const *e) -> char const * {
116 DB("Could not load layout: " << e);
120 auto c = load_layer_ids("../ids.json");
121 auto e = c.map_err([](char const *e) -> char const * {
122 DB("Could not load ids: " << e);
127 App::~App() { g_app = nullptr; }
130 if (!this->display->ok()) {
134 this->display->add_global_handler(
135 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
136 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
139 this->display->add_global_handler(
140 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
141 this->controller = std::make_unique<genivi::controller>(r, name, v);
143 // XXX: This protocol needs the output, so lets just add our mapping
145 this->controller->add_proxy_to_id_mapping(
146 this->outputs.back()->proxy.get(),
147 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
148 this->outputs.back()->proxy.get())));
151 // First level objects
152 this->display->roundtrip();
153 // Second level objects
154 this->display->roundtrip();
155 // Third level objects
156 this->display->roundtrip();
158 return init_layout();
161 int App::dispatch_events() {
162 int ret = this->display->dispatch();
164 logerror("wl_display_dipatch() returned error %d",
165 this->display->get_error());
168 this->display->flush();
170 // execute pending tasks, that is layout changes etc.
171 this->controller->execute_pending();
172 this->display->roundtrip();
178 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
179 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
180 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
181 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
182 // |_____| |___/ \_\/_/
183 int App::init_layout() {
184 if (!this->controller) {
185 logerror("ivi_controller global not available");
189 if (this->outputs.empty()) {
190 logerror("no output was set up!");
194 auto &c = this->controller;
196 auto &o = this->outputs.front();
197 auto &s = c->screens.begin()->second;
198 auto &layers = c->layers;
200 // XXX: Write output dimensions to ivi controller...
201 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
209 // Setup our dummy scene...
210 c->layer_create(100, 0, 0); // bottom layer, anything else
211 c->layer_create(1000, 0, 0); // top layer, mandelbrot
213 auto &l100 = c->layers[100];
214 auto &l1k = c->layers[1000];
216 // Set layers fullscreen
217 l100->set_destination_rectangle(0, 0, o->width, o->height);
218 l1k->set_destination_rectangle(0, 0, o->width, o->height);
219 l100->set_visibility(1);
220 l1k->set_visibility(1);
222 // Add layers to screen
223 s->set_render_order({100, 1000});
227 this->display->flush();
232 binding_api::result_type binding_api::register_surface(uint32_t appid,
234 logdebug("%s appid %u surfid %u", __func__, appid, surfid);
236 return Err<json_object *>("invalid appid");
239 if (surfid > 0xffff) {
240 return Err<json_object *>("invalid surfaceid");
243 return Ok(json_object_new_int((appid << 16) + surfid));
246 binding_api::result_type binding_api::debug_layers() {
247 logdebug("%s", __func__);
248 return Ok(to_json(this->app->controller->lprops));
251 binding_api::result_type binding_api::debug_surfaces() {
252 logdebug("%s", __func__);
253 return Ok(to_json(this->app->controller->sprops));
256 binding_api::result_type binding_api::debug_status() {
257 logdebug("%s", __func__);
258 json_object *jr = json_object_new_object();
259 json_object_object_add(jr, "surfaces",
260 to_json(this->app->controller->sprops));
261 json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));