/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. * History:
  26. * ================================================================
  27. * 2010-03-27 Tao Yu <yut616@gmail.com> created.
  28. *
  29. */
  30. #include <psputils.h>
  31. #include <pspkernel.h>
  32. #include <pspdebug.h>
  33. #include <pspge.h>
  34. #include <pspdebug.h>
  35. #include "ftk_key.h"
  36. #include "ftk_log.h"
  37. #include "ftk_source_psp.h"
  38. #include "ftk_display_psp.h"
  39. typedef struct _PrivInfo
  40. {
  41. int psp_keypress; /* for keyboard driver */
  42. FtkEvent event;
  43. FtkDisplay* display;
  44. FtkOnEvent on_event;
  45. void* ctx;
  46. }PrivInfo;
  47. static int ftk_source_psp_get_fd(FtkSource* thiz)
  48. {
  49. return -1;
  50. }
  51. static int ftk_source_psp_check(FtkSource* thiz)
  52. {
  53. return 0;
  54. }
  55. static Ret ftk_source_psp_event_handler(FtkSource *thiz)
  56. {
  57. DECL_PRIV(thiz, priv);
  58. if(priv->on_event != NULL && priv->event.type != FTK_EVT_NOP)
  59. {
  60. priv->on_event(priv->ctx, &priv->event);
  61. priv->event.type = FTK_EVT_NOP;
  62. }
  63. return RET_OK;
  64. }
  65. static Ret ftk_source_psp_dispatch(FtkSource* thiz)
  66. {
  67. SceCtrlData pad;
  68. int new_keypress = 0;
  69. int x = 0, y = 0;
  70. DECL_PRIV(thiz, priv);
  71. sceCtrlReadBufferPositive(&pad, 1);
  72. /* Lx/Ly is (0~255) , x/y is 480x272 */
  73. x = (pad.Lx * ftk_display_width(priv->display)) >> 8;
  74. y = (pad.Ly * ftk_display_height(priv->display)) >> 8;
  75. if(pad.Buttons & PSP_CTRL_SQUARE)
  76. {
  77. /* mouse left button */
  78. new_keypress = FTK_KEY_ENTER;
  79. }
  80. if(pad.Buttons & PSP_CTRL_CROSS)
  81. {
  82. /* mouse right button */
  83. }
  84. if(pad.Buttons & PSP_CTRL_TRIANGLE)
  85. {
  86. /* mouse mid button */
  87. }
  88. if(pad.Buttons & PSP_CTRL_CIRCLE) new_keypress = FTK_KEY_ENTER;
  89. if(pad.Buttons & PSP_CTRL_LEFT) new_keypress = FTK_KEY_LEFT;
  90. if(pad.Buttons & PSP_CTRL_RIGHT) new_keypress = FTK_KEY_RIGHT;
  91. if(pad.Buttons & PSP_CTRL_UP) new_keypress = FTK_KEY_UP;
  92. if(pad.Buttons & PSP_CTRL_DOWN) new_keypress = FTK_KEY_DOWN;
  93. if(pad.Buttons & PSP_CTRL_LTRIGGER) new_keypress = FTK_KEY_PAGEUP;
  94. if(pad.Buttons & PSP_CTRL_RTRIGGER) new_keypress = FTK_KEY_PAGEDOWN;
  95. /* if keyboard driver got a key, only give it more once that
  96. key was let go. This is just a quick hack, there's surely a
  97. better way to handle this.. */
  98. if(priv->psp_keypress < 0 && (-(priv->psp_keypress)) != new_keypress)
  99. priv->psp_keypress = 0;
  100. if(priv->psp_keypress == 0)
  101. priv->psp_keypress = new_keypress;
  102. if(priv->psp_keypress > 0)
  103. {
  104. if(priv->psp_keypress == FTK_KEY_ENTER)
  105. {
  106. if(pad.Buttons & PSP_CTRL_SQUARE)
  107. {
  108. priv->event.u.mouse.x = x;
  109. priv->event.u.mouse.y = y;
  110. priv->event.type = FTK_EVT_MOUSE_DOWN;
  111. ftk_source_psp_event_handler(thiz);
  112. priv->event.type = FTK_EVT_MOUSE_UP;
  113. }
  114. else
  115. {
  116. priv->event.u.key.code = priv->psp_keypress;
  117. priv->event.type = FTK_EVT_KEY_DOWN;
  118. ftk_source_psp_event_handler(thiz);
  119. priv->event.u.key.code = priv->psp_keypress;
  120. priv->event.type = FTK_EVT_KEY_UP;
  121. }
  122. }
  123. else
  124. {
  125. priv->event.u.key.code = priv->psp_keypress;
  126. priv->event.type = FTK_EVT_KEY_DOWN;
  127. }
  128. priv->psp_keypress = -(priv->psp_keypress);
  129. }
  130. else
  131. {
  132. priv->event.u.mouse.x = x;
  133. priv->event.u.mouse.y = y;
  134. priv->event.type = FTK_EVT_MOUSE_MOVE;
  135. }
  136. return ftk_source_psp_event_handler(thiz);
  137. }
  138. static void ftk_source_psp_destroy(FtkSource* thiz)
  139. {
  140. if(thiz != NULL)
  141. {
  142. FTK_ZFREE(thiz, sizeof(thiz) + sizeof(PrivInfo));
  143. }
  144. return;
  145. }
  146. FtkSource* ftk_source_psp_create(FtkDisplay* display, FtkOnEvent on_event, void* ctx)
  147. {
  148. FtkSource* thiz = NULL;
  149. return_val_if_fail(display != NULL && on_event != NULL, NULL);
  150. thiz = (FtkSource*)FTK_ZALLOC(sizeof(FtkSource) + sizeof(PrivInfo));
  151. if(thiz != NULL)
  152. {
  153. DECL_PRIV(thiz, priv);
  154. thiz->get_fd = ftk_source_psp_get_fd;
  155. thiz->check = ftk_source_psp_check;
  156. thiz->dispatch = ftk_source_psp_dispatch;
  157. thiz->destroy = ftk_source_psp_destroy;
  158. thiz->ref = 1;
  159. priv->ctx = ctx;
  160. priv->on_event = on_event;
  161. priv->display = display;
  162. priv->psp_keypress = 0;
  163. }
  164. return thiz;
  165. }