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 struct wm::area area_from_json(json const &j) {
42 j["name"].get<std::string>(),
44 get<uint32_t>(j["width"]),
45 get<uint32_t>(j["height"]),
49 get<uint32_t>(j["zorder"]),
53 result<struct layout> layout_from_json(json const &j) {
55 auto &ja = j["areas"];
57 auto l = wm::layout{j["name"].get<std::string>(), uint32_t(ja.size()), {}};
59 if (ja.size() > layout::MAX_N_AREAS) {
60 return Err<struct layout>("Invalid number of areas in layout");
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) {
73 DB("loading layout from " << filename);
76 std::ifstream i(filename);
79 auto layouts = layouts_type();
80 std::transform(std::cbegin(jlayouts), std::cend(jlayouts),
81 std::back_inserter(layouts), layout_from_json);
88 App::App(wl::display *d)
94 // layouts(load_layout("../layout.json").unwrap()) {
95 assert(g_app == nullptr);
98 auto l = load_layout("../layout.json");
100 DB("Could not load layout: " << l.unwrap_err());
104 App::~App() { g_app = nullptr; }
107 if (!this->display->ok()) {
111 this->display->add_global_handler(
112 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
113 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
116 this->display->add_global_handler(
117 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
118 this->controller = std::make_unique<genivi::controller>(r, name, v);
120 // XXX: This protocol needs the output, so lets just add our mapping
122 this->controller->add_proxy_to_id_mapping(
123 this->outputs.back()->proxy.get(),
124 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
125 this->outputs.back()->proxy.get())));
128 // First level objects
129 this->display->roundtrip();
130 // Second level objects
131 this->display->roundtrip();
132 // Third level objects
133 this->display->roundtrip();
135 return init_layout();
138 int App::dispatch_events() {
139 int ret = this->display->dispatch();
141 logerror("wl_display_dipatch() returned error %d",
142 this->display->get_error());
145 this->display->flush();
147 // execute pending tasks, that is layout changes etc.
148 this->controller->execute_pending();
149 this->display->roundtrip();
155 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
156 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
157 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
158 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
159 // |_____| |___/ \_\/_/
160 int App::init_layout() {
161 if (!this->controller) {
162 logerror("ivi_controller global not available");
166 if (this->outputs.empty()) {
167 logerror("no output was set up!");
171 auto &c = this->controller;
173 auto &o = this->outputs.front();
174 auto &s = c->screens.begin()->second;
175 auto &layers = c->layers;
177 // XXX: Write output dimensions to ivi controller...
178 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
186 // Setup our dummy scene...
187 c->layer_create(100, 0, 0); // bottom layer, anything else
188 c->layer_create(1000, 0, 0); // top layer, mandelbrot
190 auto &l100 = c->layers[100];
191 auto &l1k = c->layers[1000];
193 // Set layers fullscreen
194 l100->set_destination_rectangle(0, 0, o->width, o->height);
195 l1k->set_destination_rectangle(0, 0, o->width, o->height);
196 l100->set_visibility(1);
197 l1k->set_visibility(1);
199 // Add layers to screen
200 s->set_render_order({100, 1000});
204 this->display->flush();
209 binding_api::result_type binding_api::register_surface(uint32_t appid,
211 logdebug("%s appid %u surfid %u", __func__, appid, surfid);
213 return Err<json_object *>("invalid appid");
216 if (surfid > 0xffff) {
217 return Err<json_object *>("invalid surfaceid");
220 return Ok(json_object_new_int((appid << 16) + surfid));
223 binding_api::result_type binding_api::debug_layers() {
224 logdebug("%s", __func__);
225 return Ok(to_json(this->app->controller->lprops));
228 binding_api::result_type binding_api::debug_surfaces() {
229 logdebug("%s", __func__);
230 return Ok(to_json(this->app->controller->sprops));
233 binding_api::result_type binding_api::debug_status() {
234 logdebug("%s", __func__);
235 json_object *jr = json_object_new_object();
236 json_object_object_add(jr, "surfaces",
237 to_json(this->app->controller->sprops));
238 json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));