2 // Created by mfritzsc on 7/11/17.
6 #include "json_helper.hpp"
10 #include "wayland.hpp"
17 #include <json-c/json.h>
19 #include <bits/signum.h>
29 using json = nlohmann::json;
31 struct wm::area area_from_json(json const &j) {
34 j["name"].get<std::string>(),
36 get<int32_t>(j["width"]), get<int32_t>(j["height"]),
37 get<int32_t>(j["x"]), get<int32_t>(j["y"]),
39 get<uint32_t>(j["zorder"]),
43 result<struct layout> layout_from_json(json const &j) {
45 auto &ja = j["areas"];
47 auto l = layout{j["name"].get<std::string>(), uint32_t(ja.size()), {}};
49 if (ja.size() > layout::MAX_N_AREAS) {
50 return Err<struct layout>("Invalid number of areas in layout");
53 logdebug("Loading layout '%s' with %u areas", l.name.c_str(),
56 std::transform(std::cbegin(ja), std::cend(ja), std::begin(l.areas),
62 struct result<layouts_type> load_layout(char const *filename) {
63 DB("loading layout from " << filename);
66 std::ifstream i(filename);
69 auto layouts = layouts_type();
70 layouts.reserve(jlayouts.size());
71 std::transform(std::cbegin(jlayouts), std::cend(jlayouts),
72 std::back_inserter(layouts), layout_from_json);
77 struct result<surface_id_to_layer_map>
78 load_layer_ids(char const *filename) {
79 DB("loading IDs from " << filename);
82 std::ifstream i(filename);
85 return to_surface_id_to_layer_map(jids);
91 // ___| | __ _ ___ ___ / \ _ __ _ __ (_)_ __ ___ _ __ | |
92 // / __| |/ _` / __/ __| / _ \ | '_ \| '_ \ | | '_ ` _ \| '_ \| |
93 // | (__| | (_| \__ \__ \ / ___ \| |_) | |_) | | | | | | | | |_) | |
94 // \___|_|\__,_|___/___/ /_/ \_\ .__/| .__/ |_|_| |_| |_| .__/|_|
96 App::App(wl::display *d)
102 layouts(), // load_layout("../layout.json").unwrap()),
103 surface2layer(load_layer_ids("../ids.json").unwrap()) {
104 // layouts(load_layout("../layout.json").unwrap()) {
105 assert(g_app == nullptr);
109 auto l = load_layout("../layout.json");
111 logerror("Coult not load layout configuration: %s", l.err().value());
113 } catch (std::exception &e) {
114 logerror("Coult not load layout configuration: %s", e.what());
118 App::~App() { g_app = nullptr; }
121 if (!this->display->ok()) {
125 this->display->add_global_handler(
126 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
127 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
130 this->display->add_global_handler(
131 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
132 this->controller = std::make_unique<genivi::controller>(r, name, v);
134 // Init controller hooks
135 this->controller->chooks = &this->chooks;
137 // XXX: This protocol needs the output, so lets just add our mapping
139 this->controller->add_proxy_to_id_mapping(
140 this->outputs.back()->proxy.get(),
141 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
142 this->outputs.back()->proxy.get())));
145 // First level objects
146 this->display->roundtrip();
147 // Second level objects
148 this->display->roundtrip();
149 // Third level objects
150 this->display->roundtrip();
152 return init_layout();
155 int App::dispatch_events() {
156 int ret = this->display->dispatch();
158 logerror("wl_display_dipatch() returned error %d",
159 this->display->get_error());
162 this->display->flush();
164 // execute pending tasks, that is layout changes etc.
165 this->controller->execute_pending();
166 this->display->roundtrip();
172 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
173 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
174 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
175 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
176 // |_____| |___/ \_\/_/
177 int App::init_layout() {
178 if (!this->controller) {
179 logerror("ivi_controller global not available");
183 if (this->outputs.empty()) {
184 logerror("no output was set up!");
188 auto &c = this->controller;
190 auto &o = this->outputs.front();
191 auto &s = c->screens.begin()->second;
192 auto &layers = c->layers;
194 // XXX: Write output dimensions to ivi controller...
195 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
203 // Quick and dirty setup of layers
204 // XXX: This likely needs to be sorted by order (note, we don't (yet?)
205 // do any zorder arrangement).
206 for (auto const &i : this->surface2layer.mapping) {
207 c->layer_create(i.layer_id, o->width, o->height);
208 auto &l = layers[i.layer_id];
209 l->set_destination_rectangle(0, 0, o->width, o->height);
210 l->set_visibility(1);
211 logdebug("Setting up layer %s (%d) for surfaces %d-%d", i.name.c_str(),
212 i.layer_id, i.id_min, i.id_max);
215 // Add layers to screen (XXX: are they sorted correctly?)
216 s->set_render_order(this->surface2layer.layers);
220 this->display->flush();
226 // _ __ _ __ _____ _(_) ___ __| | | ____|_ _____ _ __ | |_ ___
227 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | | _| \ \ / / _ \ '_ \| __/ __|
228 // | |_) | | | (_) > <| | __/ (_| | | |___ \ V / __/ | | | |_\__ \
229 // | .__/|_| \___/_/\_\_|\___|\__,_| |_____| \_/ \___|_| |_|\__|___/
231 void App::surface_created(uint32_t surface_id) {
232 DB("surface_id is " << surface_id);
234 this->surface2layer.get_layer_for_surface(surface_id).value_or(-1);
235 if (layer_id == -1) {
236 logerror("Surface %d (0x%x) is not part of any layer!", surface_id,
239 auto rect = this->surface2layer.get_rect_for_surface(surface_id).value();
240 this->controller->add_task(
241 "fullscreen surface",
242 [layer_id, surface_id, rect](struct genivi::controller *c) {
243 auto &s = c->surfaces[surface_id];
251 w = c->output_size.w + 1 + w;
254 h = c->output_size.h + 1 + h;
256 logdebug("Computed rect={ %d, %d, %d, %d }", x, y, w, h);
258 s->set_configuration(w, h);
259 s->set_source_rectangle(0, 0, w, h);
260 s->set_destination_rectangle(x, y, w, h);
262 s->set_visibility(1);
263 c->layers[layer_id]->add_surface(s.get());
264 logdebug("Surface %u now on layer %u", surface_id, layer_id);
269 void App::surface_removed(uint32_t surface_id) {
270 DB("surface_id is " << surface_id);
274 // | |__ (_)_ __ __| (_)_ __ __ _ __ _ _ __ (_) (_)_ __ ___ _ __ | |
275 // | '_ \| | '_ \ / _` | | '_ \ / _` | / _` | '_ \| | | | '_ ` _ \| '_ \| |
276 // | |_) | | | | | (_| | | | | | (_| | | (_| | |_) | | | | | | | | | |_) | |
277 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___\__,_| .__/|_| |_|_| |_| |_| .__/|_|
278 // |___/_____| |_| |_|
279 binding_api::result_type binding_api::register_surface(uint32_t appid,
281 logdebug("%s appid %u surfid %u", __func__, appid, surfid);
283 return Err<json_object *>("invalid appid");
286 if (surfid > 0xffff) {
287 return Err<json_object *>("invalid surfaceid");
290 return Ok(json_object_new_int((appid << 16) + surfid));
293 binding_api::result_type binding_api::debug_layers() {
294 logdebug("%s", __func__);
295 return Ok(to_json(this->app->controller->lprops));
298 binding_api::result_type binding_api::debug_surfaces() {
299 logdebug("%s", __func__);
300 return Ok(to_json(this->app->controller->sprops));
303 binding_api::result_type binding_api::debug_status() {
304 logdebug("%s", __func__);
305 json_object *jr = json_object_new_object();
306 json_object_object_add(jr, "surfaces",
307 to_json(this->app->controller->sprops));
308 json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
312 binding_api::result_type binding_api::debug_terminate() {
313 logdebug("%s", __func__);
314 raise(SIGKILL); // XXX afb-daemon kills it's pgroup using TERM, which
315 // doesn't play well with perf
316 return Ok(json_object_new_object());
320 // ___ ___ _ __ | |_ _ __ ___ | | | ___ _ __ | |__ ___ ___ | | _____
321 // / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
322 // | (_| (_) | | | | |_| | | (_) | | | __/ | | | | | (_) | (_) | <\__ \
323 // \___\___/|_| |_|\__|_| \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
325 void controller_hooks::surface_created(uint32_t surface_id) {
326 this->app->surface_created(surface_id);
329 void controller_hooks::surface_removed(uint32_t surface_id) {
330 this->app->surface_removed(surface_id);