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 static int CurrentCursor = MOUSE_CURSOR_POINTER;
144 int fgGetMouseCursor()
146 return CurrentCursor;
149 void fgSetMouseCursor(int cursor)
151 CurrentCursor = cursor;
152 if (cursor == MOUSE_CURSOR_NONE) cursor = GLUT_CURSOR_NONE;
153 else if(cursor == MOUSE_CURSOR_POINTER) cursor = GLUT_CURSOR_INHERIT;
154 else if(cursor == MOUSE_CURSOR_WAIT) cursor = GLUT_CURSOR_WAIT;
155 else if(cursor == MOUSE_CURSOR_CROSSHAIR) cursor = GLUT_CURSOR_CROSSHAIR;
156 else if(cursor == MOUSE_CURSOR_LEFTRIGHT) cursor = GLUT_CURSOR_LEFT_RIGHT;
157 // Otherwise, pass it through unchanged...
158 glutSetCursor(cursor);
161 void fgWarpMouse(int x, int y)
163 glutWarpPointer(x, y);
166 int fgGetKeyModifiers()
169 if(GlutModifiers & GLUT_ACTIVE_SHIFT) result |= KEYMOD_SHIFT;
170 if(GlutModifiers & GLUT_ACTIVE_CTRL) result |= KEYMOD_CTRL;
171 if(GlutModifiers & GLUT_ACTIVE_ALT) result |= KEYMOD_ALT;
175 void fgRequestRedraw()
180 void fgOSOpenWindow(int w, int h, int bpp, bool alpha)
182 int mode = GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE;
183 if(alpha) mode |= GLUT_ALPHA;
185 glutInitDisplayMode(mode);
186 glutInitWindowSize(w, h);
187 if(!fgGetBool("/sim/startup/game-mode")) {
188 glutCreateWindow("FlightGear");
190 char game_mode_str[256];
191 sprintf(game_mode_str, "width=%d height=%d bpp=%d", w, h, bpp);
192 glutGameModeString( game_mode_str );
196 // Register these here. Calling them before the window is open
198 glutMotionFunc(GLUTmotion);
199 glutPassiveMotionFunc(GLUTmotion);
200 glutMouseFunc(GLUTmouse);
201 glutSpecialUpFunc(GLUTspecialkeyup);
202 glutSpecialFunc(GLUTspecialkey);
203 glutKeyboardUpFunc(GLUTkeyup);
204 glutKeyboardFunc(GLUTkey);
205 glutIdleFunc(GLUTidle);
206 glutDisplayFunc(GLUTdraw);
207 glutReshapeFunc(GLUTreshape);