1 /*****************************************************************************
2 * qte.cpp : QT Embedded plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 1998-2002 VideoLAN
5 * $Id: qte.cpp,v 1.7 2002/12/11 21:50:03 jpsaman Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, 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>
74 #include "netutils.h" /* network_ChannelJoin */
77 /*****************************************************************************
79 *****************************************************************************/
80 #define ALT_FS_TEXT N_("alternate fullscreen method")
81 #define ALT_FS_LONGTEXT N_( \
82 "There are two ways to make a fullscreen window, unfortunately each one " \
83 "has its drawbacks.\n" \
84 "1) Let the window manager handle your fullscreen window (default). But " \
85 "things like taskbars will likely show on top of the video.\n" \
86 "2) Completly bypass the window manager, but then nothing will be able " \
87 "to show on top of the video.")
88 #define DISPLAY_TEXT N_("QT Embedded display name")
89 #define DISPLAY_LONGTEXT N_( \
90 "Specify the Qt Embedded hardware display you want to use. By default vlc will " \
91 "use the value of the DISPLAY environment variable.")
92 #define DRAWABLE_TEXT N_("QT Embedded drawable")
93 #define DRAWABLE_LONGTEXT N_( \
94 "Specify a QT Embedded drawable to use instead of opening a new window. This " \
95 "option is DANGEROUS, use with care.")
97 /*****************************************************************************
99 *****************************************************************************/
100 static int Open ( vlc_object_t * );
101 static void Close ( vlc_object_t * );
102 static void Render ( vout_thread_t *, picture_t * );
103 static void Display ( vout_thread_t *, picture_t * );
104 static int Manage ( vout_thread_t * );
105 static int Init ( vout_thread_t * );
106 static void End ( vout_thread_t * );
108 static int CreateQtWindow ( vout_thread_t * );
109 static void DestroyQtWindow( vout_thread_t * );
111 static int NewPicture ( vout_thread_t *, picture_t * );
112 static void FreePicture ( vout_thread_t *, picture_t * );
114 static void ToggleFullScreen ( vout_thread_t * );
116 static void RunQtThread( event_thread_t *p_event );
119 /*****************************************************************************
120 * Exported prototypes
121 *****************************************************************************/
126 add_category_hint( N_("QT Embedded"), NULL );
127 add_string( "qte-display", "portrait", NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT);
128 add_bool( "qte-altfullscreen", 0, NULL, ALT_FS_TEXT, ALT_FS_LONGTEXT);
129 // add_integer( "qte-drawable", -1, NULL, NULL, NULL); //DRAWABLE_TEXT, DRAWABLE_LONGTEXT );
130 set_description( _("QT Embedded module") );
131 set_capability( "video output", 30 );
132 set_callbacks( Open, Close);
137 /*****************************************************************************
138 * Seeking function TODO: put this in a generic location !
139 *****************************************************************************/
140 static inline void vout_Seek( off_t i_seek )
144 /*****************************************************************************
145 * Open: allocate video thread output method
146 *****************************************************************************/
147 static int Open( vlc_object_t *p_this )
149 vout_thread_t * p_vout = (vout_thread_t *)p_this;
151 /* Allocate structure */
152 p_vout->p_sys = (struct vout_sys_t*) malloc( sizeof( struct vout_sys_t ) );
154 if( p_vout->p_sys == NULL )
156 msg_Err( p_vout, "out of memory" );
160 // memset(p_vout->p_sys, 0, sizeof( struct vout_sys_t ));
161 p_vout->pf_init = Init;
162 p_vout->pf_end = End;
163 p_vout->pf_manage = NULL; //Manage;
164 p_vout->pf_render = NULL; //Render;
165 p_vout->pf_display = Display;
167 CreateQtWindow(p_vout);
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_Err( p_vout, "Close" );
181 DestroyQtWindow(p_vout);
185 /*****************************************************************************
186 * Init: initialize video thread output method
187 *****************************************************************************
188 * This function create the buffers needed by the output thread. It is called
189 * at the beginning of the thread, but also each time the window is resized.
190 *****************************************************************************/
191 static int Init( vout_thread_t *p_vout )
196 int dd = QPixmap::defaultDepth();
198 I_OUTPUTPICTURES = 0;
200 p_vout->output.i_chroma = (dd == 16) ? VLC_FOURCC('R','V','1','6'): VLC_FOURCC('R','V','3','2');
201 p_vout->output.i_rmask = 0xf800;
202 p_vout->output.i_gmask = 0x07e0;
203 p_vout->output.i_bmask = 0x001f;
205 psz_display = config_GetPsz(p_vout, "qte-display");
206 if( strncmp(psz_display, "portrait", 8)==0 )
208 /* All we have is an RGB image with square pixels */
209 p_vout->output.i_width = p_vout->p_sys->i_width;
210 p_vout->output.i_height = p_vout->p_sys->i_height;
211 p_vout->output.i_aspect = p_vout->output.i_width
213 / p_vout->output.i_height;
217 /* We may need to convert the chroma, but at least we keep the
219 p_vout->output.i_width = p_vout->render.i_width;
220 p_vout->output.i_height = p_vout->render.i_height;
221 p_vout->output.i_aspect = p_vout->render.i_aspect;
224 /* Try to initialize MAX_DIRECTBUFFERS direct buffers */
225 while( I_OUTPUTPICTURES < QTE_MAX_DIRECTBUFFERS )
229 /* Find an empty picture slot */
230 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
232 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
234 p_pic = p_vout->p_picture + i_index;
239 /* Allocate the picture */
240 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
245 p_pic->i_status = DESTROYED_PICTURE;
246 p_pic->i_type = DIRECT_PICTURE;
248 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
257 /*****************************************************************************
258 * Render: render previously calculated output
259 *****************************************************************************/
260 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
265 /*****************************************************************************
266 * Display: displays previously rendered output
267 *****************************************************************************
268 * This function sends the currently rendered image to screen.
269 *****************************************************************************/
270 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
272 unsigned int x, y, w, h;
274 vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
277 if(p_vout->p_sys->pcVoutWidget)
279 // shameless borrowed from opie mediaplayer....
280 #ifndef USE_DIRECT_PAINTER
281 QPainter p(p_vout->p_sys->pcVoutWidget);
284 int dd = QPixmap::defaultDepth();
285 int bytes = ( dd == 16 ) ? 2 : 4;
288 QImage rotatedFrame( rw, rh, bytes << 3 );
290 ushort* in = (ushort*)p_pic->p_sys->pQImage->bits();
291 ushort* out = (ushort*)rotatedFrame.bits();
293 int spl = rotatedFrame.bytesPerLine() / bytes;
294 for (int x=0; x<h; x++)
298 ushort* lout = out++ + (w - 1)*spl;
299 for (int y=0; y<w; y++)
307 ulong* lout = ((ulong *)out)++ + (w - 1)*spl;
308 for (int y=0; y<w; y++)
310 *lout=*((ulong*)in)++;
316 p.drawImage( x, y, rotatedFrame, 0, 0, rw, rh );
318 QDirectPainter p(p_vout->p_sys->pcVoutWidget);
320 // just copy the image to the frame buffer...
321 memcpy(p.frameBuffer(), (p_pic->p_sys->pQImage->jumpTable())[0], h * p.lineStep());
326 /*****************************************************************************
327 * Manage: handle X11 events
328 *****************************************************************************
329 * This function should be called regularly by video output thread. It manages
330 * X11 events and allows window resizing. It returns a non null value on
332 *****************************************************************************/
333 static int Manage( vout_thread_t *p_vout )
338 /*****************************************************************************
339 * End: terminate video thread output method
340 *****************************************************************************
341 * Destroy the buffers created by vout_Init. It is called at the end of
342 * the thread, but also each time the window is resized.
343 *****************************************************************************/
344 static void End( vout_thread_t *p_vout )
348 /* Free the direct buffers we allocated */
349 for( i_index = I_OUTPUTPICTURES ; i_index ; )
352 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
357 /*****************************************************************************
358 * NewPicture: allocate a picture
359 *****************************************************************************
360 * Returns 0 on success, -1 otherwise
361 *****************************************************************************/
362 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
364 int dd = QPixmap::defaultDepth();
366 msg_Dbg(p_vout, "+NewPicture::dd = %d",dd );
368 p_pic->p_sys = (picture_sys_t*) malloc( sizeof( picture_sys_t ) );
370 if( p_pic->p_sys == NULL )
375 switch(p_vout->output.i_chroma)
377 case VLC_FOURCC('R','V','1','6'):
380 p_pic->p_sys->pQImage = new QImage(p_vout->output.i_width,
381 p_vout->output.i_height,
384 if(p_pic->p_sys->pQImage == NULL)
389 p_pic->p->p_pixels = (p_pic->p_sys->pQImage->jumpTable())[0];
391 p_pic->p->i_pitch = p_pic->p_sys->pQImage->bytesPerLine();
393 p_pic->p->i_lines = p_vout->output.i_height;
394 p_pic->p->i_pixel_pitch = 2;
395 p_pic->p->i_visible_pitch = 0;
396 // p_pic->p->i_pixel_bytes = 2;
397 // p_pic->p->b_margin = 0;
405 case VLC_FOURCC('R','V','3','2'):
408 p_pic->p_sys->pQImage = new QImage(p_vout->output.i_width,
409 p_vout->output.i_height,
412 if(p_pic->p_sys->pQImage == NULL)
417 p_pic->p->p_pixels = (p_pic->p_sys->pQImage->jumpTable())[0];
419 p_pic->p->i_pitch = p_pic->p_sys->pQImage->bytesPerLine();
421 p_pic->p->i_lines = p_vout->output.i_height;
422 p_pic->p->i_pixel_pitch = 4;
423 p_pic->p->i_visible_pitch = 0;
424 // p_pic->p->i_pixel_bytes = 4;
425 // p_pic->p->b_margin = 0;
438 msg_Dbg(p_vout, "-NewPicture: %d %d %d",p_vout->output.i_width,
439 p_vout->output.i_height,
440 p_vout->output.i_chroma );
445 /*****************************************************************************
446 * FreePicture: destroy a picture allocated with NewPicture
447 *****************************************************************************/
448 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
450 delete p_pic->p_sys->pQImage;
453 /*****************************************************************************
454 * ToggleFullScreen: Enable or disable full screen mode
455 *****************************************************************************
456 * This function will switch between fullscreen and window mode.
458 *****************************************************************************/
459 static void ToggleFullScreen ( vout_thread_t *p_vout )
464 /*****************************************************************************
465 * CreateQtWindow: create qte applicaton / window
466 *****************************************************************************
467 * Create a window according to video output given size, and set other
468 * properties according to the display properties.
469 *****************************************************************************/
470 static int CreateQtWindow( vout_thread_t *p_vout )
472 /* for displaying the vout in a qt window we need the QtApplication */
473 p_vout->p_sys->pcVoutWidget = NULL;
474 msg_Dbg( p_vout, "creating RunQtThread" );
476 p_vout->p_sys->p_event = (event_thread_t*) vlc_object_create( p_vout, sizeof(event_thread_t) );
477 p_vout->p_sys->p_event->p_vout = p_vout;
479 /* Initializations */
480 p_vout->p_sys->i_width = 320;
481 p_vout->p_sys->i_height = 240;
483 /* create thread to exec the qpe application */
484 if ( vlc_thread_create( p_vout->p_sys->p_event, "QT Embedded Thread",
486 VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE) )
488 msg_Err( p_vout, "cannot create QT Embedded Thread" );
489 vlc_object_destroy( p_vout->p_sys->p_event );
490 p_vout->p_sys->p_event = NULL;
494 if( p_vout->p_sys->p_event->b_error )
496 msg_Err( p_vout, "RunQtThread failed" );
500 vlc_object_attach( p_vout->p_sys->p_event, p_vout );
501 msg_Dbg( p_vout, "RunQtThread running" );
503 // just wait until the crew is complete...
504 while(p_vout->p_sys->pcVoutWidget == NULL)
512 Close( VLC_OBJECT(p_vout) );
517 /*****************************************************************************
518 * DestroyQtWindow: destroy the window
519 *****************************************************************************/
520 static void DestroyQtWindow( vout_thread_t *p_vout )
522 // quit qt application loop
523 if(p_vout->p_sys->pcQApplication)
525 if(p_vout->p_sys->bOwnsQApp)
527 p_vout->p_sys->pcQApplication->quit();
531 p_vout->p_sys->bRunning = FALSE;
534 while(p_vout->p_sys->pcVoutWidget)
541 /*****************************************************************************
542 * main loop of qtapplication
543 *****************************************************************************/
544 static void RunQtThread(event_thread_t *p_event)
548 msg_Dbg( p_event->p_vout, "+qte::RunQtThread" );
552 QApplication* pApp = new QApplication(argc, NULL);
555 p_event->p_vout->p_sys->pcQApplication = pApp;
556 p_event->p_vout->p_sys->bOwnsQApp = TRUE;
558 msg_Dbg( p_event->p_vout, "RunQtThread application created" );
562 p_event->p_vout->p_sys->pcQApplication = qApp;
563 msg_Dbg( p_event->p_vout, "RunQtThread applicaton attached" );
566 /* signal the creation of the window */
567 vlc_thread_ready( p_event );
568 msg_Dbg( p_event->p_vout, "+qte::RunQtThread ready" );
570 if (p_event->p_vout->p_sys->pcQApplication)
572 QWidget vo(0, "qte");
575 p_event->p_vout->p_sys->pcVoutWidget = &vo;
576 p_event->p_vout->p_sys->bRunning = TRUE;
578 if(p_event->p_vout->p_sys->bOwnsQApp)
580 // run the main loop of qtapplication until someone says: 'quit'
581 p_event->p_vout->p_sys->pcQApplication->exec();
585 while(!p_event->b_die && p_event->p_vout->p_sys->bRunning)
587 /* Check if we are asked to exit */
596 p_event->p_vout->p_sys->pcVoutWidget = NULL;
598 if(p_event->p_vout->p_sys->bOwnsQApp)
600 delete p_event->p_vout->p_sys->pcQApplication;
601 p_event->p_vout->p_sys->pcQApplication = NULL;
604 msg_Dbg( p_event->p_vout, "-qte::RunQtThread terminating" );