Implement surface names
[staging/windowmanager.git] / src / layers.cpp
index 8f79451..464e20b 100644 (file)
@@ -15,6 +15,7 @@
  */
 
 #include <algorithm>
+#include <regex>
 
 #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<std::string>();
+   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<struct layer_map> 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<struct layer_map> 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<layer> get_surface_id_to_layer(struct layer_map const *s2l,
 
 optional<int> layer_map::get_layer_id(int surface_id) {
    auto e = get_surface_id_to_layer(this, surface_id);
-   return e ? optional<int>(e->layer_id) : nullopt;
+   if (! e) {
+      auto i = this->surfaces.find(surface_id);
+      if (i != this->surfaces.end()) {
+         return optional<int>(int(i->second));
+      }
+      return nullopt;
+   }
+   return optional<int>(e->layer_id);
+}
+
+optional<int> 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<int>(r.second);
+      }
+   }
+   logdebug("role %s does NOT match any layer", role.c_str());
+   return nullopt;
 }
 
 optional<genivi::rect> layer_map::get_layer_rect(int surface_id) {