1 /*****************************************************************************
2 * vout_beos.cpp: beos video output display method
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: vout_beos.cpp,v 1.23 2001/03/25 17:09:14 richards 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>
58 #include "video_output.h"
60 #include "interface.h"
66 #include "VideoWindow.h"
71 #define BITS_PER_PLANE 16
72 #define BYTES_PER_PIXEL 2
74 /*****************************************************************************
75 * vout_sys_t: BeOS video output method descriptor
76 *****************************************************************************
77 * This structure is part of the video output thread descriptor.
78 * It describes the BeOS specific properties of an output thread.
79 *****************************************************************************/
81 typedef struct vout_sys_s
83 VideoWindow * p_window;
85 byte_t * pp_buffer[2];
91 /*****************************************************************************
92 * beos_GetAppWindow : retrieve a BWindow pointer from the window name
93 *****************************************************************************/
95 BWindow *beos_GetAppWindow(char *name)
100 for (index = 0 ; ; index++)
102 window = be_app->WindowAt(index);
105 if (window->LockWithTimeout(200000) == B_OK)
107 if (strcmp(window->Name(), name) == 0)
118 /*****************************************************************************
119 * DrawingThread : thread that really does the drawing
120 *****************************************************************************/
122 int32 DrawingThread(void *data)
125 w = (VideoWindow*) data;
127 while(!w->teardownwindow)
132 w->view->DrawBitmap(w->bitmap[w->i_buffer_index], w->bitmap[w->i_buffer_index]->Bounds(), w->Bounds());
141 /*****************************************************************************
142 * VideoWindow constructor and destructor
143 *****************************************************************************/
145 VideoWindow::VideoWindow(BRect frame, const char *name, vout_thread_t *p_video_output )
146 : BWindow(frame, name, B_TITLED_WINDOW, NULL)
148 float minWidth, minHeight, maxWidth, maxHeight;
150 teardownwindow = false;
152 p_vout = p_video_output;
153 fDrawThreadID = NULL;
158 view = new VLCView(Bounds());
160 bitmap[0] = new BBitmap(Bounds(), B_BITMAP_WILL_OVERLAY|B_BITMAP_RESERVE_OVERLAY_CHANNEL, B_YCbCr422);
161 fUsingOverlay = true;
163 p_vout->b_YCbr = true;
165 if (bitmap[0]->InitCheck() != B_OK)
168 p_vout->b_YCbr = false;
169 fUsingOverlay = false;
171 screen = new BScreen();
172 color_space space = screen->ColorSpace();
177 bitmap[0] = new BBitmap(Bounds(), B_RGB15);
178 bitmap[1] = new BBitmap(Bounds(), B_RGB15);
181 else if(space == B_RGB16)
183 bitmap[0] = new BBitmap(Bounds(), B_RGB16);
184 bitmap[1] = new BBitmap(Bounds(), B_RGB16);
187 else //default to 32bpp
189 bitmap[0] = new BBitmap(Bounds(), B_RGB32);
190 bitmap[1] = new BBitmap(Bounds(), B_RGB32);
193 memset(bitmap[0]->Bits(), 0, bitmap[0]->BitsLength());
194 memset(bitmap[1]->Bits(), 0, bitmap[1]->BitsLength());
195 SetTitle(VOUT_TITLE " (BBitmap output)");
201 view->SetViewOverlay(bitmap[0], bitmap[0]->Bounds(), Bounds(), &key, B_FOLLOW_ALL,
202 B_OVERLAY_FILTER_HORIZONTAL|B_OVERLAY_FILTER_VERTICAL);
203 view->SetViewColor(key);
204 SetTitle(VOUT_TITLE " (Overlay output)");
205 GetSizeLimits(&minWidth, &maxWidth, &minHeight, &maxHeight);
206 SetSizeLimits((float) Bounds().IntegerWidth(), maxWidth, (float) Bounds().IntegerHeight(), maxHeight);
207 memset(bitmap[0]->Bits(), 0, bitmap[0]->BitsLength());
211 fDrawThreadID = spawn_thread(DrawingThread, "drawing_thread",
212 B_DISPLAY_PRIORITY, (void*) this);
213 resume_thread(fDrawThreadID);
216 i_bytes_per_pixel = bitmap[0]->BytesPerRow()/bitmap[0]->Bounds().IntegerWidth();
217 fRowBytes = bitmap[0]->BytesPerRow();
222 VideoWindow::~VideoWindow()
230 teardownwindow = true;
231 wait_for_thread(fDrawThreadID, &result);
238 /*****************************************************************************
239 * VideoWindow::FrameResized
240 *****************************************************************************/
241 void VideoWindow::FrameResized( float width, float height )
245 /*****************************************************************************
247 *****************************************************************************/
249 void VideoWindow::Zoom(BPoint origin, float width, float height )
253 MoveTo(rect.left, rect.top);
254 ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
255 be_app->ShowCursor();
261 screen = new BScreen(this);
262 BRect rect = screen->Frame();
265 ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
266 be_app->HideCursor();
268 is_zoomed = !is_zoomed;
271 /*****************************************************************************
272 * VideoWindow::MessageReceived
273 *****************************************************************************/
275 void VideoWindow::MessageReceived( BMessage * p_message )
279 switch( p_message->what )
283 // post the message to the interface window which will handle it
284 p_win = beos_GetAppWindow( "interface" );
287 p_win->PostMessage( p_message );
292 BWindow::MessageReceived( p_message );
297 /*****************************************************************************
298 * VideoWindow::QuitRequested
299 *****************************************************************************/
301 bool VideoWindow::QuitRequested()
303 /* FIXME: send a message ! */
304 p_main->p_intf->b_die = 1;
305 teardownwindow = true;
309 /*****************************************************************************
311 *****************************************************************************/
312 VLCView::VLCView(BRect bounds) : BView(bounds, "", B_FOLLOW_ALL, B_WILL_DRAW)
314 SetViewColor(B_TRANSPARENT_32_BIT);
317 /*****************************************************************************
319 *****************************************************************************/
325 /*****************************************************************************
327 *****************************************************************************/
328 void VLCView::MouseDown(BPoint point)
330 VideoWindow *w = (VideoWindow *) Window();
333 BWindow *win = Window();
341 /*****************************************************************************
343 *****************************************************************************/
344 static int vout_Probe ( probedata_t *p_data );
345 static int vout_Create ( struct vout_thread_s * );
346 static int vout_Init ( struct vout_thread_s * );
347 static void vout_End ( struct vout_thread_s * );
348 static void vout_Destroy ( struct vout_thread_s * );
349 static int vout_Manage ( struct vout_thread_s * );
350 static void vout_Display ( struct vout_thread_s * );
352 static int BeosOpenDisplay ( vout_thread_t *p_vout );
353 static void BeosCloseDisplay( vout_thread_t *p_vout );
355 /*****************************************************************************
356 * Functions exported as capabilities. They are declared as static so that
357 * we don't pollute the namespace too much.
358 *****************************************************************************/
359 void _M( vout_getfunctions )( function_list_t * p_function_list )
361 p_function_list->pf_probe = vout_Probe;
362 p_function_list->functions.vout.pf_create = vout_Create;
363 p_function_list->functions.vout.pf_init = vout_Init;
364 p_function_list->functions.vout.pf_end = vout_End;
365 p_function_list->functions.vout.pf_destroy = vout_Destroy;
366 p_function_list->functions.vout.pf_manage = vout_Manage;
367 p_function_list->functions.vout.pf_display = vout_Display;
368 p_function_list->functions.vout.pf_setpalette = NULL;
371 /*****************************************************************************
372 * vout_Probe: probe the video driver and return a score
373 *****************************************************************************
374 * This function tries to initialize SDL and returns a score to the
375 * plugin manager so that it can select the best plugin.
376 *****************************************************************************/
377 static int vout_Probe( probedata_t *p_data )
379 if( TestMethod( VOUT_METHOD_VAR, "beos" ) )
387 /*****************************************************************************
388 * vout_Create: allocates BeOS video thread output method
389 *****************************************************************************
390 * This function allocates and initializes a BeOS vout method.
391 *****************************************************************************/
392 int vout_Create( vout_thread_t *p_vout )
394 /* Allocate structure */
395 p_vout->p_sys = (vout_sys_t*) malloc( sizeof( vout_sys_t ) );
396 if( p_vout->p_sys == NULL )
398 intf_ErrMsg( "error: %s", strerror(ENOMEM) );
402 /* Set video window's size */
403 p_vout->i_width = main_GetIntVariable( VOUT_WIDTH_VAR,
404 VOUT_WIDTH_DEFAULT );
405 p_vout->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
406 VOUT_HEIGHT_DEFAULT );
408 /* Open and initialize device */
409 if( BeosOpenDisplay( p_vout ) )
411 intf_ErrMsg("vout error: can't open display");
412 free( p_vout->p_sys );
419 /*****************************************************************************
420 * vout_Init: initialize BeOS video thread output method
421 *****************************************************************************/
422 int vout_Init( vout_thread_t *p_vout )
424 VideoWindow * p_win = p_vout->p_sys->p_window;
428 i_page_size = p_vout->i_width * p_vout->i_height * p_vout->i_bytes_per_pixel;
430 p_vout->p_sys->i_width = p_vout->i_width;
431 p_vout->p_sys->i_height = p_vout->i_height;
433 if(p_win->fUsingOverlay)
435 if(p_win->bitmap[0] != NULL)
437 vout_SetBuffers( p_vout, (byte_t *)p_win->bitmap[0]->Bits(),
438 (byte_t *)p_win->bitmap[0]->Bits());
439 delete p_win->bitmap[0];
440 p_win->bitmap[0] = NULL;
445 if((p_win->bitmap[0] != NULL) && (p_win->bitmap[1] != NULL))
447 vout_SetBuffers( p_vout, (byte_t *)p_win->bitmap[0]->Bits(),
448 (byte_t *)p_win->bitmap[1]->Bits());
454 /*****************************************************************************
455 * vout_End: terminate BeOS video thread output method
456 *****************************************************************************/
457 void vout_End( vout_thread_t *p_vout )
461 /*****************************************************************************
462 * vout_Destroy: destroy BeOS video thread output method
463 *****************************************************************************
464 * Terminate an output method created by DummyCreateOutputMethod
465 *****************************************************************************/
466 void vout_Destroy( vout_thread_t *p_vout )
468 BeosCloseDisplay( p_vout );
470 free( p_vout->p_sys );
473 /*****************************************************************************
474 * vout_Manage: handle BeOS events
475 *****************************************************************************
476 * This function should be called regularly by video output thread. It manages
477 * console events. It returns a non null value on error.
478 *****************************************************************************/
479 int vout_Manage( vout_thread_t *p_vout )
481 VideoWindow * p_win = p_vout->p_sys->p_window;
483 float minWidth, minHeight, maxWidth, maxHeight;
485 if( (p_vout->i_width != p_vout->p_sys->i_width) ||
486 (p_vout->i_height != p_vout->p_sys->i_height) )
488 /* If video output size has changed, change interface window size */
489 intf_DbgMsg( "resizing output window" );
490 if(p_win->fUsingOverlay)
493 p_win->view->ClearViewOverlay();
494 p_vout->p_sys->i_width = p_vout->i_width;
495 p_vout->p_sys->i_height = p_vout->i_height;;
496 p_win->GetSizeLimits(&minWidth, &maxWidth, &minHeight, &maxHeight);
497 p_win->SetSizeLimits((float) p_vout->p_sys->i_width, maxWidth, (float) p_vout->p_sys->i_height, maxHeight);
498 p_win->ResizeTo(p_vout->p_sys->i_width, p_vout->p_sys->i_height);
499 p_win->bitmap[0] = new BBitmap(p_win->Bounds(),
500 B_BITMAP_WILL_OVERLAY|B_BITMAP_RESERVE_OVERLAY_CHANNEL,
502 memset(p_win->bitmap[0]->Bits(), 0, p_win->bitmap[0]->BitsLength());
503 p_win->view->SetViewOverlay(p_win->bitmap[0], p_win->bitmap[0]->Bounds(), p_win->Bounds(), &key, B_FOLLOW_ALL,
504 B_OVERLAY_FILTER_HORIZONTAL|B_OVERLAY_FILTER_VERTICAL);
505 p_win->view->SetViewColor(key);
507 vout_SetBuffers( p_vout, (byte_t *)p_win->bitmap[0]->Bits(),
508 (byte_t *)p_win->bitmap[0]->Bits());
509 delete p_win->bitmap[0];
515 /*****************************************************************************
516 * vout_Display: displays previously rendered output
517 *****************************************************************************
518 * This function send the currently rendered image to BeOS image, waits until
519 * it is displayed and switch the two rendering buffers, preparing next frame.
520 *****************************************************************************/
521 void vout_Display( vout_thread_t *p_vout )
523 VideoWindow * p_win = p_vout->p_sys->p_window;
525 p_win->i_buffer_index = p_vout->i_buffer_index;
526 p_vout->i_buffer_index = ++p_vout->i_buffer_index & 1;
527 p_win->fDirty = true;
530 /* following functions are local */
532 /*****************************************************************************
533 * BeosOpenDisplay: open and initialize BeOS device
534 *****************************************************************************
535 * XXX?? The framebuffer mode is only provided as a fast and efficient way to
536 * display video, providing the card is configured and the mode ok. It is
537 * not portable, and is not supposed to work with many cards. Use at your
539 *****************************************************************************/
541 static int BeosOpenDisplay( vout_thread_t *p_vout )
543 p_vout->p_sys->p_window =
544 new VideoWindow( BRect( 80, 50, 80+p_vout->i_width-1, 50+p_vout->i_height-1 ), NULL, p_vout );
545 if( p_vout->p_sys->p_window == 0 )
547 free( p_vout->p_sys );
548 intf_ErrMsg( "error: cannot allocate memory for VideoWindow" );
551 VideoWindow * p_win = p_vout->p_sys->p_window;
553 p_vout->i_screen_depth = p_win->i_screen_depth;
554 p_vout->i_bytes_per_pixel = p_win->i_bytes_per_pixel;
555 p_vout->i_bytes_per_line = p_vout->i_width*p_win->i_bytes_per_pixel;
557 switch( p_vout->i_screen_depth )
560 intf_ErrMsg( "vout error: 8 bit mode not fully supported" );
563 p_vout->i_red_mask = 0x7c00;
564 p_vout->i_green_mask = 0x03e0;
565 p_vout->i_blue_mask = 0x001f;
568 p_vout->i_red_mask = 0xf800;
569 p_vout->i_green_mask = 0x07e0;
570 p_vout->i_blue_mask = 0x001f;
575 p_vout->i_red_mask = 0xff0000;
576 p_vout->i_green_mask = 0x00ff00;
577 p_vout->i_blue_mask = 0x0000ff;
583 /*****************************************************************************
584 * BeosDisplay: close and reset BeOS device
585 *****************************************************************************
586 * Returns all resources allocated by BeosOpenDisplay and restore the original
587 * state of the device.
588 *****************************************************************************/
589 static void BeosCloseDisplay( vout_thread_t *p_vout )
591 /* Destroy the video window */
592 p_vout->p_sys->p_window->Lock();
593 p_vout->p_sys->p_window->Quit();