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 layouts.reserve(jlayouts.size());
69 std::transform(std::cbegin(jlayouts), std::cend(jlayouts),
70 std::back_inserter(layouts), layout_from_json);
75 struct result<surface_id_to_layer_map>
76 load_layer_ids(char const *filename) {
77 DB("loading IDs from " << filename);
80 std::ifstream i(filename);
83 return to_surface_id_to_layer_map(jids);
89 // ___| | __ _ ___ ___ / \ _ __ _ __ (_)_ __ ___ _ __ | |
90 // / __| |/ _` / __/ __| / _ \ | '_ \| '_ \ | | '_ ` _ \| '_ \| |
91 // | (__| | (_| \__ \__ \ / ___ \| |_) | |_) | | | | | | | | |_) | |
92 // \___|_|\__,_|___/___/ /_/ \_\ .__/| .__/ |_|_| |_| |_| .__/|_|
94 App::App(wl::display *d)
95 : api{this}, chooks{this}, display{d}, controller{} {
96 // layouts(load_layout("../layout.json").unwrap()) {
97 assert(g_app == nullptr);
100 auto a = load_layout("../layout.json");
101 auto b = a.map_err([](char const *e) -> char const * {
102 DB("Could not load layout: " << e);
106 auto c = load_layer_ids("../ids.json");
107 auto e = c.map_err([](char const *e) -> char const * {
108 DB("Could not load ids: " << e);
113 App::~App() { g_app = nullptr; }
116 if (!this->display->ok()) {
120 this->display->add_global_handler(
121 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
122 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
125 this->display->add_global_handler(
126 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
127 this->controller = std::make_unique<genivi::controller>(r, name, v);
129 // Init controller hooks
130 this->controller->chooks = &this->chooks;
132 // XXX: This protocol needs the output, so lets just add our mapping
134 this->controller->add_proxy_to_id_mapping(
135 this->outputs.back()->proxy.get(),
136 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
137 this->outputs.back()->proxy.get())));
140 // First level objects
141 this->display->roundtrip();
142 // Second level objects
143 this->display->roundtrip();
144 // Third level objects
145 this->display->roundtrip();
147 return init_layout();
150 int App::dispatch_events() {
151 int ret = this->display->dispatch();
153 logerror("wl_display_dipatch() returned error %d",
154 this->display->get_error());
157 this->display->flush();
159 // execute pending tasks, that is layout changes etc.
160 this->controller->execute_pending();
161 this->display->roundtrip();
167 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
168 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
169 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
170 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
171 // |_____| |___/ \_\/_/
172 int App::init_layout() {
173 if (!this->controller) {
174 logerror("ivi_controller global not available");
178 if (this->outputs.empty()) {
179 logerror("no output was set up!");
183 auto &c = this->controller;
185 auto &o = this->outputs.front();
186 auto &s = c->screens.begin()->second;
187 auto &layers = c->layers;
189 // XXX: Write output dimensions to ivi controller...
190 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
198 // Setup our dummy scene...
199 c->layer_create(100, 0, 0); // bottom layer, anything else
200 c->layer_create(1000, 0, 0); // top layer, mandelbrot
202 auto &l100 = c->layers[100];
203 auto &l1k = c->layers[1000];
205 // Set layers fullscreen
206 l100->set_destination_rectangle(0, 0, o->width, o->height);
207 l1k->set_destination_rectangle(0, 0, o->width, o->height);
208 l100->set_visibility(1);
209 l1k->set_visibility(1);
211 // Add layers to screen
212 s->set_render_order({100, 1000});
216 this->display->flush();
221 void App::surface_created(uint32_t surface_id) {
222 DB("surface_id is " << surface_id);
223 this->controller->add_task("fullscreen surface", [surface_id](struct genivi::controller *c) {
224 auto &s = c->surfaces[surface_id];
225 s->set_destination_rectangle(0, 0, c->output_size.w, c->output_size.h);
226 s->set_visibility(1);
227 uint32_t lid = surface_id == 0x16180 ? 1000 : 100;
228 c->layers[lid]->add_surface(s.get());
229 logdebug("Surface %u now fullscreen on layer %u", surface_id, lid);
233 void App::surface_removed(uint32_t surface_id) {
234 DB("surface_id is " << surface_id);
238 // | |__ (_)_ __ __| (_)_ __ __ _ __ _ _ __ (_) (_)_ __ ___ _ __ | |
239 // | '_ \| | '_ \ / _` | | '_ \ / _` | / _` | '_ \| | | | '_ ` _ \| '_ \| |
240 // | |_) | | | | | (_| | | | | | (_| | | (_| | |_) | | | | | | | | | |_) | |
241 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___\__,_| .__/|_| |_|_| |_| |_| .__/|_|
242 // |___/_____| |_| |_|
243 binding_api::result_type binding_api::register_surface(uint32_t appid,
245 logdebug("%s appid %u surfid %u", __func__, appid, surfid);
247 return Err<json_object *>("invalid appid");
250 if (surfid > 0xffff) {
251 return Err<json_object *>("invalid surfaceid");
254 return Ok(json_object_new_int((appid << 16) + surfid));
257 binding_api::result_type binding_api::debug_layers() {
258 logdebug("%s", __func__);
259 return Ok(to_json(this->app->controller->lprops));
262 binding_api::result_type binding_api::debug_surfaces() {
263 logdebug("%s", __func__);
264 return Ok(to_json(this->app->controller->sprops));
267 binding_api::result_type binding_api::debug_status() {
268 logdebug("%s", __func__);
269 json_object *jr = json_object_new_object();
270 json_object_object_add(jr, "surfaces",
271 to_json(this->app->controller->sprops));
272 json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
277 // ___ ___ _ __ | |_ _ __ ___ | | | ___ _ __ | |__ ___ ___ | | _____
278 // / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
279 // | (_| (_) | | | | |_| | | (_) | | | __/ | | | | | (_) | (_) | <\__ \
280 // \___\___/|_| |_|\__|_| \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
282 void controller_hooks::surface_created(uint32_t surface_id) {
283 this->app->surface_created(surface_id);
286 void controller_hooks::surface_removed(uint32_t surface_id) {
287 this->app->surface_removed(surface_id);