update
[apps/onscreenapp.git] / app / 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 #include <QGuiApplication>
18 #include <QCommandLineParser>
19 #include <QtGui/QBitmap>
20 #include <QtGui/QGuiApplication>
21 #include <QtQml/QQmlApplicationEngine>
22 #include <QtQml/QQmlContext>
23 #include <QtQml/qqml.h>
24 #include <QQuickWindow>
25 #include <QtQuickControls2/QQuickStyle>
26
27 #include "eventhandler.h"
28
29 using namespace std;
30
31 static EventHandler* eventHandler;
32
33 int main(int argc, char *argv[])
34 {
35     QGuiApplication app(argc, argv);
36
37     QCoreApplication::setOrganizationDomain("LinuxFoundation");
38     QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
39     QCoreApplication::setApplicationName("Onscreenapp");
40     QCoreApplication::setApplicationVersion("0.1.0");
41
42     QQuickStyle::setStyle("AGL");
43
44     QCommandLineParser parser;
45     parser.addPositionalArgument("port", app.translate("main", "port for binding"));
46     parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
47     parser.addHelpOption();
48     parser.addVersionOption();
49     parser.process(app);
50     QStringList positionalArguments = parser.positionalArguments();
51
52     int port = 1700;
53     QString token = "wm";
54
55     if (positionalArguments.length() == 2) {
56         port = positionalArguments.takeFirst().toInt();
57         token = positionalArguments.takeFirst();
58     }
59
60     HMI_DEBUG(APP_ID, "port = %d, token = %s", port, token.toStdString().c_str());
61
62     eventHandler = new EventHandler();
63     eventHandler->init(port, token.toStdString().c_str());
64
65     QQmlApplicationEngine engine;
66     engine.rootContext()->setContextProperty("eventHandler", eventHandler);
67     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
68     if (engine.rootObjects().isEmpty()) {
69         HMI_DEBUG(APP_ID, "Fatal Error, rootObject is empty!");
70         return -1;
71     }
72
73     QObject *root = engine.rootObjects().first();
74     QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
75
76     eventHandler->setQuickWindow(window);
77     QObject::connect(eventHandler, SIGNAL(signalLoader(QVariant)), window, SLOT(qmlLoader(QVariant)));
78     QObject::connect(eventHandler, SIGNAL(signalOnScreenParameter(QVariant)), window, SLOT(qmlOnScreenParameter(QVariant)));
79
80     HMI_DEBUG(APP_ID, "Launched!");
81
82     return app.exec();
83 }
84