nxmu_mouse.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /****************************************************************************
  2. * graphics/nxmu/nxmu__mouse.c
  3. *
  4. * Copyright (C) 2008-2009, 2011-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 <errno.h>
  41. #include <debug.h>
  42. #include <nuttx/nx/nxglib.h>
  43. #include <nuttx/nx/nx.h>
  44. #include "nxfe.h"
  45. #ifdef CONFIG_NX_XYINPUT
  46. /****************************************************************************
  47. * Pre-processor Definitions
  48. ****************************************************************************/
  49. /****************************************************************************
  50. * Private Types
  51. ****************************************************************************/
  52. /****************************************************************************
  53. * Private Data
  54. ****************************************************************************/
  55. static struct nxgl_point_s g_mpos;
  56. static struct nxgl_point_s g_mrange;
  57. static uint8_t g_mbutton;
  58. static struct nxbe_window_s *g_mwnd;
  59. /****************************************************************************
  60. * Public Data
  61. ****************************************************************************/
  62. /****************************************************************************
  63. * Private Functions
  64. ****************************************************************************/
  65. /****************************************************************************
  66. * Public Functions
  67. ****************************************************************************/
  68. /****************************************************************************
  69. * Name: nxmu_mouseinit
  70. *
  71. * Description:
  72. * Initialize with the mouse in the center of the display
  73. *
  74. ****************************************************************************/
  75. void nxmu_mouseinit(int x, int y)
  76. {
  77. g_mrange.x = x;
  78. g_mrange.y = y;
  79. g_mpos.x = x / 2;
  80. g_mpos.y = y / 2;
  81. g_mbutton = 0;
  82. }
  83. /****************************************************************************
  84. * Name: nxmu_mousereport
  85. *
  86. * Description:
  87. * Report mouse position info to the specified window
  88. *
  89. * Input Parameters:
  90. * wnd - The window to receive the mouse report
  91. *
  92. * Returned Value:
  93. * 0: Mouse report sent; >0: Mouse report not sent; <0: An error occurred
  94. *
  95. ****************************************************************************/
  96. int nxmu_mousereport(struct nxbe_window_s *wnd)
  97. {
  98. struct nxclimsg_mousein_s outmsg;
  99. /* Does this window support mouse callbacks? */
  100. if (wnd->cb->mousein)
  101. {
  102. /* Yes.. Is the mouse position visible in this window? */
  103. if (nxbe_visible(wnd, &g_mpos))
  104. {
  105. /* Yes... Convert the mouse position to window relative
  106. * coordinates and send it to the client
  107. */
  108. outmsg.msgid = NX_CLIMSG_MOUSEIN;
  109. outmsg.wnd = wnd;
  110. outmsg.buttons = g_mbutton;
  111. nxgl_vectsubtract(&outmsg.pos, &g_mpos, &wnd->bounds.pt1);
  112. return nxmu_sendclientwindow(wnd, &outmsg, sizeof(struct nxclimsg_mousein_s));
  113. }
  114. }
  115. /* No error occurred, but the mouse report was not sent */
  116. return 1;
  117. }
  118. /****************************************************************************
  119. * Name: nxmu_mousein
  120. *
  121. * Description:
  122. * New positional data has been received from the thread or interrupt
  123. * handler that manages some kind of pointing hardware. Route that
  124. * positional data to the appropriate window client.
  125. *
  126. ****************************************************************************/
  127. int nxmu_mousein(FAR struct nxfe_state_s *fe,
  128. FAR const struct nxgl_point_s *pos, int buttons)
  129. {
  130. struct nxbe_window_s *wnd;
  131. nxgl_coord_t x = pos->x;
  132. nxgl_coord_t y = pos->y;
  133. uint8_t oldbuttons;
  134. int ret;
  135. /* Clip x and y to within the bounding rectangle */
  136. if (x < 0)
  137. {
  138. x = 0;
  139. }
  140. else if (x >= g_mrange.x)
  141. {
  142. x = g_mrange.x - 1;
  143. }
  144. if (y < 0)
  145. {
  146. y = 0;
  147. }
  148. else if (y >= g_mrange.y)
  149. {
  150. y = g_mrange.y - 1;
  151. }
  152. /* Look any change in values */
  153. if (x != g_mpos.x || y != g_mpos.y || buttons != g_mbutton)
  154. {
  155. /* Update the mouse value */
  156. oldbuttons = g_mbutton;
  157. g_mpos.x = x;
  158. g_mpos.y = y;
  159. g_mbutton = buttons;
  160. /* If a button is already down, regard this as part of a mouse drag
  161. * event. Pass all the following events to the window where the drag
  162. * started in.
  163. */
  164. if (oldbuttons && g_mwnd && g_mwnd->cb->mousein)
  165. {
  166. struct nxclimsg_mousein_s outmsg;
  167. outmsg.msgid = NX_CLIMSG_MOUSEIN;
  168. outmsg.wnd = g_mwnd;
  169. outmsg.buttons = g_mbutton;
  170. nxgl_vectsubtract(&outmsg.pos, &g_mpos, &g_mwnd->bounds.pt1);
  171. return nxmu_sendclientwindow(g_mwnd, &outmsg, sizeof(struct nxclimsg_mousein_s));
  172. }
  173. /* Pick the window to receive the mouse event. Start with the top
  174. * window and go down. Stop with the first window that gets the mouse
  175. * report
  176. */
  177. for (wnd = fe->be.topwnd; wnd; wnd = wnd->below)
  178. {
  179. /* The background window normally has no callback structure (unless
  180. * a client has taken control of the background via nx_requestbkgd()).
  181. */
  182. if (wnd->cb)
  183. {
  184. ret = nxmu_mousereport(wnd);
  185. if (ret == 0)
  186. {
  187. break;
  188. }
  189. }
  190. }
  191. g_mwnd = wnd;
  192. }
  193. return OK;
  194. }
  195. #endif /* CONFIG_NX_XYINPUT */