- fixed colors' bug (TODO: write a function in vout that recalculate colors
[vlc.git] / plugins / sdl / intf_sdl.c
1 /*****************************************************************************
2  * intf_sdl.c: SDL interface plugin
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: intf_sdl.c,v 1.21 2001/01/08 22:42:50 bozo Exp $
6  *
7  * Authors:
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include "defs.h"
28
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <SDL/SDL.h>                                /* for all the SDL stuff */
33
34 #include "config.h"
35 #include "common.h"
36 #include "threads.h"
37 #include "mtime.h"
38 #include "plugins.h"
39
40 #include "stream_control.h"
41 #include "input_ext-intf.h"
42
43 #include "video.h"
44 #include "video_output.h"
45
46
47 #include "interface.h"
48 #include "intf_msg.h"
49 #include "keystrokes.h"
50
51 #include "main.h"
52
53 typedef struct vout_sys_s
54 {
55     int i_width;
56     int i_height;
57     SDL_Surface *   p_display;                             /* display device */
58     SDL_Overlay *   p_overlay;
59     boolean_t   b_fullscreen;
60     boolean_t   b_reopen_display;
61     boolean_t   b_toggle_fullscreen;
62     Uint8   *   p_buffer[2];
63                                                      /* Buffers informations */
64 }   vout_sys_t;
65
66
67 /* local prototype */
68 void intf_SDL_Keymap( intf_thread_t * p_intf );
69 void intf_SDL_Resize( intf_thread_t * p_intf, int width, int height );
70 void intf_SDL_Fullscreen(intf_thread_t * p_intf);
71 void intf_SDL_YUVSwitch(intf_thread_t * p_intf);
72   
73
74 /*****************************************************************************
75  * intf_SDLCreate: initialize and create SDL interface
76  *****************************************************************************/
77 int intf_SDLCreate( intf_thread_t *p_intf )
78 {
79     /* Check that b_video is set */
80     if( !p_main->b_video )
81     {
82         intf_ErrMsg( "error: SDL interface requires a video output thread" );
83         return( 1 );
84     }
85
86     /* Spawn video output thread */
87     p_intf->p_vout = vout_CreateThread( main_GetPszVariable( VOUT_DISPLAY_VAR,
88                                                              NULL), 0,
89                                         main_GetIntVariable( VOUT_WIDTH_VAR,
90                                                          VOUT_WIDTH_DEFAULT ),
91                                         main_GetIntVariable( VOUT_HEIGHT_VAR,
92                                                         VOUT_HEIGHT_DEFAULT ),
93                                         NULL, 0, NULL );
94
95     if( p_intf->p_vout == NULL )                                  /* error */
96     {
97         intf_ErrMsg( "error: can't create video output thread" );
98         free( p_intf->p_sys );
99         return( 1 );
100     }
101     intf_SDL_Keymap( p_intf );
102     return( 0 );
103 }
104
105 /*****************************************************************************
106  * intf_SDLDestroy: destroy interface
107  *****************************************************************************/
108 void intf_SDLDestroy( intf_thread_t *p_intf )
109 {
110     /* Close input thread, if any (blocking) */
111     if( p_intf->p_input )
112     {
113         input_DestroyThread( p_intf->p_input, NULL );
114     }
115
116     /* Close video output thread, if any (blocking) */
117     if( p_intf->p_vout )
118     {
119         vout_DestroyThread( p_intf->p_vout, NULL );
120     }
121 }
122
123
124 /*****************************************************************************
125  * intf_SDLManage: event loop
126  *****************************************************************************/
127 void intf_SDLManage( intf_thread_t *p_intf )
128 {
129     SDL_Event event;                                            /* SDL event */
130     Uint8   i_key;
131     
132     while ( SDL_PollEvent(&event) )
133     {
134         i_key = event.key.keysym.sym;                          /* forward it */
135
136         switch (event.type) {
137             case SDL_VIDEORESIZE:                      /* Resizing of window */
138                 intf_SDL_Resize( p_intf, event.resize.w, event.resize.h );
139                 break;
140             case SDL_KEYDOWN:                         /* if a key is pressed */
141                 switch(i_key) {
142                                                     /* switch to fullscreen  */
143                     case SDLK_f:
144                         intf_SDL_Fullscreen(p_intf);
145                         break;
146                     case SDLK_y:
147                         intf_SDL_YUVSwitch(p_intf);
148                         break; 
149                   default :
150                         if( intf_ProcessKey( p_intf, (char ) i_key ) )
151                         {
152                             intf_DbgMsg( "unhandled key '%c' (%i)",
153                                          (char) i_key, i_key );
154                         }
155                         break;
156                 }
157                 break;
158             case SDL_QUIT:
159                 intf_ProcessKey( p_intf, INTF_KEY_QUIT ); 
160                 break;
161            default:
162                 break;
163         }
164     }
165 }
166
167 void intf_SDL_Resize( intf_thread_t * p_intf, int width, int height )
168 {
169     intf_Msg( "Video display resized (%dx%d)", width, height ); 
170     vlc_mutex_lock( &p_intf->p_vout->change_lock );
171     p_intf->p_vout->p_sys->i_width = width;
172     p_intf->p_vout->p_sys->i_height = height;
173     p_intf->p_vout->p_sys->b_reopen_display = 1;
174     vlc_mutex_unlock( &p_intf->p_vout->change_lock );
175 }
176
177 void intf_SDL_YUVSwitch(intf_thread_t * p_intf)
178 {
179     vlc_mutex_lock( &p_intf->p_vout->change_lock );
180     p_intf->p_vout->b_need_render = 1 - p_intf->p_vout->b_need_render;
181     intf_DbgMsg( "need render now : '%d'",p_intf->p_vout->b_need_render); 
182     p_intf->p_vout->p_sys->b_reopen_display = 1;
183     vlc_mutex_unlock( &p_intf->p_vout->change_lock );
184 }
185 void intf_SDL_Fullscreen(intf_thread_t * p_intf)
186 {
187     vlc_mutex_lock( &p_intf->p_vout->change_lock );
188     p_intf->p_vout->p_sys->b_fullscreen = 1-p_intf->p_vout->p_sys->b_fullscreen;
189     p_intf->p_vout->p_sys->b_toggle_fullscreen = 1;
190     vlc_mutex_unlock( &p_intf->p_vout->change_lock );
191
192     
193
194
195
196 void intf_SDL_Keymap(intf_thread_t * p_intf )
197 {
198     /* p_intf->p_intf_getKey = intf_getKey; */
199     intf_AssignKey(p_intf, SDLK_q,      INTF_KEY_QUIT, 0);
200     intf_AssignKey(p_intf, SDLK_ESCAPE, INTF_KEY_QUIT, 0);
201     /* intf_AssignKey(p_intf,3,'Q'); */
202     intf_AssignKey(p_intf, SDLK_0,      INTF_KEY_SET_CHANNEL,0);
203     intf_AssignKey(p_intf, SDLK_1,      INTF_KEY_SET_CHANNEL,1);
204     intf_AssignKey(p_intf, SDLK_2,      INTF_KEY_SET_CHANNEL,2);
205     intf_AssignKey(p_intf, SDLK_3,      INTF_KEY_SET_CHANNEL,3);
206     intf_AssignKey(p_intf, SDLK_4,      INTF_KEY_SET_CHANNEL,4);
207     intf_AssignKey(p_intf, SDLK_5,      INTF_KEY_SET_CHANNEL,5);
208     intf_AssignKey(p_intf, SDLK_6,      INTF_KEY_SET_CHANNEL,6);
209     intf_AssignKey(p_intf, SDLK_7,      INTF_KEY_SET_CHANNEL,7);
210     intf_AssignKey(p_intf, SDLK_8,      INTF_KEY_SET_CHANNEL,8);
211     intf_AssignKey(p_intf, SDLK_9,      INTF_KEY_SET_CHANNEL,9);
212     intf_AssignKey(p_intf, SDLK_PLUS,   INTF_KEY_INC_VOLUME, 0);
213     intf_AssignKey(p_intf, SDLK_MINUS,  INTF_KEY_DEC_VOLUME, 0);
214     intf_AssignKey(p_intf, SDLK_m,      INTF_KEY_TOGGLE_VOLUME, 0);
215     /* intf_AssignKey(p_intf,'M','M'); */
216     intf_AssignKey(p_intf, SDLK_g,      INTF_KEY_DEC_GAMMA, 0);
217     /* intf_AssignKey(p_intf,'G','G'); */
218     intf_AssignKey(p_intf, SDLK_c,      INTF_KEY_TOGGLE_GRAYSCALE, 0);
219     intf_AssignKey(p_intf, SDLK_SPACE,  INTF_KEY_TOGGLE_INTERFACE, 0);
220     intf_AssignKey(p_intf, SDLK_i,         INTF_KEY_TOGGLE_INFO, 0);
221     intf_AssignKey(p_intf, SDLK_s,      INTF_KEY_TOGGLE_SCALING, 0);
222
223 }
224