92f1c5ffbfe36a7c5e9887954163c4d5eccfb95f
[apps/onscreenapp.git] / app / main.cpp
1
2 #include <QtQml/qqml.h>
3 #include <qlibwindowmanager.h>
4 #include <QQuickWindow>
5 #include <QtCore/QCommandLineParser>
6 #include <QtCore/QDebug>
7 #include <QtCore/QDir>
8 #include <QtCore/QStandardPaths>
9 #include <QtCore/QUrlQuery>
10 #include <QtGui/QGuiApplication>
11 #include <QtQml/QQmlApplicationEngine>
12 #include <QtQml/QQmlContext>
13 #include <QtQuickControls2/QQuickStyle>
14 #include <libhomescreen.hpp>
15
16 int main(int argc, char* argv[]) {
17     QString role = QString("Restriction");
18
19     QGuiApplication app(argc, argv);
20
21     QQuickStyle::setStyle("AGL");
22
23     QQmlApplicationEngine engine;
24
25     QCommandLineParser parser;
26     parser.addPositionalArgument("port",
27                                  app.translate("main", "port for binding"));
28     parser.addPositionalArgument("secret",
29                                  app.translate("main", "secret for binding"));
30     parser.addHelpOption();
31     parser.addVersionOption();
32     parser.process(app);
33     QStringList positionalArguments = parser.positionalArguments();
34
35     if (positionalArguments.length() == 2) {
36         int port = positionalArguments.takeFirst().toInt();
37         QString secret = positionalArguments.takeFirst();
38         QUrl bindingAddress;
39         bindingAddress.setScheme(QStringLiteral("ws"));
40         bindingAddress.setHost(QStringLiteral("localhost"));
41         bindingAddress.setPort(port);
42         bindingAddress.setPath(QStringLiteral("/api"));
43         QUrlQuery query;
44         query.addQueryItem(QStringLiteral("token"), secret);
45         bindingAddress.setQuery(query);
46
47         std::string token = secret.toStdString();
48         LibHomeScreen* hs = new LibHomeScreen();
49         QLibWindowmanager* qwm = new QLibWindowmanager();
50
51         if (qwm->init(port, secret) != 0) {
52             exit(EXIT_FAILURE);
53         }
54         // Request a surface as described in layers.json windowmanager’s file
55         if (qwm->requestSurface(role) != 0) {
56             exit(EXIT_FAILURE);
57         }
58         // Create an event callback against an event type. Here a lambda is
59         // called when SyncDraw event occurs
60         qwm->set_event_handler(QLibWindowmanager::Event_SyncDraw,
61                                [qwm, role](json_object* object) {
62                                    fprintf(stderr, "Surface got syncDraw!\n");
63                                    qwm->endDraw(role);
64                                });
65
66         engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
67         QObject* root = engine.rootObjects().first();
68
69         // HomeScreen
70         hs->init(port, token.c_str());
71
72         // release restriction
73         hs->set_event_handler(
74             LibHomeScreen::Event_ReleaseRestriction,
75             [qwm, role, root](json_object* object) {
76                 json_object* areaJ = nullptr;
77                 if (json_object_object_get_ex(object, "area", &areaJ)) {
78                     QString area(QLatin1String(json_object_get_string(areaJ)));
79
80                     QMetaObject::invokeMethod(root, "hideImage");
81
82                     // qwm->releaseWR(role, area);
83                     qwm->deactivateSurface(role);
84                 }
85             });
86
87         // allocate restriction
88         hs->set_event_handler(
89             LibHomeScreen::Event_AllocateRestriction,
90             [qwm, role, root](json_object* object) {
91                 json_object* areaJ = nullptr;
92                 if (json_object_object_get_ex(object, "area", &areaJ)) {
93                     QString area(QLatin1String(json_object_get_string(areaJ)));
94                     qDebug()
95                         << "Surface got Event_AllocateRestriction " << area;
96
97                     QMetaObject::invokeMethod(root, "showImage",
98                                               Q_ARG(QVariant, area));
99
100                     // qwm->allocateWR(role, area);
101                     qwm->activateSurface(role, area.prepend("restriction."));
102                 }
103             });
104     }
105
106     return app.exec();
107 }