add script wm-request
[staging/windowmanager.git] / src / app.cpp
index e14984e..6a7fc69 100644 (file)
  * limitations under the License.
  */
 
-//
-// Created by mfritzsc on 7/11/17.
-//
-
 #include "app.hpp"
 #include "json_helper.hpp"
 #include "layers.hpp"
@@ -139,7 +135,7 @@ App::App(wl::display *d)
    try {
       {
          auto l = load_layer_map(
-                 this->config.get_string("layers.json").value().c_str());
+            this->config.get_string("layers.json").value().c_str());
          if (l.is_ok()) {
             this->layers = l.unwrap();
          } else {
@@ -148,8 +144,8 @@ App::App(wl::display *d)
       }
 
       {
-         auto l = load_layout(
-                 this->config.get_string("layout.json").value().c_str());
+         auto l =
+            load_layout(this->config.get_string("layout.json").value().c_str());
          if (l.is_ok()) {
             this->layouts = l.unwrap();
          } else {
@@ -213,8 +209,7 @@ int App::dispatch_events() {
    this->display->flush();
 
    // execute pending tasks, that is layout changes etc.
-   this->controller->execute_pending();
-   this->display->roundtrip();
+   this->execute_pending();
 
    return 0;
 }
@@ -308,11 +303,11 @@ void App::surface_set_layout(uint32_t surface_id) {
 
    // configure surface to wxh dimensions
    s->set_configuration(w, h);
-   // set source rect to "entire surface"
-   s->set_source_rectangle(0, 0, w, h);
    // set destination to the display rectangle
    s->set_destination_rectangle(x, y, w, h);
 
+   // XXX: visibility should be determined independently of our
+   //      layer + geometry setup.
    s->set_visibility(1);
    this->controller->layers[layer_id]->add_surface(s.get());
 
@@ -320,6 +315,52 @@ void App::surface_set_layout(uint32_t surface_id) {
             surface_id, layer_id, x, y, w, h);
 }
 
+char const *App::activate_surface(uint32_t surface_id) {
+   if (!this->controller->surface_exists(surface_id)) {
+      return "Surface does not exist";
+   }
+
+   // This shouild involve a policy check, but as we do not (yet) have
+   // such a thing, we will just switch to this surface.
+   // XXX: input focus missing!!1
+
+   // Make it visible, no (or little effect) if already visible
+   auto &s = this->controller->surfaces[surface_id];
+
+   // Set all others invisible
+   for (auto &i : this->controller->surfaces) {
+      auto &si = this->controller->sprops[i.second->id];
+      if (si.visibility == 1 && si.id != s->id &&
+          int(si.id) != this->layers.main_surface) {
+         i.second->set_visibility(0);
+      }
+   }
+   s->set_visibility(1);
+
+   // commit changes
+   this->controller->commit_changes();
+   this->display->flush();
+
+   // no error
+   return nullptr;
+}
+
+void App::add_task(char const *name, std::function<void()> &&f) {
+   this->pending.emplace_back(std::make_pair(name, f));
+}
+
+void App::execute_pending() {
+   if (!this->pending.empty()) {
+      for (auto &t : this->pending) {
+         logdebug("executing task '%s'", t.first);
+         t.second();
+      }
+      this->pending.clear();
+      this->controller->commit_changes();
+      this->display->flush();
+   }
+}
+
 //                      _          _   _____                 _
 //  _ __  _ __ _____  _(_) ___  __| | | ____|_   _____ _ __ | |_ ___
 // | '_ \| '__/ _ \ \/ / |/ _ \/ _` | |  _| \ \ / / _ \ '_ \| __/ __|
@@ -330,11 +371,8 @@ void App::surface_created(uint32_t surface_id) {
    DB("surface_id is " << surface_id);
 
    // We need to execute the surface setup after its creation.
-   // XXX: perhaps move the late-tasks functionality to App?
-   this->controller->add_task("surface_set_layout",
-                              [surface_id, this](struct genivi::controller *) {
-                                 this->surface_set_layout(surface_id);
-                              });
+   this->add_task("surface_set_layout",
+                  [surface_id, this] { this->surface_set_layout(surface_id); });
 }
 
 void App::surface_removed(uint32_t surface_id) {
@@ -387,6 +425,24 @@ 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) {
+      return Err<json_object *>(e);
+   }
+   return Ok(json_object_new_object());
+}
+
+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());
+}
+
 //                  _             _ _            _                 _
 //   ___ ___  _ __ | |_ _ __ ___ | | | ___ _ __ | |__   ___   ___ | | _____
 //  / __/ _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|| '_ \ / _ \ / _ \| |/ / __|
@@ -401,4 +457,8 @@ void controller_hooks::surface_removed(uint32_t surface_id) {
    this->app->surface_removed(surface_id);
 }
 
+void controller_hooks::add_task(char const *name, std::function<void()> &&f) {
+   this->app->add_task(name, std::move(f));
+}
+
 }  // namespace wm