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 $
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>
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.
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.
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 *****************************************************************************/
27 #define MODULE_NAME beos
28 #include "modules_inner.h"
30 /*****************************************************************************
32 *****************************************************************************/
35 #include <errno.h> /* ENOMEM */
36 #include <stdlib.h> /* free() */
38 #include <string.h> /* strerror() */
39 #include <kernel/OS.h>
41 #include <Application.h>
57 #include "video_output.h"
59 #include "interface.h"
65 #include "modules_export.h"
68 #include "VideoWindow.h"
73 #define BITS_PER_PLANE 16
74 #define BYTES_PER_PIXEL 2
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 *****************************************************************************/
83 typedef struct vout_sys_s
85 VideoWindow * p_window;
87 byte_t * pp_buffer[2];
93 /*****************************************************************************
94 * beos_GetAppWindow : retrieve a BWindow pointer from the window name
95 *****************************************************************************/
97 BWindow *beos_GetAppWindow(char *name)
102 for (index = 0 ; ; index++)
104 window = be_app->WindowAt(index);
107 if (window->LockWithTimeout(20000) == B_OK)
109 if (strcmp(window->Name(), name) == 0)
120 /*****************************************************************************
121 * DrawingThread : thread that really does the drawing
122 *****************************************************************************/
124 int32 Draw(void *data)
127 p_win = (VideoWindow*) data;
128 if (p_win->LockLooper())
130 p_win->view->DrawBitmap( p_win->bitmap[p_win->i_buffer_index],
131 p_win->bitmap[p_win->i_buffer_index]->Bounds(),
133 p_win->UnlockLooper();
138 /*****************************************************************************
139 * VideoWindow constructor and destructor
140 *****************************************************************************/
142 VideoWindow::VideoWindow(BRect frame, const char *name, vout_thread_t *p_video_output )
143 : BWindow(frame, name, B_TITLED_WINDOW, B_OUTLINE_RESIZE)
146 /* set the VideoWindow variables */
147 teardownwindow = false;
149 p_video_output->b_YCbr = false;
151 /* create the view to do the display */
152 view = new VLCView(Bounds());
155 /* Bitmap mode overlay not available */
156 bitmap[0] = new BBitmap(Bounds(), B_RGB32);
157 bitmap[1] = new BBitmap(Bounds(), B_RGB32);
160 SetTitle(VOUT_TITLE " (BBitmap output)");
162 memset(bitmap[0]->Bits(), 0, bitmap[0]->BitsLength());
163 memset(bitmap[1]->Bits(), 0, bitmap[1]->BitsLength());
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;
174 VideoWindow::~VideoWindow()
180 teardownwindow = true;
181 wait_for_thread(fDrawThreadID, &result);
187 /*****************************************************************************
188 * VideoWindow::FrameResized
189 *****************************************************************************/
190 void VideoWindow::FrameResized( float width, float height )
198 float orig_width = bitmap[0]->Bounds().Width();
199 float orig_height = bitmap[0]->Bounds().Height();
201 width_scale = width / orig_width;
202 height_scale = height / orig_height;
204 /* if the width is proportionally smaller */
205 if (width_scale <= height_scale)
207 ResizeTo(width, orig_height * width_scale);
209 else /* if the height is proportionally smaller */
211 ResizeTo(orig_width * height_scale, height);
215 /*****************************************************************************
217 *****************************************************************************/
219 void VideoWindow::Zoom(BPoint origin, float width, float height )
223 MoveTo(rect.left, rect.top);
224 ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
225 be_app->ShowCursor();
231 screen = new BScreen(this);
232 BRect rect = screen->Frame();
235 ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
236 be_app->HideCursor();
238 is_zoomed = !is_zoomed;
241 /*****************************************************************************
242 * VideoWindow::MessageReceived
243 *****************************************************************************/
245 void VideoWindow::MessageReceived( BMessage * p_message )
249 switch( p_message->what )
253 // post the message to the interface window which will handle it
254 p_win = beos_GetAppWindow( "interface" );
257 p_win->PostMessage( p_message );
262 BWindow::MessageReceived( p_message );
267 /*****************************************************************************
268 * VideoWindow::QuitRequested
269 *****************************************************************************/
271 bool VideoWindow::QuitRequested()
273 /* FIXME: send a message ! */
274 p_main->p_intf->b_die = 1;
275 teardownwindow = true;
279 /*****************************************************************************
281 *****************************************************************************/
282 VLCView::VLCView(BRect bounds) : BView(bounds, "", B_FOLLOW_ALL, B_WILL_DRAW)
284 SetViewColor(B_TRANSPARENT_32_BIT);
287 /*****************************************************************************
289 *****************************************************************************/
295 /*****************************************************************************
297 *****************************************************************************/
298 void VLCView::MouseDown(BPoint point)
300 VideoWindow *w = (VideoWindow *) Window();
303 BWindow *win = Window();
311 /*****************************************************************************
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 * );
322 static int BeosOpenDisplay ( vout_thread_t *p_vout );
323 static void BeosCloseDisplay( vout_thread_t *p_vout );
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 )
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;
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 )
349 if( TestMethod( VOUT_METHOD_VAR, "beos" ) )
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 )
363 /* Allocate structure */
364 p_vout->p_sys = (vout_sys_t*) malloc( sizeof( vout_sys_t ) );
365 if( p_vout->p_sys == NULL )
367 intf_ErrMsg( "error: %s", strerror(ENOMEM) );
371 p_vout->b_scale = false;
373 /* Open and initialize device */
374 if( BeosOpenDisplay( p_vout ) )
376 intf_ErrMsg("vout error: can't open display");
377 free( p_vout->p_sys );
384 /*****************************************************************************
385 * vout_Init: initialize BeOS video thread output method
386 *****************************************************************************/
387 int vout_Init( vout_thread_t *p_vout )
389 VideoWindow * p_win = p_vout->p_sys->p_window;
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))
394 p_vout->pf_setbuffers( p_vout,
395 (byte_t *)p_win->bitmap[0]->Bits(),
396 (byte_t *)p_win->bitmap[1]->Bits());
401 /*****************************************************************************
402 * vout_End: terminate BeOS video thread output method
403 *****************************************************************************/
404 void vout_End( vout_thread_t *p_vout )
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 )
415 BeosCloseDisplay( p_vout );
416 free( p_vout->p_sys );
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 )
427 VideoWindow * p_win = p_vout->p_sys->p_window;
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 ))
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);
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);
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 )
460 VideoWindow * p_win = p_vout->p_sys->p_window;
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)
466 p_win->fDrawThreadID = spawn_thread(Draw, "drawing_thread",
467 B_DISPLAY_PRIORITY, (void*) p_win);
468 wait_for_thread(p_win->fDrawThreadID, &status);
472 /* following functions are local */
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
481 *****************************************************************************/
483 static int BeosOpenDisplay( vout_thread_t *p_vout )
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 )
490 free( p_vout->p_sys );
491 intf_ErrMsg( "error: cannot allocate memory for VideoWindow" );
494 VideoWindow * p_win = p_vout->p_sys->p_window;
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;
502 p_vout->i_red_mask = 0xff0000;
503 p_vout->i_green_mask = 0x00ff00;
504 p_vout->i_blue_mask = 0x0000ff;
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 )
516 /* Destroy the video window */
517 p_vout->p_sys->p_window->Lock();
518 p_vout->p_sys->p_window->Quit();