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