]> git.sesse.net Git - vlc/blob - plugins/beos/intf_beos.cpp
*** empty log message ***
[vlc] / plugins / beos / intf_beos.cpp
1 /*****************************************************************************
2  * intf_beos.cpp: beos interface
3  *****************************************************************************
4  * Copyright (C) 1999, 2000, 2001 VideoLAN
5  * $Id: intf_beos.cpp,v 1.15 2001/03/05 20:36:04 richards Exp $
6  *
7  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Tony Castley <tcastley@mail.powerup.com.au>
10  *          Richard Shepherd <richard@rshepherd.demon.co.uk>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 #define MODULE_NAME beos
28 #include "modules_inner.h"
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #include "defs.h"
34
35 #include <stdio.h>
36 #include <stdlib.h>                                      /* malloc(), free() */
37
38 #include <kernel/OS.h>
39 #include <storage/Path.h>
40 #include <Alert.h>
41 #include <View.h>
42 #include <CheckBox.h>
43 #include <Button.h>
44 #include <Slider.h>
45 #include <StatusBar.h>
46 #include <Application.h>
47 #include <Message.h>
48 #include <NodeInfo.h>
49 #include <Locker.h>
50 #include <DirectWindow.h>
51 #include <Box.h>
52 #include <MenuBar.h>
53 #include <MenuItem.h>
54 #include <FilePanel.h>
55 #include <Alert.h>
56 #include <malloc.h>
57 #include <string.h>
58
59 extern "C"
60 {
61 #include "config.h"
62 #include "common.h"
63 #include "threads.h"
64 #include "mtime.h"
65 #include "tests.h"
66 #include "modules.h"
67
68 #include "stream_control.h"
69 #include "input_ext-intf.h"
70
71 #include "interface.h"
72 #include "intf_plst.h"
73 #include "intf_msg.h"
74 #include "audio_output.h"
75 #include "MsgVals.h"
76
77
78 #include "main.h"
79 }
80
81 #include "InterfaceWindow.h"
82 #include "Bitmaps.h"
83 #include "TransportButton.h"
84
85 /*****************************************************************************
86  * intf_sys_t: description and status of FB interface
87  *****************************************************************************/
88 typedef struct intf_sys_s
89 {
90     InterfaceWindow * p_window;
91     char              i_key;
92 } intf_sys_t;
93
94 /*****************************************************************************
95  * InterfaceWindow
96  *****************************************************************************/
97  
98 InterfaceWindow::InterfaceWindow( BRect frame, const char *name , intf_thread_t  *p_interface )
99     : BWindow(frame, name, B_FLOATING_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
100         B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_WILL_ACCEPT_FIRST_CLICK |B_ASYNCHRONOUS_CONTROLS)
101 {
102     file_panel = NULL;
103     p_intf = p_interface;
104         BRect ButtonRect;
105         float xStart = 2.0;
106         float yStart = 40.0;
107
108     SetName( "interface" );
109     SetTitle(VOUT_TITLE " (BeOS interface)");
110     BRect rect(0, 0, 0, MENU_HEIGHT);
111     
112     BMenuBar *menu_bar; 
113     menu_bar = new BMenuBar(rect, "main menu");
114     AddChild( menu_bar );
115
116         BMenu *m;
117         BMenuItem *i;
118
119         menu_bar->AddItem( m = new BMenu("File") );
120         menu_bar->ResizeToPreferred();
121         m->AddItem( new BMenuItem("Open file...", new BMessage(OPEN_FILE), 'O'));
122         m->AddItem( new BMenuItem("Open DVD...", new BMessage(OPEN_DVD), 'D'));
123         m->AddSeparatorItem();
124         m->AddItem( new BMenuItem("Quit", new BMessage(B_QUIT_REQUESTED), 'Q'));
125         
126
127     rect = Bounds();
128     rect.top += menu_bar->Bounds().IntegerHeight()+1;
129
130     BBox* p_view;
131         p_view = new BBox( rect, NULL, B_FOLLOW_ALL, B_WILL_DRAW );
132         p_view->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
133     
134         /* Buttons */
135         /* Slow play */
136         ButtonRect.SetLeftTop(BPoint(xStart, yStart));
137         ButtonRect.SetRightBottom(ButtonRect.LeftTop() + kSkipButtonSize);
138         xStart += kRewindBitmapWidth;
139         TransportButton* p_slow = new TransportButton(ButtonRect, B_EMPTY_STRING,
140                                                                                         kSkipBackBitmapBits,
141                                                                                         kPressedSkipBackBitmapBits,
142                                                                                         kDisabledSkipBackBitmapBits,
143                                                                                         new BMessage(SLOWER_PLAY));
144         p_view->AddChild( p_slow );
145
146         /* Play Pause */
147         ButtonRect.SetLeftTop(BPoint(xStart, yStart));
148         ButtonRect.SetRightBottom(ButtonRect.LeftTop() + kPlayButtonSize);
149         xStart += kPlayPauseBitmapWidth + 1.0;
150         PlayPauseButton* p_play = new PlayPauseButton(ButtonRect, B_EMPTY_STRING,
151                                                                                         kPlayButtonBitmapBits,
152                                                                                         kPressedPlayButtonBitmapBits,
153                                                                                         kDisabledPlayButtonBitmapBits,
154                                                                                         kPlayingPlayButtonBitmapBits,
155                                                                                         kPressedPlayingPlayButtonBitmapBits,
156                                                                                         kPausedPlayButtonBitmapBits,
157                                                                                         kPressedPausedPlayButtonBitmapBits,
158                                                                                         new BMessage(START_PLAYBACK));
159    
160         p_view->AddChild( p_play );
161         p_play->SetPlaying();
162
163         /* Fast Foward */
164         ButtonRect.SetLeftTop(BPoint(xStart, yStart));
165         ButtonRect.SetRightBottom(ButtonRect.LeftTop() + kSkipButtonSize);
166         xStart += kRewindBitmapWidth;
167         TransportButton* p_fast = new TransportButton(ButtonRect, B_EMPTY_STRING,
168                                                                                         kSkipForwardBitmapBits,
169                                                                                         kPressedSkipForwardBitmapBits,
170                                                                                         kDisabledSkipForwardBitmapBits,
171                                                                                         new BMessage(FASTER_PLAY));
172         p_view->AddChild( p_fast );
173
174         /* Stop */
175         ButtonRect.SetLeftTop(BPoint(xStart, yStart));
176         ButtonRect.SetRightBottom(ButtonRect.LeftTop() + kStopButtonSize);
177         xStart += kStopBitmapWidth;
178         TransportButton* p_stop = new TransportButton(ButtonRect, B_EMPTY_STRING,
179                                                                                         kStopButtonBitmapBits,
180                                                                                         kPressedStopButtonBitmapBits,
181                                                                                         kDisabledStopButtonBitmapBits,
182                                                                                         new BMessage(STOP_PLAYBACK));
183         p_view->AddChild( p_stop );
184
185         ButtonRect.SetLeftTop(BPoint(xStart + 5, yStart + 6));
186         ButtonRect.SetRightBottom(ButtonRect.LeftTop() + kSpeakerButtonSize);
187         xStart += kSpeakerIconBitmapWidth;
188  
189         TransportButton* p_mute = new TransportButton(ButtonRect, B_EMPTY_STRING,
190                                                                                         kSpeakerIconBits,
191                                                                                         kPressedSpeakerIconBits,
192                                                                                         kSpeakerIconBits,
193                                                                                         new BMessage(VOLUME_MUTE));
194
195         p_view->AddChild( p_mute );
196  
197         /* Seek Status */       
198     rgb_color fill_color = {0,255,0};
199         p_seek = new SeekSlider(BRect(5,10,250,30), this, 0, 100,
200                                                 B_TRIANGLE_THUMB);
201         p_seek->SetValue(0);
202         p_seek->UseFillColor(true, &fill_color);
203     p_view->AddChild( p_seek );
204
205         /* Volume Slider */     
206         p_vol = new MediaSlider(BRect(xStart,40,250,60), new BMessage(VOLUME_CHG),
207                                                         0, VOLUME_MAX);
208         p_vol->SetValue(VOLUME_DEFAULT);
209         p_vol->UseFillColor(true, &fill_color);
210     p_view->AddChild( p_vol );
211     
212         /* Set size and Show */
213     AddChild( p_view );
214         ResizeTo(260,70 + menu_bar->Bounds().IntegerHeight()+1);
215     Show();
216 }
217
218 InterfaceWindow::~InterfaceWindow()
219 {
220 }
221
222 /*****************************************************************************
223  * InterfaceWindow::MessageReceived
224  *****************************************************************************/
225 void InterfaceWindow::MessageReceived( BMessage * p_message )
226 {
227         int vol_val = p_vol->Value();   // remember the current volume
228         static int playback_status;             // remember playback state
229         
230         BAlert *alert;
231         
232         Activate();
233     switch( p_message->what )
234     {
235     case OPEN_FILE:
236         if(file_panel)
237                 {
238                 file_panel->Show();
239                 break;
240                 }
241         file_panel = new BFilePanel();
242         file_panel->SetTarget(this);
243         file_panel->Show();
244         break;
245
246     case OPEN_DVD:
247         alert = new BAlert(VOUT_TITLE, "Opening DVDs not yet implemented", "Bummer");
248         alert->Go();
249         break;
250
251     case STOP_PLAYBACK:
252         // this currently stops playback not nicely
253                 if (p_intf->p_input != NULL )
254                 {
255                         // silence the sound, otherwise very horrible
256                         if (p_main->p_aout != NULL)
257                         {
258                                 p_main->p_aout->vol = 0;
259                         }
260                         snooze(400000);
261                         input_SetStatus(p_intf->p_input, INPUT_STATUS_END);
262                 }
263         break;
264         case START_PLAYBACK:
265                 // starts playing in normal mode
266 //              if (p_intf->p_input != NULL )
267 //              {                       
268 //                      if (p_main->p_aout != NULL)
269 //                      {
270 //                              p_main->p_aout->vol = vol_val;
271 //                      }
272 //                      snooze(400000);
273 //                      input_SetStatus(p_intf->p_input, INPUT_STATUS_PLAY);
274 //                      playback_status = PLAYING;
275 //              } 
276 //              break;
277         case PAUSE_PLAYBACK:
278                 // pause the playback
279                 if (p_intf->p_input != NULL )
280                 {
281                         // mute the volume if currently playing
282                         if (playback_status == PLAYING)
283                         {
284                                 if (p_main->p_aout != NULL)
285                                 {
286                                         p_main->p_aout->vol = 0;
287                                 }
288                                 playback_status = PAUSED;
289                         }
290                         else
291                         // restore the volume
292                         {
293                                 if (p_main->p_aout != NULL)
294                                 {
295                                         p_main->p_aout->vol = vol_val;
296                                 }
297                                 playback_status = PLAYING;
298                         }
299                         snooze(400000);
300                         input_SetStatus(p_intf->p_input, INPUT_STATUS_PAUSE);
301                 }
302                 break;
303         case FASTER_PLAY:
304                 // cycle the fast playback modes
305                 if (p_intf->p_input != NULL )
306                 {
307                         if (p_main->p_aout != NULL)
308                         {
309                                 p_main->p_aout->vol = 0;
310                         }
311                         snooze(400000);
312                         input_SetStatus(p_intf->p_input, INPUT_STATUS_FASTER);
313                 }
314                 break;
315         case SLOWER_PLAY:
316                 // cycle the slow playback modes
317                 if (p_intf->p_input != NULL )
318                 {
319                         if (p_main->p_aout != NULL)
320                         {
321                                 p_main->p_aout->vol = 0;
322                         }
323                         snooze(400000);
324                         input_SetStatus(p_intf->p_input, INPUT_STATUS_SLOWER);
325                 }
326                 break;
327         case SEEK_PLAYBACK:
328                 // handled by semaphores;
329 /*          if( p_intf->p_input != NULL )
330             {
331                 float new_position;
332                     if (p_message->FindFloat("be:value", &new_position) == B_OK)
333                     {
334                         printf("%e\n", new_position);
335                         input_Seek( p_intf->p_input, new_position * 100 );
336                     }
337             } */
338                 break;
339         case VOLUME_CHG:
340                 // adjust the volume
341         if (p_main->p_aout != NULL) 
342         {
343                         p_main->p_aout->vol = vol_val;
344                 }
345                 break;
346         case VOLUME_MUTE:
347                 // mute
348         if (p_main->p_aout != NULL) 
349             {
350                         if (p_main->p_aout->vol == 0)
351                         {
352                                 p_main->p_aout->vol = vol_val;
353                         }       
354                         else
355                         {
356                                 p_main->p_aout->vol = 0;
357                         }
358                 }
359                 break;
360         case SELECT_CHANNEL:
361                 break;
362         case B_REFS_RECEIVED:
363     case B_SIMPLE_DATA:
364         {
365             entry_ref ref;
366             if( p_message->FindRef( "refs", &ref ) == B_OK )
367             {
368                 BPath path( &ref );
369                 char * psz_name = strdup(path.Path());
370                 intf_PlstAdd( p_main->p_playlist, PLAYLIST_END, psz_name );
371             }
372
373         }
374         break;
375     default:
376         BWindow::MessageReceived( p_message );
377         break;
378     }
379 }
380
381 /*****************************************************************************
382  * InterfaceWindow::QuitRequested
383  *****************************************************************************/
384
385 bool InterfaceWindow::QuitRequested()
386 {
387     p_intf->b_die = 1;
388
389     return( false );
390 }
391 /*****************************************************************************
392  * MediaSlider
393  *****************************************************************************/
394 MediaSlider::MediaSlider(BRect frame,
395                                                 BMessage *message,
396                                                 int32 minValue,
397                                                 int32 maxValue)
398                                         :BSlider(frame, NULL, NULL, message, minValue, maxValue)
399 {
400
401 }
402
403 MediaSlider::~MediaSlider()
404 {
405
406 }
407
408 void MediaSlider::DrawThumb(void)
409 {
410         BRect r;
411         BView *v;
412
413         r = ThumbFrame();
414         v = OffscreenView();
415         v->SetHighColor(0,0,0);
416         r.InsetBy(r.IntegerWidth()/4, r.IntegerHeight()/6);
417         v->StrokeEllipse(r);
418         v->SetHighColor(ui_color(B_PANEL_BACKGROUND_COLOR));
419         r.InsetBy(1,1);
420         v->FillEllipse(r);
421 }
422
423 /*****************************************************************************
424  * SeekSlider
425  *****************************************************************************/
426 SeekSlider::SeekSlider(BRect frame,
427                                 InterfaceWindow *owner,
428                                 int32 minValue,
429                                 int32 maxValue,
430                                 thumb_style thumbType = B_TRIANGLE_THUMB)
431                         :MediaSlider(frame, NULL, minValue, maxValue)
432 {
433         fOwner = owner;
434         fMouseDown = false;
435 }
436
437 SeekSlider::~SeekSlider()
438 {
439 }
440
441 /*****************************************************************************
442  * SeekSlider::MouseDown
443  *****************************************************************************/
444 void SeekSlider::MouseDown(BPoint where)
445 {
446         BSlider::MouseDown(where);
447         fOwner->fScrubSem = create_sem(1, "Vlc::fScrubSem");
448         fMouseDown = true;                                      
449 }
450
451 /*****************************************************************************
452  * SeekSlider::MouseUp
453  *****************************************************************************/
454 void SeekSlider::MouseMoved(BPoint where, uint32 code, const BMessage *message)
455 {
456         BSlider::MouseMoved(where, code, message);
457         if (!fMouseDown)
458                 return;
459         release_sem(fOwner->fScrubSem);
460 }
461
462 /*****************************************************************************
463  * SeekSlider::MouseUp
464  *****************************************************************************/
465 void SeekSlider::MouseUp(BPoint where)
466 {
467         BSlider::MouseUp(where);
468         delete_sem(fOwner->fScrubSem);
469         fOwner->fScrubSem = B_ERROR;
470         fMouseDown = false;                                     
471 }
472         
473
474 extern "C"
475 {
476
477 /*****************************************************************************
478  * Local prototypes.
479  *****************************************************************************/
480 static int  intf_Probe     ( probedata_t *p_data );
481 static int  intf_Open      ( intf_thread_t *p_intf );
482 static void intf_Close     ( intf_thread_t *p_intf );
483 static void intf_Run       ( intf_thread_t *p_intf );
484
485 /*****************************************************************************
486  * Functions exported as capabilities. They are declared as static so that
487  * we don't pollute the namespace too much.
488  *****************************************************************************/
489 void _M( intf_getfunctions )( function_list_t * p_function_list )
490 {
491     p_function_list->pf_probe = intf_Probe;
492     p_function_list->functions.intf.pf_open  = intf_Open;
493     p_function_list->functions.intf.pf_close = intf_Close;
494     p_function_list->functions.intf.pf_run   = intf_Run;
495 }
496
497 /*****************************************************************************
498  * intf_Probe: probe the interface and return a score
499  *****************************************************************************
500  * This function tries to initialize Gnome and returns a score to the
501  * plugin manager so that it can select the best plugin.
502  *****************************************************************************/
503 static int intf_Probe( probedata_t *p_data )
504 {
505     if( TestMethod( INTF_METHOD_VAR, "beos" ) )
506     {
507         return( 999 );
508     }
509
510     return( 100 );
511 }
512
513 /*****************************************************************************
514  * intf_Open: initialize interface
515  *****************************************************************************/
516 static int intf_Open( intf_thread_t *p_intf )
517 {
518     /* Allocate instance and initialize some members */
519     p_intf->p_sys = (intf_sys_t*) malloc( sizeof( intf_sys_t ) );
520     if( p_intf->p_sys == NULL )
521     {
522         intf_ErrMsg("error: %s", strerror(ENOMEM));
523         return( 1 );
524     }
525     p_intf->p_sys->i_key = -1;
526     
527     /* Create the interface window */
528     p_intf->p_sys->p_window =
529         new InterfaceWindow( BRect( 50, 50, 400, 100 ),
530                              VOUT_TITLE " (BeOS interface)", p_intf );
531     if( p_intf->p_sys->p_window == 0 )
532     {
533         free( p_intf->p_sys );
534         intf_ErrMsg( "error: cannot allocate memory for InterfaceWindow" );
535         return( 1 );
536     }
537     
538     return( 0 );
539 }
540
541 /*****************************************************************************
542  * intf_Close: destroy dummy interface
543  *****************************************************************************/
544 static void intf_Close( intf_thread_t *p_intf )
545 {
546     /* Destroy the interface window */
547     p_intf->p_sys->p_window->Lock();
548     p_intf->p_sys->p_window->Quit();    
549
550     /* Destroy structure */
551     free( p_intf->p_sys );
552 }
553
554
555 /*****************************************************************************
556  * intf_Run: event loop
557  *****************************************************************************/
558 static void intf_Run( intf_thread_t *p_intf )
559 {
560         
561         float progress;
562         bool seekNeeded = false;
563         
564     while( !p_intf->b_die )
565     {
566
567         /* Manage core vlc functions through the callback */
568         p_intf->pf_manage( p_intf );
569
570             /* Manage the slider */
571             if( p_intf->p_input != NULL && p_intf->p_sys->p_window != NULL)
572         {
573             if (acquire_sem(p_intf->p_sys->p_window->fScrubSem) == B_OK)
574                         {
575                                 seekNeeded = true;
576                         }               
577
578                         if (seekNeeded)
579             {
580                 uint32 seekTo = (p_intf->p_sys->p_window->p_seek->Value() * 
581                                 p_intf->p_input->stream.p_selected_area->i_size) / 100;
582                                 input_Seek( p_intf->p_input, seekTo );
583                                 seekNeeded = false;             
584             }
585                         else if (p_intf->p_sys->p_window->Lock())
586             {
587                     progress = (100. * p_intf->p_input->stream.p_selected_area->i_tell) /
588                                         p_intf->p_input->stream.p_selected_area->i_size;
589                     p_intf->p_sys->p_window->p_seek->SetValue(progress);
590                     p_intf->p_sys->p_window->Unlock();
591                 }
592          }
593
594         /* Wait a bit */
595         msleep( INTF_IDLE_SLEEP );
596     }
597 }
598
599 } /* extern "C" */