a5b9ecead3353431e22aaea3bc61e7cedb47ceb0
[apps/agl-cluster-demo-dashboard.git] / app / main.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 2018, 2019 Konsulko Group
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <QtCore/QDebug>
19 #include <QtCore/QCommandLineParser>
20 #include <QtCore/QUrlQuery>
21 #include <QtGui/QGuiApplication>
22 #include <QtQml/QQmlApplicationEngine>
23 #include <QtQml/QQmlContext>
24 #include <QtQml/qqml.h>
25 #include <QQuickWindow>
26 #include <QtQuickControls2/QQuickStyle>
27 #include <glib.h>
28
29 #include <qlibwindowmanager.h>
30 #include <signalcomposer.h>
31
32 // Global indicating whether canned animation should run
33 bool runAnimation = true;
34
35 void read_config(void)
36 {
37         GKeyFile* conf_file;
38         gboolean value;
39
40         // Load settings from configuration file if it exists
41         conf_file = g_key_file_new();
42         if(conf_file &&
43            g_key_file_load_from_dirs(conf_file,
44                                      "AGL.conf",
45                                      (const gchar**) g_get_system_config_dirs(),
46                                      NULL,
47                                      G_KEY_FILE_KEEP_COMMENTS,
48                                      NULL) == TRUE) {
49                 GError *err = NULL;
50                 value = g_key_file_get_boolean(conf_file,
51                                                "dashboard",
52                                                "animation",
53                                                &err);
54                 if(value) {
55                         runAnimation = true;
56                 } else {
57                         if(err == NULL) {
58                                 runAnimation = false;
59                         } else {
60                                 qWarning("Invalid value for \"animation\" key!");
61                         }
62                 }
63         }
64
65 }
66
67 int main(int argc, char *argv[])
68 {
69     // Slight hack, using the homescreen role greatly simplifies things wrt
70     // the windowmanager
71     QString myname = QString("homescreen");
72
73     QGuiApplication app(argc, argv);
74
75     QCommandLineParser parser;
76     parser.addPositionalArgument("port", app.translate("main", "port for binding"));
77     parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
78     parser.addHelpOption();
79     parser.addVersionOption();
80     parser.process(app);
81     QStringList positionalArguments = parser.positionalArguments();
82
83     QQmlApplicationEngine engine;
84
85     if (positionalArguments.length() == 2) {
86         int port = positionalArguments.takeFirst().toInt();
87         QString secret = positionalArguments.takeFirst();
88         QUrl bindingAddress;
89         bindingAddress.setScheme(QStringLiteral("ws"));
90         bindingAddress.setHost(QStringLiteral("localhost"));
91         bindingAddress.setPort(port);
92         bindingAddress.setPath(QStringLiteral("/api"));
93         QUrlQuery query;
94         query.addQueryItem(QStringLiteral("token"), secret);
95         bindingAddress.setQuery(query);
96         QQmlContext *context = engine.rootContext();
97         context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
98
99         std::string token = secret.toStdString();
100         QLibWindowmanager* qwm = new QLibWindowmanager();
101
102         // WindowManager
103         if(qwm->init(port, secret) != 0){
104             exit(EXIT_FAILURE);
105         }
106
107         // Request a surface as described in layers.json windowmanager’s file
108         if (qwm->requestSurface(myname) != 0) {
109             exit(EXIT_FAILURE);
110         }
111
112         // Create an event callback against an event type. Here a lambda is called when SyncDraw event occurs
113         qwm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [qwm, myname](json_object*) {
114             fprintf(stderr, "Surface got syncDraw!\n");
115             qwm->endDraw(myname);
116         });
117
118         context->setContextProperty("SignalComposer", new SignalComposer(bindingAddress, context));
119         read_config();
120         context->setContextProperty("runAnimation", runAnimation);
121
122         engine.load(QUrl(QStringLiteral("qrc:/cluster-gauges.qml")));
123
124         // Find the instantiated model QObject and connect the signals/slots
125         QList<QObject *> mobjs = engine.rootObjects();
126
127         QQuickWindow *window = qobject_cast<QQuickWindow *>(mobjs.first());
128         QObject::connect(window, SIGNAL(frameSwapped()), qwm, SLOT(slotActivateSurface()));
129     }
130
131     return app.exec();
132 }