add source for ces2019 horizontal
[apps/onscreenapp.git] / app / eventhandler.cpp
diff --git a/app/eventhandler.cpp b/app/eventhandler.cpp
new file mode 100644 (file)
index 0000000..c07e215
--- /dev/null
@@ -0,0 +1,246 @@
+/*
+ * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "eventhandler.h"
+#include <functional>
+#include <QUrl>
+#include <QDebug>
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QQuickWindow>
+#include <QPixmap>
+#include <QBitmap>
+#include <QImage>
+
+void* EventHandler::myThis = 0;
+#define SCREEN_AREA "OnScreen"
+#define SCREEN_NAME "OnScreenApp"
+
+EventHandler::EventHandler(QObject *parent) :
+    QObject(parent),
+    mp_hs(NULL),
+    mp_wm(NULL),
+    mp_qw(NULL)
+{
+    m_isActive = false;
+    m_vecOSM.clear();
+}
+
+EventHandler::~EventHandler()
+{
+    if (mp_hs != NULL) {
+        delete mp_hs;
+    }
+    if (mp_wm != NULL) {
+        delete mp_wm;
+    }
+}
+
+void EventHandler::init(int port, const char *token)
+{
+    myThis = this;
+    mp_wm = new QLibWindowmanager();
+    mp_wm->init(port, token);
+
+    mp_hs = new LibHomeScreen();
+    mp_hs->init(port, token);
+
+    mp_hs->registerCallback(nullptr, EventHandler::onRep_static);
+    mp_hs->set_event_handler(LibHomeScreen::Event_OnScreenMessage, [this](json_object *object){
+        struct json_object *value;
+        json_object_object_get_ex(object, "display_message", &value);
+        const char *display_message = json_object_get_string(value);
+
+        HMI_DEBUG("onscreenapp", "display_message = %s", display_message);
+
+        QString data(display_message);
+        QJsonDocument doc = QJsonDocument::fromJson(data.toUtf8());
+        QJsonObject jsonObj(doc.object());
+
+        ON_SCREEN_MESSAGE osm;
+
+        if (jsonObj.contains("app")) {
+            HMI_DEBUG("onscreenapp", "json parse. app is %s", jsonObj["app"].toString().toStdString().c_str());
+            osm.app = jsonObj["app"].toString();
+        }
+
+        if (jsonObj.contains("message")) {
+            HMI_DEBUG("onscreenapp", "json parse. message is %s", jsonObj["message"].toString().toStdString().c_str());
+            osm.message = jsonObj["message"].toString();
+        }
+
+        if (jsonObj.contains("file")) {
+            HMI_DEBUG("onscreenapp", "json parse. file is %s", jsonObj["file"].toString().toStdString().c_str());
+            osm.file = jsonObj["file"].toString();
+
+            if (osm.file.left(1) == "/") {
+                osm.file = QUrl::fromLocalFile(osm.file).toString();
+            }
+        }
+
+        bool isNewApp = true;
+        for(QVector<ON_SCREEN_MESSAGE>::iterator it = m_vecOSM.begin(); it != m_vecOSM.end(); ++it) {
+            if (it->app == osm.app) {
+                it->message = osm.message;
+                it->file = osm.file;
+                isNewApp = false;
+                break;
+            }
+        }
+
+        if (isNewApp) {
+            m_vecOSM.append(osm);
+        }
+
+        if (osm.app == "Phone") {
+            if (osm.message == "incoming call") {
+                this->activateSurface(osm.app);
+                emit this->signalLoader(QVariant(osm.file));
+                emit this->signalSetClearBackgroud();
+            }
+            else if (osm.message == "call rejected") {
+                this->deactivateSurface(osm.app);
+            }
+        }
+        else if (osm.app == "System") {
+            if (osm.message == "error") {
+                this->activateSurface(osm.app);
+                emit this->signalLoader(QVariant(osm.file));
+                emit this->signalSetClearBackgroud();
+            }
+            else if (osm.message == "rejected") {
+                this->deactivateSurface(osm.app);
+            }
+        }
+        else {
+            if (osm.message == "onscreenapp:unload") {
+                this->deactivateSurface(osm.app);
+            }
+            else {
+                this->activateSurface(osm.app);
+                if (osm.file.isEmpty()) {
+                    emit this->signalSetDefaultBackgroud(QVariant(osm.message));
+                }
+                else {
+                    emit this->signalLoader(QVariant(osm.file));
+                    emit this->signalSetClearBackgroud();
+                }
+
+                emit this->signalOnScreenMessage(QVariant(osm.message));
+            }
+        }
+
+    });
+
+    if (mp_wm->requestSurface(SCREEN_NAME) != 0) {
+        HMI_DEBUG("onscreenapp", "!!!!LayoutHandler requestSurface Failed!!!!!");
+        exit(EXIT_FAILURE);
+    }
+
+    mp_wm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [this](json_object *object) {
+        HMI_DEBUG("onscreenapp", "Surface %s got syncDraw!", SCREEN_NAME);
+        this->mp_wm->endDraw(SCREEN_NAME);
+    });
+
+    mp_wm->set_event_handler(QLibWindowmanager::Event_Active, [this](json_object *object) {
+        struct json_object *value;
+        json_object_object_get_ex(object, "drawing_name", &value);
+        const char *name = json_object_get_string(value);
+
+        HMI_DEBUG("onscreenapp", "Event_Active kKeyDrawingName = %s", name);
+    });
+
+    mp_wm->set_event_handler(QLibWindowmanager::Event_Inactive, [this](json_object *object) {
+        struct json_object *value;
+        json_object_object_get_ex(object, "drawing_name", &value);
+        const char *name = json_object_get_string(value);
+
+        HMI_DEBUG("onscreenapp", "Event_Inactive kKeyDrawingName = %s", name);
+    });
+
+    HMI_DEBUG("onscreenapp", "LayoutHander::init() finished.");
+}
+
+void EventHandler::setQuickWindow(QQuickWindow *qw)
+{
+    mp_qw = qw;
+}
+
+void EventHandler::onRep_static(struct json_object* reply_contents)
+{
+    static_cast<EventHandler*>(EventHandler::myThis)->onRep(reply_contents);
+}
+void EventHandler::onRep(struct json_object* reply_contents)
+{
+    const char* str = json_object_to_json_string(reply_contents);
+    HMI_DEBUG("onscreenapp", "EventHandler::onReply %s", str);
+}
+
+void EventHandler::activateSurface(QString app)
+{
+    HMI_DEBUG("onscreenapp", "EventHandler::activateSurface()");
+    if (m_isActive == false) {
+        m_isActive = true;
+        mp_wm->activateSurface(SCREEN_NAME);
+    }
+
+    QString label = SCREEN_AREA + app;
+    mp_wm->activateSurface(label, SCREEN_AREA);
+}
+
+void EventHandler::deactivateSurface(QString app)
+{
+    HMI_DEBUG("onscreenapp", "EventHandler::deactivateSurface()");
+
+    for(QVector<ON_SCREEN_MESSAGE>::iterator it = m_vecOSM.begin(); it != m_vecOSM.end(); ++it) {
+        if (it->app == app) {
+            m_vecOSM.erase(it);
+            break;
+        }
+    }
+
+    if (m_vecOSM.length() > 0) {
+        ON_SCREEN_MESSAGE osm = m_vecOSM.last();
+
+        if (!osm.file.isEmpty()) {
+            emit this->signalLoader(QVariant(osm.file));
+            emit this->signalSetClearBackgroud();
+            emit this->signalOnScreenMessage(QVariant(osm.message));
+        }
+
+    } else {
+        // nothing on screen
+        emit this->signalLoader(QVariant(""));
+        mp_wm->deactivateSurface(SCREEN_NAME);
+        m_isActive = false;
+    }
+}
+
+void EventHandler::onScreenReply(const QString &message)
+{
+    HMI_DEBUG("onscreenapp", "EventHandler::onScreenReply()");
+    mp_hs->onScreenReply(message.toLatin1());
+}
+
+#if USE_TEST_DISPLAY
+void EventHandler::slotActivateSurface(){
+    // This is needed for first rendering when the app is launched
+    if(!m_isActive){
+        m_isActive = true;
+        this->activateSurface();
+    }
+}
+#endif