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 bool NeedRedraw = false;
23 void fgRegisterIdleHandler(fgIdleHandler func)
28 void fgRegisterDrawHandler(fgDrawHandler func)
34 void fgRegisterWindowResizeHandler(fgWindowResizeHandler func)
36 // Noop. SDL does not support window resize.
39 void fgRegisterKeyHandler(fgKeyHandler func)
44 void fgRegisterMouseClickHandler(fgMouseClickHandler func)
46 MouseClickHandler = func;
49 void fgRegisterMouseMotionHandler(fgMouseMotionHandler func)
51 MouseMotionHandler = func;
55 // fg_os implementation
58 void fgOSOpenWindow(int w, int h, int bpp, bool alpha, bool fullscreen)
60 int cbits = (bpp <= 16) ? 5 : 8;
61 int zbits = (bpp <= 16) ? 16 : 32;
63 SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE); // FIXME: handle errors
65 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, cbits);
66 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, cbits);
67 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, cbits);
69 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
70 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, zbits);
71 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
73 int vidmask = SDL_OPENGL;
74 if(fullscreen) vidmask |= SDL_FULLSCREEN;
75 SDL_SetVideoMode(w, h, 16, vidmask); // FIXME: handle errors
77 SDL_WM_SetCaption("FlightGear", "FlightGear");
79 // This enables keycode translation (e.g. capital letters when
80 // shift is pressed, as well as i18n input methods). Eventually,
81 // we may want to port the input maps to specify <mod-shift>
82 // explicitly, and will turn this off.
85 // FIXME: we may not get what we asked for (especially in full
86 // screen modes), so these need to be propagated back to the
87 // property tree for the rest of the code to inspect...
89 // SDL_Surface* screen = SDL_GetVideoSurface();
90 // int realw = screen->w;
91 // int realh = screen->h;
94 static void handleKey(int key, int keyup)
98 case SDLK_RSHIFT: modmask = KEYMOD_SHIFT; break;
99 case SDLK_LSHIFT: modmask = KEYMOD_SHIFT; break;
100 case SDLK_RCTRL: modmask = KEYMOD_CTRL; break;
101 case SDLK_LCTRL: modmask = KEYMOD_CTRL; break;
102 case SDLK_RALT: modmask = KEYMOD_ALT; break;
103 case SDLK_LALT: modmask = KEYMOD_ALT; break;
105 case SDLK_LEFT: key = PU_KEY_LEFT; break;
106 case SDLK_UP: key = PU_KEY_UP; break;
107 case SDLK_RIGHT: key = PU_KEY_RIGHT; break;
108 case SDLK_DOWN: key = PU_KEY_DOWN; break;
109 case SDLK_PAGEUP: key = PU_KEY_PAGE_UP; break;
110 case SDLK_PAGEDOWN: key = PU_KEY_PAGE_DOWN; break;
111 case SDLK_HOME: key = PU_KEY_HOME; break;
112 case SDLK_END: key = PU_KEY_END; break;
113 case SDLK_INSERT: key = PU_KEY_INSERT; break;
114 case SDLK_F1: key = PU_KEY_F1; break;
115 case SDLK_F2: key = PU_KEY_F2; break;
116 case SDLK_F3: key = PU_KEY_F3; break;
117 case SDLK_F4: key = PU_KEY_F4; break;
118 case SDLK_F5: key = PU_KEY_F5; break;
119 case SDLK_F6: key = PU_KEY_F6; break;
120 case SDLK_F7: key = PU_KEY_F7; break;
121 case SDLK_F8: key = PU_KEY_F8; break;
122 case SDLK_F9: key = PU_KEY_F9; break;
123 case SDLK_F10: key = PU_KEY_F10; break;
124 case SDLK_F11: key = PU_KEY_F11; break;
125 case SDLK_F12: key = PU_KEY_F12; break;
127 if(keyup) CurrentModifiers &= ~modmask;
128 else CurrentModifiers |= modmask;
129 if(modmask == 0 && KeyHandler)
130 (*KeyHandler)(key, CurrentModifiers, CurrentMouseX, CurrentMouseY);
133 // FIXME: need to export this and get the rest of FlightGear to use
134 // it, the SDL hook is required to do things like reset the video
135 // mode and key repeat. Integrate with existing fgExit() in util.cxx.
136 void fgOSExit(int code)
146 while(SDL_PollEvent(&e)) {
153 handleKey(e.key.keysym.unicode, e.key.state == SDL_RELEASED);
155 case SDL_MOUSEBUTTONDOWN:
156 case SDL_MOUSEBUTTONUP:
157 // Note offset: SDL uses buttons 1,2,3 not 0,1,2
158 CurrentMouseX = e.button.x;
159 CurrentMouseY = e.button.y;
160 if(MouseClickHandler)
161 (*MouseClickHandler)(e.button.button - 1,
162 e.button.state == SDL_RELEASED,
163 e.button.x, e.button.y);
165 case SDL_MOUSEMOTION:
166 CurrentMouseX = e.motion.x;
167 CurrentMouseY = e.motion.y;
168 if(MouseMotionHandler)
169 (*MouseMotionHandler)(e.motion.x, e.motion.y);
173 if(IdleHandler) (*IdleHandler)();
174 if(NeedRedraw && DrawHandler) {
176 SDL_GL_SwapBuffers();
182 int fgGetKeyModifiers()
184 return CurrentModifiers;
187 void fgWarpMouse(int x, int y)
192 void fgRequestRedraw()
197 void fgOSInit(int* argc, char** argv)
199 // Nothing to do here. SDL has no command line options.
202 void fgOSFullScreen()
204 // Noop. SDL must set fullscreen at window open time and cannot
208 // Figure out what to do with these
209 void fgSetMouseCursor(int cursor) {}
210 int fgGetMouseCursor() { return MOUSE_CURSOR_POINTER; }