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, bool alpha, bool fullscreen)
62 int cbits = (bpp <= 16) ? 5 : 8;
63 int zbits = (bpp <= 16) ? 16 : 32;
65 SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE); // FIXME: handle errors
67 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, cbits);
68 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, cbits);
69 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, cbits);
71 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
72 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, zbits);
73 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
75 int vidmask = SDL_OPENGL;
76 if(fullscreen) vidmask |= SDL_FULLSCREEN;
77 SDL_SetVideoMode(w, h, 16, vidmask); // FIXME: handle errors
79 SDL_WM_SetCaption("FlightGear", "FlightGear");
81 // This enables keycode translation (e.g. capital letters when
82 // shift is pressed, as well as i18n input methods). Eventually,
83 // we may want to port the input maps to specify <mod-shift>
84 // explicitly, and will turn this off.
88 fgSetMouseCursor(MOUSE_CURSOR_POINTER);
90 // FIXME: we may not get what we asked for (especially in full
91 // screen modes), so these need to be propagated back to the
92 // property tree for the rest of the code to inspect...
94 // SDL_Surface* screen = SDL_GetVideoSurface();
95 // int realw = screen->w;
96 // int realh = screen->h;
99 static void handleKey(int key, int keyup)
103 case SDLK_RSHIFT: modmask = KEYMOD_SHIFT; break;
104 case SDLK_LSHIFT: modmask = KEYMOD_SHIFT; break;
105 case SDLK_RCTRL: modmask = KEYMOD_CTRL; break;
106 case SDLK_LCTRL: modmask = KEYMOD_CTRL; break;
107 case SDLK_RALT: modmask = KEYMOD_ALT; break;
108 case SDLK_LALT: modmask = KEYMOD_ALT; break;
110 case SDLK_LEFT: key = PU_KEY_LEFT; break;
111 case SDLK_UP: key = PU_KEY_UP; break;
112 case SDLK_RIGHT: key = PU_KEY_RIGHT; break;
113 case SDLK_DOWN: key = PU_KEY_DOWN; break;
114 case SDLK_PAGEUP: key = PU_KEY_PAGE_UP; break;
115 case SDLK_PAGEDOWN: key = PU_KEY_PAGE_DOWN; break;
116 case SDLK_HOME: key = PU_KEY_HOME; break;
117 case SDLK_END: key = PU_KEY_END; break;
118 case SDLK_INSERT: key = PU_KEY_INSERT; break;
119 case SDLK_F1: key = PU_KEY_F1; break;
120 case SDLK_F2: key = PU_KEY_F2; break;
121 case SDLK_F3: key = PU_KEY_F3; break;
122 case SDLK_F4: key = PU_KEY_F4; break;
123 case SDLK_F5: key = PU_KEY_F5; break;
124 case SDLK_F6: key = PU_KEY_F6; break;
125 case SDLK_F7: key = PU_KEY_F7; break;
126 case SDLK_F8: key = PU_KEY_F8; break;
127 case SDLK_F9: key = PU_KEY_F9; break;
128 case SDLK_F10: key = PU_KEY_F10; break;
129 case SDLK_F11: key = PU_KEY_F11; break;
130 case SDLK_F12: key = PU_KEY_F12; break;
132 if(keyup) CurrentModifiers &= ~modmask;
133 else CurrentModifiers |= modmask;
134 if(modmask == 0 && KeyHandler)
135 (*KeyHandler)(key, CurrentModifiers, CurrentMouseX, CurrentMouseY);
138 // FIXME: need to export this and get the rest of FlightGear to use
139 // it, the SDL hook is required to do things like reset the video
140 // mode and key repeat. Integrate with existing fgExit() in util.cxx.
141 void fgOSExit(int code)
151 while(SDL_PollEvent(&e)) {
158 handleKey(e.key.keysym.unicode, e.key.state == SDL_RELEASED);
160 case SDL_MOUSEBUTTONDOWN:
161 case SDL_MOUSEBUTTONUP:
162 // Note offset: SDL uses buttons 1,2,3 not 0,1,2
163 CurrentMouseX = e.button.x;
164 CurrentMouseY = e.button.y;
165 if(MouseClickHandler)
166 (*MouseClickHandler)(e.button.button - 1,
167 e.button.state == SDL_RELEASED,
168 e.button.x, e.button.y);
170 case SDL_MOUSEMOTION:
171 CurrentMouseX = e.motion.x;
172 CurrentMouseY = e.motion.y;
173 if(MouseMotionHandler)
174 (*MouseMotionHandler)(e.motion.x, e.motion.y);
178 if(IdleHandler) (*IdleHandler)();
179 if(NeedRedraw && DrawHandler) {
181 SDL_GL_SwapBuffers();
187 int fgGetKeyModifiers()
189 return CurrentModifiers;
192 void fgWarpMouse(int x, int y)
197 void fgRequestRedraw()
202 void fgOSInit(int* argc, char** argv)
204 // Nothing to do here. SDL has no command line options.
207 void fgOSFullScreen()
209 // Noop. SDL must set fullscreen at window open time and cannot
213 static struct cursor_rec {
215 SDL_Cursor* sdlCursor;
220 char *img[32]; // '.' == white, '#' == black, ' ' == transparent
222 { MOUSE_CURSOR_POINTER, 0, // must be first!
240 { MOUSE_CURSOR_CROSSHAIR, 0,
259 { MOUSE_CURSOR_WAIT, 0,
277 { MOUSE_CURSOR_LEFTRIGHT, 0,
288 { MOUSE_CURSOR_NONE, 0, // must come last!
289 1, 1, 0, 0, { " " } },
292 #define NCURSORS (sizeof(cursors)/sizeof(struct cursor_rec))
294 void fgSetMouseCursor(int cursor)
296 if(cursor == MOUSE_CURSOR_NONE) {
297 SDL_ShowCursor(SDL_DISABLE);
300 SDL_ShowCursor(SDL_ENABLE);
301 for(int i=0; i<NCURSORS; i++) {
302 if(cursor == cursors[i].name) {
303 CurrentMouseCursor = cursor;
304 SDL_SetCursor(cursors[i].sdlCursor);
308 // Default to pointer
309 CurrentMouseCursor = MOUSE_CURSOR_POINTER;
310 SDL_SetCursor(cursors[0].sdlCursor);
313 int fgGetMouseCursor()
315 return CurrentMouseCursor;
318 static void initCursors()
320 unsigned char mask[128], img[128];
322 for(int i=0; i<NCURSORS; i++) {
323 if(cursors[i].name == MOUSE_CURSOR_NONE) break;
324 for(int j=0; j<128; j++) mask[j] = img[j] = 0;
325 for(int y=0; y<cursors[i].h; y++) {
326 for(int x=0; x<cursors[i].w; x++) {
327 int byte = (4 * y) + (x >> 3);
328 int bit = 1 << (7 - (x & 7));
329 int pix = cursors[i].img[y][x];
330 if(pix != ' ') { mask[byte] |= bit; }
331 if(pix == '#') { img[byte] |= bit; }
334 cursors[i].sdlCursor = SDL_CreateCursor(img, mask, 32, 32,