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);
88 // ___| | __ _ ___ ___ / \ _ __ _ __ (_)_ __ ___ _ __ | |
89 // / __| |/ _` / __/ __| / _ \ | '_ \| '_ \ | | '_ ` _ \| '_ \| |
90 // | (__| | (_| \__ \__ \ / ___ \| |_) | |_) | | | | | | | | |_) | |
91 // \___|_|\__,_|___/___/ /_/ \_\ .__/| .__/ |_|_| |_| |_| .__/|_|
93 App::App(wl::display *d)
94 : api{this}, chooks{this}, display{d}, controller{} {
95 // layouts(load_layout("../layout.json").unwrap()) {
96 assert(g_app == nullptr);
99 auto a = load_layout("../layout.json");
100 auto b = a.map_err([](char const *e) -> char const * {
101 DB("Could not load layout: " << e);
105 auto c = load_layer_ids("../ids.json");
106 auto e = c.map_err([](char const *e) -> char const * {
107 DB("Could not load ids: " << e);
112 App::~App() { g_app = nullptr; }
115 if (!this->display->ok()) {
119 this->display->add_global_handler(
120 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
121 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
124 this->display->add_global_handler(
125 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
126 this->controller = std::make_unique<genivi::controller>(r, name, v);
128 // Init controller hooks
129 this->controller->chooks = &this->chooks;
131 // XXX: This protocol needs the output, so lets just add our mapping
133 this->controller->add_proxy_to_id_mapping(
134 this->outputs.back()->proxy.get(),
135 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
136 this->outputs.back()->proxy.get())));
139 // First level objects
140 this->display->roundtrip();
141 // Second level objects
142 this->display->roundtrip();
143 // Third level objects
144 this->display->roundtrip();
146 return init_layout();
149 int App::dispatch_events() {
150 int ret = this->display->dispatch();
152 logerror("wl_display_dipatch() returned error %d",
153 this->display->get_error());
156 this->display->flush();
158 // execute pending tasks, that is layout changes etc.
159 this->controller->execute_pending();
160 this->display->roundtrip();
166 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
167 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
168 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
169 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
170 // |_____| |___/ \_\/_/
171 int App::init_layout() {
172 if (!this->controller) {
173 logerror("ivi_controller global not available");
177 if (this->outputs.empty()) {
178 logerror("no output was set up!");
182 auto &c = this->controller;
184 auto &o = this->outputs.front();
185 auto &s = c->screens.begin()->second;
186 auto &layers = c->layers;
188 // XXX: Write output dimensions to ivi controller...
189 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
197 // Setup our dummy scene...
198 c->layer_create(100, 0, 0); // bottom layer, anything else
199 c->layer_create(1000, 0, 0); // top layer, mandelbrot
201 auto &l100 = c->layers[100];
202 auto &l1k = c->layers[1000];
204 // Set layers fullscreen
205 l100->set_destination_rectangle(0, 0, o->width, o->height);
206 l1k->set_destination_rectangle(0, 0, o->width, o->height);
207 l100->set_visibility(1);
208 l1k->set_visibility(1);
210 // Add layers to screen
211 s->set_render_order({100, 1000});
215 this->display->flush();
220 void App::surface_created(uint32_t surface_id) {
221 DB("surface_id is " << surface_id);
222 this->controller->add_task("fullscreen surface", [surface_id](struct genivi::controller *c) {
223 auto &s = c->surfaces[surface_id];
224 s->set_destination_rectangle(0, 0, c->output_size.w, c->output_size.h);
225 s->set_visibility(1);
226 uint32_t lid = surface_id == 0x16180 ? 1000 : 100;
227 c->layers[lid]->add_surface(s.get());
228 logdebug("Surface %u now fullscreen on layer %u", surface_id, lid);
232 void App::surface_removed(uint32_t surface_id) {
233 DB("surface_id is " << surface_id);
237 // | |__ (_)_ __ __| (_)_ __ __ _ __ _ _ __ (_) (_)_ __ ___ _ __ | |
238 // | '_ \| | '_ \ / _` | | '_ \ / _` | / _` | '_ \| | | | '_ ` _ \| '_ \| |
239 // | |_) | | | | | (_| | | | | | (_| | | (_| | |_) | | | | | | | | | |_) | |
240 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___\__,_| .__/|_| |_|_| |_| |_| .__/|_|
241 // |___/_____| |_| |_|
242 binding_api::result_type binding_api::register_surface(uint32_t appid,
244 logdebug("%s appid %u surfid %u", __func__, appid, surfid);
246 return Err<json_object *>("invalid appid");
249 if (surfid > 0xffff) {
250 return Err<json_object *>("invalid surfaceid");
253 return Ok(json_object_new_int((appid << 16) + surfid));
256 binding_api::result_type binding_api::debug_layers() {
257 logdebug("%s", __func__);
258 return Ok(to_json(this->app->controller->lprops));
261 binding_api::result_type binding_api::debug_surfaces() {
262 logdebug("%s", __func__);
263 return Ok(to_json(this->app->controller->sprops));
266 binding_api::result_type binding_api::debug_status() {
267 logdebug("%s", __func__);
268 json_object *jr = json_object_new_object();
269 json_object_object_add(jr, "surfaces",
270 to_json(this->app->controller->sprops));
271 json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
276 // ___ ___ _ __ | |_ _ __ ___ | | | ___ _ __ | |__ ___ ___ | | _____
277 // / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
278 // | (_| (_) | | | | |_| | | (_) | | | __/ | | | | | (_) | (_) | <\__ \
279 // \___\___/|_| |_|\__|_| \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
281 void controller_hooks::surface_created(uint32_t surface_id) {
282 this->app->surface_created(surface_id);
285 void controller_hooks::surface_removed(uint32_t surface_id) {
286 this->app->surface_removed(surface_id);