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