2 // Created by mfritzsc on 7/11/17.
6 #include "json_helper.hpp"
16 #include <json-c/json.h>
25 std::cerr << __FILE__ << ":" << __LINE__ << ":" << __func__ << ": " << expr \
34 using json = nlohmann::json;
36 // We ned to manually unwrap numbers
38 result<T> get(json const &j) {
40 std::istringstream s(j.get<std::string>());
42 return s.fail() ? Err<T>("Could not read int") : Ok(r);
45 struct wm::area area_from_json(json const &j) {
47 j["name"].get<std::string>(),
49 get<uint32_t>(j["width"]).unwrap(),
50 get<uint32_t>(j["height"]).unwrap(),
51 get<int32_t>(j["x"]).unwrap(),
52 get<int32_t>(j["y"]).unwrap(),
54 get<uint32_t>(j["zorder"]).unwrap(),
58 struct layout layout_from_json(json const &j) {
59 auto &ja = j["areas"];
61 auto l = wm::layout{j["name"].get<std::string>(), uint32_t(ja.size()), {}};
63 logdebug("Loading layout '%s' with %u areas", l.name.c_str(),
66 std::transform(std::cbegin(ja), std::cend(ja), std::begin(l.areas),
72 struct result<layouts_type> load_layout(char const *filename) {
74 std::ifstream i(filename);
77 size_t nlayouts = jlayouts.size();
78 auto layouts = layouts_type(nlayouts);
80 std::transform(std::cbegin(jlayouts), std::cend(jlayouts),
81 std::begin(layouts), layout_from_json);
88 App::App(wl::display *d)
94 // layouts(load_layout("../layout.json").unwrap()) {
95 assert(g_app == nullptr);
99 App::~App() { g_app = nullptr; }
102 if (!this->display->ok()) {
106 this->display->add_global_handler(
107 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
108 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
111 this->display->add_global_handler(
112 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
113 this->controller = std::make_unique<genivi::controller>(r, name, v);
115 // XXX: This protocol needs the output, so lets just add our mapping
117 this->controller->add_proxy_to_id_mapping(
118 this->outputs.back()->proxy.get(),
119 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
120 this->outputs.back()->proxy.get())));
123 // First level objects
124 this->display->roundtrip();
125 // Second level objects
126 this->display->roundtrip();
127 // Third level objects
128 this->display->roundtrip();
130 return init_layout();
133 int App::dispatch_events() {
134 int ret = this->display->dispatch();
136 logerror("wl_display_dipatch() returned error %d",
137 this->display->get_error());
140 this->display->flush();
142 // execute pending tasks, that is layout changes etc.
143 this->controller->execute_pending();
144 this->display->roundtrip();
150 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
151 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
152 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
153 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
154 // |_____| |___/ \_\/_/
155 int App::init_layout() {
156 if (!this->controller) {
157 logerror("ivi_controller global not available");
161 if (this->outputs.empty()) {
162 logerror("no output was set up!");
166 auto &c = this->controller;
168 auto &o = this->outputs.front();
169 auto &s = c->screens.begin()->second;
170 auto &layers = c->layers;
172 // XXX: Write output dimensions to ivi controller...
173 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
181 // Setup our dummy scene...
182 c->layer_create(100, 0, 0); // bottom layer, anything else
183 c->layer_create(1000, 0, 0); // top layer, mandelbrot
185 auto &l100 = c->layers[100];
186 auto &l1k = c->layers[1000];
188 // Set layers fullscreen
189 l100->set_destination_rectangle(0, 0, o->width, o->height);
190 l1k->set_destination_rectangle(0, 0, o->width, o->height);
191 l100->set_visibility(1);
192 l1k->set_visibility(1);
194 // Add layers to screen
195 s->set_render_order({100, 1000});
199 this->display->flush();
204 binding_api::result_type binding_api::register_surface(uint32_t appid,
206 logdebug("%s appid %u surfid %u", __func__, appid, surfid);
208 return Err<json_object *>("invalid appid");
211 if (surfid > 0xffff) {
212 return Err<json_object *>("invalid surfaceid");
215 return Ok(json_object_new_int((appid << 16) + surfid));
218 binding_api::result_type binding_api::debug_layers() {
219 logdebug("%s", __func__);
220 return Ok(to_json(this->app->controller->lprops));
223 binding_api::result_type binding_api::debug_surfaces() {
224 logdebug("%s", __func__);
225 return Ok(to_json(this->app->controller->sprops));
228 binding_api::result_type binding_api::debug_status() {
229 logdebug("%s", __func__);
230 json_object *jr = json_object_new_object();
231 json_object_object_add(jr, "surfaces",
232 to_json(this->app->controller->sprops));
233 json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));