Apply QtQuickControls2 style from platform
[apps/hvac.git] / app / main.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
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 <QtCore/QDebug>
18 #include <QtCore/QCommandLineParser>
19 #include <QtCore/QUrlQuery>
20 #include <QtGui/QGuiApplication>
21 #include <QtQml/QQmlApplicationEngine>
22 #include <QtQml/QQmlContext>
23 #include <QQuickWindow>
24 #include <libhomescreen.hpp>
25 #include <qlibwindowmanager.h>
26
27 int main(int argc, char *argv[])
28 {
29     QString myname = QString("HVAC");
30
31     QGuiApplication app(argc, argv);
32     app.setApplicationVersion(QStringLiteral("0.1.0"));
33     app.setOrganizationDomain(QStringLiteral("automotivelinux.org"));
34     app.setOrganizationName(QStringLiteral("AutomotiveGradeLinux"));
35
36     QCommandLineParser parser;
37     parser.addPositionalArgument("port", app.translate("main", "port for binding"));
38     parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
39     parser.addHelpOption();
40     parser.addVersionOption();
41     parser.process(app);
42     QStringList positionalArguments = parser.positionalArguments();
43
44     QQmlApplicationEngine engine;
45     if (positionalArguments.length() == 2) {
46         int port = positionalArguments.takeFirst().toInt();
47         QString secret = positionalArguments.takeFirst();
48         QUrl bindingAddress;
49         bindingAddress.setScheme(QStringLiteral("ws"));
50         bindingAddress.setHost(QStringLiteral("localhost"));
51         bindingAddress.setPort(port);
52         bindingAddress.setPath(QStringLiteral("/api"));
53         QUrlQuery query;
54         query.addQueryItem(QStringLiteral("token"), secret);
55         bindingAddress.setQuery(query);
56         QQmlContext *context = engine.rootContext();
57         context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
58
59         std::string token = secret.toStdString();
60         LibHomeScreen* hs = new LibHomeScreen();
61         QLibWindowmanager* qwm = new QLibWindowmanager();
62
63         // WindowManager
64         if(qwm->init(port,secret) != 0){
65             exit(EXIT_FAILURE);
66         }
67         // Request a surface as described in layers.json windowmanager’s file
68         if (qwm->requestSurface(myname) != 0) {
69             exit(EXIT_FAILURE);
70         }
71         // Create an event callbnewack against an event type. Here a lambda is called when SyncDraw event occurs
72         qwm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [qwm, myname](json_object *object) {
73             fprintf(stderr, "Surface got syncDraw!\n");
74             qwm->endDraw(myname);
75         });
76
77         // HomeScreen
78         hs->init(port, token.c_str());
79         // Set the event handler for Event_TapShortcut which will activate the surface for windowmanager
80         hs->set_event_handler(LibHomeScreen::Event_TapShortcut, [qwm, myname](json_object *object){
81             json_object *appnameJ = nullptr;
82             if(json_object_object_get_ex(object, "application_name", &appnameJ))
83             {
84                 const char *appname = json_object_get_string(appnameJ);
85                 if(myname == appname)
86                 {
87                     qDebug("Surface %s got tapShortcut\n", appname);
88                     qwm->activateSurface(myname);
89                 }
90             }
91         });
92
93         engine.load(QUrl(QStringLiteral("qrc:/HVAC.qml")));
94         QObject *root = engine.rootObjects().first();
95         QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
96         QObject::connect(window, SIGNAL(frameSwapped()), qwm, SLOT(slotActivateSurface()
97         ));
98     }
99     return app.exec();
100 }
101