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