2 * Copyright (C) 2018 The Qt Company Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
34 org::AGL::afm::user* afm_user_daemon_proxy;
39 static inline void cleanup(org::AGL::afm::user* p) {
41 afm_user_daemon_proxy = Q_NULLPTR;
45 void noOutput(QtMsgType, const QMessageLogContext&, const QString&) {}
49 int main(int argc, char* argv[]) {
50 QString role = QString("warehouse");
51 QGuiApplication app(argc, argv);
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();
59 app.setApplicationName("warehouse");
61 QQuickStyle::setStyle("AGL");
63 QQmlApplicationEngine engine;
64 QQmlContext* context = engine.rootContext();
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();
74 QStringList positionalArguments = parser.positionalArguments();
76 if (positionalArguments.length() == 2) {
77 int port = positionalArguments.takeFirst().toInt();
78 QString secret = positionalArguments.takeFirst();
80 bindingAddress.setScheme(QStringLiteral("ws"));
81 bindingAddress.setHost(QStringLiteral("localhost"));
82 bindingAddress.setPort(port);
83 bindingAddress.setPath(QStringLiteral("/api"));
85 query.addQueryItem(QStringLiteral("token"), secret);
86 bindingAddress.setQuery(query);
88 std::string token = secret.toStdString();
90 // import C++ class to QML
91 qmlRegisterType<NativeAppModel>("NativeAppModel", 1, 0, "NativeAppModel");
92 qmlRegisterType<ServerAppModel>("ServerAppModel", 1, 0, "ServerAppModel");
94 QLibHomeScreen* homescreenHandler = new QLibHomeScreen();
95 QLibWindowmanager* qwm = new QLibWindowmanager();
98 if (qwm->init(port, secret) != 0) {
102 AGLScreenInfo screenInfo(qwm->get_scale_factor());
104 // Request a surface as described in layers.json windowmanager’s file
105 if (qwm->requestSurface(role) != 0) {
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");
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);
128 context->setContextProperty(QStringLiteral("homescreenHandler"),
130 context->setContextProperty(QStringLiteral("screenInfo"), &screenInfo);
131 engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
132 QObject* root = engine.rootObjects().first();
134 QQuickWindow* window = qobject_cast<QQuickWindow*>(root);
135 QObject::connect(window, SIGNAL(frameSwapped()), qwm,
136 SLOT(slotActivateSurface()));