warehouse for ces2019
[apps/onscreenapp.git] / app / src / main.cpp
1 /*
2  * Copyright (C) 2018 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 <QtQml/qqml.h>
18 #include <qlibwindowmanager.h>
19 #include <QQuickWindow>
20 #include <QtCore/QCommandLineParser>
21 #include <QtCore/QDebug>
22 #include <QtCore/QDir>
23 #include <QtCore/QStandardPaths>
24 #include <QtCore/QUrlQuery>
25 #include <QtGui/QGuiApplication>
26 #include <QtQml/QQmlApplicationEngine>
27 #include <QtQml/QQmlContext>
28 #include <QtQuickControls2/QQuickStyle>
29 #include "afm_user_daemon_proxy.h"
30 #include "nativeappmodel.h"
31 #include "qlibhomescreen.h"
32 #include "serverappmodel.h"
33
34 org::AGL::afm::user* afm_user_daemon_proxy;
35
36 namespace {
37
38 struct Cleanup {
39   static inline void cleanup(org::AGL::afm::user* p) {
40     delete p;
41     afm_user_daemon_proxy = Q_NULLPTR;
42   }
43 };
44
45 void noOutput(QtMsgType, const QMessageLogContext&, const QString&) {}
46
47 }  // namespace
48
49 int main(int argc, char* argv[]) {
50   QString role = QString("warehouse");
51   QGuiApplication app(argc, argv);
52
53   // use launch process
54   QScopedPointer<org::AGL::afm::user, Cleanup> afm_user_daemon_proxy(
55       new org::AGL::afm::user("org.AGL.afm.user", "/org/AGL/afm/user",
56                               QDBusConnection::sessionBus(), 0));
57   ::afm_user_daemon_proxy = afm_user_daemon_proxy.data();
58
59   app.setApplicationName("warehouse");
60
61   QQuickStyle::setStyle("AGL");
62
63   QQmlApplicationEngine engine;
64   QQmlContext* context = engine.rootContext();
65
66   QCommandLineParser parser;
67   parser.addPositionalArgument("port",
68                                app.translate("main", "port for binding"));
69   parser.addPositionalArgument("secret",
70                                app.translate("main", "secret for binding"));
71   parser.addHelpOption();
72   parser.addVersionOption();
73   parser.process(app);
74   QStringList positionalArguments = parser.positionalArguments();
75
76   if (positionalArguments.length() == 2) {
77     int port = positionalArguments.takeFirst().toInt();
78     QString secret = positionalArguments.takeFirst();
79     QUrl bindingAddress;
80     bindingAddress.setScheme(QStringLiteral("ws"));
81     bindingAddress.setHost(QStringLiteral("localhost"));
82     bindingAddress.setPort(port);
83     bindingAddress.setPath(QStringLiteral("/api"));
84     QUrlQuery query;
85     query.addQueryItem(QStringLiteral("token"), secret);
86     bindingAddress.setQuery(query);
87
88     std::string token = secret.toStdString();
89
90     // import C++ class to QML
91     qmlRegisterType<NativeAppModel>("NativeAppModel", 1, 0, "NativeAppModel");
92     qmlRegisterType<ServerAppModel>("ServerAppModel", 1, 0, "ServerAppModel");
93
94     QLibHomeScreen* homescreenHandler = new QLibHomeScreen();
95     QLibWindowmanager* qwm = new QLibWindowmanager();
96
97     // WindowManager
98     if (qwm->init(port, secret) != 0) {
99       exit(EXIT_FAILURE);
100     }
101
102     AGLScreenInfo screenInfo(qwm->get_scale_factor());
103
104     // Request a surface as described in layers.json windowmanager’s file
105     if (qwm->requestSurface(role) != 0) {
106       exit(EXIT_FAILURE);
107     }
108
109     // Create an event callback against an event type. Here a lambda is
110     // called when SyncDraw event occurs
111     qwm->set_event_handler(QLibWindowmanager::Event_SyncDraw,
112                            [qwm, role](json_object* object) {
113                              fprintf(stderr, "Surface got syncDraw!\n");
114
115                              qwm->endDraw(role);
116                            });
117
118     // HomeScreen
119     homescreenHandler->init(port, token.c_str());
120     // Set the event handler for Event_TapShortcut which will activate the
121     // surface for windowmanager
122     homescreenHandler->set_event_handler(
123         QLibHomeScreen::Event_TapShortcut, [qwm, role](json_object* object) {
124             qDebug("Surface warehouse got tapShortcut.\n");
125             qwm->activateWindow(role);
126         });
127
128     context->setContextProperty(QStringLiteral("homescreenHandler"),
129                                 homescreenHandler);
130     context->setContextProperty(QStringLiteral("screenInfo"), &screenInfo);
131     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
132     QObject* root = engine.rootObjects().first();
133
134     QQuickWindow* window = qobject_cast<QQuickWindow*>(root);
135     QObject::connect(window, SIGNAL(frameSwapped()), qwm,
136                      SLOT(slotActivateSurface()));
137   }
138   return app.exec();
139 }