POI: AGL LifeCycle Management
[apps/agl-service-windowmanager.git] / client / main.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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
18 #include <QDebug>
19 #include <QDir>
20 #include <QGuiApplication>
21 #include <QQmlApplicationEngine>
22 #include <QQmlContext>
23 #include <QQuickView>
24 #include <QQuickWindow>
25 #include "communication.h"
26
27 #include "qlibwindowmanager.h"
28
29 int main(int argc, char *argv[]) {
30     QGuiApplication app(argc, argv);
31
32     qDebug() << QCoreApplication::arguments();
33
34     if (QCoreApplication::arguments().count() < 5) {
35         qWarning() << "Wrong parameters specified for the application. "
36                       "Please restart with correct parameters:"
37                       "width, height, name, color [port] [token]:\n\n"
38                       "/usr/bin/WindowManagerSampleApp/"
39                       "qmlWindowManagerSampleApp width height name color\n";
40         exit(EXIT_FAILURE);
41     }
42
43     QString label = QCoreApplication::arguments().at(3);
44
45     QLibWindowmanager* qwm = new QLibWindowmanager();
46
47     QString token = "wm";
48     int port = 1700;
49     if(QCoreApplication::arguments().count() == 7){
50         bool ok;
51         port = QCoreApplication::arguments().at(5).toInt(&ok);
52         if(ok == false){
53             port = 1700;
54         }
55         else{
56             token = QCoreApplication::arguments().at(6);
57         }
58     }
59     const char* ctoken = token.toLatin1().data();
60     if (qwm->init(port, ctoken) != 0) {
61         exit(EXIT_FAILURE);
62     }
63
64     if (qwm->requestSurface(
65             QCoreApplication::arguments().at(3).toLatin1().data()) != 0) {
66         exit(EXIT_FAILURE);
67     }
68
69     qwm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [qwm](char const *label) {
70         fprintf(stderr, "Surface %s got syncDraw!\n", label);
71         qwm->endDraw(label);
72     });
73     qwm->set_event_handler(QLibWindowmanager::Event_Active, [](char const *label) {
74         fprintf(stderr, "Surface %s got activated!\n", label);
75     });
76     qwm->set_event_handler(QLibWindowmanager::Event_Visible, [](char const *label) {
77         fprintf(stderr, "Surface %s got visible!\n", label);
78     });
79     qwm->set_event_handler(QLibWindowmanager::Event_FlushDraw, [](char const *label) {
80         fprintf(stderr, "Surface %s got flushDraw!\n", label);
81     });
82
83     communication comm;
84     comm.setWidth(QCoreApplication::arguments().at(1).toUInt());
85     comm.setHeight(QCoreApplication::arguments().at(2).toUInt());
86     comm.setAppName(label);
87     comm.setColor(QCoreApplication::arguments().at(4));
88
89     QQmlApplicationEngine engine;
90     engine.rootContext()->setContextProperty("COMM", &comm);
91
92     engine.load(
93         QUrl(QStringLiteral("qrc:/extra/WindowManagerSampleApp.qml")));
94     QObject *root = engine.rootObjects().first();
95     QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
96     QObject::connect(root, SIGNAL(frameSwapped()), qwm, SLOT(slotActivateSurface()));
97
98     return app.exec();
99 }