sbAppleRemoteService.mm
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN NIGHTINGALE GPL
4 //
5 // This file is part of the Nightingale Media Player.
6 //
7 // Copyright(c) 2014
8 // http://getnightingale.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END NIGHTINGALE GPL
24 //
25 */
26 
27 #include "sbAppleRemoteService.h"
28 
30 #include "nsCOMPtr.h"
31 #include "sbIMediacoreManager.h"
32 #include "sbIMediacoreStatus.h"
33 #include "sbIApplicationController.h"
34 
35 
36 #define MAX_VOLUME 1
37 
38 @interface AppleRemoteDelegate : NSObject
39 {
41  sbAppleRemoteService *mOwner; // weak
42 }
43 
44 - (id)initWithCallback:(sbAppleRemoteService *)aCallback;
45 
46 - (void)startListening;
47 - (void)stopListening;
48 
49 @end
50 
51 
52 @implementation AppleRemoteDelegate
53 
54 - (id)initWithCallback:(sbAppleRemoteService *)aCallback
55 {
56  if ((self = [super init])) {
57  mAppleRemote = [[SBAppleRemoteListener alloc] initWithDelegate: self];
58  mOwner = aCallback;
59  }
60 
61  return self;
62 }
63 
64 - (void)dealloc
65 {
66  [mAppleRemote release];
67  [super dealloc];
68 }
69 
71 {
73 }
74 
76 {
78 }
79 
80 - (void)onButtonPressed:(EAppleRemoteButton)aButton isHold:(BOOL)aIsHold
81 {
82  // Process this event right away
83  if (!aIsHold) {
84  switch (aButton) {
85  case ePlusButton:
86  mOwner->OnVolumeUpPressed();
87  break;
88  case eMinusButton:
89  mOwner->OnVolumeDownPressed();
90  break;
91  case eMenuButton:
92  mOwner->OnMenuButtonPressed();
93  break;
94  case ePlayButton:
95  mOwner->OnPlayButtonPressed();
96  break;
97  case eRightButton:
98  mOwner->OnNextTrackReleased();
99  break;
100  case eLeftButton:
101  mOwner->OnPrevTrackReleased();
102  break;
103  }
104  }
105  else {
106  }
107 }
108 
109 - (void)onButtonReleased:(EAppleRemoteButton)aButton
110 {
111  // TODO: support this...
112 }
113 
114 @end
115 
116 
118 
119 
120 NS_IMPL_ISUPPORTS1(sbAppleRemoteService, sbIAppleRemoteService)
121 
122 sbAppleRemoteService::sbAppleRemoteService()
123 {
124  mDelegate = [[AppleRemoteDelegate alloc] initWithCallback:this];
125 }
126 
127 sbAppleRemoteService::~sbAppleRemoteService()
128 {
129  [mDelegate release];
130 }
131 
132 NS_IMETHODIMP
133 sbAppleRemoteService::StartListening()
134 {
135  [mDelegate startListening];
136  return NS_OK;
137 }
138 
139 NS_IMETHODIMP
140 sbAppleRemoteService::StopListening()
141 {
142  [mDelegate stopListening];
143  return NS_OK;
144 }
145 
146 NS_IMETHODIMP
147 sbAppleRemoteService::GetIsSupported(PRBool *aIsSupported)
148 {
149  NS_ENSURE_ARG_POINTER(aIsSupported);
150  *aIsSupported = [SBAppleRemoteListener isRemoteAvailable];
151  return NS_OK;
152 }
153 
154 NS_IMETHODIMP
155 sbAppleRemoteService::OnPlayButtonPressed()
156 {
157  nsresult rv;
158  nsCOMPtr<sbIMediacoreManager> manager =
159  do_GetService("@songbirdnest.com/Songbird/Mediacore/Manager;1", &rv);
160  NS_ENSURE_SUCCESS(rv, rv);
161 
162  nsCOMPtr<sbIMediacorePlaybackControl> playbackControl;
163  rv = manager->GetPlaybackControl(getter_AddRefs(playbackControl));
164 
165  PRBool isPlaying = PR_FALSE;
166  GetIsPlaybackPlaying(&isPlaying);
167 
168  if (NS_SUCCEEDED(rv) && playbackControl && isPlaying) {
169  nsCOMPtr<sbIMediacoreStatus> status;
170  rv = manager->GetStatus(getter_AddRefs(status));
171  NS_ENSURE_SUCCESS(rv, rv);
172 
173  PRUint32 state;
174  rv = status->GetState(&state);
175  NS_ENSURE_SUCCESS(rv, NO);
176 
177  if (state == sbIMediacoreStatus::STATUS_PLAYING ||
179  {
180  rv = playbackControl->Pause();
181  NS_ENSURE_SUCCESS(rv, rv);
182  }
183  }
184  else {
185  nsCOMPtr<sbIApplicationController> appController =
186  do_GetService("@songbirdnest.com/Songbird/ApplicationController;1", &rv);
187  NS_ENSURE_SUCCESS(rv, rv);
188 
189  rv = appController->PlayDefault();
190  NS_ENSURE_SUCCESS(rv, rv);
191  }
192 
193  return NS_OK;
194 }
195 
196 NS_IMETHODIMP
197 sbAppleRemoteService::OnMenuButtonPressed()
198 {
199  // XXXkreeger Show the library?
200  return NS_ERROR_NOT_IMPLEMENTED;
201 }
202 
203 NS_IMETHODIMP
204 sbAppleRemoteService::OnVolumeUpPressed()
205 {
206  nsresult rv;
207  nsCOMPtr<sbIMediacoreVolumeControl> volControl;
208  rv = GetVolumeControl(getter_AddRefs(volControl));
209  NS_ENSURE_SUCCESS(rv, rv);
210 
211  double curVolume;
212  rv = volControl->GetVolume(&curVolume);
213  NS_ENSURE_SUCCESS(rv, rv);
214 
215  curVolume += 0.05;
216  if (curVolume > MAX_VOLUME)
217  curVolume = MAX_VOLUME;
218 
219  return volControl->SetVolume(curVolume);
220 }
221 
222 NS_IMETHODIMP
223 sbAppleRemoteService::OnVolumeDownPressed()
224 {
225  nsresult rv;
226  nsCOMPtr<sbIMediacoreVolumeControl> volControl;
227  rv = GetVolumeControl(getter_AddRefs(volControl));
228  NS_ENSURE_SUCCESS(rv, rv);
229 
230  double curVolume;
231  rv = volControl->GetVolume(&curVolume);
232  NS_ENSURE_SUCCESS(rv, rv);
233 
234  curVolume -= 0.05;
235  if (curVolume < 0.0)
236  curVolume = 0;
237 
238  return volControl->SetVolume(curVolume);
239 }
240 
241 NS_IMETHODIMP
242 sbAppleRemoteService::OnNextTrackPressed()
243 {
244  // TODO: Support scanning...
245  return NS_ERROR_NOT_IMPLEMENTED;
246 }
247 
248 NS_IMETHODIMP
249 sbAppleRemoteService::OnNextTrackReleased()
250 {
251  nsresult rv;
252  nsCOMPtr<sbIMediacoreSequencer> sequencer;
253  rv = GetSequencer(getter_AddRefs(sequencer));
254  NS_ENSURE_SUCCESS(rv, rv);
255 
256  return sequencer->Next(false);
257 }
258 
259 NS_IMETHODIMP
260 sbAppleRemoteService::OnPrevTrackPressed()
261 {
262  return NS_ERROR_NOT_IMPLEMENTED;
263 }
264 
265 NS_IMETHODIMP
266 sbAppleRemoteService::OnPrevTrackReleased()
267 {
268  nsresult rv;
269  nsCOMPtr<sbIMediacoreSequencer> sequencer;
270  rv = GetSequencer(getter_AddRefs(sequencer));
271  NS_ENSURE_SUCCESS(rv, rv);
272 
273  return sequencer->Previous(false);
274 }
275 
276 NS_IMETHODIMP
277 sbAppleRemoteService::GetPlaybackControl(sbIMediacorePlaybackControl **aPlayCntrl)
278 {
279  nsresult rv;
280  nsCOMPtr<sbIMediacoreManager> manager =
281  do_GetService("@songbirdnest.com/Songbird/Mediacore/Manager;1", &rv);
282  NS_ENSURE_SUCCESS(rv, rv);
283 
284  return manager->GetPlaybackControl(aPlayCntrl);
285 }
286 
287 NS_IMETHODIMP
288 sbAppleRemoteService::GetSequencer(sbIMediacoreSequencer **aSequencer)
289 {
290  nsresult rv;
291  nsCOMPtr<sbIMediacoreManager> manager =
292  do_GetService("@songbirdnest.com/Songbird/Mediacore/Manager;1", &rv);
293  NS_ENSURE_SUCCESS(rv, rv);
294 
295  return manager->GetSequencer(aSequencer);
296 }
297 
298 NS_IMETHODIMP
299 sbAppleRemoteService::GetVolumeControl(sbIMediacoreVolumeControl **aVolControl)
300 {
301  nsresult rv;
302  nsCOMPtr<sbIMediacoreManager> manager =
303  do_GetService("@songbirdnest.com/Songbird/Mediacore/Manager;1", &rv);
304  NS_ENSURE_SUCCESS(rv, rv);
305 
306  return manager->GetVolumeControl(aVolControl);
307 }
308 
309 NS_IMETHODIMP
310 sbAppleRemoteService::GetIsPlaybackPlaying(PRBool *aIsPlaying)
311 {
312  NS_ENSURE_ARG_POINTER(aIsPlaying);
313 
314  nsresult rv;
315  nsCOMPtr<sbIMediacoreManager> manager =
316  do_GetService("@songbirdnest.com/Songbird/Mediacore/Manager;1", &rv);
317  NS_ENSURE_SUCCESS(rv, rv);
318 
319  nsCOMPtr<sbIMediacoreStatus> status;
320  rv = manager->GetStatus(getter_AddRefs(status));
321  NS_ENSURE_SUCCESS(rv, rv);
322 
323  PRUint32 state;
324  rv = status->GetState(&state);
325  NS_ENSURE_SUCCESS(rv, rv);
326 
327  *aIsPlaying = (state == sbIMediacoreStatus::STATUS_PLAYING ||
329 
330  return NS_OK;
331 }
332 
SBAppleRemoteListener * mAppleRemote
return NS_OK
const unsigned long STATUS_PLAYING
menuItem id
Definition: FeedWriter.js:971
NS_IMPL_ISUPPORTS1(sbDeviceCapabilitiesUtils, sbIDeviceCapabilitiesUtils) sbDeviceCapabilitiesUtils
sbAppleRemoteService * mOwner
EAppleRemoteButton
_window init
Definition: FeedWriter.js:1144
const unsigned long STATUS_BUFFERING
#define MAX_VOLUME