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