mp_msg.c: Avoid out-of-bounds read if empty string is printed
[mplayer/glamo.git] / libvo / sdl_common.c
blob42156d0cd358c7907a219c403dd6e016e76fca03
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 "osdep/keycodes.h"
26 #include "input/input.h"
27 #include "input/mouse.h"
28 #include "video_out.h"
30 static int old_w;
31 static int old_h;
32 static int mode_flags;
33 static int reinit;
35 int vo_sdl_init(void)
37 reinit = 0;
39 if (!SDL_WasInit(SDL_INIT_VIDEO) &&
40 SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE) < 0)
41 return 0;
43 // Setup Keyrepeats (500/30 are defaults)
44 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, 100 /*SDL_DEFAULT_REPEAT_INTERVAL*/);
46 // Easiest way to get uppercase characters
47 SDL_EnableUNICODE(1);
49 // We don't want those in our event queue.
50 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
51 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
52 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
54 return 1;
57 void vo_sdl_uninit(void)
59 if (SDL_WasInit(SDL_INIT_VIDEO))
60 SDL_QuitSubSystem(SDL_INIT_VIDEO);
63 void vo_sdl_fullscreen(void)
65 if (vo_fs) {
66 vo_dwidth = old_w;
67 vo_dheight = old_h;
68 } else {
69 old_w = vo_dwidth;
70 old_h = vo_dheight;
71 vo_dwidth = vo_screenwidth;
72 vo_dheight = vo_screenheight;
74 vo_fs = !vo_fs;
75 sdl_set_mode(0, mode_flags);
76 // on OSX at least we now need to do a full reinit.
77 // TODO: this should only be set if really necessary.
78 reinit = 1;
81 int sdl_set_mode(int bpp, uint32_t flags)
83 SDL_Surface *s;
84 mode_flags = flags;
85 if (vo_fs) flags |= SDL_FULLSCREEN;
86 // doublebuf with opengl creates flickering
87 if (vo_doublebuffering && !(flags & SDL_OPENGL))
88 flags |= SDL_DOUBLEBUF;
89 s = SDL_SetVideoMode(vo_dwidth, vo_dheight, bpp, flags);
90 if (!s) {
91 mp_msg(MSGT_VO, MSGL_FATAL, "SDL SetVideoMode failed: %s\n", SDL_GetError());
92 return -1;
94 vo_dwidth = s->w;
95 vo_dheight = s->h;
96 return 0;
99 static const struct mp_keymap keysym_map[] = {
100 {SDLK_RETURN, KEY_ENTER}, {SDLK_ESCAPE, KEY_ESC},
101 {SDLK_F1, KEY_F+1}, {SDLK_F2, KEY_F+2}, {SDLK_F3, KEY_F+3},
102 {SDLK_F4, KEY_F+4}, {SDLK_F5, KEY_F+5}, {SDLK_F6, KEY_F+6},
103 {SDLK_F7, KEY_F+7}, {SDLK_F8, KEY_F+8}, {SDLK_F9, KEY_F+9},
104 {SDLK_F10, KEY_F+10}, {SDLK_F11, KEY_F+11}, {SDLK_F12, KEY_F+12},
105 {SDLK_KP_PLUS, '+'}, {SDLK_KP_MINUS, '-'}, {SDLK_TAB, KEY_TAB},
106 {SDLK_PAGEUP, KEY_PAGE_UP}, {SDLK_PAGEDOWN, KEY_PAGE_DOWN},
107 {SDLK_UP, KEY_UP}, {SDLK_DOWN, KEY_DOWN},
108 {SDLK_LEFT, KEY_LEFT}, {SDLK_RIGHT, KEY_RIGHT},
109 {SDLK_KP_MULTIPLY, '*'}, {SDLK_KP_DIVIDE, '/'},
110 {SDLK_KP0, KEY_KP0}, {SDLK_KP1, KEY_KP1}, {SDLK_KP2, KEY_KP2},
111 {SDLK_KP3, KEY_KP3}, {SDLK_KP4, KEY_KP4}, {SDLK_KP5, KEY_KP5},
112 {SDLK_KP6, KEY_KP6}, {SDLK_KP7, KEY_KP7}, {SDLK_KP8, KEY_KP8},
113 {SDLK_KP9, KEY_KP9},
114 {SDLK_KP_PERIOD, KEY_KPDEC}, {SDLK_KP_ENTER, KEY_KPENTER},
115 {0, 0}
118 int sdl_default_handle_event(SDL_Event *event)
120 int mpkey;
121 if (!event) {
122 int res = reinit ? VO_EVENT_REINIT : 0;
123 reinit = 0;
124 return res;
126 switch (event->type) {
127 case SDL_VIDEORESIZE:
128 vo_dwidth = event->resize.w;
129 vo_dheight = event->resize.h;
130 return VO_EVENT_RESIZE;
132 case SDL_VIDEOEXPOSE:
133 return VO_EVENT_EXPOSE;
135 case SDL_MOUSEMOTION:
136 vo_mouse_movement(global_vo, event->motion.x, event->motion.y);
137 break;
139 case SDL_MOUSEBUTTONDOWN:
140 if (!vo_nomouse_input)
141 mplayer_put_key((MOUSE_BTN0 + event->button.button - 1) | MP_KEY_DOWN);
142 break;
144 case SDL_MOUSEBUTTONUP:
145 if (!vo_nomouse_input)
146 mplayer_put_key(MOUSE_BTN0 + event->button.button - 1);
147 break;
149 case SDL_KEYDOWN:
150 mpkey = lookup_keymap_table(keysym_map, event->key.keysym.sym);
151 if (!mpkey &&
152 event->key.keysym.unicode > 0 &&
153 event->key.keysym.unicode < 128)
154 mpkey = event->key.keysym.unicode;
155 if (mpkey)
156 mplayer_put_key(mpkey);
157 break;
159 case SDL_QUIT:
160 mplayer_put_key(KEY_CLOSE_WIN);
161 break;
163 return 0;