Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef QApplication Application;
#include "mpv.h"
#include "screensaver.h"
#include "qclipboardproxy.h"
#include "mediacontrols.h"

#else
#include <QGuiApplication>
Expand Down Expand Up @@ -101,6 +102,7 @@ int main(int argc, char **argv)
qmlRegisterType<ScreenSaver>("com.stremio.screensaver", 1, 0, "ScreenSaver");
qmlRegisterType<MpvObject>("com.stremio.libmpv", 1, 0, "MpvObject");
qmlRegisterType<ClipboardProxy>("com.stremio.clipboard", 1, 0, "Clipboard");
qmlRegisterType<MediaControls>("com.stremio.mediacontrols", 1, 0, "MediaControls");

InitializeParameters(engine, app);

Expand Down
63 changes: 61 additions & 2 deletions main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.stremio.process 1.0
import com.stremio.screensaver 1.0
import com.stremio.libmpv 1.0
import com.stremio.clipboard 1.0
import com.stremio.mediacontrols 1.0
import QtQml 2.2

import "autoupdater.js" as Autoupdater
Expand Down Expand Up @@ -76,6 +77,7 @@ ApplicationWindow {
mpv.setProperty(args[0], args[1]);
if (args[0] === "pause") {
shouldDisableScreensaver(!args[1]);
syncMediaControls();
}
}
if (ev === "mpv-observe-prop") mpv.observeProperty(args)
Expand Down Expand Up @@ -145,7 +147,36 @@ ApplicationWindow {
}

function isPlayerPlaying() {
return root.visible && typeof(mpv.getProperty("path"))==="string" && !mpv.getProperty("pause")
return root.visible && isMediaPlaying()
}

function hasLoadedMedia() {
var path = mpv.getProperty("path")
return typeof(path) === "string" && path.length > 0 && !mpv.getProperty("idle-active")
}

function isMediaPlaying() {
return hasLoadedMedia() && !mpv.getProperty("pause")
}

function syncMediaControls() {
mediaControls.active = hasLoadedMedia()
mediaControls.playing = isMediaPlaying()
}

function setMediaPaused(paused) {
if (!hasLoadedMedia()) {
syncMediaControls()
return
}

mpv.setProperty("pause", paused)
wakeupEvent()
syncMediaControls()
}

function toggleMediaPlayPause() {
setMediaPaused(!mpv.getProperty("pause"))
}

// Received external message
Expand Down Expand Up @@ -230,6 +261,26 @@ ApplicationWindow {
id: clipboard
}

MediaControls {
id: mediaControls
}

Connections {
target: mediaControls

function onPlayRequested() {
setMediaPaused(false)
}

function onPauseRequested() {
setMediaPaused(true)
}

function onTogglePlayPauseRequested() {
toggleMediaPlayPause()
}
}

//
// Streaming server
//
Expand Down Expand Up @@ -299,7 +350,13 @@ ApplicationWindow {
MpvObject {
id: mpv
anchors.fill: parent
onMpvEvent: function(ev, args) { transport.event(ev, args) }
onShellPropertyChanged: function(name, value) {
syncMediaControls();
}
onMpvEvent: function(ev, args) {
transport.event(ev, args)
syncMediaControls();
}
}

//
Expand Down Expand Up @@ -689,5 +746,7 @@ ApplicationWindow {
// Check for updates
console.info(" **** Completed. Loading Autoupdater ***")
Autoupdater.initAutoUpdater(autoUpdater, root.autoUpdaterErr, autoUpdaterShortTimer, autoUpdaterLongTimer, autoUpdaterRestartTimer, webView.profile.httpUserAgent);

syncMediaControls();
}
}
63 changes: 63 additions & 0 deletions mediacontrols.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "mediacontrols.h"

#ifndef Q_OS_MACOS

MediaControls::MediaControls(QObject *parent)
: QObject(parent), m_active(false), m_playing(false), d(nullptr)
{
}

MediaControls::~MediaControls() = default;

bool MediaControls::isActive() const
{
return m_active;
}

bool MediaControls::isPlaying() const
{
return m_playing;
}

void MediaControls::setActive(bool active)
{
if (m_active == active) {
return;
}

m_active = active;
Q_EMIT activeChanged();
}

void MediaControls::setPlaying(bool playing)
{
if (m_playing == playing) {
return;
}

m_playing = playing;
Q_EMIT playingChanged();
}

void MediaControls::requestPlay()
{
if (m_active) {
Q_EMIT playRequested();
}
}

void MediaControls::requestPause()
{
if (m_active) {
Q_EMIT pauseRequested();
}
}

void MediaControls::requestTogglePlayPause()
{
if (m_active) {
Q_EMIT togglePlayPauseRequested();
}
}

#endif // Q_OS_MACOS
41 changes: 41 additions & 0 deletions mediacontrols.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef MEDIACONTROLS_H
#define MEDIACONTROLS_H

#include <QObject>

class MediaControlsPrivate;

class MediaControls : public QObject
{
Q_OBJECT
Q_PROPERTY(bool active READ isActive WRITE setActive NOTIFY activeChanged)
Q_PROPERTY(bool playing READ isPlaying WRITE setPlaying NOTIFY playingChanged)

public:
explicit MediaControls(QObject *parent = nullptr);
~MediaControls() override;

bool isActive() const;
bool isPlaying() const;

public slots:
void setActive(bool active);
void setPlaying(bool playing);
void requestPlay();
void requestPause();
void requestTogglePlayPause();

signals:
void activeChanged();
void playingChanged();
void playRequested();
void pauseRequested();
void togglePlayPauseRequested();

private:
bool m_active;
bool m_playing;
MediaControlsPrivate *d;
};

#endif // MEDIACONTROLS_H
Loading