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 = 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)
100 layouts(load_layout("../layout.json").unwrap()),
101 surface2layer(load_layer_ids("../ids.json").unwrap()) {
102 // layouts(load_layout("../layout.json").unwrap()) {
103 assert(g_app == nullptr);
107 App::~App() { g_app = nullptr; }
110 if (!this->display->ok()) {
114 this->display->add_global_handler(
115 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
116 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
119 this->display->add_global_handler(
120 "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
121 this->controller = std::make_unique<genivi::controller>(r, name, v);
123 // Init controller hooks
124 this->controller->chooks = &this->chooks;
126 // XXX: This protocol needs the output, so lets just add our mapping
128 this->controller->add_proxy_to_id_mapping(
129 this->outputs.back()->proxy.get(),
130 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
131 this->outputs.back()->proxy.get())));
134 // First level objects
135 this->display->roundtrip();
136 // Second level objects
137 this->display->roundtrip();
138 // Third level objects
139 this->display->roundtrip();
141 return init_layout();
144 int App::dispatch_events() {
145 int ret = this->display->dispatch();
147 logerror("wl_display_dipatch() returned error %d",
148 this->display->get_error());
151 this->display->flush();
153 // execute pending tasks, that is layout changes etc.
154 this->controller->execute_pending();
155 this->display->roundtrip();
161 // (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
162 // | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
163 // | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
164 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
165 // |_____| |___/ \_\/_/
166 int App::init_layout() {
167 if (!this->controller) {
168 logerror("ivi_controller global not available");
172 if (this->outputs.empty()) {
173 logerror("no output was set up!");
177 auto &c = this->controller;
179 auto &o = this->outputs.front();
180 auto &s = c->screens.begin()->second;
181 auto &layers = c->layers;
183 // XXX: Write output dimensions to ivi controller...
184 c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
192 // Quick and dirty setup of layers
193 // XXX: This likely needs to be sorted by order (note, we don't (yet?)
194 // do any zorder arrangement).
195 std::vector<unsigned> ls;
196 ls.reserve(this->surface2layer.mapping.size());
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(), i.layer_id, i.id_min, i.id_max);
203 ls.push_back(unsigned(i.layer_id));
206 // Add layers to screen
207 s->set_render_order(ls);
211 this->display->flush();
216 void App::surface_created(uint32_t surface_id) {
217 DB("surface_id is " << surface_id);
219 this->surface2layer.get_layer_for_surface(surface_id).value_or(-1);
220 if (layer_id == -1) {
221 logerror("Surface %d (0x%x) is not part of any layer!", surface_id,
224 this->controller->add_task(
225 "fullscreen surface", [layer_id, surface_id](struct genivi::controller *c) {
226 auto &s = c->surfaces[surface_id];
227 s->set_destination_rectangle(0, 0, c->output_size.w,
229 s->set_visibility(1);
230 c->layers[layer_id]->add_surface(s.get());
231 logdebug("Surface %u now fullscreen on layer %u", surface_id, layer_id);
236 void App::surface_removed(uint32_t surface_id) {
237 DB("surface_id is " << surface_id);
241 // | |__ (_)_ __ __| (_)_ __ __ _ __ _ _ __ (_) (_)_ __ ___ _ __ | |
242 // | '_ \| | '_ \ / _` | | '_ \ / _` | / _` | '_ \| | | | '_ ` _ \| '_ \| |
243 // | |_) | | | | | (_| | | | | | (_| | | (_| | |_) | | | | | | | | | |_) | |
244 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___\__,_| .__/|_| |_|_| |_| |_| .__/|_|
245 // |___/_____| |_| |_|
246 binding_api::result_type binding_api::register_surface(uint32_t appid,
248 logdebug("%s appid %u surfid %u", __func__, appid, surfid);
250 return Err<json_object *>("invalid appid");
253 if (surfid > 0xffff) {
254 return Err<json_object *>("invalid surfaceid");
257 return Ok(json_object_new_int((appid << 16) + surfid));
260 binding_api::result_type binding_api::debug_layers() {
261 logdebug("%s", __func__);
262 return Ok(to_json(this->app->controller->lprops));
265 binding_api::result_type binding_api::debug_surfaces() {
266 logdebug("%s", __func__);
267 return Ok(to_json(this->app->controller->sprops));
270 binding_api::result_type binding_api::debug_status() {
271 logdebug("%s", __func__);
272 json_object *jr = json_object_new_object();
273 json_object_object_add(jr, "surfaces",
274 to_json(this->app->controller->sprops));
275 json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
280 // ___ ___ _ __ | |_ _ __ ___ | | | ___ _ __ | |__ ___ ___ | | _____
281 // / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
282 // | (_| (_) | | | | |_| | | (_) | | | __/ | | | | | (_) | (_) | <\__ \
283 // \___\___/|_| |_|\__|_| \___/|_|_|\___|_|___|_| |_|\___/ \___/|_|\_\___/
285 void controller_hooks::surface_created(uint32_t surface_id) {
286 this->app->surface_created(surface_id);
289 void controller_hooks::surface_removed(uint32_t surface_id) {
290 this->app->surface_removed(surface_id);