subreader: fix unsafe sscanf calls with "%["
[mplayer.git] / libvo / sdl_common.c
blob5b4c6845654fe23a8d0a7bafa904de1f9c2f24f0
1 /*
2 * common SDL routines
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "sdl_common.h"
22 #include "old_vo_defines.h"
23 #include "mp_msg.h"
24 #include "mp_fifo.h"
25 #include "input/keycodes.h"
26 #include "input/input.h"
27 #include "video_out.h"
28 #include "aspect.h"
30 static int old_w;
31 static int old_h;
32 static int mode_flags;
33 static int reinit;
34 static int screen_w, screen_h;
36 int vo_sdl_init(void)
38 reinit = 0;
40 if (!SDL_WasInit(SDL_INIT_VIDEO) &&
41 SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE) < 0)
42 return 0;
44 if (screen_w == 0) {
45 const SDL_VideoInfo *vi = SDL_GetVideoInfo();
46 screen_w = vi->current_w;
47 screen_h = vi->current_h;
50 // Setup Keyrepeats (500/30 are defaults)
51 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, 100 /*SDL_DEFAULT_REPEAT_INTERVAL*/);
53 // Easiest way to get uppercase characters
54 SDL_EnableUNICODE(1);
56 // We don't want those in our event queue.
57 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
58 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
59 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
61 return 1;
64 void vo_sdl_uninit(void)
66 if (SDL_WasInit(SDL_INIT_VIDEO))
67 SDL_QuitSubSystem(SDL_INIT_VIDEO);
70 void sdl_update_xinerama_info(void)
72 if (vo_screenwidth <= 0 || vo_screenheight <= 0) {
73 vo_screenwidth = screen_w;
74 vo_screenheight = screen_h;
76 aspect_save_screenres(vo_screenwidth, vo_screenheight);
79 void vo_sdl_fullscreen(void)
81 if (vo_fs) {
82 vo_dwidth = old_w;
83 vo_dheight = old_h;
84 } else {
85 old_w = vo_dwidth;
86 old_h = vo_dheight;
87 vo_dwidth = vo_screenwidth;
88 vo_dheight = vo_screenheight;
90 vo_fs = !vo_fs;
91 sdl_set_mode(0, mode_flags);
92 // on OSX at least we now need to do a full reinit.
93 // TODO: this should only be set if really necessary.
94 reinit = 1;
97 int sdl_set_mode(int bpp, uint32_t flags)
99 SDL_Surface *s;
100 mode_flags = flags;
101 if (vo_fs) flags |= SDL_FULLSCREEN;
102 // doublebuf with opengl creates flickering
103 if (vo_doublebuffering && !(flags & SDL_OPENGL))
104 flags |= SDL_DOUBLEBUF;
105 s = SDL_SetVideoMode(vo_dwidth, vo_dheight, bpp, flags);
106 if (!s) {
107 mp_msg(MSGT_VO, MSGL_FATAL, "SDL SetVideoMode failed: %s\n", SDL_GetError());
108 return -1;
110 vo_dwidth = s->w;
111 vo_dheight = s->h;
112 return 0;
115 static const struct mp_keymap keysym_map[] = {
116 {SDLK_RETURN, KEY_ENTER}, {SDLK_ESCAPE, KEY_ESC},
117 {SDLK_F1, KEY_F+1}, {SDLK_F2, KEY_F+2}, {SDLK_F3, KEY_F+3},
118 {SDLK_F4, KEY_F+4}, {SDLK_F5, KEY_F+5}, {SDLK_F6, KEY_F+6},
119 {SDLK_F7, KEY_F+7}, {SDLK_F8, KEY_F+8}, {SDLK_F9, KEY_F+9},
120 {SDLK_F10, KEY_F+10}, {SDLK_F11, KEY_F+11}, {SDLK_F12, KEY_F+12},
121 {SDLK_KP_PLUS, '+'}, {SDLK_KP_MINUS, '-'}, {SDLK_TAB, KEY_TAB},
122 {SDLK_PAGEUP, KEY_PAGE_UP}, {SDLK_PAGEDOWN, KEY_PAGE_DOWN},
123 {SDLK_UP, KEY_UP}, {SDLK_DOWN, KEY_DOWN},
124 {SDLK_LEFT, KEY_LEFT}, {SDLK_RIGHT, KEY_RIGHT},
125 {SDLK_KP_MULTIPLY, '*'}, {SDLK_KP_DIVIDE, '/'},
126 {SDLK_KP0, KEY_KP0}, {SDLK_KP1, KEY_KP1}, {SDLK_KP2, KEY_KP2},
127 {SDLK_KP3, KEY_KP3}, {SDLK_KP4, KEY_KP4}, {SDLK_KP5, KEY_KP5},
128 {SDLK_KP6, KEY_KP6}, {SDLK_KP7, KEY_KP7}, {SDLK_KP8, KEY_KP8},
129 {SDLK_KP9, KEY_KP9},
130 {SDLK_KP_PERIOD, KEY_KPDEC}, {SDLK_KP_ENTER, KEY_KPENTER},
131 {SDLK_BACKSPACE, KEY_BACKSPACE},
132 {SDLK_INSERT, KEY_INSERT}, {SDLK_DELETE, KEY_DEL},
133 {SDLK_HOME, KEY_HOME}, {SDLK_END, KEY_END},
134 {0, 0}
137 static int get_mod_state(void)
139 SDLMod mod = SDL_GetModState();
140 int modifiers = 0;
141 if (mod & KMOD_SHIFT)
142 modifiers |= KEY_MODIFIER_SHIFT;
143 if (mod & KMOD_CTRL)
144 modifiers |= KEY_MODIFIER_CTRL;
145 if (mod & KMOD_ALT)
146 modifiers |= KEY_MODIFIER_ALT;
147 if (mod & KMOD_META)
148 modifiers |= KEY_MODIFIER_META;
149 return modifiers;
152 int sdl_default_handle_event(SDL_Event *event)
154 int mpkey;
155 if (!event) {
156 int res = reinit ? VO_EVENT_REINIT : 0;
157 reinit = 0;
158 return res;
160 switch (event->type) {
161 case SDL_VIDEORESIZE:
162 vo_dwidth = event->resize.w;
163 vo_dheight = event->resize.h;
164 return VO_EVENT_RESIZE;
166 case SDL_VIDEOEXPOSE:
167 return VO_EVENT_EXPOSE;
169 case SDL_MOUSEMOTION:
170 vo_mouse_movement(global_vo, event->motion.x, event->motion.y);
171 break;
173 case SDL_MOUSEBUTTONDOWN:
174 if (!vo_nomouse_input)
175 mplayer_put_key((MOUSE_BTN0 + event->button.button - 1)
176 | MP_KEY_DOWN | get_mod_state());
177 break;
179 case SDL_MOUSEBUTTONUP:
180 if (!vo_nomouse_input)
181 mplayer_put_key((MOUSE_BTN0 + event->button.button - 1)
182 | get_mod_state());
183 break;
185 case SDL_KEYDOWN: {
186 int mods = get_mod_state();
187 int sym = event->key.keysym.sym;
188 mpkey = lookup_keymap_table(keysym_map, sym);
189 if (!mpkey) {
190 int unicode = event->key.keysym.unicode;
191 if (unicode >= 32 && unicode < 128) {
192 mpkey = unicode;
193 } else {
194 // SDL translates CTRL+letter to a control code < 32
195 // Compensate for "unexpected" SDL behavior.
196 // Note that CTRL combined with any non-letter key works fine.
197 if (sym >= SDLK_a && sym <= SDLK_z) {
198 mpkey = sym - SDLK_a + 'a';
199 if (mods & KEY_MODIFIER_SHIFT)
200 mpkey += 'A' - 'a';
204 if (mpkey)
205 mplayer_put_key(mpkey | mods);
206 break;
209 case SDL_QUIT:
210 mplayer_put_key(KEY_CLOSE_WIN);
211 break;
213 return 0;