2 // Created by mfritzsc on 7/11/17.
6 #include "json_helper.hpp"
16 #include <json-c/json.h>
26 std::ostringstream o; \
27 o << __FILE__ << ":" << __LINE__ << ":" << __func__ << ": " << expr; \
28 logdebug(o.str().c_str()); \
37 using json = nlohmann::json;
39 // We ned to manually unwrap numbers
41 result<T> get(json const &j) {
44 std::istringstream s(j.get<std::string>());
46 return !s.eof() || s.fail() ? Err<T>("Could not read int") : Ok(r);
49 struct wm::area area_from_json(json const &j) {
52 j["name"].get<std::string>(),
54 get<uint32_t>(j["width"]),
55 get<uint32_t>(j["height"]),
59 get<uint32_t>(j["zorder"]),
63 result<struct layout> layout_from_json(json const &j) {
65 auto &ja = j["areas"];
67 auto l = wm::layout{j["name"].get<std::string>(), uint32_t(ja.size()), {}};
69 if (ja.size() > layout::MAX_N_AREAS) {
70 return Err<struct layout>("Invalid number of areas in layout");
73 logdebug("Loading layout '%s' with %u areas", l.name.c_str(),
76 std::transform(std::cbegin(ja), std::cend(ja), std::begin(l.areas),
82 struct result<layouts_type> load_layout(char const *filename) {
83 DB("loading layout from " << filename);
86 std::ifstream i(filename);
89 auto layouts = layouts_type();
90 std::transform(std::cbegin(jlayouts), std::cend(jlayouts),
91 std::back_inserter(layouts), layout_from_json);
98 App::App(wl::display *d)
104 // layouts(load_layout("../layout.json").unwrap()) {
105 assert(g_app == nullptr);
108 auto l = load_layout("../layout.json");
110 DB("Could not load layout: " << l.unwrap_err());
114 App::~App() { g_app = nullptr; }
117 if (!this->display->ok()) {
121 this->display->add_global_handler(
122 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
123 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
126 this->display->add_global_handler(
127 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
128 this->controller = std::make_unique<genivi::controller>(r, name, v);
130 // XXX: This protocol needs the output, so lets just add our mapping
132 this->controller->add_proxy_to_id_mapping(
133 this->outputs.back()->proxy.get(),
134 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
135 this->outputs.back()->proxy.get())));
138 // First level objects
139 this->display->roundtrip();
140 // Second level objects
141 this->display->roundtrip();
142 // Third level objects
143 this->display->roundtrip();
145 return init_layout();
148 int App::dispatch_events() {
149 int ret = this->display->dispatch();
151 logerror("wl_display_dipatch() returned error %d",
152 this->display->get_error());
155 this->display->flush();
157 // execute pending tasks, that is layout changes etc.
158 this->controller->execute_pending();
159 this->display->roundtrip();
165 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
166 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
167 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
168 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
169 // |_____| |___/ \_\/_/
170 int App::init_layout() {
171 if (!this->controller) {
172 logerror("ivi_controller global not available");
176 if (this->outputs.empty()) {
177 logerror("no output was set up!");
181 auto &c = this->controller;
183 auto &o = this->outputs.front();
184 auto &s = c->screens.begin()->second;
185 auto &layers = c->layers;
187 // XXX: Write output dimensions to ivi controller...
188 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
196 // Setup our dummy scene...
197 c->layer_create(100, 0, 0); // bottom layer, anything else
198 c->layer_create(1000, 0, 0); // top layer, mandelbrot
200 auto &l100 = c->layers[100];
201 auto &l1k = c->layers[1000];
203 // Set layers fullscreen
204 l100->set_destination_rectangle(0, 0, o->width, o->height);
205 l1k->set_destination_rectangle(0, 0, o->width, o->height);
206 l100->set_visibility(1);
207 l1k->set_visibility(1);
209 // Add layers to screen
210 s->set_render_order({100, 1000});
214 this->display->flush();
219 binding_api::result_type binding_api::register_surface(uint32_t appid,
221 logdebug("%s appid %u surfid %u", __func__, appid, surfid);
223 return Err<json_object *>("invalid appid");
226 if (surfid > 0xffff) {
227 return Err<json_object *>("invalid surfaceid");
230 return Ok(json_object_new_int((appid << 16) + surfid));
233 binding_api::result_type binding_api::debug_layers() {
234 logdebug("%s", __func__);
235 return Ok(to_json(this->app->controller->lprops));
238 binding_api::result_type binding_api::debug_surfaces() {
239 logdebug("%s", __func__);
240 return Ok(to_json(this->app->controller->sprops));
243 binding_api::result_type binding_api::debug_status() {
244 logdebug("%s", __func__);
245 json_object *jr = json_object_new_object();
246 json_object_object_add(jr, "surfaces",
247 to_json(this->app->controller->sprops));
248 json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));