1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
4 
5   This software is provided 'as-is', without any express or implied
6   warranty.  In no event will the authors be held liable for any damages
7   arising from the use of this software.
8 
9   Permission is granted to anyone to use this software for any purpose,
10   including commercial applications, and to alter it and redistribute it
11   freely, subject to the following restrictions:
12 
13   1. The origin of this software must not be misrepresented; you must not
14      claim that you wrote the original software. If you use this software
15      in a product, an acknowledgment in the product documentation would be
16      appreciated but is not required.
17   2. Altered source versions must be plainly marked as such, and must not be
18      misrepresented as being the original software.
19   3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../../SDL_internal.h"
22 
23 #if SDL_VIDEO_DRIVER_PSP
24 
25 /* Being a null driver, there's no event stream. We just define stubs for
26    most of the API. */
27 
28 #include "SDL.h"
29 #include "../../events/SDL_sysevents.h"
30 #include "../../events/SDL_events_c.h"
31 #include "../../events/SDL_keyboard_c.h"
32 #include "SDL_pspvideo.h"
33 #include "SDL_pspevents_c.h"
34 #include "SDL_keyboard.h"
35 #include "../../thread/SDL_systhread.h"
36 #include <psphprm.h>
37 
38 #ifdef PSPIRKEYB
39 #include <pspirkeyb.h>
40 #include <pspirkeyb_rawkeys.h>
41 
42 #define IRKBD_CONFIG_FILE     NULL    /* this will take ms0:/seplugins/pspirkeyb.ini */
43 
44 static int irkbd_ready = 0;
45 static SDL_Keycode keymap[256];
46 #endif
47 
48 static enum PspHprmKeys hprm = 0;
49 static SDL_sem *event_sem = NULL;
50 static SDL_Thread *thread = NULL;
51 static int running = 0;
52 static struct {
53     enum PspHprmKeys id;
54     SDL_Keycode sym;
55 } keymap_psp[] = {
56     { PSP_HPRM_PLAYPAUSE, SDLK_F10 },
57     { PSP_HPRM_FORWARD,   SDLK_F11 },
58     { PSP_HPRM_BACK,      SDLK_F12 },
59     { PSP_HPRM_VOL_UP,    SDLK_F13 },
60     { PSP_HPRM_VOL_DOWN,  SDLK_F14 },
61     { PSP_HPRM_HOLD,      SDLK_F15 }
62 };
63 
EventUpdate(void * data)64 int EventUpdate(void *data)
65 {
66     while (running) {
67                 SDL_SemWait(event_sem);
68                                 sceHprmPeekCurrentKey(&hprm);
69                 SDL_SemPost(event_sem);
70                 /* Delay 1/60th of a second */
71                 sceKernelDelayThread(1000000 / 60);
72         }
73         return 0;
74 }
75 
PSP_PumpEvents(_THIS)76 void PSP_PumpEvents(_THIS)
77 {
78     int i;
79     enum PspHprmKeys keys;
80     enum PspHprmKeys changed;
81     static enum PspHprmKeys old_keys = 0;
82     SDL_Keysym sym;
83 
84     SDL_SemWait(event_sem);
85     keys = hprm;
86     SDL_SemPost(event_sem);
87 
88     /* HPRM Keyboard */
89     changed = old_keys ^ keys;
90     old_keys = keys;
91     if(changed) {
92         for(i=0; i<sizeof(keymap_psp)/sizeof(keymap_psp[0]); i++) {
93             if(changed & keymap_psp[i].id) {
94                 sym.scancode = keymap_psp[i].id;
95                 sym.sym = keymap_psp[i].sym;
96 
97                 /* out of date
98                 SDL_PrivateKeyboard((keys & keymap_psp[i].id) ?
99                             SDL_PRESSED : SDL_RELEASED,
100                             &sym);
101         */
102                 SDL_SendKeyboardKey((keys & keymap_psp[i].id) ?
103                                     SDL_PRESSED : SDL_RELEASED, SDL_GetScancodeFromKey(keymap_psp[i].sym));
104             }
105         }
106     }
107 
108 #ifdef PSPIRKEYB
109     if (irkbd_ready) {
110             unsigned char buffer[255];
111         int i, length, count;
112         SIrKeybScanCodeData *scanData;
113 
114             if(pspIrKeybReadinput(buffer, &length) >= 0) {
115                 if((length % sizeof(SIrKeybScanCodeData)) == 0){
116                     count = length / sizeof(SIrKeybScanCodeData);
117                     for( i=0; i < count; i++ ) {
118                 unsigned char raw, pressed;
119                         scanData=(SIrKeybScanCodeData*) buffer+i;
120                         raw = scanData->raw;
121                         pressed = scanData->pressed;
122                 sym.scancode = raw;
123                 sym.sym = keymap[raw];
124                 /* not tested */
125                 /* SDL_PrivateKeyboard(pressed?SDL_PRESSED:SDL_RELEASED, &sym); */
126                 SDL_SendKeyboardKey((keys & keymap_psp[i].id) ?
127                                     SDL_PRESSED : SDL_RELEASED, SDL_GetScancodeFromKey(keymap[raw]));
128 
129                 }
130             }
131         }
132     }
133 #endif
134     sceKernelDelayThread(0);
135 
136     return;
137 }
138 
PSP_InitOSKeymap(_THIS)139 void PSP_InitOSKeymap(_THIS)
140 {
141 #ifdef PSPIRKEYB
142     int i;
143     for (i=0; i<SDL_TABLESIZE(keymap); ++i)
144         keymap[i] = SDLK_UNKNOWN;
145 
146     keymap[KEY_ESC] = SDLK_ESCAPE;
147 
148     keymap[KEY_F1] = SDLK_F1;
149     keymap[KEY_F2] = SDLK_F2;
150     keymap[KEY_F3] = SDLK_F3;
151     keymap[KEY_F4] = SDLK_F4;
152     keymap[KEY_F5] = SDLK_F5;
153     keymap[KEY_F6] = SDLK_F6;
154     keymap[KEY_F7] = SDLK_F7;
155     keymap[KEY_F8] = SDLK_F8;
156     keymap[KEY_F9] = SDLK_F9;
157     keymap[KEY_F10] = SDLK_F10;
158     keymap[KEY_F11] = SDLK_F11;
159     keymap[KEY_F12] = SDLK_F12;
160     keymap[KEY_F13] = SDLK_PRINT;
161     keymap[KEY_F14] = SDLK_PAUSE;
162 
163     keymap[KEY_GRAVE] = SDLK_BACKQUOTE;
164     keymap[KEY_1] = SDLK_1;
165     keymap[KEY_2] = SDLK_2;
166     keymap[KEY_3] = SDLK_3;
167     keymap[KEY_4] = SDLK_4;
168     keymap[KEY_5] = SDLK_5;
169     keymap[KEY_6] = SDLK_6;
170     keymap[KEY_7] = SDLK_7;
171     keymap[KEY_8] = SDLK_8;
172     keymap[KEY_9] = SDLK_9;
173     keymap[KEY_0] = SDLK_0;
174     keymap[KEY_MINUS] = SDLK_MINUS;
175     keymap[KEY_EQUAL] = SDLK_EQUALS;
176     keymap[KEY_BACKSPACE] = SDLK_BACKSPACE;
177 
178     keymap[KEY_TAB] = SDLK_TAB;
179     keymap[KEY_Q] = SDLK_q;
180     keymap[KEY_W] = SDLK_w;
181     keymap[KEY_E] = SDLK_e;
182     keymap[KEY_R] = SDLK_r;
183     keymap[KEY_T] = SDLK_t;
184     keymap[KEY_Y] = SDLK_y;
185     keymap[KEY_U] = SDLK_u;
186     keymap[KEY_I] = SDLK_i;
187     keymap[KEY_O] = SDLK_o;
188     keymap[KEY_P] = SDLK_p;
189     keymap[KEY_LEFTBRACE] = SDLK_LEFTBRACKET;
190     keymap[KEY_RIGHTBRACE] = SDLK_RIGHTBRACKET;
191     keymap[KEY_ENTER] = SDLK_RETURN;
192 
193     keymap[KEY_CAPSLOCK] = SDLK_CAPSLOCK;
194     keymap[KEY_A] = SDLK_a;
195     keymap[KEY_S] = SDLK_s;
196     keymap[KEY_D] = SDLK_d;
197     keymap[KEY_F] = SDLK_f;
198     keymap[KEY_G] = SDLK_g;
199     keymap[KEY_H] = SDLK_h;
200     keymap[KEY_J] = SDLK_j;
201     keymap[KEY_K] = SDLK_k;
202     keymap[KEY_L] = SDLK_l;
203     keymap[KEY_SEMICOLON] = SDLK_SEMICOLON;
204     keymap[KEY_APOSTROPHE] = SDLK_QUOTE;
205     keymap[KEY_BACKSLASH] = SDLK_BACKSLASH;
206 
207     keymap[KEY_Z] = SDLK_z;
208     keymap[KEY_X] = SDLK_x;
209     keymap[KEY_C] = SDLK_c;
210     keymap[KEY_V] = SDLK_v;
211     keymap[KEY_B] = SDLK_b;
212     keymap[KEY_N] = SDLK_n;
213     keymap[KEY_M] = SDLK_m;
214     keymap[KEY_COMMA] = SDLK_COMMA;
215     keymap[KEY_DOT] = SDLK_PERIOD;
216     keymap[KEY_SLASH] = SDLK_SLASH;
217 
218     keymap[KEY_SPACE] = SDLK_SPACE;
219 
220     keymap[KEY_UP] = SDLK_UP;
221     keymap[KEY_DOWN] = SDLK_DOWN;
222     keymap[KEY_LEFT] = SDLK_LEFT;
223     keymap[KEY_RIGHT] = SDLK_RIGHT;
224 
225     keymap[KEY_HOME] = SDLK_HOME;
226     keymap[KEY_END] = SDLK_END;
227     keymap[KEY_INSERT] = SDLK_INSERT;
228     keymap[KEY_DELETE] = SDLK_DELETE;
229 
230     keymap[KEY_NUMLOCK] = SDLK_NUMLOCK;
231     keymap[KEY_LEFTMETA] = SDLK_LSUPER;
232 
233     keymap[KEY_KPSLASH] = SDLK_KP_DIVIDE;
234     keymap[KEY_KPASTERISK] = SDLK_KP_MULTIPLY;
235     keymap[KEY_KPMINUS] = SDLK_KP_MINUS;
236     keymap[KEY_KPPLUS] = SDLK_KP_PLUS;
237     keymap[KEY_KPDOT] = SDLK_KP_PERIOD;
238     keymap[KEY_KPEQUAL] = SDLK_KP_EQUALS;
239 
240     keymap[KEY_LEFTCTRL] = SDLK_LCTRL;
241     keymap[KEY_RIGHTCTRL] = SDLK_RCTRL;
242     keymap[KEY_LEFTALT] = SDLK_LALT;
243     keymap[KEY_RIGHTALT] = SDLK_RALT;
244     keymap[KEY_LEFTSHIFT] = SDLK_LSHIFT;
245     keymap[KEY_RIGHTSHIFT] = SDLK_RSHIFT;
246 #endif
247 }
248 
PSP_EventInit(_THIS)249 void PSP_EventInit(_THIS)
250 {
251 #ifdef PSPIRKEYB
252     int outputmode = PSP_IRKBD_OUTPUT_MODE_SCANCODE;
253     int ret = pspIrKeybInit(IRKBD_CONFIG_FILE, 0);
254     if (ret == PSP_IRKBD_RESULT_OK) {
255             pspIrKeybOutputMode(outputmode);
256         irkbd_ready = 1;
257     } else {
258         irkbd_ready = 0;
259     }
260 #endif
261     /* Start thread to read data */
262     if((event_sem =  SDL_CreateSemaphore(1)) == NULL) {
263         SDL_SetError("Can't create input semaphore\n");
264         return;
265     }
266     running = 1;
267     if((thread = SDL_CreateThreadInternal(EventUpdate, "PSPInputThread", 4096, NULL)) == NULL) {
268         SDL_SetError("Can't create input thread\n");
269         return;
270     }
271 }
272 
PSP_EventQuit(_THIS)273 void PSP_EventQuit(_THIS)
274 {
275     running = 0;
276     SDL_WaitThread(thread, NULL);
277     SDL_DestroySemaphore(event_sem);
278 #ifdef PSPIRKEYB
279     if (irkbd_ready) {
280             pspIrKeybFinish();
281         irkbd_ready = 0;
282     }
283 #endif
284 }
285 
286 /* end of SDL_pspevents.c ... */
287 
288 #endif /* SDL_VIDEO_DRIVER_PSP */
289 
290 /* vi: set ts=4 sw=4 expandtab: */
291