3 #include <simgear/compiler.h>
4 #include <simgear/structure/exception.hxx>
5 #include <simgear/debug/logstream.hxx>
13 // fg_os callback registration APIs
16 static fgIdleHandler IdleHandler = 0;
17 static fgDrawHandler DrawHandler = 0;
18 static fgWindowResizeHandler WindowResizeHandler = 0;
19 static fgKeyHandler KeyHandler = 0;
20 static fgMouseClickHandler MouseClickHandler = 0;
21 static fgMouseMotionHandler MouseMotionHandler = 0;
23 static int CurrentModifiers = 0;
24 static int CurrentMouseX = 0;
25 static int CurrentMouseY = 0;
26 static int CurrentMouseCursor = MOUSE_CURSOR_POINTER;
27 static bool NeedRedraw = false;
28 static int VidMask = SDL_OPENGL|SDL_RESIZABLE;
30 void fgRegisterIdleHandler(fgIdleHandler func)
35 void fgRegisterDrawHandler(fgDrawHandler func)
41 void fgRegisterWindowResizeHandler(fgWindowResizeHandler func)
43 WindowResizeHandler = func;
46 void fgRegisterKeyHandler(fgKeyHandler func)
51 void fgRegisterMouseClickHandler(fgMouseClickHandler func)
53 MouseClickHandler = func;
56 void fgRegisterMouseMotionHandler(fgMouseMotionHandler func)
58 MouseMotionHandler = func;
62 // fg_os implementation
64 static void initCursors();
66 void fgOSOpenWindow(int w, int h, int bpp,
67 bool alpha, bool stencil, bool fullscreen)
69 int cbits = (bpp <= 16) ? 5 : 8;
70 int zbits = (bpp <= 16) ? 16 : 24;
72 if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE) == -1)
73 throw sg_throwable(string("Failed to initialize SDL: ")
77 SDL_WM_SetCaption("FlightGear", "FlightGear");
79 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, cbits);
80 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, cbits);
81 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, cbits);
83 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
84 if(bpp > 16 && stencil)
85 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
86 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, zbits);
87 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
90 VidMask |= SDL_FULLSCREEN;
92 if (SDL_SetVideoMode(w, h, 16, VidMask) == 0)
93 throw sg_throwable(string("Failed to set SDL video mode: ")
96 // This enables keycode translation (e.g. capital letters when
97 // shift is pressed, as well as i18n input methods). Eventually,
98 // we may want to port the input maps to specify <mod-shift>
99 // explicitly, and will turn this off.
100 SDL_EnableUNICODE(1);
101 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
104 fgSetMouseCursor(MOUSE_CURSOR_POINTER);
106 // FIXME: we may not get what we asked for (especially in full
107 // screen modes), so these need to be propagated back to the
108 // property tree for the rest of the code to inspect...
110 // SDL_Surface* screen = SDL_GetVideoSurface();
111 // int realw = screen->w;
112 // int realh = screen->h;
115 static void handleKey(int key, int raw, int keyup)
119 case SDLK_RSHIFT: modmask = KEYMOD_SHIFT; break;
120 case SDLK_LSHIFT: modmask = KEYMOD_SHIFT; break;
121 case SDLK_RCTRL: modmask = KEYMOD_CTRL; break;
122 case SDLK_LCTRL: modmask = KEYMOD_CTRL; break;
123 case SDLK_RALT: modmask = KEYMOD_ALT; break;
124 case SDLK_LALT: modmask = KEYMOD_ALT; break;
126 case SDLK_LEFT: key = PU_KEY_LEFT; break;
127 case SDLK_UP: key = PU_KEY_UP; break;
128 case SDLK_RIGHT: key = PU_KEY_RIGHT; break;
129 case SDLK_DOWN: key = PU_KEY_DOWN; break;
130 case SDLK_PAGEUP: key = PU_KEY_PAGE_UP; break;
131 case SDLK_PAGEDOWN: key = PU_KEY_PAGE_DOWN; break;
132 case SDLK_HOME: key = PU_KEY_HOME; break;
133 case SDLK_END: key = PU_KEY_END; break;
134 case SDLK_INSERT: key = PU_KEY_INSERT; break;
135 case SDLK_F1: key = PU_KEY_F1; break;
136 case SDLK_F2: key = PU_KEY_F2; break;
137 case SDLK_F3: key = PU_KEY_F3; break;
138 case SDLK_F4: key = PU_KEY_F4; break;
139 case SDLK_F5: key = PU_KEY_F5; break;
140 case SDLK_F6: key = PU_KEY_F6; break;
141 case SDLK_F7: key = PU_KEY_F7; break;
142 case SDLK_F8: key = PU_KEY_F8; break;
143 case SDLK_F9: key = PU_KEY_F9; break;
144 case SDLK_F10: key = PU_KEY_F10; break;
145 case SDLK_F11: key = PU_KEY_F11; break;
146 case SDLK_F12: key = PU_KEY_F12; break;
149 // Keypad codes. This is a situation where we *don't* want the
150 // Unicode cooking for our input. Oddly, neither PUI nor Glut
151 // define these anywhere, so I lifted the numbers out of
152 // FlightGear's keyboard.xml. Some unused code are therefore
155 case SDLK_KP0: key = 364; break;
156 case SDLK_KP1: key = 363; break;
157 case SDLK_KP2: key = 359; break;
158 case SDLK_KP3: key = 361; break;
159 case SDLK_KP4: key = 356; break;
160 case SDLK_KP5: key = 309; break;
161 case SDLK_KP6: key = 358; break;
162 case SDLK_KP7: key = 362; break;
163 case SDLK_KP8: key = 357; break;
164 case SDLK_KP9: key = 360; break;
165 case SDLK_KP_ENTER: key = 269; break;
170 CurrentModifiers &= ~modmask;
171 keymod = CurrentModifiers | KEYMOD_RELEASED;
173 CurrentModifiers |= modmask;
174 keymod = CurrentModifiers & ~KEYMOD_RELEASED;
176 if(modmask == 0 && KeyHandler)
177 (*KeyHandler)(key, keymod, CurrentMouseX, CurrentMouseY);
180 // FIXME: Integrate with existing fgExit() in util.cxx.
181 void fgOSExit(int code)
191 while(SDL_PollEvent(&e)) {
198 key = e.key.keysym.unicode;
199 if(key == 0) key = e.key.keysym.sym;
200 handleKey(key, e.key.keysym.sym, e.key.state == SDL_RELEASED);
202 case SDL_MOUSEBUTTONDOWN:
203 case SDL_MOUSEBUTTONUP:
204 // Note offset: SDL uses buttons 1,2,3 not 0,1,2
205 CurrentMouseX = e.button.x;
206 CurrentMouseY = e.button.y;
207 if(MouseClickHandler)
208 (*MouseClickHandler)(e.button.button - 1,
209 e.button.state == SDL_RELEASED,
210 e.button.x, e.button.y);
212 case SDL_MOUSEMOTION:
213 CurrentMouseX = e.motion.x;
214 CurrentMouseY = e.motion.y;
215 if(MouseMotionHandler)
216 (*MouseMotionHandler)(e.motion.x, e.motion.y);
218 case SDL_VIDEORESIZE:
219 if (SDL_SetVideoMode(e.resize.w, e.resize.h, 16, VidMask) == 0)
220 throw sg_throwable(string("Failed to set SDL video mode: ")
223 if (WindowResizeHandler)
224 (*WindowResizeHandler)(e.resize.w, e.resize.h);
228 if(IdleHandler) (*IdleHandler)();
229 if(NeedRedraw && DrawHandler) {
231 SDL_GL_SwapBuffers();
237 int fgGetKeyModifiers()
239 return CurrentModifiers;
242 void fgWarpMouse(int x, int y)
247 void fgRequestRedraw()
252 void fgOSInit(int* argc, char** argv)
254 // Nothing to do here. SDL has no command line options.
257 void fgOSFullScreen()
259 // Noop. SDL must set fullscreen at window open time and cannot
263 static struct cursor_rec {
265 SDL_Cursor* sdlCursor;
270 char *img[32]; // '.' == white, '#' == black, ' ' == transparent
272 { MOUSE_CURSOR_POINTER, 0, // must be first!
290 { MOUSE_CURSOR_CROSSHAIR, 0,
309 { MOUSE_CURSOR_WAIT, 0,
327 { MOUSE_CURSOR_LEFTRIGHT, 0,
338 { MOUSE_CURSOR_NONE, 0, // must come last!
339 1, 1, 0, 0, { " " } },
342 #define NCURSORS (sizeof(cursors)/sizeof(struct cursor_rec))
344 void fgSetMouseCursor(int cursor)
346 if(cursor == MOUSE_CURSOR_NONE) {
347 SDL_ShowCursor(SDL_DISABLE);
350 SDL_ShowCursor(SDL_ENABLE);
351 for(unsigned int i=0; i<NCURSORS; i++) {
352 if(cursor == cursors[i].name) {
353 CurrentMouseCursor = cursor;
354 SDL_SetCursor(cursors[i].sdlCursor);
358 // Default to pointer
359 CurrentMouseCursor = MOUSE_CURSOR_POINTER;
360 SDL_SetCursor(cursors[0].sdlCursor);
363 int fgGetMouseCursor()
365 return CurrentMouseCursor;
368 static void initCursors()
370 unsigned char mask[128], img[128];
371 for(unsigned int i=0; i<NCURSORS; i++) {
372 if(cursors[i].name == MOUSE_CURSOR_NONE) break;
373 for(int j=0; j<128; j++) mask[j] = img[j] = 0;
374 for(int y=0; y<cursors[i].h; y++) {
375 for(int x=0; x<cursors[i].w; x++) {
376 int byte = (4 * y) + (x >> 3);
377 int bit = 1 << (7 - (x & 7));
378 int pix = cursors[i].img[y][x];
379 if(pix != ' ') { mask[byte] |= bit; }
380 if(pix == '#') { img[byte] |= bit; }
383 cursors[i].sdlCursor = SDL_CreateCursor(img, mask, 32, 32,