]> git.sesse.net Git - vlc/blob - modules/video_output/qte/qte.cpp
a1bca2abafa4563d89c4705dec866093ca3abf83
[vlc] / modules / video_output / qte / qte.cpp
1 /*****************************************************************************
2  * qte.cpp : QT Embedded plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 1998-2003 VideoLAN
5  * $Id: qte.cpp,v 1.18 2003/05/05 16:09:38 gbazin 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 "qte.h"
75
76 /*****************************************************************************
77  * Module descriptor
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. By default VLC will " \
82     "use the value of the DISPLAY environment variable.")
83
84 /*****************************************************************************
85  * Local prototypes
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 * );
94
95 static int  OpenDisplay ( vout_thread_t * );
96 static void CloseDisplay( vout_thread_t * );
97
98 static int  NewPicture     ( vout_thread_t *, picture_t * );
99 static void FreePicture    ( vout_thread_t *, picture_t * );
100
101 static void ToggleFullScreen      ( vout_thread_t * );
102
103 static void RunQtThread( event_thread_t *p_event );
104 } /* extern "C" */
105
106 /*****************************************************************************
107 * Exported prototypes
108 *****************************************************************************/
109 extern "C"
110 {
111
112 vlc_module_begin();
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);
119 vlc_module_end();
120
121 } /* extern "C" */
122
123 /*****************************************************************************
124  * Seeking function TODO: put this in a generic location !
125  *****************************************************************************/
126 static inline void vout_Seek( off_t i_seek )
127 {
128 }
129
130 /*****************************************************************************
131  * Open: allocate video thread output method
132  *****************************************************************************/
133 static int Open( vlc_object_t *p_this )
134 {
135     vout_thread_t * p_vout = (vout_thread_t *)p_this;
136
137     /* Allocate structure */
138     p_vout->p_sys = (struct vout_sys_t*) malloc( sizeof( struct vout_sys_t ) );
139
140     if( p_vout->p_sys == NULL )
141     {
142         msg_Err( p_vout, "out of memory" );
143         return( 1 );
144     }
145
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;
151
152 #ifdef NEED_QTE_MAIN
153     p_vout->p_sys->p_qte_main = module_Need( p_this, "gui-helper", "qte" );
154     if( p_vout->p_sys->p_qte_main == NULL )
155     {
156         free( p_vout->p_sys );
157         return VLC_ENOMOD;
158     }
159 #endif
160
161     if (OpenDisplay(p_vout))
162     {
163         msg_Err( p_vout, "Cannot set up qte video output" );
164         Close(p_this);
165         return( -1 );
166     }
167     return( 0 );
168 }
169
170 /*****************************************************************************
171  * CloseVideo: destroy Sys video thread output method
172  *****************************************************************************
173  * Terminate an output method created by Open
174  *****************************************************************************/
175 static void Close ( vlc_object_t *p_this )
176 {
177     vout_thread_t * p_vout = (vout_thread_t *)p_this;
178
179     msg_Dbg( p_vout, "Close" );
180     if( p_vout->p_sys->p_event )
181     {
182         vlc_object_detach( p_vout->p_sys->p_event );
183
184         /* Kill RunQtThread */
185         p_vout->p_sys->p_event->b_die = VLC_TRUE;
186         CloseDisplay(p_vout);
187
188         vlc_thread_join( p_vout->p_sys->p_event );
189         vlc_object_destroy( p_vout->p_sys->p_event );
190     }
191
192 #ifdef NEED_QTE_MAIN
193     msg_Dbg( p_vout, "Releasing gui-helper" );
194     module_Unneed( p_vout, p_vout->p_sys->p_qte_main );
195 #endif
196
197     if( p_vout->p_sys )
198     {
199         free( p_vout->p_sys );
200         p_vout->p_sys = NULL;
201     }
202 }
203
204 /*****************************************************************************
205  * Init: initialize video thread output method
206  *****************************************************************************
207  * This function create the buffers needed by the output thread. It is called
208  * at the beginning of the thread, but also each time the window is resized.
209  *****************************************************************************/
210 static int Init( vout_thread_t *p_vout )
211 {
212     int         i_index;
213     picture_t*  p_pic;
214     int         dd = QPixmap::defaultDepth();
215
216     I_OUTPUTPICTURES = 0;
217
218     p_vout->output.i_chroma = (dd == 16) ? VLC_FOURCC('R','V','1','6'): VLC_FOURCC('R','V','3','2');
219     p_vout->output.i_rmask  = 0xf800;
220     p_vout->output.i_gmask  = 0x07e0;
221     p_vout->output.i_bmask  = 0x001f;
222
223     /* All we have is an RGB image with square pixels */
224     p_vout->output.i_width  = p_vout->p_sys->i_width;
225     p_vout->output.i_height = p_vout->p_sys->i_height;
226     if( !p_vout->b_fullscreen )
227     {
228         p_vout->output.i_aspect = p_vout->output.i_width
229                                    * VOUT_ASPECT_FACTOR
230                                    / p_vout->output.i_height;
231     }
232     else
233     {
234         p_vout->output.i_aspect = p_vout->render.i_aspect;
235     }
236 #if 0
237     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 );
238 #endif
239     /* Try to initialize MAX_DIRECTBUFFERS direct buffers */
240     while( I_OUTPUTPICTURES < QTE_MAX_DIRECTBUFFERS )
241     {
242         p_pic = NULL;
243
244         /* Find an empty picture slot */
245         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
246         {
247             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
248             {
249                 p_pic = p_vout->p_picture + i_index;
250                 break;
251             }
252         }
253
254         /* Allocate the picture */
255         if( p_pic == NULL ||  NewPicture( p_vout, p_pic ) )
256         {
257             break;
258         }
259
260         p_pic->i_status = DESTROYED_PICTURE;
261         p_pic->i_type   = DIRECT_PICTURE;
262
263         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
264
265         I_OUTPUTPICTURES++;
266     }
267
268     return( 0 );
269 }
270
271
272 /*****************************************************************************
273  * Render: render previously calculated output
274  *****************************************************************************/
275 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
276 {
277     ;
278 }
279
280 /*****************************************************************************
281  * Display: displays previously rendered output
282  *****************************************************************************
283  * This function sends the currently rendered image to screen.
284  *****************************************************************************/
285 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
286 {
287     unsigned int x, y, w, h;
288
289     vout_PlacePicture( p_vout, p_vout->output.i_width, p_vout->output.i_height,
290                        &x, &y, &w, &h );
291 #if 0
292     msg_Dbg(p_vout, "+qte::Display( p_vout, i_width=%d, i_height=%d, x=%u, y=%u, w=%u, h=%u",
293         p_vout->output.i_width, p_vout->output.i_height, x, y, w, h );
294 #endif
295
296     if(p_vout->p_sys->p_VideoWidget)
297     {
298 // shameless borrowed from opie mediaplayer....
299 #ifndef USE_DIRECT_PAINTER
300         msg_Dbg(p_vout, "Not using direct painter");
301         QPainter p(p_vout->p_sys->p_VideoWidget);
302
303         /* rotate frame */
304         int dd      = QPixmap::defaultDepth();
305         int bytes   = ( dd == 16 ) ? 2 : 4;
306         int rw = h, rh = w;
307
308         QImage rotatedFrame( rw, rh, bytes << 3 );
309
310         ushort* in  = (ushort*)p_pic->p_sys->pQImage->bits();
311         ushort* out = (ushort*)rotatedFrame.bits();
312
313         int spl = rotatedFrame.bytesPerLine() / bytes;
314         for (int x=0; x<h; x++)
315         {
316             if ( bytes == 2 )
317             {
318                 ushort* lout = out++ + (w - 1)*spl;
319                 for (int y=0; y<w; y++)
320                 {
321                     *lout=*in++;
322                     lout-=spl;
323                 }
324             }
325             else
326             {
327                 ulong* lout = ((ulong *)out)++ + (w - 1)*spl;
328                 for (int y=0; y<w; y++)
329                 {
330                     *lout=*((ulong*)in)++;
331                     lout-=spl;
332                 }
333             }
334         }
335
336         p.drawImage( x, y, rotatedFrame, 0, 0, rw, rh );
337 #else
338         QDirectPainter p(p_vout->p_sys->p_VideoWidget);
339         p.transformOrientation();
340         // just copy the image to the frame buffer...
341         memcpy(p.frameBuffer(), (p_pic->p_sys->pQImage->jumpTable())[0], h * p.lineStep());
342 #endif
343     }
344 }
345
346 /*****************************************************************************
347  * Manage: handle Qte events
348  *****************************************************************************
349  * This function should be called regularly by video output thread. It manages
350  * Qte events and allows window resizing. It returns a non null value on
351  * error.
352  *****************************************************************************/
353 static int Manage( vout_thread_t *p_vout )
354 {
355     msg_Dbg( p_vout, "Manage" );
356
357     /* Fullscreen change */
358     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
359     {
360         p_vout->b_fullscreen = ! p_vout->b_fullscreen;
361
362 //        p_vout->p_sys->b_cursor_autohidden = 0;
363 //        SDL_ShowCursor( p_vout->p_sys->b_cursor &&
364 //                        ! p_vout->p_sys->b_cursor_autohidden );
365
366         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
367         p_vout->i_changes |= VOUT_SIZE_CHANGE;
368     }
369
370     /*
371      * Size change
372      */
373     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
374     {
375         msg_Dbg( p_vout, "video display resized (%dx%d)",
376                  p_vout->p_sys->i_width, p_vout->p_sys->i_height );
377
378         CloseDisplay( p_vout );
379         OpenDisplay( p_vout );
380
381         /* We don't need to signal the vout thread about the size change if
382          * we can handle rescaling ourselves */
383         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
384     }
385
386     /* Pointer change */
387 //    if( ! p_vout->p_sys->b_cursor_autohidden &&
388 //        ( mdate() - p_vout->p_sys->i_lastmoved > 2000000 ) )
389 //    {
390 //        /* Hide the mouse automatically */
391 //        p_vout->p_sys->b_cursor_autohidden = 1;
392 //        SDL_ShowCursor( 0 );
393 //    }
394
395     return 0;
396 }
397
398 /*****************************************************************************
399  * End: terminate video thread output method
400  *****************************************************************************
401  * Destroy the buffers created by vout_Init. It is called at the end of
402  * the thread, but also each time the window is resized.
403  *****************************************************************************/
404 static void End( vout_thread_t *p_vout )
405 {
406     int i_index;
407
408     /* Free the direct buffers we allocated */
409     for( i_index = I_OUTPUTPICTURES ; i_index ; )
410     {
411         i_index--;
412         FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
413     }
414 }
415
416
417 /*****************************************************************************
418  * NewPicture: allocate a picture
419  *****************************************************************************
420  * Returns 0 on success, -1 otherwise
421  *****************************************************************************/
422 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
423 {
424     int dd = QPixmap::defaultDepth();
425
426     p_pic->p_sys = (picture_sys_t*) malloc( sizeof( picture_sys_t ) );
427     if( p_pic->p_sys == NULL )
428     {
429         return -1;
430     }
431
432     /* Create the display */
433     p_pic->p_sys->pQImage = new QImage(p_vout->output.i_width,
434                                        p_vout->output.i_height, dd );
435
436     if(p_pic->p_sys->pQImage == NULL)
437     {
438         return -1;
439     }
440
441     switch( dd )
442     {
443         case 8:
444             p_pic->p->i_pixel_pitch = 1;
445             break;
446         case 15:
447         case 16:
448             p_pic->p->i_pixel_pitch = 2;
449             break;
450         case 24:
451         case 32:
452             p_pic->p->i_pixel_pitch = 4;
453             break;
454         default:
455             return( -1 );
456     }
457
458     p_pic->p->p_pixels = (p_pic->p_sys->pQImage->jumpTable())[0];
459     p_pic->p->i_pitch = p_pic->p_sys->pQImage->bytesPerLine();
460     p_pic->p->i_lines = p_vout->output.i_height;
461     p_pic->p->i_visible_pitch =
462             p_pic->p->i_pixel_pitch * p_vout->output.i_width;
463
464     p_pic->i_planes = 1;
465
466     return 0;
467 }
468
469 /*****************************************************************************
470  * FreePicture: destroy a picture allocated with NewPicture
471  *****************************************************************************/
472 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
473 {
474     delete p_pic->p_sys->pQImage;
475 }
476
477 /*****************************************************************************
478  * ToggleFullScreen: Enable or disable full screen mode
479  *****************************************************************************
480  * This function will switch between fullscreen and window mode.
481  *
482  *****************************************************************************/
483 static void ToggleFullScreen ( vout_thread_t *p_vout )
484 {
485     if ( p_vout->b_fullscreen )
486        p_vout->p_sys->p_VideoWidget->showFullScreen();
487     else
488       p_vout->p_sys->p_VideoWidget->showNormal();
489
490     p_vout->b_fullscreen = !p_vout->b_fullscreen;
491 }
492
493 /*****************************************************************************
494  * OpenDisplay: create qte applicaton / window
495  *****************************************************************************
496  * Create a window according to video output given size, and set other
497  * properties according to the display properties.
498  *****************************************************************************/
499 static int OpenDisplay( vout_thread_t *p_vout )
500 {
501     /* for displaying the vout in a qt window we need the QtApplication */
502     p_vout->p_sys->p_QApplication = NULL;
503     p_vout->p_sys->p_VideoWidget = NULL;
504
505     p_vout->p_sys->p_event = (event_thread_t*) vlc_object_create( p_vout, sizeof(event_thread_t) );
506     p_vout->p_sys->p_event->p_vout = p_vout;
507
508     /* Initializations */
509 #if 1 /* FIXME: I need an event queue to handle video output size changes. */
510     p_vout->b_fullscreen = VLC_TRUE;
511 #endif
512
513     /* Set main window's size */
514     QWidget *desktop = p_vout->p_sys->p_QApplication->desktop();
515     p_vout->p_sys->i_width = p_vout->b_fullscreen ? desktop->height() :
516                                                     p_vout->i_window_width;
517     p_vout->p_sys->i_height = p_vout->b_fullscreen ? desktop->width() :
518                                                      p_vout->i_window_height;
519
520 #if 0 /* FIXME: I need an event queue to handle video output size changes. */
521     /* Update dimensions */
522     p_vout->i_changes |= VOUT_SIZE_CHANGE;
523     p_vout->i_window_width = p_vout->p_sys->i_width;
524     p_vout->i_window_height = p_vout->p_sys->i_height;
525 #endif
526
527     msg_Dbg( p_vout, "OpenDisplay (h=%d,w=%d)",p_vout->p_sys->i_height,p_vout->p_sys->i_width);
528
529     /* create thread to exec the qpe application */
530     if ( vlc_thread_create( p_vout->p_sys->p_event, "QT Embedded Thread",
531                             RunQtThread,
532                             VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE) )
533     {
534         msg_Err( p_vout, "cannot create QT Embedded Thread" );
535         vlc_object_destroy( p_vout->p_sys->p_event );
536         p_vout->p_sys->p_event = NULL;
537         return -1;
538     }
539
540     if( p_vout->p_sys->p_event->b_error )
541     {
542         msg_Err( p_vout, "RunQtThread failed" );
543         return -1;
544     }
545
546     vlc_object_attach( p_vout->p_sys->p_event, p_vout );
547     msg_Dbg( p_vout, "RunQtThread running" );
548
549     // just wait until the crew is complete...
550     while(p_vout->p_sys->p_VideoWidget == NULL)
551     {
552         msleep(1);
553     }
554     return VLC_SUCCESS;
555 }
556
557
558 /*****************************************************************************
559  * CloseDisplay: destroy the window
560  *****************************************************************************/
561 static void CloseDisplay( vout_thread_t *p_vout )
562 {
563     // quit qt application loop
564     msg_Dbg( p_vout, "Destroying Qt Window" );
565 #ifdef NEED_QTE_MAIN
566     if(p_vout->p_sys->p_QApplication)
567     {
568         p_vout->p_sys->bRunning = FALSE;
569         while(p_vout->p_sys->p_VideoWidget)
570         {
571             msleep(1);
572         }
573     }
574 #else
575     if (p_vout->p_sys->p_QApplication)
576        p_vout->p_sys->p_QApplication->quit();
577 #endif
578 }
579
580 /*****************************************************************************
581  * main loop of qtapplication
582  *****************************************************************************/
583 static void RunQtThread(event_thread_t *p_event)
584 {
585     msg_Dbg( p_event->p_vout, "RunQtThread Starting" );
586
587 #ifdef NEED_QTE_MAIN
588     if (qApp)
589     {
590         p_event->p_vout->p_sys->p_QApplication = qApp;
591         p_event->p_vout->p_sys->bOwnsQApp = FALSE;
592         p_event->p_vout->p_sys->p_VideoWidget = qApp->mainWidget();
593         msg_Dbg( p_event->p_vout, "RunQtThread applicaton attached" );
594     }
595 #else
596     if (qApp==NULL)
597     {
598         int argc = 0;
599         QApplication* pApp = new QApplication(argc, NULL);
600         if(pApp)
601         {
602             p_event->p_vout->p_sys->p_QApplication = pApp;
603             p_event->p_vout->p_sys->bOwnsQApp = TRUE;
604         }
605         QWidget* pWidget = new QWidget();
606         if (pWidget)
607         {
608             p_event->p_vout->p_sys->p_VideoWidget = pWidget;
609         }
610     }
611 #endif
612     /* signal the creation of the window */
613     vlc_thread_ready( p_event );
614     msg_Dbg( p_event->p_vout, "RunQtThread ready" );
615
616     if (p_event->p_vout->p_sys->p_QApplication)
617     {
618         /* Set default window width and heigh to exactly preferred size. */
619         QWidget *desktop = p_event->p_vout->p_sys->p_QApplication->desktop();
620         p_event->p_vout->p_sys->p_VideoWidget->setMinimumWidth( 10 );
621         p_event->p_vout->p_sys->p_VideoWidget->setMinimumHeight( 10 );
622         p_event->p_vout->p_sys->p_VideoWidget->setBaseSize( p_event->p_vout->p_sys->i_width,
623         p_event->p_vout->p_sys->i_height );
624         p_event->p_vout->p_sys->p_VideoWidget->setMaximumWidth( desktop->width() );
625         p_event->p_vout->p_sys->p_VideoWidget->setMaximumHeight( desktop->height() );
626         /* Check on fullscreen */
627         if (p_event->p_vout->b_fullscreen)
628             p_event->p_vout->p_sys->p_VideoWidget->showFullScreen();
629         else
630            p_event->p_vout->p_sys->p_VideoWidget->showNormal();
631
632         p_event->p_vout->p_sys->p_VideoWidget->show();
633         p_event->p_vout->p_sys->bRunning = TRUE;
634
635 #ifdef NEED_QTE_MAIN
636         while(!p_event->b_die && p_event->p_vout->p_sys->bRunning)
637         {
638            /* Check if we are asked to exit */
639            if( p_event->b_die )
640                break;
641
642            msleep(100);
643         }
644 #else
645         // run the main loop of qtapplication until someone says: 'quit'
646         p_event->p_vout->p_sys->pcQApplication->exec();
647 #endif
648     }
649
650 #ifndef NEED_QTE_MAIN
651     if(p_event->p_vout->p_sys->p_QApplication)
652     {
653         delete p_event->p_vout->p_sys->p_VideoWidget;
654         p_event->p_vout->p_sys->p_VideoWidget = NULL;
655         delete p_event->p_vout->p_sys->p_QApplication;
656         p_event->p_vout->p_sys->p_QApplication = NULL;
657     }
658 #else
659     p_event->p_vout->p_sys->p_VideoWidget = NULL;
660 #endif
661
662     msg_Dbg( p_event->p_vout, "RunQtThread terminating" );
663 }
664