]> git.sesse.net Git - vlc/blob - modules/video_output/qte/qte.cpp
* Coding style cleanup: removed tabs and trailing spaces.
[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.20 2003/12/22 14:32:56 sam 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 //    if( p_vout->p_vlc->b_die )
396 //        p_vout->p_sys->bRunning = FALSE;
397
398     return 0;
399 }
400
401 /*****************************************************************************
402  * End: terminate video thread output method
403  *****************************************************************************
404  * Destroy the buffers created by vout_Init. It is called at the end of
405  * the thread, but also each time the window is resized.
406  *****************************************************************************/
407 static void End( vout_thread_t *p_vout )
408 {
409     int i_index;
410
411     /* Free the direct buffers we allocated */
412     for( i_index = I_OUTPUTPICTURES ; i_index ; )
413     {
414         i_index--;
415         FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
416     }
417 }
418
419
420 /*****************************************************************************
421  * NewPicture: allocate a picture
422  *****************************************************************************
423  * Returns 0 on success, -1 otherwise
424  *****************************************************************************/
425 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
426 {
427     int dd = QPixmap::defaultDepth();
428
429     p_pic->p_sys = (picture_sys_t*) malloc( sizeof( picture_sys_t ) );
430     if( p_pic->p_sys == NULL )
431     {
432         return -1;
433     }
434
435     /* Create the display */
436     p_pic->p_sys->pQImage = new QImage(p_vout->output.i_width,
437                                        p_vout->output.i_height, dd );
438
439     if(p_pic->p_sys->pQImage == NULL)
440     {
441         return -1;
442     }
443
444     switch( dd )
445     {
446         case 8:
447             p_pic->p->i_pixel_pitch = 1;
448             break;
449         case 15:
450         case 16:
451             p_pic->p->i_pixel_pitch = 2;
452             break;
453         case 24:
454         case 32:
455             p_pic->p->i_pixel_pitch = 4;
456             break;
457         default:
458             return( -1 );
459     }
460
461     p_pic->p->p_pixels = (p_pic->p_sys->pQImage->jumpTable())[0];
462     p_pic->p->i_pitch = p_pic->p_sys->pQImage->bytesPerLine();
463     p_pic->p->i_lines = p_vout->output.i_height;
464     p_pic->p->i_visible_pitch =
465             p_pic->p->i_pixel_pitch * p_vout->output.i_width;
466
467     p_pic->i_planes = 1;
468
469     return 0;
470 }
471
472 /*****************************************************************************
473  * FreePicture: destroy a picture allocated with NewPicture
474  *****************************************************************************/
475 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
476 {
477     delete p_pic->p_sys->pQImage;
478 }
479
480 /*****************************************************************************
481  * ToggleFullScreen: Enable or disable full screen mode
482  *****************************************************************************
483  * This function will switch between fullscreen and window mode.
484  *
485  *****************************************************************************/
486 static void ToggleFullScreen ( vout_thread_t *p_vout )
487 {
488     if ( p_vout->b_fullscreen )
489        p_vout->p_sys->p_VideoWidget->showFullScreen();
490     else
491        p_vout->p_sys->p_VideoWidget->showNormal();
492
493     p_vout->b_fullscreen = !p_vout->b_fullscreen;
494 }
495
496 /*****************************************************************************
497  * OpenDisplay: create qte applicaton / window
498  *****************************************************************************
499  * Create a window according to video output given size, and set other
500  * properties according to the display properties.
501  *****************************************************************************/
502 static int OpenDisplay( vout_thread_t *p_vout )
503 {
504     /* for displaying the vout in a qt window we need the QtApplication */
505     p_vout->p_sys->p_QApplication = NULL;
506     p_vout->p_sys->p_VideoWidget = NULL;
507
508     p_vout->p_sys->p_event = (event_thread_t*) vlc_object_create( p_vout, sizeof(event_thread_t) );
509     p_vout->p_sys->p_event->p_vout = p_vout;
510
511     /* Initializations */
512 #if 1 /* FIXME: I need an event queue to handle video output size changes. */
513     p_vout->b_fullscreen = VLC_TRUE;
514 #endif
515
516     /* Set main window's size */
517     QWidget *desktop = p_vout->p_sys->p_QApplication->desktop();
518     p_vout->p_sys->i_width = p_vout->b_fullscreen ? desktop->height() :
519                                                     p_vout->i_window_width;
520     p_vout->p_sys->i_height = p_vout->b_fullscreen ? desktop->width() :
521                                                      p_vout->i_window_height;
522
523 #if 0 /* FIXME: I need an event queue to handle video output size changes. */
524     /* Update dimensions */
525     p_vout->i_changes |= VOUT_SIZE_CHANGE;
526     p_vout->i_window_width = p_vout->p_sys->i_width;
527     p_vout->i_window_height = p_vout->p_sys->i_height;
528 #endif
529
530     msg_Dbg( p_vout, "OpenDisplay (h=%d,w=%d)",p_vout->p_sys->i_height,p_vout->p_sys->i_width);
531
532     /* create thread to exec the qpe application */
533     if ( vlc_thread_create( p_vout->p_sys->p_event, "QT Embedded Thread",
534                             RunQtThread,
535                             VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE) )
536     {
537         msg_Err( p_vout, "cannot create QT Embedded Thread" );
538         vlc_object_destroy( p_vout->p_sys->p_event );
539         p_vout->p_sys->p_event = NULL;
540         return -1;
541     }
542
543     if( p_vout->p_sys->p_event->b_error )
544     {
545         msg_Err( p_vout, "RunQtThread failed" );
546         return -1;
547     }
548
549     vlc_object_attach( p_vout->p_sys->p_event, p_vout );
550     msg_Dbg( p_vout, "RunQtThread running" );
551
552     // just wait until the crew is complete...
553     while(p_vout->p_sys->p_VideoWidget == NULL)
554     {
555         msleep(1);
556     }
557     return VLC_SUCCESS;
558 }
559
560
561 /*****************************************************************************
562  * CloseDisplay: destroy the window
563  *****************************************************************************/
564 static void CloseDisplay( vout_thread_t *p_vout )
565 {
566     // quit qt application loop
567     msg_Dbg( p_vout, "Destroying Qt Window" );
568 #ifdef NEED_QTE_MAIN
569     if(p_vout->p_sys->p_QApplication)
570     {
571         p_vout->p_sys->bRunning = FALSE;
572         while(p_vout->p_sys->p_VideoWidget)
573         {
574             msleep(1);
575         }
576     }
577 #else
578     if (p_vout->p_sys->p_QApplication)
579        p_vout->p_sys->p_QApplication->quit();
580 #endif
581 }
582
583 /*****************************************************************************
584  * main loop of qtapplication
585  *****************************************************************************/
586 static void RunQtThread(event_thread_t *p_event)
587 {
588     msg_Dbg( p_event->p_vout, "RunQtThread Starting" );
589
590 #ifdef NEED_QTE_MAIN
591     if (qApp)
592     {
593         p_event->p_vout->p_sys->p_QApplication = qApp;
594         p_event->p_vout->p_sys->bOwnsQApp = FALSE;
595         p_event->p_vout->p_sys->p_VideoWidget = qApp->mainWidget();
596         msg_Dbg( p_event->p_vout, "RunQtThread applicaton attached" );
597     }
598 #else
599     if (qApp==NULL)
600     {
601         int argc = 0;
602         QApplication* pApp = new QApplication(argc, NULL);
603         if(pApp)
604         {
605             p_event->p_vout->p_sys->p_QApplication = pApp;
606             p_event->p_vout->p_sys->bOwnsQApp = TRUE;
607         }
608         QWidget* pWidget = new QWidget();
609         if (pWidget)
610             {
611             p_event->p_vout->p_sys->p_VideoWidget = pWidget;
612         }
613     }
614 #endif
615     /* signal the creation of the window */
616     vlc_thread_ready( p_event );
617     msg_Dbg( p_event->p_vout, "RunQtThread ready" );
618
619     if (p_event->p_vout->p_sys->p_QApplication)
620     {
621         /* Set default window width and heigh to exactly preferred size. */
622             QWidget *desktop = p_event->p_vout->p_sys->p_QApplication->desktop();
623             p_event->p_vout->p_sys->p_VideoWidget->setMinimumWidth( 10 );
624              p_event->p_vout->p_sys->p_VideoWidget->setMinimumHeight( 10 );
625             p_event->p_vout->p_sys->p_VideoWidget->setBaseSize( p_event->p_vout->p_sys->i_width,
626             p_event->p_vout->p_sys->i_height );
627         p_event->p_vout->p_sys->p_VideoWidget->setMaximumWidth( desktop->width() );
628         p_event->p_vout->p_sys->p_VideoWidget->setMaximumHeight( desktop->height() );
629         /* Check on fullscreen */
630         if (p_event->p_vout->b_fullscreen)
631                   p_event->p_vout->p_sys->p_VideoWidget->showFullScreen();
632         else
633                 p_event->p_vout->p_sys->p_VideoWidget->showNormal();
634
635         p_event->p_vout->p_sys->p_VideoWidget->show();
636         p_event->p_vout->p_sys->bRunning = TRUE;
637
638 #ifdef NEED_QTE_MAIN
639         while(!p_event->b_die && p_event->p_vout->p_sys->bRunning)
640               {
641                /* Check if we are asked to exit */
642            if( p_event->b_die )
643                break;
644
645                msleep(100);
646             }
647 #else
648         // run the main loop of qtapplication until someone says: 'quit'
649         p_event->p_vout->p_sys->pcQApplication->exec();
650 #endif
651     }
652
653 #ifndef NEED_QTE_MAIN
654     if(p_event->p_vout->p_sys->p_QApplication)
655     {
656         delete p_event->p_vout->p_sys->p_VideoWidget;
657         p_event->p_vout->p_sys->p_VideoWidget = NULL;
658         delete p_event->p_vout->p_sys->p_QApplication;
659         p_event->p_vout->p_sys->p_QApplication = NULL;
660     }
661 #else
662     p_event->p_vout->p_sys->p_VideoWidget = NULL;
663 #endif
664
665     msg_Dbg( p_event->p_vout, "RunQtThread terminating" );
666 }
667