Add onscreenapp
[apps/onscreenapp.git] / sample / app / eventhandler.cpp
1 /*
2  * Copyright (c) 2019 TOYOTA MOTOR CORPORATION
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <functional>
18 #include <QUrl>
19 #include <QJsonDocument>
20 #include <QJsonObject>
21 #include <QQuickWindow>
22 //#include <QtQml/QQmlContext>
23 #include <QQmlContext>
24 #include <QtQml/QQmlApplicationEngine>
25 #include "eventhandler.h"
26
27 void* EventHandler::myThis = 0;
28
29 const char _drawing_name[] = "drawing_name";
30
31 EventHandler::EventHandler(QObject *parent) :
32     QObject(parent),
33     mp_hs(NULL),
34     mp_wm(NULL),
35     mp_qw(NULL)
36 {
37
38 }
39
40 EventHandler::~EventHandler()
41 {
42     if (mp_hs != NULL) {
43         delete mp_hs;
44     }
45     if (mp_wm != NULL) {
46         delete mp_wm;
47     }
48 }
49
50 void EventHandler::init(int port, const char *token)
51 {
52     myThis = this;
53     mp_wm = new QLibWindowmanager();
54     mp_wm->init(port, token);
55
56     mp_hs = new QLibHomeScreen();
57     mp_hs->init(port, token);
58
59     mp_hs->set_event_handler(QLibHomeScreen::Event_ShowWindow, [this](json_object *object){
60         this->mp_wm->activateWindow(ROLE_NAME, "normal");
61         HMI_DEBUG(APP_ID, "received showWindow event, end!, line=%d", __LINE__);
62     });
63
64     mp_hs->set_event_handler(QLibHomeScreen::Event_ReplyShowWindow, [this](json_object *object){
65         HMI_DEBUG(APP_ID, "got Event_ReplyShowWindow!\n");
66         const char* msg = json_object_to_json_string(object);
67         emit this->signalOnReplyShowWindow(msg);
68     });
69
70     if (mp_wm->requestSurface(ROLE_NAME) != 0) {
71         HMI_DEBUG(APP_ID, "!!!!LayoutHandler requestSurface Failed!!!!!");
72         exit(EXIT_FAILURE);
73     }
74
75     // Create an event callback against an event type. Here a lambda is called when SyncDraw event occurs
76     mp_wm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [this](json_object *object) {
77         HMI_DEBUG(APP_ID, "Surface got syncDraw!");
78         this->mp_wm->endDraw(ROLE_NAME);
79     });
80
81     mp_wm->set_event_handler(QLibWindowmanager::Event_Visible, [this](json_object *object) {
82         struct json_object *value;
83         json_object_object_get_ex(object, _drawing_name, &value);
84         const char *name = json_object_get_string(value);
85
86         HMI_DEBUG(APP_ID, "Event_Active kKeyDrawingName = %s", name);
87     });
88
89     mp_wm->set_event_handler(QLibWindowmanager::Event_Invisible, [this](json_object *object) {
90         struct json_object *value;
91         json_object_object_get_ex(object, _drawing_name, &value);
92         const char *name = json_object_get_string(value);
93
94         HMI_DEBUG(APP_ID, "Event_Inactive kKeyDrawingName = %s", name);
95     });
96
97     HMI_DEBUG(APP_ID, "LayoutHander::init() finished.");
98 }
99
100 void EventHandler::setQuickWindow(QQuickWindow *qw)
101 {
102     mp_qw = qw;
103     QObject::connect(mp_qw, SIGNAL(frameSwapped()), mp_wm, SLOT(slotActivateSurface()));
104 }
105
106 void EventHandler::showWindow(QString id, QString json)
107 {
108     if(json.isNull())
109         mp_hs->tapShortcut(id);
110     else
111         mp_hs->showWindow(id.toStdString().c_str(), json_tokener_parse(json.toStdString().c_str()));
112 }
113
114 void EventHandler::hideWindow(QString id)
115 {
116     mp_hs->hideWindow(id.toStdString().c_str());
117 }