ckeyboard.hxx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /****************************************************************************
  2. * apps/include/graphics/nxwm/keyboard.hxx
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. #ifndef __NXWM_INCLUDE_CKEYBOARD_HXX
  21. #define __NXWM_INCLUDE_CKEYBOARD_HXX
  22. /****************************************************************************
  23. * Included Files
  24. ****************************************************************************/
  25. #include <nuttx/nx/nxglib.h>
  26. #include <semaphore.h>
  27. #include <pthread.h>
  28. #include <nuttx/input/touchscreen.h>
  29. #include "graphics/nxwidgets/cnxserver.hxx"
  30. #include "graphics/nxwm/ccalibration.hxx"
  31. /****************************************************************************
  32. * Pre-processor Definitions
  33. ****************************************************************************/
  34. /****************************************************************************
  35. * Implementation Classes
  36. ****************************************************************************/
  37. namespace NxWM
  38. {
  39. /**
  40. * The CKeyboard class provides the the calibration window and obtains
  41. * calibration data.
  42. */
  43. class CKeyboard
  44. {
  45. private:
  46. /**
  47. * The state of the listener thread.
  48. */
  49. enum EListenerState
  50. {
  51. LISTENER_NOTRUNNING = 0, /**< The listener thread has not yet been started */
  52. LISTENER_STARTED, /**< The listener thread has been started, but is not yet running */
  53. LISTENER_RUNNING, /**< The listener thread is running normally */
  54. LISTENER_STOPREQUESTED, /**< The listener thread has been requested to stop */
  55. LISTENER_TERMINATED, /**< The listener thread terminated normally */
  56. LISTENER_FAILED /**< The listener thread terminated abnormally */
  57. };
  58. /**
  59. * CKeyboard state data
  60. */
  61. NXWidgets::CNxServer *m_server; /**< The current NX server */
  62. int m_kbdFd; /**< File descriptor of the opened keyboard device */
  63. pthread_t m_thread; /**< The listener thread ID */
  64. volatile enum EListenerState m_state; /**< The state of the listener thread */
  65. sem_t m_waitSem; /**< Used to synchronize with the listener thread */
  66. /**
  67. * Open the keyboard device. Not very interesting for the case of
  68. * standard device but much more interesting for a USB keyboard device
  69. * that may disappear when the keyboard is disconnect but later reappear
  70. * when the keyboard is reconnected. In this case, this function will
  71. * not return until the keyboard device was successfully opened (or
  72. * until an irrecoverable error occurs.
  73. *
  74. * Opens the keyboard device specified by CONFIG_NXWM_KEYBOARD_DEVPATH.
  75. *
  76. * @return On success, then method returns a valid file descriptor that
  77. * can be used to redirect stdin. A negated errno value is returned
  78. * if an irrecoverable error occurs.
  79. */
  80. inline int open(void);
  81. /**
  82. * This is the heart of the keyboard listener thread. It contains the
  83. * actual logic that listeners for and dispatches keyboard events to the
  84. * NX server.
  85. *
  86. * @return If the session terminates gracefully (i.e., because >m_state
  87. * is no longer equal to LISTENER_RUNNING, then method returns OK. A
  88. * negated errno value is returned if an error occurs while reading from
  89. * the keyboard device. A read error, depending upon the type of the
  90. * error, may simply indicate that a USB keyboard was removed and we
  91. * should wait for the keyboard to be connected.
  92. */
  93. inline int session(void);
  94. /**
  95. * The keyboard listener thread. This is the entry point of a thread
  96. * that listeners for and dispatches keyboard events to the NX server.
  97. * It simply opens the keyboard device (using CKeyboard::open()) and
  98. * executes the session (via CKeyboard::session()).
  99. *
  100. * If an errors while reading from the keyboard device AND we are
  101. * configured to use a USB keyboard, then this function will wait for
  102. * the USB keyboard to be re-connected.
  103. *
  104. * @param arg. The CKeyboard 'this' pointer cast to a void*.
  105. * @return This function normally does not return but may return NULL on
  106. * error conditions.
  107. */
  108. static FAR void *listener(FAR void *arg);
  109. public:
  110. /**
  111. * CKeyboard Constructor
  112. *
  113. * @param server. An instance of the NX server. This will be needed for
  114. * injecting mouse data.
  115. */
  116. CKeyboard(NXWidgets::CNxServer *server);
  117. /**
  118. * CKeyboard Destructor
  119. */
  120. ~CKeyboard(void);
  121. /**
  122. * Start the keyboard listener thread.
  123. *
  124. * @return True if the keyboard listener thread was correctly started.
  125. */
  126. bool start(void);
  127. };
  128. }
  129. #endif // __NXWM_INCLUDE_CKEYBOARD_HXX