From c5d6e9b06e808c440e867b529ac1e08391d9a3f7 Mon Sep 17 00:00:00 2001 From: Michael Lotz Date: Tue, 26 Jan 2010 04:41:54 +0000 Subject: [PATCH] Add some scripting to the MediaPlayer so it can be remote controlled. Example usage to skip one track forward: hey MediaPlayer Do Next of Window 0 git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35294 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/mediaplayer/MainWin.cpp | 155 +++++++++++++++++++++++++++++++ src/apps/mediaplayer/MainWin.h | 5 + 2 files changed, 160 insertions(+) diff --git a/src/apps/mediaplayer/MainWin.cpp b/src/apps/mediaplayer/MainWin.cpp index d131015909..e661ce9afe 100644 --- a/src/apps/mediaplayer/MainWin.cpp +++ b/src/apps/mediaplayer/MainWin.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -103,6 +104,37 @@ enum { M_SHOW_IF_NEEDED }; + +static property_info sPropertyInfo[] = { + { "Next", { B_EXECUTE_PROPERTY }, + { B_DIRECT_SPECIFIER, 0 }, "Skip to the next track.", 0 + }, + { "Prev", { B_EXECUTE_PROPERTY }, + { B_DIRECT_SPECIFIER, 0 }, "Skip to the previous track.", 0 + }, + { "Play", { B_EXECUTE_PROPERTY }, + { B_DIRECT_SPECIFIER, 0 }, "Start playing.", 0 + }, + { "Stop", { B_EXECUTE_PROPERTY }, + { B_DIRECT_SPECIFIER, 0 }, "Stop playing.", 0 + }, + { "Pause", { B_EXECUTE_PROPERTY }, + { B_DIRECT_SPECIFIER, 0 }, "Pause playback.", 0 + }, + { "TogglePlaying", { B_EXECUTE_PROPERTY }, + { B_DIRECT_SPECIFIER, 0 }, "Toggle pause/play.", 0 + }, + { "Mute", { B_EXECUTE_PROPERTY }, + { B_DIRECT_SPECIFIER, 0 }, "Toggle mute.", 0 + }, + { "Volume", { B_GET_PROPERTY, B_SET_PROPERTY, 0 }, + { B_DIRECT_SPECIFIER, 0 }, "Gets/sets the volume (0.0-2.0).", 0, + { B_FLOAT_TYPE } + }, + { 0, { 0 }, { 0 }, 0, 0 } +}; + + //#define printf(a...) @@ -378,6 +410,89 @@ MainWin::MessageReceived(BMessage* msg) { // msg->PrintToStream(); switch (msg->what) { + case B_EXECUTE_PROPERTY: + case B_GET_PROPERTY: + case B_SET_PROPERTY: + { + BMessage reply(B_REPLY); + status_t result = B_BAD_SCRIPT_SYNTAX; + int32 index; + BMessage specifier; + int32 what; + const char* property; + + if (msg->GetCurrentSpecifier(&index, &specifier, &what, + &property) != B_OK) { + return BWindow::MessageReceived(msg); + } + + BPropertyInfo propertyInfo(sPropertyInfo); + switch (propertyInfo.FindMatch(msg, index, &specifier, what, + property)) { + case 0: + fControls->SkipForward(); + result = B_OK; + break; + + case 1: + fControls->SkipBackward(); + result = B_OK; + break; + + case 2: + fController->Play(); + result = B_OK; + break; + + case 3: + fController->Stop(); + result = B_OK; + break; + + case 4: + fController->Pause(); + result = B_OK; + break; + + case 5: + fController->TogglePlaying(); + result = B_OK; + break; + + case 6: + fController->ToggleMute(); + result = B_OK; + break; + + case 7: + { + if (msg->what == B_GET_PROPERTY) { + result = reply.AddFloat("result", + fController->Volume()); + } else if (msg->what == B_SET_PROPERTY) { + float newVolume; + result = msg->FindFloat("data", &newVolume); + if (result == B_OK) + fController->SetVolume(newVolume); + } + + break; + } + + default: + return BWindow::MessageReceived(msg); + } + + if (result != B_OK) { + reply.what = B_MESSAGE_NOT_UNDERSTOOD; + reply.AddString("message", strerror(result)); + reply.AddInt32("error", result); + } + + msg->SendReply(&reply); + break; + } + case B_REFS_RECEIVED: printf("MainWin::MessageReceived: B_REFS_RECEIVED\n"); _RefsReceived(msg); @@ -934,6 +1049,46 @@ MainWin::GetQuitMessage(BMessage* message) } +BHandler* +MainWin::ResolveSpecifier(BMessage* message, int32 index, BMessage* specifier, + int32 what, const char* property) +{ + BPropertyInfo propertyInfo(sPropertyInfo); + switch (propertyInfo.FindMatch(message, index, specifier, what, property)) { + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + case 7: + return this; + } + + return BWindow::ResolveSpecifier(message, index, specifier, what, property); +} + + +status_t +MainWin::GetSupportedSuites(BMessage* data) +{ + if (data == NULL) + return B_BAD_VALUE; + + status_t status = data->AddString("suites", "suite/vnd.Haiku-MediaPlayer"); + if (status != B_OK) + return status; + + BPropertyInfo propertyInfo(sPropertyInfo); + status = data->AddFlat("messages", &propertyInfo); + if (status != B_OK) + return status; + + return BWindow::GetSupportedSuites(data); +} + + // #pragma mark - diff --git a/src/apps/mediaplayer/MainWin.h b/src/apps/mediaplayer/MainWin.h index 4d8b76bfdf..870a7a406a 100644 --- a/src/apps/mediaplayer/MainWin.h +++ b/src/apps/mediaplayer/MainWin.h @@ -74,6 +74,11 @@ public: void GetQuitMessage(BMessage* message); + virtual BHandler* ResolveSpecifier(BMessage* message, int32 index, + BMessage* specifier, int32 what, + const char* property); + virtual status_t GetSupportedSuites(BMessage* data); + private: void _RefsReceived(BMessage* message); void _PlaylistItemOpened(