]> git.sesse.net Git - vlc/blob - plugins/beos/vout_beos.cpp
e35cf0887fa78676508f99ee5ea00b621d078f06
[vlc] / plugins / beos / vout_beos.cpp
1 /*****************************************************************************
2  * vout_beos.cpp: beos video output display method
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: vout_beos.cpp,v 1.39 2002/02/08 15:57:29 sam 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 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <errno.h>                                                 /* ENOMEM */
31 #include <stdlib.h>                                                /* free() */
32 #include <stdio.h>
33 #include <string.h>                                            /* strerror() */
34 #include <InterfaceKit.h>
35 #include <DirectWindow.h>
36 #include <Application.h>
37 #include <Bitmap.h>
38
39 extern "C"
40 {
41 #include <videolan/vlc.h>
42
43 #include "video.h"
44 #include "video_output.h"
45
46 #include "interface.h"
47 }
48
49 #include "VideoWindow.h"
50
51 #define BITS_PER_PLANE  16
52 #define BYTES_PER_PIXEL 2
53
54 /*****************************************************************************
55  * vout_sys_t: BeOS video output method descriptor
56  *****************************************************************************
57  * This structure is part of the video output thread descriptor.
58  * It describes the BeOS specific properties of an output thread.
59  *****************************************************************************/
60 typedef struct vout_sys_s
61 {
62     VideoWindow *  p_window;
63
64     s32 i_width;
65     s32 i_height;
66
67     u8 *pp_buffer[2];
68     int i_index;
69
70 } vout_sys_t;
71
72
73 /*****************************************************************************
74  * beos_GetAppWindow : retrieve a BWindow pointer from the window name
75  *****************************************************************************/
76 BWindow *beos_GetAppWindow(char *name)
77 {
78     int32       index;
79     BWindow     *window;
80     
81     for (index = 0 ; ; index++)
82     {
83         window = be_app->WindowAt(index);
84         if (window == NULL)
85             break;
86         if (window->LockWithTimeout(20000) == B_OK)
87         {
88             if (strcmp(window->Name(), name) == 0)
89             {
90                 window->Unlock();
91                 break;
92             }
93             window->Unlock();
94         }
95     }
96     return window; 
97 }
98
99 /**************************************************************************** 
100  * DrawingThread : thread that really does the drawing 
101  ****************************************************************************/ 
102 int32 Draw(void *data) 
103
104     //rudolf: sync init: 
105     BScreen *screen; 
106     display_mode disp_mode; 
107     static uint32 refresh, oldrefresh = 0; 
108
109     screen = new BScreen(); 
110     screen-> GetMode(&disp_mode); 
111     refresh = 
112          (disp_mode.timing.pixel_clock * 1000)/((disp_mode.timing.h_total)* 
113          (disp_mode.timing.v_total)); 
114     if (!(refresh == oldrefresh)) 
115     { 
116         intf_WarnMsg( 1, "vout info: new refreshrate is %ld:Hz", refresh ); 
117         oldrefresh = refresh; 
118         if (refresh < 61 ) 
119         { 
120             intf_WarnMsg( 1, "vout info: enabling retrace sync" ); 
121         } 
122         else 
123         { 
124             intf_WarnMsg( 1, "vout info: disabling retrace sync" ); 
125         } 
126     } 
127
128     VideoWindow* p_win; 
129     p_win = (VideoWindow *) data; 
130     if ( p_win-> voutWindow-> LockLooper() ) 
131     { 
132         //rudolf: sync: 
133         if (refresh  < 61) 
134         { 
135             screen-> WaitForRetrace(22000);//set timeout for  < 45 Hz... 
136         } 
137
138         p_win-> view-> DrawBitmap( p_win-> bitmap[p_win-> i_buffer], 
139                                  p_win-> bitmap[p_win-> i_buffer]-> Bounds(), 
140                                  p_win-> voutWindow-> Bounds() );  
141         p_win-> voutWindow-> UnlockLooper(); 
142     } 
143     return B_OK; 
144 }
145
146 /*****************************************************************************
147  * bitmapWindow : This is the bitmap window output
148  *****************************************************************************/
149 bitmapWindow::bitmapWindow(BRect frame, VideoWindow *theOwner)
150         : BWindow( frame, NULL, B_TITLED_WINDOW, 
151                    B_OUTLINE_RESIZE | B_NOT_CLOSABLE | B_NOT_MINIMIZABLE )
152 {
153     is_zoomed = false;
154     origRect = frame;
155     owner = theOwner;
156     SetTitle(VOUT_TITLE " (BBitmap output)");
157 }
158
159 bitmapWindow::~bitmapWindow()
160 {
161 }
162
163 void bitmapWindow::FrameResized( float width, float height )
164 {
165     if (is_zoomed)
166     {
167         return;
168     }
169     float width_scale;
170     float height_scale;
171
172     width_scale = width / origRect.Width();
173     height_scale = height / origRect.Height();
174     
175     /* if the width is proportionally smaller */
176     if (width_scale <= height_scale)
177     {
178         ResizeTo(width, origRect.Height() * width_scale);
179     }
180     else /* if the height is proportionally smaller */
181     {
182         ResizeTo(origRect.Width() * height_scale, height);
183     }
184 }
185
186 void bitmapWindow::Zoom(BPoint origin, float width, float height )
187 {
188     if(is_zoomed)
189     {
190         MoveTo(origRect.left, origRect.top);
191         ResizeTo(origRect.IntegerWidth(), origRect.IntegerHeight());
192         be_app->ShowCursor();
193     }
194     else
195     {
196         BScreen *screen;
197         screen = new BScreen(this);
198         BRect rect = screen->Frame();
199         delete screen;
200         MoveTo(0,0);
201         ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
202         be_app->HideCursor();
203     }
204     is_zoomed = !is_zoomed;
205 }
206
207 /*****************************************************************************
208  * directWindow : This is the bitmap window output
209  *****************************************************************************/
210 directWindow::directWindow(BRect frame, VideoWindow *theOwner)
211         : BDirectWindow( frame, NULL, B_TITLED_WINDOW, 
212                    B_OUTLINE_RESIZE | B_NOT_CLOSABLE | B_NOT_MINIMIZABLE )
213 {
214     is_zoomed = false;
215     origRect = frame;
216     owner = theOwner;
217     SetTitle(VOUT_TITLE " (DirectWindow output)");
218 }
219
220 directWindow::~directWindow()
221 {
222 }
223
224 void directWindow::DirectConnected(direct_buffer_info *info)
225 {
226 }
227
228 void directWindow::FrameResized( float width, float height )
229 {
230     if (is_zoomed)
231     {
232         return;
233     }
234     float width_scale;
235     float height_scale;
236
237     width_scale = width / origRect.Width();
238     height_scale = height / origRect.Height();
239     
240     /* if the width is proportionally smaller */
241     if (width_scale <= height_scale)
242     {
243         ResizeTo(width, origRect.Height() * width_scale);
244     }
245     else /* if the height is proportionally smaller */
246     {
247         ResizeTo(origRect.Width() * height_scale, height);
248     }
249 }
250
251 void directWindow::Zoom(BPoint origin, float width, float height )
252 {
253     if(is_zoomed)
254     {
255         SetFullScreen(false);
256         MoveTo(origRect.left, origRect.top);
257         ResizeTo(origRect.IntegerWidth(), origRect.IntegerHeight());
258         be_app->ShowCursor();
259     }
260     else
261     {
262         SetFullScreen(true);
263         BScreen *screen;
264         screen = new BScreen(this);
265         BRect rect = screen->Frame();
266         delete screen;
267         MoveTo(0,0);
268         ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
269         be_app->HideCursor();
270     }
271     is_zoomed = !is_zoomed;
272 }
273
274 /*****************************************************************************
275  * VideoWindow constructor and destructor
276  *****************************************************************************/
277 VideoWindow::VideoWindow( int width, int height, 
278                           vout_thread_t *p_video_output )
279 {
280     if ( BDirectWindow::SupportsWindowMode() )
281     { 
282         voutWindow = new directWindow( BRect( 80, 50, 
283                                           80 + width, 50 + height ), this );
284     }
285     else
286     {
287         voutWindow = new bitmapWindow( BRect( 80, 50, 
288                                           80 + width, 50 + height ), this );
289     }
290
291     /* set the VideoWindow variables */
292     teardownwindow = false;
293     
294     /* create the view to do the display */
295     view = new VLCView( voutWindow->Bounds() );
296     voutWindow->AddChild(view);
297     
298     /* Bitmap mode overlay not available, set the system to 32bits
299      * and let BeOS do all the work */
300     bitmap[0] = new BBitmap( voutWindow->Bounds(), B_RGB32);
301     bitmap[1] = new BBitmap( voutWindow->Bounds(), B_RGB32);
302     memset(bitmap[0]->Bits(), 0, bitmap[0]->BitsLength());
303     memset(bitmap[1]->Bits(), 0, bitmap[1]->BitsLength());
304
305     i_width = bitmap[0]->Bounds().IntegerWidth();
306     i_height = bitmap[0]->Bounds().IntegerHeight();
307
308     voutWindow->Show();
309 }
310
311 VideoWindow::~VideoWindow()
312 {
313     int32 result;
314
315     voutWindow->Hide();
316     voutWindow->Sync();
317     voutWindow->Lock();
318     voutWindow->Quit();
319     teardownwindow = true;
320     wait_for_thread(fDrawThreadID, &result);
321     delete bitmap[0];
322     delete bitmap[1];
323 }
324
325 void VideoWindow::resizeIfRequired( int newWidth, int newHeight )
326 {
327     if (( newWidth != i_width + 1) &&
328         ( newHeight != i_height + 1) &&
329         ( newWidth != 0 ))
330     {
331         if ( voutWindow->Lock() )
332         {
333             view->ClearViewBitmap();
334             i_width = newWidth - 1;
335             i_height = newHeight -1;
336             voutWindow->ResizeTo((float) i_width, (float) i_height); 
337             voutWindow->Unlock();
338         }
339     }
340 }
341
342 void VideoWindow::drawBuffer(int bufferIndex)
343 {
344     status_t status;
345     
346     i_buffer = bufferIndex; 
347     
348     fDrawThreadID = spawn_thread(Draw, "drawing_thread",
349                     B_DISPLAY_PRIORITY, (void*) this);
350     wait_for_thread(fDrawThreadID, &status);
351 }
352
353 /*****************************************************************************
354  * VLCView::VLCView
355  *****************************************************************************/
356 VLCView::VLCView(BRect bounds) : BView(bounds, "", B_FOLLOW_ALL, B_WILL_DRAW)
357 {
358     SetViewColor(B_TRANSPARENT_32_BIT);
359 }
360
361 /*****************************************************************************
362  * VLCView::~VLCView
363  *****************************************************************************/
364 VLCView::~VLCView()
365 {
366 }
367
368 /*****************************************************************************
369  * VLCVIew::~VLCView
370  *****************************************************************************/
371 void VLCView::MouseDown(BPoint point)
372 {
373     BWindow *win = Window();
374     win->Zoom();
375 }
376
377 extern "C"
378 {
379
380 /*****************************************************************************
381  * Local prototypes
382  *****************************************************************************/
383 static int  vout_Probe      ( probedata_t *p_data );
384 static int  vout_Create     ( vout_thread_t * );
385 static int  vout_Init       ( vout_thread_t * );
386 static void vout_End        ( vout_thread_t * );
387 static void vout_Destroy    ( vout_thread_t * );
388 static int  vout_Manage     ( vout_thread_t * );
389 static void vout_Display    ( vout_thread_t *, picture_t * );
390 static void vout_Render     ( vout_thread_t *, picture_t * );
391
392 static int  BeosOpenDisplay ( vout_thread_t *p_vout );
393 static void BeosCloseDisplay( vout_thread_t *p_vout );
394
395 /*****************************************************************************
396  * Functions exported as capabilities. They are declared as static so that
397  * we don't pollute the namespace too much.
398  *****************************************************************************/
399 void _M( vout_getfunctions )( function_list_t * p_function_list )
400 {
401     p_function_list->pf_probe = vout_Probe;
402     p_function_list->functions.vout.pf_create     = vout_Create;
403     p_function_list->functions.vout.pf_init       = vout_Init;
404     p_function_list->functions.vout.pf_end        = vout_End;
405     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
406     p_function_list->functions.vout.pf_manage     = vout_Manage;
407     p_function_list->functions.vout.pf_display    = vout_Display;
408     p_function_list->functions.vout.pf_render     = vout_Render;
409 }
410
411 /*****************************************************************************
412  * vout_Probe: probe the video driver and return a score
413  *****************************************************************************
414  * This function tries to initialize SDL and returns a score to the
415  * plugin manager so that it can select the best plugin.
416  *****************************************************************************/
417 static int vout_Probe( probedata_t *p_data )
418 {
419     return( 100 );
420 }
421
422 /*****************************************************************************
423  * vout_Create: allocates BeOS video thread output method
424  *****************************************************************************
425  * This function allocates and initializes a BeOS vout method.
426  *****************************************************************************/
427 int vout_Create( vout_thread_t *p_vout )
428 {
429     /* Allocate structure */
430     p_vout->p_sys = (vout_sys_t*) malloc( sizeof( vout_sys_t ) );
431     if( p_vout->p_sys == NULL )
432     {
433         intf_ErrMsg( "error: %s", strerror(ENOMEM) );
434         return( 1 );
435     }
436
437     if( p_vout->render.i_height * p_vout->render.i_aspect
438          >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
439     {
440         p_vout->p_sys->i_width = p_vout->render.i_height
441             * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
442         p_vout->p_sys->i_height = p_vout->render.i_height;
443     }
444     else
445     {
446         p_vout->p_sys->i_width = p_vout->render.i_width;
447         p_vout->p_sys->i_height = p_vout->render.i_width
448             * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
449     }
450
451     /* Open and initialize device */
452     if( BeosOpenDisplay( p_vout ) )
453     {
454         intf_ErrMsg("vout error: can't open display");
455         free( p_vout->p_sys );
456         return( 1 );
457     }
458
459     return( 0 );
460 }
461
462 /*****************************************************************************
463  * vout_Init: initialize BeOS video thread output method
464  *****************************************************************************/
465 int vout_Init( vout_thread_t *p_vout )
466 {
467     VideoWindow *p_win = p_vout->p_sys->p_window;
468
469     int i_index;
470     picture_t *p_pic;
471
472     I_OUTPUTPICTURES = 0;
473
474     p_vout->output.i_width  = p_vout->p_sys->i_width;
475     p_vout->output.i_height = p_vout->p_sys->i_height;
476     p_vout->output.i_aspect = p_vout->p_sys->i_width
477                                * VOUT_ASPECT_FACTOR
478                                / p_vout->p_sys->i_height;
479     p_vout->output.i_chroma = FOURCC_RV32;
480
481     p_pic = NULL;
482
483     /* Find an empty picture slot */
484     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
485     {
486         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
487         {
488             p_pic = p_vout->p_picture + i_index;
489             break;
490         }
491     }
492
493     if( p_pic == NULL )
494     {
495         return 0;
496     }
497
498     p_vout->p_sys->i_index = 0;
499     p_pic->p->p_pixels = p_vout->p_sys->pp_buffer[0];
500     p_pic->p->i_pixel_bytes = 4;
501     p_pic->p->i_lines = p_vout->p_sys->i_height;
502     p_pic->p->b_margin = 0;
503     p_pic->p->i_pitch = 4 * p_vout->p_sys->i_width;
504
505     p_pic->p->i_red_mask   = 0x00ff0000;
506     p_pic->p->i_green_mask = 0x0000ff00;
507     p_pic->p->i_blue_mask  = 0x000000ff;
508
509     p_pic->i_planes = 1;
510
511     p_pic->i_status = DESTROYED_PICTURE;
512     p_pic->i_type   = DIRECT_PICTURE;
513
514     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
515
516     I_OUTPUTPICTURES++;
517
518     return( 0 );
519 }
520
521 /*****************************************************************************
522  * vout_End: terminate BeOS video thread output method
523  *****************************************************************************/
524 void vout_End( vout_thread_t *p_vout )
525 {
526     /* place code here to end the video */
527 }
528
529 /*****************************************************************************
530  * vout_Destroy: destroy BeOS video thread output method
531  *****************************************************************************
532  * Terminate an output method created by DummyCreateOutputMethod
533  *****************************************************************************/
534 void vout_Destroy( vout_thread_t *p_vout )
535 {
536     BeosCloseDisplay( p_vout );
537     free( p_vout->p_sys );
538 }
539
540 /*****************************************************************************
541  * vout_Manage: handle BeOS events
542  *****************************************************************************
543  * This function should be called regularly by video output thread. It manages
544  * console events. It returns a non null value on error.
545  *****************************************************************************/
546 int vout_Manage( vout_thread_t *p_vout )
547 {
548     VideoWindow * p_win = p_vout->p_sys->p_window;
549     
550 //    p_win->resizeIfRequired(p_vout->p_sys->pp_buffer[p_vout->p_sys->i_index].i_pic_width,
551 //                            p_vout->p_sys->pp_buffer[p_vout->p_sys->i_index].i_pic_height);
552                             
553     return( 0 );
554 }
555
556 /*****************************************************************************
557  * vout_Render: render previously calculated output
558  *****************************************************************************/
559 void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
560 {
561     ;
562 }
563
564 /*****************************************************************************
565  * vout_Display: displays previously rendered output
566  *****************************************************************************
567  * This function send the currently rendered image to BeOS image, waits until
568  * it is displayed and switch the two rendering buffers, preparing next frame.
569  *****************************************************************************/
570 void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
571 {
572     VideoWindow * p_win = p_vout->p_sys->p_window;
573
574     /* draw buffer if required */    
575     if (!p_win->teardownwindow)
576     {
577        p_win->drawBuffer(p_vout->p_sys->i_index);
578     }
579     /* change buffer */
580     p_vout->p_sys->i_index = ++p_vout->p_sys->i_index & 1;
581     p_pic->p->p_pixels = p_vout->p_sys->pp_buffer[p_vout->p_sys->i_index];
582 }
583
584 /* following functions are local */
585
586 /*****************************************************************************
587  * BeosOpenDisplay: open and initialize BeOS device
588  *****************************************************************************/
589 static int BeosOpenDisplay( vout_thread_t *p_vout )
590
591     p_vout->p_sys->p_window = new VideoWindow(
592             main_GetIntVariable( VOUT_WIDTH_VAR, VOUT_WIDTH_DEFAULT ) + 1,
593             main_GetIntVariable( VOUT_HEIGHT_VAR, VOUT_HEIGHT_DEFAULT ) + 1,
594             p_vout );
595
596     if( p_vout->p_sys->p_window == NULL )
597     {
598         intf_ErrMsg( "error: cannot allocate memory for VideoWindow" );
599         return( 1 );
600     }   
601     
602     p_vout->p_sys->i_width    = p_vout->p_sys->p_window->i_width + 1;
603     p_vout->p_sys->i_height   = p_vout->p_sys->p_window->i_height + 1;
604
605     p_vout->p_sys->pp_buffer[0] = (u8*)p_vout->p_sys->p_window->bitmap[0]->Bits();
606     p_vout->p_sys->pp_buffer[1] = (u8*)p_vout->p_sys->p_window->bitmap[1]->Bits();
607
608     return( 0 );
609 }
610
611 /*****************************************************************************
612  * BeosDisplay: close and reset BeOS device
613  *****************************************************************************
614  * Returns all resources allocated by BeosOpenDisplay and restore the original
615  * state of the device.
616  *****************************************************************************/
617 static void BeosCloseDisplay( vout_thread_t *p_vout )
618 {    
619     /* Destroy the video window */
620     delete p_vout->p_sys->p_window;
621 }
622
623 } /* extern "C" */