Modify function argument from char to json
[src/libhomescreen.git] / sample / template / main.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
18 #include <QtCore/QDebug>
19 #include <QtCore/QCommandLineParser>
20 #include <QtCore/QUrlQuery>
21 #include <QtCore/QSettings>
22 #include <QtGui/QGuiApplication>
23 #include <QtQml/QQmlApplicationEngine>
24 #include <QtQml/QQmlContext>
25 #include <QtQuickControls2/QQuickStyle>
26 #include <QQuickWindow>
27
28 #include <libhomescreen/libhomescreen.hpp>
29 #include "wmhandler.h"
30 #include "smhandler.h"
31
32
33 static LibHomeScreen*     hs;
34 static LibWindowmanager*  wm;
35 static LibSMWrapper*      smw;
36 static WmHandler*         wmh;
37
38 static std::string myname = std::string("Templete");
39
40
41 static void onRep(struct json_object* reply_contents);
42 static void onEv(const std::string& event, struct json_object* event_contents);
43
44
45 int main(int argc, char *argv[])
46 {
47     QGuiApplication       app(argc, argv);
48     QQmlApplicationEngine engine;
49     QQmlContext*          context = engine.rootContext();
50     QObject*              root;
51     QQuickWindow*         window;
52
53     QQuickStyle::setStyle("AGL");
54
55     /*
56      * Set argument and option
57      */
58     QCommandLineParser parser;
59     parser.addPositionalArgument("port", app.translate("main", "port for binding"));
60     parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
61     parser.addHelpOption();
62     parser.addVersionOption();
63     parser.process(app);
64     QStringList positionalArguments = parser.positionalArguments();
65
66     if (positionalArguments.length() == 2) {
67         /*
68          * Get argument
69          */
70         int port = positionalArguments.takeFirst().toInt();
71         QString secret = positionalArguments.takeFirst();
72         std::string token = secret.toStdString();
73
74
75         /*
76          * Get instance
77          */
78         hs = new LibHomeScreen();
79         wm = new LibWindowmanager();
80         smw = new LibSMWrapper(port, secret);
81         wmh = new WmHandler();
82
83
84         /*
85          * Set WindowManager
86          */
87         // Initialize
88         if(wm->init(port, token.c_str()) != 0){
89             exit(EXIT_FAILURE);
90         }
91
92         // Application should call requestSurface at first
93         json_object *obj = json_object_new_object();
94         json_object_object_add(obj, wm->kKeyDrawingName, json_object_new_string(app_name.c_str()));
95         if (wm->requestSurface(obj) != 0) {
96             exit(EXIT_FAILURE);
97         }
98
99         // Set event handlers for each event
100         wm->set_event_handler(LibWindowmanager::Event_Active, [wm](json_object *object) {
101             const char *label = json_object_get_string(
102                 json_object_object_get(object, wm->kKeyDrawingName));
103             fprintf(stderr, "Surface %s got activated!\n", label);
104         });
105         wm->set_event_handler(LibWindowmanager::Event_Inactive, [wm](json_object *object) {
106             const char *label = json_object_get_string(
107                 json_object_object_get(object, wm->kKeyDrawingName));
108             fprintf(stderr, "Surface %s got deactivated!\n", label);
109         });
110         wm->set_event_handler(LibWindowmanager::Event_Visible, [wm](json_object *object) {
111             const char *label = json_object_get_string(
112                 json_object_object_get(object, wm->kKeyDrawingName));
113             fprintf(stderr, "Surface %s got visible!\n", label);
114         });
115         wm->set_event_handler(LibWindowmanager::Event_Invisible, [wm](json_object *object) {
116             const char *label = json_object_get_string(
117                 json_object_object_get(object, wm->kKeyDrawingName));
118             fprintf(stderr, "Surface %s got invisible!\n", label);
119         });
120         wm->set_event_handler(LibWindowmanager::Event_SyncDraw, [wm](json_object *object) {
121             const char *label = json_object_get_string(
122                 json_object_object_get(object, wm->kKeyDrawingName));
123             const char *area = json_object_get_string(
124                 json_object_object_get(object, wm->kKeyDrawingArea));
125                 fprintf(stderr, "Surface %s got syncDraw!\n", label);
126             // Application should call LibWindowmanager::endDraw() in SyncDraw handler
127             json_object *obj = json_object_new_object();
128             json_object_object_add(obj, wm->kKeyDrawingName, json_object_new_string(app_name.c_str()));
129             wm->endDraw(obj);
130         });
131         wm->set_event_handler(LibWindowmanager::Event_FlushDraw, [wm](json_object *object) {
132             const char *label = json_object_get_string(
133                 json_object_object_get(object, wm->kKeyDrawingName));
134             fprintf(stderr, "Surface %s got flushDraw!\n", label);
135         });
136
137         // Initialize WmHandler
138         wmh->init(wm, myname.c_str());
139
140
141         /*
142          * Set HomeScreen
143          */
144         // Initialize
145         hs->init(port, token.c_str());
146
147         // Set event handler
148         hs->set_event_handler(LibHomeScreen::Event_TapShortcut, [wm](json_object *object) {
149             const char *appname = json_object_get_string(
150                             json_object_object_get(object, "application_name"));
151             if(myname == appname) {
152                 qDebug("Surface %s got tapShortcut\n", appname);
153                 // Application should call LibWindowmanager::endDraw() in TapShortcut handler
154                                 json_object *obj = json_object_new_object();
155                                 json_object_object_add(obj, wm->kKeyDrawingName, json_object_new_string(app_name.c_str()));
156                                 json_object_object_add(obj, wm->kKeyDrawingArea, json_object_new_string("normal.full"));
157                 wm->activateSurface(obj);
158             }
159         });
160
161         /*
162          * Set SoundManager
163          */
164         smw->wrapper_registerCallback(onEv, onRep);
165         smw->subscribe(QString("newMainConnection"));
166         smw->subscribe(QString("mainConnectionStateChanged"));
167         smw->subscribe(QString("removedMainConnection"));
168         smw->subscribe(QString("asyncSetSourceState"));
169         smw->subscribe(QString("asyncConnect"));
170
171         // Set context property for SoundManager
172         context->setContextProperty("smw", smw);
173
174
175         /*
176          * Load qml
177          */
178         engine.load(QUrl(QStringLiteral("qrc:/QmlForThisApp.qml")));
179
180
181         /*
182          * Set slot for WindowManager and SoundManager
183          */
184         root = engine.rootObjects().first();
185         window = qobject_cast<QQuickWindow *>(root);
186
187         // Set slot for calling LibWindowmanager::activateSurface() when loading qml have completed
188         QObject::connect(window, SIGNAL(frameSwapped()),
189             wmh, SLOT(slotActivateSurface()));
190
191         // Set slot for SoundManager
192         QObject::connect(smw, SIGNAL(smEvent(QVariant, QVariant)),
193             root, SLOT(slotEvent(QVariant, QVariant)));
194         QObject::connect(smw, SIGNAL(smReply(QVariant)),
195             root, SLOT(slotReply(QVariant)));
196     }
197
198     return app.exec();
199 }
200
201 static void onRep(struct json_object* reply_contents)
202 {
203     qDebug("%s is called", __FUNCTION__);
204     QString str = QString(json_object_get_string(reply_contents));
205     QJsonParseError error;
206     QJsonDocument jdoc = QJsonDocument::fromJson(str.toUtf8(), &error);
207     QJsonObject jobj = jdoc.object();
208
209     smw->emit_reply(jobj);
210     json_object_put(reply_contents);
211 }
212
213 static void onEv(const std::string& event, struct json_object* event_contents)
214 {
215     qDebug("%s is called", __FUNCTION__);
216     const QString event_name = QString(event.c_str());
217     QString str = QString(json_object_get_string(event_contents));
218     QJsonParseError error;
219     QJsonDocument jdoc = QJsonDocument::fromJson(str.toUtf8(), &error);
220     const QJsonObject jobj = jdoc.object();
221     smw->emit_event(event_name, jobj);
222
223     json_object_put(event_contents);
224 }
225