+char const *App::api_deactivate_surface(char const *drawing_name) {
+ ST();
+ auto const &surface_id = this->lookup_id(drawing_name);
+
+ if (!surface_id) {
+ return "Surface does not exist";
+ }
+
+ if (*surface_id == this->layers.main_surface) {
+ return "Cannot deactivate main_surface";
+ }
+
+ auto o_state = *this->layers.get_layout_state(*surface_id);
+
+ if (!o_state) {
+ return "Could not find layer for surface";
+ }
+
+ struct LayoutState &state = *o_state;
+
+ if (state.main == -1) {
+ return "No surface active";
+ }
+
+ if (*surface_id == this->layers.main_surface) {
+ logdebug("Refusing to deactivate main_surface %d", *surface_id);
+ return nullptr;
+ }
+
+ if (state.main == *surface_id) {
+ if (state.sub != -1) {
+ std::string sub = std::move(*this->lookup_name(state.sub));
+ this->emit_syncdraw(sub.c_str());
+
+ this->deactivate(*surface_id);
+ this->surface_set_layout(state.sub);
+ state.main = state.sub;
+ state.sub = -1;
+ state.s = LayoutState::Single;
+
+ this->layout_commit();
+ this->enqueue_flushdraw(state.sub);
+ } else {
+ this->deactivate(*surface_id);
+ state.main = -1;
+ }
+ } else if (state.sub == *surface_id) {
+ std::string main = std::move(*this->lookup_name(state.main));
+ this->emit_syncdraw(main.c_str());
+
+ this->deactivate(*surface_id);
+ this->deactivate(*surface_id);
+ this->surface_set_layout(state.main);
+ state.sub = -1;
+ state.s = LayoutState::Single;
+
+ this->layout_commit();
+ this->enqueue_flushdraw(state.main);
+ } else {
+ return "Surface is not active";
+ }
+
+ return nullptr;
+}
+
+void App::enqueue_flushdraw(int surface_id) {
+ this->check_flushdraw(surface_id);
+ logdebug("Enqueuing EndDraw for surface_id %d", surface_id);
+ this->pending_end_draw.push_back(surface_id);