1 /*****************************************************************************
2 * qte.cpp : QT Embedded plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 1998-2002 VideoLAN
5 * $Id: qte.cpp,v 1.8 2002/12/24 19:25:54 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", "landscape", 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 if( p_vout->p_sys->p_event )
183 vlc_object_detach( p_vout->p_sys->p_event );
185 /* Kill RunQtThread */
186 p_vout->p_sys->p_event->b_die = VLC_TRUE;
187 DestroyQtWindow(p_vout);
189 vlc_thread_join( p_vout->p_sys->p_event );
190 vlc_object_destroy( p_vout->p_sys->p_event );
195 free( p_vout->p_sys );
196 p_vout->p_sys = NULL;
200 /*****************************************************************************
201 * Init: initialize video thread output method
202 *****************************************************************************
203 * This function create the buffers needed by the output thread. It is called
204 * at the beginning of the thread, but also each time the window is resized.
205 *****************************************************************************/
206 static int Init( vout_thread_t *p_vout )
210 int dd = QPixmap::defaultDepth();
212 I_OUTPUTPICTURES = 0;
214 p_vout->output.i_chroma = (dd == 16) ? VLC_FOURCC('R','V','1','6'): VLC_FOURCC('R','V','3','2');
215 p_vout->output.i_rmask = 0xf800;
216 p_vout->output.i_gmask = 0x07e0;
217 p_vout->output.i_bmask = 0x001f;
219 /* We may need to convert the chroma, but at least we keep the
221 p_vout->output.i_width = p_vout->p_sys->i_width;
222 p_vout->output.i_height = p_vout->p_sys->i_height;
223 p_vout->output.i_aspect = p_vout->render.i_aspect;
225 /* Try to initialize MAX_DIRECTBUFFERS direct buffers */
226 while( I_OUTPUTPICTURES < QTE_MAX_DIRECTBUFFERS )
230 /* Find an empty picture slot */
231 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
233 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
235 p_pic = p_vout->p_picture + i_index;
240 /* Allocate the picture */
241 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
246 p_pic->i_status = DESTROYED_PICTURE;
247 p_pic->i_type = DIRECT_PICTURE;
249 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
258 /*****************************************************************************
259 * Render: render previously calculated output
260 *****************************************************************************/
261 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
266 /*****************************************************************************
267 * Display: displays previously rendered output
268 *****************************************************************************
269 * This function sends the currently rendered image to screen.
270 *****************************************************************************/
271 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
273 unsigned int x, y, w, h;
275 vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
278 msg_Err(p_vout, "+qte::Display( p_vout, i_width=%d, i_height=%d, x=%u, y=%u, w=%u, h=%u",
279 p_vout->p_sys->i_width, p_vout->p_sys->i_height, x, y, w, h );
281 if(p_vout->p_sys->pcVoutWidget)
283 // shameless borrowed from opie mediaplayer....
284 #ifndef USE_DIRECT_PAINTER
285 QPainter p(p_vout->p_sys->pcVoutWidget);
288 int dd = QPixmap::defaultDepth();
289 int bytes = ( dd == 16 ) ? 2 : 4;
292 QImage rotatedFrame( rw, rh, bytes << 3 );
294 ushort* in = (ushort*)p_pic->p_sys->pQImage->bits();
295 ushort* out = (ushort*)rotatedFrame.bits();
297 int spl = rotatedFrame.bytesPerLine() / bytes;
298 for (int x=0; x<h; x++)
302 ushort* lout = out++ + (w - 1)*spl;
303 for (int y=0; y<w; y++)
311 ulong* lout = ((ulong *)out)++ + (w - 1)*spl;
312 for (int y=0; y<w; y++)
314 *lout=*((ulong*)in)++;
320 p.drawImage( x, y, rotatedFrame, 0, 0, rw, rh );
322 QDirectPainter p(p_vout->p_sys->pcVoutWidget);
324 // just copy the image to the frame buffer...
325 memcpy(p.frameBuffer(), (p_pic->p_sys->pQImage->jumpTable())[0], h * p.lineStep());
328 msg_Err(p_vout, "-qte::Display" );
332 /*****************************************************************************
333 * Manage: handle X11 events
334 *****************************************************************************
335 * This function should be called regularly by video output thread. It manages
336 * X11 events and allows window resizing. It returns a non null value on
338 *****************************************************************************/
339 static int Manage( vout_thread_t *p_vout )
344 /*****************************************************************************
345 * End: terminate video thread output method
346 *****************************************************************************
347 * Destroy the buffers created by vout_Init. It is called at the end of
348 * the thread, but also each time the window is resized.
349 *****************************************************************************/
350 static void End( vout_thread_t *p_vout )
354 /* Free the direct buffers we allocated */
355 for( i_index = I_OUTPUTPICTURES ; i_index ; )
358 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
363 /*****************************************************************************
364 * NewPicture: allocate a picture
365 *****************************************************************************
366 * Returns 0 on success, -1 otherwise
367 *****************************************************************************/
368 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
370 int dd = QPixmap::defaultDepth();
372 msg_Dbg(p_vout, "+NewPicture::dd = %d",dd );
374 p_pic->p_sys = (picture_sys_t*) malloc( sizeof( picture_sys_t ) );
376 if( p_pic->p_sys == NULL )
381 switch(p_vout->output.i_chroma)
383 case VLC_FOURCC('R','V','1','6'):
386 p_pic->p_sys->pQImage = new QImage(p_vout->output.i_width,
387 p_vout->output.i_height,
390 if(p_pic->p_sys->pQImage == NULL)
395 p_pic->p->p_pixels = (p_pic->p_sys->pQImage->jumpTable())[0];
397 p_pic->p->i_pitch = p_pic->p_sys->pQImage->bytesPerLine();
399 p_pic->p->i_lines = p_vout->output.i_height;
400 p_pic->p->i_pixel_pitch = 2;
401 p_pic->p->i_visible_pitch = 0;
402 // p_pic->p->i_pixel_bytes = 2;
403 // p_pic->p->b_margin = 0;
411 case VLC_FOURCC('R','V','3','2'):
414 p_pic->p_sys->pQImage = new QImage(p_vout->output.i_width,
415 p_vout->output.i_height,
418 if(p_pic->p_sys->pQImage == NULL)
423 p_pic->p->p_pixels = (p_pic->p_sys->pQImage->jumpTable())[0];
425 p_pic->p->i_pitch = p_pic->p_sys->pQImage->bytesPerLine();
427 p_pic->p->i_lines = p_vout->output.i_height;
428 p_pic->p->i_pixel_pitch = 4;
429 p_pic->p->i_visible_pitch = 0;
430 // p_pic->p->i_pixel_bytes = 4;
431 // p_pic->p->b_margin = 0;
444 msg_Dbg(p_vout, "-NewPicture: %d %d %d",p_vout->output.i_width,
445 p_vout->output.i_height,
446 p_vout->output.i_chroma );
451 /*****************************************************************************
452 * FreePicture: destroy a picture allocated with NewPicture
453 *****************************************************************************/
454 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
456 delete p_pic->p_sys->pQImage;
459 /*****************************************************************************
460 * ToggleFullScreen: Enable or disable full screen mode
461 *****************************************************************************
462 * This function will switch between fullscreen and window mode.
464 *****************************************************************************/
465 static void ToggleFullScreen ( vout_thread_t *p_vout )
470 /*****************************************************************************
471 * CreateQtWindow: create qte applicaton / window
472 *****************************************************************************
473 * Create a window according to video output given size, and set other
474 * properties according to the display properties.
475 *****************************************************************************/
476 static int CreateQtWindow( vout_thread_t *p_vout )
478 /* for displaying the vout in a qt window we need the QtApplication */
479 p_vout->p_sys->pcVoutWidget = NULL;
480 msg_Dbg( p_vout, "creating RunQtThread" );
482 p_vout->p_sys->p_event = (event_thread_t*) vlc_object_create( p_vout, sizeof(event_thread_t) );
483 p_vout->p_sys->p_event->p_vout = p_vout;
485 /* Initializations */
486 p_vout->p_sys->i_width = 320;
487 p_vout->p_sys->i_height = 240;
489 /* create thread to exec the qpe application */
490 if ( vlc_thread_create( p_vout->p_sys->p_event, "QT Embedded Thread",
492 VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE) )
494 msg_Err( p_vout, "cannot create QT Embedded Thread" );
495 vlc_object_destroy( p_vout->p_sys->p_event );
496 p_vout->p_sys->p_event = NULL;
500 if( p_vout->p_sys->p_event->b_error )
502 msg_Err( p_vout, "RunQtThread failed" );
506 vlc_object_attach( p_vout->p_sys->p_event, p_vout );
507 msg_Dbg( p_vout, "RunQtThread running" );
509 // just wait until the crew is complete...
510 while(p_vout->p_sys->pcVoutWidget == NULL)
518 Close( VLC_OBJECT(p_vout) );
523 /*****************************************************************************
524 * DestroyQtWindow: destroy the window
525 *****************************************************************************/
526 static void DestroyQtWindow( vout_thread_t *p_vout )
528 // quit qt application loop
529 if(p_vout->p_sys->pcQApplication)
531 if(p_vout->p_sys->bOwnsQApp)
533 p_vout->p_sys->pcQApplication->quit();
537 p_vout->p_sys->bRunning = FALSE;
540 while(p_vout->p_sys->pcVoutWidget)
547 /*****************************************************************************
548 * main loop of qtapplication
549 *****************************************************************************/
550 static void RunQtThread(event_thread_t *p_event)
554 msg_Dbg( p_event->p_vout, "+qte::RunQtThread" );
558 QApplication* pApp = new QApplication(argc, NULL);
561 p_event->p_vout->p_sys->pcQApplication = pApp;
562 p_event->p_vout->p_sys->bOwnsQApp = TRUE;
564 msg_Dbg( p_event->p_vout, "RunQtThread application created" );
568 p_event->p_vout->p_sys->pcQApplication = qApp;
569 msg_Dbg( p_event->p_vout, "RunQtThread applicaton attached" );
572 /* signal the creation of the window */
573 vlc_thread_ready( p_event );
574 msg_Dbg( p_event->p_vout, "+qte::RunQtThread ready" );
576 if (p_event->p_vout->p_sys->pcQApplication)
578 QWidget vo(0, "qte");
581 p_event->p_vout->p_sys->pcVoutWidget = &vo;
582 p_event->p_vout->p_sys->bRunning = TRUE;
584 if(p_event->p_vout->p_sys->bOwnsQApp)
586 // run the main loop of qtapplication until someone says: 'quit'
587 p_event->p_vout->p_sys->pcQApplication->exec();
591 while(!p_event->b_die && p_event->p_vout->p_sys->bRunning)
593 /* Check if we are asked to exit */
602 p_event->p_vout->p_sys->pcVoutWidget = NULL;
604 if(p_event->p_vout->p_sys->bOwnsQApp)
606 delete p_event->p_vout->p_sys->pcQApplication;
607 p_event->p_vout->p_sys->pcQApplication = NULL;
610 msg_Dbg( p_event->p_vout, "-qte::RunQtThread terminating" );