]> git.sesse.net Git - vlc/blob - plugins/beos/intf_beos.cpp
More changes to interface
[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.17 2001/03/06 01:26:06 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 <Screen.h>
57 #include <malloc.h>
58 #include <string.h>
59
60 extern "C"
61 {
62 #include "config.h"
63 #include "common.h"
64 #include "threads.h"
65 #include "mtime.h"
66 #include "tests.h"
67 #include "modules.h"
68
69 #include "stream_control.h"
70 #include "input_ext-intf.h"
71
72 #include "interface.h"
73 #include "intf_plst.h"
74 #include "intf_msg.h"
75 #include "audio_output.h"
76 #include "MsgVals.h"
77
78
79 #include "main.h"
80 }
81
82 #include "InterfaceWindow.h"
83 #include "Bitmaps.h"
84 #include "TransportButton.h"
85
86 /*****************************************************************************
87  * intf_sys_t: description and status of FB interface
88  *****************************************************************************/
89 typedef struct intf_sys_s
90 {
91     InterfaceWindow * p_window;
92     char              i_key;
93 } intf_sys_t;
94
95 /*****************************************************************************
96  * InterfaceWindow
97  *****************************************************************************/
98  
99 InterfaceWindow::InterfaceWindow( BRect frame, const char *name , intf_thread_t  *p_interface )
100     : BWindow(frame, name, B_FLOATING_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
101         B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_WILL_ACCEPT_FIRST_CLICK |B_ASYNCHRONOUS_CONTROLS)
102 {
103     file_panel = NULL;
104     p_intf = p_interface;
105         BRect ButtonRect;
106         float xStart = 5.0;
107         float yStart = 20.0;
108
109     SetName( "interface" );
110     SetTitle(VOUT_TITLE " (BeOS interface)");
111     BRect rect(0, 0, 0, 0);
112     
113     BMenuBar *menu_bar; 
114     menu_bar = new BMenuBar(rect, "main menu");
115     AddChild( menu_bar );
116
117         BMenu *m;
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, B_PLAIN_BORDER );
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,2,255,15), 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,20,255,30), 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,50 + 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         Activate();
232     switch( p_message->what )
233     {
234     case OPEN_FILE:
235         if(file_panel)
236                 {
237                 file_panel->Show();
238                 break;
239                 }
240         file_panel = new BFilePanel();
241         file_panel->SetTarget(this);
242         file_panel->Show();
243         break;
244
245     case OPEN_DVD:
246             alert = new BAlert(VOUT_TITLE, "Play DVD from menu not yet supported", "Bummer");
247             alert->Go();
248             //intf_PlstAdd( p_main->p_playlist, PLAYLIST_END, "dvd:/dev/disk/ide/atapi/1/master/0/raw" );
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_vol->SetEnabled(true);
353                                 p_main->p_aout->vol = vol_val;
354                         }       
355                         else
356                         {
357                                 p_vol->SetEnabled(false);
358                                 p_main->p_aout->vol = 0;
359                         }
360                 }
361                 break;
362         case SELECT_CHANNEL:
363                 break;
364         case B_REFS_RECEIVED:
365     case B_SIMPLE_DATA:
366         {
367             entry_ref ref;
368             if( p_message->FindRef( "refs", &ref ) == B_OK )
369             {
370                 BPath path( &ref );
371                 char * psz_name = strdup(path.Path());
372                 intf_PlstAdd( p_main->p_playlist, PLAYLIST_END, psz_name );
373             }
374
375         }
376         break;
377     default:
378         BWindow::MessageReceived( p_message );
379         break;
380     }
381 }
382
383 /*****************************************************************************
384  * InterfaceWindow::QuitRequested
385  *****************************************************************************/
386
387 bool InterfaceWindow::QuitRequested()
388 {
389     p_intf->b_die = 1;
390
391     return( false );
392 }
393 /*****************************************************************************
394  * MediaSlider
395  *****************************************************************************/
396 MediaSlider::MediaSlider(BRect frame,
397                                                 BMessage *message,
398                                                 int32 minValue,
399                                                 int32 maxValue)
400                                         :BSlider(frame, NULL, NULL, message, minValue, maxValue)
401 {
402
403 }
404
405 MediaSlider::~MediaSlider()
406 {
407
408 }
409
410 void MediaSlider::DrawThumb(void)
411 {
412         BRect r;
413         BView *v;
414
415         rgb_color black = {0,0,0};
416         r = ThumbFrame();
417         v = OffscreenView();
418         if(IsEnabled())
419                 v->SetHighColor(black);
420         else
421                 v->SetHighColor(tint_color(black, B_LIGHTEN_2_TINT));
422         r.InsetBy(r.IntegerWidth()/4, r.IntegerHeight()/6);
423         v->StrokeEllipse(r);
424         if(IsEnabled())
425                 v->SetHighColor(ui_color(B_PANEL_BACKGROUND_COLOR));
426         else
427                 v->SetHighColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR), B_LIGHTEN_2_TINT));
428         r.InsetBy(1,1);
429         v->FillEllipse(r);
430 }
431
432 /*****************************************************************************
433  * SeekSlider
434  *****************************************************************************/
435 SeekSlider::SeekSlider(BRect frame,
436                                 InterfaceWindow *owner,
437                                 int32 minValue,
438                                 int32 maxValue,
439                                 thumb_style thumbType = B_TRIANGLE_THUMB)
440                         :MediaSlider(frame, NULL, minValue, maxValue)
441 {
442         fOwner = owner;
443         fMouseDown = false;
444 }
445
446 SeekSlider::~SeekSlider()
447 {
448 }
449
450 /*****************************************************************************
451  * SeekSlider::MouseDown
452  *****************************************************************************/
453 void SeekSlider::MouseDown(BPoint where)
454 {
455         BSlider::MouseDown(where);
456         fOwner->fScrubSem = create_sem(1, "Vlc::fScrubSem");
457         fMouseDown = true;                                      
458 }
459
460 /*****************************************************************************
461  * SeekSlider::MouseUp
462  *****************************************************************************/
463 void SeekSlider::MouseMoved(BPoint where, uint32 code, const BMessage *message)
464 {
465         BSlider::MouseMoved(where, code, message);
466         if (!fMouseDown)
467                 return;
468         release_sem(fOwner->fScrubSem);
469 }
470
471 /*****************************************************************************
472  * SeekSlider::MouseUp
473  *****************************************************************************/
474 void SeekSlider::MouseUp(BPoint where)
475 {
476         BSlider::MouseUp(where);
477         delete_sem(fOwner->fScrubSem);
478         fOwner->fScrubSem = B_ERROR;
479         fMouseDown = false;                                     
480 }
481         
482
483 extern "C"
484 {
485
486 /*****************************************************************************
487  * Local prototypes.
488  *****************************************************************************/
489 static int  intf_Probe     ( probedata_t *p_data );
490 static int  intf_Open      ( intf_thread_t *p_intf );
491 static void intf_Close     ( intf_thread_t *p_intf );
492 static void intf_Run       ( intf_thread_t *p_intf );
493
494 /*****************************************************************************
495  * Functions exported as capabilities. They are declared as static so that
496  * we don't pollute the namespace too much.
497  *****************************************************************************/
498 void _M( intf_getfunctions )( function_list_t * p_function_list )
499 {
500     p_function_list->pf_probe = intf_Probe;
501     p_function_list->functions.intf.pf_open  = intf_Open;
502     p_function_list->functions.intf.pf_close = intf_Close;
503     p_function_list->functions.intf.pf_run   = intf_Run;
504 }
505
506 /*****************************************************************************
507  * intf_Probe: probe the interface and return a score
508  *****************************************************************************
509  * This function tries to initialize Gnome and returns a score to the
510  * plugin manager so that it can select the best plugin.
511  *****************************************************************************/
512 static int intf_Probe( probedata_t *p_data )
513 {
514     if( TestMethod( INTF_METHOD_VAR, "beos" ) )
515     {
516         return( 999 );
517     }
518
519     return( 100 );
520 }
521
522 /*****************************************************************************
523  * intf_Open: initialize interface
524  *****************************************************************************/
525 static int intf_Open( intf_thread_t *p_intf )
526 {
527     BScreen *screen;
528     screen = new BScreen();
529     BRect rect = screen->Frame();
530     rect.top = rect.bottom-100;
531     rect.bottom -= 50;
532     rect.left += 50;
533     rect.right = rect.left + 350;
534     delete screen;
535     
536     /* Allocate instance and initialize some members */
537     p_intf->p_sys = (intf_sys_t*) malloc( sizeof( intf_sys_t ) );
538     if( p_intf->p_sys == NULL )
539     {
540         intf_ErrMsg("error: %s", strerror(ENOMEM));
541         return( 1 );
542     }
543     p_intf->p_sys->i_key = -1;
544     
545     /* Create the interface window */
546     p_intf->p_sys->p_window =
547         new InterfaceWindow( rect,
548                              VOUT_TITLE " (BeOS interface)", p_intf );
549     if( p_intf->p_sys->p_window == 0 )
550     {
551         free( p_intf->p_sys );
552         intf_ErrMsg( "error: cannot allocate memory for InterfaceWindow" );
553         return( 1 );
554     }
555     
556     return( 0 );
557 }
558
559 /*****************************************************************************
560  * intf_Close: destroy dummy interface
561  *****************************************************************************/
562 static void intf_Close( intf_thread_t *p_intf )
563 {
564     /* Destroy the interface window */
565     p_intf->p_sys->p_window->Lock();
566     p_intf->p_sys->p_window->Quit();    
567
568     /* Destroy structure */
569     free( p_intf->p_sys );
570 }
571
572
573 /*****************************************************************************
574  * intf_Run: event loop
575  *****************************************************************************/
576 static void intf_Run( intf_thread_t *p_intf )
577 {
578         
579         float progress;
580         bool seekNeeded = false;
581         
582     while( !p_intf->b_die )
583     {
584
585         /* Manage core vlc functions through the callback */
586         p_intf->pf_manage( p_intf );
587
588             /* Manage the slider */
589             if( p_intf->p_input != NULL && p_intf->p_sys->p_window != NULL)
590         {
591             if (acquire_sem(p_intf->p_sys->p_window->fScrubSem) == B_OK)
592                         {
593                                 seekNeeded = true;
594                         }               
595
596                         if (seekNeeded)
597             {
598                 uint32 seekTo = (p_intf->p_sys->p_window->p_seek->Value() * 
599                                 p_intf->p_input->stream.p_selected_area->i_size) / 100;
600                                 input_Seek( p_intf->p_input, seekTo );
601                                 seekNeeded = false;             
602             }
603                         else if (p_intf->p_sys->p_window->Lock())
604             {
605                     progress = (100. * p_intf->p_input->stream.p_selected_area->i_tell) /
606                                         p_intf->p_input->stream.p_selected_area->i_size;
607                     p_intf->p_sys->p_window->p_seek->SetValue(progress);
608                     p_intf->p_sys->p_window->Unlock();
609                 }
610          }
611
612         /* Wait a bit */
613         msleep( INTF_IDLE_SLEEP );
614     }
615 }
616
617 } /* extern "C" */