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 */
42 #include <vlc_interface.h>
45 #ifdef HAVE_MACHINE_PARAM_H
47 # include <machine/param.h>
48 # include <sys/types.h> /* typedef ushort */
53 # include <netinet/in.h> /* BSD: struct in_addr */
57 # include <sys/shm.h> /* shmget(), shmctl() */
61 #include <qapplication.h>
65 # define USE_DIRECT_PAINTER
66 # include <qdirectpainter_qws.h>
67 # include <qgfxraster_qws.h>
74 /*****************************************************************************
76 *****************************************************************************/
77 #define DISPLAY_TEXT N_("QT Embedded display")
78 #define DISPLAY_LONGTEXT N_( \
79 "Qt Embedded hardware display to use. " \
80 "By default VLC will use the value of the DISPLAY environment variable.")
82 /*****************************************************************************
84 *****************************************************************************/
85 static int Open ( vlc_object_t * );
86 static void Close ( vlc_object_t * );
87 static void Render ( vout_thread_t *, picture_t * );
88 static void Display ( vout_thread_t *, picture_t * );
89 static int Manage ( vout_thread_t * );
90 static int Init ( vout_thread_t * );
91 static void End ( vout_thread_t * );
93 static int OpenDisplay ( vout_thread_t * );
94 static void CloseDisplay( vout_thread_t * );
96 static int NewPicture ( vout_thread_t *, picture_t * );
97 static void FreePicture ( vout_thread_t *, picture_t * );
99 static void ToggleFullScreen ( vout_thread_t * );
101 static void RunQtThread( event_thread_t *p_event );
104 /*****************************************************************************
105 * Exported prototypes
106 *****************************************************************************/
111 set_category( CAT_VIDEO );
112 set_subcategory( SUBCAT_VIDEO_VOUT );
113 // add_category_hint( N_("QT Embedded"), NULL );
114 // add_string( "qte-display", "landscape", NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT);
115 set_description( _("QT Embedded video output") );
116 set_capability( "video output", 70 );
117 add_shortcut( "qte" );
118 set_callbacks( Open, Close);
123 /*****************************************************************************
124 * Seeking function TODO: put this in a generic location !
125 *****************************************************************************/
126 static inline void vout_Seek( off_t i_seek )
130 /*****************************************************************************
131 * Open: allocate video thread output method
132 *****************************************************************************/
133 static int Open( vlc_object_t *p_this )
135 vout_thread_t * p_vout = (vout_thread_t *)p_this;
137 /* Allocate structure */
138 p_vout->p_sys = (struct vout_sys_t*) malloc( sizeof( struct vout_sys_t ) );
140 if( p_vout->p_sys == NULL )
142 msg_Err( p_vout, "out of memory" );
146 p_vout->pf_init = Init;
147 p_vout->pf_end = End;
148 p_vout->pf_manage = Manage;
149 p_vout->pf_render = NULL; //Render;
150 p_vout->pf_display = Display;
153 p_vout->p_sys->p_qte_main =
154 module_Need( p_this, "gui-helper", "qte", VLC_TRUE );
155 if( p_vout->p_sys->p_qte_main == NULL )
157 free( p_vout->p_sys );
162 if (OpenDisplay(p_vout))
164 msg_Err( p_vout, "Cannot set up qte video output" );
171 /*****************************************************************************
172 * CloseVideo: destroy Sys video thread output method
173 *****************************************************************************
174 * Terminate an output method created by Open
175 *****************************************************************************/
176 static void Close ( vlc_object_t *p_this )
178 vout_thread_t * p_vout = (vout_thread_t *)p_this;
180 msg_Dbg( p_vout, "close" );
181 if( p_vout->p_sys->p_event )
183 vlc_object_detach( p_vout->p_sys->p_event );
185 /* Kill RunQtThread */
186 vlc_object_kill( p_vout->p_sys->p_event );
187 CloseDisplay(p_vout);
189 vlc_thread_join( p_vout->p_sys->p_event );
190 vlc_object_destroy( p_vout->p_sys->p_event );
194 msg_Dbg( p_vout, "releasing gui-helper" );
195 module_Unneed( p_vout, p_vout->p_sys->p_qte_main );
200 free( p_vout->p_sys );
201 p_vout->p_sys = NULL;
205 /*****************************************************************************
206 * Init: initialize video thread output method
207 *****************************************************************************
208 * This function create the buffers needed by the output thread. It is called
209 * at the beginning of the thread, but also each time the window is resized.
210 *****************************************************************************/
211 static int Init( vout_thread_t *p_vout )
215 int dd = QPixmap::defaultDepth();
217 I_OUTPUTPICTURES = 0;
219 p_vout->output.i_chroma = (dd == 16) ? VLC_FOURCC('R','V','1','6'): VLC_FOURCC('R','V','3','2');
220 p_vout->output.i_rmask = 0xf800;
221 p_vout->output.i_gmask = 0x07e0;
222 p_vout->output.i_bmask = 0x001f;
224 /* All we have is an RGB image with square pixels */
225 p_vout->output.i_width = p_vout->p_sys->i_width;
226 p_vout->output.i_height = p_vout->p_sys->i_height;
227 if( !p_vout->b_fullscreen )
229 p_vout->output.i_aspect = p_vout->output.i_width
231 / p_vout->output.i_height;
235 p_vout->output.i_aspect = p_vout->render.i_aspect;
238 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 );
240 /* Try to initialize MAX_DIRECTBUFFERS direct buffers */
241 while( I_OUTPUTPICTURES < QTE_MAX_DIRECTBUFFERS )
245 /* Find an empty picture slot */
246 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
248 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
250 p_pic = p_vout->p_picture + i_index;
255 /* Allocate the picture */
256 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
261 p_pic->i_status = DESTROYED_PICTURE;
262 p_pic->i_type = DIRECT_PICTURE;
264 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
273 /*****************************************************************************
274 * Render: render previously calculated output
275 *****************************************************************************/
276 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
281 /*****************************************************************************
282 * Display: displays previously rendered output
283 *****************************************************************************
284 * This function sends the currently rendered image to screen.
285 *****************************************************************************/
286 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
288 unsigned int x, y, w, h;
290 vout_PlacePicture( p_vout, p_vout->output.i_width, p_vout->output.i_height,
293 msg_Dbg(p_vout, "+qte::Display( p_vout, i_width=%d, i_height=%d, x=%u, y=%u, w=%u, h=%u",
294 p_vout->output.i_width, p_vout->output.i_height, x, y, w, h );
297 if(p_vout->p_sys->p_VideoWidget)
299 // shameless borrowed from opie mediaplayer....
300 #ifndef USE_DIRECT_PAINTER
301 msg_Dbg(p_vout, "not using direct painter");
302 QPainter p(p_vout->p_sys->p_VideoWidget);
305 int dd = QPixmap::defaultDepth();
306 int bytes = ( dd == 16 ) ? 2 : 4;
309 QImage rotatedFrame( rw, rh, bytes << 3 );
311 ushort* in = (ushort*)p_pic->p_sys->pQImage->bits();
312 ushort* out = (ushort*)rotatedFrame.bits();
314 int spl = rotatedFrame.bytesPerLine() / bytes;
315 for (int x=0; x<h; x++)
319 ushort* lout = out++ + (w - 1)*spl;
320 for (int y=0; y<w; y++)
328 ulong* lout = ((ulong *)out)++ + (w - 1)*spl;
329 for (int y=0; y<w; y++)
331 *lout=*((ulong*)in)++;
337 p.drawImage( x, y, rotatedFrame, 0, 0, rw, rh );
339 QDirectPainter p(p_vout->p_sys->p_VideoWidget);
340 p.transformOrientation();
341 // just copy the image to the frame buffer...
342 memcpy(p.frameBuffer(), (p_pic->p_sys->pQImage->jumpTable())[0], h * p.lineStep());
347 /*****************************************************************************
348 * Manage: handle Qte events
349 *****************************************************************************
350 * This function should be called regularly by video output thread. It manages
351 * Qte events and allows window resizing. It returns a non null value on
353 *****************************************************************************/
354 static int Manage( vout_thread_t *p_vout )
356 // msg_Dbg( p_vout, "Manage" );
358 /* Fullscreen change */
359 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
361 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
363 // p_vout->p_sys->b_cursor_autohidden = 0;
364 // SDL_ShowCursor( p_vout->p_sys->b_cursor &&
365 // ! p_vout->p_sys->b_cursor_autohidden );
367 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
368 p_vout->i_changes |= VOUT_SIZE_CHANGE;
374 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
376 msg_Dbg( p_vout, "video display resized (%dx%d)",
377 p_vout->p_sys->i_width, p_vout->p_sys->i_height );
379 CloseDisplay( p_vout );
380 OpenDisplay( p_vout );
382 /* We don't need to signal the vout thread about the size change if
383 * we can handle rescaling ourselves */
384 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
388 // if( ! p_vout->p_sys->b_cursor_autohidden &&
389 // ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
391 // /* Hide the mouse automatically */
392 // p_vout->p_sys->b_cursor_autohidden = 1;
393 // SDL_ShowCursor( 0 );
396 // if( p_vout->p_libvlc->b_die )
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 = VLC_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, VLC_TRUE) )
539 msg_Err( p_vout, "cannot create QT Embedded Thread" );
540 vlc_object_destroy( 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(event_thread_t *p_event)
590 msg_Dbg( p_event->p_vout, "RunQtThread starting" );
595 p_event->p_vout->p_sys->p_QApplication = qApp;
596 p_event->p_vout->p_sys->bOwnsQApp = FALSE;
597 p_event->p_vout->p_sys->p_VideoWidget = qApp->mainWidget();
598 msg_Dbg( p_event->p_vout, "RunQtThread applicaton attached" );
604 QApplication* pApp = new QApplication(argc, NULL);
607 p_event->p_vout->p_sys->p_QApplication = pApp;
608 p_event->p_vout->p_sys->bOwnsQApp = TRUE;
610 QWidget* pWidget = new QWidget();
613 p_event->p_vout->p_sys->p_VideoWidget = pWidget;
617 /* signal the creation of the window */
618 vlc_thread_ready( p_event );
619 msg_Dbg( p_event->p_vout, "RunQtThread ready" );
621 if (p_event->p_vout->p_sys->p_QApplication)
623 /* Set default window width and heigh to exactly preferred size. */
624 QWidget *desktop = p_event->p_vout->p_sys->p_QApplication->desktop();
625 p_event->p_vout->p_sys->p_VideoWidget->setMinimumWidth( 10 );
626 p_event->p_vout->p_sys->p_VideoWidget->setMinimumHeight( 10 );
627 p_event->p_vout->p_sys->p_VideoWidget->setBaseSize( p_event->p_vout->p_sys->i_width,
628 p_event->p_vout->p_sys->i_height );
629 p_event->p_vout->p_sys->p_VideoWidget->setMaximumWidth( desktop->width() );
630 p_event->p_vout->p_sys->p_VideoWidget->setMaximumHeight( desktop->height() );
631 /* Check on fullscreen */
632 if (p_event->p_vout->b_fullscreen)
633 p_event->p_vout->p_sys->p_VideoWidget->showFullScreen();
635 p_event->p_vout->p_sys->p_VideoWidget->showNormal();
637 p_event->p_vout->p_sys->p_VideoWidget->show();
638 p_event->p_vout->p_sys->bRunning = TRUE;
641 while(!p_event->b_die && p_event->p_vout->p_sys->bRunning)
643 /* Check if we are asked to exit */
650 // run the main loop of qtapplication until someone says: 'quit'
651 p_event->p_vout->p_sys->pcQApplication->exec();
655 #ifndef NEED_QTE_MAIN
656 if(p_event->p_vout->p_sys->p_QApplication)
658 delete p_event->p_vout->p_sys->p_VideoWidget;
659 p_event->p_vout->p_sys->p_VideoWidget = NULL;
660 delete p_event->p_vout->p_sys->p_QApplication;
661 p_event->p_vout->p_sys->p_QApplication = NULL;
664 p_event->p_vout->p_sys->p_VideoWidget = NULL;
667 msg_Dbg( p_event->p_vout, "RunQtThread terminating" );