PageRenderTime 173ms CodeModel.GetById 81ms app.highlight 13ms RepoModel.GetById 77ms app.codeStats 0ms

/src/backend/psp/ftk_source_psp.c

http://ftk.googlecode.com/
C | 193 lines | 129 code | 28 blank | 36 comment | 30 complexity | 24f3f24f3313dafab181166b7e0448c3 MD5 | raw file
  1/*
  2 * File: ftk_source_psp.c	 
  3 * Author:  Tao Yu <yut616@gmail.com>
  4 * Brief:	psp event handler
  5 *
  6 * Copyright (c) 2009 - 2010  Li XianJing <xianjimli@hotmail.com>
  7 *
  8 * Licensed under the Academic Free License version 2.1
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA	 02111-1307	 USA
 23 */
 24
 25/*
 26 * History:
 27 * ================================================================
 28 * 2010-03-27 Tao Yu <yut616@gmail.com> created.
 29 *
 30 */
 31#include <psputils.h>
 32#include <pspkernel.h>
 33#include <pspdebug.h>
 34#include <pspge.h>
 35#include <pspdebug.h>
 36#include "ftk_key.h"
 37#include "ftk_log.h"
 38#include "ftk_source_psp.h"
 39#include "ftk_display_psp.h"
 40
 41typedef struct _PrivInfo
 42{
 43	int psp_keypress;  /* for keyboard driver */
 44	FtkEvent event;
 45	FtkDisplay* display;
 46	FtkOnEvent on_event;
 47	void* ctx;
 48}PrivInfo;
 49
 50static int ftk_source_psp_get_fd(FtkSource* thiz)
 51{
 52	return -1;
 53}
 54
 55static int ftk_source_psp_check(FtkSource* thiz)
 56{
 57	return 0;
 58}
 59
 60static Ret ftk_source_psp_event_handler(FtkSource *thiz)
 61{
 62	DECL_PRIV(thiz, priv);
 63	if(priv->on_event != NULL && priv->event.type != FTK_EVT_NOP)
 64	{
 65		priv->on_event(priv->ctx, &priv->event);
 66		priv->event.type = FTK_EVT_NOP;
 67	}
 68	return RET_OK;
 69}
 70
 71static Ret ftk_source_psp_dispatch(FtkSource* thiz)
 72{
 73	SceCtrlData pad;
 74	int new_keypress = 0;
 75	int x = 0, y = 0; 
 76
 77	DECL_PRIV(thiz, priv);
 78
 79	sceCtrlReadBufferPositive(&pad, 1); 
 80
 81	/* Lx/Ly is (0~255) , x/y is 480x272 */
 82	x = (pad.Lx * ftk_display_width(priv->display)) >> 8;
 83	y = (pad.Ly * ftk_display_height(priv->display)) >> 8;
 84
 85	if(pad.Buttons & PSP_CTRL_SQUARE)
 86	{
 87		/* mouse left button */
 88		new_keypress = FTK_KEY_ENTER;
 89	}
 90	if(pad.Buttons & PSP_CTRL_CROSS)
 91	{
 92		/* mouse right button */
 93	}
 94	if(pad.Buttons & PSP_CTRL_TRIANGLE)
 95	{
 96		/* mouse mid button */
 97	}
 98
 99	if(pad.Buttons & PSP_CTRL_CIRCLE)	new_keypress = FTK_KEY_ENTER;
100
101	if(pad.Buttons & PSP_CTRL_LEFT)		new_keypress = FTK_KEY_LEFT;
102	if(pad.Buttons & PSP_CTRL_RIGHT) 	new_keypress = FTK_KEY_RIGHT;
103	if(pad.Buttons & PSP_CTRL_UP) 		new_keypress = FTK_KEY_UP;
104	if(pad.Buttons & PSP_CTRL_DOWN) 	new_keypress = FTK_KEY_DOWN;
105
106	if(pad.Buttons & PSP_CTRL_LTRIGGER) new_keypress = FTK_KEY_PAGEUP;
107	if(pad.Buttons & PSP_CTRL_RTRIGGER) new_keypress = FTK_KEY_PAGEDOWN;
108
109	/* if keyboard driver got a key, only give it more once that
110	   key was let go.  This is just a quick hack, there's surely a
111	   better way to handle this.. */
112	if(priv->psp_keypress < 0 && (-(priv->psp_keypress)) != new_keypress)
113		priv->psp_keypress = 0;
114
115	if(priv->psp_keypress == 0)
116		priv->psp_keypress = new_keypress;
117	
118	if(priv->psp_keypress > 0)
119	{
120		if(priv->psp_keypress == FTK_KEY_ENTER)
121		{
122			if(pad.Buttons & PSP_CTRL_SQUARE)
123			{
124				priv->event.u.mouse.x = x;
125				priv->event.u.mouse.y = y;
126				priv->event.type = FTK_EVT_MOUSE_DOWN;
127				ftk_source_psp_event_handler(thiz);
128
129				priv->event.type = FTK_EVT_MOUSE_UP;
130			}
131			else
132			{
133				priv->event.u.key.code = priv->psp_keypress;
134				priv->event.type = FTK_EVT_KEY_DOWN;
135				ftk_source_psp_event_handler(thiz);
136
137				priv->event.u.key.code = priv->psp_keypress;
138				priv->event.type = FTK_EVT_KEY_UP;
139			}
140		}
141		else
142		{
143			priv->event.u.key.code = priv->psp_keypress;
144			priv->event.type = FTK_EVT_KEY_DOWN;
145		}
146		priv->psp_keypress = -(priv->psp_keypress);
147	}
148	else
149	{
150		priv->event.u.mouse.x = x;
151		priv->event.u.mouse.y = y;
152		priv->event.type = FTK_EVT_MOUSE_MOVE;
153	}
154	return ftk_source_psp_event_handler(thiz);
155}
156
157static void ftk_source_psp_destroy(FtkSource* thiz)
158{
159	if(thiz != NULL)
160	{
161		FTK_ZFREE(thiz, sizeof(thiz) + sizeof(PrivInfo));
162	}
163
164	return;
165}
166
167FtkSource* ftk_source_psp_create(FtkDisplay* display, FtkOnEvent on_event, void* ctx)
168{
169	FtkSource* thiz = NULL;
170	
171	return_val_if_fail(display != NULL && on_event != NULL, NULL);
172
173	thiz = (FtkSource*)FTK_ZALLOC(sizeof(FtkSource) + sizeof(PrivInfo));
174
175	if(thiz != NULL)
176	{
177		DECL_PRIV(thiz, priv);
178		thiz->get_fd   = ftk_source_psp_get_fd;
179		thiz->check	   = ftk_source_psp_check;
180		thiz->dispatch = ftk_source_psp_dispatch;
181		thiz->destroy  = ftk_source_psp_destroy;
182
183		thiz->ref = 1;
184		priv->ctx = ctx;
185		priv->on_event = on_event;
186		priv->display = display;
187
188		priv->psp_keypress = 0;
189	}
190
191	return thiz;
192}
193