2 // Created by mfritzsc on 7/11/17.
6 #include "json_helper.hpp"
10 #include "wayland.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 return to_surface_id_to_layer_map(jids);
87 App::App(wl::display *d)
88 : api{this}, display{d}, controller{} {
89 // layouts(load_layout("../layout.json").unwrap()) {
90 assert(g_app == nullptr);
93 auto a = load_layout("../layout.json");
94 auto b = a.map_err([](char const *e) -> char const * {
95 DB("Could not load layout: " << e);
99 auto c = load_layer_ids("../ids.json");
100 auto e = c.map_err([](char const *e) -> char const * {
101 DB("Could not load ids: " << e);
106 App::~App() { g_app = nullptr; }
109 if (!this->display->ok()) {
113 this->display->add_global_handler(
114 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
115 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
118 this->display->add_global_handler(
119 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
120 this->controller = std::make_unique<genivi::controller>(r, name, v);
122 // XXX: This protocol needs the output, so lets just add our mapping
124 this->controller->add_proxy_to_id_mapping(
125 this->outputs.back()->proxy.get(),
126 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
127 this->outputs.back()->proxy.get())));
130 // First level objects
131 this->display->roundtrip();
132 // Second level objects
133 this->display->roundtrip();
134 // Third level objects
135 this->display->roundtrip();
137 return init_layout();
140 int App::dispatch_events() {
141 int ret = this->display->dispatch();
143 logerror("wl_display_dipatch() returned error %d",
144 this->display->get_error());
147 this->display->flush();
149 // execute pending tasks, that is layout changes etc.
150 this->controller->execute_pending();
151 this->display->roundtrip();
157 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
158 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
159 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
160 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
161 // |_____| |___/ \_\/_/
162 int App::init_layout() {
163 if (!this->controller) {
164 logerror("ivi_controller global not available");
168 if (this->outputs.empty()) {
169 logerror("no output was set up!");
173 auto &c = this->controller;
175 auto &o = this->outputs.front();
176 auto &s = c->screens.begin()->second;
177 auto &layers = c->layers;
179 // XXX: Write output dimensions to ivi controller...
180 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
188 // Setup our dummy scene...
189 c->layer_create(100, 0, 0); // bottom layer, anything else
190 c->layer_create(1000, 0, 0); // top layer, mandelbrot
192 auto &l100 = c->layers[100];
193 auto &l1k = c->layers[1000];
195 // Set layers fullscreen
196 l100->set_destination_rectangle(0, 0, o->width, o->height);
197 l1k->set_destination_rectangle(0, 0, o->width, o->height);
198 l100->set_visibility(1);
199 l1k->set_visibility(1);
201 // Add layers to screen
202 s->set_render_order({100, 1000});
206 this->display->flush();
211 binding_api::result_type binding_api::register_surface(uint32_t appid,
213 logdebug("%s appid %u surfid %u", __func__, appid, surfid);
215 return Err<json_object *>("invalid appid");
218 if (surfid > 0xffff) {
219 return Err<json_object *>("invalid surfaceid");
222 return Ok(json_object_new_int((appid << 16) + surfid));
225 binding_api::result_type binding_api::debug_layers() {
226 logdebug("%s", __func__);
227 return Ok(to_json(this->app->controller->lprops));
230 binding_api::result_type binding_api::debug_surfaces() {
231 logdebug("%s", __func__);
232 return Ok(to_json(this->app->controller->sprops));
235 binding_api::result_type binding_api::debug_status() {
236 logdebug("%s", __func__);
237 json_object *jr = json_object_new_object();
238 json_object_object_add(jr, "surfaces",
239 to_json(this->app->controller->sprops));
240 json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));