xref: /haiku/src/apps/mediaplayer/ControllerView.cpp (revision f2b4344867e97c3f4e742a1b4a15e6879644601a)
1 /*
2  * Controller.cpp - Media Player for the Haiku Operating System
3  *
4  * Copyright (C) 2006 Marcus Overhagen <marcus@overhagen.de>
5  * Copyright (C) 2007 Stephan Aßmus <superstippi@gmx.de>
6  * Copyright (C) 2008-2009 Fredrik Modéen 	<[FirstName]@[LastName].se> (MIT ok)
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  *
21  */
22 
23 
24 #include "ControllerView.h"
25 
26 #include <Autolock.h>
27 #include <Message.h>
28 #include <stdio.h>
29 #include <string.h>
30 
31 #include "Controller.h"
32 #include "Playlist.h"
33 #include "PlaylistObserver.h"
34 
35 
36 ControllerView::ControllerView(BRect frame, Controller* controller,
37 		Playlist* playlist)
38 	:
39 	TransportControlGroup(frame, true, true, false),
40 	fController(controller),
41 	fPlaylist(playlist),
42 	fPlaylistObserver(new PlaylistObserver(this))
43 {
44 	fPlaylist->AddListener(fPlaylistObserver);
45 }
46 
47 
48 ControllerView::~ControllerView()
49 {
50 	fPlaylist->RemoveListener(fPlaylistObserver);
51 	delete fPlaylistObserver;
52 }
53 
54 
55 void
56 ControllerView::AttachedToWindow()
57 {
58 	TransportControlGroup::AttachedToWindow();
59 }
60 
61 
62 void
63 ControllerView::Draw(BRect updateRect)
64 {
65 	TransportControlGroup::Draw(updateRect);
66 }
67 
68 
69 void
70 ControllerView::MessageReceived(BMessage* message)
71 {
72 	switch (message->what) {
73 		case MSG_PLAYLIST_ITEM_ADDED:
74 		case MSG_PLAYLIST_ITEM_REMOVED:
75 		case MSG_PLAYLIST_ITEMS_SORTED:
76 		case MSG_PLAYLIST_CURRENT_ITEM_CHANGED:
77 			_CheckSkippable();
78 			break;
79 
80 		case MSG_PLAYLIST_IMPORT_FAILED:
81 			break;
82 
83 		default:
84 			TransportControlGroup::MessageReceived(message);
85 	}
86 }
87 
88 
89 // #pragma mark -
90 
91 
92 uint32
93 ControllerView::EnabledButtons()
94 {
95 	// TODO: superflous
96 	return 0xffffffff;
97 }
98 
99 
100 void
101 ControllerView::TogglePlaying()
102 {
103 	BAutolock _(fPlaylist);
104 	if (fPlaylist->CurrentItemIndex() == fPlaylist->CountItems() - 1
105 		&& Position() == 1.0) {
106 		// Reached end of playlist and end of last item
107 		// -> start again from the first item.
108 		fPlaylist->SetCurrentItemIndex(0, true);
109 	} else
110 		fController->TogglePlaying();
111 }
112 
113 
114 void
115 ControllerView::Stop()
116 {
117 	fController->Stop();
118 }
119 
120 
121 void
122 ControllerView::Rewind()
123 {
124 	// TODO: make it so this function is called repeatedly
125 	//printf("ControllerView::Rewind()\n");
126 }
127 
128 
129 void
130 ControllerView::Forward()
131 {
132 	// TODO: make it so this function is called repeatedly
133 	//printf("ControllerView::Forward()\n");
134 }
135 
136 
137 void
138 ControllerView::SkipBackward()
139 {
140 	BAutolock _(fPlaylist);
141 	int32 index = fPlaylist->CurrentItemIndex() - 1;
142 	if (index < 0)
143 		index = 0;
144 	fPlaylist->SetCurrentItemIndex(index, true);
145 }
146 
147 
148 void
149 ControllerView::SkipForward()
150 {
151 	BAutolock _(fPlaylist);
152 	int32 index = fPlaylist->CurrentItemIndex() + 1;
153 	if (index >= fPlaylist->CountItems())
154 		index = fPlaylist->CountItems() - 1;
155 	fPlaylist->SetCurrentItemIndex(index, true);
156 }
157 
158 
159 void
160 ControllerView::VolumeChanged(float value)
161 {
162 	fController->SetVolume(value);
163 }
164 
165 
166 void
167 ControllerView::ToggleMute()
168 {
169 	fController->ToggleMute();
170 }
171 
172 
173 void
174 ControllerView::PositionChanged(float value)
175 {
176 	// 0.0 ... 1.0
177 	fController->SetPosition(value);
178 }
179 
180 
181 // #pragma mark -
182 
183 
184 void
185 ControllerView::_CheckSkippable()
186 {
187 	BAutolock _(fPlaylist);
188 
189 	bool canSkipNext, canSkipPrevious;
190 	fPlaylist->GetSkipInfo(&canSkipPrevious, &canSkipNext);
191 	SetSkippable(canSkipPrevious, canSkipNext);
192 }
193