[libsdl] Initial Android port of famous libSDL
[0xdroid:external_libsdl-12.git] / src / video / android / SDL_androidevents.cpp
1 /*
2     SDL - Simple DirectMedia Layer
3     Copyright (C) 1997-2006 Sam Lantinga
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19     Sam Lantinga
20     slouken@libsdl.org
21 */
22 #include "SDL_config.h"
23
24 /* Being a android driver, there's no event stream. We just define stubs for
25    most of the API. */
26
27 #include "SDL.h"
28 extern "C" {
29 #include "../../events/SDL_sysevents.h"
30 #include "../../events/SDL_events_c.h"
31 }
32
33 #include "SDL_androidvideo.h"
34 #include "SDL_androidevents_c.h"
35
36 #include "utils/threads.h"
37 #include "ui/EventHub.h"
38 #include "ui/KeycodeLabels.h"
39
40 extern "C" {
41
42 #define MAX_KEYCODE 128
43 static SDLKey keymap[MAX_KEYCODE];
44 static char key_status_buf[2][SDLK_LAST];
45 static char *key_status;
46 static int key_status_index;
47 static Mutex mu;
48
49 void ANDROID_PumpEvents(_THIS)
50 {
51     Mutex::Autolock lock(&mu);
52
53     char* prev_key_status = key_status_buf[key_status_index];
54     key_status_index ^= 1;
55
56     for (int i = 1 /* skip SDLK_UNKNOWN */; i < SDLK_LAST; i++) {
57         if (key_status[i] != prev_key_status[i]) {
58             int state;
59             if (prev_key_status[i]) {
60                 state = SDL_RELEASED;
61             }
62             else {
63                 state = SDL_PRESSED;
64             }
65             SDL_keysym keysym;
66             keysym.scancode = 0; /* TODO: fill the scancode */
67             keysym.sym = (SDLKey)i;
68             keysym.mod = (SDLMod)0; /* TODO: fill modifiers */
69             keysym.unicode = 0; /* TODO: unicode??? */
70             SDL_PrivateKeyboard(state, &keysym);
71         }
72     }
73     memcpy(prev_key_status, key_status, sizeof(key_status_buf[0]));
74
75     key_status = prev_key_status;
76 }
77
78 static int runEventLoop(void* dummy) {
79     sp<EventHub> hub = new EventHub;
80     while (1) {
81         int32_t deviceId;
82         int32_t type;
83         int32_t scancode, keycode;
84         uint32_t flags;
85         int32_t value;
86         nsecs_t when;
87         bool res = hub->getEvent(&deviceId, &type, &scancode, &keycode,
88                                  &flags, &value, &when);
89         /*
90         printf("id=%d type=%d scan=%d key=%d "
91                "flag=%d val=%d when=%d res=%d\n",
92                deviceId, type, scancode, keycode,
93                flags, value, when, res);
94         */
95         Mutex::Autolock lock(&mu);
96         if (type & EventHub::CLASS_KEYBOARD) {
97             if (keycode >= MAX_KEYCODE || keycode < 0) {
98                 continue;
99             }
100             SDLKey key = keymap[keycode];
101             key_status[key] = value;
102         }
103     }
104     return 0;
105 }
106
107 void ANDROID_InitOSKeymap(_THIS)
108 {
109     memset(keymap, 0, sizeof(keymap));
110     keymap[kKeyCodeUnknown] = SDLK_UNKNOWN;
111     keymap[kKeyCodeSoftLeft] = SDLK_LEFT;
112     keymap[kKeyCodeSoftRight] = SDLK_RIGHT;
113     keymap[kKeyCodeHome] = SDLK_HOME;
114     keymap[kKeyCodeBack] = SDLK_BACKSPACE;
115     keymap[kKeyCodeCall] = SDLK_RETURN;
116     keymap[kKeyCodeEndCall] = SDLK_ESCAPE;
117     keymap[kKeyCode0] = SDLK_0;
118     keymap[kKeyCode1] = SDLK_1;
119     keymap[kKeyCode2] = SDLK_2;
120     keymap[kKeyCode3] = SDLK_3;
121     keymap[kKeyCode4] = SDLK_4;
122     keymap[kKeyCode5] = SDLK_5;
123     keymap[kKeyCode6] = SDLK_6;
124     keymap[kKeyCode7] = SDLK_7;
125     keymap[kKeyCode8] = SDLK_8;
126     keymap[kKeyCode9] = SDLK_9;
127     keymap[kKeyCodeStar] = SDLK_ASTERISK; /* is star asterisk? */
128     keymap[kKeyCodePound] = SDLK_EURO; /* pound? */
129     keymap[kKeyCodeDpadUp] = SDLK_UP;
130     keymap[kKeyCodeDpadDown] = SDLK_DOWN;
131     keymap[kKeyCodeDpadLeft] = SDLK_LEFT;
132     keymap[kKeyCodeDpadRight] = SDLK_RIGHT;
133     keymap[kKeyCodeDpadCenter] = SDLK_WORLD_0; /* center? */
134     keymap[kKeyCodeVolumeUp] = SDLK_WORLD_1; /* volume up? */
135     keymap[kKeyCodeVolumeDown] = SDLK_WORLD_2; /* volume down? */
136     keymap[kKeyCodePower] = SDLK_POWER;
137     keymap[kKeyCodeCamera] = SDLK_WORLD_3; /* camera? */
138     keymap[kKeyCodeClear] = SDLK_CLEAR;
139     keymap[kKeyCodeA] = SDLK_a;
140     keymap[kKeyCodeB] = SDLK_b;
141     keymap[kKeyCodeC] = SDLK_c;
142     keymap[kKeyCodeD] = SDLK_d;
143     keymap[kKeyCodeE] = SDLK_e;
144     keymap[kKeyCodeF] = SDLK_f;
145     keymap[kKeyCodeG] = SDLK_g;
146     keymap[kKeyCodeH] = SDLK_h;
147     keymap[kKeyCodeI] = SDLK_i;
148     keymap[kKeyCodeJ] = SDLK_j;
149     keymap[kKeyCodeK] = SDLK_k;
150     keymap[kKeyCodeL] = SDLK_l;
151     keymap[kKeyCodeM] = SDLK_m;
152     keymap[kKeyCodeN] = SDLK_n;
153     keymap[kKeyCodeO] = SDLK_o;
154     keymap[kKeyCodeP] = SDLK_p;
155     keymap[kKeyCodeQ] = SDLK_q;
156     keymap[kKeyCodeR] = SDLK_r;
157     keymap[kKeyCodeS] = SDLK_s;
158     keymap[kKeyCodeT] = SDLK_t;
159     keymap[kKeyCodeU] = SDLK_u;
160     keymap[kKeyCodeV] = SDLK_v;
161     keymap[kKeyCodeW] = SDLK_w;
162     keymap[kKeyCodeX] = SDLK_x;
163     keymap[kKeyCodeY] = SDLK_y;
164     keymap[kKeyCodeZ] = SDLK_z;
165     keymap[kKeyCodeComma] = SDLK_COMMA;
166     keymap[kKeyCodePeriod] = SDLK_PERIOD;
167     keymap[kKeyCodeAltLeft] = SDLK_LEFT;
168     keymap[kKeyCodeAltRight] = SDLK_RIGHT;
169     keymap[kKeyCodeShiftLeft] = SDLK_LEFT;
170     keymap[kKeyCodeShiftRight] = SDLK_RIGHT;
171     keymap[kKeyCodeTab] = SDLK_TAB;
172     keymap[kKeyCodeSpace] = SDLK_SPACE;
173     keymap[kKeyCodeSym] = SDLK_WORLD_4; /* sym? */
174     keymap[kKeyCodeExplorer] = SDLK_WORLD_5; /* explore? */
175     keymap[kKeyCodeEnvelope] = SDLK_WORLD_6; /* envelope? */
176     keymap[kKeyCodeNewline] = SDLK_RETURN;
177     keymap[kKeyCodeDel] = SDLK_DELETE;
178     keymap[kKeyCodeGrave] = SDLK_BACKQUOTE; /* grave? */
179     keymap[kKeyCodeMinus] = SDLK_MINUS;
180     keymap[kKeyCodeEquals] = SDLK_EQUALS;
181     keymap[kKeyCodeLeftBracket] = SDLK_LEFTBRACKET;
182     keymap[kKeyCodeRightBracket] = SDLK_RIGHTBRACKET;
183     keymap[kKeyCodeBackslash] = SDLK_BACKSLASH;
184     keymap[kKeyCodeSemicolon] = SDLK_SEMICOLON;
185     keymap[kKeyCodeApostrophe] = SDLK_QUOTE;
186     keymap[kKeyCodeSlash] = SDLK_SLASH;
187     keymap[kKeyCodeAt] = SDLK_AT;
188     keymap[kKeyCodeNum] = SDLK_NUMLOCK; /* num? */
189     keymap[kKeyCodeHeadSetHook] = SDLK_WORLD_7; /* head set hook? */
190     keymap[kKeyCodeFocus] = SDLK_WORLD_8; /* focus? */
191     keymap[kKeyCodePlus] = SDLK_PLUS;
192     keymap[kKeyCodeMenu] = SDLK_MENU;
193     keymap[kKeyCodeNotification] = SDLK_WORLD_9; /* notification? */
194     keymap[kKeyCodeSearch] = SDLK_WORLD_10; /* search? */
195     keymap[kKeyCodePlayPause] = SDLK_PAUSE; /* play pause? */
196     keymap[kKeyCodeStop] = SDLK_WORLD_11; /* stop? */
197     keymap[kKeyCodeNextSong] = SDLK_WORLD_12; /* next song? */
198     keymap[kKeyCodePreviousSong] = SDLK_WORLD_13; /* previous song? */
199     keymap[kKeyCodeRewind] = SDLK_WORLD_14; /* rewind? */
200     keymap[kKeyCodeForward] = SDLK_WORLD_15; /* forward? */
201
202     memset(key_status_buf, 0, sizeof(key_status_buf));
203
204     key_status = key_status_buf[key_status_index];
205     key_status_index ^= 1;
206
207     androidCreateThread(&runEventLoop, NULL);
208 }
209
210 }
211
212 /* end of SDL_androidevents.c ... */
213