]> git.sesse.net Git - vlc/blob - plugins/beos/vout_beos.cpp
* ./src/audio_output/aout_s16.c: fixed a segfault. It may have unexpected
[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.37 2002/01/05 18:25:48 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 } vout_sys_t;
67
68
69 /*****************************************************************************
70  * beos_GetAppWindow : retrieve a BWindow pointer from the window name
71  *****************************************************************************/
72 BWindow *beos_GetAppWindow(char *name)
73 {
74     int32       index;
75     BWindow     *window;
76     
77     for (index = 0 ; ; index++)
78     {
79         window = be_app->WindowAt(index);
80         if (window == NULL)
81             break;
82         if (window->LockWithTimeout(20000) == B_OK)
83         {
84             if (strcmp(window->Name(), name) == 0)
85             {
86                 window->Unlock();
87                 break;
88             }
89             window->Unlock();
90         }
91     }
92     return window; 
93 }
94
95 /**************************************************************************** 
96  * DrawingThread : thread that really does the drawing 
97  ****************************************************************************/ 
98 int32 Draw(void *data) 
99
100     //rudolf: sync init: 
101     BScreen *screen; 
102     display_mode disp_mode; 
103     static uint32 refresh, oldrefresh = 0; 
104
105     screen = new BScreen(); 
106     screen-> GetMode(&disp_mode); 
107     refresh = 
108          (disp_mode.timing.pixel_clock * 1000)/((disp_mode.timing.h_total)* 
109          (disp_mode.timing.v_total)); 
110     if (!(refresh == oldrefresh)) 
111     { 
112         printf("\nNew refreshrate is %d:Hz\n",refresh); 
113         oldrefresh = refresh; 
114         if (refresh  < 61) 
115         { 
116             printf("Enabling retrace sync.\n"); 
117         } 
118         else 
119         { 
120             printf("Disabling retrace sync.\n"); 
121         } 
122     } 
123
124     VideoWindow* p_win; 
125     p_win = (VideoWindow *) data; 
126     if ( p_win-> voutWindow-> LockLooper() ) 
127     { 
128         //rudolf: sync: 
129         if (refresh  < 61) 
130         { 
131             screen-> WaitForRetrace(22000);//set timeout for  < 45 Hz... 
132         } 
133
134         p_win-> view-> DrawBitmap( p_win-> bitmap[p_win-> i_buffer], 
135                                  p_win-> bitmap[p_win-> i_buffer]-> Bounds(), 
136                                  p_win-> voutWindow-> Bounds() );  
137         p_win-> voutWindow-> UnlockLooper(); 
138     } 
139     return B_OK; 
140 }
141
142 /*****************************************************************************
143  * bitmapWindow : This is the bitmap window output
144  *****************************************************************************/
145 bitmapWindow::bitmapWindow(BRect frame, VideoWindow *theOwner)
146         : BWindow( frame, NULL, B_TITLED_WINDOW, 
147                    B_OUTLINE_RESIZE | B_NOT_CLOSABLE | B_NOT_MINIMIZABLE )
148 {
149     is_zoomed = false;
150     origRect = frame;
151     owner = theOwner;
152     SetTitle(VOUT_TITLE " (BBitmap output)");
153 }
154
155 bitmapWindow::~bitmapWindow()
156 {
157 }
158
159 void bitmapWindow::FrameResized( float width, float height )
160 {
161     if (is_zoomed)
162     {
163         return;
164     }
165     float width_scale;
166     float height_scale;
167
168     width_scale = width / origRect.Width();
169     height_scale = height / origRect.Height();
170     
171     /* if the width is proportionally smaller */
172     if (width_scale <= height_scale)
173     {
174         ResizeTo(width, origRect.Height() * width_scale);
175     }
176     else /* if the height is proportionally smaller */
177     {
178         ResizeTo(origRect.Width() * height_scale, height);
179     }
180 }
181
182 void bitmapWindow::Zoom(BPoint origin, float width, float height )
183 {
184     if(is_zoomed)
185     {
186         MoveTo(origRect.left, origRect.top);
187         ResizeTo(origRect.IntegerWidth(), origRect.IntegerHeight());
188         be_app->ShowCursor();
189     }
190     else
191     {
192         BScreen *screen;
193         screen = new BScreen(this);
194         BRect rect = screen->Frame();
195         delete screen;
196         MoveTo(0,0);
197         ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
198         be_app->HideCursor();
199     }
200     is_zoomed = !is_zoomed;
201 }
202
203 /*****************************************************************************
204  * directWindow : This is the bitmap window output
205  *****************************************************************************/
206 directWindow::directWindow(BRect frame, VideoWindow *theOwner)
207         : BDirectWindow( frame, NULL, B_TITLED_WINDOW, 
208                    B_OUTLINE_RESIZE | B_NOT_CLOSABLE | B_NOT_MINIMIZABLE )
209 {
210     is_zoomed = false;
211     origRect = frame;
212     owner = theOwner;
213     SetTitle(VOUT_TITLE " (DirectWindow output)");
214 }
215
216 directWindow::~directWindow()
217 {
218 }
219
220 void directWindow::DirectConnected(direct_buffer_info *info)
221 {
222 }
223
224 void directWindow::FrameResized( float width, float height )
225 {
226     if (is_zoomed)
227     {
228         return;
229     }
230     float width_scale;
231     float height_scale;
232
233     width_scale = width / origRect.Width();
234     height_scale = height / origRect.Height();
235     
236     /* if the width is proportionally smaller */
237     if (width_scale <= height_scale)
238     {
239         ResizeTo(width, origRect.Height() * width_scale);
240     }
241     else /* if the height is proportionally smaller */
242     {
243         ResizeTo(origRect.Width() * height_scale, height);
244     }
245 }
246
247 void directWindow::Zoom(BPoint origin, float width, float height )
248 {
249     if(is_zoomed)
250     {
251         SetFullScreen(false);
252         MoveTo(origRect.left, origRect.top);
253         ResizeTo(origRect.IntegerWidth(), origRect.IntegerHeight());
254         be_app->ShowCursor();
255     }
256     else
257     {
258         SetFullScreen(true);
259         BScreen *screen;
260         screen = new BScreen(this);
261         BRect rect = screen->Frame();
262         delete screen;
263         MoveTo(0,0);
264         ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
265         be_app->HideCursor();
266     }
267     is_zoomed = !is_zoomed;
268 }
269
270 /*****************************************************************************
271  * VideoWindow constructor and destructor
272  *****************************************************************************/
273 VideoWindow::VideoWindow( int width, int height, 
274                           vout_thread_t *p_video_output )
275 {
276     if ( BDirectWindow::SupportsWindowMode() )
277     { 
278         voutWindow = new directWindow( BRect( 80, 50, 
279                                           80 + width, 50 + height ), this );
280     }
281     else
282     {
283         voutWindow = new bitmapWindow( BRect( 80, 50, 
284                                           80 + width, 50 + height ), this );
285     }
286
287     /* set the VideoWindow variables */
288     teardownwindow = false;
289     
290     /* create the view to do the display */
291     view = new VLCView( voutWindow->Bounds() );
292     voutWindow->AddChild(view);
293     
294     /* Bitmap mode overlay not available */
295 #if BITS_PER_PLANE == 32
296     bitmap[0] = new BBitmap( voutWindow->Bounds(), B_RGB32);
297     bitmap[1] = new BBitmap( voutWindow->Bounds(), B_RGB32);
298 #else
299     bitmap[0] = new BBitmap( voutWindow->Bounds(), B_RGB32);
300     bitmap[1] = new BBitmap( voutWindow->Bounds(), B_RGB32);
301 #endif
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 #if BITS_PER_PLANE == 32
359     SetViewColor(B_TRANSPARENT_32_BIT);
360 #else
361     SetViewColor(B_TRANSPARENT_16_BIT);
362 #endif
363 }
364
365 /*****************************************************************************
366  * VLCView::~VLCView
367  *****************************************************************************/
368 VLCView::~VLCView()
369 {
370 }
371
372 /*****************************************************************************
373  * VLCVIew::~VLCView
374  *****************************************************************************/
375 void VLCView::MouseDown(BPoint point)
376 {
377     BWindow *win = Window();
378     win->Zoom();
379 }
380
381 extern "C"
382 {
383
384 /*****************************************************************************
385  * Local prototypes
386  *****************************************************************************/
387 static int  vout_Probe      ( probedata_t *p_data );
388 static int  vout_Create     ( vout_thread_t * );
389 static int  vout_Init       ( vout_thread_t * );
390 static void vout_End        ( vout_thread_t * );
391 static void vout_Destroy    ( vout_thread_t * );
392 static int  vout_Manage     ( vout_thread_t * );
393 static void vout_Display    ( vout_thread_t *, picture_t * );
394 static void vout_Render     ( vout_thread_t *, picture_t * );
395
396 static int  BeosOpenDisplay ( vout_thread_t *p_vout );
397 static void BeosCloseDisplay( vout_thread_t *p_vout );
398
399 /*****************************************************************************
400  * Functions exported as capabilities. They are declared as static so that
401  * we don't pollute the namespace too much.
402  *****************************************************************************/
403 void _M( vout_getfunctions )( function_list_t * p_function_list )
404 {
405     p_function_list->pf_probe = vout_Probe;
406     p_function_list->functions.vout.pf_create     = vout_Create;
407     p_function_list->functions.vout.pf_init       = vout_Init;
408     p_function_list->functions.vout.pf_end        = vout_End;
409     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
410     p_function_list->functions.vout.pf_manage     = vout_Manage;
411     p_function_list->functions.vout.pf_display    = vout_Display;
412     p_function_list->functions.vout.pf_render     = vout_Render;
413 }
414
415 /*****************************************************************************
416  * vout_Probe: probe the video driver and return a score
417  *****************************************************************************
418  * This function tries to initialize SDL and returns a score to the
419  * plugin manager so that it can select the best plugin.
420  *****************************************************************************/
421 static int vout_Probe( probedata_t *p_data )
422 {
423     if( TestMethod( VOUT_METHOD_VAR, "beos" ) )
424     {
425         return( 999 );
426     }
427     return( 100 );
428 }
429
430 /*****************************************************************************
431  * vout_Create: allocates BeOS video thread output method
432  *****************************************************************************
433  * This function allocates and initializes a BeOS vout method.
434  *****************************************************************************/
435 int vout_Create( vout_thread_t *p_vout )
436 {
437     /* Allocate structure */
438     p_vout->p_sys = (vout_sys_t*) malloc( sizeof( vout_sys_t ) );
439     if( p_vout->p_sys == NULL )
440     {
441         intf_ErrMsg( "error: %s", strerror(ENOMEM) );
442         return( 1 );
443     }
444
445     if( p_vout->render.i_height * p_vout->render.i_aspect
446          >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
447     {
448         p_vout->p_sys->i_width = p_vout->render.i_height
449             * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
450         p_vout->p_sys->i_height = p_vout->render.i_height;
451     }
452     else
453     {
454         p_vout->p_sys->i_width = p_vout->render.i_width;
455         p_vout->p_sys->i_height = p_vout->render.i_width
456             * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
457     }
458
459     /* Open and initialize device */
460     if( BeosOpenDisplay( p_vout ) )
461     {
462         intf_ErrMsg("vout error: can't open display");
463         free( p_vout->p_sys );
464         return( 1 );
465     }
466
467     return( 0 );
468 }
469
470 /*****************************************************************************
471  * vout_Init: initialize BeOS video thread output method
472  *****************************************************************************/
473 int vout_Init( vout_thread_t *p_vout )
474 {
475     VideoWindow * p_win = p_vout->p_sys->p_window;
476
477     if((p_win->bitmap[0] != NULL) && (p_win->bitmap[1] != NULL))
478     {
479         p_vout->pf_setbuffers( p_vout,
480                (byte_t *)p_win->bitmap[0]->Bits(),
481                (byte_t *)p_win->bitmap[1]->Bits());
482     }
483     return( 0 );
484 }
485
486 /*****************************************************************************
487  * vout_End: terminate BeOS video thread output method
488  *****************************************************************************/
489 void vout_End( vout_thread_t *p_vout )
490 {
491     /* place code here to end the video */
492 }
493
494 /*****************************************************************************
495  * vout_Destroy: destroy BeOS video thread output method
496  *****************************************************************************
497  * Terminate an output method created by DummyCreateOutputMethod
498  *****************************************************************************/
499 void vout_Destroy( vout_thread_t *p_vout )
500 {
501     BeosCloseDisplay( p_vout );
502     free( p_vout->p_sys );
503 }
504
505 /*****************************************************************************
506  * vout_Manage: handle BeOS events
507  *****************************************************************************
508  * This function should be called regularly by video output thread. It manages
509  * console events. It returns a non null value on error.
510  *****************************************************************************/
511 int vout_Manage( vout_thread_t *p_vout )
512 {
513     VideoWindow * p_win = p_vout->p_sys->p_window;
514     
515     p_win->resizeIfRequired(p_vout->p_buffer[p_vout->i_buffer_index].i_pic_width,
516                             p_vout->p_buffer[p_vout->i_buffer_index].i_pic_height);
517                             
518     return( 0 );
519 }
520
521 /*****************************************************************************
522  * vout_Render: render previously calculated output
523  *****************************************************************************/
524 void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
525 {
526     ;
527 }
528
529 /*****************************************************************************
530  * vout_Display: displays previously rendered output
531  *****************************************************************************
532  * This function send the currently rendered image to BeOS image, waits until
533  * it is displayed and switch the two rendering buffers, preparing next frame.
534  *****************************************************************************/
535 void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
536 {
537
538     VideoWindow * p_win = p_vout->p_sys->p_window;
539     /* draw buffer if required */    
540     if (!p_win->teardownwindow)
541     {
542        p_win->drawBuffer(p_vout->i_buffer_index);
543     }
544     /* change buffer */
545     p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
546 }
547
548 /* following functions are local */
549
550 /*****************************************************************************
551  * BeosOpenDisplay: open and initialize BeOS device
552  *****************************************************************************/
553 static int BeosOpenDisplay( vout_thread_t *p_vout )
554
555     
556     VideoWindow * p_win = new VideoWindow( p_vout->i_width - 1, 
557                                            p_vout->i_height - 1, 
558                                            p_vout );
559
560     if( p_win == 0 )
561     {
562         free( p_vout->p_sys );
563         intf_ErrMsg( "error: cannot allocate memory for VideoWindow" );
564         return( 1 );
565     }   
566     
567     p_vout->p_sys->p_window = p_win;
568     /* set the system to 32bits always
569        let BeOS do all the work */
570     p_vout->p_sys->i_width    = p_win->i_width + 1;
571     p_vout->p_sys->i_height   = p_win->i_height + 1;
572 #if 0
573     p_vout->i_screen_depth    = BITS_PER_PLANE;
574     p_vout->i_bytes_per_pixel = BYTES_PER_PIXEL;
575     p_vout->i_bytes_per_line  = p_vout->p_sys->i_width * BYTES_PER_PIXEL;
576
577     p_vout->i_red_mask =        0xff0000;
578     p_vout->i_green_mask =      0x00ff00;
579     p_vout->i_blue_mask =       0x0000ff;
580 #endif
581
582     return( 0 );
583 }
584
585 /*****************************************************************************
586  * BeosDisplay: close and reset BeOS device
587  *****************************************************************************
588  * Returns all resources allocated by BeosOpenDisplay and restore the original
589  * state of the device.
590  *****************************************************************************/
591 static void BeosCloseDisplay( vout_thread_t *p_vout )
592 {    
593     /* Destroy the video window */
594     delete p_vout->p_sys->p_window;
595 }
596
597 } /* extern "C" */