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