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