App: cleanup name/id mapping and its reverse
[staging/windowmanager.git] / src / app.cpp
index b8d3a86..6a3e2e6 100644 (file)
@@ -128,7 +128,6 @@ App::App(wl::display *d)
      config(),
      layouts(),
      layers(),
-     name_mapping(),
      id_alloc{},
      last_active() {
    assert(g_app == nullptr);
@@ -357,14 +356,25 @@ void App::surface_set_layout(uint32_t surface_id) {
    if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
       logdebug("Activating main_surface (%d)", surface_id);
 
-      this->activate_surface(surface_id);
+      this->activate_surface(this->lookup_name(surface_id).value_or("unknown-name").c_str());
    }
 
    logdebug("Surface %u now on layer %u with rect { %d, %d, %d, %d }",
             surface_id, layer_id, x, y, w, h);
 }
 
-char const *App::activate_surface(uint32_t surface_id) {
+char const *App::activate_surface(char const *drawing_name) {
+   int surface_id = -1;
+
+   {
+      auto oid = this->lookup_id(drawing_name);
+      if (oid) {
+         surface_id = oid.value();
+      } else {
+         return "Surface does not exist";
+      }
+   }
+
    if (!this->controller->surface_exists(surface_id)) {
       return "Surface does not exist";
    }
@@ -400,7 +410,18 @@ char const *App::activate_surface(uint32_t surface_id) {
    return nullptr;
 }
 
-char const *App::deactivate_surface(uint32_t surface_id) {
+char const *App::deactivate_surface(char const *drawing_name) {
+   int surface_id = -1;
+
+   {
+      auto oid = this->lookup_id(drawing_name);
+      if (oid) {
+         surface_id = oid.value();
+      } else {
+         return "Surface does not exist";
+      }
+   }
+
    if (surface_id == this->layers.main_surface) {
       return "Cannot deactivate main_surface";
    }
@@ -426,9 +447,9 @@ char const *App::deactivate_surface(uint32_t surface_id) {
 
    if (! this->last_active.empty()) {
       // Should be active already, shouldn't it?
-      this->activate_surface(this->last_active.front());
+      this->activate_surface(this->lookup_name(this->last_active.front()).value_or("unknown-name").c_str());
    } else {
-      this->activate_surface(this->layers.main_surface);
+      this->activate_surface(this->layers.main_surface_name.c_str());
    }
 
    return nullptr;
@@ -500,7 +521,7 @@ result<int> App::request_surface(char const *drawing_name) {
       if (!this->layers.main_surface_name.empty() &&
           this->layers.main_surface_name == drawing_name) {
          this->layers.main_surface = id;
-         this->activate_surface(id);
+         this->activate_surface(drawing_name);
          logdebug("Set main_surface id to %u", id);
       }
 
@@ -511,36 +532,6 @@ result<int> App::request_surface(char const *drawing_name) {
    return Err<int>("Surface already present");
 }
 
-char const *App::activate_surface(char const *drawing_name) {
-   auto osid = this->id_alloc.lookup(drawing_name);
-
-   if (osid) {
-      logdebug("ativate surface with name %s and id %u", drawing_name,
-               osid.value());
-      auto ret = this->activate_surface(osid.value());
-      if (!ret) {
-         this->emit_activated(drawing_name);
-      }
-      return ret;
-   }
-
-   logerror("surface %s unknown", drawing_name);
-   return "Surface unknown";
-}
-
-char const *App::deactivate_surface(char const *drawing_name) {
-   auto osid = this->id_alloc.lookup(drawing_name);
-
-   if (osid) {
-      logdebug("deativate surface with name %s and id %u", drawing_name,
-               osid.value());
-      return this->deactivate_surface(osid.value());
-   }
-
-   logerror("surface %s unknown", drawing_name);
-   return "Surface unknown";
-}
-
 //  _     _           _ _                            _   _                 _
 // | |__ (_)_ __   __| (_)_ __   __ _     __ _ _ __ (_) (_)_ __ ___  _ __ | |
 // | '_ \| | '_ \ / _` | | '_ \ / _` |   / _` | '_ \| | | | '_ ` _ \| '_ \| |
@@ -582,7 +573,7 @@ binding_api::result_type binding_api::enddraw(char const* drawing_name) {
 
 binding_api::result_type binding_api::list_drawing_names() {
    logdebug("%s", __func__);
-   json j = this->app->id_alloc.names;
+   json j = this->app->id_alloc.name2id;
    return Ok(json_tokener_parse(j.dump().c_str()));
 }
 
@@ -612,22 +603,12 @@ binding_api::result_type binding_api::debug_terminate() {
    return Ok(json_object_new_object());
 }
 
-binding_api::result_type binding_api::demo_activate_surface(
-   uint32_t surfaceid) {
-   char const *e = this->app->activate_surface(surfaceid);
-   if (e != nullptr) {
-      return Err<json_object *>(e);
-   }
-   return Ok(json_object_new_object());
+binding_api::result_type binding_api::demo_activate_surface(uint32_t s) {
+   return Err<json_object *>("not implemented");
 }
 
 binding_api::result_type binding_api::demo_activate_all() {
-   for (auto &s : this->app->controller->surfaces) {
-      s.second->set_visibility(1);
-   }
-   this->app->controller->commit_changes();
-   this->app->display->flush();
-   return Ok(json_object_new_object());
+   return Err<json_object *>("not implemented");
 }
 
 //                  _             _ _            _                 _