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