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
This commit is contained in:
Michael Lotz 2010-01-26 04:41:54 +00:00
parent a387e84423
commit c5d6e9b06e
2 changed files with 160 additions and 0 deletions

View File

@ -38,6 +38,7 @@
#include <MessageRunner.h>
#include <Messenger.h>
#include <PopUpMenu.h>
#include <PropertyInfo.h>
#include <RecentItems.h>
#include <Roster.h>
#include <Screen.h>
@ -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 -

View File

@ -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(