]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_os_sdl.cxx
set /sim/fg-current to current working directory; getcwd() is defined in
[flightgear.git] / src / Main / fg_os_sdl.cxx
1 #include <stdlib.h>
2
3 #include <simgear/compiler.h>
4 #include <simgear/structure/exception.hxx>
5 #include <simgear/debug/logstream.hxx>
6
7 #include <SDL/SDL.h>
8 #include <plib/pu.h>
9
10 #include "fg_os.hxx"
11
12 //
13 // fg_os callback registration APIs
14 //
15
16 static fgIdleHandler IdleHandler = 0;
17 static fgDrawHandler DrawHandler = 0;
18 static fgWindowResizeHandler WindowResizeHandler = 0;
19 static fgKeyHandler KeyHandler = 0;
20 static fgMouseClickHandler MouseClickHandler = 0;
21 static fgMouseMotionHandler MouseMotionHandler = 0;
22
23 static int CurrentModifiers = 0;
24 static int CurrentMouseX = 0;
25 static int CurrentMouseY = 0;
26 static int CurrentMouseCursor = MOUSE_CURSOR_POINTER;
27 static bool NeedRedraw = false;
28 static int VidMask = SDL_OPENGL|SDL_RESIZABLE;
29
30 void fgRegisterIdleHandler(fgIdleHandler func)
31 {
32     IdleHandler = func;
33 }
34
35 void fgRegisterDrawHandler(fgDrawHandler func)
36 {
37     DrawHandler = func;
38     NeedRedraw = true;
39 }
40
41 void fgRegisterWindowResizeHandler(fgWindowResizeHandler func)
42 {
43     WindowResizeHandler = func;
44 }
45
46 void fgRegisterKeyHandler(fgKeyHandler func)
47 {
48     KeyHandler = func;
49 }
50
51 void fgRegisterMouseClickHandler(fgMouseClickHandler func)
52 {
53     MouseClickHandler = func;
54 }
55
56 void fgRegisterMouseMotionHandler(fgMouseMotionHandler func)
57 {
58     MouseMotionHandler = func;
59 }
60
61 //
62 // fg_os implementation
63 //
64 static void initCursors();
65
66 void fgOSOpenWindow(int w, int h, int bpp,
67                     bool alpha, bool stencil, bool fullscreen)
68 {
69     int cbits = (bpp <= 16) ?  5 :  8;
70     int zbits = (bpp <= 16) ? 16 : 24;
71
72     if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE) == -1)
73         throw sg_throwable(string("Failed to initialize SDL: ")
74                            + SDL_GetError());
75     atexit(SDL_Quit);
76
77     SDL_WM_SetCaption("FlightGear", "FlightGear");
78
79     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, cbits);
80     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, cbits);
81     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, cbits);
82     if(alpha)
83         SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
84     if(bpp > 16 && stencil)
85         SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
86     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, zbits);
87     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
88
89     if(fullscreen) {
90         VidMask |= SDL_FULLSCREEN;
91     }
92     if (SDL_SetVideoMode(w, h, 16, VidMask) == 0)
93         throw sg_throwable(string("Failed to set SDL video mode: ")
94                                    + SDL_GetError());
95
96     // This enables keycode translation (e.g. capital letters when
97     // shift is pressed, as well as i18n input methods).  Eventually,
98     // we may want to port the input maps to specify <mod-shift>
99     // explicitly, and will turn this off.
100     SDL_EnableUNICODE(1);
101     SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
102
103     initCursors();
104     fgSetMouseCursor(MOUSE_CURSOR_POINTER);
105
106     // FIXME: we may not get what we asked for (especially in full
107     // screen modes), so these need to be propagated back to the
108     // property tree for the rest of the code to inspect...
109     //
110     // SDL_Surface* screen = SDL_GetVideoSurface();
111     // int realw = screen->w;
112     // int realh = screen->h;
113 }
114
115
116 struct keydata {
117     keydata() : unicode(0), mod(0) {};
118     Uint16 unicode;
119     unsigned int mod;
120 } keys[SDLK_LAST];
121
122
123 static void handleKey(int key, int raw, int keyup)
124 {
125     int modmask = 0;
126     switch(key) {
127     case SDLK_RSHIFT: modmask = KEYMOD_SHIFT; break;
128     case SDLK_LSHIFT: modmask = KEYMOD_SHIFT; break;
129     case SDLK_RCTRL:  modmask = KEYMOD_CTRL;  break;
130     case SDLK_LCTRL:  modmask = KEYMOD_CTRL;  break;
131     case SDLK_RALT:   modmask = KEYMOD_ALT;   break;
132     case SDLK_LALT:   modmask = KEYMOD_ALT;   break;
133
134     case SDLK_LEFT:     key = PU_KEY_LEFT;      break;
135     case SDLK_UP:       key = PU_KEY_UP;        break;
136     case SDLK_RIGHT:    key = PU_KEY_RIGHT;     break;
137     case SDLK_DOWN:     key = PU_KEY_DOWN;      break;
138     case SDLK_PAGEUP:   key = PU_KEY_PAGE_UP;   break;
139     case SDLK_PAGEDOWN: key = PU_KEY_PAGE_DOWN; break;
140     case SDLK_HOME:     key = PU_KEY_HOME;      break;
141     case SDLK_END:      key = PU_KEY_END;       break;
142     case SDLK_INSERT:   key = PU_KEY_INSERT;    break;
143     case SDLK_F1:       key = PU_KEY_F1;        break;
144     case SDLK_F2:       key = PU_KEY_F2;        break;
145     case SDLK_F3:       key = PU_KEY_F3;        break;
146     case SDLK_F4:       key = PU_KEY_F4;        break;
147     case SDLK_F5:       key = PU_KEY_F5;        break;
148     case SDLK_F6:       key = PU_KEY_F6;        break;
149     case SDLK_F7:       key = PU_KEY_F7;        break;
150     case SDLK_F8:       key = PU_KEY_F8;        break;
151     case SDLK_F9:       key = PU_KEY_F9;        break;
152     case SDLK_F10:      key = PU_KEY_F10;       break;
153     case SDLK_F11:      key = PU_KEY_F11;       break;
154     case SDLK_F12:      key = PU_KEY_F12;       break;
155     }
156
157     // Keypad codes.  This is a situation where we *don't* want the
158     // Unicode cooking for our input.  Oddly, neither PUI nor Glut
159     // define these anywhere, so I lifted the numbers out of
160     // FlightGear's keyboard.xml.  Some unused code are therefore
161     // missing.
162     switch(raw) {
163     case SDLK_KP0:      key = 364; break;
164     case SDLK_KP1:      key = 363; break;
165     case SDLK_KP2:      key = 359; break;
166     case SDLK_KP3:      key = 361; break;
167     case SDLK_KP4:      key = 356; break;
168     case SDLK_KP5:      key = 309; break;
169     case SDLK_KP6:      key = 358; break;
170     case SDLK_KP7:      key = 362; break;
171     case SDLK_KP8:      key = 357; break;
172     case SDLK_KP9:      key = 360; break;
173     case SDLK_KP_ENTER: key = 269; break;
174     }
175
176     int keymod = 0;
177     if(keyup) {
178         CurrentModifiers &= ~modmask;
179         keymod = CurrentModifiers | KEYMOD_RELEASED;
180     } else {
181         CurrentModifiers |= modmask;
182         keymod = CurrentModifiers & ~KEYMOD_RELEASED;
183     }
184
185     if(modmask == 0 && KeyHandler) {
186         if (keymod & KEYMOD_RELEASED) {
187             if (keys[raw].mod) {
188                 key = keys[raw].unicode;
189                 keys[raw].mod = 0;
190             }
191         } else {
192             keys[raw].mod = keymod;
193             keys[raw].unicode = key;
194         }
195         (*KeyHandler)(key, keymod, CurrentMouseX, CurrentMouseY);
196     }
197 }
198
199 // FIXME: Integrate with existing fgExit() in util.cxx.
200 void fgOSExit(int code)
201 {
202     exit(code);
203 }
204
205 void fgOSMainLoop()
206 {
207     while(1) {
208         SDL_Event e;
209         int key;
210         while(SDL_PollEvent(&e)) {
211             switch(e.type) {
212             case SDL_QUIT:
213                 fgOSExit(0);
214                 break;
215             case SDL_KEYDOWN:
216             case SDL_KEYUP:
217                 key = e.key.keysym.unicode;
218                 if(key == 0) key = e.key.keysym.sym;
219                 handleKey(key, e.key.keysym.sym, e.key.state == SDL_RELEASED);
220                 break;
221             case SDL_MOUSEBUTTONDOWN:
222             case SDL_MOUSEBUTTONUP:
223                 // Note offset: SDL uses buttons 1,2,3 not 0,1,2
224                 CurrentMouseX = e.button.x;
225                 CurrentMouseY = e.button.y;
226                 if(MouseClickHandler)
227                     (*MouseClickHandler)(e.button.button - 1,
228                                          e.button.state == SDL_RELEASED,
229                                          e.button.x, e.button.y, true, 0);
230                 break;
231             case SDL_MOUSEMOTION:
232                 CurrentMouseX = e.motion.x;
233                 CurrentMouseY = e.motion.y;
234                 if(MouseMotionHandler)
235                     (*MouseMotionHandler)(e.motion.x, e.motion.y);
236                 break;
237             case SDL_VIDEORESIZE:
238                 if (SDL_SetVideoMode(e.resize.w, e.resize.h, 16, VidMask) == 0)
239                     throw sg_throwable(string("Failed to set SDL video mode: ")
240                             + SDL_GetError());
241
242                 if (WindowResizeHandler)
243                     (*WindowResizeHandler)(e.resize.w, e.resize.h);
244                 break;
245             }
246         }
247         if(IdleHandler) (*IdleHandler)();
248         if(NeedRedraw && DrawHandler) {
249             (*DrawHandler)();
250             SDL_GL_SwapBuffers();
251             NeedRedraw = false;
252         }
253     }
254 }
255
256 int fgGetKeyModifiers()
257 {
258     return CurrentModifiers;
259 }
260
261 void fgWarpMouse(int x, int y)
262 {
263     SDL_Event e[10];
264     SDL_PumpEvents();
265     SDL_PeepEvents(e, 10, SDL_GETEVENT, SDL_MOUSEMOTIONMASK);
266     SDL_WarpMouse(x, y);
267 }
268
269 void fgRequestRedraw()
270 {
271     NeedRedraw = true;
272 }
273
274 void fgOSInit(int* argc, char** argv)
275 {
276     // Nothing to do here.  SDL has no command line options.
277 }
278
279 void fgOSFullScreen()
280 {
281     // Noop.  SDL must set fullscreen at window open time and cannot
282     // change modes.
283 }
284
285 static struct cursor_rec {
286     int name;
287     SDL_Cursor* sdlCursor;
288     int w;
289     int h;
290     int hotx;
291     int hoty;
292     char *img[32]; // '.' == white, '#' == black, ' ' == transparent
293 } cursors[] = {
294     { MOUSE_CURSOR_POINTER, 0, // must be first!
295       10, 16, 1, 1,
296       { "..        ",
297         ".#.       ",
298         ".##.      ",
299         ".###.     ",
300         ".####.    ",
301         ".#####.   ",
302         ".######.  ",
303         ".#######. ",
304         ".########.",
305         ".#####....",
306         ".##.##.   ",
307         ".#. .##.  ",
308         "..  .##.  ",
309         "     .##. ",
310         "     .##. ",
311         "      ..  " } },
312     { MOUSE_CURSOR_CROSSHAIR, 0,
313       17, 17, 8, 8,
314       { "       ...       ",
315         "       .#.       ",
316         "       .#.       ",
317         "       .#.       ",
318         "       .#.       ",
319         "       .#.       ",
320         "       .#.       ",
321         "........#........",
322         ".#######.#######.",
323         "........#........",
324         "       .#.       ",
325         "       .#.       ",
326         "       .#.       ",
327         "       .#.       ",
328         "       .#.       ",
329         "       .#.       ",
330         "       ...       " } },
331     { MOUSE_CURSOR_WAIT, 0,
332       16, 16, 7, 7,
333       { "  .########.    ",
334         "  .########.    ",
335         "  .########.    ",
336         " .##########.   ",
337         ".##....#...##.  ",
338         "##.....#....##..",
339         "#......#.....###",
340         "#.....###....###",
341         "#.....###....###",
342         "#....#.......###",
343         "##..#.......##..",
344         ".##........##.  ",
345         " .##########.   ",
346         "  .########.    ",
347         "  .########.    ",
348         "  .########.    " } },
349     { MOUSE_CURSOR_LEFTRIGHT, 0,
350       17, 9, 8, 4,
351       { "    ..     ..    ",
352         "   .#.     .#.   ",
353         "  .##.......##.  ",
354         " .#############. ",
355         ".####.......####.",
356         " .#############. ",
357         "  .##.......##.  ",
358         "   .#.     .#.   ",
359         "    ..     ..    " } },
360     { MOUSE_CURSOR_NONE, 0, // must come last!
361       1, 1, 0, 0, { " " } },
362 };
363
364 #define NCURSORS (sizeof(cursors)/sizeof(struct cursor_rec))
365
366 void fgSetMouseCursor(int cursor)
367 {
368     if(cursor == MOUSE_CURSOR_NONE) {
369         SDL_ShowCursor(SDL_DISABLE);
370         return;
371     }
372     SDL_ShowCursor(SDL_ENABLE);
373     for(unsigned int i=0; i<NCURSORS; i++) {
374         if(cursor == cursors[i].name) {
375             CurrentMouseCursor = cursor;
376             SDL_SetCursor(cursors[i].sdlCursor);
377             return;
378         }
379     }
380     // Default to pointer
381     CurrentMouseCursor = MOUSE_CURSOR_POINTER;
382     SDL_SetCursor(cursors[0].sdlCursor);
383 }
384
385 int fgGetMouseCursor()
386 {
387     return CurrentMouseCursor;
388 }
389
390 static void initCursors()
391 {
392     unsigned char mask[128], img[128];
393     for(unsigned int i=0; i<NCURSORS; i++) {
394         if(cursors[i].name == MOUSE_CURSOR_NONE) break;
395         for(int j=0; j<128; j++) mask[j] = img[j] = 0;
396         for(int y=0; y<cursors[i].h; y++) {
397             for(int x=0; x<cursors[i].w; x++) {
398                 int byte = (4 * y) + (x >> 3);
399                 int bit = 1 << (7 - (x & 7));
400                 int pix = cursors[i].img[y][x];
401                 if(pix != ' ') { mask[byte] |= bit; }
402                 if(pix == '#') { img[byte] |= bit; }
403             }
404         }
405         cursors[i].sdlCursor = SDL_CreateCursor(img, mask, 32, 32, 
406                                                 cursors[i].hotx,
407                                                 cursors[i].hoty);
408     }
409 }
410
411 // Noop; the graphics context is always current
412 void fgMakeCurrent()
413 {
414 }
415
416 bool fgOSIsMainCamera(const osg::Camera*)
417 {
418   return true;
419 }
420
421 bool fgOSIsMainContext(const osg::GraphicsContext*)
422 {
423   return true;
424 }