PageRenderTime 395ms CodeModel.GetById 181ms app.highlight 18ms RepoModel.GetById 193ms app.codeStats 0ms

/src/backend/ucosii/ftk_source_input.c

http://ftk.googlecode.com/
C | 269 lines | 205 code | 35 blank | 29 comment | 16 complexity | a6dafb8dd6f2afb3aa42c6061a77461f MD5 | raw file
  1/*
  2 * File: ftk_source_input.c    
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   source to handle /dev/input/xx
  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 * 2009-10-03 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "ftk_log.h"

 33#include "ftk_key.h"

 34#include "ftk_display.h"

 35#include "ftk_source_input.h"

 36#include "ftk_pipe.h"

 37#include "ftk_wnd_manager.h"

 38
 39typedef struct _PrivInfo
 40{
 41	int x;
 42	int y;
 43	FtkPipe *pipe;
 44	FtkEvent event;
 45	FtkOnEvent on_event;
 46	void* user_data;
 47}PrivInfo;
 48
 49static FtkPipe *s_pipe = NULL;
 50
 51static int s_key_map[0xff] = {0};
 52
 53static void ftk_keymap_init(void)
 54{
 55	s_key_map[0xBD] = FTK_KEY_MINUS;
 56	s_key_map[0x20] = FTK_KEY_SPACE;
 57	s_key_map[0xBB] = FTK_KEY_EQUAL;
 58	s_key_map[0x08] = FTK_KEY_BACKSPACE;
 59	s_key_map[0x09] = FTK_KEY_TAB;
 60	s_key_map[0xC0] = FTK_KEY_QUOTELEFT;
 61	s_key_map[0xDE] = FTK_KEY_QUOTERIGHT;
 62	s_key_map[0xDB] = FTK_KEY_BRACKETLEFT;
 63	s_key_map[0xDD] = FTK_KEY_BRACKETRIGHT;
 64	s_key_map[0x0d] = FTK_KEY_ENTER;
 65	s_key_map[0xBA] = FTK_KEY_SEMICOLON;
 66	s_key_map[0x30] = FTK_KEY_0;
 67	s_key_map[0x31] = FTK_KEY_1; 
 68	s_key_map[0x32] = FTK_KEY_2;
 69	s_key_map[0x33] = FTK_KEY_3;
 70	s_key_map[0x34] = FTK_KEY_4;
 71	s_key_map[0x35] = FTK_KEY_5;
 72	s_key_map[0x36] = FTK_KEY_6;
 73	s_key_map[0x37] = FTK_KEY_7;
 74	s_key_map[0x38] = FTK_KEY_8;
 75	s_key_map[0x39] = FTK_KEY_9;
 76	s_key_map[0x70] = FTK_KEY_F1; 
 77	s_key_map[0x71] = FTK_KEY_F2;
 78	s_key_map[0x72] = FTK_KEY_F3;
 79	s_key_map[0x73] = FTK_KEY_F4;
 80	s_key_map[0x74] = FTK_KEY_F5;
 81	s_key_map[0x75] = FTK_KEY_F6;
 82	s_key_map[0x76] = FTK_KEY_F7;
 83	s_key_map[0x77] = FTK_KEY_F8;
 84	s_key_map[0x78] = FTK_KEY_F9;
 85	s_key_map[0xBC] = FTK_KEY_COMMA;
 86	s_key_map[0xBE] = FTK_KEY_DOT;
 87	s_key_map[0xBF] = FTK_KEY_SLASH;
 88	s_key_map[0x10] = FTK_KEY_RIGHTSHIFT;
 89	s_key_map[0x11] = FTK_KEY_LEFTCTRL;
 90	s_key_map[0x14] = FTK_KEY_CAPSLOCK;
 91	s_key_map[0x40] = FTK_KEY_LEFTALT;
 92	s_key_map[0x41] = FTK_KEY_a;
 93	s_key_map[0x42] = FTK_KEY_b;
 94	s_key_map[0x43] = FTK_KEY_c;
 95	s_key_map[0x44] = FTK_KEY_d;
 96	s_key_map[0x45] = FTK_KEY_e;
 97	s_key_map[0x46] = FTK_KEY_f;
 98	s_key_map[0x47] = FTK_KEY_g;
 99	s_key_map[0x48] = FTK_KEY_h;
100	s_key_map[0x49] = FTK_KEY_i;
101	s_key_map[0x4a] = FTK_KEY_j;
102	s_key_map[0x4b] = FTK_KEY_k;
103	s_key_map[0x4c] = FTK_KEY_l;
104	s_key_map[0x4d] = FTK_KEY_m;
105	s_key_map[0x4e] = FTK_KEY_n;
106	s_key_map[0x4f] = FTK_KEY_o;
107	s_key_map[0x50] = FTK_KEY_p;
108	s_key_map[0x51] = FTK_KEY_q;
109	s_key_map[0x52] = FTK_KEY_r;
110	s_key_map[0x53] = FTK_KEY_s;
111	s_key_map[0x54] = FTK_KEY_t;
112	s_key_map[0x55] = FTK_KEY_u;
113	s_key_map[0x56] = FTK_KEY_v;
114	s_key_map[0x57] = FTK_KEY_w;
115	s_key_map[0x58] = FTK_KEY_x;
116	s_key_map[0x59] = FTK_KEY_y;
117	s_key_map[0x5a] = FTK_KEY_z;
118	s_key_map[0x21] = FTK_KEY_PAGEUP;
119	s_key_map[0x25] = FTK_KEY_LEFT;
120	s_key_map[0x27] = FTK_KEY_RIGHT;
121	s_key_map[0x24] = FTK_KEY_HOME;
122	s_key_map[0x23] = FTK_KEY_END;
123	s_key_map[0x28] = FTK_KEY_DOWN;
124	s_key_map[0x22] = FTK_KEY_PAGEDOWN;
125	s_key_map[0x2d] = FTK_KEY_INSERT;
126	s_key_map[0x2e] = FTK_KEY_DELETE;
127	s_key_map[0x26] = FTK_KEY_UP;
128
129	return;
130}
131
132int ftk_add_event(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
133{
134	if(s_pipe != NULL)
135	{
136		struct input_event ievent;
137
138		ievent.hwnd = hwnd;
139		ievent.message = message;
140		ievent.wparam = wParam;
141		ievent.lparam = lParam;
142
143		ftk_pipe_write(s_pipe, &ievent, sizeof(struct input_event));
144		return 0;
145	}
146
147	return -1;
148}
149
150static Ret ftk_on_key_event(PrivInfo* priv, int down, unsigned char vkey)
151{
152	priv->event.type = down ? FTK_EVT_KEY_DOWN : FTK_EVT_KEY_UP;
153	priv->event.u.key.code = s_key_map[vkey];
154
155	return RET_OK;
156}
157
158
159static int ftk_source_input_get_fd(FtkSource* thiz)
160{
161	DECL_PRIV(thiz, priv);
162
163	return ftk_pipe_get_read_handle(priv->pipe);
164}
165
166static int ftk_key_map(FtkSource* thiz, int key)
167{
168	return s_key_map[key] != 0 ? s_key_map[key] : key;
169}
170
171static int ftk_source_input_check(FtkSource* thiz)
172{
173	return -1;
174}
175
176static Ret ftk_source_input_dispatch(FtkSource* thiz)
177{
178	int ret = 0;
179	struct input_event ievent;
180	DECL_PRIV(thiz, priv);
181	ret = ftk_pipe_read(priv->pipe, &ievent, sizeof(ievent));
182	return_val_if_fail(ret == sizeof(ievent), RET_FAIL);
183
184	switch (ievent.message)
185	{
186	case WM_KEYDOWN:
187	case WM_KEYUP:
188		{
189			ftk_on_key_event(priv, ievent.message==WM_KEYDOWN, 0xFF & ievent.wparam);
190			break;
191		}
192	case WM_LBUTTONUP:
193	case WM_LBUTTONDOWN:
194		{
195			int xPos, yPos;
196
197			xPos = LOWORD(ievent.lparam); 
198			yPos = HIWORD(ievent.lparam); 
199			priv->event.u.mouse.x = xPos;
200			priv->event.u.mouse.y = yPos;
201			priv->event.type = ievent.message == WM_LBUTTONUP? FTK_EVT_MOUSE_UP : FTK_EVT_MOUSE_DOWN;
202
203			break;
204		}
205	case WM_MOUSEMOVE:
206		{
207			int xPos = LOWORD(ievent.lparam); 
208			int yPos = HIWORD(ievent.lparam); 
209			priv->event.u.mouse.x = xPos;
210			priv->event.u.mouse.y = yPos;
211			priv->event.type = FTK_EVT_MOUSE_MOVE;
212
213			break;
214		}
215	default:
216		break;
217	}
218
219	if(priv->on_event != NULL && priv->event.type != FTK_EVT_NOP)
220	{
221		priv->on_event(priv->user_data, &priv->event);
222		priv->event.type = FTK_EVT_NOP;
223	}
224
225	return RET_OK;
226}
227
228static void ftk_source_input_destroy(FtkSource* thiz)
229{
230	if(thiz != NULL)
231	{
232		DECL_PRIV(thiz, priv);
233
234		ftk_pipe_destroy(priv->pipe);
235		FTK_ZFREE(thiz, sizeof(thiz) + sizeof(PrivInfo));
236
237		s_pipe = NULL;
238	}
239
240	return;
241}
242
243FtkSource* ftk_source_input_create(FtkOnEvent on_event, void* user_data)
244{
245	FtkSource* thiz = (FtkSource*)FTK_ZALLOC(sizeof(FtkSource) + sizeof(PrivInfo));
246
247	if(thiz != NULL)
248	{
249		DECL_PRIV(thiz, priv);
250		thiz->get_fd   = ftk_source_input_get_fd;
251		thiz->check    = ftk_source_input_check;
252		thiz->dispatch = ftk_source_input_dispatch;
253		thiz->destroy  = ftk_source_input_destroy;
254
255		s_pipe = priv->pipe = ftk_pipe_create();
256		if(priv->pipe == NULL)
257		{
258			FTK_ZFREE(thiz, sizeof(thiz) + sizeof(PrivInfo));
259			return NULL;
260		}
261
262		thiz->ref = 1;
263		priv->on_event = on_event;
264		priv->user_data = user_data;
265	}
266
267	return thiz;
268}
269