9 // fg_os callback registration APIs
12 static fgIdleHandler IdleHandler = 0;
13 static fgDrawHandler DrawHandler = 0;
14 static fgKeyHandler KeyHandler = 0;
15 static fgMouseClickHandler MouseClickHandler = 0;
16 static fgMouseMotionHandler MouseMotionHandler = 0;
18 static int CurrentModifiers = 0;
19 static int CurrentMouseX = 0;
20 static int CurrentMouseY = 0;
21 static int CurrentMouseCursor = MOUSE_CURSOR_POINTER;
22 static bool NeedRedraw = false;
24 void fgRegisterIdleHandler(fgIdleHandler func)
29 void fgRegisterDrawHandler(fgDrawHandler func)
35 void fgRegisterWindowResizeHandler(fgWindowResizeHandler func)
37 // Noop. SDL does not support window resize.
40 void fgRegisterKeyHandler(fgKeyHandler func)
45 void fgRegisterMouseClickHandler(fgMouseClickHandler func)
47 MouseClickHandler = func;
50 void fgRegisterMouseMotionHandler(fgMouseMotionHandler func)
52 MouseMotionHandler = func;
56 // fg_os implementation
58 static void initCursors();
60 void fgOSOpenWindow(int w, int h, int bpp,
61 bool alpha, bool stencil, bool fullscreen)
63 int cbits = (bpp <= 16) ? 5 : 8;
64 int zbits = (bpp <= 16) ? 16 : 24;
66 SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE); // FIXME: handle errors
68 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, cbits);
69 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, cbits);
70 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, cbits);
72 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
74 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
75 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, zbits);
76 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
78 int vidmask = SDL_OPENGL;
80 vidmask |= SDL_FULLSCREEN;
82 SDL_SetVideoMode(w, h, 16, vidmask); // FIXME: handle errors
84 SDL_WM_SetCaption("FlightGear", "FlightGear");
86 // This enables keycode translation (e.g. capital letters when
87 // shift is pressed, as well as i18n input methods). Eventually,
88 // we may want to port the input maps to specify <mod-shift>
89 // explicitly, and will turn this off.
91 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
94 fgSetMouseCursor(MOUSE_CURSOR_POINTER);
96 // FIXME: we may not get what we asked for (especially in full
97 // screen modes), so these need to be propagated back to the
98 // property tree for the rest of the code to inspect...
100 // SDL_Surface* screen = SDL_GetVideoSurface();
101 // int realw = screen->w;
102 // int realh = screen->h;
105 static void handleKey(int key, int raw, int keyup)
109 case SDLK_RSHIFT: modmask = KEYMOD_SHIFT; break;
110 case SDLK_LSHIFT: modmask = KEYMOD_SHIFT; break;
111 case SDLK_RCTRL: modmask = KEYMOD_CTRL; break;
112 case SDLK_LCTRL: modmask = KEYMOD_CTRL; break;
113 case SDLK_RALT: modmask = KEYMOD_ALT; break;
114 case SDLK_LALT: modmask = KEYMOD_ALT; break;
116 case SDLK_LEFT: key = PU_KEY_LEFT; break;
117 case SDLK_UP: key = PU_KEY_UP; break;
118 case SDLK_RIGHT: key = PU_KEY_RIGHT; break;
119 case SDLK_DOWN: key = PU_KEY_DOWN; break;
120 case SDLK_PAGEUP: key = PU_KEY_PAGE_UP; break;
121 case SDLK_PAGEDOWN: key = PU_KEY_PAGE_DOWN; break;
122 case SDLK_HOME: key = PU_KEY_HOME; break;
123 case SDLK_END: key = PU_KEY_END; break;
124 case SDLK_INSERT: key = PU_KEY_INSERT; break;
125 case SDLK_F1: key = PU_KEY_F1; break;
126 case SDLK_F2: key = PU_KEY_F2; break;
127 case SDLK_F3: key = PU_KEY_F3; break;
128 case SDLK_F4: key = PU_KEY_F4; break;
129 case SDLK_F5: key = PU_KEY_F5; break;
130 case SDLK_F6: key = PU_KEY_F6; break;
131 case SDLK_F7: key = PU_KEY_F7; break;
132 case SDLK_F8: key = PU_KEY_F8; break;
133 case SDLK_F9: key = PU_KEY_F9; break;
134 case SDLK_F10: key = PU_KEY_F10; break;
135 case SDLK_F11: key = PU_KEY_F11; break;
136 case SDLK_F12: key = PU_KEY_F12; break;
139 // Keypad codes. This is a situation where we *don't* want the
140 // Unicode cooking for our input. Oddly, neither PUI nor Glut
141 // define these anywhere, so I lifted the numbers out of
142 // FlightGear's keyboard.xml. Some unused code are therefore
145 case SDLK_KP0: key = 364; break;
146 case SDLK_KP1: key = 363; break;
147 case SDLK_KP2: key = 359; break;
148 case SDLK_KP3: key = 361; break;
149 case SDLK_KP4: key = 356; break;
150 case SDLK_KP5: key = 309; break;
151 case SDLK_KP6: key = 358; break;
152 case SDLK_KP7: key = 362; break;
153 case SDLK_KP8: key = 357; break;
154 case SDLK_KP9: key = 360; break;
155 case SDLK_KP_ENTER: key = 269; break;
160 CurrentModifiers &= ~modmask;
161 keymod = CurrentModifiers | KEYMOD_RELEASED;
163 CurrentModifiers |= modmask;
164 keymod = CurrentModifiers & ~KEYMOD_RELEASED;
166 if(modmask == 0 && KeyHandler)
167 (*KeyHandler)(key, keymod, CurrentMouseX, CurrentMouseY);
170 // FIXME: need to export this and get the rest of FlightGear to use
171 // it, the SDL hook is required to do things like reset the video
172 // mode and key repeat. Integrate with existing fgExit() in util.cxx.
173 void fgOSExit(int code)
184 while(SDL_PollEvent(&e)) {
191 key = e.key.keysym.unicode;
192 if(key == 0) key = e.key.keysym.sym;
193 handleKey(key, e.key.keysym.sym, e.key.state == SDL_RELEASED);
195 case SDL_MOUSEBUTTONDOWN:
196 case SDL_MOUSEBUTTONUP:
197 // Note offset: SDL uses buttons 1,2,3 not 0,1,2
198 CurrentMouseX = e.button.x;
199 CurrentMouseY = e.button.y;
200 if(MouseClickHandler)
201 (*MouseClickHandler)(e.button.button - 1,
202 e.button.state == SDL_RELEASED,
203 e.button.x, e.button.y);
205 case SDL_MOUSEMOTION:
206 CurrentMouseX = e.motion.x;
207 CurrentMouseY = e.motion.y;
208 if(MouseMotionHandler)
209 (*MouseMotionHandler)(e.motion.x, e.motion.y);
213 if(IdleHandler) (*IdleHandler)();
214 if(NeedRedraw && DrawHandler) {
216 SDL_GL_SwapBuffers();
222 int fgGetKeyModifiers()
224 return CurrentModifiers;
227 void fgWarpMouse(int x, int y)
232 void fgRequestRedraw()
237 void fgOSInit(int* argc, char** argv)
239 // Nothing to do here. SDL has no command line options.
242 void fgOSFullScreen()
244 // Noop. SDL must set fullscreen at window open time and cannot
248 static struct cursor_rec {
250 SDL_Cursor* sdlCursor;
255 char *img[32]; // '.' == white, '#' == black, ' ' == transparent
257 { MOUSE_CURSOR_POINTER, 0, // must be first!
275 { MOUSE_CURSOR_CROSSHAIR, 0,
294 { MOUSE_CURSOR_WAIT, 0,
312 { MOUSE_CURSOR_LEFTRIGHT, 0,
323 { MOUSE_CURSOR_NONE, 0, // must come last!
324 1, 1, 0, 0, { " " } },
327 #define NCURSORS (sizeof(cursors)/sizeof(struct cursor_rec))
329 void fgSetMouseCursor(int cursor)
331 if(cursor == MOUSE_CURSOR_NONE) {
332 SDL_ShowCursor(SDL_DISABLE);
335 SDL_ShowCursor(SDL_ENABLE);
336 for(int i=0; i<NCURSORS; i++) {
337 if(cursor == cursors[i].name) {
338 CurrentMouseCursor = cursor;
339 SDL_SetCursor(cursors[i].sdlCursor);
343 // Default to pointer
344 CurrentMouseCursor = MOUSE_CURSOR_POINTER;
345 SDL_SetCursor(cursors[0].sdlCursor);
348 int fgGetMouseCursor()
350 return CurrentMouseCursor;
353 static void initCursors()
355 unsigned char mask[128], img[128];
357 for(int i=0; i<NCURSORS; i++) {
358 if(cursors[i].name == MOUSE_CURSOR_NONE) break;
359 for(int j=0; j<128; j++) mask[j] = img[j] = 0;
360 for(int y=0; y<cursors[i].h; y++) {
361 for(int x=0; x<cursors[i].w; x++) {
362 int byte = (4 * y) + (x >> 3);
363 int bit = 1 << (7 - (x & 7));
364 int pix = cursors[i].img[y][x];
365 if(pix != ' ') { mask[byte] |= bit; }
366 if(pix == '#') { img[byte] |= bit; }
369 cursors[i].sdlCursor = SDL_CreateCursor(img, mask, 32, 32,