vout_sdl.c 25.2 KB
Newer Older
Pierre Baillet's avatar
Pierre Baillet committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*****************************************************************************
 * vout_sdl.c: SDL video output display method
 *****************************************************************************
 * Copyright (C) 1998, 1999, 2000 VideoLAN
 *
 * Authors:
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include "defs.h"

#include <errno.h>                                                 /* ENOMEM */
#include <stdlib.h>                                                /* free() */
#include <string.h>                                            /* strerror() */

#include <SDL/SDL.h>

#include "config.h"
#include "common.h"
#include "threads.h"
#include "mtime.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
38
39
40
41
42
43
#include "tests.h"
#include "modules.h"

/* FIXME: it's up to the _interface_ to do this, not the video output */
#include "stream_control.h"
#include "input_ext-intf.h"
Pierre Baillet's avatar
Pierre Baillet committed
44
45
46
47
48

#include "video.h"
#include "video_output.h"

#include "intf_msg.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
49
50
51
#include "interface.h"
/* FIXME: get rid of this */
#include "keystrokes.h"
52
#include "main.h"
Pierre Baillet's avatar
Pierre Baillet committed
53

Sam Hocevar's avatar
   
Sam Hocevar committed
54
55
56
/*****************************************************************************
 * FIXME: this file is ...                                                   *
 *                                                                           *
Sam Hocevar's avatar
   
Sam Hocevar committed
57
58
59
60
61
62
 *              XXX   XXX     FIXME     XXX     XXX   XXX   XXX              *
 *              XXX   XXX   XXX   XXX   XXX     XXX   XXX   XXX              *
 *              XXX   XXX   XXX         XXX       FIXME     XXX              *
 *              XXX   XXX   XXX  TODO   XXX        XXX      XXX              *
 *              XXX   XXX   XXX   XXX   XXX        XXX                       *
 *                FIXME       FIXME       FIXME    XXX      XXX              *
Sam Hocevar's avatar
   
Sam Hocevar committed
63
64
65
 *                                                                           *
 *****************************************************************************/

Pierre Baillet's avatar
Pierre Baillet committed
66
67
68
69
70
71
72
73
/*****************************************************************************
 * vout_sys_t: video output SDL method descriptor
 *****************************************************************************
 * This structure is part of the video output thread descriptor.
 * It describes the SDL specific properties of an output thread.
 *****************************************************************************/
typedef struct vout_sys_s
{
74
75
    int i_width;
    int i_height;
Pierre Baillet's avatar
Pierre Baillet committed
76
    SDL_Surface *   p_display;                             /* display device */
77
78
    SDL_Overlay *   p_overlay;                             /* overlay device */
    boolean_t   b_fullscreen;
Sam Hocevar's avatar
   
Sam Hocevar committed
79
    boolean_t   b_overlay;
80
    boolean_t   b_cursor;
81
    boolean_t   b_reopen_display;
Sam Hocevar's avatar
   
Sam Hocevar committed
82
    Uint8   *   p_sdl_buf[2];                           /* Buffer information */
Pierre Baillet's avatar
Pierre Baillet committed
83
84
85
}   vout_sys_t;

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
 * Local prototypes.
 *****************************************************************************/
static int  vout_Probe     ( probedata_t *p_data );
static int  vout_Create    ( struct vout_thread_s * );
static int  vout_Init      ( struct vout_thread_s * );
static void vout_End       ( struct vout_thread_s * );
static void vout_Destroy   ( struct vout_thread_s * );
static int  vout_Manage    ( struct vout_thread_s * );
static void vout_Display   ( struct vout_thread_s * );

static int  SDLOpenDisplay      ( vout_thread_t *p_vout );
static void SDLCloseDisplay     ( vout_thread_t *p_vout );

/*****************************************************************************
 * Functions exported as capabilities. They are declared as static so that
 * we don't pollute the namespace too much.
Pierre Baillet's avatar
Pierre Baillet committed
102
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
103
104
105
106
107
108
109
110
111
112
113
114
void vout_getfunctions( function_list_t * p_function_list )
{
    p_function_list->pf_probe = vout_Probe;
    p_function_list->functions.vout.pf_create     = vout_Create;
    p_function_list->functions.vout.pf_init       = vout_Init;
    p_function_list->functions.vout.pf_end        = vout_End;
    p_function_list->functions.vout.pf_destroy    = vout_Destroy;
    p_function_list->functions.vout.pf_manage     = vout_Manage;
    p_function_list->functions.vout.pf_display    = vout_Display;
    p_function_list->functions.vout.pf_setpalette = NULL;
}

Pierre Baillet's avatar
Pierre Baillet committed
115
/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
 * intf_Probe: probe the video driver and return a score
 *****************************************************************************
 * This function tries to initialize SDL and returns a score to the
 * plugin manager so that it can select the best plugin.
 *****************************************************************************/
static int vout_Probe( probedata_t *p_data )
{
    if( TestMethod( VOUT_METHOD_VAR, "sdl" ) )
    {
        return( 999 );
    }

    return( 40 );
}

/*****************************************************************************
 * vout_Create: allocate SDL video thread output method
Pierre Baillet's avatar
Pierre Baillet committed
133
134
135
136
137
 *****************************************************************************
 * This function allocate and initialize a SDL vout method. It uses some of the
 * vout properties to choose the correct mode, and change them according to the
 * mode actually used.
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
138
int vout_Create( vout_thread_t *p_vout )
Pierre Baillet's avatar
Pierre Baillet committed
139
140
141
142
143
{
    /* Allocate structure */
    p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
    if( p_vout->p_sys == NULL )
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
144
        intf_ErrMsg( "vout error: can't create p_sys (%s)", strerror(ENOMEM) );
Pierre Baillet's avatar
Pierre Baillet committed
145
146
147
        return( 1 );
    }

148
    /* Initialize library */
149
150
    if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD | SDL_INIT_NOPARACHUTE)
            < 0 )
Pierre Baillet's avatar
Pierre Baillet committed
151
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
152
        intf_ErrMsg( "vout error: can't initialize SDL (%s)", SDL_GetError() );
Pierre Baillet's avatar
Pierre Baillet committed
153
154
155
        free( p_vout->p_sys );
        return( 1 );
    }
156
157
158
159

    /* Force the software yuv even if it is not used */
    /* If we don't do this, p_vout is not correctly initialized
       and it's impossible to switch between soft/hard yuv */
Sam Hocevar's avatar
   
Sam Hocevar committed
160
    /* FIXME: this is a broken way to do !! fix this !! */
161
162
    p_vout->b_need_render = 1;

Sam Hocevar's avatar
   
Sam Hocevar committed
163
    p_vout->p_sys->b_cursor = 1; /* TODO should be done with a main_GetInt.. */
Sam Hocevar's avatar
   
Sam Hocevar committed
164
165
166
167
    p_vout->p_sys->b_fullscreen = main_GetIntVariable( VOUT_FULLSCREEN_VAR,
                                VOUT_FULLSCREEN_DEFAULT );
    p_vout->p_sys->b_overlay = main_GetIntVariable( VOUT_OVERLAY_VAR,
                                VOUT_OVERLAY_DEFAULT );
168
169
170
171
    p_vout->p_sys->i_width = main_GetIntVariable( VOUT_WIDTH_VAR, 
                                VOUT_WIDTH_DEFAULT );
    p_vout->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
                                VOUT_HEIGHT_DEFAULT );
172

Sam Hocevar's avatar
   
Sam Hocevar committed
173
174
175
    p_vout->p_sys->p_display = NULL;
    p_vout->p_sys->p_overlay = NULL;

176
177
    if( SDLOpenDisplay(p_vout) )
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
178
179
180
        intf_ErrMsg( "vout error: can't initialize SDL (%s)", SDL_GetError() );
        free( p_vout->p_sys );
        return( 1 );
181
182
    }

Sam Hocevar's avatar
   
Sam Hocevar committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
    /* FIXME: get rid of this ASAP, it's FUCKING UGLY */
    { intf_thread_t * p_intf = p_main->p_intf;
    /* p_intf->p_intf_getKey = intf_getKey; */
    intf_AssignKey(p_intf, SDLK_q,      INTF_KEY_QUIT, 0);
    intf_AssignKey(p_intf, SDLK_ESCAPE, INTF_KEY_QUIT, 0);
    /* intf_AssignKey(p_intf,3,'Q'); */
    intf_AssignKey(p_intf, SDLK_0,      INTF_KEY_SET_CHANNEL,0);
    intf_AssignKey(p_intf, SDLK_1,      INTF_KEY_SET_CHANNEL,1);
    intf_AssignKey(p_intf, SDLK_2,      INTF_KEY_SET_CHANNEL,2);
    intf_AssignKey(p_intf, SDLK_3,      INTF_KEY_SET_CHANNEL,3);
    intf_AssignKey(p_intf, SDLK_4,      INTF_KEY_SET_CHANNEL,4);
    intf_AssignKey(p_intf, SDLK_5,      INTF_KEY_SET_CHANNEL,5);
    intf_AssignKey(p_intf, SDLK_6,      INTF_KEY_SET_CHANNEL,6);
    intf_AssignKey(p_intf, SDLK_7,      INTF_KEY_SET_CHANNEL,7);
    intf_AssignKey(p_intf, SDLK_8,      INTF_KEY_SET_CHANNEL,8);
    intf_AssignKey(p_intf, SDLK_9,      INTF_KEY_SET_CHANNEL,9);
    intf_AssignKey(p_intf, SDLK_PLUS,   INTF_KEY_INC_VOLUME, 0);
    intf_AssignKey(p_intf, SDLK_MINUS,  INTF_KEY_DEC_VOLUME, 0);
    intf_AssignKey(p_intf, SDLK_m,      INTF_KEY_TOGGLE_VOLUME, 0);
    /* intf_AssignKey(p_intf,'M','M'); */
    intf_AssignKey(p_intf, SDLK_g,      INTF_KEY_DEC_GAMMA, 0);
    /* intf_AssignKey(p_intf,'G','G'); */
    intf_AssignKey(p_intf, SDLK_c,      INTF_KEY_TOGGLE_GRAYSCALE, 0);
    intf_AssignKey(p_intf, SDLK_SPACE,  INTF_KEY_TOGGLE_INTERFACE, 0);
    intf_AssignKey(p_intf, SDLK_i,      INTF_KEY_TOGGLE_INFO, 0);
    intf_AssignKey(p_intf, SDLK_s,      INTF_KEY_TOGGLE_SCALING, 0); }

Pierre Baillet's avatar
Pierre Baillet committed
210
    return( 0 );
211
}
Pierre Baillet's avatar
Pierre Baillet committed
212
213

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
214
 * vout_Init: initialize SDL video thread output method
Pierre Baillet's avatar
Pierre Baillet committed
215
216
217
 *****************************************************************************
 * This function initialize the SDL display device.
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
218
int vout_Init( vout_thread_t *p_vout )
Pierre Baillet's avatar
Pierre Baillet committed
219
220
221
222
223
{
    return( 0 );
}

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
224
 * vout_End: terminate Sys video thread output method
Pierre Baillet's avatar
Pierre Baillet committed
225
226
227
 *****************************************************************************
 * Terminate an output method created by vout_SDLCreate
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
228
void vout_End( vout_thread_t *p_vout )
Pierre Baillet's avatar
Pierre Baillet committed
229
{
230
231
    SDLCloseDisplay( p_vout );
    SDL_Quit();
Pierre Baillet's avatar
Pierre Baillet committed
232
233
234
}

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
235
 * vout_Destroy: destroy Sys video thread output method
Pierre Baillet's avatar
Pierre Baillet committed
236
237
238
 *****************************************************************************
 * Terminate an output method created by vout_SDLCreate
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
239
void vout_Destroy( vout_thread_t *p_vout )
Pierre Baillet's avatar
Pierre Baillet committed
240
241
242
243
244
{
    free( p_vout->p_sys );
}

/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
245
 * vout_Manage: handle Sys events
Pierre Baillet's avatar
Pierre Baillet committed
246
247
248
249
 *****************************************************************************
 * This function should be called regularly by video output thread. It returns
 * a non null value if an error occured.
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
250
int vout_Manage( vout_thread_t *p_vout )
Pierre Baillet's avatar
Pierre Baillet committed
251
{
Sam Hocevar's avatar
   
Sam Hocevar committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
    SDL_Event event;                                            /* SDL event */
    Uint8   i_key;

    /* FIXME: do this nicely */
    input_thread_t * p_input = p_main->p_intf->p_input;

    /* Process events */
    while( SDL_PollEvent(&event) )
    {
        switch( event.type )
        {
        case SDL_VIDEORESIZE:                          /* Resizing of window */
            p_vout->i_width = event.resize.w;
            p_vout->i_height = event.resize.h;
            p_vout->i_changes |= VOUT_SIZE_CHANGE;
            break;

        case SDL_MOUSEBUTTONDOWN:
            switch( event.button.button )
            {
            case SDL_BUTTON_MIDDLE:
                p_vout->i_changes |= VOUT_CURSOR_CHANGE;
                break;

            case SDL_BUTTON_RIGHT:
                p_main->p_intf->b_menu_change = 1;
                break;
            }
            break;

        case SDL_QUIT:
            intf_ProcessKey( p_main->p_intf, SDLK_q );
            break;

        case SDL_KEYDOWN:                             /* if a key is pressed */
            i_key = event.key.keysym.sym;

            switch( i_key )
            {
            case SDLK_f:                             /* switch to fullscreen */
                p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
                break;

            case SDLK_y:                               /* switch to hard YUV */
                p_vout->i_changes |= VOUT_YUV_CHANGE;
                break;

            case SDLK_c:                                 /* toggle grayscale */
                p_vout->b_grayscale = ! p_vout->b_grayscale;
               	p_vout->i_changes |= VOUT_GRAYSCALE_CHANGE;
                break;

            case SDLK_i:                                      /* toggle info */
                p_vout->b_info = ! p_vout->b_info;
               	p_vout->i_changes |= VOUT_INFO_CHANGE;
                break;

	    case SDLK_s:                                   /* toggle scaling */
                p_vout->b_scale = ! p_vout->b_scale;
               	p_vout->i_changes |= VOUT_SCALE_CHANGE;
                break;

	    case SDLK_SPACE:                             /* toggle interface */
                p_vout->b_interface = ! p_vout->b_interface;
               	p_vout->i_changes |= VOUT_INTF_CHANGE;
                break;

            /* FIXME : this is temporary */
            case SDLK_p:
Sam Hocevar's avatar
   
Sam Hocevar committed
321
                if( p_input != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
322
                {
Sam Hocevar's avatar
   
Sam Hocevar committed
323
                    input_SetRate( p_input, INPUT_RATE_PAUSE );
Sam Hocevar's avatar
   
Sam Hocevar committed
324
325
326
327
                }
                break;

            case SDLK_a:
Sam Hocevar's avatar
   
Sam Hocevar committed
328
                if( p_input != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
329
                {
Sam Hocevar's avatar
   
Sam Hocevar committed
330
                    input_SetRate( p_input, INPUT_RATE_FASTER );
Sam Hocevar's avatar
   
Sam Hocevar committed
331
332
333
334
                }
                break;

            case SDLK_z:
Sam Hocevar's avatar
   
Sam Hocevar committed
335
                if( p_input != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
336
                {
Sam Hocevar's avatar
   
Sam Hocevar committed
337
                    input_SetRate( p_input, INPUT_RATE_SLOWER );
Sam Hocevar's avatar
   
Sam Hocevar committed
338
339
340
341
                }
                break;

            case SDLK_j:
Sam Hocevar's avatar
   
Sam Hocevar committed
342
343
344
345
346
347
348
                if( p_input != NULL )
                {
                    /* Jump forwards */
                    input_Seek( p_input, p_input->stream.i_tell
                                 + p_input->stream.i_size / 20 );
                                                               /* gabuzomeu */
                }
Sam Hocevar's avatar
   
Sam Hocevar committed
349
350
351
                break;

            case SDLK_b:
Sam Hocevar's avatar
   
Sam Hocevar committed
352
353
354
355
356
357
                if( p_input != NULL )
                {
                    /* Jump backwards */
                    input_Seek( p_input, p_input->stream.i_tell
                                 - p_input->stream.i_size / 20 );
                }
Sam Hocevar's avatar
   
Sam Hocevar committed
358
359
360
361
362
363
364
365
366
367
368
369
370
371
                break;

            default:
                if( intf_ProcessKey( p_main->p_intf, (char )i_key ) )
                {
                   intf_DbgMsg( "unhandled key '%c' (%i)", (char)i_key, i_key );                }
                break;
            }
            break;

        default:
            break;
        }
    }
372
373
374
375

    /*
     * Size Change 
     */
Sam Hocevar's avatar
   
Sam Hocevar committed
376
    if( p_vout->i_changes & VOUT_SIZE_CHANGE )
377
    {
378
379
        p_vout->p_sys->i_width = p_vout->i_width;
        p_vout->p_sys->i_height = p_vout->i_height;
380

381
382
        /* Need to reopen display */
        SDLCloseDisplay( p_vout );
Sam Hocevar's avatar
   
Sam Hocevar committed
383
        if( SDLOpenDisplay( p_vout ) )
384
        {
385
386
          intf_ErrMsg( "error: can't open DISPLAY default display" );
          return( 1 );
387
        }
388
        p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
389
    }
390
391
392
393
    
    /*
     * YUV Change 
     */
Sam Hocevar's avatar
   
Sam Hocevar committed
394
    if( p_vout->i_changes & VOUT_YUV_CHANGE )
395
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
396
        p_vout->b_need_render = ! p_vout->b_need_render;
397
398
399
        
        /* Need to reopen display */
        SDLCloseDisplay( p_vout );
Sam Hocevar's avatar
   
Sam Hocevar committed
400
        if( SDLOpenDisplay( p_vout ) )
401
402
403
404
405
406
        {
          intf_ErrMsg( "error: can't open DISPLAY default display" );
          return( 1 );
        }
        p_vout->i_changes &= ~VOUT_YUV_CHANGE;
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
407

408
409
410
    /*
     * Fullscreen change
     */
Sam Hocevar's avatar
   
Sam Hocevar committed
411
    if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
412
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
        p_vout->p_sys->b_fullscreen = ! p_vout->p_sys->b_fullscreen;

        if( p_vout->p_sys->b_fullscreen )
        {
            p_vout->p_sys->b_fullscreen = 0;
            p_vout->p_sys->b_cursor = 1;
            SDL_ShowCursor( 1 );
        }
        else
        {
            p_vout->p_sys->b_fullscreen = 1;
            p_vout->p_sys->b_cursor = 0;
            SDL_ShowCursor( 0 );
        }

        SDL_WM_ToggleFullScreen(p_vout->p_sys->p_display);

430
        p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
431
    }
432
433
434
435

    /*
     * Pointer change
     */
Sam Hocevar's avatar
   
Sam Hocevar committed
436
    if( p_vout->i_changes & VOUT_CURSOR_CHANGE )
437
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
438
439
440
441
442
443
444
445
446
447
448
        if( p_vout->p_sys->b_cursor )
        {
            SDL_ShowCursor( 0 );
            p_vout->p_sys->b_cursor = 0;
        }
        else
        {
            SDL_ShowCursor( 1 );
            p_vout->p_sys->b_cursor = 1;
        }
        p_vout->i_changes &= ~VOUT_CURSOR_CHANGE;
449
450
    }
    
Pierre Baillet's avatar
Pierre Baillet committed
451
452
    return( 0 );
}
Sam Hocevar's avatar
   
Sam Hocevar committed
453

454
/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
455
 * vout_SetPalette: sets an 8 bpp palette
456
457
458
459
460
 *****************************************************************************
 * This function sets the palette given as an argument. It does not return
 * anything, but could later send information on which colors it was unable
 * to set.
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
461
void vout_SetPalette( p_vout_thread_t p_vout, u16 *red, u16 *green,
462
                         u16 *blue, u16 *transp)
463
464
465
466
467
468
{
     /* Create a display surface with a grayscale palette */
    SDL_Color colors[256];
    int i;
  
    /* Fill colors with color information */
Sam Hocevar's avatar
   
Sam Hocevar committed
469
470
471
472
473
    for( i = 0; i < 256; i++ )
    {
        colors[ i ].r = red[ i ] >> 8;
        colors[ i ].g = green[ i ] >> 8;
        colors[ i ].b = blue[ i ] >> 8;
474
475
476
    }
    
    /* Set palette */
Sam Hocevar's avatar
   
Sam Hocevar committed
477
    if( SDL_SetColors( p_vout->p_sys->p_display, colors, 0, 256 ) == 0 )
Sam Hocevar's avatar
   
Sam Hocevar committed
478
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
479
        intf_ErrMsg( "vout error: failed setting palette" );
Sam Hocevar's avatar
   
Sam Hocevar committed
480
    }
481
482
483

}

Pierre Baillet's avatar
Pierre Baillet committed
484
/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
485
 * vout_Display: displays previously rendered output
Pierre Baillet's avatar
Pierre Baillet committed
486
487
488
489
 *****************************************************************************
 * This function send the currently rendered image to the display, wait until
 * it is displayed and switch the two rendering buffer, preparing next frame.
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
490
void vout_Display( vout_thread_t *p_vout )
Pierre Baillet's avatar
Pierre Baillet committed
491
{
Pierre Baillet's avatar
Pierre Baillet committed
492
    SDL_Rect    disp;
493
    if((p_vout->p_sys->p_display != NULL) && !p_vout->p_sys->b_reopen_display)
494
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
495
        if( p_vout->b_need_render )
496
497
        {  
            /* Change display frame */
Pierre Baillet's avatar
Pierre Baillet committed
498
499
            SDL_Flip( p_vout->p_sys->p_display );
        }
500
        else
501
        {
502
503
504
505
506
        
            /*
             * p_vout->p_rendered_pic->p_y/u/v contains the YUV buffers to
             * render 
             */
507
508
            /* TODO: support for streams other than 4:2:0 */
            /* create the overlay if necessary */
509
            if( p_vout->p_sys->p_overlay == NULL )
510
511
512
            {
                p_vout->p_sys->p_overlay = SDL_CreateYUVOverlay( 
                                             p_vout->p_rendered_pic->i_width, 
513
                                             p_vout->p_rendered_pic->i_height,
514
515
516
                                             SDL_YV12_OVERLAY, 
                                             p_vout->p_sys->p_display
                                           );
Sam Hocevar's avatar
   
Sam Hocevar committed
517
518
519
                intf_Msg("vout: YUV acceleration %s",
                            p_vout->p_sys->p_overlay->hw_overlay
                            ? "activated" : "unavailable !" ); 
520
            }
521

522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
            SDL_LockYUVOverlay(p_vout->p_sys->p_overlay);
            /* copy the data into video buffers */
            /* Y first */
            memcpy(p_vout->p_sys->p_overlay->pixels[0],
                   p_vout->p_rendered_pic->p_y,
                   p_vout->p_sys->p_overlay->h *
                   p_vout->p_sys->p_overlay->pitches[0]);
            /* then V */
            memcpy(p_vout->p_sys->p_overlay->pixels[1],
                   p_vout->p_rendered_pic->p_v,
                   p_vout->p_sys->p_overlay->h *
                   p_vout->p_sys->p_overlay->pitches[1] / 2);
            /* and U */
            memcpy(p_vout->p_sys->p_overlay->pixels[2],
                   p_vout->p_rendered_pic->p_u,
                   p_vout->p_sys->p_overlay->h *
                   p_vout->p_sys->p_overlay->pitches[2] / 2);
539

540
541
542
543
            disp.w = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_width;
            disp.h = (&p_vout->p_buffer[p_vout->i_buffer_index])->i_pic_height;
            disp.x = (p_vout->i_width - disp.w)/2;
            disp.y = (p_vout->i_height - disp.h)/2;
544

545
546
547
548
            SDL_DisplayYUVOverlay( p_vout->p_sys->p_overlay , &disp );
            SDL_UnlockYUVOverlay(p_vout->p_sys->p_overlay);
        }
    }
Pierre Baillet's avatar
Pierre Baillet committed
549
550
551
552
553
554
555
556
557
558
}

/* following functions are local */

/*****************************************************************************
 * SDLOpenDisplay: open and initialize SDL device
 *****************************************************************************
 * Open and initialize display according to preferences specified in the vout
 * thread fields.
 *****************************************************************************/
559
static int SDLOpenDisplay( vout_thread_t *p_vout )
Pierre Baillet's avatar
Pierre Baillet committed
560
{
561
    SDL_Rect    clipping_rect;
562
563
    Uint32      flags;
    int bpp;
Pierre Baillet's avatar
Pierre Baillet committed
564
565
566
    /* Open display 
     * TODO: Check that we can request for a DOUBLEBUF HWSURFACE display
     */
567
568

    /* init flags and cursor */
569
    flags = SDL_ANYFORMAT | SDL_HWPALETTE;
570

571
    if( p_vout->p_sys->b_fullscreen )
572
573
574
575
576
577
578
579
580
581
582
583
584
585
        flags |= SDL_FULLSCREEN;
    else
        flags |= SDL_RESIZABLE;

    if( p_vout->b_need_render )
        flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
    else
        flags |= SDL_SWSURFACE; /* save video memory */

    bpp = SDL_VideoModeOK(p_vout->p_sys->i_width,
                          p_vout->p_sys->i_height,
                          p_vout->i_screen_depth, flags);

    if(bpp == 0)
Pierre Baillet's avatar
Pierre Baillet committed
586
    {
587
588
        intf_ErrMsg( "error: can't open DISPLAY default display" );
        return( 1 );
Pierre Baillet's avatar
Pierre Baillet committed
589
    }
590
591
592
593
594

    p_vout->p_sys->p_display = SDL_SetVideoMode(p_vout->p_sys->i_width,
                                                p_vout->p_sys->i_height,
                                                bpp, flags);

Pierre Baillet's avatar
Pierre Baillet committed
595
596
    if( p_vout->p_sys->p_display == NULL )
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
597
        intf_ErrMsg( "error: can't open DISPLAY default display" );
Pierre Baillet's avatar
Pierre Baillet committed
598
599
        return( 1 );
    }
600

601
602
    SDL_LockSurface(p_vout->p_sys->p_display);

603
604
605
606
    if( p_vout->p_sys->b_fullscreen )
        SDL_ShowCursor( 0 );
    else
        SDL_ShowCursor( 1 );
607

608
    SDL_WM_SetCaption( VOUT_TITLE , VOUT_TITLE );
Sam Hocevar's avatar
   
Sam Hocevar committed
609
    SDL_EventState(SDL_KEYUP , SDL_IGNORE);                /* ignore keys up */
610
    SDL_EventState(SDL_MOUSEBUTTONUP, SDL_IGNORE);          
Sam Hocevar's avatar
   
Sam Hocevar committed
611

612
    if( p_vout->b_need_render )
Pierre Baillet's avatar
Pierre Baillet committed
613
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
614
        p_vout->p_sys->p_sdl_buf[ 0 ] = p_vout->p_sys->p_display->pixels;
615
        SDL_Flip(p_vout->p_sys->p_display);
Sam Hocevar's avatar
   
Sam Hocevar committed
616
        p_vout->p_sys->p_sdl_buf[ 1 ] = p_vout->p_sys->p_display->pixels;
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
        SDL_Flip(p_vout->p_sys->p_display);

        /* Set clipping for text */
        clipping_rect.x = 0;
        clipping_rect.y = 0;
        clipping_rect.w = p_vout->p_sys->p_display->w;
        clipping_rect.h = p_vout->p_sys->p_display->h;
        SDL_SetClipRect(p_vout->p_sys->p_display, &clipping_rect);

        /* Set thread information */
        p_vout->i_width =           p_vout->p_sys->p_display->w;
        p_vout->i_height =          p_vout->p_sys->p_display->h;
        p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;

        p_vout->i_screen_depth =
            p_vout->p_sys->p_display->format->BitsPerPixel;
        p_vout->i_bytes_per_pixel =
            p_vout->p_sys->p_display->format->BytesPerPixel;

        p_vout->i_red_mask =        p_vout->p_sys->p_display->format->Rmask;
        p_vout->i_green_mask =      p_vout->p_sys->p_display->format->Gmask;
        p_vout->i_blue_mask =       p_vout->p_sys->p_display->format->Bmask;

        /* FIXME: palette in 8bpp ?? */
        /* Set and initialize buffers */
Sam Hocevar's avatar
   
Sam Hocevar committed
642
643
        vout_SetBuffers( p_vout, p_vout->p_sys->p_sdl_buf[ 0 ],
                                 p_vout->p_sys->p_sdl_buf[ 1 ] );
Pierre Baillet's avatar
Pierre Baillet committed
644
    }
645
646
    else
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
647
648
        p_vout->p_sys->p_sdl_buf[ 0 ] = p_vout->p_sys->p_display->pixels;
        p_vout->p_sys->p_sdl_buf[ 1 ] = p_vout->p_sys->p_display->pixels;
649
650
651
652
653
654

        /* Set thread information */
        p_vout->i_width =           p_vout->p_sys->p_display->w;
        p_vout->i_height =          p_vout->p_sys->p_display->h;
        p_vout->i_bytes_per_line =  p_vout->p_sys->p_display->pitch;

Sam Hocevar's avatar
   
Sam Hocevar committed
655
656
        vout_SetBuffers( p_vout, p_vout->p_sys->p_sdl_buf[ 0 ],
                                 p_vout->p_sys->p_sdl_buf[ 1 ] );
657
658
    }

659
    p_vout->p_sys->b_reopen_display = 0;
Pierre Baillet's avatar
Pierre Baillet committed
660
661
662
663
664
665
666
667
668
669
670
671

    return( 0 );
}

/*****************************************************************************
 * SDLCloseDisplay: close and reset SDL device
 *****************************************************************************
 * This function returns all resources allocated by SDLOpenDisplay and restore
 * the original state of the device.
 *****************************************************************************/
static void SDLCloseDisplay( vout_thread_t *p_vout )
{
Pierre Baillet's avatar
Pierre Baillet committed
672
    if( p_vout->p_sys->p_display != NULL )
673
    {
Pierre Baillet's avatar
Pierre Baillet committed
674
675
        if( p_vout->p_sys->p_overlay != NULL )
        {            
Sam Hocevar's avatar
   
Sam Hocevar committed
676
            SDL_FreeYUVOverlay( p_vout->p_sys->p_overlay );
677
678
            p_vout->p_sys->p_overlay = NULL;
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
679

680
        SDL_UnlockSurface ( p_vout->p_sys->p_display );
681
682
683
        SDL_FreeSurface( p_vout->p_sys->p_display );
        p_vout->p_sys->p_display = NULL;
    }
Pierre Baillet's avatar
Pierre Baillet committed
684
685
}