1 /*****************************************************************************
2 * qte.cpp : QT Embedded plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 1998-2003 the VideoLAN team
7 * Authors: Gerald Hansink <gerald.hansink@ordina.nl>
8 * Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 /*****************************************************************************
31 * - written for ipaq, so hardcoded assumptions specific for ipaq...
33 * - no "mouse events" handling
35 *****************************************************************************/
39 #include <errno.h> /* ENOMEM */
45 #include <vlc_common.h>
46 #include <vlc_plugin.h>
47 #include <vlc_interface.h>
50 #ifdef HAVE_MACHINE_PARAM_H
52 # include <machine/param.h>
53 # include <sys/types.h> /* typedef ushort */
58 # include <netinet/in.h> /* BSD: struct in_addr */
62 # include <sys/shm.h> /* shmget(), shmctl() */
66 #include <qapplication.h>
70 # define USE_DIRECT_PAINTER
71 # include <qdirectpainter_qws.h>
72 # include <qgfxraster_qws.h>
79 /*****************************************************************************
81 *****************************************************************************/
82 #define DISPLAY_TEXT N_("QT Embedded display")
83 #define DISPLAY_LONGTEXT N_( \
84 "Qt Embedded hardware display to use. " \
85 "By default VLC will use the value of the DISPLAY environment variable.")
87 /*****************************************************************************
89 *****************************************************************************/
90 static int Open ( vlc_object_t * );
91 static void Close ( vlc_object_t * );
92 static void Render ( vout_thread_t *, picture_t * );
93 static void Display ( vout_thread_t *, picture_t * );
94 static int Manage ( vout_thread_t * );
95 static int Init ( vout_thread_t * );
96 static void End ( vout_thread_t * );
98 static int OpenDisplay ( vout_thread_t * );
99 static void CloseDisplay( vout_thread_t * );
101 static int NewPicture ( vout_thread_t *, picture_t * );
102 static void FreePicture ( vout_thread_t *, picture_t * );
104 static void ToggleFullScreen ( vout_thread_t * );
106 static void* RunQtThread( vlc_object_t *p_this );
109 /*****************************************************************************
110 * Exported prototypes
111 *****************************************************************************/
116 set_category( CAT_VIDEO )
117 set_subcategory( SUBCAT_VIDEO_VOUT )
118 // add_category_hint( N_("QT Embedded"), NULL )
119 // add_string( "qte-display", "landscape", NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT)
120 set_description( N_("QT Embedded video output") )
121 set_capability( "video output", 70 )
122 add_shortcut( "qte" )
123 set_callbacks( Open, Close)
128 /*****************************************************************************
129 * Seeking function TODO: put this in a generic location !
130 *****************************************************************************/
131 static inline void vout_Seek( off_t i_seek )
135 /*****************************************************************************
136 * Open: allocate video thread output method
137 *****************************************************************************/
138 static int Open( vlc_object_t *p_this )
140 vout_thread_t * p_vout = (vout_thread_t *)p_this;
142 /* Allocate structure */
143 p_vout->p_sys = (struct vout_sys_t*) malloc( sizeof( struct vout_sys_t ) );
145 if( p_vout->p_sys == NULL )
148 p_vout->pf_init = Init;
149 p_vout->pf_end = End;
150 p_vout->pf_manage = Manage;
151 p_vout->pf_render = NULL; //Render;
152 p_vout->pf_display = Display;
155 p_vout->p_sys->p_qte_main =
156 module_need( p_this, "gui-helper", "qte", true );
157 if( p_vout->p_sys->p_qte_main == NULL )
159 free( p_vout->p_sys );
164 if (OpenDisplay(p_vout))
166 msg_Err( p_vout, "Cannot set up qte video output" );
173 /*****************************************************************************
174 * CloseVideo: destroy Sys video thread output method
175 *****************************************************************************
176 * Terminate an output method created by Open
177 *****************************************************************************/
178 static void Close ( vlc_object_t *p_this )
180 vout_thread_t * p_vout = (vout_thread_t *)p_this;
182 msg_Dbg( p_vout, "close" );
183 if( p_vout->p_sys->p_event )
185 vlc_object_detach( p_vout->p_sys->p_event );
187 /* Kill RunQtThread */
188 vlc_object_kill( p_vout->p_sys->p_event );
189 CloseDisplay(p_vout);
191 vlc_thread_join( p_vout->p_sys->p_event );
192 vlc_object_release( p_vout->p_sys->p_event );
196 msg_Dbg( p_vout, "releasing gui-helper" );
197 module_unneed( p_vout, p_vout->p_sys->p_qte_main );
200 free( p_vout->p_sys );
201 p_vout->p_sys = NULL;
204 /*****************************************************************************
205 * Init: initialize video thread output method
206 *****************************************************************************
207 * This function create the buffers needed by the output thread. It is called
208 * at the beginning of the thread, but also each time the window is resized.
209 *****************************************************************************/
210 static int Init( vout_thread_t *p_vout )
214 int dd = QPixmap::defaultDepth();
216 I_OUTPUTPICTURES = 0;
218 p_vout->output.i_chroma = (dd == 16) ? VLC_CODEC_RGB16 : VLC_CODEC_RGB32;
219 p_vout->output.i_rmask = 0xf800;
220 p_vout->output.i_gmask = 0x07e0;
221 p_vout->output.i_bmask = 0x001f;
223 /* All we have is an RGB image with square pixels */
224 p_vout->output.i_width = p_vout->p_sys->i_width;
225 p_vout->output.i_height = p_vout->p_sys->i_height;
226 if( !p_vout->b_fullscreen )
228 p_vout->output.i_aspect = p_vout->output.i_width
230 / p_vout->output.i_height;
234 p_vout->output.i_aspect = p_vout->render.i_aspect;
237 msg_Dbg( p_vout, "Init (h=%d,w=%d,aspect=%d)",p_vout->output.i_height,p_vout->output.i_width,p_vout->output.i_aspect );
239 /* Try to initialize MAX_DIRECTBUFFERS direct buffers */
240 while( I_OUTPUTPICTURES < QTE_MAX_DIRECTBUFFERS )
244 /* Find an empty picture slot */
245 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
247 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
249 p_pic = p_vout->p_picture + i_index;
254 /* Allocate the picture */
255 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
260 p_pic->i_status = DESTROYED_PICTURE;
261 p_pic->i_type = DIRECT_PICTURE;
263 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
272 /*****************************************************************************
273 * Render: render previously calculated output
274 *****************************************************************************/
275 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
280 /*****************************************************************************
281 * Display: displays previously rendered output
282 *****************************************************************************
283 * This function sends the currently rendered image to screen.
284 *****************************************************************************/
285 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
287 unsigned int x, y, w, h;
289 vout_PlacePicture( p_vout, p_vout->output.i_width, p_vout->output.i_height,
292 msg_Dbg(p_vout, "+qte::Display( p_vout, i_width=%d, i_height=%d, x=%u, y=%u, w=%u, h=%u",
293 p_vout->output.i_width, p_vout->output.i_height, x, y, w, h );
296 if(p_vout->p_sys->p_VideoWidget)
298 // shameless borrowed from opie mediaplayer....
299 #ifndef USE_DIRECT_PAINTER
300 msg_Dbg(p_vout, "not using direct painter");
301 QPainter p(p_vout->p_sys->p_VideoWidget);
304 int dd = QPixmap::defaultDepth();
305 int bytes = ( dd == 16 ) ? 2 : 4;
308 QImage rotatedFrame( rw, rh, bytes << 3 );
310 ushort* in = (ushort*)p_pic->p_sys->pQImage->bits();
311 ushort* out = (ushort*)rotatedFrame.bits();
313 int spl = rotatedFrame.bytesPerLine() / bytes;
314 for (int x=0; x<h; x++)
318 ushort* lout = out++ + (w - 1)*spl;
319 for (int y=0; y<w; y++)
327 ulong* lout = ((ulong *)out)++ + (w - 1)*spl;
328 for (int y=0; y<w; y++)
330 *lout=*((ulong*)in)++;
336 p.drawImage( x, y, rotatedFrame, 0, 0, rw, rh );
338 QDirectPainter p(p_vout->p_sys->p_VideoWidget);
339 p.transformOrientation();
340 // just copy the image to the frame buffer...
341 memcpy(p.frameBuffer(), (p_pic->p_sys->pQImage->jumpTable())[0], h * p.lineStep());
346 /*****************************************************************************
347 * Manage: handle Qte events
348 *****************************************************************************
349 * This function should be called regularly by video output thread. It manages
350 * Qte events and allows window resizing. It returns a non null value on
352 *****************************************************************************/
353 static int Manage( vout_thread_t *p_vout )
355 // msg_Dbg( p_vout, "Manage" );
357 /* Fullscreen change */
358 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
360 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
362 // p_vout->p_sys->b_cursor_autohidden = 0;
363 // SDL_ShowCursor( p_vout->p_sys->b_cursor &&
364 // ! p_vout->p_sys->b_cursor_autohidden );
366 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
367 p_vout->i_changes |= VOUT_SIZE_CHANGE;
373 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
375 msg_Dbg( p_vout, "video display resized (%dx%d)",
376 p_vout->p_sys->i_width, p_vout->p_sys->i_height );
378 CloseDisplay( p_vout );
379 OpenDisplay( p_vout );
381 /* We don't need to signal the vout thread about the size change if
382 * we can handle rescaling ourselves */
383 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
387 // if( ! p_vout->p_sys->b_cursor_autohidden &&
388 // ( mdate() - p_vout->p_sys->i_lastmoved >
389 // p_vout->p_sys->i_mouse_hide_timeout ) )
391 // /* Hide the mouse automatically */
392 // p_vout->p_sys->b_cursor_autohidden = 1;
393 // SDL_ShowCursor( 0 );
396 // if( !vlc_object_alive (p_vout->p_libvlc) )
397 // p_vout->p_sys->bRunning = FALSE;
402 /*****************************************************************************
403 * End: terminate video thread output method
404 *****************************************************************************
405 * Destroy the buffers created by vout_Init. It is called at the end of
406 * the thread, but also each time the window is resized.
407 *****************************************************************************/
408 static void End( vout_thread_t *p_vout )
412 /* Free the direct buffers we allocated */
413 for( i_index = I_OUTPUTPICTURES ; i_index ; )
416 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
421 /*****************************************************************************
422 * NewPicture: allocate a picture
423 *****************************************************************************
424 * Returns 0 on success, -1 otherwise
425 *****************************************************************************/
426 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
428 int dd = QPixmap::defaultDepth();
430 p_pic->p_sys = (picture_sys_t*) malloc( sizeof( picture_sys_t ) );
431 if( p_pic->p_sys == NULL )
436 /* Create the display */
437 p_pic->p_sys->pQImage = new QImage(p_vout->output.i_width,
438 p_vout->output.i_height, dd );
440 if(p_pic->p_sys->pQImage == NULL)
448 p_pic->p->i_pixel_pitch = 1;
452 p_pic->p->i_pixel_pitch = 2;
456 p_pic->p->i_pixel_pitch = 4;
462 p_pic->p->p_pixels = (p_pic->p_sys->pQImage->jumpTable())[0];
463 p_pic->p->i_pitch = p_pic->p_sys->pQImage->bytesPerLine();
464 p_pic->p->i_lines = p_vout->output.i_height;
465 p_pic->p->i_visible_lines = p_vout->output.i_height;
466 p_pic->p->i_visible_pitch =
467 p_pic->p->i_pixel_pitch * p_vout->output.i_width;
474 /*****************************************************************************
475 * FreePicture: destroy a picture allocated with NewPicture
476 *****************************************************************************/
477 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
479 delete p_pic->p_sys->pQImage;
482 /*****************************************************************************
483 * ToggleFullScreen: Enable or disable full screen mode
484 *****************************************************************************
485 * This function will switch between fullscreen and window mode.
487 *****************************************************************************/
488 static void ToggleFullScreen ( vout_thread_t *p_vout )
490 if ( p_vout->b_fullscreen )
491 p_vout->p_sys->p_VideoWidget->showFullScreen();
493 p_vout->p_sys->p_VideoWidget->showNormal();
495 p_vout->b_fullscreen = !p_vout->b_fullscreen;
498 /*****************************************************************************
499 * OpenDisplay: create qte applicaton / window
500 *****************************************************************************
501 * Create a window according to video output given size, and set other
502 * properties according to the display properties.
503 *****************************************************************************/
504 static int OpenDisplay( vout_thread_t *p_vout )
506 /* for displaying the vout in a qt window we need the QtApplication */
507 p_vout->p_sys->p_QApplication = NULL;
508 p_vout->p_sys->p_VideoWidget = NULL;
510 p_vout->p_sys->p_event = (event_thread_t*) vlc_object_create( p_vout, sizeof(event_thread_t) );
511 p_vout->p_sys->p_event->p_vout = p_vout;
513 /* Initializations */
514 #if 1 /* FIXME: I need an event queue to handle video output size changes. */
515 p_vout->b_fullscreen = true;
518 /* Set main window's size */
519 QWidget *desktop = p_vout->p_sys->p_QApplication->desktop();
520 p_vout->p_sys->i_width = p_vout->b_fullscreen ? desktop->height() :
521 p_vout->i_window_width;
522 p_vout->p_sys->i_height = p_vout->b_fullscreen ? desktop->width() :
523 p_vout->i_window_height;
525 #if 0 /* FIXME: I need an event queue to handle video output size changes. */
526 /* Update dimensions */
527 p_vout->i_changes |= VOUT_SIZE_CHANGE;
528 p_vout->i_window_width = p_vout->p_sys->i_width;
529 p_vout->i_window_height = p_vout->p_sys->i_height;
532 msg_Dbg( p_vout, "opening display (h=%d,w=%d)",p_vout->p_sys->i_height,p_vout->p_sys->i_width);
534 /* create thread to exec the qpe application */
535 if ( vlc_thread_create( p_vout->p_sys->p_event, "QT Embedded Thread",
537 VLC_THREAD_PRIORITY_OUTPUT, true) )
539 msg_Err( p_vout, "cannot create QT Embedded Thread" );
540 vlc_object_release( p_vout->p_sys->p_event );
541 p_vout->p_sys->p_event = NULL;
545 if( p_vout->p_sys->p_event->b_error )
547 msg_Err( p_vout, "RunQtThread failed" );
551 vlc_object_attach( p_vout->p_sys->p_event, p_vout );
552 msg_Dbg( p_vout, "RunQtThread running" );
554 // just wait until the crew is complete...
555 while(p_vout->p_sys->p_VideoWidget == NULL)
563 /*****************************************************************************
564 * CloseDisplay: destroy the window
565 *****************************************************************************/
566 static void CloseDisplay( vout_thread_t *p_vout )
568 // quit qt application loop
569 msg_Dbg( p_vout, "destroying Qt Window" );
571 if(p_vout->p_sys->p_QApplication)
573 p_vout->p_sys->bRunning = FALSE;
574 while(p_vout->p_sys->p_VideoWidget)
580 if (p_vout->p_sys->p_QApplication)
581 p_vout->p_sys->p_QApplication->quit();
585 /*****************************************************************************
586 * main loop of qtapplication
587 *****************************************************************************/
588 static void* RunQtThread( vlc_object_t *p_this )
590 event_thread_t *p_event = (event_thread_t *)p_this;
591 int canc = vlc_savecancel ();
592 msg_Dbg( p_event->p_vout, "RunQtThread starting" );
597 p_event->p_vout->p_sys->p_QApplication = qApp;
598 p_event->p_vout->p_sys->bOwnsQApp = FALSE;
599 p_event->p_vout->p_sys->p_VideoWidget = qApp->mainWidget();
600 msg_Dbg( p_event->p_vout, "RunQtThread applicaton attached" );
606 QApplication* pApp = new QApplication(argc, NULL);
609 p_event->p_vout->p_sys->p_QApplication = pApp;
610 p_event->p_vout->p_sys->bOwnsQApp = TRUE;
612 QWidget* pWidget = new QWidget();
615 p_event->p_vout->p_sys->p_VideoWidget = pWidget;
619 /* signal the creation of the window */
620 vlc_thread_ready( p_event );
621 msg_Dbg( p_event->p_vout, "RunQtThread ready" );
623 if (p_event->p_vout->p_sys->p_QApplication)
625 /* Set default window width and heigh to exactly preferred size. */
626 QWidget *desktop = p_event->p_vout->p_sys->p_QApplication->desktop();
627 p_event->p_vout->p_sys->p_VideoWidget->setMinimumWidth( 10 );
628 p_event->p_vout->p_sys->p_VideoWidget->setMinimumHeight( 10 );
629 p_event->p_vout->p_sys->p_VideoWidget->setBaseSize( p_event->p_vout->p_sys->i_width,
630 p_event->p_vout->p_sys->i_height );
631 p_event->p_vout->p_sys->p_VideoWidget->setMaximumWidth( desktop->width() );
632 p_event->p_vout->p_sys->p_VideoWidget->setMaximumHeight( desktop->height() );
633 /* Check on fullscreen */
634 if (p_event->p_vout->b_fullscreen)
635 p_event->p_vout->p_sys->p_VideoWidget->showFullScreen();
637 p_event->p_vout->p_sys->p_VideoWidget->showNormal();
639 p_event->p_vout->p_sys->p_VideoWidget->show();
640 p_event->p_vout->p_sys->bRunning = TRUE;
643 while(vlc_object_alive (p_event) && p_event->p_vout->p_sys->bRunning)
645 /* Check if we are asked to exit */
646 if( !vlc_object_alive (p_event) )
652 // run the main loop of qtapplication until someone says: 'quit'
653 p_event->p_vout->p_sys->pcQApplication->exec();
657 #ifndef NEED_QTE_MAIN
658 if(p_event->p_vout->p_sys->p_QApplication)
660 delete p_event->p_vout->p_sys->p_VideoWidget;
661 p_event->p_vout->p_sys->p_VideoWidget = NULL;
662 delete p_event->p_vout->p_sys->p_QApplication;
663 p_event->p_vout->p_sys->p_QApplication = NULL;
666 p_event->p_vout->p_sys->p_VideoWidget = NULL;
669 msg_Dbg( p_event->p_vout, "RunQtThread terminating" );
670 vlc_restorecancel (canc);