nxtk_events.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /****************************************************************************
  2. * graphics/nxtk/nxtk_events.c
  3. *
  4. * Copyright (C) 2008-2009, 2012 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <stdint.h>
  40. #include <stdbool.h>
  41. #include <stdlib.h>
  42. #include <semaphore.h>
  43. #include <debug.h>
  44. #include <errno.h>
  45. #include <assert.h>
  46. #include <nuttx/nx/nx.h>
  47. #include "nxtk_internal.h"
  48. /****************************************************************************
  49. * Definitions
  50. ****************************************************************************/
  51. /****************************************************************************
  52. * Private Types
  53. ****************************************************************************/
  54. /****************************************************************************
  55. * Private Function Prototypes
  56. ****************************************************************************/
  57. static void nxtk_redraw(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
  58. bool morem, FAR void *arg);
  59. static void nxtk_position(NXWINDOW hwnd, FAR const struct nxgl_size_s *size,
  60. FAR const struct nxgl_point_s *pos,
  61. FAR const struct nxgl_rect_s *bounds,
  62. FAR void *arg);
  63. #ifdef CONFIG_NX_MOUSE
  64. static void nxtk_mousein(NXWINDOW hwnd, FAR const struct nxgl_point_s *pos,
  65. uint8_t buttons, FAR void *arg);
  66. #endif
  67. #ifdef CONFIG_NX_KBD
  68. static void nxtk_kbdin(NXWINDOW hwnd, uint8_t nch, const uint8_t *ch,
  69. FAR void *arg);
  70. #endif
  71. #ifdef CONFIG_NX_MULTIUSER
  72. static void nxtk_blocked(NXWINDOW hwnd, FAR void *arg1, FAR void *arg2);
  73. #endif
  74. /****************************************************************************
  75. * Private Data
  76. ****************************************************************************/
  77. /****************************************************************************
  78. * Public Data
  79. ****************************************************************************/
  80. const struct nx_callback_s g_nxtkcb =
  81. {
  82. nxtk_redraw, /* redraw */
  83. nxtk_position /* position */
  84. #ifdef CONFIG_NX_MOUSE
  85. , nxtk_mousein /* mousein */
  86. #endif
  87. #ifdef CONFIG_NX_KBD
  88. , nxtk_kbdin /* kbdin */
  89. #endif
  90. #ifdef CONFIG_NX_MULTIUSER
  91. , nxtk_blocked
  92. #endif
  93. };
  94. /****************************************************************************
  95. * Private Functions
  96. ****************************************************************************/
  97. /****************************************************************************
  98. * Name: nxtk_redraw
  99. ****************************************************************************/
  100. static void nxtk_redraw(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
  101. bool more, FAR void *arg)
  102. {
  103. FAR struct nxtk_framedwindow_s *fwnd = (FAR struct nxtk_framedwindow_s *)hwnd;
  104. struct nxgl_rect_s intersection;
  105. DEBUGASSERT(hwnd && rect && fwnd->fwcb);
  106. gvdbg("hwnd=%p rect={(%d,%d),(%d,%d)} more=%d\n",
  107. hwnd, rect->pt1.x, rect->pt1.y, rect->pt2.x, rect->pt2.y, more);
  108. /* The incoming rectangle (rect) is relative to the containing window
  109. * (i.e., (0,0) is the top left corner of the outer, containing window).
  110. * If any part of the rectangle overlaps the client sub-window region, then
  111. * forward the redraw callback.
  112. */
  113. if (fwnd->fwcb->redraw)
  114. {
  115. /* Clip the redraw rectangle so that it lies within the client sub-window
  116. * bounds and move the rectangle to that it is relative to the client
  117. * sub-window (i.e., (0,0) is the top left corner of the client sub-window).
  118. */
  119. nxtk_containerclip(fwnd, &intersection, rect, &fwnd->fwrect);
  120. gvdbg("fwrect intersection={(%d,%d),(%d,%d)}\n",
  121. intersection.pt1.x, intersection.pt1.y,
  122. intersection.pt2.x, intersection.pt2.y);
  123. if (!nxgl_nullrect(&intersection))
  124. {
  125. fwnd->fwcb->redraw((NXTKWINDOW)fwnd, &intersection, false, fwnd->fwarg);
  126. }
  127. }
  128. /* If any part of the rectangle overlaps the client toolbar region, then
  129. * forward the redraw callback.
  130. */
  131. if (fwnd->tbcb && fwnd->tbcb->redraw)
  132. {
  133. /* Clip the redraw rectangle so that it lies within the toolbar sub-window
  134. * bounds and move the rectangle to that it is relative to the toolbar
  135. * sub-window (i.e., (0,0) is the top left corner of the client sub-window).
  136. */
  137. nxtk_containerclip(fwnd, &intersection, rect, &fwnd->tbrect);
  138. gvdbg("tbrect intersection={(%d,%d),(%d,%d)}\n",
  139. intersection.pt1.x, intersection.pt1.y,
  140. intersection.pt2.x, intersection.pt2.y);
  141. if (!nxgl_nullrect(&intersection))
  142. {
  143. fwnd->tbcb->redraw((NXTKWINDOW)fwnd, &intersection, false, fwnd->tbarg);
  144. }
  145. }
  146. /* Then draw the frame */
  147. nxtk_drawframe(fwnd, rect);
  148. }
  149. /****************************************************************************
  150. * Name: nxtk_position
  151. ****************************************************************************/
  152. static void nxtk_position(NXWINDOW hwnd, FAR const struct nxgl_size_s *size,
  153. FAR const struct nxgl_point_s *pos,
  154. FAR const struct nxgl_rect_s *bounds,
  155. FAR void *arg)
  156. {
  157. FAR struct nxtk_framedwindow_s *fwnd = (FAR struct nxtk_framedwindow_s *)hwnd;
  158. struct nxgl_size_s subwindowsize;
  159. gvdbg("hwnd=%p size=(%d,%d) pos=(%d,%d) bounds={(%d,%d),(%d,%d)}\n",
  160. hwnd, size->w, size->h, pos->x, pos->y,
  161. bounds->pt1.x, bounds->pt1.y, bounds->pt2.x, bounds->pt2.y);
  162. /* Recalculate the dimensions of the toolbar and client windows */
  163. nxtk_setsubwindows(fwnd);
  164. /* Report the size / position of the client sub-window */
  165. if (fwnd->fwcb->position)
  166. {
  167. nxgl_rectsize(&subwindowsize, &fwnd->fwrect);
  168. fwnd->fwcb->position((NXTKWINDOW)fwnd, &subwindowsize,
  169. &fwnd->fwrect.pt1, bounds, fwnd->fwarg);
  170. }
  171. /* Report the size / position of the toolbar sub-window */
  172. if (fwnd->tbcb && fwnd->tbcb->position)
  173. {
  174. nxgl_rectsize(&subwindowsize, &fwnd->tbrect);
  175. fwnd->tbcb->position((NXTKWINDOW)fwnd, &subwindowsize,
  176. &fwnd->tbrect.pt1, bounds, fwnd->tbarg);
  177. }
  178. }
  179. /****************************************************************************
  180. * Name: nxtk_mousein
  181. ****************************************************************************/
  182. #ifdef CONFIG_NX_MOUSE
  183. static void nxtk_mousein(NXWINDOW hwnd, FAR const struct nxgl_point_s *pos,
  184. uint8_t buttons, FAR void *arg)
  185. {
  186. FAR struct nxtk_framedwindow_s *fwnd = (FAR struct nxtk_framedwindow_s *)hwnd;
  187. struct nxgl_point_s abspos;
  188. struct nxgl_point_s relpos;
  189. /* Raise the window to the top if any mouse button was pressed or if auto-raise
  190. * is configured. Do this before reporting the mouse event (because processing
  191. * of the mouse event could change the ordering again).
  192. */
  193. /* REVISIT: This does not work correctly. In a scenario where (1) there are
  194. * multiple queued touchscreen events and (2) the result of the first input
  195. * was to switch windows, then this autoraise implementation will cause the
  196. * window to revert to the previous window. Not good behavior.
  197. */
  198. #ifndef CONFIG_NX_MULTIUSER /* Queuing only happens in multi-user mode */
  199. #ifdef CONFIG_NXTK_AUTORAISE
  200. if (fwnd->wnd.above != NULL)
  201. #else
  202. if (buttons != 0 && fwnd->wnd.above != NULL)
  203. #endif
  204. {
  205. nx_raise((NXWINDOW)&fwnd->wnd);
  206. }
  207. #endif
  208. /* When we get here, the mouse position that we receive has already been
  209. * offset by the window origin. Here we need to detect mouse events in
  210. * the various regions of the windows: The toolbar, the client window,
  211. * or the frame. And then offset the position accordingly.
  212. */
  213. /* The fwrect and tbrect boxes are both in absolute display coordinates. So
  214. * the easiest thing to do is to restore the mouse position to absolute
  215. * display coordiantes before making the comparisons and adjustments.
  216. */
  217. nxgl_vectoradd(&abspos, pos, &fwnd->wnd.bounds.pt1);
  218. /* In order to deliver mouse release events to the same window where the
  219. * mouse down event happened, we store the initial mouse down location.
  220. */
  221. if (fwnd->mbutton == 0 && buttons != 0)
  222. {
  223. fwnd->mpos = abspos;
  224. }
  225. fwnd->mbutton = buttons;
  226. /* Is the mouse position inside of the client window region? */
  227. if (fwnd->fwcb->mousein && nxgl_rectinside(&fwnd->fwrect, &fwnd->mpos))
  228. {
  229. nxgl_vectsubtract(&relpos, &abspos, &fwnd->fwrect.pt1);
  230. fwnd->fwcb->mousein((NXTKWINDOW)fwnd, &relpos, buttons, fwnd->fwarg);
  231. }
  232. /* If the mouse position inside the toobar region? */
  233. else if (fwnd->tbcb->mousein && nxgl_rectinside(&fwnd->tbrect, &fwnd->mpos))
  234. {
  235. nxgl_vectsubtract(&relpos, &abspos, &fwnd->tbrect.pt1);
  236. fwnd->tbcb->mousein((NXTKWINDOW)fwnd, &relpos, buttons, fwnd->tbarg);
  237. }
  238. }
  239. #endif
  240. /****************************************************************************
  241. * Name: nxtk_kbdin
  242. ****************************************************************************/
  243. #ifdef CONFIG_NX_KBD
  244. static void nxtk_kbdin(NXWINDOW hwnd, uint8_t nch, const uint8_t *ch,
  245. FAR void *arg)
  246. {
  247. FAR struct nxtk_framedwindow_s *fwnd = (FAR struct nxtk_framedwindow_s *)hwnd;
  248. /* Only the client window gets keyboard input */
  249. if (fwnd->fwcb->kbdin)
  250. {
  251. fwnd->fwcb->kbdin((NXTKWINDOW)fwnd, nch, ch, fwnd->fwarg);
  252. }
  253. }
  254. #endif
  255. /****************************************************************************
  256. * Name: nxtk_blocked
  257. ****************************************************************************/
  258. #ifdef CONFIG_NX_MULTIUSER
  259. static void nxtk_blocked(NXWINDOW hwnd, FAR void *arg1, FAR void *arg2)
  260. {
  261. FAR struct nxtk_framedwindow_s *fwnd = (FAR struct nxtk_framedwindow_s *)hwnd;
  262. /* Only the client window gets keyboard input */
  263. if (fwnd->fwcb->blocked)
  264. {
  265. fwnd->fwcb->blocked((NXTKWINDOW)fwnd, fwnd->fwarg, arg2);
  266. }
  267. }
  268. #endif
  269. /****************************************************************************
  270. * Public Functions
  271. ****************************************************************************/