PageRenderTime 53ms CodeModel.GetById 20ms app.highlight 16ms RepoModel.GetById 14ms app.codeStats 0ms

/apps/desktop/ftk_tester.c

http://ftk.googlecode.com/
C | 234 lines | 169 code | 36 blank | 29 comment | 50 complexity | bbddda9c79ef7761588255bcad1af6b0 MD5 | raw file
  1/*
  2 * File: ftk_tester.c    
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   event record/replay
  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-09-04 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include <byteswap.h>
 33#include "ftk_tester.h"
 34#include "ftk_file_system.h"
 35
 36static int ftk_host_is_le(void)
 37{
 38	int val = 0x11223344;
 39	unsigned char* p = (unsigned char*)&val;
 40
 41	return (*p == 0x44);
 42}
 43
 44static inline int ftk_htole32(int val)
 45{
 46	if(!ftk_host_is_le())
 47	{
 48		val = bswap_32(val);
 49	}
 50
 51	return val;
 52}
 53
 54static inline short ftk_htole16(short val)
 55{
 56	if(!ftk_host_is_le())
 57	{
 58		val = bswap_16(val);
 59	}
 60
 61	return val;
 62}
 63
 64static inline int ftk_le32toh(int val)
 65{
 66	if(!ftk_host_is_le())
 67	{
 68		val = bswap_32(val);
 69	}
 70
 71	return val;
 72}
 73
 74static inline short ftk_le16toh(short val)
 75{
 76	if(!ftk_host_is_le())
 77	{
 78		val = bswap_16(val);
 79	}
 80
 81	return val;
 82}
 83
 84static Ret ftk_event_tole(FtkEvent* event)
 85{
 86	event->time = ftk_htole32(event->time);
 87	event->type = ftk_htole32(event->type);
 88	if(event->type == FTK_EVT_KEY_DOWN || event->type == FTK_EVT_KEY_UP)
 89	{
 90		event->u.key.code = ftk_htole32(event->u.key.code);
 91	}
 92
 93	if(event->type == FTK_EVT_MOUSE_MOVE || event->type == FTK_EVT_MOUSE_DOWN 
 94		|| event->type == FTK_EVT_MOUSE_UP)
 95	{
 96		event->u.mouse.x = ftk_htole16(event->u.mouse.x);
 97		event->u.mouse.y = ftk_htole16(event->u.mouse.y);
 98	}
 99
100	return RET_OK;
101}
102
103static Ret ftk_event_fromle(FtkEvent* event)
104{
105	event->time = ftk_le32toh(event->time);
106	event->type = ftk_le32toh(event->type);
107	if(event->type == FTK_EVT_KEY_DOWN || event->type == FTK_EVT_KEY_UP)
108	{
109		event->u.key.code = ftk_le32toh(event->u.key.code);
110	}
111
112	if(event->type == FTK_EVT_MOUSE_MOVE || event->type == FTK_EVT_MOUSE_DOWN 
113		|| event->type == FTK_EVT_MOUSE_UP)
114	{
115		event->u.mouse.x = ftk_le16toh(event->u.mouse.x);
116		event->u.mouse.y = ftk_le16toh(event->u.mouse.y);
117	}
118
119	return RET_OK;
120}
121
122static Ret ftk_event_record(void* ctx, FtkEvent* event)
123{
124	FtkFsHandle handle = ctx;
125	return_val_if_fail(ctx != NULL && event != NULL, RET_OK);
126
127	if(event->type == FTK_EVT_QUIT)
128	{
129		ftk_file_close(handle);
130		ftk_wnd_manager_remove_global_listener(ftk_default_wnd_manager(), 
131			(FtkListener)ftk_event_record, handle);
132	}
133	else if(event->type == FTK_EVT_KEY_UP || event->type == FTK_EVT_KEY_DOWN 
134		|| event->type == FTK_EVT_MOUSE_DOWN || event->type == FTK_EVT_MOUSE_UP 
135		|| event->type == FTK_EVT_MOUSE_MOVE)
136	{
137		FtkEvent evt = *event;
138		evt.time = ftk_get_relative_time();
139		ftk_logd("%s: type=%d\n", __func__, event->type);
140		ftk_event_tole(&evt);
141		ftk_file_write(handle, &evt, sizeof(FtkEvent));
142	}
143
144	return RET_OK;
145}
146
147Ret ftk_tester_start_record(const char* filename)
148{
149	FtkFsHandle handle = ftk_file_open(filename, "wb+");
150	return_val_if_fail(handle != NULL, RET_FAIL);
151
152	ftk_wnd_manager_add_global_listener(ftk_default_wnd_manager(), 
153		(FtkListener)ftk_event_record, handle);
154
155	ftk_logd("%s\n", __func__);
156
157	return RET_OK;
158}
159
160typedef struct _FtkEventInjector
161{
162	FtkEvent event;
163	FtkSource* timer;
164	FtkFsHandle handle;
165}FtkEventInjector;
166
167static Ret ftk_event_inject(void* ctx)
168{
169	int len = 0;
170	Ret ret = 0;
171	time_t last_time = 0;
172	FtkEventInjector* injector = ctx;
173	return_val_if_fail(ctx != NULL, RET_REMOVE);
174	
175	if(injector->event.type == FTK_EVT_QUIT)
176	{
177		ftk_file_close(injector->handle);
178		FTK_FREE(injector);
179		ret = RET_REMOVE;
180	}
181	else
182	{
183		last_time = injector->event.time;
184		injector->event.time = ftk_get_relative_time();
185		ftk_wnd_manager_dispatch_event(ftk_default_wnd_manager(), &injector->event);
186
187		ftk_logd("%s: type=%d\n", __func__, injector->event.type);
188
189		len = ftk_file_read(injector->handle, &injector->event, sizeof(FtkEvent));
190		ftk_event_fromle(&injector->event);
191		if(len == sizeof(FtkEvent))
192		{
193			time_t interval = injector->event.time - last_time;
194			ftk_source_timer_modify(injector->timer, interval);
195			ret = RET_OK;
196		}
197		else
198		{
199			ftk_file_close(injector->handle);
200			FTK_FREE(injector);
201			ret = RET_REMOVE;
202		}
203	}
204
205	return ret;
206}
207
208Ret ftk_tester_start_play(const char* filename)
209{
210	Ret ret = RET_FAIL;
211	FtkEventInjector* injector = NULL;
212	FtkFsHandle handle = ftk_file_open(filename, "r");
213
214	ftk_logd("%s: is litteend=%d\n", __func__, ftk_host_is_le());
215	return_val_if_fail(handle != NULL, RET_FAIL);
216
217	if((injector =  FTK_ZALLOC(sizeof(FtkEventInjector))) != NULL)
218	{
219		injector->handle = handle;
220		injector->timer = ftk_source_timer_create(3000, ftk_event_inject, injector);
221		ftk_file_read(handle, &injector->event, sizeof(FtkEvent));
222		ftk_event_fromle(&injector->event);
223		ftk_main_loop_add_source(ftk_default_main_loop(), injector->timer);
224		ret = RET_OK;
225	}
226	else
227	{
228		ftk_file_close(handle);
229	}
230	ftk_logd("%s\n", __func__);
231	
232	return ret;
233}
234