2 // Created by mfritzsc on 7/11/17.
6 #include "json_helper.hpp"
16 #include <json-c/json.h>
26 using json = nlohmann::json;
28 struct wm::area area_from_json(json const &j) {
31 j["name"].get<std::string>(),
33 get<uint32_t>(j["width"]),
34 get<uint32_t>(j["height"]),
38 get<uint32_t>(j["zorder"]),
42 result<struct layout> layout_from_json(json const &j) {
44 auto &ja = j["areas"];
46 auto l = wm::layout{j["name"].get<std::string>(), uint32_t(ja.size()), {}};
48 if (ja.size() > layout::MAX_N_AREAS) {
49 return Err<struct layout>("Invalid number of areas in layout");
52 logdebug("Loading layout '%s' with %u areas", l.name.c_str(),
55 std::transform(std::cbegin(ja), std::cend(ja), std::begin(l.areas),
61 struct result<layouts_type> load_layout(char const *filename) {
62 DB("loading layout from " << filename);
65 std::ifstream i(filename);
68 auto layouts = layouts_type();
69 std::transform(std::cbegin(jlayouts), std::cend(jlayouts),
70 std::back_inserter(layouts), layout_from_json);
77 App::App(wl::display *d)
83 // layouts(load_layout("../layout.json").unwrap()) {
84 assert(g_app == nullptr);
87 auto l = load_layout("../layout.json");
89 DB("Could not load layout: " << l.unwrap_err());
93 App::~App() { g_app = nullptr; }
96 if (!this->display->ok()) {
100 this->display->add_global_handler(
101 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
102 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
105 this->display->add_global_handler(
106 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
107 this->controller = std::make_unique<genivi::controller>(r, name, v);
109 // XXX: This protocol needs the output, so lets just add our mapping
111 this->controller->add_proxy_to_id_mapping(
112 this->outputs.back()->proxy.get(),
113 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
114 this->outputs.back()->proxy.get())));
117 // First level objects
118 this->display->roundtrip();
119 // Second level objects
120 this->display->roundtrip();
121 // Third level objects
122 this->display->roundtrip();
124 return init_layout();
127 int App::dispatch_events() {
128 int ret = this->display->dispatch();
130 logerror("wl_display_dipatch() returned error %d",
131 this->display->get_error());
134 this->display->flush();
136 // execute pending tasks, that is layout changes etc.
137 this->controller->execute_pending();
138 this->display->roundtrip();
144 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
145 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
146 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
147 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
148 // |_____| |___/ \_\/_/
149 int App::init_layout() {
150 if (!this->controller) {
151 logerror("ivi_controller global not available");
155 if (this->outputs.empty()) {
156 logerror("no output was set up!");
160 auto &c = this->controller;
162 auto &o = this->outputs.front();
163 auto &s = c->screens.begin()->second;
164 auto &layers = c->layers;
166 // XXX: Write output dimensions to ivi controller...
167 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
175 // Setup our dummy scene...
176 c->layer_create(100, 0, 0); // bottom layer, anything else
177 c->layer_create(1000, 0, 0); // top layer, mandelbrot
179 auto &l100 = c->layers[100];
180 auto &l1k = c->layers[1000];
182 // Set layers fullscreen
183 l100->set_destination_rectangle(0, 0, o->width, o->height);
184 l1k->set_destination_rectangle(0, 0, o->width, o->height);
185 l100->set_visibility(1);
186 l1k->set_visibility(1);
188 // Add layers to screen
189 s->set_render_order({100, 1000});
193 this->display->flush();
198 binding_api::result_type binding_api::register_surface(uint32_t appid,
200 logdebug("%s appid %u surfid %u", __func__, appid, surfid);
202 return Err<json_object *>("invalid appid");
205 if (surfid > 0xffff) {
206 return Err<json_object *>("invalid surfaceid");
209 return Ok(json_object_new_int((appid << 16) + surfid));
212 binding_api::result_type binding_api::debug_layers() {
213 logdebug("%s", __func__);
214 return Ok(to_json(this->app->controller->lprops));
217 binding_api::result_type binding_api::debug_surfaces() {
218 logdebug("%s", __func__);
219 return Ok(to_json(this->app->controller->sprops));
222 binding_api::result_type binding_api::debug_status() {
223 logdebug("%s", __func__);
224 json_object *jr = json_object_new_object();
225 json_object_object_add(jr, "surfaces",
226 to_json(this->app->controller->sprops));
227 json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));