]> git.sesse.net Git - vlc/blob - modules/video_output/qte/qte.cpp
* ./modules/video_output/qte/.cvsignore: added a missing .cvsignore.
[vlc] / modules / video_output / qte / qte.cpp
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 $
6  *
7  * Authors: Gerald Hansink <gerald.hansink@ordain.nl>
8  *          Jean-Paul Saman <jpsaman@wxs.nl>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 /*****************************************************************************
30  * notes:
31  * - written for ipaq, so hardcoded assumptions specific for ipaq...
32  * - runs full screen
33  * - no "mouse events" handling
34  * - etc.
35  *****************************************************************************/
36
37 extern "C"
38 {
39 #include <errno.h>                                                 /* ENOMEM */
40 #include <stdlib.h>                                                /* free() */
41 #include <string.h>                                                /* strerror() */
42
43 #include <vlc/vlc.h>
44 #include <vlc/intf.h>
45 #include <vlc/vout.h>
46
47 #ifdef HAVE_MACHINE_PARAM_H
48     /* BSD */
49 #   include <machine/param.h>
50 #   include <sys/types.h>                                  /* typedef ushort */
51 #   include <sys/ipc.h>
52 #endif
53
54 #ifndef WIN32
55 #   include <netinet/in.h>                            /* BSD: struct in_addr */
56 #endif
57
58 #ifdef HAVE_SYS_SHM_H
59 #   include <sys/shm.h>                                /* shmget(), shmctl() */
60 #endif
61 } /* extern "C" */
62
63 #include <qapplication.h>
64 #include <qpainter.h>
65
66 #ifdef Q_WS_QWS
67 # define USE_DIRECT_PAINTER
68 # include <qdirectpainter_qws.h>
69 # include <qgfxraster_qws.h>
70 #endif
71
72 extern "C"
73 {
74 #include "netutils.h"                                 /* network_ChannelJoin */
75 #include "qte.h"
76
77 /*****************************************************************************
78  * Module descriptor
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.")
96
97 /*****************************************************************************
98  * Local prototypes
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 * );
107
108 static int  CreateQtWindow ( vout_thread_t * );
109 static void DestroyQtWindow( vout_thread_t * );
110
111 static int  NewPicture     ( vout_thread_t *, picture_t * );
112 static void FreePicture    ( vout_thread_t *, picture_t * );
113
114 static void ToggleFullScreen      ( vout_thread_t * );
115
116 static void RunQtThread( event_thread_t *p_event );
117 } /* extern "C" */
118
119 /*****************************************************************************
120 * Exported prototypes
121 *****************************************************************************/
122 extern "C"
123 {
124
125 vlc_module_begin();
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);
133 vlc_module_end();
134
135 } /* extern "C" */
136
137 /*****************************************************************************
138  * Seeking function TODO: put this in a generic location !
139  *****************************************************************************/
140 static inline void vout_Seek( off_t i_seek )
141 {
142 }
143
144 /*****************************************************************************
145  * Open: allocate video thread output method
146  *****************************************************************************/
147 static int Open( vlc_object_t *p_this )
148 {
149     vout_thread_t * p_vout = (vout_thread_t *)p_this;
150
151     /* Allocate structure */
152     p_vout->p_sys = (struct vout_sys_t*) malloc( sizeof( struct vout_sys_t ) );
153
154     if( p_vout->p_sys == NULL )
155     {
156         msg_Err( p_vout, "out of memory" );
157         return( 1 );
158     }
159
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;
166
167     CreateQtWindow(p_vout);
168     return( 0 );
169 }
170
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 )
177 {
178     vout_thread_t * p_vout = (vout_thread_t *)p_this;
179
180     msg_Err( p_vout, "Close" );
181     DestroyQtWindow(p_vout);
182     free(p_vout->p_sys);
183 }
184
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 )
192 {
193     int         i_index;
194     picture_t*  p_pic;
195     char                *psz_display;
196     int         dd = QPixmap::defaultDepth();
197
198     I_OUTPUTPICTURES = 0;
199
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;
204
205     psz_display = config_GetPsz(p_vout, "qte-display");
206     if( strncmp(psz_display, "portrait", 8)==0 )
207     {
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
212                                     * VOUT_ASPECT_FACTOR
213                                     / p_vout->output.i_height;
214     }
215     else
216     {
217          /* We may need to convert the chroma, but at least we keep the
218           * aspect ratio */
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;
222     }
223
224     /* Try to initialize MAX_DIRECTBUFFERS direct buffers */
225     while( I_OUTPUTPICTURES < QTE_MAX_DIRECTBUFFERS )
226     {
227         p_pic = NULL;
228
229         /* Find an empty picture slot */
230         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
231         {
232             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
233             {
234                 p_pic = p_vout->p_picture + i_index;
235                 break;
236             }
237         }
238
239         /* Allocate the picture */
240         if( p_pic == NULL ||  NewPicture( p_vout, p_pic ) )
241         {
242             break;
243         }
244
245         p_pic->i_status = DESTROYED_PICTURE;
246         p_pic->i_type   = DIRECT_PICTURE;
247
248         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
249
250         I_OUTPUTPICTURES++;
251     }
252
253     return( 0 );
254 }
255
256
257 /*****************************************************************************
258  * Render: render previously calculated output
259  *****************************************************************************/
260 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
261 {
262     ;
263 }
264
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 )
271 {
272     unsigned int x, y, w, h;
273
274     vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
275                        &x, &y, &w, &h );
276
277     if(p_vout->p_sys->pcVoutWidget)
278     {
279 // shameless borrowed from opie mediaplayer....
280 #ifndef USE_DIRECT_PAINTER
281         QPainter p(p_vout->p_sys->pcVoutWidget);
282
283         /* rotate frame */
284         int dd      = QPixmap::defaultDepth();
285         int bytes   = ( dd == 16 ) ? 2 : 4;
286         int rw = h, rh = w;
287
288         QImage rotatedFrame( rw, rh, bytes << 3 );
289
290         ushort* in  = (ushort*)p_pic->p_sys->pQImage->bits();
291         ushort* out = (ushort*)rotatedFrame.bits();
292
293         int spl = rotatedFrame.bytesPerLine() / bytes;
294         for (int x=0; x<h; x++)
295         {
296             if ( bytes == 2 )
297             {
298                 ushort* lout = out++ + (w - 1)*spl;
299                 for (int y=0; y<w; y++)
300                 {
301                     *lout=*in++;
302                     lout-=spl;
303                 }
304             }
305             else
306             {
307                 ulong* lout = ((ulong *)out)++ + (w - 1)*spl;
308                 for (int y=0; y<w; y++)
309                 {
310                     *lout=*((ulong*)in)++;
311                     lout-=spl;
312                 }
313             }
314         }
315
316         p.drawImage( x, y, rotatedFrame, 0, 0, rw, rh );
317 #else
318         QDirectPainter p(p_vout->p_sys->pcVoutWidget);
319
320         // just copy the image to the frame buffer...
321         memcpy(p.frameBuffer(), (p_pic->p_sys->pQImage->jumpTable())[0], h * p.lineStep());
322 #endif
323     }
324 }
325
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
331  * error.
332  *****************************************************************************/
333 static int Manage( vout_thread_t *p_vout )
334 {
335     return 0;
336 }
337
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 )
345 {
346     int i_index;
347
348     /* Free the direct buffers we allocated */
349     for( i_index = I_OUTPUTPICTURES ; i_index ; )
350     {
351         i_index--;
352         FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
353     }
354 }
355
356
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 )
363 {
364     int dd = QPixmap::defaultDepth();
365
366     msg_Dbg(p_vout, "+NewPicture::dd = %d",dd );
367
368     p_pic->p_sys = (picture_sys_t*) malloc( sizeof( picture_sys_t ) );
369
370     if( p_pic->p_sys == NULL )
371     {
372         return -1;
373     }
374
375     switch(p_vout->output.i_chroma)
376     {
377     case VLC_FOURCC('R','V','1','6'):
378         if(dd == 16)
379         {
380             p_pic->p_sys->pQImage = new QImage(p_vout->output.i_width,
381                                                p_vout->output.i_height,
382                                                dd );
383
384             if(p_pic->p_sys->pQImage == NULL)
385             {
386                 return -1;
387             }
388
389             p_pic->p->p_pixels = (p_pic->p_sys->pQImage->jumpTable())[0];
390
391             p_pic->p->i_pitch = p_pic->p_sys->pQImage->bytesPerLine();
392
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;
398             p_pic->i_planes         = 1;
399         }
400         else
401         {
402             return -1;
403         }
404         break;
405     case VLC_FOURCC('R','V','3','2'):
406         if(dd == 32)
407         {
408             p_pic->p_sys->pQImage = new QImage(p_vout->output.i_width,
409                                                p_vout->output.i_height,
410                                                dd );
411
412             if(p_pic->p_sys->pQImage == NULL)
413             {
414                 return -1;
415             }
416
417             p_pic->p->p_pixels = (p_pic->p_sys->pQImage->jumpTable())[0];
418
419             p_pic->p->i_pitch = p_pic->p_sys->pQImage->bytesPerLine();
420
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;
426             p_pic->i_planes         = 1;
427         }
428         else
429         {
430             return -1;
431         }
432         break;
433     default:
434         return -1;
435         break;
436     }
437
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 );
441
442     return 0;
443 }
444
445 /*****************************************************************************
446  * FreePicture: destroy a picture allocated with NewPicture
447  *****************************************************************************/
448 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
449 {
450     delete p_pic->p_sys->pQImage;
451 }
452
453 /*****************************************************************************
454  * ToggleFullScreen: Enable or disable full screen mode
455  *****************************************************************************
456  * This function will switch between fullscreen and window mode.
457  *
458  *****************************************************************************/
459 static void ToggleFullScreen ( vout_thread_t *p_vout )
460 {
461 }
462
463
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 )
471 {
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" );
475
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;
478
479     /* Initializations */
480     p_vout->p_sys->i_width  = 320;
481     p_vout->p_sys->i_height = 240;
482
483     /* create thread to exec the qpe application */
484     if ( vlc_thread_create( p_vout->p_sys->p_event, "QT Embedded Thread",
485                                                         RunQtThread,
486                             VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE) )
487     {
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;
491         goto error;
492     }
493
494     if( p_vout->p_sys->p_event->b_error )
495     {
496         msg_Err( p_vout, "RunQtThread failed" );
497         goto error;
498     }
499
500     vlc_object_attach( p_vout->p_sys->p_event, p_vout );
501     msg_Dbg( p_vout, "RunQtThread running" );
502
503     // just wait until the crew is complete...
504     while(p_vout->p_sys->pcVoutWidget == NULL)
505     {
506         msleep(1);
507     }
508
509     return VLC_SUCCESS;
510
511  error:
512     Close( VLC_OBJECT(p_vout) );
513     return VLC_EGENERIC;
514 }
515
516
517 /*****************************************************************************
518  * DestroyQtWindow: destroy the window
519  *****************************************************************************/
520 static void DestroyQtWindow( vout_thread_t *p_vout )
521 {
522     // quit qt application loop
523     if(p_vout->p_sys->pcQApplication)
524     {
525         if(p_vout->p_sys->bOwnsQApp)
526         {
527             p_vout->p_sys->pcQApplication->quit();
528         }
529         else
530         {
531             p_vout->p_sys->bRunning = FALSE;
532         }
533
534         while(p_vout->p_sys->pcVoutWidget)
535         {
536             msleep(1);
537         }
538     }
539 }
540
541 /*****************************************************************************
542  * main loop of qtapplication
543  *****************************************************************************/
544 static void RunQtThread(event_thread_t *p_event)
545 {
546     int     argc    = 0;
547
548     msg_Dbg( p_event->p_vout, "+qte::RunQtThread" );
549
550     if(qApp == NULL)
551     {
552         QApplication* pApp = new QApplication(argc, NULL);
553         if(pApp)
554         {
555             p_event->p_vout->p_sys->pcQApplication = pApp;
556             p_event->p_vout->p_sys->bOwnsQApp = TRUE;
557         }
558         msg_Dbg( p_event->p_vout, "RunQtThread application created" );
559     }
560     else
561     {
562         p_event->p_vout->p_sys->pcQApplication = qApp;
563         msg_Dbg( p_event->p_vout, "RunQtThread applicaton attached" );
564     }
565
566     /* signal the creation of the window */
567     vlc_thread_ready( p_event );
568     msg_Dbg( p_event->p_vout, "+qte::RunQtThread ready" );
569
570     if (p_event->p_vout->p_sys->pcQApplication)
571     {
572         QWidget vo(0, "qte");
573         vo.showFullScreen();
574         vo.show();
575         p_event->p_vout->p_sys->pcVoutWidget = &vo;
576         p_event->p_vout->p_sys->bRunning = TRUE;
577
578         if(p_event->p_vout->p_sys->bOwnsQApp)
579         {
580             // run the main loop of qtapplication until someone says: 'quit'
581             p_event->p_vout->p_sys->pcQApplication->exec();
582         }
583         else
584         {
585             while(!p_event->b_die && p_event->p_vout->p_sys->bRunning)
586                         {
587                    /* Check if we are asked to exit */
588                if( p_event->b_die )
589                    break;
590
591                                 msleep(100);
592                         }
593         }
594     }
595
596     p_event->p_vout->p_sys->pcVoutWidget = NULL;
597
598     if(p_event->p_vout->p_sys->bOwnsQApp)
599     {
600         delete p_event->p_vout->p_sys->pcQApplication;
601         p_event->p_vout->p_sys->pcQApplication = NULL;
602     }
603
604     msg_Dbg( p_event->p_vout, "-qte::RunQtThread terminating" );
605 }
606