b88bda48b5d77c277ab9f908bbc41a4d34c9488f
[apps/onscreenapp.git] / sample / app / eventhandler.cpp
1 /*
2  * Copyright (c) 2017 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 "eventhandler.h"
24
25 void* EventHandler::myThis = 0;
26
27 EventHandler::EventHandler(QObject *parent) :
28     QObject(parent),
29     mp_hs(NULL),
30     mp_wm(NULL),
31     mp_qw(NULL)
32 {
33
34 }
35
36 EventHandler::~EventHandler()
37 {
38     if (mp_hs != NULL) {
39         delete mp_hs;
40     }
41     if (mp_wm != NULL) {
42         delete mp_wm;
43     }
44 }
45
46 void EventHandler::init(int port, const char *token)
47 {
48     myThis = this;
49     mp_wm = new QLibWindowmanager();
50     mp_wm->init(port, token);
51
52     mp_hs = new QLibHomeScreen();
53     mp_hs->init(port, token);
54
55     mp_hs->set_event_handler(QLibHomeScreen::Event_TapShortcut, [this](json_object *object){
56         this->mp_wm->activateWindow(ROLE_NAME);
57         HMI_DEBUG(APP_ID, "received showWindow event, end!, line=%d", __LINE__);
58     });
59
60     mp_hs->set_event_handler(QLibHomeScreen::Event_ReplyShowWindow, [this](json_object *object){
61         HMI_DEBUG(APP_ID, "got Event_ReplyShowWindow!\n");
62         const char* msg = json_object_to_json_string(object);
63         emit this->signalOnReplyShowWindow(msg);
64     });
65
66     if (mp_wm->requestSurface(ROLE_NAME) != 0) {
67         HMI_DEBUG(APP_ID, "!!!!LayoutHandler requestSurface Failed!!!!!");
68         exit(EXIT_FAILURE);
69     }
70
71     // Create an event callback against an event type. Here a lambda is called when SyncDraw event occurs
72     mp_wm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [this](json_object *object) {
73         HMI_DEBUG(APP_ID, "Surface got syncDraw!");
74         this->mp_wm->endDraw(ROLE_NAME);
75     });
76
77     mp_wm->set_event_handler(QLibWindowmanager::Event_Visible, [this](json_object *object) {
78         struct json_object *value;
79         json_object_object_get_ex(object, "drawing_name", &value);
80         const char *name = json_object_get_string(value);
81
82         HMI_DEBUG(APP_ID, "Event_Active kKeyDrawingName = %s", name);
83     });
84
85     mp_wm->set_event_handler(QLibWindowmanager::Event_Invisible, [this](json_object *object) {
86         struct json_object *value;
87         json_object_object_get_ex(object, "drawing_name", &value);
88         const char *name = json_object_get_string(value);
89
90         HMI_DEBUG(APP_ID, "Event_Inactive kKeyDrawingName = %s", name);
91     });
92
93     HMI_DEBUG(APP_ID, "LayoutHander::init() finished.");
94 }
95
96 void EventHandler::setQuickWindow(QQuickWindow *qw)
97 {
98     mp_qw = qw;
99     QObject::connect(mp_qw, SIGNAL(frameSwapped()), mp_wm, SLOT(slotActivateSurface()));
100 }
101
102 void EventHandler::showWindow(QString id, QString json)
103 {
104     if(json.isNull())
105         mp_hs->tapShortcut(id);
106     else
107         mp_hs->showWindow(id.toStdString().c_str(), json_tokener_parse(json.toStdString().c_str()));
108 }