homescreen: remove unused DBus code artifacts
[apps/homescreen.git] / homescreen / src / main.cpp
1 /*
2  * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
3  * Copyright (c) 2017, 2018 TOYOTA MOTOR CORPORATION
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <QGuiApplication>
19 #include <QCommandLineParser>
20 #include <QtCore/QUrlQuery>
21 #include <QtGui/QGuiApplication>
22 #include <QtQml/QQmlApplicationEngine>
23 #include <QtQml/QQmlContext>
24 #include <QtQml/qqml.h>
25 #include <QQuickWindow>
26
27 #include <qlibwindowmanager.h>
28 #include <weather.h>
29 #include <bluetooth.h>
30 #include "applicationlauncher.h"
31 #include "statusbarmodel.h"
32 #include "mastervolume.h"
33 #include "homescreenhandler.h"
34 #include "hmi-debug.h"
35 #include "chromecontroller.h"
36
37 int main(int argc, char *argv[])
38 {
39     QGuiApplication a(argc, argv);
40
41     QCoreApplication::setOrganizationDomain("LinuxFoundation");
42     QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
43     QCoreApplication::setApplicationName("HomeScreen");
44     QCoreApplication::setApplicationVersion("0.7.0");
45
46     QCommandLineParser parser;
47     parser.addPositionalArgument("port", a.translate("main", "port for binding"));
48     parser.addPositionalArgument("secret", a.translate("main", "secret for binding"));
49     parser.addHelpOption();
50     parser.addVersionOption();
51     parser.process(a);
52     QStringList positionalArguments = parser.positionalArguments();
53
54     int port = 1700;
55     QString token = "wm";
56     QString graphic_role = "homescreen"; // defined in layers.json in Window Manager
57
58     if (positionalArguments.length() == 2) {
59         port = positionalArguments.takeFirst().toInt();
60         token = positionalArguments.takeFirst();
61     }
62
63     HMI_DEBUG("HomeScreen","port = %d, token = %s", port, token.toStdString().c_str());
64
65     // import C++ class to QML
66     // qmlRegisterType<ApplicationLauncher>("HomeScreen", 1, 0, "ApplicationLauncher");
67     qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
68     qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
69     qmlRegisterUncreatableType<ChromeController>("SpeechChrome", 1, 0, "SpeechChromeController",
70                                                  QLatin1String("SpeechChromeController is uncreatable."));
71
72     ApplicationLauncher *launcher = new ApplicationLauncher();
73     QLibWindowmanager* layoutHandler = new QLibWindowmanager();
74     if(layoutHandler->init(port,token) != 0){
75         exit(EXIT_FAILURE);
76     }
77
78     AGLScreenInfo screenInfo(layoutHandler->get_scale_factor());
79
80     if (layoutHandler->requestSurface(graphic_role) != 0) {
81         exit(EXIT_FAILURE);
82     }
83
84     layoutHandler->set_event_handler(QLibWindowmanager::Event_SyncDraw, [layoutHandler, &graphic_role](json_object *object) {
85         layoutHandler->endDraw(graphic_role);
86     });
87
88     layoutHandler->set_event_handler(QLibWindowmanager::Event_ScreenUpdated, [layoutHandler, launcher](json_object *object) {
89         json_object *jarray = json_object_object_get(object, "ids");
90         int arrLen = json_object_array_length(jarray);
91         for( int idx = 0; idx < arrLen; idx++)
92         {
93             QString label = QString(json_object_get_string(     json_object_array_get_idx(jarray, idx) ));
94             HMI_DEBUG("HomeScreen","Event_ScreenUpdated application: %s.", label.toStdString().c_str());
95             QMetaObject::invokeMethod(launcher, "setCurrent", Qt::QueuedConnection, Q_ARG(QString, label));
96         }
97     });
98
99     HomescreenHandler* homescreenHandler = new HomescreenHandler();
100     homescreenHandler->init(port, token.toStdString().c_str());
101
102     QUrl bindingAddress;
103     bindingAddress.setScheme(QStringLiteral("ws"));
104     bindingAddress.setHost(QStringLiteral("localhost"));
105     bindingAddress.setPort(port);
106     bindingAddress.setPath(QStringLiteral("/api"));
107
108     QUrlQuery query;
109     query.addQueryItem(QStringLiteral("token"), token);
110     bindingAddress.setQuery(query);
111
112     // mail.qml loading
113     QQmlApplicationEngine engine;
114     engine.rootContext()->setContextProperty("bindingAddress", bindingAddress);
115     engine.rootContext()->setContextProperty("layoutHandler", layoutHandler);
116     engine.rootContext()->setContextProperty("homescreenHandler", homescreenHandler);
117     engine.rootContext()->setContextProperty("launcher", launcher);
118     engine.rootContext()->setContextProperty("weather", new Weather(bindingAddress));
119     engine.rootContext()->setContextProperty("bluetooth", new Bluetooth(bindingAddress, engine.rootContext()));
120     engine.rootContext()->setContextProperty("speechChromeController", new ChromeController(bindingAddress, &engine));
121     engine.rootContext()->setContextProperty("screenInfo", &screenInfo);
122     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
123
124     QObject *root = engine.rootObjects().first();
125     QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
126     QObject::connect(window, SIGNAL(frameSwapped()), layoutHandler, SLOT(slotActivateSurface()));
127
128     QList<QObject *> sobjs = engine.rootObjects();
129     StatusBarModel *statusBar = sobjs.first()->findChild<StatusBarModel *>("statusBar");
130     statusBar->init(bindingAddress, engine.rootContext());
131
132     return a.exec();
133 }