]> git.sesse.net Git - vlc/blob - plugins/beos/vout_beos.cpp
* ./include/common.h: BeOS compile fixes.
[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.43 2002/02/27 03:47:56 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_Create     ( vout_thread_t * );
384 static int  vout_Init       ( vout_thread_t * );
385 static void vout_End        ( vout_thread_t * );
386 static void vout_Destroy    ( vout_thread_t * );
387 static int  vout_Manage     ( vout_thread_t * );
388 static void vout_Display    ( vout_thread_t *, picture_t * );
389 static void vout_Render     ( vout_thread_t *, picture_t * );
390
391 static int  BeosOpenDisplay ( vout_thread_t *p_vout );
392 static void BeosCloseDisplay( vout_thread_t *p_vout );
393
394 /*****************************************************************************
395  * Functions exported as capabilities. They are declared as static so that
396  * we don't pollute the namespace too much.
397  *****************************************************************************/
398 void _M( vout_getfunctions )( function_list_t * p_function_list )
399 {
400     p_function_list->functions.vout.pf_create     = vout_Create;
401     p_function_list->functions.vout.pf_init       = vout_Init;
402     p_function_list->functions.vout.pf_end        = vout_End;
403     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
404     p_function_list->functions.vout.pf_manage     = vout_Manage;
405     p_function_list->functions.vout.pf_display    = vout_Display;
406     p_function_list->functions.vout.pf_render     = vout_Render;
407 }
408
409 /*****************************************************************************
410  * vout_Create: allocates BeOS video thread output method
411  *****************************************************************************
412  * This function allocates and initializes a BeOS vout method.
413  *****************************************************************************/
414 int vout_Create( vout_thread_t *p_vout )
415 {
416     /* Allocate structure */
417     p_vout->p_sys = (vout_sys_t*) malloc( sizeof( vout_sys_t ) );
418     if( p_vout->p_sys == NULL )
419     {
420         intf_ErrMsg( "error: %s", strerror(ENOMEM) );
421         return( 1 );
422     }
423
424     if( p_vout->render.i_height * p_vout->render.i_aspect
425          >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
426     {
427         p_vout->p_sys->i_width = p_vout->render.i_height
428             * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
429         p_vout->p_sys->i_height = p_vout->render.i_height;
430     }
431     else
432     {
433         p_vout->p_sys->i_width = p_vout->render.i_width;
434         p_vout->p_sys->i_height = p_vout->render.i_width
435             * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
436     }
437
438     return( 0 );
439 }
440
441 /*****************************************************************************
442  * vout_Init: initialize BeOS video thread output method
443  *****************************************************************************/
444 int vout_Init( vout_thread_t *p_vout )
445 {
446     int i_index;
447     picture_t *p_pic;
448
449     I_OUTPUTPICTURES = 0;
450
451     /* Open and initialize device */
452     if( BeosOpenDisplay( p_vout ) )
453     {
454         intf_ErrMsg("vout error: can't open display");
455         return 0;
456     }
457
458     p_vout->output.i_width  = p_vout->p_sys->i_width;
459     p_vout->output.i_height = p_vout->p_sys->i_height;
460     p_vout->output.i_aspect = p_vout->p_sys->i_width
461                                * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
462     p_vout->output.i_chroma = FOURCC_RV32;
463
464     p_pic = NULL;
465
466     /* Find an empty picture slot */
467     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
468     {
469         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
470         {
471             p_pic = p_vout->p_picture + i_index;
472             break;
473         }
474     }
475
476     if( p_pic == NULL )
477     {
478         return 0;
479     }
480
481     p_vout->p_sys->i_index = 0;
482     p_pic->p->p_pixels = p_vout->p_sys->pp_buffer[0];
483     p_pic->p->i_pixel_bytes = 4;
484     p_pic->p->i_lines = p_vout->p_sys->i_height;
485     p_pic->p->b_margin = 0;
486     p_pic->p->i_pitch = 4 * p_vout->p_sys->i_width;
487
488     p_pic->p->i_red_mask   = 0x00ff0000;
489     p_pic->p->i_green_mask = 0x0000ff00;
490     p_pic->p->i_blue_mask  = 0x000000ff;
491
492     p_pic->i_planes = 1;
493
494     p_pic->i_status = DESTROYED_PICTURE;
495     p_pic->i_type   = DIRECT_PICTURE;
496
497     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
498
499     I_OUTPUTPICTURES++;
500
501     return( 0 );
502 }
503
504 /*****************************************************************************
505  * vout_End: terminate BeOS video thread output method
506  *****************************************************************************/
507 void vout_End( vout_thread_t *p_vout )
508 {
509     BeosCloseDisplay( p_vout );
510 }
511
512 /*****************************************************************************
513  * vout_Destroy: destroy BeOS video thread output method
514  *****************************************************************************
515  * Terminate an output method created by DummyCreateOutputMethod
516  *****************************************************************************/
517 void vout_Destroy( vout_thread_t *p_vout )
518 {
519     free( p_vout->p_sys );
520 }
521
522 /*****************************************************************************
523  * vout_Manage: handle BeOS events
524  *****************************************************************************
525  * This function should be called regularly by video output thread. It manages
526  * console events. It returns a non null value on error.
527  *****************************************************************************/
528 int vout_Manage( vout_thread_t *p_vout )
529 {
530 //    VideoWindow * p_win = p_vout->p_sys->p_window;
531     
532 //    p_win->resizeIfRequired(p_vout->p_sys->pp_buffer[p_vout->p_sys->i_index].i_pic_width,
533 //                            p_vout->p_sys->pp_buffer[p_vout->p_sys->i_index].i_pic_height);
534                             
535     return( 0 );
536 }
537
538 /*****************************************************************************
539  * vout_Render: render previously calculated output
540  *****************************************************************************/
541 void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
542 {
543     ;
544 }
545
546 /*****************************************************************************
547  * vout_Display: displays previously rendered output
548  *****************************************************************************
549  * This function send the currently rendered image to BeOS image, waits until
550  * it is displayed and switch the two rendering buffers, preparing next frame.
551  *****************************************************************************/
552 void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
553 {
554     VideoWindow * p_win = p_vout->p_sys->p_window;
555
556     /* draw buffer if required */    
557     if (!p_win->teardownwindow)
558     {
559        p_win->drawBuffer(p_vout->p_sys->i_index);
560     }
561     /* change buffer */
562     p_vout->p_sys->i_index = ++p_vout->p_sys->i_index & 1;
563     p_pic->p->p_pixels = p_vout->p_sys->pp_buffer[p_vout->p_sys->i_index];
564 }
565
566 /* following functions are local */
567
568 /*****************************************************************************
569  * BeosOpenDisplay: open and initialize BeOS device
570  *****************************************************************************/
571 static int BeosOpenDisplay( vout_thread_t *p_vout )
572
573     p_vout->p_sys->p_window = new VideoWindow( p_vout->p_sys->i_width - 1,
574                                                p_vout->p_sys->i_height - 1,
575                                                p_vout );
576
577     if( p_vout->p_sys->p_window == NULL )
578     {
579         intf_ErrMsg( "error: cannot allocate memory for VideoWindow" );
580         return( 1 );
581     }   
582     
583     p_vout->p_sys->i_width    = p_vout->p_sys->p_window->i_width + 1;
584     p_vout->p_sys->i_height   = p_vout->p_sys->p_window->i_height + 1;
585
586     p_vout->p_sys->pp_buffer[0] = (u8*)p_vout->p_sys->p_window->bitmap[0]->Bits();
587     p_vout->p_sys->pp_buffer[1] = (u8*)p_vout->p_sys->p_window->bitmap[1]->Bits();
588
589     return( 0 );
590 }
591
592 /*****************************************************************************
593  * BeosDisplay: close and reset BeOS device
594  *****************************************************************************
595  * Returns all resources allocated by BeosOpenDisplay and restore the original
596  * state of the device.
597  *****************************************************************************/
598 static void BeosCloseDisplay( vout_thread_t *p_vout )
599 {    
600     /* Destroy the video window */
601     delete p_vout->p_sys->p_window;
602 }
603
604 } /* extern "C" */