]> 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.16 2001/03/05 22:29:02 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 <Alert.h>
53 #include <MenuBar.h>
54 #include <MenuItem.h>
55 #include <FilePanel.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, 0);
111     
112     BMenuBar *menu_bar; 
113     menu_bar = new BMenuBar(rect, "main menu");
114     AddChild( menu_bar );
115
116         BMenu *m;
117
118         menu_bar->AddItem( m = new BMenu("File") );
119         menu_bar->ResizeToPreferred();
120         m->AddItem( new BMenuItem("Open file...", new BMessage(OPEN_FILE), 'O'));
121         m->AddItem( new BMenuItem("Open DVD...", new BMessage(OPEN_DVD), 'D'));
122         m->AddSeparatorItem();
123         m->AddItem( new BMenuItem("Quit", new BMessage(B_QUIT_REQUESTED), 'Q'));
124         
125
126     rect = Bounds();
127     rect.top += menu_bar->Bounds().IntegerHeight()+1;
128
129     BBox* p_view;
130         p_view = new BBox( rect, NULL, B_FOLLOW_ALL, B_WILL_DRAW );
131         p_view->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
132     
133         /* Buttons */
134         /* Slow play */
135         ButtonRect.SetLeftTop(BPoint(xStart, yStart));
136         ButtonRect.SetRightBottom(ButtonRect.LeftTop() + kSkipButtonSize);
137         xStart += kRewindBitmapWidth;
138         TransportButton* p_slow = new TransportButton(ButtonRect, B_EMPTY_STRING,
139                                                                                         kSkipBackBitmapBits,
140                                                                                         kPressedSkipBackBitmapBits,
141                                                                                         kDisabledSkipBackBitmapBits,
142                                                                                         new BMessage(SLOWER_PLAY));
143         p_view->AddChild( p_slow );
144
145         /* Play Pause */
146         ButtonRect.SetLeftTop(BPoint(xStart, yStart));
147         ButtonRect.SetRightBottom(ButtonRect.LeftTop() + kPlayButtonSize);
148         xStart += kPlayPauseBitmapWidth + 1.0;
149         PlayPauseButton* p_play = new PlayPauseButton(ButtonRect, B_EMPTY_STRING,
150                                                                                         kPlayButtonBitmapBits,
151                                                                                         kPressedPlayButtonBitmapBits,
152                                                                                         kDisabledPlayButtonBitmapBits,
153                                                                                         kPlayingPlayButtonBitmapBits,
154                                                                                         kPressedPlayingPlayButtonBitmapBits,
155                                                                                         kPausedPlayButtonBitmapBits,
156                                                                                         kPressedPausedPlayButtonBitmapBits,
157                                                                                         new BMessage(START_PLAYBACK));
158    
159         p_view->AddChild( p_play );
160         p_play->SetPlaying();
161
162         /* Fast Foward */
163         ButtonRect.SetLeftTop(BPoint(xStart, yStart));
164         ButtonRect.SetRightBottom(ButtonRect.LeftTop() + kSkipButtonSize);
165         xStart += kRewindBitmapWidth;
166         TransportButton* p_fast = new TransportButton(ButtonRect, B_EMPTY_STRING,
167                                                                                         kSkipForwardBitmapBits,
168                                                                                         kPressedSkipForwardBitmapBits,
169                                                                                         kDisabledSkipForwardBitmapBits,
170                                                                                         new BMessage(FASTER_PLAY));
171         p_view->AddChild( p_fast );
172
173         /* Stop */
174         ButtonRect.SetLeftTop(BPoint(xStart, yStart));
175         ButtonRect.SetRightBottom(ButtonRect.LeftTop() + kStopButtonSize);
176         xStart += kStopBitmapWidth;
177         TransportButton* p_stop = new TransportButton(ButtonRect, B_EMPTY_STRING,
178                                                                                         kStopButtonBitmapBits,
179                                                                                         kPressedStopButtonBitmapBits,
180                                                                                         kDisabledStopButtonBitmapBits,
181                                                                                         new BMessage(STOP_PLAYBACK));
182         p_view->AddChild( p_stop );
183
184         ButtonRect.SetLeftTop(BPoint(xStart + 5, yStart + 6));
185         ButtonRect.SetRightBottom(ButtonRect.LeftTop() + kSpeakerButtonSize);
186         xStart += kSpeakerIconBitmapWidth;
187  
188         TransportButton* p_mute = new TransportButton(ButtonRect, B_EMPTY_STRING,
189                                                                                         kSpeakerIconBits,
190                                                                                         kPressedSpeakerIconBits,
191                                                                                         kSpeakerIconBits,
192                                                                                         new BMessage(VOLUME_MUTE));
193
194         p_view->AddChild( p_mute );
195  
196         /* Seek Status */       
197     rgb_color fill_color = {0,255,0};
198         p_seek = new SeekSlider(BRect(5,10,250,30), this, 0, 100,
199                                                 B_TRIANGLE_THUMB);
200         p_seek->SetValue(0);
201         p_seek->UseFillColor(true, &fill_color);
202     p_view->AddChild( p_seek );
203
204         /* Volume Slider */     
205         p_vol = new MediaSlider(BRect(xStart,40,250,60), new BMessage(VOLUME_CHG),
206                                                         0, VOLUME_MAX);
207         p_vol->SetValue(VOLUME_DEFAULT);
208         p_vol->UseFillColor(true, &fill_color);
209     p_view->AddChild( p_vol );
210     
211         /* Set size and Show */
212     AddChild( p_view );
213         ResizeTo(260,70 + menu_bar->Bounds().IntegerHeight()+1);
214     Show();
215 }
216
217 InterfaceWindow::~InterfaceWindow()
218 {
219 }
220
221 /*****************************************************************************
222  * InterfaceWindow::MessageReceived
223  *****************************************************************************/
224 void InterfaceWindow::MessageReceived( BMessage * p_message )
225 {
226         int vol_val = p_vol->Value();   // remember the current volume
227         static int playback_status;             // remember playback state
228         BAlert *alert;
229         
230         Activate();
231     switch( p_message->what )
232     {
233     case OPEN_FILE:
234         if(file_panel)
235                 {
236                 file_panel->Show();
237                 break;
238                 }
239         file_panel = new BFilePanel();
240         file_panel->SetTarget(this);
241         file_panel->Show();
242         break;
243
244     case OPEN_DVD:
245             alert = new BAlert(VOUT_TITLE, "Opening DVD not yet supported", "Bummer");
246             alert->Go();
247             //intf_PlstAdd( p_main->p_playlist, PLAYLIST_END, "dvd:/dev/disk/ide/atapi/1/master/0/raw" );
248         break;
249
250     case STOP_PLAYBACK:
251         // this currently stops playback not nicely
252                 if (p_intf->p_input != NULL )
253                 {
254                         // silence the sound, otherwise very horrible
255                         if (p_main->p_aout != NULL)
256                         {
257                                 p_main->p_aout->vol = 0;
258                         }
259                         snooze(400000);
260                         input_SetStatus(p_intf->p_input, INPUT_STATUS_END);
261                 }
262         break;
263         case START_PLAYBACK:
264                 // starts playing in normal mode
265 //              if (p_intf->p_input != NULL )
266 //              {                       
267 //                      if (p_main->p_aout != NULL)
268 //                      {
269 //                              p_main->p_aout->vol = vol_val;
270 //                      }
271 //                      snooze(400000);
272 //                      input_SetStatus(p_intf->p_input, INPUT_STATUS_PLAY);
273 //                      playback_status = PLAYING;
274 //              } 
275 //              break;
276         case PAUSE_PLAYBACK:
277                 // pause the playback
278                 if (p_intf->p_input != NULL )
279                 {
280                         // mute the volume if currently playing
281                         if (playback_status == PLAYING)
282                         {
283                                 if (p_main->p_aout != NULL)
284                                 {
285                                         p_main->p_aout->vol = 0;
286                                 }
287                                 playback_status = PAUSED;
288                         }
289                         else
290                         // restore the volume
291                         {
292                                 if (p_main->p_aout != NULL)
293                                 {
294                                         p_main->p_aout->vol = vol_val;
295                                 }
296                                 playback_status = PLAYING;
297                         }
298                         snooze(400000);
299                         input_SetStatus(p_intf->p_input, INPUT_STATUS_PAUSE);
300                 }
301                 break;
302         case FASTER_PLAY:
303                 // cycle the fast playback modes
304                 if (p_intf->p_input != NULL )
305                 {
306                         if (p_main->p_aout != NULL)
307                         {
308                                 p_main->p_aout->vol = 0;
309                         }
310                         snooze(400000);
311                         input_SetStatus(p_intf->p_input, INPUT_STATUS_FASTER);
312                 }
313                 break;
314         case SLOWER_PLAY:
315                 // cycle the slow playback modes
316                 if (p_intf->p_input != NULL )
317                 {
318                         if (p_main->p_aout != NULL)
319                         {
320                                 p_main->p_aout->vol = 0;
321                         }
322                         snooze(400000);
323                         input_SetStatus(p_intf->p_input, INPUT_STATUS_SLOWER);
324                 }
325                 break;
326         case SEEK_PLAYBACK:
327                 // handled by semaphores;
328 /*          if( p_intf->p_input != NULL )
329             {
330                 float new_position;
331                     if (p_message->FindFloat("be:value", &new_position) == B_OK)
332                     {
333                         printf("%e\n", new_position);
334                         input_Seek( p_intf->p_input, new_position * 100 );
335                     }
336             } */
337                 break;
338         case VOLUME_CHG:
339                 // adjust the volume
340         if (p_main->p_aout != NULL) 
341         {
342                         p_main->p_aout->vol = vol_val;
343                 }
344                 break;
345         case VOLUME_MUTE:
346                 // mute
347         if (p_main->p_aout != NULL) 
348             {
349                         if (p_main->p_aout->vol == 0)
350                         {
351                                 p_vol->SetEnabled(true);
352                                 p_main->p_aout->vol = vol_val;
353                         }       
354                         else
355                         {
356                                 p_vol->SetEnabled(false);
357                                 p_main->p_aout->vol = 0;
358                         }
359                 }
360                 break;
361         case SELECT_CHANNEL:
362                 break;
363         case B_REFS_RECEIVED:
364     case B_SIMPLE_DATA:
365         {
366             entry_ref ref;
367             if( p_message->FindRef( "refs", &ref ) == B_OK )
368             {
369                 BPath path( &ref );
370                 char * psz_name = strdup(path.Path());
371                 intf_PlstAdd( p_main->p_playlist, PLAYLIST_END, psz_name );
372             }
373
374         }
375         break;
376     default:
377         BWindow::MessageReceived( p_message );
378         break;
379     }
380 }
381
382 /*****************************************************************************
383  * InterfaceWindow::QuitRequested
384  *****************************************************************************/
385
386 bool InterfaceWindow::QuitRequested()
387 {
388     p_intf->b_die = 1;
389
390     return( false );
391 }
392 /*****************************************************************************
393  * MediaSlider
394  *****************************************************************************/
395 MediaSlider::MediaSlider(BRect frame,
396                                                 BMessage *message,
397                                                 int32 minValue,
398                                                 int32 maxValue)
399                                         :BSlider(frame, NULL, NULL, message, minValue, maxValue)
400 {
401
402 }
403
404 MediaSlider::~MediaSlider()
405 {
406
407 }
408
409 void MediaSlider::DrawThumb(void)
410 {
411         BRect r;
412         BView *v;
413
414         r = ThumbFrame();
415         v = OffscreenView();
416         v->SetHighColor(0,0,0);
417         r.InsetBy(r.IntegerWidth()/4, r.IntegerHeight()/6);
418         v->StrokeEllipse(r);
419         v->SetHighColor(ui_color(B_PANEL_BACKGROUND_COLOR));
420         r.InsetBy(1,1);
421         v->FillEllipse(r);
422 }
423
424 /*****************************************************************************
425  * SeekSlider
426  *****************************************************************************/
427 SeekSlider::SeekSlider(BRect frame,
428                                 InterfaceWindow *owner,
429                                 int32 minValue,
430                                 int32 maxValue,
431                                 thumb_style thumbType = B_TRIANGLE_THUMB)
432                         :MediaSlider(frame, NULL, minValue, maxValue)
433 {
434         fOwner = owner;
435         fMouseDown = false;
436 }
437
438 SeekSlider::~SeekSlider()
439 {
440 }
441
442 /*****************************************************************************
443  * SeekSlider::MouseDown
444  *****************************************************************************/
445 void SeekSlider::MouseDown(BPoint where)
446 {
447         BSlider::MouseDown(where);
448         fOwner->fScrubSem = create_sem(1, "Vlc::fScrubSem");
449         fMouseDown = true;                                      
450 }
451
452 /*****************************************************************************
453  * SeekSlider::MouseUp
454  *****************************************************************************/
455 void SeekSlider::MouseMoved(BPoint where, uint32 code, const BMessage *message)
456 {
457         BSlider::MouseMoved(where, code, message);
458         if (!fMouseDown)
459                 return;
460         release_sem(fOwner->fScrubSem);
461 }
462
463 /*****************************************************************************
464  * SeekSlider::MouseUp
465  *****************************************************************************/
466 void SeekSlider::MouseUp(BPoint where)
467 {
468         BSlider::MouseUp(where);
469         delete_sem(fOwner->fScrubSem);
470         fOwner->fScrubSem = B_ERROR;
471         fMouseDown = false;                                     
472 }
473         
474
475 extern "C"
476 {
477
478 /*****************************************************************************
479  * Local prototypes.
480  *****************************************************************************/
481 static int  intf_Probe     ( probedata_t *p_data );
482 static int  intf_Open      ( intf_thread_t *p_intf );
483 static void intf_Close     ( intf_thread_t *p_intf );
484 static void intf_Run       ( intf_thread_t *p_intf );
485
486 /*****************************************************************************
487  * Functions exported as capabilities. They are declared as static so that
488  * we don't pollute the namespace too much.
489  *****************************************************************************/
490 void _M( intf_getfunctions )( function_list_t * p_function_list )
491 {
492     p_function_list->pf_probe = intf_Probe;
493     p_function_list->functions.intf.pf_open  = intf_Open;
494     p_function_list->functions.intf.pf_close = intf_Close;
495     p_function_list->functions.intf.pf_run   = intf_Run;
496 }
497
498 /*****************************************************************************
499  * intf_Probe: probe the interface and return a score
500  *****************************************************************************
501  * This function tries to initialize Gnome and returns a score to the
502  * plugin manager so that it can select the best plugin.
503  *****************************************************************************/
504 static int intf_Probe( probedata_t *p_data )
505 {
506     if( TestMethod( INTF_METHOD_VAR, "beos" ) )
507     {
508         return( 999 );
509     }
510
511     return( 100 );
512 }
513
514 /*****************************************************************************
515  * intf_Open: initialize interface
516  *****************************************************************************/
517 static int intf_Open( intf_thread_t *p_intf )
518 {
519     /* Allocate instance and initialize some members */
520     p_intf->p_sys = (intf_sys_t*) malloc( sizeof( intf_sys_t ) );
521     if( p_intf->p_sys == NULL )
522     {
523         intf_ErrMsg("error: %s", strerror(ENOMEM));
524         return( 1 );
525     }
526     p_intf->p_sys->i_key = -1;
527     
528     /* Create the interface window */
529     p_intf->p_sys->p_window =
530         new InterfaceWindow( BRect( 50, 50, 400, 100 ),
531                              VOUT_TITLE " (BeOS interface)", p_intf );
532     if( p_intf->p_sys->p_window == 0 )
533     {
534         free( p_intf->p_sys );
535         intf_ErrMsg( "error: cannot allocate memory for InterfaceWindow" );
536         return( 1 );
537     }
538     
539     return( 0 );
540 }
541
542 /*****************************************************************************
543  * intf_Close: destroy dummy interface
544  *****************************************************************************/
545 static void intf_Close( intf_thread_t *p_intf )
546 {
547     /* Destroy the interface window */
548     p_intf->p_sys->p_window->Lock();
549     p_intf->p_sys->p_window->Quit();    
550
551     /* Destroy structure */
552     free( p_intf->p_sys );
553 }
554
555
556 /*****************************************************************************
557  * intf_Run: event loop
558  *****************************************************************************/
559 static void intf_Run( intf_thread_t *p_intf )
560 {
561         
562         float progress;
563         bool seekNeeded = false;
564         
565     while( !p_intf->b_die )
566     {
567
568         /* Manage core vlc functions through the callback */
569         p_intf->pf_manage( p_intf );
570
571             /* Manage the slider */
572             if( p_intf->p_input != NULL && p_intf->p_sys->p_window != NULL)
573         {
574             if (acquire_sem(p_intf->p_sys->p_window->fScrubSem) == B_OK)
575                         {
576                                 seekNeeded = true;
577                         }               
578
579                         if (seekNeeded)
580             {
581                 uint32 seekTo = (p_intf->p_sys->p_window->p_seek->Value() * 
582                                 p_intf->p_input->stream.p_selected_area->i_size) / 100;
583                                 input_Seek( p_intf->p_input, seekTo );
584                                 seekNeeded = false;             
585             }
586                         else if (p_intf->p_sys->p_window->Lock())
587             {
588                     progress = (100. * p_intf->p_input->stream.p_selected_area->i_tell) /
589                                         p_intf->p_input->stream.p_selected_area->i_size;
590                     p_intf->p_sys->p_window->p_seek->SetValue(progress);
591                     p_intf->p_sys->p_window->Unlock();
592                 }
593          }
594
595         /* Wait a bit */
596         msleep( INTF_IDLE_SLEEP );
597     }
598 }
599
600 } /* extern "C" */