2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
28 #include <libhomescreen/libhomescreen.hpp>
29 #include "wmhandler.h"
30 #include "smhandler.h"
33 static LibHomeScreen* hs;
34 static LibWindowmanager* wm;
35 static LibSMWrapper* smw;
36 static WmHandler* wmh;
38 static std::string myname = std::string("Templete");
41 static void onRep(struct json_object* reply_contents);
42 static void onEv(const std::string& event, struct json_object* event_contents);
45 int main(int argc, char *argv[])
47 QGuiApplication app(argc, argv);
48 QQmlApplicationEngine engine;
49 QQmlContext* context = engine.rootContext();
53 QQuickStyle::setStyle("AGL");
56 * Set argument and option
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();
64 QStringList positionalArguments = parser.positionalArguments();
66 if (positionalArguments.length() == 2) {
70 int port = positionalArguments.takeFirst().toInt();
71 QString secret = positionalArguments.takeFirst();
72 std::string token = secret.toStdString();
78 hs = new LibHomeScreen();
79 wm = new LibWindowmanager();
80 smw = new LibSMWrapper(port, secret);
81 wmh = new WmHandler();
88 if(wm->init(port, token.c_str()) != 0){
92 // Application should call requestSurface at first
93 if (wm->requestSurface(myname.c_str()) != 0) {
97 // Set event handlers for each event
98 wm->set_event_handler(LibWindowmanager::Event_Active, [wm](json_object *object) {
99 const char *label = json_object_get_string(
100 json_object_object_get(object, wm->kKeyDrawingName));
101 fprintf(stderr, "Surface %s got activated!\n", label);
103 wm->set_event_handler(LibWindowmanager::Event_Inactive, [wm](json_object *object) {
104 const char *label = json_object_get_string(
105 json_object_object_get(object, wm->kKeyDrawingName));
106 fprintf(stderr, "Surface %s got deactivated!\n", label);
108 wm->set_event_handler(LibWindowmanager::Event_Visible, [wm](json_object *object) {
109 const char *label = json_object_get_string(
110 json_object_object_get(object, wm->kKeyDrawingName));
111 fprintf(stderr, "Surface %s got visible!\n", label);
113 wm->set_event_handler(LibWindowmanager::Event_Invisible, [wm](json_object *object) {
114 const char *label = json_object_get_string(
115 json_object_object_get(object, wm->kKeyDrawingName));
116 fprintf(stderr, "Surface %s got invisible!\n", label);
118 wm->set_event_handler(LibWindowmanager::Event_SyncDraw, [wm](json_object *object) {
119 const char *label = json_object_get_string(
120 json_object_object_get(object, wm->kKeyDrawingName));
121 const char *area = json_object_get_string(
122 json_object_object_get(object, wm->kKeyDrawingArea));
123 fprintf(stderr, "Surface %s got syncDraw! area: %s.\n", label, area);
124 // Application should call LibWindowmanager::endDraw() in SyncDraw handler
125 wm->endDraw(app_name.c_str());
127 wm->set_event_handler(LibWindowmanager::Event_FlushDraw, [wm](json_object *object) {
128 const char *label = json_object_get_string(
129 json_object_object_get(object, wm->kKeyDrawingName));
130 fprintf(stderr, "Surface %s got flushDraw!\n", label);
133 // Initialize WmHandler
134 wmh->init(wm, myname.c_str());
141 hs->init(port, token.c_str());
144 hs->set_event_handler(LibHomeScreen::Event_ShowWindow, [hs, wm](json_object *object) {
145 qDebug("Surface %s got showWindow\n", myname.c_str());
146 struct json_object *param_obj = json_object_object_get(object, hs->_keyParameter);
147 const char *area = json_object_get_string(
148 json_object_object_get(param_obj, hs->_keyArea));
149 // Application should call LibWindowmanager::activateWindow() in showWindow handler
151 wm->activateWindow(myname.c_str(), hs->_areaNormal);
153 wm->activateWindow(myname.c_str(), area);
159 smw->wrapper_registerCallback(onEv, onRep);
160 smw->subscribe(QString("newMainConnection"));
161 smw->subscribe(QString("mainConnectionStateChanged"));
162 smw->subscribe(QString("removedMainConnection"));
163 smw->subscribe(QString("asyncSetSourceState"));
164 smw->subscribe(QString("asyncConnect"));
166 // Set context property for SoundManager
167 context->setContextProperty("smw", smw);
173 engine.load(QUrl(QStringLiteral("qrc:/QmlForThisApp.qml")));
177 * Set slot for WindowManager and SoundManager
179 root = engine.rootObjects().first();
180 window = qobject_cast<QQuickWindow *>(root);
182 // Set slot for calling LibWindowmanager::activateSurface() when loading qml have completed
183 QObject::connect(window, SIGNAL(frameSwapped()),
184 wmh, SLOT(slotActivateSurface()));
186 // Set slot for SoundManager
187 QObject::connect(smw, SIGNAL(smEvent(QVariant, QVariant)),
188 root, SLOT(slotEvent(QVariant, QVariant)));
189 QObject::connect(smw, SIGNAL(smReply(QVariant)),
190 root, SLOT(slotReply(QVariant)));
196 static void onRep(struct json_object* reply_contents)
198 qDebug("%s is called", __FUNCTION__);
199 QString str = QString(json_object_get_string(reply_contents));
200 QJsonParseError error;
201 QJsonDocument jdoc = QJsonDocument::fromJson(str.toUtf8(), &error);
202 QJsonObject jobj = jdoc.object();
204 smw->emit_reply(jobj);
205 json_object_put(reply_contents);
208 static void onEv(const std::string& event, struct json_object* event_contents)
210 qDebug("%s is called", __FUNCTION__);
211 const QString event_name = QString(event.c_str());
212 QString str = QString(json_object_get_string(event_contents));
213 QJsonParseError error;
214 QJsonDocument jdoc = QJsonDocument::fromJson(str.toUtf8(), &error);
215 const QJsonObject jobj = jdoc.object();
216 smw->emit_event(event_name, jobj);
218 json_object_put(event_contents);