From 6a4504b1fe5e17a09a019edf0377646cc5dd72aa Mon Sep 17 00:00:00 2001 From: Marcus Fritzsch Date: Thu, 17 Aug 2017 16:45:56 +0200 Subject: Implement surface names * request_surface(name: string) -> id: int. * activate_surface(name: string). * names will be mapped to their respective layers by use of the layers' surface rola match, a regex. * the generated IDs are global and not reused. * allow wp-request to use -p, disable use of pygments even if found. Things missing: * surface removal does not remove already established mappings/names. * Mostly untested. Signed-off-by: Marcus Fritzsch --- src/layers.cpp | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) (limited to 'src/layers.cpp') diff --git a/src/layers.cpp b/src/layers.cpp index 8f79451..464e20b 100644 --- a/src/layers.cpp +++ b/src/layers.cpp @@ -15,6 +15,7 @@ */ #include +#include #include "json_helper.hpp" #include "layers.hpp" @@ -31,7 +32,8 @@ layer::layer(nlohmann::json const &j) { } else { this->id_min = this->id_max = j["surface_id"]; } - this->name = j["name"].get(); + this->role = j["role"]; + this->name = j["name"]; this->layer_id = j["layer_id"]; this->rect = genivi::full_rect; if (j["area"]["type"] == "rect") { @@ -47,14 +49,25 @@ struct result to_layer_map(nlohmann::json const &j) { try { layer_map stl{}; auto m = j["mappings"]; - stl.layers.reserve(m.size()); + std::transform(std::cbegin(m), std::cend(m), std::inserter(stl.mapping, stl.mapping.end()), - [&stl](nlohmann::json const &j) { - auto k = layer(j); - stl.layers.push_back(unsigned(k.layer_id)); - return k; + [](nlohmann::json const &j) { + return layer(j); }); + + // XXX: add sanity checks here? + // * check for double IDs + // * check for double names/roles + + stl.layers.reserve(m.size()); + std::transform(std::cbegin(stl.mapping), std::cend(stl.mapping), + std::back_inserter(stl.layers), + [&stl](struct layer const &k) { + stl.roles.emplace_back(std::make_pair(k.role, k.layer_id)); + return unsigned(k.layer_id); + }); + // XXX need to sort layers? for (auto i : stl.mapping) { if (i.name.empty()) { @@ -68,6 +81,7 @@ struct result to_layer_map(nlohmann::json const &j) { auto msi = j.find("main_surface"); if (msi != j.end()) { stl.main_surface = (*msi)["surface_id"]; + stl.main_surface_name = msi->value("surface_role", ""); } // Check lookup @@ -125,7 +139,26 @@ optional get_surface_id_to_layer(struct layer_map const *s2l, optional layer_map::get_layer_id(int surface_id) { auto e = get_surface_id_to_layer(this, surface_id); - return e ? optional(e->layer_id) : nullopt; + if (! e) { + auto i = this->surfaces.find(surface_id); + if (i != this->surfaces.end()) { + return optional(int(i->second)); + } + return nullopt; + } + return optional(e->layer_id); +} + +optional layer_map::get_layer_id(std::string const &role) { + for (auto const &r : this->roles) { + auto re = std::regex(r.first); + if (std::regex_match(role, re)) { + logdebug("role %s matches layer %d", role.c_str(), r.second); + return optional(r.second); + } + } + logdebug("role %s does NOT match any layer", role.c_str()); + return nullopt; } optional layer_map::get_layer_rect(int surface_id) { -- cgit 1.2.3-korg