Change debug message
[apps/mediaplayer.git] / app / main.cpp
index 528f840..4c406e6 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2016 The Qt Company Ltd.
+ * Copyright (C) 2016, 2017 Toyota Motor Corporation
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <QtQml/qqml.h>
 #include <QtQuickControls2/QQuickStyle>
 
-#ifdef HAVE_LIBHOMESCREEN
+#include <QQuickWindow>
+#include "qlibwindowmanager.h"
+#include "qlibsoundmanager.h"
 #include <libhomescreen.hpp>
-#endif
 
-#ifdef HAVE_LIGHTMEDIASCANNER
-#include "lightmediascanner.h"
-#endif
-
-#ifdef HAVE_DBUS
-#include "dbus.h"
-#endif
-
-#include "playlistwithmetadata.h"
-
-#ifndef HAVE_LIGHTMEDIASCANNER
-QVariantList readMusicFile(const QString &path)
-{
-    QVariantList ret;
-    QDir dir(path);
-    for (const auto &entry : dir.entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot, QDir::Name)) {
-        QFileInfo fileInfo(dir.absoluteFilePath(entry));
-        if (fileInfo.isDir()) {
-            ret.append(readMusicFile(fileInfo.absoluteFilePath()));
-        } else if (fileInfo.isFile()) {
-            ret.append(QUrl::fromLocalFile(fileInfo.absoluteFilePath()));
-        }
-    }
-    return ret;
-}
-#endif
+static LibHomeScreen* hs;
+static QLibWindowmanager* qwm;
+static QLibSoundmanager* smw;
+static std::string myname = std::string("MediaPlayer");
 
 int main(int argc, char *argv[])
 {
-#ifdef HAVE_LIBHOMESCREEN
-    LibHomeScreen libHomeScreen;
-
-    if (!libHomeScreen.renderAppToAreaAllowed(0, 1)) {
-        qWarning() << "renderAppToAreaAllowed is denied";
-        return -1;
-    }
-#endif
-
     QGuiApplication app(argc, argv);
 
     QQuickStyle::setStyle("AGL");
 
-    qmlRegisterType<PlaylistWithMetadata>("MediaPlayer", 1, 0, "PlaylistWithMetadata");
-
-    QVariantList mediaFiles;
-
-#ifdef HAVE_LIGHTMEDIASCANNER
-    mediaFiles = LightMediaScanner::processLightMediaScanner();
-#else
-    QString music;
-
-    for (const auto &music : QStandardPaths::standardLocations(QStandardPaths::MusicLocation)) {
-        mediaFiles.append(readMusicFile(music));
-    }
-#endif
-
     QQmlApplicationEngine engine;
     QQmlContext *context = engine.rootContext();
 
@@ -108,25 +65,59 @@ int main(int argc, char *argv[])
         query.addQueryItem(QStringLiteral("token"), secret);
         bindingAddress.setQuery(query);
         context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
-    }
 
-    context->setContextProperty("mediaFiles", mediaFiles);
+        std::string token = secret.toStdString();
 
-#if defined(HAVE_DBUS)
-    DbusService dbus_service;
-    context->setContextProperty("dbus", &dbus_service);
-#endif
+        hs = new LibHomeScreen();
+        qwm = new QLibWindowmanager();
+        smw = new QLibSoundmanager();
 
-    engine.load(QUrl(QStringLiteral("qrc:/MediaPlayer.qml")));
+        // WindowManager
+        if(qwm->init(port,secret) != 0){
+            exit(EXIT_FAILURE);
+        }
+        if (qwm->requestSurface(myname.c_str()) != 0) {
+            exit(EXIT_FAILURE);
+        }
+        qwm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [qwm](json_object *object) {
+            fprintf(stderr, "Surface got syncDraw!\n");
+            qwm->endDraw(myname.c_str());
+            });
+        qwm->set_event_handler(QLibWindowmanager::Event_FlushDraw, [&engine, smw](json_object *object) {
+            fprintf(stderr, "Surface got flushDraw!\n");
+            QObject *root = engine.rootObjects().first();
+            int sourceID = root->property("sourceID").toInt();
+            smw->connect(sourceID, "default");
+            });
+        
+        // HomeScreen
+        hs->init(port, token.c_str());
+        hs->set_event_handler(LibHomeScreen::Event_TapShortcut, [qwm](json_object *object){
+            const char *appname = json_object_get_string(
+                json_object_object_get(object, "application_name"));
+            if(myname == appname)
+            {
+                qDebug("Surface %s got tapShortcut\n", appname);
+                qwm->activateSurface(myname.c_str());
+            }
+        });
+
+        // SoundManager, event handler is set inside smw
+        smw->init(port, secret);
+
+        engine.rootContext()->setContextProperty("smw",smw);
 
-#if defined(HAVE_DBUS)
-#if defined(HAVE_LIGHTMEDIASCANNER)
-    if (!dbus_service.enableLMS())
-       qWarning() << "Cannot run enableLMS";
-#endif
-    if (!dbus_service.enableBluetooth())
-       qWarning() << "Cannot run enableBluetooth";
-#endif
+    }
+
+    engine.load(QUrl(QStringLiteral("qrc:/MediaPlayer.qml")));
+        QObject *root = engine.rootObjects().first();
+        QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
+        QObject::connect(window, SIGNAL(frameSwapped()),
+            qwm, SLOT(slotActivateSurface()));  // This should be disconnected, but when...
+        QObject::connect(smw, SIGNAL(reply(QVariant)),
+            root, SLOT(slotReply(QVariant)));
+        QObject::connect(smw, SIGNAL(event(QVariant, QVariant)),
+            root, SLOT(slotEvent(QVariant, QVariant)));
 
     return app.exec();
 }