1 // The mac puts this in a weird location (GLUT/glut.h), so the
2 // configure script detects the location and defines it as a macro.
12 #include "fg_props.hxx"
16 // fg_os callback registration APIs
17 // (These are not glut-specific)
20 static fgIdleHandler IdleHandler = 0;
21 static fgDrawHandler DrawHandler = 0;
22 static fgWindowResizeHandler WindowResizeHandler = 0;
23 static fgKeyHandler KeyHandler = 0;
24 static fgMouseClickHandler MouseClickHandler = 0;
25 static fgMouseMotionHandler MouseMotionHandler = 0;
27 void fgRegisterIdleHandler(fgIdleHandler func)
32 void fgRegisterDrawHandler(fgDrawHandler func)
37 void fgRegisterWindowResizeHandler(fgWindowResizeHandler func)
39 WindowResizeHandler = func;
42 void fgRegisterKeyHandler(fgKeyHandler func)
47 void fgRegisterMouseClickHandler(fgMouseClickHandler func)
49 MouseClickHandler = func;
52 void fgRegisterMouseMotionHandler(fgMouseMotionHandler func)
54 MouseMotionHandler = func;
58 // Native glut callbacks.
59 // These translate the glut event model into fg*Handler callbacks
62 static int GlutModifiers = 0;
64 static void callKeyHandler(int k, int mods, int x, int y)
66 int puiup = mods & KEYMOD_RELEASED ? PU_UP : PU_DOWN;
67 if(puKeyboard(k, puiup))
69 if(KeyHandler) (*KeyHandler)(k, mods, x, y);
72 static void GLUTmotion (int x, int y)
74 if(MouseMotionHandler) (*MouseMotionHandler)(x, y);
77 static void GLUTmouse (int button, int updown, int x, int y)
79 GlutModifiers = glutGetModifiers();
80 if(MouseClickHandler) (*MouseClickHandler)(button, updown, x, y);
83 static void GLUTspecialkeyup(int k, int x, int y)
85 GlutModifiers = glutGetModifiers();
86 callKeyHandler(256 + k, fgGetKeyModifiers() | KEYMOD_RELEASED, x, y);
89 static void GLUTspecialkey(int k, int x, int y)
91 GlutModifiers = glutGetModifiers();
92 callKeyHandler(256 + k, fgGetKeyModifiers(), x, y);
95 static void GLUTkeyup(unsigned char k, int x, int y)
97 GlutModifiers = glutGetModifiers();
98 callKeyHandler(k, fgGetKeyModifiers() | KEYMOD_RELEASED, x, y);
101 static void GLUTkey(unsigned char k, int x, int y)
103 GlutModifiers = glutGetModifiers();
104 callKeyHandler(k, fgGetKeyModifiers(), x, y);
107 static void GLUTidle()
109 if(IdleHandler) (*IdleHandler)();
112 static void GLUTdraw()
114 if(DrawHandler) (*DrawHandler)();
118 static void GLUTreshape(int w, int h)
120 if(WindowResizeHandler) (*WindowResizeHandler)(w, h);
124 // fg_os API definition
127 void fgOSInit(int* argc, char** argv)
129 glutInit(argc, argv);
132 void fgOSFullScreen()
142 void fgOSExit(int code)
147 static int CurrentCursor = MOUSE_CURSOR_POINTER;
149 int fgGetMouseCursor()
151 return CurrentCursor;
154 void fgSetMouseCursor(int cursor)
156 CurrentCursor = cursor;
157 if (cursor == MOUSE_CURSOR_NONE) cursor = GLUT_CURSOR_NONE;
158 else if(cursor == MOUSE_CURSOR_POINTER) cursor = GLUT_CURSOR_INHERIT;
159 else if(cursor == MOUSE_CURSOR_WAIT) cursor = GLUT_CURSOR_WAIT;
160 else if(cursor == MOUSE_CURSOR_CROSSHAIR) cursor = GLUT_CURSOR_CROSSHAIR;
161 else if(cursor == MOUSE_CURSOR_LEFTRIGHT) cursor = GLUT_CURSOR_LEFT_RIGHT;
162 // Otherwise, pass it through unchanged...
163 glutSetCursor(cursor);
166 void fgWarpMouse(int x, int y)
168 glutWarpPointer(x, y);
171 int fgGetKeyModifiers()
174 if(GlutModifiers & GLUT_ACTIVE_SHIFT) result |= KEYMOD_SHIFT;
175 if(GlutModifiers & GLUT_ACTIVE_CTRL) result |= KEYMOD_CTRL;
176 if(GlutModifiers & GLUT_ACTIVE_ALT) result |= KEYMOD_ALT;
180 void fgRequestRedraw()
185 void fgOSOpenWindow(int w, int h, int bpp, bool alpha,
186 bool stencil, bool fullscreen)
188 int mode = GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE;
189 if(alpha) mode |= GLUT_ALPHA;
190 if(stencil) mode |= GLUT_STENCIL;
192 glutInitDisplayMode(mode);
193 glutInitWindowSize(w, h);
194 if(!fgGetBool("/sim/startup/game-mode")) {
195 glutCreateWindow("FlightGear");
197 char game_mode_str[256];
198 sprintf(game_mode_str, "width=%d height=%d bpp=%d", w, h, bpp);
199 glutGameModeString( game_mode_str );
203 // Register these here. Calling them before the window is open
205 glutMotionFunc(GLUTmotion);
206 glutPassiveMotionFunc(GLUTmotion);
207 glutMouseFunc(GLUTmouse);
208 glutSpecialUpFunc(GLUTspecialkeyup);
209 glutSpecialFunc(GLUTspecialkey);
210 glutKeyboardUpFunc(GLUTkeyup);
211 glutKeyboardFunc(GLUTkey);
212 glutIdleFunc(GLUTidle);
213 glutDisplayFunc(GLUTdraw);
214 glutReshapeFunc(GLUTreshape);