nx_eventhandler.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /****************************************************************************
  2. * libs/libnx/nxmu/nx_eventhandler.c
  3. *
  4. * Copyright (C) 2008-2009, 2011-2013, 2017 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 <stdlib.h>
  41. #include <mqueue.h>
  42. #include <assert.h>
  43. #include <errno.h>
  44. #include <debug.h>
  45. #include <nuttx/mqueue.h>
  46. #include <nuttx/nx/nx.h>
  47. #include <nuttx/nx/nxbe.h>
  48. #include <nuttx/nx/nxmu.h>
  49. #include "nxcontext.h"
  50. /****************************************************************************
  51. * Private Functions
  52. ****************************************************************************/
  53. /****************************************************************************
  54. * Name: nx_connected
  55. *
  56. * Description:
  57. * The server has completed the connection and is ready.
  58. *
  59. ****************************************************************************/
  60. static inline void nx_connected(FAR struct nxfe_conn_s *conn)
  61. {
  62. DEBUGASSERT(conn->state == NX_CLISTATE_NOTCONNECTED);
  63. conn->state = NX_CLISTATE_CONNECTED;
  64. }
  65. /****************************************************************************
  66. * Name: nx_disconnected
  67. ****************************************************************************/
  68. static inline void nx_disconnected(FAR struct nxfe_conn_s *conn)
  69. {
  70. /* Close the server and client MQs */
  71. (void)mq_close(conn->cwrmq);
  72. (void)mq_close(conn->crdmq);
  73. /* And free the client structure */
  74. lib_ufree(conn);
  75. }
  76. /****************************************************************************
  77. * Public Functions
  78. ****************************************************************************/
  79. /****************************************************************************
  80. * Name: nx_eventhandler
  81. *
  82. * Description:
  83. * The client code must call this function periodically to process
  84. * incoming messages from the server. If CONFIG_NX_BLOCKING is defined,
  85. * then this function not return until a server message is received.
  86. *
  87. * When CONFIG_NX_BLOCKING is not defined, the client must exercise
  88. * caution in the looping to assure that it does not eat up all of
  89. * the CPU bandwidth calling nx_eventhandler repeatedly. nx_eventnotify
  90. * may be called to get a signal event whenever a new incoming server
  91. * event is available.
  92. *
  93. * Input Parameters:
  94. * handle - the handle returned by nx_connect
  95. *
  96. * Returned Value:
  97. * OK: No errors occurred. If CONFIG_NX_BLOCKING is defined, then
  98. * one or more server message was processed.
  99. * ERROR: An error occurred and errno has been set appropriately. Of
  100. * particular interest, it will return errno == EHOSTDOWN when the
  101. * server is disconnected. After that event, the handle can no
  102. * longer be used.
  103. *
  104. ****************************************************************************/
  105. int nx_eventhandler(NXHANDLE handle)
  106. {
  107. FAR struct nxfe_conn_s *conn = (FAR struct nxfe_conn_s *)handle;
  108. struct nxsvrmsg_s *msg;
  109. struct nxbe_window_s *wnd;
  110. char buffer[NX_MXCLIMSGLEN];
  111. int nbytes;
  112. /* Get the next message from our incoming message queue */
  113. do
  114. {
  115. nbytes = _MQ_RECEIVE(conn->crdmq, buffer, NX_MXCLIMSGLEN, 0);
  116. if (nbytes < 0)
  117. {
  118. int errcode = _MQ_GETERRNO(nbytes);
  119. /* EINTR is not an error. The wait was interrupted by a signal and
  120. * we just need to try reading again.
  121. */
  122. if (errcode != EINTR)
  123. {
  124. if (errcode == EAGAIN)
  125. {
  126. /* EAGAIN is not an error. It occurs because the MQ is opened with
  127. * O_NONBLOCK and there is no message available now.
  128. */
  129. return OK;
  130. }
  131. else
  132. {
  133. gerr("ERROR: _MQ_RECEIVE failed: %d\n", errcode);
  134. _MQ_SETERRNO(nbytes);
  135. return ERROR;
  136. }
  137. }
  138. }
  139. }
  140. while (nbytes < 0);
  141. DEBUGASSERT(nbytes >= sizeof(struct nxclimsg_s));
  142. /* Dispatch the message appropriately */
  143. msg = (struct nxsvrmsg_s *)buffer;
  144. ginfo("Received msgid=%d\n", msg->msgid);
  145. switch (msg->msgid)
  146. {
  147. case NX_CLIMSG_CONNECTED:
  148. nx_connected(conn);
  149. break;
  150. case NX_CLIMSG_DISCONNECTED:
  151. nx_disconnected(conn);
  152. set_errno(EHOSTDOWN);
  153. return ERROR;
  154. case NX_CLIMSG_REDRAW:
  155. {
  156. FAR struct nxclimsg_redraw_s *redraw = (FAR struct nxclimsg_redraw_s *)buffer;
  157. wnd = redraw->wnd;
  158. DEBUGASSERT(wnd);
  159. if (wnd->cb->redraw)
  160. {
  161. wnd->cb->redraw((NXWINDOW)wnd, &redraw->rect, redraw->more, wnd->arg);
  162. }
  163. }
  164. break;
  165. case NX_CLIMSG_NEWPOSITION:
  166. {
  167. FAR struct nxclimsg_newposition_s *postn = (FAR struct nxclimsg_newposition_s *)buffer;
  168. wnd = postn->wnd;
  169. DEBUGASSERT(wnd);
  170. if (wnd->cb->position)
  171. {
  172. wnd->cb->position((NXWINDOW)wnd, &postn->size, &postn->pos, &postn->bounds, wnd->arg);
  173. }
  174. }
  175. break;
  176. #ifdef CONFIG_NX_XYINPUT
  177. case NX_CLIMSG_MOUSEIN:
  178. {
  179. FAR struct nxclimsg_mousein_s *mouse = (FAR struct nxclimsg_mousein_s *)buffer;
  180. wnd = mouse->wnd;
  181. DEBUGASSERT(wnd);
  182. if (wnd->cb->mousein)
  183. {
  184. wnd->cb->mousein((NXWINDOW)wnd, &mouse->pos, mouse->buttons, wnd->arg);
  185. }
  186. }
  187. break;
  188. #endif
  189. #ifdef CONFIG_NX_KBD
  190. case NX_CLIMSG_KBDIN:
  191. {
  192. FAR struct nxclimsg_kbdin_s *kbd = (FAR struct nxclimsg_kbdin_s *)buffer;
  193. wnd = kbd->wnd;
  194. DEBUGASSERT(wnd);
  195. if (wnd->cb->kbdin)
  196. {
  197. wnd->cb->kbdin((NXWINDOW)wnd, kbd->nch, kbd->ch, wnd->arg);
  198. }
  199. }
  200. break;
  201. #endif
  202. case NX_CLIMSG_BLOCKED:
  203. {
  204. FAR struct nxclimsg_blocked_s *blocked = (FAR struct nxclimsg_blocked_s *)buffer;
  205. wnd = blocked->wnd;
  206. DEBUGASSERT(wnd);
  207. if (wnd->cb->blocked)
  208. {
  209. wnd->cb->blocked((NXWINDOW)wnd, wnd->arg, blocked->arg);
  210. }
  211. }
  212. break;
  213. default:
  214. gerr("ERROR: Unrecognized message opcode: %d\n",
  215. ((FAR struct nxsvrmsg_s *)buffer)->msgid);
  216. break;
  217. }
  218. return OK;
  219. }