building ghost-sdl works again.
[blender.git] / intern / ghost / intern / GHOST_SystemSDL.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file ghost/intern/GHOST_SystemSDL.cpp
24  *  \ingroup GHOST
25  */
26
27 #include <assert.h>
28
29 #include "GHOST_SystemSDL.h"
30
31 #include "GHOST_WindowManager.h"
32
33 #include "GHOST_EventCursor.h"
34 #include "GHOST_EventKey.h"
35 #include "GHOST_EventButton.h"
36 #include "GHOST_EventWheel.h"
37
38 GHOST_SystemSDL::GHOST_SystemSDL()
39     :
40       GHOST_System()
41 {
42         if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
43                 printf("Error initializing SDL:  %s\n", SDL_GetError());
44         }
45
46         /* SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); */
47         /* SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4); */
48         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
49         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
50         SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
51         SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
52         SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
53         SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
54 }
55
56 GHOST_SystemSDL::~GHOST_SystemSDL()
57 {
58         SDL_Quit();
59 }
60
61 GHOST_IWindow *
62 GHOST_SystemSDL::createWindow(const STR_String& title,
63                               GHOST_TInt32 left,
64                               GHOST_TInt32 top,
65                               GHOST_TUns32 width,
66                               GHOST_TUns32 height,
67                               GHOST_TWindowState state,
68                               GHOST_TDrawingContextType type,
69                               bool stereoVisual,
70                               const GHOST_TUns16 numOfAASamples,
71                               const GHOST_TEmbedderWindowID parentWindow
72                               )
73 {
74         GHOST_WindowSDL *window = NULL;
75
76         window = new GHOST_WindowSDL(this, title, left, top, width, height, state, parentWindow, type, stereoVisual, numOfAASamples);
77
78         if (window) {
79                 if (GHOST_kWindowStateFullScreen == state) {
80                         SDL_Window *sdl_win = window->getSDLWindow();
81                         SDL_DisplayMode mode;
82
83                         static_cast<GHOST_DisplayManagerSDL *> (m_displayManager)->getCurrentDisplayModeSDL(mode);
84
85                         SDL_SetWindowDisplayMode(sdl_win, &mode);
86                         SDL_ShowWindow(sdl_win);
87                         SDL_SetWindowFullscreen(sdl_win, SDL_TRUE);
88                 }
89
90                 if (window->getValid()) {
91                         m_windowManager->addWindow(window);
92                         pushEvent(new GHOST_Event(getMilliSeconds(), GHOST_kEventWindowSize, window));
93                 }
94                 else {
95                         delete window;
96                         window = NULL;
97                 }
98         }
99         return window;
100 }
101
102 GHOST_TSuccess
103 GHOST_SystemSDL::init() {
104         GHOST_TSuccess success = GHOST_System::init();
105
106         if (success) {
107                 m_displayManager = new GHOST_DisplayManagerSDL(this);
108
109                 if (m_displayManager) {
110                         return GHOST_kSuccess;
111                 }
112         }
113
114         return GHOST_kFailure;
115 }
116
117 /**
118  * Returns the dimensions of the main display on this system.
119  * \return The dimension of the main display.
120  */
121 void
122 GHOST_SystemSDL::getAllDisplayDimensions(GHOST_TUns32& width,
123                                          GHOST_TUns32& height) const
124 {
125         SDL_DisplayMode mode;
126         SDL_GetDesktopDisplayMode(0, &mode); /* note, always 0 display */
127         width = mode.w;
128         height = mode.h;
129 }
130
131 void
132 GHOST_SystemSDL::getMainDisplayDimensions(GHOST_TUns32& width,
133                                           GHOST_TUns32& height) const
134 {
135         SDL_DisplayMode mode;
136         SDL_GetCurrentDisplayMode(0, &mode); /* note, always 0 display */
137         width = mode.w;
138         height = mode.h;
139 }
140
141 GHOST_TUns8
142 GHOST_SystemSDL::getNumDisplays() const
143 {
144         return SDL_GetNumVideoDisplays();
145 }
146
147 GHOST_TSuccess
148 GHOST_SystemSDL::getModifierKeys(GHOST_ModifierKeys& keys) const
149 {
150         SDL_Keymod mod = SDL_GetModState();
151
152         keys.set(GHOST_kModifierKeyLeftShift,    (mod & KMOD_LSHIFT) != 0);
153         keys.set(GHOST_kModifierKeyRightShift,   (mod & KMOD_RSHIFT) != 0);
154         keys.set(GHOST_kModifierKeyLeftControl,  (mod & KMOD_LCTRL) != 0);
155         keys.set(GHOST_kModifierKeyRightControl, (mod & KMOD_RCTRL) != 0);
156         keys.set(GHOST_kModifierKeyLeftAlt,      (mod & KMOD_LALT) != 0);
157         keys.set(GHOST_kModifierKeyRightAlt,     (mod & KMOD_RALT) != 0);
158         keys.set(GHOST_kModifierKeyOS,           (mod & (KMOD_LGUI | KMOD_RGUI)) != 0);
159
160         return GHOST_kSuccess;
161 }
162
163 #define GXMAP(k, x, y) case x: k = y; break
164
165 static GHOST_TKey
166 convertSDLKey(SDL_Scancode key)
167 {
168         GHOST_TKey type;
169
170         if ((key >= SDL_SCANCODE_A) && (key <= SDL_SCANCODE_Z)) {
171                 type = GHOST_TKey(key - SDL_SCANCODE_A + int(GHOST_kKeyA));
172         }
173         else if ((key >= SDL_SCANCODE_1) && (key <= SDL_SCANCODE_0)) {
174                 type = (key == SDL_SCANCODE_0) ? GHOST_kKey0 : GHOST_TKey(key - SDL_SCANCODE_1 + int(GHOST_kKey1));
175         }
176         else if ((key >= SDL_SCANCODE_F1) && (key <= SDL_SCANCODE_F12)) {
177                 type = GHOST_TKey(key - SDL_SCANCODE_F1 + int(GHOST_kKeyF1));
178         }
179         else if ((key >= SDL_SCANCODE_F13) && (key <= SDL_SCANCODE_F24)) {
180                 type = GHOST_TKey(key - SDL_SCANCODE_F13 + int(GHOST_kKeyF13));
181         }
182         else {
183                 switch (key) {
184                         /* TODO SDL_SCANCODE_NONUSBACKSLASH */
185
186                         GXMAP(type, SDL_SCANCODE_BACKSPACE,      GHOST_kKeyBackSpace);
187                         GXMAP(type, SDL_SCANCODE_TAB,            GHOST_kKeyTab);
188                         GXMAP(type, SDL_SCANCODE_RETURN,         GHOST_kKeyEnter);
189                         GXMAP(type, SDL_SCANCODE_ESCAPE,         GHOST_kKeyEsc);
190                         GXMAP(type, SDL_SCANCODE_SPACE,          GHOST_kKeySpace);
191
192                         GXMAP(type, SDL_SCANCODE_SEMICOLON,      GHOST_kKeySemicolon);
193                         GXMAP(type, SDL_SCANCODE_PERIOD,         GHOST_kKeyPeriod);
194                         GXMAP(type, SDL_SCANCODE_COMMA,          GHOST_kKeyComma);
195                         GXMAP(type, SDL_SCANCODE_APOSTROPHE,     GHOST_kKeyQuote);
196                         GXMAP(type, SDL_SCANCODE_GRAVE,          GHOST_kKeyAccentGrave);
197                         GXMAP(type, SDL_SCANCODE_MINUS,          GHOST_kKeyMinus);
198                         GXMAP(type, SDL_SCANCODE_EQUALS,         GHOST_kKeyEqual);
199
200                         GXMAP(type, SDL_SCANCODE_SLASH,          GHOST_kKeySlash);
201                         GXMAP(type, SDL_SCANCODE_BACKSLASH,      GHOST_kKeyBackslash);
202                         GXMAP(type, SDL_SCANCODE_KP_EQUALS,      GHOST_kKeyEqual);
203                         GXMAP(type, SDL_SCANCODE_LEFTBRACKET,    GHOST_kKeyLeftBracket);
204                         GXMAP(type, SDL_SCANCODE_RIGHTBRACKET,   GHOST_kKeyRightBracket);
205                         GXMAP(type, SDL_SCANCODE_PAUSE,          GHOST_kKeyPause);
206
207                         GXMAP(type, SDL_SCANCODE_LSHIFT,         GHOST_kKeyLeftShift);
208                         GXMAP(type, SDL_SCANCODE_RSHIFT,         GHOST_kKeyRightShift);
209                         GXMAP(type, SDL_SCANCODE_LCTRL,          GHOST_kKeyLeftControl);
210                         GXMAP(type, SDL_SCANCODE_RCTRL,          GHOST_kKeyRightControl);
211                         GXMAP(type, SDL_SCANCODE_LALT,           GHOST_kKeyLeftAlt);
212                         GXMAP(type, SDL_SCANCODE_RALT,           GHOST_kKeyRightAlt);
213                         GXMAP(type, SDL_SCANCODE_LGUI,           GHOST_kKeyOS);
214                         GXMAP(type, SDL_SCANCODE_RGUI,           GHOST_kKeyOS);
215
216                         GXMAP(type, SDL_SCANCODE_INSERT,         GHOST_kKeyInsert);
217                         GXMAP(type, SDL_SCANCODE_DELETE,         GHOST_kKeyDelete);
218                         GXMAP(type, SDL_SCANCODE_HOME,           GHOST_kKeyHome);
219                         GXMAP(type, SDL_SCANCODE_END,            GHOST_kKeyEnd);
220                         GXMAP(type, SDL_SCANCODE_PAGEUP,         GHOST_kKeyUpPage);
221                         GXMAP(type, SDL_SCANCODE_PAGEDOWN,       GHOST_kKeyDownPage);
222
223                         GXMAP(type, SDL_SCANCODE_LEFT,           GHOST_kKeyLeftArrow);
224                         GXMAP(type, SDL_SCANCODE_RIGHT,          GHOST_kKeyRightArrow);
225                         GXMAP(type, SDL_SCANCODE_UP,             GHOST_kKeyUpArrow);
226                         GXMAP(type, SDL_SCANCODE_DOWN,           GHOST_kKeyDownArrow);
227
228                         GXMAP(type, SDL_SCANCODE_CAPSLOCK,       GHOST_kKeyCapsLock);
229                         GXMAP(type, SDL_SCANCODE_SCROLLLOCK,     GHOST_kKeyScrollLock);
230                         GXMAP(type, SDL_SCANCODE_NUMLOCKCLEAR,   GHOST_kKeyNumLock);
231                         GXMAP(type, SDL_SCANCODE_PRINTSCREEN,    GHOST_kKeyPrintScreen);
232
233                         /* keypad events */
234
235                         /* note, sdl defines a bunch of kp defines I never saw before like
236                          * SDL_SCANCODE_KP_PERCENT, SDL_SCANCODE_KP_XOR - campbell */
237                         GXMAP(type, SDL_SCANCODE_KP_0,           GHOST_kKeyNumpad0);
238                         GXMAP(type, SDL_SCANCODE_KP_1,           GHOST_kKeyNumpad1);
239                         GXMAP(type, SDL_SCANCODE_KP_2,           GHOST_kKeyNumpad2);
240                         GXMAP(type, SDL_SCANCODE_KP_3,           GHOST_kKeyNumpad3);
241                         GXMAP(type, SDL_SCANCODE_KP_4,           GHOST_kKeyNumpad4);
242                         GXMAP(type, SDL_SCANCODE_KP_5,           GHOST_kKeyNumpad5);
243                         GXMAP(type, SDL_SCANCODE_KP_6,           GHOST_kKeyNumpad6);
244                         GXMAP(type, SDL_SCANCODE_KP_7,           GHOST_kKeyNumpad7);
245                         GXMAP(type, SDL_SCANCODE_KP_8,           GHOST_kKeyNumpad8);
246                         GXMAP(type, SDL_SCANCODE_KP_9,           GHOST_kKeyNumpad9);
247                         GXMAP(type, SDL_SCANCODE_KP_PERIOD,      GHOST_kKeyNumpadPeriod);
248
249                         GXMAP(type, SDL_SCANCODE_KP_ENTER,       GHOST_kKeyNumpadEnter);
250                         GXMAP(type, SDL_SCANCODE_KP_PLUS,        GHOST_kKeyNumpadPlus);
251                         GXMAP(type, SDL_SCANCODE_KP_MINUS,       GHOST_kKeyNumpadMinus);
252                         GXMAP(type, SDL_SCANCODE_KP_MULTIPLY,    GHOST_kKeyNumpadAsterisk);
253                         GXMAP(type, SDL_SCANCODE_KP_DIVIDE,      GHOST_kKeyNumpadSlash);
254
255                         /* Media keys in some keyboards and laptops with XFree86/Xorg */
256                         GXMAP(type, SDL_SCANCODE_AUDIOPLAY,      GHOST_kKeyMediaPlay);
257                         GXMAP(type, SDL_SCANCODE_AUDIOSTOP,      GHOST_kKeyMediaStop);
258                         GXMAP(type, SDL_SCANCODE_AUDIOPREV,      GHOST_kKeyMediaFirst);
259                         // GXMAP(type,XF86XK_AudioRewind,       GHOST_kKeyMediaFirst);
260                         GXMAP(type, SDL_SCANCODE_AUDIONEXT,      GHOST_kKeyMediaLast);
261
262                         default:
263                                 printf("Unknown\n");
264                                 type = GHOST_kKeyUnknown;
265                                 break;
266                 }
267         }
268
269         return type;
270 }
271 #undef GXMAP
272
273
274 void
275 GHOST_SystemSDL::processEvent(SDL_Event *sdl_event)
276 {
277         GHOST_Event *g_event = NULL;
278
279         switch (sdl_event->type) {
280                 case SDL_WINDOWEVENT:
281                 {
282                         SDL_WindowEvent &sdl_sub_evt = sdl_event->window;
283                         GHOST_WindowSDL *window = findGhostWindow(SDL_GetWindowFromID(sdl_sub_evt.windowID));
284                         //assert(window != NULL); // can be NULL on close window.
285
286                         switch (sdl_sub_evt.event) {
287                                 case SDL_WINDOWEVENT_EXPOSED:
288                                         g_event = new GHOST_Event(getMilliSeconds(), GHOST_kEventWindowUpdate, window);
289                                         break;
290                                 case SDL_WINDOWEVENT_RESIZED:
291                                         g_event = new GHOST_Event(getMilliSeconds(), GHOST_kEventWindowSize, window);
292                                         break;
293                                 case SDL_WINDOWEVENT_MOVED:
294                                         g_event = new GHOST_Event(getMilliSeconds(), GHOST_kEventWindowMove, window);
295                                         break;
296                                 case SDL_WINDOWEVENT_FOCUS_GAINED:
297                                         g_event = new GHOST_Event(getMilliSeconds(), GHOST_kEventWindowActivate, window);
298                                         break;
299                                 case SDL_WINDOWEVENT_FOCUS_LOST:
300                                         g_event = new GHOST_Event(getMilliSeconds(), GHOST_kEventWindowDeactivate, window);
301                                         break;
302                                 case SDL_WINDOWEVENT_CLOSE:
303                                         g_event = new GHOST_Event(getMilliSeconds(), GHOST_kEventWindowClose, window);
304                                         break;
305                         }
306                 }
307                         break;
308                 case SDL_QUIT:
309                         g_event = new GHOST_Event(getMilliSeconds(), GHOST_kEventQuit, NULL);
310                         break;
311
312                 case SDL_MOUSEMOTION:
313                 {
314                         SDL_MouseMotionEvent &sdl_sub_evt = sdl_event->motion;
315                         SDL_Window *sdl_win = SDL_GetWindowFromID(sdl_sub_evt.windowID);
316                         GHOST_WindowSDL *window = findGhostWindow(sdl_win);
317                         assert(window != NULL);
318
319                         int x_win, y_win;
320                         SDL_GetWindowPosition(sdl_win, &x_win, &y_win);
321
322                         GHOST_TInt32 x_root = sdl_sub_evt.x + x_win;
323                         GHOST_TInt32 y_root = sdl_sub_evt.y + y_win;
324
325 #if 0
326                         if (window->getCursorGrabMode() != GHOST_kGrabDisable && window->getCursorGrabMode() != GHOST_kGrabNormal)
327                         {
328                                 GHOST_TInt32 x_new = x_root;
329                                 GHOST_TInt32 y_new = y_root;
330                                 GHOST_TInt32 x_accum, y_accum;
331                                 GHOST_Rect bounds;
332
333                                 /* fallback to window bounds */
334                                 if (window->getCursorGrabBounds(bounds) == GHOST_kFailure)
335                                         window->getClientBounds(bounds);
336
337                                 /* could also clamp to screen bounds
338                                  * wrap with a window outside the view will fail atm  */
339                                 bounds.wrapPoint(x_new, y_new, 8); /* offset of one incase blender is at screen bounds */
340                                 window->getCursorGrabAccum(x_accum, y_accum);
341
342                                 // cant use setCursorPosition because the mouse may have no focus!
343                                 if (x_new != x_root || y_new != y_root) {
344                                         if (1) {  //xme.time > m_last_warp) {
345                                                 /* when wrapping we don't need to add an event because the
346                                                  * setCursorPosition call will cause a new event after */
347                                                 SDL_WarpMouseInWindow(sdl_win, x_new - x_win, y_new - y_win); /* wrap */
348                                                 window->setCursorGrabAccum(x_accum + (x_root - x_new), y_accum + (y_root - y_new));
349                                                 // m_last_warp= lastEventTime(xme.time);
350                                         }
351                                         else {
352                                                 // setCursorPosition(x_new, y_new); /* wrap but don't accumulate */
353                                                 SDL_WarpMouseInWindow(sdl_win, x_new - x_win, y_new - y_win);
354                                         }
355
356                                         g_event = new GHOST_EventCursor(getMilliSeconds(), GHOST_kEventCursorMove, window, x_new, y_new);
357                                 }
358                                 else {
359                                         g_event = new GHOST_EventCursor(getMilliSeconds(), GHOST_kEventCursorMove, window, x_root + x_accum, y_root + y_accum);
360                                 }
361                         }
362                         else
363 #endif
364                         {
365                                 g_event = new GHOST_EventCursor(getMilliSeconds(), GHOST_kEventCursorMove, window, x_root, y_root);
366                         }
367                         break;
368                 }
369                 case SDL_MOUSEBUTTONUP:
370                 case SDL_MOUSEBUTTONDOWN:
371                 {
372                         SDL_MouseButtonEvent &sdl_sub_evt = sdl_event->button;
373                         GHOST_TButtonMask gbmask = GHOST_kButtonMaskLeft;
374                         GHOST_TEventType type = (sdl_sub_evt.state == SDL_PRESSED) ? GHOST_kEventButtonDown : GHOST_kEventButtonUp;
375
376                         GHOST_WindowSDL *window = findGhostWindow(SDL_GetWindowFromID(sdl_sub_evt.windowID));
377                         assert(window != NULL);
378
379                         /* process rest of normal mouse buttons */
380                         if (sdl_sub_evt.button == SDL_BUTTON_LEFT)
381                                 gbmask = GHOST_kButtonMaskLeft;
382                         else if (sdl_sub_evt.button == SDL_BUTTON_MIDDLE)
383                                 gbmask = GHOST_kButtonMaskMiddle;
384                         else if (sdl_sub_evt.button == SDL_BUTTON_RIGHT)
385                                 gbmask = GHOST_kButtonMaskRight;
386                         /* these buttons are untested! */
387                         else if (sdl_sub_evt.button == SDL_BUTTON_X1)
388                                 gbmask = GHOST_kButtonMaskButton4;
389                         else if (sdl_sub_evt.button == SDL_BUTTON_X2)
390                                 gbmask = GHOST_kButtonMaskButton5;
391                         else
392                                 break;
393
394                         g_event = new GHOST_EventButton(getMilliSeconds(), type, window, gbmask);
395                         break;
396                 }
397                 case SDL_MOUSEWHEEL:
398                 {
399                         SDL_MouseWheelEvent &sdl_sub_evt = sdl_event->wheel;
400                         GHOST_WindowSDL *window = findGhostWindow(SDL_GetWindowFromID(sdl_sub_evt.windowID));
401                         assert(window != NULL);
402                         g_event = new GHOST_EventWheel(getMilliSeconds(), window, sdl_sub_evt.y);
403                 }
404                         break;
405                 case SDL_KEYDOWN:
406                 case SDL_KEYUP:
407                 {
408                         SDL_KeyboardEvent &sdl_sub_evt = sdl_event->key;
409                         SDL_Keycode sym = sdl_sub_evt.keysym.sym;
410                         GHOST_TEventType type = (sdl_sub_evt.state == SDL_PRESSED) ? GHOST_kEventKeyDown : GHOST_kEventKeyUp;
411
412                         GHOST_WindowSDL *window = findGhostWindow(SDL_GetWindowFromID(sdl_sub_evt.windowID));
413                         assert(window != NULL);
414
415                         GHOST_TKey gkey = convertSDLKey(sdl_sub_evt.keysym.scancode);
416                         /* note, the sdl_sub_evt.keysym.sym is truncated, for unicode support ghost has to be modified */
417                         /* printf("%d\n", sym); */
418                         if (sym > 127) {
419                                 switch (sym) {
420                                         case SDLK_KP_DIVIDE: sym = '/'; break;
421                                         case SDLK_KP_MULTIPLY: sym = '*'; break;
422                                         case SDLK_KP_MINUS: sym = '-'; break;
423                                         case SDLK_KP_PLUS: sym = '+'; break;
424                                         case SDLK_KP_1: sym = '1'; break;
425                                         case SDLK_KP_2: sym = '2'; break;
426                                         case SDLK_KP_3: sym = '3'; break;
427                                         case SDLK_KP_4: sym = '4'; break;
428                                         case SDLK_KP_5: sym = '5'; break;
429                                         case SDLK_KP_6: sym = '6'; break;
430                                         case SDLK_KP_7: sym = '7'; break;
431                                         case SDLK_KP_8: sym = '8'; break;
432                                         case SDLK_KP_9: sym = '9'; break;
433                                         case SDLK_KP_0: sym = '0'; break;
434                                         case SDLK_KP_PERIOD: sym = '.'; break;
435                                         default: sym = 0; break;
436                                 }
437                         }
438                         else {
439                                 if (sdl_sub_evt.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT)) {
440                                         /* lame US keyboard assumptions */
441                                         if (sym >= 'a' && sym <= ('a' + 32)) {
442                                                 sym -= 32;
443                                         }
444                                         else {
445                                                 switch (sym) {
446                                                         case '`': sym = '~'; break;
447                                                         case '1': sym = '!'; break;
448                                                         case '2': sym = '@'; break;
449                                                         case '3': sym = '#'; break;
450                                                         case '4': sym = '$'; break;
451                                                         case '5': sym = '%'; break;
452                                                         case '6': sym = '^'; break;
453                                                         case '7': sym = '&'; break;
454                                                         case '8': sym = '*'; break;
455                                                         case '9': sym = '('; break;
456                                                         case '0': sym = ')'; break;
457                                                         case '-': sym = '_'; break;
458                                                         case '=': sym = '+'; break;
459                                                         case '[': sym = '{'; break;
460                                                         case ']': sym = '}'; break;
461                                                         case '\\': sym = '|'; break;
462                                                         case ';': sym = ':'; break;
463                                                         case '\'': sym = '"'; break;
464                                                         case ',': sym = '<'; break;
465                                                         case '.': sym = '>'; break;
466                                                         case '/': sym = '?'; break;
467                                                         default:            break;
468                                                 }
469                                         }
470                                 }
471                         }
472
473                         g_event = new GHOST_EventKey(getMilliSeconds(), type, window, gkey, sym, NULL);
474                 }
475                         break;
476         }
477
478         if (g_event) {
479                 pushEvent(g_event);
480         }
481 }
482
483 GHOST_TSuccess
484 GHOST_SystemSDL::getCursorPosition(GHOST_TInt32& x,
485                                    GHOST_TInt32& y) const
486 {
487         int x_win, y_win;
488         SDL_Window *win = SDL_GetMouseFocus();
489         SDL_GetWindowPosition(win, &x_win, &y_win);
490
491         int xi, yi;
492         SDL_GetMouseState(&xi, &yi);
493         x = xi + x_win;
494         y = yi + x_win;
495
496         return GHOST_kSuccess;
497 }
498
499 GHOST_TSuccess
500 GHOST_SystemSDL::setCursorPosition(GHOST_TInt32 x,
501                                    GHOST_TInt32 y)
502 {
503         int x_win, y_win;
504         SDL_Window *win = SDL_GetMouseFocus();
505         SDL_GetWindowPosition(win, &x_win, &y_win);
506
507         SDL_WarpMouseInWindow(win, x - x_win, y - y_win);
508         return GHOST_kSuccess;
509 }
510
511 bool
512 GHOST_SystemSDL::generateWindowExposeEvents()
513 {
514         std::vector<GHOST_WindowSDL *>::iterator w_start = m_dirty_windows.begin();
515         std::vector<GHOST_WindowSDL *>::const_iterator w_end = m_dirty_windows.end();
516         bool anyProcessed = false;
517
518         for (; w_start != w_end; ++w_start) {
519                 GHOST_Event *g_event = new
520                                        GHOST_Event(
521                                            getMilliSeconds(),
522                                            GHOST_kEventWindowUpdate,
523                                            *w_start
524                                            );
525
526                 (*w_start)->validate();
527
528                 if (g_event) {
529                         printf("Expose events pushed\n");
530                         pushEvent(g_event);
531                         anyProcessed = true;
532                 }
533         }
534
535         m_dirty_windows.clear();
536         return anyProcessed;
537 }
538
539
540 bool
541 GHOST_SystemSDL::processEvents(bool waitForEvent)
542 {
543         // Get all the current events -- translate them into
544         // ghost events and call base class pushEvent() method.
545
546         bool anyProcessed = false;
547
548         do {
549                 GHOST_TimerManager *timerMgr = getTimerManager();
550
551                 if (waitForEvent && m_dirty_windows.empty() && !SDL_HasEvents(SDL_FIRSTEVENT, SDL_LASTEVENT)) {
552                         GHOST_TUns64 next = timerMgr->nextFireTime();
553
554                         if (next == GHOST_kFireTimeNever) {
555                                 SDL_WaitEventTimeout(NULL, -1);
556                                 //SleepTillEvent(m_display, -1);
557                         }
558                         else {
559                                 GHOST_TInt64 maxSleep = next - getMilliSeconds();
560
561                                 if (maxSleep >= 0) {
562                                         SDL_WaitEventTimeout(NULL, next - getMilliSeconds());
563                                         // SleepTillEvent(m_display, next - getMilliSeconds()); // X11
564                                 }
565                         }
566                 }
567
568                 if (timerMgr->fireTimers(getMilliSeconds())) {
569                         anyProcessed = true;
570                 }
571
572                 SDL_Event sdl_event;
573                 while (SDL_PollEvent(&sdl_event)) {
574                         processEvent(&sdl_event);
575                         anyProcessed = true;
576                 }
577
578                 if (generateWindowExposeEvents()) {
579                         anyProcessed = true;
580                 }
581         } while (waitForEvent && !anyProcessed);
582
583         return anyProcessed;
584 }
585
586
587 GHOST_WindowSDL *
588 GHOST_SystemSDL::findGhostWindow(SDL_Window *sdl_win)
589 {
590         if (sdl_win == NULL) return NULL;
591
592         // It is not entirely safe to do this as the backptr may point
593         // to a window that has recently been removed.
594         // We should always check the window manager's list of windows
595         // and only process events on these windows.
596
597         std::vector<GHOST_IWindow *> & win_vec = m_windowManager->getWindows();
598
599         std::vector<GHOST_IWindow *>::iterator win_it = win_vec.begin();
600         std::vector<GHOST_IWindow *>::const_iterator win_end = win_vec.end();
601
602         for (; win_it != win_end; ++win_it) {
603                 GHOST_WindowSDL *window = static_cast<GHOST_WindowSDL *>(*win_it);
604                 if (window->getSDLWindow() == sdl_win) {
605                         return window;
606                 }
607         }
608         return NULL;
609 }
610
611
612 void
613 GHOST_SystemSDL::addDirtyWindow(GHOST_WindowSDL *bad_wind)
614 {
615         GHOST_ASSERT((bad_wind != NULL), "addDirtyWindow() NULL ptr trapped (window)");
616
617         m_dirty_windows.push_back(bad_wind);
618 }
619
620
621 GHOST_TSuccess GHOST_SystemSDL::getButtons(GHOST_Buttons& buttons) const
622 {
623         Uint8 state = SDL_GetMouseState(NULL, NULL);
624         buttons.set(GHOST_kButtonMaskLeft,   (state & SDL_BUTTON_LMASK) != 0);
625         buttons.set(GHOST_kButtonMaskMiddle, (state & SDL_BUTTON_MMASK) != 0);
626         buttons.set(GHOST_kButtonMaskRight,  (state & SDL_BUTTON_RMASK) != 0);
627
628         return GHOST_kSuccess;
629 }
630
631 GHOST_TUns8 *
632 GHOST_SystemSDL::getClipboard(bool selection) const
633 {
634         return (GHOST_TUns8 *)SDL_GetClipboardText();
635 }
636
637 void
638 GHOST_SystemSDL::putClipboard(GHOST_TInt8 *buffer, bool selection) const
639 {
640         SDL_SetClipboardText(buffer);
641 }
642
643 GHOST_TUns64
644 GHOST_SystemSDL::getMilliSeconds()
645 {
646         return GHOST_TUns64(SDL_GetTicks()); /* note, 32 -> 64bits */
647 }