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