homescreenhandler: Handle output identification better
[apps/homescreen.git] / homescreen / src / homescreenhandler.cpp
index e61f0de..9353713 100644 (file)
@@ -11,6 +11,8 @@
 #include "homescreenhandler.h"
 #include "hmi-debug.h"
 
+QScreen *find_screen(const char *output);
+
 // defined by meson build file
 #include QT_QPA_HEADER
 
@@ -27,8 +29,6 @@ HomescreenHandler::HomescreenHandler(Shell *_aglShell, ApplicationLauncher *laun
 {
        mp_launcher = launcher;
        mp_applauncher_client = new AppLauncherClient();
-       QPlatformNativeInterface *native = qApp->platformNativeInterface();
-       m_output = getWlOutput(native, qApp->screens().first());
 
        //
        // The "started" event is received any time a start request is made to applaunchd,
@@ -93,13 +93,89 @@ void HomescreenHandler::addAppToStack(const QString& app_id)
 void HomescreenHandler::activateApp(const QString& app_id)
 {
        struct agl_shell *agl_shell = aglShell->shell.get();
+       QScreen *tmp_screen = qApp->screens().first();
+       QPlatformNativeInterface *native = qApp->platformNativeInterface();
+       struct wl_output *mm_output = nullptr;
+
+       if (!tmp_screen) {
+               HMI_DEBUG("HomeScreen", "No output found to activate on!\n");
+       } else {
+               mm_output = getWlOutput(native, tmp_screen);
+
+               HMI_DEBUG("HomeScreen", "Activating app_id %s by default on output %p\n",
+                               app_id.toStdString().c_str(), mm_output);
+       }
 
        if (mp_launcher) {
                mp_launcher->setCurrent(app_id);
        }
 
-       HMI_DEBUG("HomeScreen", "Activating application %s", app_id.toStdString().c_str());
-       agl_shell_activate_app(agl_shell, app_id.toStdString().c_str(), m_output);
+       // search for a pending application which might have a different output
+       auto iter = pending_app_list.begin();
+       bool found_pending_app = false;
+       while (iter != pending_app_list.end()) {
+               const QString &app_to_search = iter->first;
+
+               if (app_to_search == app_id) {
+                       found_pending_app = true;
+                       HMI_DEBUG("HomeScreen", "Found app_id %s in pending list  of applications",
+                                       app_id.toStdString().c_str());
+                       break;
+               }
+
+               iter++;
+       }
+
+       if (found_pending_app) {
+               const QString &output_name = iter->second;
+               QScreen *screen =
+                       ::find_screen(output_name.toStdString().c_str());
+
+               if (!screen) {
+                       HMI_DEBUG("HomeScreen", "Can't activate application %s on another "
+                                 "output, because output %s could not be found. "
+                                 "Trying with remoting ones.",
+                                 app_id.toStdString().c_str(),
+                                 output_name.toStdString().c_str());
+
+                       // try with remoting-remote-X which is the streaming
+                       // one
+                       std::string new_remote_output = 
+                               "remoting-" + output_name.toStdString();
+
+                       screen = ::find_screen(new_remote_output.c_str());
+                       if (!screen) {
+                               HMI_DEBUG("HomeScreen", "Can't activate application %s on another "
+                                         "output, because output remoting-%s could not be found",
+                                         app_id.toStdString().c_str(),
+                                         output_name.toStdString().c_str());
+                               return;
+                       }
+
+                       HMI_DEBUG("HomeScreen", "Found a stream remoting output %s to activate application %s on",
+                                 new_remote_output.c_str(),
+                                 app_id.toStdString().c_str());
+               }
+
+               mm_output = getWlOutput(native, screen);
+               pending_app_list.erase(iter);
+
+               HMI_DEBUG("HomeScreen", "For application %s found another "
+                               "output to activate %s\n",
+                               app_id.toStdString().c_str(),
+                               output_name.toStdString().c_str());
+       }
+
+       if (!mm_output) {
+               HMI_DEBUG("HomeScreen", "No suitable output found for activating %s",
+                               app_id.toStdString().c_str());
+               return;
+       }
+
+       HMI_DEBUG("HomeScreen", "Activating application %s",
+                       app_id.toStdString().c_str());
+
+       agl_shell_activate_app(agl_shell, app_id.toStdString().c_str(), mm_output);
 }
 
 void HomescreenHandler::deactivateApp(const QString& app_id)
@@ -121,5 +197,7 @@ void HomescreenHandler::processAppStatusEvent(const QString &app_id, const QStri
        } else if (status == "terminated") {
                HMI_DEBUG("HomeScreen", "Application %s terminated, activating last app", app_id.toStdString().c_str());
                deactivateApp(app_id);
+       } else if (status == "deactivated") {
+               HMI_DEBUG("HomeScreen", "Application %s deactivated, activating last app", app_id.toStdString().c_str());
        }
 }