1/*
2    SDL - Simple DirectMedia Layer
3    Copyright (C) 1997-2012 Sam Lantinga
4    Copyright (C) 2001  Hsieh-Fu Tsai
5    Copyright (C) 2002  Greg Haerr <greg@censoft.com>
6
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Library General Public
9    License as published by the Free Software Foundation; either
10    version 2 of the License, or (at your option) any later version.
11
12    This library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Library General Public License for more details.
16
17    You should have received a copy of the GNU Library General Public
18    License along with this library; if not, write to the Free
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21    Sam Lantinga
22    slouken@libsdl.org
23
24    Hsieh-Fu Tsai
25    clare@setabox.com
26*/
27#include "SDL_config.h"
28
29#include "SDL_keysym.h"
30#include "../../events/SDL_events_c.h"
31
32#include "SDL_nxevents_c.h"
33#include "SDL_nximage_c.h"
34
35// The translation tables from a nanox keysym to a SDL keysym
36static SDLKey NX_NONASCII_keymap [MWKEY_LAST + 1] ;
37
38void NX_InitOSKeymap (_THIS)
39{
40    int i ;
41
42    Dprintf ("enter NX_InitOSKeymap\n") ;
43
44    // Map the nanox scancodes to SDL keysyms
45    for (i = 0; i < SDL_arraysize (NX_NONASCII_keymap); ++ i)
46        NX_NONASCII_keymap [i] = SDLK_UNKNOWN ;
47
48    NX_NONASCII_keymap [MWKEY_LEFT        & 0xFF] = SDLK_LEFT ;
49    NX_NONASCII_keymap [MWKEY_RIGHT       & 0xFF] = SDLK_RIGHT ;
50    NX_NONASCII_keymap [MWKEY_UP          & 0xFF] = SDLK_UP ;
51    NX_NONASCII_keymap [MWKEY_DOWN        & 0xFF] = SDLK_DOWN ;
52    NX_NONASCII_keymap [MWKEY_INSERT      & 0xFF] = SDLK_INSERT ;
53    NX_NONASCII_keymap [MWKEY_DELETE      & 0xFF] = SDLK_DELETE ;
54    NX_NONASCII_keymap [MWKEY_HOME        & 0xFF] = SDLK_HOME ;
55    NX_NONASCII_keymap [MWKEY_END         & 0xFF] = SDLK_END ;
56    NX_NONASCII_keymap [MWKEY_PAGEUP      & 0xFF] = SDLK_PAGEUP ;
57    NX_NONASCII_keymap [MWKEY_PAGEDOWN    & 0xFF] = SDLK_PAGEDOWN ;
58
59    NX_NONASCII_keymap [MWKEY_KP0         & 0xFF] = SDLK_KP0 ;
60    NX_NONASCII_keymap [MWKEY_KP1         & 0xFF] = SDLK_KP1 ;
61    NX_NONASCII_keymap [MWKEY_KP2         & 0xFF] = SDLK_KP2 ;
62    NX_NONASCII_keymap [MWKEY_KP3         & 0xFF] = SDLK_KP3 ;
63    NX_NONASCII_keymap [MWKEY_KP4         & 0xFF] = SDLK_KP4 ;
64    NX_NONASCII_keymap [MWKEY_KP5         & 0xFF] = SDLK_KP5 ;
65    NX_NONASCII_keymap [MWKEY_KP6         & 0xFF] = SDLK_KP6 ;
66    NX_NONASCII_keymap [MWKEY_KP7         & 0xFF] = SDLK_KP7 ;
67    NX_NONASCII_keymap [MWKEY_KP8         & 0xFF] = SDLK_KP8 ;
68    NX_NONASCII_keymap [MWKEY_KP9         & 0xFF] = SDLK_KP9 ;
69    NX_NONASCII_keymap [MWKEY_KP_PERIOD   & 0xFF] = SDLK_KP_PERIOD ;
70    NX_NONASCII_keymap [MWKEY_KP_DIVIDE   & 0xFF] = SDLK_KP_DIVIDE ;
71    NX_NONASCII_keymap [MWKEY_KP_MULTIPLY & 0xFF] = SDLK_KP_MULTIPLY ;
72    NX_NONASCII_keymap [MWKEY_KP_MINUS    & 0xFF] = SDLK_KP_MINUS ;
73    NX_NONASCII_keymap [MWKEY_KP_PLUS     & 0xFF] = SDLK_KP_PLUS ;
74    NX_NONASCII_keymap [MWKEY_KP_ENTER    & 0xFF] = SDLK_KP_ENTER ;
75    NX_NONASCII_keymap [MWKEY_KP_EQUALS   & 0xFF] = SDLK_KP_EQUALS ;
76
77    NX_NONASCII_keymap [MWKEY_F1          & 0xFF] = SDLK_F1 ;
78    NX_NONASCII_keymap [MWKEY_F2          & 0xFF] = SDLK_F2 ;
79    NX_NONASCII_keymap [MWKEY_F3          & 0xFF] = SDLK_F3 ;
80    NX_NONASCII_keymap [MWKEY_F4          & 0xFF] = SDLK_F4 ;
81    NX_NONASCII_keymap [MWKEY_F5          & 0xFF] = SDLK_F5 ;
82    NX_NONASCII_keymap [MWKEY_F6          & 0xFF] = SDLK_F6 ;
83    NX_NONASCII_keymap [MWKEY_F7          & 0xFF] = SDLK_F7 ;
84    NX_NONASCII_keymap [MWKEY_F8          & 0xFF] = SDLK_F8 ;
85    NX_NONASCII_keymap [MWKEY_F9          & 0xFF] = SDLK_F9 ;
86    NX_NONASCII_keymap [MWKEY_F10         & 0xFF] = SDLK_F10 ;
87    NX_NONASCII_keymap [MWKEY_F11         & 0xFF] = SDLK_F11 ;
88    NX_NONASCII_keymap [MWKEY_F12         & 0xFF] = SDLK_F12 ;
89
90    NX_NONASCII_keymap [MWKEY_NUMLOCK     & 0xFF] = SDLK_NUMLOCK ;
91    NX_NONASCII_keymap [MWKEY_CAPSLOCK    & 0xFF] = SDLK_CAPSLOCK ;
92    NX_NONASCII_keymap [MWKEY_SCROLLOCK   & 0xFF] = SDLK_SCROLLOCK ;
93    NX_NONASCII_keymap [MWKEY_LSHIFT      & 0xFF] = SDLK_LSHIFT ;
94    NX_NONASCII_keymap [MWKEY_RSHIFT      & 0xFF] = SDLK_RSHIFT ;
95    NX_NONASCII_keymap [MWKEY_LCTRL       & 0xFF] = SDLK_LCTRL ;
96    NX_NONASCII_keymap [MWKEY_RCTRL       & 0xFF] = SDLK_RCTRL ;
97    NX_NONASCII_keymap [MWKEY_LALT        & 0xFF] = SDLK_LALT ;
98    NX_NONASCII_keymap [MWKEY_RALT        & 0xFF] = SDLK_RALT ;
99    NX_NONASCII_keymap [MWKEY_LMETA       & 0xFF] = SDLK_LMETA ;
100    NX_NONASCII_keymap [MWKEY_RMETA       & 0xFF] = SDLK_RMETA ;
101    NX_NONASCII_keymap [MWKEY_ALTGR       & 0xFF] = SDLK_MODE ;
102
103    NX_NONASCII_keymap [MWKEY_PRINT       & 0xFF] = SDLK_PRINT ;
104    NX_NONASCII_keymap [MWKEY_SYSREQ      & 0xFF] = SDLK_SYSREQ ;
105    NX_NONASCII_keymap [MWKEY_PAUSE       & 0xFF] = SDLK_PAUSE ;
106    NX_NONASCII_keymap [MWKEY_BREAK       & 0xFF] = SDLK_BREAK ;
107    NX_NONASCII_keymap [MWKEY_MENU        & 0xFF] = SDLK_MENU ;
108
109    Dprintf ("leave NX_InitOSKeymap\n") ;
110}
111
112SDL_keysym * NX_TranslateKey (GR_EVENT_KEYSTROKE * keystroke, SDL_keysym * keysym)
113{
114    GR_KEY ch = keystroke -> ch ;
115
116    Dprintf ("enter NX_TranslateKey\n") ;
117
118    keysym -> scancode = keystroke -> scancode ;
119    keysym -> sym = SDLK_UNKNOWN ;
120
121    if (ch & MWKEY_NONASCII_MASK) {
122        keysym -> sym = NX_NONASCII_keymap [ch & 0xFF] ;
123    } else {
124        keysym -> sym = ch & 0x7F ;
125    }
126
127    keysym -> mod = KMOD_NONE ;
128
129#if 1   //   Retrieve more mode information
130    {
131        GR_KEYMOD   mod = keystroke -> modifiers ;
132
133        if (mod & MWKMOD_LSHIFT)
134            keysym -> mod |= KMOD_LSHIFT ;
135        if (mod & MWKMOD_RSHIFT)
136            keysym -> mod |= KMOD_RSHIFT ;
137        if (mod & MWKMOD_LCTRL)
138            keysym -> mod |= KMOD_LCTRL ;
139        if (mod & MWKMOD_RCTRL)
140            keysym -> mod |= KMOD_RCTRL ;
141        if (mod & MWKMOD_LALT)
142            keysym -> mod |= KMOD_LALT ;
143        if (mod & MWKMOD_RALT)
144            keysym -> mod |= KMOD_RALT ;
145        if (mod & MWKMOD_LMETA)
146            keysym -> mod |= KMOD_LMETA ;
147        if (mod & MWKMOD_RMETA)
148            keysym -> mod |= KMOD_RMETA ;
149        if (mod & MWKMOD_NUM)
150            keysym -> mod |= KMOD_NUM ;
151        if (mod & MWKMOD_CAPS)
152            keysym -> mod |= KMOD_CAPS ;
153        if (mod & MWKMOD_ALTGR)
154            keysym -> mod |= KMOD_MODE ;
155    }
156#endif
157
158    keysym -> unicode = ch ;
159
160    Dprintf ("leave NX_TranslateKey\n") ;
161    return keysym ;
162}
163
164static int check_boundary (_THIS, int x, int y)
165{
166    if (x < OffsetX || y < OffsetY || x > OffsetX + this -> screen -> w ||
167        y > OffsetY + this -> screen -> h)
168        return 0 ;
169
170    return 1 ;
171}
172
173void NX_PumpEvents (_THIS)
174{
175    GR_EVENT         event ;
176    static GR_BUTTON last_button_down = 0 ;
177
178    GrCheckNextEvent (& event) ;
179    while (event.type != GR_EVENT_TYPE_NONE) {
180
181        // dispatch event
182        switch (event.type) {
183            case GR_EVENT_TYPE_MOUSE_ENTER :
184            {
185                Dprintf ("mouse enter\n") ;
186                SDL_PrivateAppActive (1, SDL_APPMOUSEFOCUS) ;
187                break ;
188            }
189
190            case GR_EVENT_TYPE_MOUSE_EXIT :
191            {
192                Dprintf ("mouse exit\n") ;
193                SDL_PrivateAppActive (0, SDL_APPMOUSEFOCUS) ;
194                break ;
195            }
196
197            case GR_EVENT_TYPE_FOCUS_IN :
198            {
199                Dprintf ("focus in\n") ;
200                SDL_PrivateAppActive (1, SDL_APPINPUTFOCUS) ;
201                break ;
202            }
203
204            case GR_EVENT_TYPE_FOCUS_OUT :
205            {
206                Dprintf ("focus out\n") ;
207                SDL_PrivateAppActive (0, SDL_APPINPUTFOCUS) ;
208                break ;
209            }
210
211            case GR_EVENT_TYPE_MOUSE_MOTION :
212            {
213                Dprintf ("mouse motion\n") ;
214
215                if (SDL_VideoSurface) {
216                    if (currently_fullscreen) {
217                        if (check_boundary (this, event.button.x, event.button.y)) {
218                            SDL_PrivateMouseMotion (0, 0, event.button.x - OffsetX,
219                                event.button.y - OffsetY) ;
220                        }
221                    } else {
222                        SDL_PrivateMouseMotion (0, 0, event.button.x, event.button.y) ;
223                    }
224                }
225                break ;
226            }
227
228            case GR_EVENT_TYPE_BUTTON_DOWN :
229            {
230                int button = event.button.buttons ;
231
232                Dprintf ("button down\n") ;
233
234                switch (button) {
235                    case MWBUTTON_L :
236                        button = 1 ;
237                        break ;
238                    case MWBUTTON_M :
239                        button = 2 ;
240                        break ;
241                    case MWBUTTON_R :
242                        button = 3 ;
243                        break ;
244                    default :
245                        button = 0 ;
246                }
247                last_button_down = button ;
248
249                if (currently_fullscreen) {
250                    if (check_boundary (this, event.button.x, event.button.y)) {
251                        SDL_PrivateMouseButton (SDL_PRESSED, button,
252                            event.button.x - OffsetX, event.button.y - OffsetY) ;
253                    }
254                } else {
255                    SDL_PrivateMouseButton (SDL_PRESSED, button,
256                        event.button.x, event.button.y) ;
257                }
258                break ;
259            }
260
261            // do not konw which button is released
262            case GR_EVENT_TYPE_BUTTON_UP :
263            {
264                Dprintf ("button up\n") ;
265
266                if (currently_fullscreen) {
267                    if (check_boundary (this, event.button.x, event.button.y)) {
268                        SDL_PrivateMouseButton (SDL_RELEASED, last_button_down,
269                            event.button.x - OffsetX, event.button.y - OffsetY) ;
270                    }
271                } else {
272                    SDL_PrivateMouseButton (SDL_RELEASED, last_button_down,
273                        event.button.x, event.button.y) ;
274                }
275                last_button_down = 0 ;
276                break ;
277            }
278
279            case GR_EVENT_TYPE_KEY_DOWN :
280            {
281                SDL_keysym keysym ;
282
283                Dprintf ("key down\n") ;
284                SDL_PrivateKeyboard (SDL_PRESSED,
285                    NX_TranslateKey (& event.keystroke, & keysym)) ;
286                break ;
287            }
288
289            case GR_EVENT_TYPE_KEY_UP :
290            {
291                SDL_keysym keysym ;
292
293                Dprintf ("key up\n") ;
294                SDL_PrivateKeyboard (SDL_RELEASED,
295                    NX_TranslateKey (& event.keystroke, & keysym)) ;
296                break ;
297            }
298
299            case GR_EVENT_TYPE_CLOSE_REQ :
300            {
301                Dprintf ("close require\n") ;
302                SDL_PrivateQuit () ;
303                break ;
304            }
305
306            case GR_EVENT_TYPE_EXPOSURE :
307            {
308                Dprintf ("event_type_exposure\n") ;
309                if (SDL_VideoSurface) {
310                    NX_RefreshDisplay (this) ;//, & event.exposure) ;
311                }
312                break ;
313            }
314
315            case GR_EVENT_TYPE_UPDATE :
316            {
317                switch (event.update.utype) {
318                    case GR_UPDATE_MAP :
319                    {
320                        Dprintf ("GR_UPDATE_MAP\n") ;
321                        // If we're not active, make ourselves active
322                        if (!(SDL_GetAppState () & SDL_APPACTIVE)) {
323                            // Send an internal activate event
324                            SDL_PrivateAppActive (1, SDL_APPACTIVE) ;
325                        }
326                        if (SDL_VideoSurface) {
327                            NX_RefreshDisplay (this) ;
328                        }
329                        break ;
330                    }
331
332                    case GR_UPDATE_UNMAP :
333                    case GR_UPDATE_UNMAPTEMP :
334                    {
335                        Dprintf ("GR_UPDATE_UNMAP or GR_UPDATE_UNMAPTEMP\n") ;
336                        // If we're active, make ourselves inactive
337                        if (SDL_GetAppState () & SDL_APPACTIVE) {
338                            // Send an internal deactivate event
339                            SDL_PrivateAppActive (0, SDL_APPACTIVE | SDL_APPINPUTFOCUS) ;
340                        }
341                        break ;
342                    }
343
344                    case GR_UPDATE_SIZE :
345                    {
346                        Dprintf ("GR_UPDATE_SIZE\n") ;
347                        SDL_PrivateResize (event.update.width, event.update.height) ;
348                        break ;
349                    }
350
351                    case GR_UPDATE_MOVE :
352		    case GR_UPDATE_REPARENT :
353                    {
354                        Dprintf ("GR_UPDATE_MOVE or GR_UPDATE_REPARENT\n") ;
355#ifdef ENABLE_NANOX_DIRECT_FB
356			if (Clientfb) {
357			    /* Get current window position and fb pointer*/
358			    if (currently_fullscreen)
359				GrGetWindowFBInfo(FSwindow, &fbinfo);
360			    else
361				GrGetWindowFBInfo(SDL_Window, &fbinfo);
362			}
363#endif
364                        break ;
365                    }
366
367                    default :
368                        Dprintf ("unknown GR_EVENT_TYPE_UPDATE\n") ;
369                        break ;
370                }
371                break ;
372            }
373
374            default :
375            {
376                Dprintf ("pump event default\n") ;
377            }
378        }
379
380        GrCheckNextEvent (& event) ;
381    }
382}
383