Merge "homescreen/homescreen.pro: Bump protocol requirements"
[apps/homescreen.git] / homescreen / src / homescreenhandler.cpp
index bf98a53..d5e46fa 100644 (file)
 
 #include <qpa/qplatformnativeinterface.h>
 
+#define APPLAUNCH_DBUS_IFACE     "org.automotivelinux.AppLaunch"
+#define APPLAUNCH_DBUS_OBJECT    "/org/automotivelinux/AppLaunch"
+
 void* HomescreenHandler::myThis = 0;
 
-HomescreenHandler::HomescreenHandler(Shell *_aglShell, QObject *parent) :
+HomescreenHandler::HomescreenHandler(Shell *_aglShell, ApplicationLauncher *launcher, QObject *parent) :
     QObject(parent),
     aglShell(_aglShell)
 {
-
+    mp_launcher = launcher;
+    applaunch_iface = new org::automotivelinux::AppLaunch(APPLAUNCH_DBUS_IFACE, APPLAUNCH_DBUS_OBJECT,
+                                                          QDBusConnection::sessionBus(), this);
 }
 
 HomescreenHandler::~HomescreenHandler()
 {
+#if 0
     if (mp_hs != NULL) {
         delete mp_hs;
     }
+#endif
 }
 
-void HomescreenHandler::init(int port, const char *token)
+void HomescreenHandler::init(void)
 {
+#if 0
     mp_hs = new LibHomeScreen();
     mp_hs->init(port, token);
-
+#endif
     myThis = this;
 
+    /*
+     * The "started" signal is received any time a start request is made to applaunchd,
+     * and the application either starts successfully or is already running. This
+     * effectively acts as a "switch to app X" action.
+     */
+    connect(applaunch_iface, SIGNAL(started(QString)), this, SLOT(appStarted(QString)));
+    connect(applaunch_iface, SIGNAL(terminated(QString)), this, SLOT(appTerminated(QString)));
 
+#if 0
     mp_hs->registerCallback(nullptr, HomescreenHandler::onRep_static);
 
     mp_hs->set_event_handler(LibHomeScreen::Event_OnScreenMessage, [this](json_object *object){
@@ -83,6 +99,7 @@ void HomescreenHandler::init(int port, const char *token)
 
        emit showInformation(QString(QLatin1String(info)));
     });
+#endif
 }
 
 static struct wl_output *
@@ -95,7 +112,7 @@ getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
 void HomescreenHandler::tapShortcut(QString application_id)
 {
        HMI_DEBUG("HomeScreen","tapShortcut %s", application_id.toStdString().c_str());
-
+#if 0
        struct json_object* j_json = json_object_new_object();
        struct json_object* value;
 
@@ -113,8 +130,16 @@ void HomescreenHandler::tapShortcut(QString application_id)
        // the first time. Later calls to HomescreenHandler::tapShortcut will
        // require calling 'agl_shell_activate_app'
        agl_shell_activate_app(agl_shell, application_id.toStdString().c_str(), output);
+
+#endif
+
+    if (mp_launcher) {
+        mp_launcher->setCurrent(application_id);
+    }
+    appStarted(application_id);
 }
 
+#if 0
 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
 {
     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
@@ -144,3 +169,20 @@ void HomescreenHandler::onEv(const string& event, struct json_object* event_cont
         HMI_DEBUG("HomeScreen","display_message = %s", display_message);
     }
 }
+#endif
+
+void HomescreenHandler::appStarted(const QString& application_id)
+{
+    struct agl_shell *agl_shell = aglShell->shell.get();
+    QPlatformNativeInterface *native = qApp->platformNativeInterface();
+    struct wl_output *output = getWlOutput(native, qApp->screens().first());
+
+    HMI_DEBUG("HomeScreen", "Activating application %s", application_id.toStdString().c_str());
+    agl_shell_activate_app(agl_shell, application_id.toStdString().c_str(), output);
+}
+
+void HomescreenHandler::appTerminated(const QString& application_id)
+{
+    HMI_DEBUG("HomeScreen", "Application %s terminated, activating launcher", application_id.toStdString().c_str());
+    appStarted("launcher");
+}