2 // Created by mfritzsc on 7/11/17.
6 #include "json_helper.hpp"
10 #include "wayland.hpp"
17 #include <json-c/json.h>
21 #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 App::~App() { g_app = nullptr; }
112 if (!this->display->ok()) {
116 this->display->add_global_handler(
117 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
118 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
121 this->display->add_global_handler(
122 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
123 this->controller = std::make_unique<genivi::controller>(r, name, v);
125 // Init controller hooks
126 this->controller->chooks = &this->chooks;
128 // XXX: This protocol needs the output, so lets just add our mapping
130 this->controller->add_proxy_to_id_mapping(
131 this->outputs.back()->proxy.get(),
132 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
133 this->outputs.back()->proxy.get())));
136 // First level objects
137 this->display->roundtrip();
138 // Second level objects
139 this->display->roundtrip();
140 // Third level objects
141 this->display->roundtrip();
143 return init_layout();
146 int App::dispatch_events() {
147 int ret = this->display->dispatch();
149 logerror("wl_display_dipatch() returned error %d",
150 this->display->get_error());
153 this->display->flush();
155 // execute pending tasks, that is layout changes etc.
156 this->controller->execute_pending();
157 this->display->roundtrip();
163 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
164 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
165 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
166 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
167 // |_____| |___/ \_\/_/
168 int App::init_layout() {
169 if (!this->controller) {
170 logerror("ivi_controller global not available");
174 if (this->outputs.empty()) {
175 logerror("no output was set up!");
179 auto &c = this->controller;
181 auto &o = this->outputs.front();
182 auto &s = c->screens.begin()->second;
183 auto &layers = c->layers;
185 // XXX: Write output dimensions to ivi controller...
186 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
194 // Quick and dirty setup of layers
195 // XXX: This likely needs to be sorted by order (note, we don't (yet?)
196 // do any zorder arrangement).
197 for (auto const &i : this->surface2layer.mapping) {
198 c->layer_create(i.layer_id, o->width, o->height);
199 auto &l = layers[i.layer_id];
200 l->set_destination_rectangle(0, 0, o->width, o->height);
201 l->set_visibility(1);
202 logdebug("Setting up layer %s (%d) for surfaces %d-%d", i.name.c_str(),
203 i.layer_id, i.id_min, i.id_max);
206 // Add layers to screen (XXX: are they sorted correctly?)
207 s->set_render_order(this->surface2layer.layers);
211 this->display->flush();
217 // _ __ _ __ _____ _(_) ___ __| | | ____|_ _____ _ __ | |_ ___
218 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | | _| \ \ / / _ \ '_ \| __/ __|
219 // | |_) | | | (_) > <| | __/ (_| | | |___ \ V / __/ | | | |_\__ \
220 // | .__/|_| \___/_/\_\_|\___|\__,_| |_____| \_/ \___|_| |_|\__|___/
222 void App::surface_created(uint32_t surface_id) {
223 DB("surface_id is " << surface_id);
225 this->surface2layer.get_layer_for_surface(surface_id).value_or(-1);
226 if (layer_id == -1) {
227 logerror("Surface %d (0x%x) is not part of any layer!", surface_id,
230 this->controller->add_task(
231 "fullscreen surface",
232 [layer_id, surface_id](struct genivi::controller *c) {
233 auto &s = c->surfaces[surface_id];
234 // s->set_destination_rectangle(0, 0, c->output_size.w, c->output_size.h);
235 // s->set_source_rectangle(0, 100, c->output_size.w, c->output_size.h - 200);
236 if (layer_id != 1000) {
237 // s->set_source_rectangle(0, 0, c->output_size.w, c->output_size.h - 200);
238 s->set_configuration(c->output_size.w, c->output_size.h - 200);
239 s->set_destination_rectangle(0, 100, c->output_size.w,
240 c->output_size.h - 200);
242 // s->set_source_rectangle(0, 0, c->output_size.w, c->output_size.h);
243 s->set_configuration(c->output_size.w, c->output_size.h);
244 s->set_destination_rectangle(0, 0, c->output_size.w,
247 s->set_visibility(1);
248 c->layers[layer_id]->add_surface(s.get());
249 logdebug("Surface %u now on layer %u", surface_id,
255 void App::surface_removed(uint32_t surface_id) {
256 DB("surface_id is " << surface_id);
260 // | |__ (_)_ __ __| (_)_ __ __ _ __ _ _ __ (_) (_)_ __ ___ _ __ | |
261 // | '_ \| | '_ \ / _` | | '_ \ / _` | / _` | '_ \| | | | '_ ` _ \| '_ \| |
262 // | |_) | | | | | (_| | | | | | (_| | | (_| | |_) | | | | | | | | | |_) | |
263 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___\__,_| .__/|_| |_|_| |_| |_| .__/|_|
264 // |___/_____| |_| |_|
265 binding_api::result_type binding_api::register_surface(uint32_t appid,
267 logdebug("%s appid %u surfid %u", __func__, appid, surfid);
269 return Err<json_object *>("invalid appid");
272 if (surfid > 0xffff) {
273 return Err<json_object *>("invalid surfaceid");
276 return Ok(json_object_new_int((appid << 16) + surfid));
279 binding_api::result_type binding_api::debug_layers() {
280 logdebug("%s", __func__);
281 return Ok(to_json(this->app->controller->lprops));
284 binding_api::result_type binding_api::debug_surfaces() {
285 logdebug("%s", __func__);
286 return Ok(to_json(this->app->controller->sprops));
289 binding_api::result_type binding_api::debug_status() {
290 logdebug("%s", __func__);
291 json_object *jr = json_object_new_object();
292 json_object_object_add(jr, "surfaces",
293 to_json(this->app->controller->sprops));
294 json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
298 binding_api::result_type binding_api::debug_terminate() {
299 logdebug("%s", __func__);
300 raise(SIGKILL); // XXX afb-daemon kills it's pgroup using TERM, which doesn't play well with perf
301 return Ok(json_object_new_object());
305 // ___ ___ _ __ | |_ _ __ ___ | | | ___ _ __ | |__ ___ ___ | | _____
306 // / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
307 // | (_| (_) | | | | |_| | | (_) | | | __/ | | | | | (_) | (_) | <\__ \
308 // \___\___/|_| |_|\__|_| \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
310 void controller_hooks::surface_created(uint32_t surface_id) {
311 this->app->surface_created(surface_id);
314 void controller_hooks::surface_removed(uint32_t surface_id) {
315 this->app->surface_removed(surface_id);