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