From 32f1007fd53a375fd4272395c6d73e0315119119 Mon Sep 17 00:00:00 2001 From: NamedKitten Date: Sat, 17 Nov 2018 19:33:32 +0000 Subject: [PATCH] [UI+Backend] Moved to a Enum based command API for backend commands. --- src/MpvPlayerBackend.cpp | 338 +++++++++++++++++++----------------- src/MpvPlayerBackend.h | 35 +--- src/enums.hpp | 28 +++ src/main.cpp | 1 + src/qml/AudioDeviceItem.qml | 3 +- src/qml/ControlsBar.qml | 13 +- src/qml/MainMenu.qml | 40 ++--- src/qml/TrackItem.qml | 4 +- src/qml/main.qml | 6 +- 9 files changed, 247 insertions(+), 221 deletions(-) diff --git a/src/MpvPlayerBackend.cpp b/src/MpvPlayerBackend.cpp index bdfc798..b467163 100644 --- a/src/MpvPlayerBackend.cpp +++ b/src/MpvPlayerBackend.cpp @@ -239,136 +239,188 @@ MpvPlayerBackend::updateDiscord() Discord_UpdatePresence(&discordPresence); } #endif +QVariant +MpvPlayerBackend::playerCommand(const Enums::Commands& cmd) +{ + return playerCommand(cmd, QVariant("NoArgProvided")); +} QVariant -MpvPlayerBackend::getaudioDevices() const +MpvPlayerBackend::playerCommand(const Enums::Commands& cmd, + const QVariant& args) { - QVariant drivers = getProperty("audio-device-list"); - QVariant currentDevice = getProperty("audio-device"); + switch (cmd) { + case Enums::Commands::TogglePlayPause: { + command(QVariantList() << "cycle" + << "pause"); + break; + } + case Enums::Commands::ToggleMute: { + command(QVariantList() << "cycle" + << "mute"); + break; + } + case Enums::Commands::SetAudioDevice: { + setProperty("audio-device", args.toString()); + break; + } + case Enums::Commands::GetAudioDevices: { + QVariant drivers = getProperty("audio-device-list"); + QVariant currentDevice = getProperty("audio-device"); - QVariantMap newDrivers; + QVariantMap newDrivers; - QSequentialIterable iterable = drivers.value(); - foreach (const QVariant& v, iterable) { - QVariantMap item = v.toMap(); - item["selected"] = currentDevice == item["name"]; - newDrivers[item["description"].toString()] = item; + QSequentialIterable iterable = drivers.value(); + foreach (const QVariant& v, iterable) { + QVariantMap item = v.toMap(); + item["selected"] = currentDevice == item["name"]; + newDrivers[item["description"].toString()] = item; + } + QMap pulseItem; + pulseItem["name"] = "pulse"; + pulseItem["description"] = "Default (pulseaudio)"; + pulseItem["selected"] = currentDevice == "pulse"; + newDrivers[pulseItem["description"].toString()] = pulseItem; + return QVariant(newDrivers); + } + case Enums::Commands::SetVolume: { + command(QVariantList() << "set" + << "volume" << args); + break; + } + + case Enums::Commands::AddVolume: { + + command(QVariantList() << "add" + << "volume" << args); + break; + } + + case Enums::Commands::AddSpeed: { + + QString speedString = + QString::number(getProperty("speed").toDouble() + args.toDouble()); + QVariant newSpeed = + QVariant(speedString.left(speedString.lastIndexOf('.') + 2)); + + playerCommand(Enums::Commands::SetSpeed, newSpeed); + break; + } + + case Enums::Commands::SubtractSpeed: { + + QString speedString = + QString::number(getProperty("speed").toDouble() - args.toDouble()); + QVariant newSpeed = + QVariant(speedString.left(speedString.lastIndexOf('.') + 2)); + playerCommand(Enums::Commands::SetSpeed, newSpeed); + break; + } + + case Enums::Commands::ChangeSpeed: { + + playerCommand( + Enums::Commands::SetSpeed, + QVariant(getProperty("speed").toDouble() * args.toDouble())); + break; + } + + case Enums::Commands::SetSpeed: { + + command(QVariantList() << "set" + << "speed" << args.toString()); + break; + } + case Enums::Commands::ToggleStats: { + + command(QVariantList() << "script-binding" + << "stats/display-stats-toggle"); + break; + } + case Enums::Commands::NextAudioTrack: { + + command(QVariantList() << "cycle" + << "audio"); + break; + } + case Enums::Commands::NextSubtitleTrack: { + + command(QVariantList() << "cycle" + << "sub"); + + break; + } + case Enums::Commands::NextVideoTrack: { + command(QVariantList() << "cycle" + << "video"); + break; + } + case Enums::Commands::PreviousPlaylistItem: { + + command(QVariantList() << "playlist-prev"); + + break; + } + case Enums::Commands::NextPlaylistItem: { + + command(QVariantList() << "playlist-next" + << "force"); + break; + } + case Enums::Commands::LoadFile: { + command(QVariantList() << "loadfile" << args); + + break; + } + case Enums::Commands::AppendFile: { + + command(QVariantList() << "loadfile" << args << "append-play"); + break; + } + case Enums::Commands::Seek: { + + command(QVariantList() << "seek" << args); + + break; + } + case Enums::Commands::SeekAbsolute: { + + command(QVariantList() << "seek" << args << "absolute"); + + break; + } + case Enums::Commands::GetTracks: { + + return mpv::qt::get_property_variant(mpv, "track-list"); + + break; + } + case Enums::Commands::ForwardFrame: { + + command(QVariantList() << "frame-step"); + + break; + } + case Enums::Commands::BackwardFrame: { + + command(QVariantList() << "frame-back-step"); + + break; + } + + case Enums::Commands::SetTrack: { + + command(QVariantList() << "set" << args.toList()[0] << args.toList()[1]); + + break; + } + + default: { + qDebug() << "Command not found: " << cmd; + break; + } } - QMap pulseItem; - pulseItem["name"] = "pulse"; - pulseItem["description"] = "Default (pulseaudio)"; - pulseItem["selected"] = currentDevice == "pulse"; - newDrivers[pulseItem["description"].toString()] = pulseItem; - return QVariant(newDrivers); -} - -void -MpvPlayerBackend::setAudioDevice(const QString& name) -{ - setProperty("audio-device", name); -} - -void -MpvPlayerBackend::togglePlayPause() -{ - command(QVariantList() << "cycle" - << "pause"); -} - -void -MpvPlayerBackend::toggleMute() -{ - command(QVariantList() << "cycle" - << "mute"); -} - -void -MpvPlayerBackend::nextAudioTrack() -{ - command(QVariantList() << "cycle" - << "audio"); -} -void -MpvPlayerBackend::nextSubtitleTrack() -{ - command(QVariantList() << "cycle" - << "sub"); -} - -void -MpvPlayerBackend::nextVideoTrack() -{ - command(QVariantList() << "cycle" - << "video"); -} - -void -MpvPlayerBackend::prevPlaylistItem() -{ - command(QVariantList() << "playlist-prev"); -} - -void -MpvPlayerBackend::nextPlaylistItem() -{ - command(QVariantList() << "playlist-next" - << "force"); -} - -void -MpvPlayerBackend::loadFile(const QVariant& filename) -{ - command(QVariantList() << "loadfile" << filename); -} - -void -MpvPlayerBackend::appendFile(const QVariant& filename) -{ - command(QVariantList() << "loadfile" << filename << "append-play"); -} - -void -MpvPlayerBackend::setVolume(const QVariant& volume) -{ - command(QVariantList() << "set" - << "volume" << volume); -} - -void -MpvPlayerBackend::addVolume(const QVariant& volume) -{ - command(QVariantList() << "add" - << "volume" << volume); -} - -void -MpvPlayerBackend::seekAbsolute(const QVariant& seekTime) -{ - command(QVariantList() << "seek" << seekTime << "absolute"); -} - -void -MpvPlayerBackend::seek(const QVariant& seekTime) -{ - command(QVariantList() << "seek" << seekTime); -} - -QVariant -MpvPlayerBackend::getTracks() const -{ - return mpv::qt::get_property_variant(mpv, "track-list"); -} - -void -MpvPlayerBackend::setTrack(const QVariant& track, const QVariant& id) -{ - command(QVariantList() << "set" << track << id); -} - -QVariant -MpvPlayerBackend::getTrack(const QString& track) -{ - return mpv::qt::get_property_variant(mpv, track); + return QVariant("NoOutput"); } QVariant @@ -394,44 +446,6 @@ MpvPlayerBackend::toggleOnTop() AlwaysOnTop(window()->winId(), onTop); } -void -MpvPlayerBackend::toggleStats() -{ - command(QVariantList() << "script-binding" - << "stats/display-stats-toggle"); -} - -void -MpvPlayerBackend::addSpeed(const QVariant& speed) -{ - QString speedString = - QString::number(getProperty("speed").toDouble() + speed.toDouble()); - speedString = speedString.left(speedString.lastIndexOf('.') + 2); - setSpeed(QVariant(speedString)); -} - -void -MpvPlayerBackend::subtractSpeed(const QVariant& speed) -{ - QString speedString = - QString::number(getProperty("speed").toDouble() - speed.toDouble()); - speedString = speedString.left(speedString.lastIndexOf('.') + 2); - setSpeed(QVariant(speedString)); -} - -void -MpvPlayerBackend::changeSpeed(const QVariant& speedFactor) -{ - setSpeed(QVariant(getProperty("speed").toDouble() * speedFactor.toDouble())); -} - -void -MpvPlayerBackend::setSpeed(const QVariant& speed) -{ - command(QVariantList() << "set" - << "speed" << speed.toString()); -} - void MpvPlayerBackend::on_mpv_events() { diff --git a/src/MpvPlayerBackend.h b/src/MpvPlayerBackend.h index 42d6aa1..06eaa48 100644 --- a/src/MpvPlayerBackend.h +++ b/src/MpvPlayerBackend.h @@ -30,43 +30,23 @@ public: virtual Renderer* createRenderer() const; public slots: + // All 4 required for Player API + QVariant playerCommand(const Enums::Commands& command, const QVariant& args); + QVariant playerCommand(const Enums::Commands& command); void launchAboutQt(); -#ifdef DISCORD - void updateDiscord(); -#endif - void togglePlayPause(); - void toggleMute(); - void nextAudioTrack(); - void nextVideoTrack(); - void nextSubtitleTrack(); - void prevPlaylistItem(); - void nextPlaylistItem(); void toggleOnTop(); - void toggleStats(); - QVariant getTracks() const; - QVariant getaudioDevices() const; - void setAudioDevice(const QString& name); - void addSpeed(const QVariant& speed); - void subtractSpeed(const QVariant& speed); - void changeSpeed(const QVariant& speedFactor); - void setSpeed(const QVariant& speed); - QVariant getTrack(const QString& track); - void setTrack(const QVariant& track, const QVariant& id); - void setVolume(const QVariant& volume); - void addVolume(const QVariant& volume); - void loadFile(const QVariant& filename); - void appendFile(const QVariant& filename); - void seek(const QVariant& seekTime); - void seekAbsolute(const QVariant& seekTime); + // Optional but handy for MPV or custom backend settings. void command(const QVariant& params); void setProperty(const QString& name, const QVariant& value); void setOption(const QString& name, const QVariant& value); QVariant getProperty(const QString& name) const; + // Misc function. QVariant createTimestamp(const QVariant& seconds) const; signals: void onUpdate(); void mpv_events(); + // All below required for Player API void playStatusChanged(const Enums::PlayStatus& status); void volumeStatusChanged(const Enums::VolumeStatus& status); void volumeChanged(const int& volume); @@ -87,6 +67,9 @@ private slots: private: void handle_mpv_event(mpv_event* event); +#ifdef DISCORD + void updateDiscord(); +#endif }; #endif diff --git a/src/enums.hpp b/src/enums.hpp index aec7d52..f003f91 100644 --- a/src/enums.hpp +++ b/src/enums.hpp @@ -19,6 +19,34 @@ enum class VolumeStatus : int Normal = 2 }; Q_ENUM_NS(VolumeStatus) +enum class Commands : int +{ + TogglePlayPause = 0, + ToggleMute = 1, + SetAudioDevice = 2, + GetAudioDevices = 3, + AddVolume = 4, + SetVolume = 5, + AddSpeed = 6, + SubtractSpeed = 7, + ChangeSpeed = 8, + SetSpeed = 9, + ToggleStats = 10, + NextAudioTrack = 11, + NextVideoTrack = 12, + NextSubtitleTrack = 13, + PreviousPlaylistItem = 14, + NextPlaylistItem = 15, + LoadFile = 16, + AppendFile = 17, + Seek = 18, + SeekAbsolute = 19, + GetTracks = 20, + ForwardFrame = 21, + BackwardFrame = 22, + SetTrack = 23 +}; +Q_ENUM_NS(Commands) } #endif diff --git a/src/main.cpp b/src/main.cpp index dfb9189..599514b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -122,6 +122,7 @@ main(int argc, char* argv[]) ); qRegisterMetaType("Enums.PlayStatus"); qRegisterMetaType("Enums.VolumeStatus"); + qRegisterMetaType("Enums.Commands"); qmlRegisterType("player", 1, 0, "PlayerBackend"); std::setlocale(LC_NUMERIC, "C"); diff --git a/src/qml/AudioDeviceItem.qml b/src/qml/AudioDeviceItem.qml index 6f7f5ad..3bb4076 100644 --- a/src/qml/AudioDeviceItem.qml +++ b/src/qml/AudioDeviceItem.qml @@ -1,6 +1,7 @@ import QtQuick 2.11 import QtQuick.Controls 2.4 import Qt.labs.settings 1.0 +import player 1.0 Action { id: audioDeviceItem @@ -9,6 +10,6 @@ Action { checked: false onTriggered: { - player.setAudioDevice(deviceID) + player.playerCommand(Enums.Commands.SetAudioDevice, deviceID) } } diff --git a/src/qml/ControlsBar.qml b/src/qml/ControlsBar.qml index bef1bd8..6e08854 100644 --- a/src/qml/ControlsBar.qml +++ b/src/qml/ControlsBar.qml @@ -9,7 +9,6 @@ import player 1.0 Item { anchors.bottom: parent.bottom - anchors.left: parent.left anchors.right: parent.right @@ -129,7 +128,7 @@ Item { }) } onMoved: { - player.seekAbsolute(progressBar.value) + player.playerCommand(Enums.Commands.SeekAbsolute, value) } function getProgressBarHeight(nyan, isMouse) { @@ -227,7 +226,7 @@ Item { visible: false width: visible ? playPauseButton.width : 0 onClicked: { - player.prevPlaylistItem() + player.playerCommand(Enums.Commands.PreviousPlaylistItem) } background: Rectangle { color: "transparent" @@ -252,7 +251,7 @@ Item { anchors.bottom: parent.bottom anchors.left: playlistPrevButton.right onClicked: { - player.togglePlayPause() + player.playerCommand(Enums.Commands.TogglePlayPause) } background: Rectangle { color: "transparent" @@ -278,7 +277,7 @@ Item { anchors.bottom: parent.bottom anchors.left: playPauseButton.right onClicked: { - player.nextPlaylistItem() + player.playerCommand(Enums.Commands.NextPlaylistItem) } background: Rectangle { color: "transparent" @@ -295,7 +294,7 @@ Item { anchors.bottom: parent.bottom anchors.left: playlistNextButton.right onClicked: { - player.toggleMute() + player.playerCommand(Enums.Commands.ToggleMute) } background: Rectangle { color: "transparent" @@ -331,7 +330,7 @@ Item { anchors.top: parent.top anchors.bottom: parent.bottom onMoved: { - player.setVolume(Math.round(volumeBar.value).toString()) + player.playerCommand(Enums.Commands.SetVolume, Math.round(volumeBar.value).toString()) } Component.onCompleted: { player.volumeChanged.connect(function(volume) { diff --git a/src/qml/MainMenu.qml b/src/qml/MainMenu.qml index bfee711..e96dcea 100644 --- a/src/qml/MainMenu.qml +++ b/src/qml/MainMenu.qml @@ -44,7 +44,7 @@ MenuBar { subMenu.removeAction(subAction) } } - var newTracks = player.getTracks() + var newTracks = player.playerCommand(Enums.Commands.GetTracks) for (var i = 0, len = newTracks.length; i < len; i++) { var track = newTracks[i] @@ -111,7 +111,7 @@ MenuBar { title: translate.getTranslation("OPEN_FILE", i18n.language) nameFilters: ["All files (*)"] onAccepted: { - player.loadFile(String(fileDialog.file)) + player.playerCommand(Enums.Commands.LoadFile, String(fileDialog.file)) fileDialog.close() } onRejected: { @@ -124,7 +124,7 @@ MenuBar { title: translate.getTranslation("URL_FILE_PATH", i18n.language) standardButtons: StandardButton.Cancel | StandardButton.Open onAccepted: { - player.loadFile(pathText.text) + player.playerCommand(Enums.Commands.LoadFile, pathText.text) pathText.text = "" } TextField { @@ -225,35 +225,35 @@ MenuBar { Action { text: translate.getTranslation("PLAY_PAUSE", i18n.language) onTriggered: { - player.togglePlayPause() + player.playerCommand(Enums.Commands.TogglePlayPause) } shortcut: String(keybinds.playPause) } Action { text: translate.getTranslation("REWIND_10S", i18n.language) onTriggered: { - player.seek("-10") + player.playerCommand(Enums.Commands.Seek, "-10") } shortcut: keybinds.rewind10 } Action { text: translate.getTranslation("FORWARD_10S", i18n.language) onTriggered: { - player.seek("10") + player.playerCommand(Enums.Commands.Seek, "10") } shortcut: keybinds.forward10 } Action { text: translate.getTranslation("FORWARD_5S", i18n.language) onTriggered: { - player.seek("-5") + player.playerCommand(Enums.Commands.Seek, "-5") } shortcut: keybinds.rewind5 } Action { text: translate.getTranslation("FORWARD_5S", i18n.language) onTriggered: { - player.seek("5") + player.playerCommand(Enums.Commands.Seek, "5") } shortcut: keybinds.forward5 } @@ -261,7 +261,7 @@ MenuBar { text: translate.getTranslation("SPEED_DECREASE_POINT_ONE", i18n.language) onTriggered: { - player.subtractSpeed(0.1) + player.playerCommand(Enums.Commands.SubtractSpeed, 0.1) } shortcut: keybinds.decreaseSpeedByPointOne } @@ -269,35 +269,35 @@ MenuBar { text: translate.getTranslation("SPEED_INCREASE_POINT_ONE", i18n.language) onTriggered: { - player.addSpeed(0.1) + player.playerCommand(Enums.Commands.AddSpeed, 0.1) } shortcut: keybinds.increaseSpeedByPointOne } Action { text: translate.getTranslation("HALVE_SPEED", i18n.language) onTriggered: { - player.changeSpeed(0.5) + player.playerCommand(Enums.Commands.ChangeSpeed, 0.5) } shortcut: keybinds.halveSpeed } Action { text: translate.getTranslation("DOUBLE_SPEED", i18n.language) onTriggered: { - player.changeSpeed(2) + player.playerCommand(Enums.Commands.ChangeSpeed, 2) } shortcut: keybinds.doubleSpeed } Action { text: translate.getTranslation("FORWARD_FRAME", i18n.language) onTriggered: { - player.command(["frame-step"]) + player.playerCommand(Enums.Commands.ForwardFrame) } shortcut: keybinds.forwardFrame } Action { text: translate.getTranslation("BACKWARD_FRAME", i18n.language) onTriggered: { - player.command(["frame-back-step"]) + player.playerCommand(Enums.Commands.BackwardFrame) } shortcut: keybinds.backwardFrame } @@ -309,28 +309,28 @@ MenuBar { Action { text: translate.getTranslation("CYCLE_AUDIO_TRACK", i18n.language) onTriggered: { - player.nextAudioTrack() + player.playerCommand(Enums.Commands.NextAudioTrack) } shortcut: keybinds.cycleAudio } Action { text: translate.getTranslation("INCREASE_VOLUME", i18n.language) onTriggered: { - player.addVolume("2") + player.playerCommand(Enums.Commands.AddVolume, "2") } shortcut: keybinds.increaseVolume } Action { text: translate.getTranslation("DECREASE_VOLUME", i18n.language) onTriggered: { - player.addVolume("-2") + player.playerCommand(Enums.Commands.AddVolume, "-2") } shortcut: keybinds.decreaseVolume } Action { text: translate.getTranslation("MUTE_VOLUME", i18n.language) onTriggered: { - player.toggleMute() + player.playerCommand(Enums.Commands.ToggleMute) } shortcut: keybinds.mute } @@ -347,7 +347,7 @@ MenuBar { player.audioDevicesChanged.connect(updateAudioDevices) } function updateAudioDevices() { - var audioDevices = player.getaudioDevices() + var audioDevices = player.playerCommand(Enums.Commands.GetAudioDevices) for (var i = 0, len = audioDeviceMenu.count; i < len; i++) { audioDeviceMenu.takeAction(0) @@ -468,7 +468,7 @@ MenuBar { Action { text: translate.getTranslation("STATS", i18n.language) onTriggered: { - player.toggleStats() + player.playerCommand(Enums.Commands.ToggleStats) } shortcut: keybinds.statsForNerds } diff --git a/src/qml/TrackItem.qml b/src/qml/TrackItem.qml index 9d7fe0a..b26fc62 100644 --- a/src/qml/TrackItem.qml +++ b/src/qml/TrackItem.qml @@ -1,6 +1,7 @@ import QtQuick 2.11 import QtQuick.Controls 2.4 import Qt.labs.settings 1.0 +import player 1.0 Action { id: trackItem @@ -10,7 +11,6 @@ Action { checked: false onTriggered: { - checked = player.getTrack(trackType) - player.setTrack(trackType, trackID) + player.playerCommand(Enums.Commands.SetTrack, [trackType, trackID]) } } diff --git a/src/qml/main.qml b/src/qml/main.qml index 81926b1..ef59877 100644 --- a/src/qml/main.qml +++ b/src/qml/main.qml @@ -115,7 +115,7 @@ ApplicationWindow { } } else { - player.appendFile(argument) + player.playerCommand(Enums.Commands.AppendFile, argument) } } } @@ -126,7 +126,7 @@ ApplicationWindow { if (val != 0) { skipto = Math.floor(controlsBar.controls.progress.to / 9 * val) } - player.seekAbsolute(skipto) + player.playerCommand(Enums.Commands.SeekAbsolute, skipto) } function isAnyMenuOpen() { @@ -190,7 +190,7 @@ ApplicationWindow { cursorShape: controlsBar.visible ? Qt.ArrowCursor : Qt.BlankCursor onClicked: { if (appearance.clickToPause) { - player.togglePlayPause() + player.playerCommand(Enums.Commands.TogglePlayPause) } } Timer {