From aadc5f572d7fa4e6d96e090ccb8087ddba45c27a Mon Sep 17 00:00:00 2001 From: Marcus Overhagen Date: Mon, 24 Apr 2006 20:47:29 +0000 Subject: [PATCH] added playlist support git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@17225 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/mediaplayer/Controller.cpp | 23 ++++++ src/apps/mediaplayer/Controller.h | 5 ++ src/apps/mediaplayer/ControllerView.cpp | 22 +++++- src/apps/mediaplayer/ControllerView.h | 6 +- src/apps/mediaplayer/MainWin.cpp | 30 ++++---- src/apps/mediaplayer/MainWin.h | 9 ++- src/apps/mediaplayer/Player.h | 10 +++ src/apps/mediaplayer/Playlist.cpp | 97 +++++++++++++++++++++++++ src/apps/mediaplayer/Playlist.h | 50 +++++++++++++ 9 files changed, 234 insertions(+), 18 deletions(-) create mode 100644 src/apps/mediaplayer/Player.h create mode 100644 src/apps/mediaplayer/Playlist.cpp create mode 100644 src/apps/mediaplayer/Playlist.h diff --git a/src/apps/mediaplayer/Controller.cpp b/src/apps/mediaplayer/Controller.cpp index f1b266708a..558f34c61a 100644 --- a/src/apps/mediaplayer/Controller.cpp +++ b/src/apps/mediaplayer/Controller.cpp @@ -112,3 +112,26 @@ Controller::Bitmap() } +void +Controller::Stop() +{ +} + + +void +Controller::Play() +{ +} + + +void +Controller::Pause() +{ +} + + +bool +Controller::IsPaused() +{ + return false; +} diff --git a/src/apps/mediaplayer/Controller.h b/src/apps/mediaplayer/Controller.h index ead73e4c81..72831686d4 100644 --- a/src/apps/mediaplayer/Controller.h +++ b/src/apps/mediaplayer/Controller.h @@ -33,6 +33,11 @@ public: status_t SetTo(const entry_ref &ref); + void Stop(); + void Play(); + void Pause(); + bool IsPaused(); + void SetVideoView(VideoView *view); bool IsOverlayActive(); diff --git a/src/apps/mediaplayer/ControllerView.cpp b/src/apps/mediaplayer/ControllerView.cpp index 6dc64bf4f2..f800f08249 100644 --- a/src/apps/mediaplayer/ControllerView.cpp +++ b/src/apps/mediaplayer/ControllerView.cpp @@ -22,10 +22,15 @@ #include #include "ControllerView.h" +#include "Controller.h" +#include "Playlist.h" +#include "Player.h" -ControllerView::ControllerView(BRect frame, Controller *ctrl) +ControllerView::ControllerView(BRect frame, Controller *ctrl, Playlist *pl, Player *p) : TransportControlGroup(frame) , fController(ctrl) + , fPlaylist(pl) + , fPlayer(p) { } @@ -72,6 +77,10 @@ void ControllerView::TogglePlaying() { printf("ControllerView::TogglePlaying()\n"); + if (fController->IsPaused()) + fController->Play(); + else + fController->Pause(); } @@ -79,6 +88,7 @@ void ControllerView::Stop() { printf("ControllerView::Stop()\n"); + fController->Stop(); } @@ -102,6 +112,11 @@ void ControllerView::SkipBackward() { printf("ControllerView::SkipBackward()\n"); + entry_ref ref; + if (fPlaylist->PrevRef(&ref) == B_OK) { + printf("prev ref: %s\n", ref.name); + fPlayer->OpenFile(ref); + } } @@ -109,6 +124,11 @@ void ControllerView::SkipForward() { printf("ControllerView::SkipForward()\n"); + entry_ref ref; + if (fPlaylist->NextRef(&ref) == B_OK) { + printf("next ref: %s\n", ref.name); + fPlayer->OpenFile(ref); + } } diff --git a/src/apps/mediaplayer/ControllerView.h b/src/apps/mediaplayer/ControllerView.h index db2f8270c7..36d4ef59a9 100644 --- a/src/apps/mediaplayer/ControllerView.h +++ b/src/apps/mediaplayer/ControllerView.h @@ -25,11 +25,13 @@ class Controller; +class Playlist; +class Player; class ControllerView : public TransportControlGroup { public: - ControllerView(BRect frame, Controller *ctrl); + ControllerView(BRect frame, Controller *ctrl, Playlist *pl, Player *p); ~ControllerView(); private: @@ -50,6 +52,8 @@ private: private: Controller * fController; + Playlist * fPlaylist; + Player * fPlayer; }; #endif diff --git a/src/apps/mediaplayer/MainWin.cpp b/src/apps/mediaplayer/MainWin.cpp index d626e849fc..8b37e2add7 100644 --- a/src/apps/mediaplayer/MainWin.cpp +++ b/src/apps/mediaplayer/MainWin.cpp @@ -89,6 +89,7 @@ MainWin::MainWin() , fFilePanel(NULL) , fHasFile(false) , fHasVideo(false) + , fPlaylist(new Playlist) , fController(new Controller) , fIsFullscreen(false) , fKeepAspectRatio(true) @@ -128,12 +129,12 @@ MainWin::MainWin() // controls rect = BRect(0, fMenuBarHeight + 11, fBackground->Bounds().right, fBackground->Bounds().bottom); - fControls = new ControllerView(rect, fController); + fControls = new ControllerView(rect, fController, fPlaylist, this); fBackground->AddChild(fControls); fControls->ResizeToPreferred(); fControlsHeight = (int)fControls->Frame().Height() + 1; fControlsWidth = (int)fControls->Frame().Width() + 1; - fControls->SetResizingMode(B_FOLLOW_NONE); + fControls->SetResizingMode(B_FOLLOW_BOTTOM | B_FOLLOW_LEFT_RIGHT); // fControls->MoveTo(0, fBackground->Bounds().bottom - fControlsHeight + 1); // fVideoView->ResizeTo(fBackground->Bounds().Width(), fBackground->Bounds().Height() - fMenuBarHeight - fControlsHeight); @@ -154,6 +155,7 @@ MainWin::MainWin() MainWin::~MainWin() { printf("MainWin::~MainWin\n"); + delete fPlaylist; delete fController; delete fFilePanel; } @@ -752,25 +754,25 @@ void MainWin::RefsReceived(BMessage *msg) { printf("MainWin::RefsReceived\n"); - // the first file is played in this window, - // if additional files (refs) are included, - // more windows are launched + + // the playlist ist replaced by dropped files + + fPlaylist->MakeEmpty(); entry_ref ref; - int n = 0; for (int i = 0; B_OK == msg->FindRef("refs", i, &ref); i++) { BEntry entry(&ref, true); if (!entry.Exists() || !entry.IsFile()) continue; - if (n == 0) { - OpenFile(ref); - } else { - BMessage m(B_REFS_RECEIVED); - m.AddRef("refs", &ref); - gMainApp->NewWindow()->PostMessage(&m); - } - n++; + fPlaylist->AddRef(ref); } + + fPlaylist->Sort(); + + // open first file + if (fPlaylist->NextRef(&ref) == B_OK) + OpenFile(ref); + } diff --git a/src/apps/mediaplayer/MainWin.h b/src/apps/mediaplayer/MainWin.h index df4a0f54cb..4e5b106c8a 100644 --- a/src/apps/mediaplayer/MainWin.h +++ b/src/apps/mediaplayer/MainWin.h @@ -28,8 +28,10 @@ #include "Controller.h" #include "ControllerView.h" #include "VideoView.h" +#include "Player.h" +#include "Playlist.h" -class MainWin : public BWindow +class MainWin : public BWindow, public Player { public: MainWin(); @@ -48,7 +50,6 @@ public: status_t KeyDown(BMessage *msg); void CreateMenu(); - void OpenFile(const entry_ref &ref); void SetupWindow(); void SetupTrackMenus(); void SetWindowSizeLimits(); @@ -56,6 +57,9 @@ public: void ResizeVideoView(int x, int y, int width, int height); void ShowFileInfo(); + + // from Player + void OpenFile(const entry_ref &ref); void VideoFormatChange(int width, int height, float width_scale, float height_scale); @@ -85,6 +89,7 @@ public: bool fHasFile; bool fHasVideo; + Playlist * fPlaylist; Controller * fController; volatile bool fIsFullscreen; volatile bool fKeepAspectRatio; diff --git a/src/apps/mediaplayer/Player.h b/src/apps/mediaplayer/Player.h new file mode 100644 index 0000000000..f88e8106b9 --- /dev/null +++ b/src/apps/mediaplayer/Player.h @@ -0,0 +1,10 @@ +#ifndef __PLAYER_H +#define __PLAYER_H + +class Player +{ +public: + virtual void OpenFile(const entry_ref &ref) = 0; +}; + +#endif diff --git a/src/apps/mediaplayer/Playlist.cpp b/src/apps/mediaplayer/Playlist.cpp new file mode 100644 index 0000000000..2cd2b4627c --- /dev/null +++ b/src/apps/mediaplayer/Playlist.cpp @@ -0,0 +1,97 @@ +/* + * Playlist.cpp - Media Player for the Haiku Operating System + * + * Copyright (C) 2006 Marcus Overhagen + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +#include "Playlist.h" + +// TODO: using BList for objects is bad, replace it with a template + +Playlist::Playlist() + : fList() + , fCurrentIndex(-1) +{ +} + + +Playlist::~Playlist() +{ + MakeEmpty(); +} + + +void +Playlist::MakeEmpty() +{ + int count = fList.CountItems(); + for (int i = count - 1; i >= 0; i--) { + entry_ref *ref = (entry_ref *)fList.RemoveItem(i); + delete ref; + } + fCurrentIndex = -1; +} + + +int +Playlist::CountItems() +{ + return fList.CountItems(); +} + + +int +Playlist::playlist_cmp(const void *p1, const void *p2) +{ + return strcmp((*(const entry_ref **)p1)->name, (*(const entry_ref **)p2)->name); +} + + +void +Playlist::Sort() +{ + fList.SortItems(playlist_cmp); +} + + +void +Playlist::AddRef(const entry_ref &ref) +{ + fList.AddItem(new entry_ref(ref)); +} + + +status_t +Playlist::NextRef(entry_ref *ref) +{ + int count = fList.CountItems(); + if (fCurrentIndex + 2 > count) + return B_ERROR; + *ref = *(entry_ref *)fList.ItemAt(++fCurrentIndex); + return B_OK; +} + + +status_t +Playlist::PrevRef(entry_ref *ref) +{ + int count = fList.CountItems(); + if (count == 0 || fCurrentIndex <= 0) + return B_ERROR; + *ref = *(entry_ref *)fList.ItemAt(--fCurrentIndex); + return B_OK; +} + diff --git a/src/apps/mediaplayer/Playlist.h b/src/apps/mediaplayer/Playlist.h new file mode 100644 index 0000000000..472e610033 --- /dev/null +++ b/src/apps/mediaplayer/Playlist.h @@ -0,0 +1,50 @@ +/* + * Playlist.h - Media Player for the Haiku Operating System + * + * Copyright (C) 2006 Marcus Overhagen + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +#ifndef __PLAYLIST_H +#define __PLAYLIST_H + +#include +#include + +class Playlist +{ +public: + Playlist(); + ~Playlist(); + + void MakeEmpty(); + int CountItems(); + + void Sort(); + + void AddRef(const entry_ref &ref); + + status_t NextRef(entry_ref *ref); + status_t PrevRef(entry_ref *ref); + +private: + static int playlist_cmp(const void *p1, const void *p2); + +private: + BList fList; + int fCurrentIndex; +}; + +#endif