]> git.sesse.net Git - vlc/blob - plugins/beos/vout_beos.cpp
*** empty log message ***
[vlc] / plugins / beos / vout_beos.cpp
1 /*****************************************************************************
2  * vout_beos.cpp: beos video output display method
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  *
6  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
7  *          Samuel Hocevar <sam@zoy.org>
8  *          Tony Castley <tcastley@mail.powerup.com.au>
9  *          Richard Shepherd <richard@rshepherd.demon.co.uk>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 #define MODULE_NAME beos
27 #include "modules_inner.h"
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32 #include "defs.h"
33
34 #include <errno.h>                                                 /* ENOMEM */
35 #include <stdlib.h>                                                /* free() */
36 #include <stdio.h>
37 #include <string.h>                                            /* strerror() */
38 #include <kernel/OS.h>
39 #include <View.h>
40 #include <Application.h>
41 #include <Window.h>
42 #include <Locker.h>
43 #include <Screen.h>
44 #include <malloc.h>
45 #include <string.h>
46
47 extern "C"
48 {
49 #include "config.h"
50 #include "common.h"
51 #include "threads.h"
52 #include "mtime.h"
53 #include "tests.h"
54 #include "modules.h"
55
56 #include "video.h"
57 #include "video_output.h"
58
59 #include "interface.h"
60 #include "intf_msg.h"
61
62 #include "main.h"
63 }
64
65 #include "VideoWindow.h"
66 #include <Screen.h>
67
68 #define WIDTH 128
69 #define HEIGHT 64
70 #define BITS_PER_PLANE 16
71 #define BYTES_PER_PIXEL 2
72
73 /*****************************************************************************
74  * vout_sys_t: BeOS video output method descriptor
75  *****************************************************************************
76  * This structure is part of the video output thread descriptor.
77  * It describes the BeOS specific properties of an output thread.
78  *****************************************************************************/
79  
80 typedef struct vout_sys_s
81 {
82     VideoWindow *         p_window;
83
84     byte_t *              pp_buffer[2];
85     s32                   i_width;
86     s32                   i_height;
87 } vout_sys_t;
88
89
90 /*****************************************************************************
91  * beos_GetAppWindow : retrieve a BWindow pointer from the window name
92  *****************************************************************************/
93
94 BWindow *beos_GetAppWindow(char *name)
95 {
96     int32       index;
97     BWindow     *window;
98     
99     for (index = 0 ; ; index++)
100     {
101         window = be_app->WindowAt(index);
102         if (window == NULL)
103             break;
104         if (window->LockWithTimeout(200000) == B_OK)
105         {
106             if (strcmp(window->Name(), name) == 0)
107             {
108                 window->Unlock();
109                 break;
110             }
111             window->Unlock();
112         }
113     }
114     return window; 
115 }
116
117 /*****************************************************************************
118  * DrawingThread : thread that really does the drawing
119  *****************************************************************************/
120
121 int32 DrawingThread(void *data)
122 {
123     VideoWindow *w;
124     w = (VideoWindow*) data;
125     
126     while(!w->teardownwindow)
127     {
128     w->Lock();
129        if( w->fDirty )
130             {
131                 w->view->DrawBitmap(w->bitmap[w->i_buffer_index], w->bitmap[w->i_buffer_index]->Bounds(), w->Bounds());
132             w->fDirty = false;
133             }
134     w->Unlock();
135         snooze(20000);
136     }
137     return B_OK;
138 }
139
140 /*****************************************************************************
141  * VideoWindow constructor and destructor
142  *****************************************************************************/
143
144 VideoWindow::VideoWindow(BRect frame, const char *name, vout_thread_t *p_video_output )
145         : BWindow(frame, name, B_TITLED_WINDOW, NULL)
146 {
147         float minWidth, minHeight, maxWidth, maxHeight; 
148
149     teardownwindow = false;
150     is_zoomed = false;
151     p_vout = p_video_output;
152         fDrawThreadID = NULL;
153         
154     rect = Frame();
155     view = new VLCView(Bounds());
156     AddChild(view);
157         bitmap[0] = new BBitmap(Bounds(), B_BITMAP_WILL_OVERLAY|B_BITMAP_RESERVE_OVERLAY_CHANNEL, B_YCbCr422);
158         fUsingOverlay = true;
159         i_screen_depth = 32;
160         p_vout->b_YCbr = true;
161         
162         if (bitmap[0]->InitCheck() != B_OK)
163         {
164                 delete bitmap[0];
165                 p_vout->b_YCbr = false;
166                 fUsingOverlay = false;
167                 BScreen *screen;
168                 screen = new BScreen();
169                 color_space space = screen->ColorSpace();
170                 delete screen;
171
172                 if(space == B_RGB15)
173                         {
174                         bitmap[0] = new BBitmap(Bounds(), B_RGB15);
175                         bitmap[1] = new BBitmap(Bounds(), B_RGB15);
176                 i_screen_depth = 15;
177                     }
178                     else if(space == B_RGB16)
179                         {
180                         bitmap[0] = new BBitmap(Bounds(), B_RGB16);
181                         bitmap[1] = new BBitmap(Bounds(), B_RGB16);
182                         i_screen_depth = 16;
183                         }
184                         else //default to 32bpp
185                         {
186                         bitmap[0] = new BBitmap(Bounds(), B_RGB32);
187                         bitmap[1] = new BBitmap(Bounds(), B_RGB32);
188                         i_screen_depth = 32;
189                         }
190                 memset(bitmap[0]->Bits(), 0, bitmap[0]->BitsLength());
191                 memset(bitmap[1]->Bits(), 0, bitmap[1]->BitsLength());
192                 SetTitle(VOUT_TITLE " (BBitmap output)");
193          }
194
195         if(fUsingOverlay)
196                 {
197                 memset(bitmap[0]->Bits(), 0, bitmap[0]->BitsLength());
198                 rgb_color key;
199                 view->SetViewOverlay(bitmap[0], bitmap[0]->Bounds(), Bounds(), &key, B_FOLLOW_ALL,
200                                 B_OVERLAY_FILTER_HORIZONTAL|B_OVERLAY_FILTER_VERTICAL);
201                 view->SetViewColor(key);
202                 SetTitle(VOUT_TITLE " (Overlay output)");
203                 GetSizeLimits(&minWidth, &maxWidth, &minHeight, &maxHeight); 
204                 SetSizeLimits((float) Bounds().IntegerWidth(), maxWidth, (float) Bounds().IntegerHeight(), maxHeight);
205                 }
206         else
207                 {
208         fDrawThreadID = spawn_thread(DrawingThread, "drawing_thread",
209                     B_DISPLAY_PRIORITY, (void*) this);
210         resume_thread(fDrawThreadID);
211         }
212
213         i_bytes_per_pixel = bitmap[0]->BytesPerRow()/bitmap[0]->Bounds().IntegerWidth();
214     fRowBytes = bitmap[0]->BytesPerRow();
215     fDirty = false;
216     Show();
217 }
218
219 VideoWindow::~VideoWindow()
220 {
221     int32 result;
222
223     Hide();
224     Sync();
225     if(!fUsingOverlay)
226         {
227             wait_for_thread(fDrawThreadID, &result);
228         delete bitmap[0];
229         delete bitmap[1];
230         }
231  }
232
233
234 /*****************************************************************************
235  * VideoWindow::FrameResized
236  *****************************************************************************/
237 void VideoWindow::FrameResized( float width, float height )
238 {
239 }
240
241 /*****************************************************************************
242  * VideoWindow::Zoom
243  *****************************************************************************/
244
245 void VideoWindow::Zoom(BPoint origin, float width, float height )
246 {
247 if(is_zoomed)
248         {
249         MoveTo(rect.left, rect.top);
250         ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
251         be_app->ShowCursor();
252         }
253 else
254         {
255         rect = Frame();
256         BScreen *screen;
257         screen = new BScreen(this);
258         BRect rect = screen->Frame();
259         delete screen;
260         MoveTo(0,0);
261         ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
262         be_app->HideCursor();
263         }
264 is_zoomed = !is_zoomed;
265 }
266
267 /*****************************************************************************
268  * VideoWindow::MessageReceived
269  *****************************************************************************/
270
271 void VideoWindow::MessageReceived( BMessage * p_message )
272 {
273     BWindow * p_win;
274     
275     switch( p_message->what )
276     {
277     case B_KEY_DOWN:
278     case B_SIMPLE_DATA:
279         // post the message to the interface window which will handle it
280         p_win = beos_GetAppWindow( "interface" );
281         if( p_win != NULL )
282         {
283             p_win->PostMessage( p_message );
284         }
285         break;
286     
287     default:
288         BWindow::MessageReceived( p_message );
289         break;
290     }
291 }
292
293 /*****************************************************************************
294  * VideoWindow::QuitRequested
295  *****************************************************************************/
296
297 bool VideoWindow::QuitRequested()
298 {
299     /* FIXME: send a message ! */
300     p_main->p_intf->b_die = 1;
301     teardownwindow = true;
302     return( false );
303 }
304
305 /*****************************************************************************
306  * VLCView::VLCView
307  *****************************************************************************/
308 VLCView::VLCView(BRect bounds) : BView(bounds, "", B_FOLLOW_ALL, B_WILL_DRAW)
309 {
310 SetViewColor(B_TRANSPARENT_32_BIT);
311 }
312
313 /*****************************************************************************
314  * VLCView::~VLCView
315  *****************************************************************************/
316 VLCView::~VLCView()
317 {
318
319 }
320
321 /*****************************************************************************
322  * VLCVIew::~VLCView
323  *****************************************************************************/
324 void VLCView::MouseDown(BPoint point)
325 {
326 VideoWindow *w = (VideoWindow *) Window();
327 if(w->is_zoomed)
328         {
329         BWindow *win = Window();
330         win->Zoom();
331         }
332 }
333
334 extern "C"
335 {
336
337 /*****************************************************************************
338  * Local prototypes
339  *****************************************************************************/
340 static int  vout_Probe      ( probedata_t *p_data );
341 static int  vout_Create     ( struct vout_thread_s * );
342 static int  vout_Init       ( struct vout_thread_s * );
343 static void vout_End        ( struct vout_thread_s * );
344 static void vout_Destroy    ( struct vout_thread_s * );
345 static int  vout_Manage     ( struct vout_thread_s * );
346 static void vout_Display    ( struct vout_thread_s * );
347
348 static int  BeosOpenDisplay ( vout_thread_t *p_vout );
349 static void BeosCloseDisplay( vout_thread_t *p_vout );
350
351 /*****************************************************************************
352  * Functions exported as capabilities. They are declared as static so that
353  * we don't pollute the namespace too much.
354  *****************************************************************************/
355 void _M( vout_getfunctions )( function_list_t * p_function_list )
356 {
357     p_function_list->pf_probe = vout_Probe;
358     p_function_list->functions.vout.pf_create     = vout_Create;
359     p_function_list->functions.vout.pf_init       = vout_Init;
360     p_function_list->functions.vout.pf_end        = vout_End;
361     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
362     p_function_list->functions.vout.pf_manage     = vout_Manage;
363     p_function_list->functions.vout.pf_display    = vout_Display;
364     p_function_list->functions.vout.pf_setpalette = NULL;
365 }
366
367 /*****************************************************************************
368  * vout_Probe: probe the video driver and return a score
369  *****************************************************************************
370  * This function tries to initialize SDL and returns a score to the
371  * plugin manager so that it can select the best plugin.
372  *****************************************************************************/
373 static int vout_Probe( probedata_t *p_data )
374 {
375     if( TestMethod( VOUT_METHOD_VAR, "beos" ) )
376     {
377         return( 999 );
378     }
379
380     return( 100 );
381 }
382
383 /*****************************************************************************
384  * vout_Create: allocates BeOS video thread output method
385  *****************************************************************************
386  * This function allocates and initializes a BeOS vout method.
387  *****************************************************************************/
388 int vout_Create( vout_thread_t *p_vout )
389 {
390     /* Allocate structure */
391     p_vout->p_sys = (vout_sys_t*) malloc( sizeof( vout_sys_t ) );
392     if( p_vout->p_sys == NULL )
393     {
394         intf_ErrMsg( "error: %s", strerror(ENOMEM) );
395         return( 1 );
396     }
397     
398     /* Set video window's size */
399     p_vout->i_width =  main_GetIntVariable( VOUT_WIDTH_VAR,
400                                             VOUT_WIDTH_DEFAULT );
401     p_vout->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
402                                             VOUT_HEIGHT_DEFAULT );
403
404     /* Open and initialize device */
405     if( BeosOpenDisplay( p_vout ) )
406     {
407         intf_ErrMsg("vout error: can't open display");
408         free( p_vout->p_sys );
409         return( 1 );
410     }
411
412     return( 0 );
413 }
414
415 /*****************************************************************************
416  * vout_Init: initialize BeOS video thread output method
417  *****************************************************************************/
418 int vout_Init( vout_thread_t *p_vout )
419 {
420     VideoWindow * p_win = p_vout->p_sys->p_window;
421     u32 i_page_size;
422
423
424     i_page_size =   p_vout->i_width * p_vout->i_height * p_vout->i_bytes_per_pixel;
425     
426     p_vout->p_sys->i_width =         p_vout->i_width;
427     p_vout->p_sys->i_height =        p_vout->i_height;    
428
429     if(p_win->fUsingOverlay)
430         {
431             vout_SetBuffers( p_vout, (byte_t *)p_win->bitmap[0]->Bits(),
432                          (byte_t *)p_win->bitmap[0]->Bits());
433         delete p_win->bitmap[0];
434         }
435     else
436                 {
437             vout_SetBuffers( p_vout, (byte_t *)p_win->bitmap[0]->Bits(),
438                          (byte_t *)p_win->bitmap[1]->Bits());
439         }
440     return( 0 );
441 }
442
443 /*****************************************************************************
444  * vout_End: terminate BeOS video thread output method
445  *****************************************************************************/
446 void vout_End( vout_thread_t *p_vout )
447 {
448 }
449
450 /*****************************************************************************
451  * vout_Destroy: destroy BeOS video thread output method
452  *****************************************************************************
453  * Terminate an output method created by DummyCreateOutputMethod
454  *****************************************************************************/
455 void vout_Destroy( vout_thread_t *p_vout )
456 {
457     BeosCloseDisplay( p_vout );
458     
459     free( p_vout->p_sys );
460 }
461
462 /*****************************************************************************
463  * vout_Manage: handle BeOS events
464  *****************************************************************************
465  * This function should be called regularly by video output thread. It manages
466  * console events. It returns a non null value on error.
467  *****************************************************************************/
468 int vout_Manage( vout_thread_t *p_vout )
469 {
470    return( 0 );
471 }
472
473 /*****************************************************************************
474  * vout_Display: displays previously rendered output
475  *****************************************************************************
476  * This function send the currently rendered image to BeOS image, waits until
477  * it is displayed and switch the two rendering buffers, preparing next frame.
478  *****************************************************************************/
479 void vout_Display( vout_thread_t *p_vout )
480 {
481     VideoWindow * p_win = p_vout->p_sys->p_window;
482     
483         p_win->i_buffer_index = p_vout->i_buffer_index;
484         p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
485     p_win->fDirty = true;
486 }
487
488 /* following functions are local */
489
490 /*****************************************************************************
491  * BeosOpenDisplay: open and initialize BeOS device
492  *****************************************************************************
493  * XXX?? The framebuffer mode is only provided as a fast and efficient way to
494  * display video, providing the card is configured and the mode ok. It is
495  * not portable, and is not supposed to work with many cards. Use at your
496  * own risk !
497  *****************************************************************************/
498
499 static int BeosOpenDisplay( vout_thread_t *p_vout )
500
501     p_vout->p_sys->p_window =
502         new VideoWindow(  BRect( 50, 150, 50+p_vout->i_width-1, 150+p_vout->i_height-1 ), NULL, p_vout );
503     if( p_vout->p_sys->p_window == 0 )
504     {
505         free( p_vout->p_sys );
506         intf_ErrMsg( "error: cannot allocate memory for VideoWindow" );
507         return( 1 );
508     }   
509     VideoWindow * p_win = p_vout->p_sys->p_window;
510     
511     p_vout->i_screen_depth =         p_win->i_screen_depth;
512     p_vout->i_bytes_per_pixel =      p_win->i_bytes_per_pixel;
513     p_vout->i_bytes_per_line =       p_vout->i_width*p_win->i_bytes_per_pixel;
514     
515     switch( p_vout->i_screen_depth )
516     {
517     case 8:
518         intf_ErrMsg( "vout error: 8 bit mode not fully supported" );
519         break;
520     case 15:
521         p_vout->i_red_mask =        0x7c00;
522         p_vout->i_green_mask =      0x03e0;
523         p_vout->i_blue_mask =       0x001f;
524         break;
525     case 16:
526         p_vout->i_red_mask =        0xf800;
527         p_vout->i_green_mask =      0x07e0;
528         p_vout->i_blue_mask =       0x001f;
529         break;
530     case 24:
531     case 32:
532     default:
533         p_vout->i_red_mask =        0xff0000;
534         p_vout->i_green_mask =      0x00ff00;
535         p_vout->i_blue_mask =       0x0000ff;
536         break;
537     }
538     return( 0 );
539 }
540
541 /*****************************************************************************
542  * BeosDisplay: close and reset BeOS device
543  *****************************************************************************
544  * Returns all resources allocated by BeosOpenDisplay and restore the original
545  * state of the device.
546  *****************************************************************************/
547 static void BeosCloseDisplay( vout_thread_t *p_vout )
548 {    
549     /* Destroy the video window */
550     p_vout->p_sys->p_window->Lock();
551     p_vout->p_sys->p_window->Quit();
552 }
553
554 } /* extern "C" */