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 */
46 #include <vlc_interface.h>
49 #ifdef HAVE_MACHINE_PARAM_H
51 # include <machine/param.h>
52 # include <sys/types.h> /* typedef ushort */
57 # include <netinet/in.h> /* BSD: struct in_addr */
61 # include <sys/shm.h> /* shmget(), shmctl() */
65 #include <qapplication.h>
69 # define USE_DIRECT_PAINTER
70 # include <qdirectpainter_qws.h>
71 # include <qgfxraster_qws.h>
78 /*****************************************************************************
80 *****************************************************************************/
81 #define DISPLAY_TEXT N_("QT Embedded display")
82 #define DISPLAY_LONGTEXT N_( \
83 "Qt Embedded hardware display to use. " \
84 "By default VLC will use the value of the DISPLAY environment variable.")
86 /*****************************************************************************
88 *****************************************************************************/
89 static int Open ( vlc_object_t * );
90 static void Close ( vlc_object_t * );
91 static void Render ( vout_thread_t *, picture_t * );
92 static void Display ( vout_thread_t *, picture_t * );
93 static int Manage ( vout_thread_t * );
94 static int Init ( vout_thread_t * );
95 static void End ( vout_thread_t * );
97 static int OpenDisplay ( vout_thread_t * );
98 static void CloseDisplay( vout_thread_t * );
100 static int NewPicture ( vout_thread_t *, picture_t * );
101 static void FreePicture ( vout_thread_t *, picture_t * );
103 static void ToggleFullScreen ( vout_thread_t * );
105 static void RunQtThread( event_thread_t *p_event );
108 /*****************************************************************************
109 * Exported prototypes
110 *****************************************************************************/
115 set_category( CAT_VIDEO );
116 set_subcategory( SUBCAT_VIDEO_VOUT );
117 // add_category_hint( N_("QT Embedded"), NULL );
118 // add_string( "qte-display", "landscape", NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT);
119 set_description( _("QT Embedded video output") );
120 set_capability( "video output", 70 );
121 add_shortcut( "qte" );
122 set_callbacks( Open, Close);
127 /*****************************************************************************
128 * Seeking function TODO: put this in a generic location !
129 *****************************************************************************/
130 static inline void vout_Seek( off_t i_seek )
134 /*****************************************************************************
135 * Open: allocate video thread output method
136 *****************************************************************************/
137 static int Open( vlc_object_t *p_this )
139 vout_thread_t * p_vout = (vout_thread_t *)p_this;
141 /* Allocate structure */
142 p_vout->p_sys = (struct vout_sys_t*) malloc( sizeof( struct vout_sys_t ) );
144 if( p_vout->p_sys == NULL )
146 msg_Err( p_vout, "out of memory" );
150 p_vout->pf_init = Init;
151 p_vout->pf_end = End;
152 p_vout->pf_manage = Manage;
153 p_vout->pf_render = NULL; //Render;
154 p_vout->pf_display = Display;
157 p_vout->p_sys->p_qte_main =
158 module_Need( p_this, "gui-helper", "qte", VLC_TRUE );
159 if( p_vout->p_sys->p_qte_main == NULL )
161 free( p_vout->p_sys );
166 if (OpenDisplay(p_vout))
168 msg_Err( p_vout, "Cannot set up qte video output" );
175 /*****************************************************************************
176 * CloseVideo: destroy Sys video thread output method
177 *****************************************************************************
178 * Terminate an output method created by Open
179 *****************************************************************************/
180 static void Close ( vlc_object_t *p_this )
182 vout_thread_t * p_vout = (vout_thread_t *)p_this;
184 msg_Dbg( p_vout, "close" );
185 if( p_vout->p_sys->p_event )
187 vlc_object_detach( p_vout->p_sys->p_event );
189 /* Kill RunQtThread */
190 vlc_object_kill( p_vout->p_sys->p_event );
191 CloseDisplay(p_vout);
193 vlc_thread_join( p_vout->p_sys->p_event );
194 vlc_object_destroy( p_vout->p_sys->p_event );
198 msg_Dbg( p_vout, "releasing gui-helper" );
199 module_Unneed( p_vout, p_vout->p_sys->p_qte_main );
204 free( p_vout->p_sys );
205 p_vout->p_sys = NULL;
209 /*****************************************************************************
210 * Init: initialize video thread output method
211 *****************************************************************************
212 * This function create the buffers needed by the output thread. It is called
213 * at the beginning of the thread, but also each time the window is resized.
214 *****************************************************************************/
215 static int Init( vout_thread_t *p_vout )
219 int dd = QPixmap::defaultDepth();
221 I_OUTPUTPICTURES = 0;
223 p_vout->output.i_chroma = (dd == 16) ? VLC_FOURCC('R','V','1','6'): VLC_FOURCC('R','V','3','2');
224 p_vout->output.i_rmask = 0xf800;
225 p_vout->output.i_gmask = 0x07e0;
226 p_vout->output.i_bmask = 0x001f;
228 /* All we have is an RGB image with square pixels */
229 p_vout->output.i_width = p_vout->p_sys->i_width;
230 p_vout->output.i_height = p_vout->p_sys->i_height;
231 if( !p_vout->b_fullscreen )
233 p_vout->output.i_aspect = p_vout->output.i_width
235 / p_vout->output.i_height;
239 p_vout->output.i_aspect = p_vout->render.i_aspect;
242 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 );
244 /* Try to initialize MAX_DIRECTBUFFERS direct buffers */
245 while( I_OUTPUTPICTURES < QTE_MAX_DIRECTBUFFERS )
249 /* Find an empty picture slot */
250 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
252 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
254 p_pic = p_vout->p_picture + i_index;
259 /* Allocate the picture */
260 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
265 p_pic->i_status = DESTROYED_PICTURE;
266 p_pic->i_type = DIRECT_PICTURE;
268 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
277 /*****************************************************************************
278 * Render: render previously calculated output
279 *****************************************************************************/
280 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
285 /*****************************************************************************
286 * Display: displays previously rendered output
287 *****************************************************************************
288 * This function sends the currently rendered image to screen.
289 *****************************************************************************/
290 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
292 unsigned int x, y, w, h;
294 vout_PlacePicture( p_vout, p_vout->output.i_width, p_vout->output.i_height,
297 msg_Dbg(p_vout, "+qte::Display( p_vout, i_width=%d, i_height=%d, x=%u, y=%u, w=%u, h=%u",
298 p_vout->output.i_width, p_vout->output.i_height, x, y, w, h );
301 if(p_vout->p_sys->p_VideoWidget)
303 // shameless borrowed from opie mediaplayer....
304 #ifndef USE_DIRECT_PAINTER
305 msg_Dbg(p_vout, "not using direct painter");
306 QPainter p(p_vout->p_sys->p_VideoWidget);
309 int dd = QPixmap::defaultDepth();
310 int bytes = ( dd == 16 ) ? 2 : 4;
313 QImage rotatedFrame( rw, rh, bytes << 3 );
315 ushort* in = (ushort*)p_pic->p_sys->pQImage->bits();
316 ushort* out = (ushort*)rotatedFrame.bits();
318 int spl = rotatedFrame.bytesPerLine() / bytes;
319 for (int x=0; x<h; x++)
323 ushort* lout = out++ + (w - 1)*spl;
324 for (int y=0; y<w; y++)
332 ulong* lout = ((ulong *)out)++ + (w - 1)*spl;
333 for (int y=0; y<w; y++)
335 *lout=*((ulong*)in)++;
341 p.drawImage( x, y, rotatedFrame, 0, 0, rw, rh );
343 QDirectPainter p(p_vout->p_sys->p_VideoWidget);
344 p.transformOrientation();
345 // just copy the image to the frame buffer...
346 memcpy(p.frameBuffer(), (p_pic->p_sys->pQImage->jumpTable())[0], h * p.lineStep());
351 /*****************************************************************************
352 * Manage: handle Qte events
353 *****************************************************************************
354 * This function should be called regularly by video output thread. It manages
355 * Qte events and allows window resizing. It returns a non null value on
357 *****************************************************************************/
358 static int Manage( vout_thread_t *p_vout )
360 // msg_Dbg( p_vout, "Manage" );
362 /* Fullscreen change */
363 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
365 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
367 // p_vout->p_sys->b_cursor_autohidden = 0;
368 // SDL_ShowCursor( p_vout->p_sys->b_cursor &&
369 // ! p_vout->p_sys->b_cursor_autohidden );
371 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
372 p_vout->i_changes |= VOUT_SIZE_CHANGE;
378 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
380 msg_Dbg( p_vout, "video display resized (%dx%d)",
381 p_vout->p_sys->i_width, p_vout->p_sys->i_height );
383 CloseDisplay( p_vout );
384 OpenDisplay( p_vout );
386 /* We don't need to signal the vout thread about the size change if
387 * we can handle rescaling ourselves */
388 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
392 // if( ! p_vout->p_sys->b_cursor_autohidden &&
393 // ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
395 // /* Hide the mouse automatically */
396 // p_vout->p_sys->b_cursor_autohidden = 1;
397 // SDL_ShowCursor( 0 );
400 // if( p_vout->p_libvlc->b_die )
401 // p_vout->p_sys->bRunning = FALSE;
406 /*****************************************************************************
407 * End: terminate video thread output method
408 *****************************************************************************
409 * Destroy the buffers created by vout_Init. It is called at the end of
410 * the thread, but also each time the window is resized.
411 *****************************************************************************/
412 static void End( vout_thread_t *p_vout )
416 /* Free the direct buffers we allocated */
417 for( i_index = I_OUTPUTPICTURES ; i_index ; )
420 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
425 /*****************************************************************************
426 * NewPicture: allocate a picture
427 *****************************************************************************
428 * Returns 0 on success, -1 otherwise
429 *****************************************************************************/
430 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
432 int dd = QPixmap::defaultDepth();
434 p_pic->p_sys = (picture_sys_t*) malloc( sizeof( picture_sys_t ) );
435 if( p_pic->p_sys == NULL )
440 /* Create the display */
441 p_pic->p_sys->pQImage = new QImage(p_vout->output.i_width,
442 p_vout->output.i_height, dd );
444 if(p_pic->p_sys->pQImage == NULL)
452 p_pic->p->i_pixel_pitch = 1;
456 p_pic->p->i_pixel_pitch = 2;
460 p_pic->p->i_pixel_pitch = 4;
466 p_pic->p->p_pixels = (p_pic->p_sys->pQImage->jumpTable())[0];
467 p_pic->p->i_pitch = p_pic->p_sys->pQImage->bytesPerLine();
468 p_pic->p->i_lines = p_vout->output.i_height;
469 p_pic->p->i_visible_lines = p_vout->output.i_height;
470 p_pic->p->i_visible_pitch =
471 p_pic->p->i_pixel_pitch * p_vout->output.i_width;
478 /*****************************************************************************
479 * FreePicture: destroy a picture allocated with NewPicture
480 *****************************************************************************/
481 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
483 delete p_pic->p_sys->pQImage;
486 /*****************************************************************************
487 * ToggleFullScreen: Enable or disable full screen mode
488 *****************************************************************************
489 * This function will switch between fullscreen and window mode.
491 *****************************************************************************/
492 static void ToggleFullScreen ( vout_thread_t *p_vout )
494 if ( p_vout->b_fullscreen )
495 p_vout->p_sys->p_VideoWidget->showFullScreen();
497 p_vout->p_sys->p_VideoWidget->showNormal();
499 p_vout->b_fullscreen = !p_vout->b_fullscreen;
502 /*****************************************************************************
503 * OpenDisplay: create qte applicaton / window
504 *****************************************************************************
505 * Create a window according to video output given size, and set other
506 * properties according to the display properties.
507 *****************************************************************************/
508 static int OpenDisplay( vout_thread_t *p_vout )
510 /* for displaying the vout in a qt window we need the QtApplication */
511 p_vout->p_sys->p_QApplication = NULL;
512 p_vout->p_sys->p_VideoWidget = NULL;
514 p_vout->p_sys->p_event = (event_thread_t*) vlc_object_create( p_vout, sizeof(event_thread_t) );
515 p_vout->p_sys->p_event->p_vout = p_vout;
517 /* Initializations */
518 #if 1 /* FIXME: I need an event queue to handle video output size changes. */
519 p_vout->b_fullscreen = VLC_TRUE;
522 /* Set main window's size */
523 QWidget *desktop = p_vout->p_sys->p_QApplication->desktop();
524 p_vout->p_sys->i_width = p_vout->b_fullscreen ? desktop->height() :
525 p_vout->i_window_width;
526 p_vout->p_sys->i_height = p_vout->b_fullscreen ? desktop->width() :
527 p_vout->i_window_height;
529 #if 0 /* FIXME: I need an event queue to handle video output size changes. */
530 /* Update dimensions */
531 p_vout->i_changes |= VOUT_SIZE_CHANGE;
532 p_vout->i_window_width = p_vout->p_sys->i_width;
533 p_vout->i_window_height = p_vout->p_sys->i_height;
536 msg_Dbg( p_vout, "opening display (h=%d,w=%d)",p_vout->p_sys->i_height,p_vout->p_sys->i_width);
538 /* create thread to exec the qpe application */
539 if ( vlc_thread_create( p_vout->p_sys->p_event, "QT Embedded Thread",
541 VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE) )
543 msg_Err( p_vout, "cannot create QT Embedded Thread" );
544 vlc_object_destroy( p_vout->p_sys->p_event );
545 p_vout->p_sys->p_event = NULL;
549 if( p_vout->p_sys->p_event->b_error )
551 msg_Err( p_vout, "RunQtThread failed" );
555 vlc_object_attach( p_vout->p_sys->p_event, p_vout );
556 msg_Dbg( p_vout, "RunQtThread running" );
558 // just wait until the crew is complete...
559 while(p_vout->p_sys->p_VideoWidget == NULL)
567 /*****************************************************************************
568 * CloseDisplay: destroy the window
569 *****************************************************************************/
570 static void CloseDisplay( vout_thread_t *p_vout )
572 // quit qt application loop
573 msg_Dbg( p_vout, "destroying Qt Window" );
575 if(p_vout->p_sys->p_QApplication)
577 p_vout->p_sys->bRunning = FALSE;
578 while(p_vout->p_sys->p_VideoWidget)
584 if (p_vout->p_sys->p_QApplication)
585 p_vout->p_sys->p_QApplication->quit();
589 /*****************************************************************************
590 * main loop of qtapplication
591 *****************************************************************************/
592 static void RunQtThread(event_thread_t *p_event)
594 msg_Dbg( p_event->p_vout, "RunQtThread starting" );
599 p_event->p_vout->p_sys->p_QApplication = qApp;
600 p_event->p_vout->p_sys->bOwnsQApp = FALSE;
601 p_event->p_vout->p_sys->p_VideoWidget = qApp->mainWidget();
602 msg_Dbg( p_event->p_vout, "RunQtThread applicaton attached" );
608 QApplication* pApp = new QApplication(argc, NULL);
611 p_event->p_vout->p_sys->p_QApplication = pApp;
612 p_event->p_vout->p_sys->bOwnsQApp = TRUE;
614 QWidget* pWidget = new QWidget();
617 p_event->p_vout->p_sys->p_VideoWidget = pWidget;
621 /* signal the creation of the window */
622 vlc_thread_ready( p_event );
623 msg_Dbg( p_event->p_vout, "RunQtThread ready" );
625 if (p_event->p_vout->p_sys->p_QApplication)
627 /* Set default window width and heigh to exactly preferred size. */
628 QWidget *desktop = p_event->p_vout->p_sys->p_QApplication->desktop();
629 p_event->p_vout->p_sys->p_VideoWidget->setMinimumWidth( 10 );
630 p_event->p_vout->p_sys->p_VideoWidget->setMinimumHeight( 10 );
631 p_event->p_vout->p_sys->p_VideoWidget->setBaseSize( p_event->p_vout->p_sys->i_width,
632 p_event->p_vout->p_sys->i_height );
633 p_event->p_vout->p_sys->p_VideoWidget->setMaximumWidth( desktop->width() );
634 p_event->p_vout->p_sys->p_VideoWidget->setMaximumHeight( desktop->height() );
635 /* Check on fullscreen */
636 if (p_event->p_vout->b_fullscreen)
637 p_event->p_vout->p_sys->p_VideoWidget->showFullScreen();
639 p_event->p_vout->p_sys->p_VideoWidget->showNormal();
641 p_event->p_vout->p_sys->p_VideoWidget->show();
642 p_event->p_vout->p_sys->bRunning = TRUE;
645 while(!p_event->b_die && p_event->p_vout->p_sys->bRunning)
647 /* Check if we are asked to exit */
654 // run the main loop of qtapplication until someone says: 'quit'
655 p_event->p_vout->p_sys->pcQApplication->exec();
659 #ifndef NEED_QTE_MAIN
660 if(p_event->p_vout->p_sys->p_QApplication)
662 delete p_event->p_vout->p_sys->p_VideoWidget;
663 p_event->p_vout->p_sys->p_VideoWidget = NULL;
664 delete p_event->p_vout->p_sys->p_QApplication;
665 p_event->p_vout->p_sys->p_QApplication = NULL;
668 p_event->p_vout->p_sys->p_VideoWidget = NULL;
671 msg_Dbg( p_event->p_vout, "RunQtThread terminating" );