]> git.sesse.net Git - vlc/blob - plugins/beos/vout_beos.cpp
Interface changes to include menu for Title and chapter navigation.
[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.31 2001/10/21 06:06:20 tcastley 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 #define MODULE_NAME beos
28 #include "modules_inner.h"
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #include "defs.h"
34
35 #include <errno.h>                                                 /* ENOMEM */
36 #include <stdlib.h>                                                /* free() */
37 #include <stdio.h>
38 #include <string.h>                                            /* strerror() */
39 #include <kernel/OS.h>
40 #include <View.h>
41 #include <Application.h>
42 #include <Window.h>
43 #include <Locker.h>
44 #include <Screen.h>
45 #include <malloc.h>
46 #include <string.h>
47
48 extern "C"
49 {
50 #include "config.h"
51 #include "common.h"
52 #include "threads.h"
53 #include "mtime.h"
54 #include "tests.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 #include "modules.h"
65 #include "modules_export.h"
66 }
67
68 #include "VideoWindow.h"
69 #include <Screen.h>
70
71 #define WIDTH 128
72 #define HEIGHT 64
73 #define BITS_PER_PLANE 16
74 #define BYTES_PER_PIXEL 2
75
76 /*****************************************************************************
77  * vout_sys_t: BeOS video output method descriptor
78  *****************************************************************************
79  * This structure is part of the video output thread descriptor.
80  * It describes the BeOS specific properties of an output thread.
81  *****************************************************************************/
82  
83 typedef struct vout_sys_s
84 {
85     VideoWindow *         p_window;
86
87     byte_t *              pp_buffer[2];
88     s32                   i_width;
89     s32                   i_height;
90 } vout_sys_t;
91
92
93 /*****************************************************************************
94  * beos_GetAppWindow : retrieve a BWindow pointer from the window name
95  *****************************************************************************/
96
97 BWindow *beos_GetAppWindow(char *name)
98 {
99     int32       index;
100     BWindow     *window;
101     
102     for (index = 0 ; ; index++)
103     {
104         window = be_app->WindowAt(index);
105         if (window == NULL)
106             break;
107         if (window->LockWithTimeout(20000) == B_OK)
108         {
109             if (strcmp(window->Name(), name) == 0)
110             {
111                 window->Unlock();
112                 break;
113             }
114             window->Unlock();
115         }
116     }
117     return window; 
118 }
119
120 /*****************************************************************************
121  * DrawingThread : thread that really does the drawing
122  *****************************************************************************/
123
124 int32 Draw(void *data)
125 {
126   VideoWindow *p_win;
127   p_win = (VideoWindow*) data;
128           if (p_win->LockLooper())
129       {
130         p_win->view->DrawBitmap( p_win->bitmap[p_win->i_buffer_index],
131                               p_win->bitmap[p_win->i_buffer_index]->Bounds(),
132                               p_win->Bounds());  
133         p_win->UnlockLooper();
134       }
135   return B_OK;
136 }
137
138 /*****************************************************************************
139  * VideoWindow constructor and destructor
140  *****************************************************************************/
141
142 VideoWindow::VideoWindow(BRect frame, const char *name, vout_thread_t *p_video_output )
143         : BWindow(frame, name, B_TITLED_WINDOW, B_OUTLINE_RESIZE)
144 {
145
146         /* set the VideoWindow variables */
147     teardownwindow = false;
148     is_zoomed = false;
149         p_video_output->b_YCbr = false;
150         
151         /* create the view to do the display */
152     view = new VLCView(Bounds());
153     AddChild(view);
154     
155     /* Bitmap mode overlay not available */
156         bitmap[0] = new BBitmap(Bounds(), B_RGB32);
157         bitmap[1] = new BBitmap(Bounds(), B_RGB32);
158
159         i_screen_depth = 32;
160         SetTitle(VOUT_TITLE " (BBitmap output)");
161
162         memset(bitmap[0]->Bits(), 0, bitmap[0]->BitsLength());
163         memset(bitmap[1]->Bits(), 0, bitmap[1]->BitsLength());
164
165         i_width = bitmap[0]->Bounds().IntegerWidth();
166         i_height = bitmap[0]->Bounds().IntegerHeight();
167         i_bytes_per_pixel = bitmap[0]->BytesPerRow()/i_width;
168     fRowBytes = bitmap[0]->BytesPerRow();
169         i_screen_depth = 8 * i_bytes_per_pixel;
170
171     Show();
172 }
173
174 VideoWindow::~VideoWindow()
175 {
176     int32 result;
177
178     Hide();
179     Sync();
180     teardownwindow = true;
181     wait_for_thread(fDrawThreadID, &result);
182         delete bitmap[0];
183         delete bitmap[1];
184  }
185
186
187 /*****************************************************************************
188  * VideoWindow::FrameResized
189  *****************************************************************************/
190 void VideoWindow::FrameResized( float width, float height )
191 {
192         if (is_zoomed)
193         {
194             return;
195         }
196         float width_scale;
197         float height_scale;
198         float orig_width = bitmap[0]->Bounds().Width();
199         float orig_height = bitmap[0]->Bounds().Height();
200
201         width_scale = width / orig_width;
202         height_scale = height / orig_height;
203         
204     /* if the width is proportionally smaller */
205     if (width_scale <= height_scale)
206     {
207         ResizeTo(width, orig_height * width_scale);
208     }
209     else /* if the height is proportionally smaller */
210     {
211         ResizeTo(orig_width * height_scale, height);
212     }
213 }
214
215 /*****************************************************************************
216  * VideoWindow::Zoom
217  *****************************************************************************/
218
219 void VideoWindow::Zoom(BPoint origin, float width, float height )
220 {
221         if(is_zoomed)
222         {
223                 MoveTo(rect.left, rect.top);
224                 ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
225                 be_app->ShowCursor();
226         }
227         else
228         {
229                 rect = Frame();
230                 BScreen *screen;
231                 screen = new BScreen(this);
232                 BRect rect = screen->Frame();
233                 delete screen;
234                 MoveTo(0,0);
235                 ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
236                 be_app->HideCursor();
237         }
238         is_zoomed = !is_zoomed;
239 }
240
241 /*****************************************************************************
242  * VideoWindow::MessageReceived
243  *****************************************************************************/
244
245 void VideoWindow::MessageReceived( BMessage * p_message )
246 {
247     BWindow * p_win;
248     
249     switch( p_message->what )
250     {
251     case B_KEY_DOWN:
252     case B_SIMPLE_DATA:
253         // post the message to the interface window which will handle it
254         p_win = beos_GetAppWindow( "interface" );
255         if( p_win != NULL )
256         {
257             p_win->PostMessage( p_message );
258         }
259         break;
260     
261     default:
262         BWindow::MessageReceived( p_message );
263         break;
264     }
265 }
266
267 /*****************************************************************************
268  * VideoWindow::QuitRequested
269  *****************************************************************************/
270
271 bool VideoWindow::QuitRequested()
272 {
273     /* FIXME: send a message ! */
274     p_main->p_intf->b_die = 1;
275     teardownwindow = true;
276     return( false );
277 }
278
279 /*****************************************************************************
280  * VLCView::VLCView
281  *****************************************************************************/
282 VLCView::VLCView(BRect bounds) : BView(bounds, "", B_FOLLOW_ALL, B_WILL_DRAW)
283 {
284         SetViewColor(B_TRANSPARENT_32_BIT);
285 }
286
287 /*****************************************************************************
288  * VLCView::~VLCView
289  *****************************************************************************/
290 VLCView::~VLCView()
291 {
292
293 }
294
295 /*****************************************************************************
296  * VLCVIew::~VLCView
297  *****************************************************************************/
298 void VLCView::MouseDown(BPoint point)
299 {
300         VideoWindow *w = (VideoWindow *) Window();
301         if(w->is_zoomed)
302         {
303                 BWindow *win = Window();
304                 win->Zoom();
305         }
306 }
307
308 extern "C"
309 {
310
311 /*****************************************************************************
312  * Local prototypes
313  *****************************************************************************/
314 static int  vout_Probe      ( probedata_t *p_data );
315 static int  vout_Create     ( struct vout_thread_s * );
316 static int  vout_Init       ( struct vout_thread_s * );
317 static void vout_End        ( struct vout_thread_s * );
318 static void vout_Destroy    ( struct vout_thread_s * );
319 static int  vout_Manage     ( struct vout_thread_s * );
320 static void vout_Display    ( struct vout_thread_s * );
321
322 static int  BeosOpenDisplay ( vout_thread_t *p_vout );
323 static void BeosCloseDisplay( vout_thread_t *p_vout );
324
325 /*****************************************************************************
326  * Functions exported as capabilities. They are declared as static so that
327  * we don't pollute the namespace too much.
328  *****************************************************************************/
329 void _M( vout_getfunctions )( function_list_t * p_function_list )
330 {
331     p_function_list->pf_probe = vout_Probe;
332     p_function_list->functions.vout.pf_create     = vout_Create;
333     p_function_list->functions.vout.pf_init       = vout_Init;
334     p_function_list->functions.vout.pf_end        = vout_End;
335     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
336     p_function_list->functions.vout.pf_manage     = vout_Manage;
337     p_function_list->functions.vout.pf_display    = vout_Display;
338     p_function_list->functions.vout.pf_setpalette = NULL;
339 }
340
341 /*****************************************************************************
342  * vout_Probe: probe the video driver and return a score
343  *****************************************************************************
344  * This function tries to initialize SDL and returns a score to the
345  * plugin manager so that it can select the best plugin.
346  *****************************************************************************/
347 static int vout_Probe( probedata_t *p_data )
348 {
349     if( TestMethod( VOUT_METHOD_VAR, "beos" ) )
350     {
351         return( 999 );
352     }
353     return( 100 );
354 }
355
356 /*****************************************************************************
357  * vout_Create: allocates BeOS video thread output method
358  *****************************************************************************
359  * This function allocates and initializes a BeOS vout method.
360  *****************************************************************************/
361 int vout_Create( vout_thread_t *p_vout )
362 {
363     /* Allocate structure */
364     p_vout->p_sys = (vout_sys_t*) malloc( sizeof( vout_sys_t ) );
365     if( p_vout->p_sys == NULL )
366     {
367         intf_ErrMsg( "error: %s", strerror(ENOMEM) );
368         return( 1 );
369     }
370     
371         p_vout->b_scale = false;
372
373     /* Open and initialize device */
374     if( BeosOpenDisplay( p_vout ) )
375     {
376         intf_ErrMsg("vout error: can't open display");
377         free( p_vout->p_sys );
378         return( 1 );
379     }
380
381     return( 0 );
382 }
383
384 /*****************************************************************************
385  * vout_Init: initialize BeOS video thread output method
386  *****************************************************************************/
387 int vout_Init( vout_thread_t *p_vout )
388 {
389     VideoWindow * p_win = p_vout->p_sys->p_window;
390     u32 i_page_size;
391     i_page_size =   p_vout->i_width * p_vout->i_height * p_vout->i_bytes_per_pixel;
392     if((p_win->bitmap[0] != NULL) && (p_win->bitmap[1] != NULL))
393         {
394         p_vout->pf_setbuffers( p_vout,
395                (byte_t *)p_win->bitmap[0]->Bits(),
396                (byte_t *)p_win->bitmap[1]->Bits());
397     }
398     return( 0 );
399 }
400
401 /*****************************************************************************
402  * vout_End: terminate BeOS video thread output method
403  *****************************************************************************/
404 void vout_End( vout_thread_t *p_vout )
405 {
406 }
407
408 /*****************************************************************************
409  * vout_Destroy: destroy BeOS video thread output method
410  *****************************************************************************
411  * Terminate an output method created by DummyCreateOutputMethod
412  *****************************************************************************/
413 void vout_Destroy( vout_thread_t *p_vout )
414 {
415     BeosCloseDisplay( p_vout );
416     free( p_vout->p_sys );
417 }
418
419 /*****************************************************************************
420  * vout_Manage: handle BeOS events
421  *****************************************************************************
422  * This function should be called regularly by video output thread. It manages
423  * console events. It returns a non null value on error.
424  *****************************************************************************/
425 int vout_Manage( vout_thread_t *p_vout )
426 {
427     VideoWindow * p_win = p_vout->p_sys->p_window;
428     
429     if ((p_vout->p_buffer[p_vout->i_buffer_index].i_pic_width != p_win->i_width + 1) &&
430         (p_vout->p_buffer[p_vout->i_buffer_index].i_pic_height != p_win->i_height + 1) &&
431         (p_vout->p_buffer[p_vout->i_buffer_index].i_pic_width != 0 ))
432     {
433         if (p_win->Lock())
434         {
435             p_win->view->ClearViewBitmap();
436             intf_Msg("Starting Change");
437             intf_Msg("New width: %d Height: %d", 
438                 p_vout->p_buffer[p_vout->i_buffer_index ].i_pic_width,
439                 p_vout->p_buffer[p_vout->i_buffer_index ].i_pic_height);
440
441             p_win->i_width = p_vout->p_buffer[p_vout->i_buffer_index].i_pic_width - 1;
442             p_win->i_height = p_vout->p_buffer[p_vout->i_buffer_index].i_pic_height -1;
443             p_win->ResizeTo((float) p_win->i_width, (float) p_win->i_height); 
444             p_win->Unlock();
445         }
446     }
447         return( 0 );
448 }
449
450 /*****************************************************************************
451  * vout_Display: displays previously rendered output
452  *****************************************************************************
453  * This function send the currently rendered image to BeOS image, waits until
454  * it is displayed and switch the two rendering buffers, preparing next frame.
455  *****************************************************************************/
456 void vout_Display( vout_thread_t *p_vout )
457 {
458         status_t status;
459
460     VideoWindow * p_win = p_vout->p_sys->p_window;
461     
462         p_win->i_buffer_index = p_vout->i_buffer_index;
463         p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
464         if (!p_win->teardownwindow)
465         {
466            p_win->fDrawThreadID = spawn_thread(Draw, "drawing_thread",
467               B_DISPLAY_PRIORITY, (void*) p_win);
468        wait_for_thread(p_win->fDrawThreadID, &status);
469     }
470 }
471
472 /* following functions are local */
473
474 /*****************************************************************************
475  * BeosOpenDisplay: open and initialize BeOS device
476  *****************************************************************************
477  * XXX?? The framebuffer mode is only provided as a fast and efficient way to
478  * display video, providing the card is configured and the mode ok. It is
479  * not portable, and is not supposed to work with many cards. Use at your
480  * own risk !
481  *****************************************************************************/
482
483 static int BeosOpenDisplay( vout_thread_t *p_vout )
484
485     
486     p_vout->p_sys->p_window =
487         new VideoWindow(  BRect( 80, 50, 80+p_vout->i_width-1, 50+p_vout->i_height-1 ), NULL, p_vout );
488     if( p_vout->p_sys->p_window == 0 )
489     {
490         free( p_vout->p_sys );
491         intf_ErrMsg( "error: cannot allocate memory for VideoWindow" );
492         return( 1 );
493     }   
494     VideoWindow * p_win = p_vout->p_sys->p_window;
495     
496     p_vout->i_screen_depth =         p_win->i_screen_depth;
497     p_vout->i_bytes_per_pixel =      p_win->i_bytes_per_pixel;
498     p_vout->i_width =                            p_win->i_width + 1;
499     p_vout->i_height =                           p_win->i_height + 1;
500     p_vout->i_bytes_per_line =       p_vout->i_width*p_win->i_bytes_per_pixel;
501
502     p_vout->i_red_mask =        0xff0000;
503     p_vout->i_green_mask =      0x00ff00;
504     p_vout->i_blue_mask =       0x0000ff;
505     return( 0 );
506 }
507
508 /*****************************************************************************
509  * BeosDisplay: close and reset BeOS device
510  *****************************************************************************
511  * Returns all resources allocated by BeosOpenDisplay and restore the original
512  * state of the device.
513  *****************************************************************************/
514 static void BeosCloseDisplay( vout_thread_t *p_vout )
515 {    
516     /* Destroy the video window */
517     p_vout->p_sys->p_window->Lock();
518     p_vout->p_sys->p_window->Quit();
519 }
520
521 } /* extern "C" */