add source for ces2019 horizontal
[apps/onscreenapp.git] / sample / 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 <QUrlQuery> 
18 #include <QQmlContext>
19 #include <QtCore/QDebug>
20 #include <QCommandLineParser>
21 #include <QtGui/QGuiApplication>
22 #include <QtQml/QQmlApplicationEngine>
23 #include <QtQuickControls2/QQuickStyle>
24 #include <QQuickWindow>
25 #include <qlibhomescreen.h>
26 #include <qlibwindowmanager.h>
27 #include "hmi-debug.h"
28
29 int main(int argc, char *argv[])
30 {
31     QGuiApplication app(argc, argv);
32     app.setApplicationVersion(QStringLiteral("3.99.3"));
33     app.setOrganizationDomain(QStringLiteral("automotivelinux.org"));
34     app.setOrganizationName(QStringLiteral("AutomotiveGradeLinux"));
35
36     QQuickStyle::setStyle("AGL");
37
38     QCommandLineParser parser;
39     parser.addPositionalArgument("port", app.translate("main", "port for binding"));
40     parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
41     parser.addHelpOption();
42     parser.addVersionOption();
43     parser.process(app);
44     QStringList positionalArguments = parser.positionalArguments();
45
46     QQmlApplicationEngine engine;
47     if (positionalArguments.length() == 2) {
48         int port = positionalArguments.takeFirst().toInt();
49         QString secret = positionalArguments.takeFirst();
50         QUrl bindingAddress;
51         bindingAddress.setScheme(QStringLiteral("ws"));
52         bindingAddress.setHost(QStringLiteral("localhost"));
53         bindingAddress.setPort(port);
54         bindingAddress.setPath(QStringLiteral("/api"));
55         QUrlQuery query;
56         query.addQueryItem(QStringLiteral("token"), secret);
57         bindingAddress.setQuery(query);
58         QQmlContext *context = engine.rootContext();
59         context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
60
61         QString myname = QString("onstest");
62
63         QLibHomeScreen* homescreenHandler = new QLibHomeScreen();
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
75         // HomeScreen
76         homescreenHandler->init(port, secret);
77
78         engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
79         engine.rootContext()->setContextProperty("homescreenHandler", homescreenHandler);
80
81         QObject *root = engine.rootObjects().first();
82         QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
83         QObject::connect(window, SIGNAL(frameSwapped()), qwm, SLOT(slotActivateSurface()));
84
85         // Create an event callback against an event type. Here a lambda is called when SyncDraw event occurs
86         qwm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [qwm, myname, root](json_object *object) {
87             fprintf(stderr, "Surface got syncDraw!\n");
88             qwm->endDraw(myname);
89         });
90
91         homescreenHandler->set_event_handler(QLibHomeScreen::Event_TapShortcut, [qwm, myname](json_object *object){
92             json_object *appnameJ = nullptr;
93             if(json_object_object_get_ex(object, "application_name", &appnameJ))
94             {
95                 const char *appname = json_object_get_string(appnameJ);
96                 qDebug("Surface %s got tapShortcut\n", appname);
97                 if(strcmp("onstest", appname) == 0)
98                 {
99                     qwm->activateSurface(myname);
100                 }
101             }
102         });
103     }
104     return app.exec();
105 }
106