app: when loading config, handle returned errors
[staging/windowmanager.git] / src / layers.cpp
index 0d01080..7cc2f00 100644 (file)
@@ -13,7 +13,7 @@ namespace wm {
 using json = nlohmann::json;
 
 surface_id_to_layer::surface_id_to_layer(nlohmann::json const &j) {
-   // DB(j);
+   DB(j);
    if (j["type"] == "range") {
       this->id_min = get<int>(j["first_surface_id"]);
       this->id_max = get<int>(j["last_surface_id"]);
@@ -22,6 +22,14 @@ surface_id_to_layer::surface_id_to_layer(nlohmann::json const &j) {
    }
    this->name = j["name"].get<std::string>();
    this->layer_id = get<int>(j["layer_id"]);
+   this->rect = genivi::rect{-1, -1, 0, 0};
+   if (j["area"]["type"] == "rect") {
+      auto jr = j["area"]["rect"];
+      this->rect = genivi::rect{
+         get<int32_t>(jr["width"]), get<int32_t>(jr["height"]),
+         get<int32_t>(jr["x"]), get<int32_t>(jr["y"]),
+      };
+   }
 }
 
 struct result<struct surface_id_to_layer_map> to_surface_id_to_layer_map(
@@ -31,14 +39,13 @@ struct result<struct surface_id_to_layer_map> to_surface_id_to_layer_map(
       surface_id_to_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 = surface_id_to_layer(j);
-            stl.layers.push_back(unsigned(k.layer_id));
-            return k;
-         });
+      std::transform(std::cbegin(m), std::cend(m),
+                     std::inserter(stl.mapping, stl.mapping.end()),
+                     [&stl](nlohmann::json const &j) {
+                        auto k = surface_id_to_layer(j);
+                        stl.layers.push_back(unsigned(k.layer_id));
+                        return k;
+                     });
       // XXX need to sort layers?
       for (auto i : stl.mapping) {
          if (i.name.empty()) {
@@ -54,15 +61,16 @@ struct result<struct surface_id_to_layer_map> to_surface_id_to_layer_map(
       // Check lookup
       auto jtests = j.value("tests", json());
 
-      if (! jtests.empty()) {
+      if (!jtests.empty()) {
          DB("Embedded tests...");
          std::vector<std::pair<int, int>> tests;
          tests.reserve(jtests.size());
          std::transform(std::cbegin(jtests), std::cend(jtests),
                         std::back_inserter(tests), [](json const &j) {
-                    return std::make_pair(get<int>(j["surface_id"]),
-                                          get<int>(j["expect_layer_id"]));
-                 });
+                           return std::make_pair(
+                              get<int>(j["surface_id"]),
+                              get<int>(j["expect_layer_id"]));
+                        });
 
          for (auto sid : tests) {
             int lid = stl.get_layer_for_surface(sid.first).value_or(-1);
@@ -70,7 +78,7 @@ struct result<struct surface_id_to_layer_map> to_surface_id_to_layer_map(
                        << ", expect=" << sid.second);
             if (lid != sid.second) {
                return Err<surface_id_to_layer_map>(
-                       "ID Map embedded test failed!");
+                  "ID Map embedded test failed!");
             }
          }
       }
@@ -87,19 +95,33 @@ inline bool
    return a.id_max < b;
 }
 
-optional<int> surface_id_to_layer_map::get_layer_for_surface(int surface_id) {
-   auto i = std::lower_bound(std::cbegin(this->mapping),
-                             std::cend(this->mapping), surface_id);
+namespace {
+optional<surface_id_to_layer> get_surface_id_to_layer(
+   struct surface_id_to_layer_map const *s2l, int surface_id) {
+   auto i = std::lower_bound(std::cbegin(s2l->mapping), std::cend(s2l->mapping),
+                             surface_id);
 
-   if (i != this->mapping.end()) {
+   if (i != s2l->mapping.end()) {
       // std::less only checks for surface_id_to_layer::id_max, so check
       // that we are actually inside of an interval here.
       if (i->id_min <= surface_id) {
-         return optional<int>(i->layer_id);
+         return optional<surface_id_to_layer>(*i);
       }
    }
 
    return nullopt;
 }
+}
+
+optional<int> surface_id_to_layer_map::get_layer_for_surface(int surface_id) {
+   auto e = get_surface_id_to_layer(this, surface_id);
+   return e ? optional<int>(e->layer_id) : nullopt;
+}
+
+optional<genivi::rect> surface_id_to_layer_map::get_rect_for_surface(
+   int surface_id) {
+   auto e = get_surface_id_to_layer(this, surface_id);
+   return e ? optional<genivi::rect>(e->rect) : nullopt;
+}
 
 }  // namespace wm