event.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* -*- mode: c; tab-width: 4; indent-tabs-mode: t; -*- */
  2. /* SCE CONFIDENTIAL
  3. $PSLibId$
  4. *
  5. * Copyright (C) 2010 Sony Computer Entertainment Inc.
  6. * All Rights Reserved.
  7. *
  8. */
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include <stdint.h>
  12. #include <stdlib.h>
  13. #include "event.h"
  14. /********************************************************************************************/
  15. /* prototypes */
  16. /********************************************************************************************/
  17. static void dumpEventElem(TouchEventElem* pEventElem);
  18. static int pushQueue(TouchEventElem **ppTop, TouchEventElem **ppElem, SceUInt bMerge);
  19. static TouchEventElem* popQueue(TouchEventElem **ppTop);
  20. static TouchEventElem* removeElem(TouchEventElem **ppTop, SceUInt8 id);
  21. static inline SceUInt64 getDelta(SceUInt64 cur, SceUInt64 prev);
  22. /********************************************************************************************/
  23. /* functions */
  24. /********************************************************************************************/
  25. void dumpEventElem(TouchEventElem* pEventElem)
  26. {
  27. printf("ID:%3d (%s) Pos(%4d,%4d) Delta(%4d,%4d) F(%3d) DF(%3d) DT:%lld\n",
  28. pEventElem->id,
  29. ((pEventElem->event == TOUCH_EVENT_PRESS) ? "P" :
  30. (pEventElem->event == TOUCH_EVENT_HOLD) ? "H" :
  31. (pEventElem->event == TOUCH_EVENT_MOVE) ? "M" :
  32. (pEventElem->event == TOUCH_EVENT_ABORT) ? "A" :
  33. (pEventElem->event == TOUCH_EVENT_ABORT_AND_RELEASE) ? "AR" : "R"),
  34. pEventElem->curX, pEventElem->curY,
  35. pEventElem->deltaX, pEventElem->deltaY,
  36. pEventElem->curF, pEventElem->deltaF,
  37. pEventElem->deltaT);
  38. }
  39. int pushQueue(TouchEventElem **ppTop, TouchEventElem **ppElem, SceUInt bMerge)
  40. {
  41. TouchEventElem *pTemp;
  42. if(!bMerge){
  43. (*ppElem)->pNext = NULL;
  44. }
  45. if(!*ppTop){
  46. *ppTop = *ppElem;
  47. return(0);
  48. }
  49. pTemp = *ppTop;
  50. while(pTemp->pNext){
  51. pTemp = pTemp->pNext;
  52. }
  53. pTemp->pNext = *ppElem;
  54. return(0);
  55. }
  56. TouchEventElem* popQueue(TouchEventElem **ppTop)
  57. {
  58. TouchEventElem *pTemp = NULL;
  59. if(*ppTop){
  60. pTemp = *ppTop;
  61. *ppTop = (*ppTop)->pNext;
  62. pTemp->pNext = NULL;
  63. }
  64. return(pTemp);
  65. }
  66. TouchEventElem* removeElem(TouchEventElem **ppTop, SceUInt8 id)
  67. {
  68. TouchEventElem *pPrev = NULL;
  69. TouchEventElem *pTemp = NULL;
  70. if(!*ppTop){
  71. return(NULL);
  72. }
  73. pTemp = *ppTop;
  74. pPrev = NULL;
  75. while(pTemp){
  76. if(pTemp->id == id){
  77. if(pPrev){
  78. pPrev->pNext = pTemp->pNext;
  79. }else{
  80. *ppTop = pTemp->pNext;
  81. }
  82. pTemp->pNext = NULL;
  83. return(pTemp);
  84. }
  85. pPrev = pTemp;
  86. pTemp = pTemp->pNext;
  87. }
  88. return(NULL);
  89. }
  90. static inline SceUInt64 getDelta(SceUInt64 cur, SceUInt64 prev)
  91. {
  92. SceUInt64 delta = 0;
  93. if(cur > prev){
  94. delta = cur - prev;
  95. }else{
  96. delta = UINT64_MAX - prev + cur + 1;
  97. }
  98. return(delta);
  99. }
  100. int touchEventInit(TouchEventInfo* pEventInfo)
  101. {
  102. int i;
  103. TouchEventElem* pElem;
  104. if(!pEventInfo){
  105. return(-1);
  106. }
  107. memset(pEventInfo, 0, sizeof(TouchEventInfo));
  108. pEventInfo->moveThreshold = MOVE_THRESHOLD;
  109. for(i = 0; i < MAX_EVENT; i++){
  110. pElem = &pEventInfo->eventElem[i];
  111. /*E initialize event queue */
  112. pushQueue(&pEventInfo->pFree, &pElem, 0);
  113. }
  114. return(0);
  115. }
  116. int touchEventSetMoveThreshold(TouchEventInfo* pEventInfo, SceUInt32 threshold)
  117. {
  118. if(!pEventInfo){
  119. return(-1);
  120. }
  121. pEventInfo->moveThreshold = threshold;
  122. return(0);
  123. }
  124. int touchEventUpdate(const SceTouchData* pData, TouchEventInfo* pEventInfo)
  125. {
  126. TouchEventElem* pTemp;
  127. TouchEventElem* pPress = NULL;
  128. TouchEventElem* pMove = NULL;
  129. TouchEventElem* pHold = NULL;
  130. TouchEventElem* pAbort = NULL;
  131. SceUInt i;
  132. if(!pEventInfo || !pData){
  133. return(-1);
  134. }
  135. if(pEventInfo->pRelease){
  136. /*E Push the previous "RELEASE" element to free queue. */
  137. pushQueue(&pEventInfo->pFree, &pEventInfo->pRelease, 1);
  138. pEventInfo->pRelease = NULL;
  139. }
  140. if(pEventInfo->pPress){
  141. /*E Merge the previous "PRESS" element to "HOLD" queue. */
  142. pushQueue(&pEventInfo->pHold, &pEventInfo->pPress, 1);
  143. pEventInfo->pPress = NULL;
  144. }
  145. if(pEventInfo->pMove){
  146. /*E Merge the previous "MOVE" element to "HOLD" queue. */
  147. pushQueue(&pEventInfo->pHold, &pEventInfo->pMove, 1);
  148. pEventInfo->pMove = NULL;
  149. }
  150. if(pEventInfo->pAbort){
  151. /*E Merge the previous "ABORT" element to "HOLD" queue. */
  152. pushQueue(&pEventInfo->pHold, &pEventInfo->pAbort, 1);
  153. pEventInfo->pAbort = NULL;
  154. }
  155. for(i =0; i < pData->reportNum; i++){
  156. pTemp = removeElem(&pEventInfo->pHold, pData->report[i].id);
  157. if(pTemp){
  158. /*E This touch is already pressed. */
  159. pTemp->deltaX = pData->report[i].x - pTemp->curX;
  160. pTemp->deltaY = pData->report[i].y - pTemp->curY;
  161. pTemp->deltaF = pData->report[i].force - pTemp->curF;
  162. pTemp->curX = pData->report[i].x;
  163. pTemp->curY = pData->report[i].y;
  164. pTemp->curF = pData->report[i].force;
  165. pTemp->deltaT = getDelta(pData->timeStamp, pEventInfo->updateTime);
  166. pTemp->elapsedT += pTemp->deltaT;
  167. pTemp->pNext = NULL;
  168. if((pData->status & SCE_TOUCH_STATUS_INTERCEPTED) ||
  169. (pData->report[i].info & SCE_TOUCH_REPORT_INFO_HIDE_UPPER_LAYER)){
  170. pTemp->event = TOUCH_EVENT_ABORT;
  171. /*E Push to "ABORT" queue. */
  172. pushQueue(&pAbort, &pTemp, 0);
  173. }else{
  174. if(abs(pTemp->deltaX) > pEventInfo->moveThreshold ||
  175. abs(pTemp->deltaY) > pEventInfo->moveThreshold){
  176. pTemp->event = TOUCH_EVENT_MOVE;
  177. /*E Push to "MOVE" queue. */
  178. pushQueue(&pMove, &pTemp, 0);
  179. }else{
  180. pTemp->event = TOUCH_EVENT_HOLD;
  181. /*E Push to "HOLD" queue. */
  182. pushQueue(&pHold, &pTemp, 0);
  183. }
  184. }
  185. }else{
  186. /*E This touch is new one. */
  187. pTemp = popQueue(&pEventInfo->pFree);
  188. if(!pTemp){
  189. return(-1);
  190. }
  191. pTemp->id = pData->report[i].id;
  192. pTemp->deltaX = 0;
  193. pTemp->deltaY = 0;
  194. pTemp->deltaF = 0;
  195. pTemp->curX = pData->report[i].x;
  196. pTemp->curY = pData->report[i].y;
  197. pTemp->curF = pData->report[i].force;
  198. pTemp->deltaT = 0;
  199. pTemp->elapsedT = 0;
  200. pTemp->pNext = NULL;
  201. if((pData->status & SCE_TOUCH_STATUS_INTERCEPTED) ||
  202. (pData->report[i].info & SCE_TOUCH_REPORT_INFO_HIDE_UPPER_LAYER)){
  203. pTemp->event = TOUCH_EVENT_ABORT;
  204. /*E Push to "ABORT" queue. */
  205. pushQueue(&pAbort, &pTemp, 0);
  206. }else{
  207. pTemp->event = TOUCH_EVENT_PRESS;
  208. /*E Push to "PRESS" queue. */
  209. pushQueue(&pPress, &pTemp, 0);
  210. }
  211. }
  212. }
  213. /*E This is the "RELEASE" touch. */
  214. pEventInfo->pRelease = pEventInfo->pHold;
  215. pTemp = pEventInfo->pRelease;
  216. while(pTemp){
  217. if(pData->status & SCE_TOUCH_STATUS_INTERCEPTED){
  218. pTemp->event = TOUCH_EVENT_ABORT_AND_RELEASE;
  219. }else{
  220. if(pTemp->event == TOUCH_EVENT_ABORT){
  221. pTemp->event = TOUCH_EVENT_ABORT_AND_RELEASE;
  222. }else{
  223. pTemp->event = TOUCH_EVENT_RELEASE;
  224. }
  225. }
  226. pTemp->deltaX = 0;
  227. pTemp->deltaY = 0;
  228. pTemp->deltaF = 0;
  229. pTemp->deltaT = 0;
  230. pTemp = pTemp->pNext;
  231. }
  232. pEventInfo->pMove = pMove;
  233. pEventInfo->pHold = pHold;
  234. pEventInfo->pPress = pPress;
  235. pEventInfo->pAbort = pAbort;
  236. /*E Uptates the time. */
  237. pEventInfo->updateTime = pData->timeStamp;
  238. return(0);
  239. }
  240. void touchEventDumpElemQueue(TouchEventElem* pEventElem)
  241. {
  242. TouchEventElem* pTmp = pEventElem;
  243. if(pTmp){
  244. while(pTmp){
  245. dumpEventElem(pTmp);
  246. pTmp = pTmp->pNext;
  247. }
  248. }
  249. }