1 /*****************************************************************************
2 * qte.cpp : QT Embedded plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 1998-2003 the VideoLAN team
7 * Authors: Gerald Hansink <gerald.hansink@ordain.nl>
8 * Jean-Paul Saman <jpsaman@wxs.nl>
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 */
40 #include <stdlib.h> /* free() */
41 #include <string.h> /* strerror() */
47 #ifdef HAVE_MACHINE_PARAM_H
49 # include <machine/param.h>
50 # include <sys/types.h> /* typedef ushort */
55 # include <netinet/in.h> /* BSD: struct in_addr */
59 # include <sys/shm.h> /* shmget(), shmctl() */
63 #include <qapplication.h>
67 # define USE_DIRECT_PAINTER
68 # include <qdirectpainter_qws.h>
69 # include <qgfxraster_qws.h>
76 /*****************************************************************************
78 *****************************************************************************/
79 #define DISPLAY_TEXT N_("QT Embedded display name")
80 #define DISPLAY_LONGTEXT N_( \
81 "Specify the Qt Embedded hardware display you want to use. " \
82 "By default VLC will use the value of the DISPLAY environment variable.")
84 /*****************************************************************************
86 *****************************************************************************/
87 static int Open ( vlc_object_t * );
88 static void Close ( vlc_object_t * );
89 static void Render ( vout_thread_t *, picture_t * );
90 static void Display ( vout_thread_t *, picture_t * );
91 static int Manage ( vout_thread_t * );
92 static int Init ( vout_thread_t * );
93 static void End ( vout_thread_t * );
95 static int OpenDisplay ( vout_thread_t * );
96 static void CloseDisplay( vout_thread_t * );
98 static int NewPicture ( vout_thread_t *, picture_t * );
99 static void FreePicture ( vout_thread_t *, picture_t * );
101 static void ToggleFullScreen ( vout_thread_t * );
103 static void RunQtThread( event_thread_t *p_event );
106 /*****************************************************************************
107 * Exported prototypes
108 *****************************************************************************/
113 set_category( CAT_VIDEO );
114 set_subcategory( SUBCAT_VIDEO_VOUT );
115 // add_category_hint( N_("QT Embedded"), NULL );
116 // add_string( "qte-display", "landscape", NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT);
117 set_description( _("QT Embedded video output") );
118 set_capability( "video output", 70 );
119 add_shortcut( "qte" );
120 set_callbacks( Open, Close);
125 /*****************************************************************************
126 * Seeking function TODO: put this in a generic location !
127 *****************************************************************************/
128 static inline void vout_Seek( off_t i_seek )
132 /*****************************************************************************
133 * Open: allocate video thread output method
134 *****************************************************************************/
135 static int Open( vlc_object_t *p_this )
137 vout_thread_t * p_vout = (vout_thread_t *)p_this;
139 /* Allocate structure */
140 p_vout->p_sys = (struct vout_sys_t*) malloc( sizeof( struct vout_sys_t ) );
142 if( p_vout->p_sys == NULL )
144 msg_Err( p_vout, "out of memory" );
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", VLC_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 p_vout->p_sys->p_event->b_die = VLC_TRUE;
189 CloseDisplay(p_vout);
191 vlc_thread_join( p_vout->p_sys->p_event );
192 vlc_object_destroy( 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 );
202 free( p_vout->p_sys );
203 p_vout->p_sys = NULL;
207 /*****************************************************************************
208 * Init: initialize video thread output method
209 *****************************************************************************
210 * This function create the buffers needed by the output thread. It is called
211 * at the beginning of the thread, but also each time the window is resized.
212 *****************************************************************************/
213 static int Init( vout_thread_t *p_vout )
217 int dd = QPixmap::defaultDepth();
219 I_OUTPUTPICTURES = 0;
221 p_vout->output.i_chroma = (dd == 16) ? VLC_FOURCC('R','V','1','6'): VLC_FOURCC('R','V','3','2');
222 p_vout->output.i_rmask = 0xf800;
223 p_vout->output.i_gmask = 0x07e0;
224 p_vout->output.i_bmask = 0x001f;
226 /* All we have is an RGB image with square pixels */
227 p_vout->output.i_width = p_vout->p_sys->i_width;
228 p_vout->output.i_height = p_vout->p_sys->i_height;
229 if( !p_vout->b_fullscreen )
231 p_vout->output.i_aspect = p_vout->output.i_width
233 / p_vout->output.i_height;
237 p_vout->output.i_aspect = p_vout->render.i_aspect;
240 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 );
242 /* Try to initialize MAX_DIRECTBUFFERS direct buffers */
243 while( I_OUTPUTPICTURES < QTE_MAX_DIRECTBUFFERS )
247 /* Find an empty picture slot */
248 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
250 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
252 p_pic = p_vout->p_picture + i_index;
257 /* Allocate the picture */
258 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
263 p_pic->i_status = DESTROYED_PICTURE;
264 p_pic->i_type = DIRECT_PICTURE;
266 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
275 /*****************************************************************************
276 * Render: render previously calculated output
277 *****************************************************************************/
278 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
283 /*****************************************************************************
284 * Display: displays previously rendered output
285 *****************************************************************************
286 * This function sends the currently rendered image to screen.
287 *****************************************************************************/
288 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
290 unsigned int x, y, w, h;
292 vout_PlacePicture( p_vout, p_vout->output.i_width, p_vout->output.i_height,
295 msg_Dbg(p_vout, "+qte::Display( p_vout, i_width=%d, i_height=%d, x=%u, y=%u, w=%u, h=%u",
296 p_vout->output.i_width, p_vout->output.i_height, x, y, w, h );
299 if(p_vout->p_sys->p_VideoWidget)
301 // shameless borrowed from opie mediaplayer....
302 #ifndef USE_DIRECT_PAINTER
303 msg_Dbg(p_vout, "not using direct painter");
304 QPainter p(p_vout->p_sys->p_VideoWidget);
307 int dd = QPixmap::defaultDepth();
308 int bytes = ( dd == 16 ) ? 2 : 4;
311 QImage rotatedFrame( rw, rh, bytes << 3 );
313 ushort* in = (ushort*)p_pic->p_sys->pQImage->bits();
314 ushort* out = (ushort*)rotatedFrame.bits();
316 int spl = rotatedFrame.bytesPerLine() / bytes;
317 for (int x=0; x<h; x++)
321 ushort* lout = out++ + (w - 1)*spl;
322 for (int y=0; y<w; y++)
330 ulong* lout = ((ulong *)out)++ + (w - 1)*spl;
331 for (int y=0; y<w; y++)
333 *lout=*((ulong*)in)++;
339 p.drawImage( x, y, rotatedFrame, 0, 0, rw, rh );
341 QDirectPainter p(p_vout->p_sys->p_VideoWidget);
342 p.transformOrientation();
343 // just copy the image to the frame buffer...
344 memcpy(p.frameBuffer(), (p_pic->p_sys->pQImage->jumpTable())[0], h * p.lineStep());
349 /*****************************************************************************
350 * Manage: handle Qte events
351 *****************************************************************************
352 * This function should be called regularly by video output thread. It manages
353 * Qte events and allows window resizing. It returns a non null value on
355 *****************************************************************************/
356 static int Manage( vout_thread_t *p_vout )
358 // msg_Dbg( p_vout, "Manage" );
360 /* Fullscreen change */
361 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
363 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
365 // p_vout->p_sys->b_cursor_autohidden = 0;
366 // SDL_ShowCursor( p_vout->p_sys->b_cursor &&
367 // ! p_vout->p_sys->b_cursor_autohidden );
369 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
370 p_vout->i_changes |= VOUT_SIZE_CHANGE;
376 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
378 msg_Dbg( p_vout, "video display resized (%dx%d)",
379 p_vout->p_sys->i_width, p_vout->p_sys->i_height );
381 CloseDisplay( p_vout );
382 OpenDisplay( p_vout );
384 /* We don't need to signal the vout thread about the size change if
385 * we can handle rescaling ourselves */
386 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
390 // if( ! p_vout->p_sys->b_cursor_autohidden &&
391 // ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
393 // /* Hide the mouse automatically */
394 // p_vout->p_sys->b_cursor_autohidden = 1;
395 // SDL_ShowCursor( 0 );
398 // if( p_vout->p_vlc->b_die )
399 // p_vout->p_sys->bRunning = FALSE;
404 /*****************************************************************************
405 * End: terminate video thread output method
406 *****************************************************************************
407 * Destroy the buffers created by vout_Init. It is called at the end of
408 * the thread, but also each time the window is resized.
409 *****************************************************************************/
410 static void End( vout_thread_t *p_vout )
414 /* Free the direct buffers we allocated */
415 for( i_index = I_OUTPUTPICTURES ; i_index ; )
418 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
423 /*****************************************************************************
424 * NewPicture: allocate a picture
425 *****************************************************************************
426 * Returns 0 on success, -1 otherwise
427 *****************************************************************************/
428 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
430 int dd = QPixmap::defaultDepth();
432 p_pic->p_sys = (picture_sys_t*) malloc( sizeof( picture_sys_t ) );
433 if( p_pic->p_sys == NULL )
438 /* Create the display */
439 p_pic->p_sys->pQImage = new QImage(p_vout->output.i_width,
440 p_vout->output.i_height, dd );
442 if(p_pic->p_sys->pQImage == NULL)
450 p_pic->p->i_pixel_pitch = 1;
454 p_pic->p->i_pixel_pitch = 2;
458 p_pic->p->i_pixel_pitch = 4;
464 p_pic->p->p_pixels = (p_pic->p_sys->pQImage->jumpTable())[0];
465 p_pic->p->i_pitch = p_pic->p_sys->pQImage->bytesPerLine();
466 p_pic->p->i_lines = p_vout->output.i_height;
467 p_pic->p->i_visible_lines = p_vout->output.i_height;
468 p_pic->p->i_visible_pitch =
469 p_pic->p->i_pixel_pitch * p_vout->output.i_width;
476 /*****************************************************************************
477 * FreePicture: destroy a picture allocated with NewPicture
478 *****************************************************************************/
479 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
481 delete p_pic->p_sys->pQImage;
484 /*****************************************************************************
485 * ToggleFullScreen: Enable or disable full screen mode
486 *****************************************************************************
487 * This function will switch between fullscreen and window mode.
489 *****************************************************************************/
490 static void ToggleFullScreen ( vout_thread_t *p_vout )
492 if ( p_vout->b_fullscreen )
493 p_vout->p_sys->p_VideoWidget->showFullScreen();
495 p_vout->p_sys->p_VideoWidget->showNormal();
497 p_vout->b_fullscreen = !p_vout->b_fullscreen;
500 /*****************************************************************************
501 * OpenDisplay: create qte applicaton / window
502 *****************************************************************************
503 * Create a window according to video output given size, and set other
504 * properties according to the display properties.
505 *****************************************************************************/
506 static int OpenDisplay( vout_thread_t *p_vout )
508 /* for displaying the vout in a qt window we need the QtApplication */
509 p_vout->p_sys->p_QApplication = NULL;
510 p_vout->p_sys->p_VideoWidget = NULL;
512 p_vout->p_sys->p_event = (event_thread_t*) vlc_object_create( p_vout, sizeof(event_thread_t) );
513 p_vout->p_sys->p_event->p_vout = p_vout;
515 /* Initializations */
516 #if 1 /* FIXME: I need an event queue to handle video output size changes. */
517 p_vout->b_fullscreen = VLC_TRUE;
520 /* Set main window's size */
521 QWidget *desktop = p_vout->p_sys->p_QApplication->desktop();
522 p_vout->p_sys->i_width = p_vout->b_fullscreen ? desktop->height() :
523 p_vout->i_window_width;
524 p_vout->p_sys->i_height = p_vout->b_fullscreen ? desktop->width() :
525 p_vout->i_window_height;
527 #if 0 /* FIXME: I need an event queue to handle video output size changes. */
528 /* Update dimensions */
529 p_vout->i_changes |= VOUT_SIZE_CHANGE;
530 p_vout->i_window_width = p_vout->p_sys->i_width;
531 p_vout->i_window_height = p_vout->p_sys->i_height;
534 msg_Dbg( p_vout, "OpenDisplay (h=%d,w=%d)",p_vout->p_sys->i_height,p_vout->p_sys->i_width);
536 /* create thread to exec the qpe application */
537 if ( vlc_thread_create( p_vout->p_sys->p_event, "QT Embedded Thread",
539 VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE) )
541 msg_Err( p_vout, "cannot create QT Embedded Thread" );
542 vlc_object_destroy( p_vout->p_sys->p_event );
543 p_vout->p_sys->p_event = NULL;
547 if( p_vout->p_sys->p_event->b_error )
549 msg_Err( p_vout, "RunQtThread failed" );
553 vlc_object_attach( p_vout->p_sys->p_event, p_vout );
554 msg_Dbg( p_vout, "RunQtThread running" );
556 // just wait until the crew is complete...
557 while(p_vout->p_sys->p_VideoWidget == NULL)
565 /*****************************************************************************
566 * CloseDisplay: destroy the window
567 *****************************************************************************/
568 static void CloseDisplay( vout_thread_t *p_vout )
570 // quit qt application loop
571 msg_Dbg( p_vout, "destroying Qt Window" );
573 if(p_vout->p_sys->p_QApplication)
575 p_vout->p_sys->bRunning = FALSE;
576 while(p_vout->p_sys->p_VideoWidget)
582 if (p_vout->p_sys->p_QApplication)
583 p_vout->p_sys->p_QApplication->quit();
587 /*****************************************************************************
588 * main loop of qtapplication
589 *****************************************************************************/
590 static void RunQtThread(event_thread_t *p_event)
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(!p_event->b_die && p_event->p_vout->p_sys->bRunning)
645 /* Check if we are asked to exit */
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" );