PageRenderTime 25ms CodeModel.GetById 10ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/backend/rt-thread/ftk_source_input.c

http://ftk.googlecode.com/
C | 184 lines | 149 code | 29 blank | 6 comment | 13 complexity | a1b553331854e7de4af55e3fe67bf289 MD5 | raw file
  1/*
  2 * ftk_source_input.c
  3 *
  4 *  Created on: 2011-1-1
  5 *	  Author: Yi.Qiu
  6 */
  7
  8#include "ftk_log.h"
  9#include "ftk_key.h"
 10#include "ftk_event.h"
 11#include "ftk_source.h"
 12
 13typedef struct _PrivInfo
 14{
 15	int		 fd;
 16	FtkEvent event;
 17	FtkOnEvent  on_event;
 18	void*	   user_data;
 19	rt_device_t	device;
 20} PrivInfo;
 21
 22static int s_key_map[0xff] = {0};
 23
 24static void ftk_keymap_init(void)
 25{
 26	s_key_map[0x1] = FTK_KEY_ESC;
 27	s_key_map[0xc] = FTK_KEY_MINUS;
 28	s_key_map[0x39] = FTK_KEY_SPACE;
 29	s_key_map[0xd] = FTK_KEY_EQUAL;
 30	s_key_map[0xe] = FTK_KEY_BACKSPACE;
 31	s_key_map[0xf] = FTK_KEY_TAB;
 32	s_key_map[0x1c] = FTK_KEY_ENTER;
 33	s_key_map[0xb] = FTK_KEY_0;
 34	s_key_map[0x2] = FTK_KEY_1; 
 35	s_key_map[0x3] = FTK_KEY_2;
 36	s_key_map[0x4] = FTK_KEY_3;
 37	s_key_map[0x5] = FTK_KEY_4;
 38	s_key_map[0x6] = FTK_KEY_5;
 39	s_key_map[0x7] = FTK_KEY_6;
 40	s_key_map[0x8] = FTK_KEY_7;
 41	s_key_map[0x9] = FTK_KEY_8;
 42	s_key_map[0xa] = FTK_KEY_9;
 43	s_key_map[0x3b] = FTK_KEY_F1; 
 44	s_key_map[0x3c] = FTK_KEY_F2;
 45	s_key_map[0x3d] = FTK_KEY_F3;
 46	s_key_map[0x3e] = FTK_KEY_F4;
 47	s_key_map[0xef] = FTK_KEY_F5;
 48	s_key_map[0x40] = FTK_KEY_F6;
 49	s_key_map[0x41] = FTK_KEY_F7;
 50	s_key_map[0x42] = FTK_KEY_F8;
 51	s_key_map[0x43] = FTK_KEY_F9;
 52	s_key_map[0x1e] = FTK_KEY_A;
 53	s_key_map[0x30] = FTK_KEY_B;
 54	s_key_map[0x2c] = FTK_KEY_C;
 55	s_key_map[0x20] = FTK_KEY_D;
 56	s_key_map[0x12] = FTK_KEY_E;
 57	s_key_map[0x21] = FTK_KEY_F;
 58	s_key_map[0x22] = FTK_KEY_G;
 59	s_key_map[0x23] = FTK_KEY_H;
 60	s_key_map[0x17] = FTK_KEY_I;
 61	s_key_map[0x24] = FTK_KEY_J;
 62	s_key_map[0x25] = FTK_KEY_K;
 63	s_key_map[0x26] = FTK_KEY_L;
 64	s_key_map[0x32] = FTK_KEY_M;
 65	s_key_map[0x31] = FTK_KEY_N;
 66	s_key_map[0x18] = FTK_KEY_O;
 67	s_key_map[0x19] = FTK_KEY_P;
 68	s_key_map[0x10] = FTK_KEY_Q;
 69	s_key_map[0x13] = FTK_KEY_R;
 70	s_key_map[0x1f] = FTK_KEY_S;
 71	s_key_map[0x14] = FTK_KEY_T;
 72	s_key_map[0x16] = FTK_KEY_U;
 73	s_key_map[0x2f] = FTK_KEY_V;
 74	s_key_map[0x11] = FTK_KEY_W;
 75	s_key_map[0x2d] = FTK_KEY_X;
 76	s_key_map[0x15] = FTK_KEY_Y;
 77	s_key_map[0x2c] = FTK_KEY_Z;
 78	s_key_map[0x4b] = FTK_KEY_LEFT;
 79	s_key_map[0x4d] = FTK_KEY_RIGHT;
 80	s_key_map[0x50] = FTK_KEY_DOWN;
 81	s_key_map[0x2e] = FTK_KEY_DELETE;
 82	s_key_map[0x48] = FTK_KEY_UP;
 83}	
 84
 85static int ftk_source_input_get_fd(FtkSource* thiz)
 86{
 87	DECL_PRIV(thiz, priv);
 88
 89	return priv->fd;
 90}
 91
 92static int ftk_source_input_check(FtkSource* thiz)
 93{
 94	return -1;
 95}
 96
 97static Ret ftk_source_input_dispatch(FtkSource* thiz)
 98{
 99	char ch;
100	int ret = 0;
101	DECL_PRIV(thiz, priv);
102
103	while(rt_device_read(priv->device, 0, &ch, 1) == 1)
104	{
105		priv->event.type = (ch & 0x80) ? FTK_EVT_KEY_UP : FTK_EVT_KEY_DOWN;
106		priv->event.u.key.code = s_key_map[ch & 0x7F];
107		
108		if(priv->on_event != NULL && priv->event.type != FTK_EVT_NOP)
109		{
110			priv->on_event(priv->user_data, &priv->event);
111			priv->event.type = FTK_EVT_NOP;
112		}
113	}	
114	
115	return RET_OK;
116}
117
118static void ftk_source_input_destroy(FtkSource* thiz)
119{
120	if (thiz != NULL)
121	{
122		DECL_PRIV(thiz, priv);
123
124		ftk_rtthread_select_fd_free(priv->fd);
125
126		rt_device_close(priv->device);
127
128		FTK_ZFREE(thiz, sizeof(thiz) + sizeof(PrivInfo));
129	}
130}
131
132static rt_err_t ftk_source_input_rx_ind(rt_device_t dev, rt_size_t size)
133{
134	FtkSource* thiz = (FtkSource*)dev->user_data;
135	DECL_PRIV(thiz, priv);
136	
137	ftk_rtthread_set_file_readble(priv->fd);
138	
139	return RET_OK;
140}
141
142FtkSource* ftk_source_input_create(const char* filename, FtkOnEvent on_event, void* user_data)
143{
144	FtkSource* thiz = (FtkSource*)FTK_ZALLOC(sizeof(FtkSource) + sizeof(PrivInfo));
145	rt_device_t device = NULL;
146
147	if (thiz != NULL)
148	{
149		DECL_PRIV(thiz, priv);
150
151		thiz->get_fd   = ftk_source_input_get_fd;
152		thiz->check	= ftk_source_input_check;
153		thiz->dispatch = ftk_source_input_dispatch;
154		thiz->destroy  = ftk_source_input_destroy;
155
156		thiz->ref = 1;
157
158		priv->device = rt_device_find(filename); 
159		if (priv->device == NULL)
160		{
161			FTK_ZFREE(thiz, sizeof(thiz) + sizeof(PrivInfo));
162			return NULL;
163		}
164		if (rt_device_open(priv->device, RT_DEVICE_OFLAG_RDONLY) != RT_EOK)
165		{
166			FTK_ZFREE(thiz, sizeof(thiz) + sizeof(PrivInfo));
167			return NULL;
168		}
169
170		priv->device->user_data = (void *)thiz;
171		rt_device_set_rx_indicate(priv->device, ftk_source_input_rx_ind);
172
173		priv->fd = ftk_rtthread_select_fd_alloc();
174
175		priv->on_event  = on_event;
176		priv->user_data = user_data;
177
178		ftk_keymap_init();
179		ftk_logd("%s: %d=%s priv->user_data=%p\n", __func__, priv->fd, filename, priv->user_data);
180	}
181	
182	return thiz;
183}
184