NXWidgets  1.19
ctouchscreen.hxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/nxwm/include/ctouchscreen.hxx
3  *
4  * Copyright (C) 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, NxWidgets, nor the names of its contributors
18  * me be 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 #ifndef __INCLUDE_CTOUCHSCREEN_HXX
37 #define __INCLUDE_CTOUCHSCREEN_HXX
38 
39 /****************************************************************************
40  * Included Files
41  ****************************************************************************/
42 
43 #include <nuttx/nx/nxglib.h>
44 
45 #include <semaphore.h>
46 #include <pthread.h>
47 
48 #include <nuttx/input/touchscreen.h>
49 
50 #include "cnxserver.hxx"
51 #include "ccalibration.hxx"
52 
53 /****************************************************************************
54  * Pre-processor Definitions
55  ****************************************************************************/
56 
57 /****************************************************************************
58  * Implementation Classes
59  ****************************************************************************/
60 
61 namespace NxWM
62 {
63  /**
64  * The CTouchscreen class provides the the calibration window and obtains
65  * callibration data.
66  */
67 
69  {
70  private:
71  /**
72  * The state of the listener thread.
73  */
74 
76  {
77  LISTENER_NOTRUNNING = 0, /**< The listener thread has not yet been started */
78  LISTENER_STARTED, /**< The listener thread has been started, but is not yet running */
79  LISTENER_RUNNING, /**< The listener thread is running normally */
80  LISTENER_STOPREQUESTED, /**< The listener thread has been requested to stop */
81  LISTENER_TERMINATED, /**< The listener thread terminated normally */
82  LISTENER_FAILED /**< The listener thread terminated abnormally */
83  };
84 
85  /**
86  * CTouchscreen state data
87  */
88 
89  NXWidgets::CNxServer *m_server; /**< The current NX server */
90  int m_touchFd; /**< File descriptor of the opened touchscreen device */
91  sem_t m_waitSem; /**< Semaphore the supports waits for touchscreen data */
92  pthread_t m_thread; /**< The listener thread ID */
93  volatile enum EListenerState m_state; /**< The state of the listener thread */
94  volatile bool m_enabled; /**< True: Normal touchscreen processing */
95  volatile bool m_capture; /**< True: There is a thread waiting for raw touch data */
96  volatile bool m_calibrated; /**< True: If have calibration data */
97  struct nxgl_size_s m_windowSize; /**< The size of the physical display */
98  struct SCalibrationData m_calibData; /**< Calibration data */
99  struct touch_sample_s m_sample; /**< In normal mode, touch data is collected here */
100  struct touch_sample_s *m_touch; /**< Points to the current touch data buffer */
101 
102  /**
103  * The touchscreen listener thread. This is the entry point of a thread that
104  * listeners for and dispatches touchscreens events to the NX server.
105  *
106  * @param arg. The CTouchscreen 'this' pointer cast to a void*.
107  * @return This function normally does not return but may return NULL on
108  * error conditions.
109  */
110 
111  static FAR void *listener(FAR void *arg);
112 
113  /**
114  * Inject touchscreen data into NX as mouse intput
115  *
116  * @param sample. The buffer where data was collected.
117  */
118 
119  void handleMouseInput(struct touch_sample_s *sample);
120 
121  public:
122 
123  /**
124  * CTouchscreen Constructor
125  *
126  * @param server. An instance of the NX server. This will be needed for
127  * injecting mouse data.
128  * @param windowSize. The size of the physical window in pixels. This
129  * is needed for touchscreen scaling.
130  */
131 
132  CTouchscreen(NXWidgets::CNxServer *server, struct nxgl_size_s *windowSize);
133 
134  /**
135  * CTouchscreen Destructor
136  */
137 
138  ~CTouchscreen(void);
139 
140  /**
141  * Start the touchscreen listener thread.
142  *
143  * @return True if the touchscreen listener thread was correctly started.
144  */
145 
146  bool start(void);
147 
148  /**
149  * Enable/disable touchscreen data processing. When enabled, touchscreen events
150  * are calibrated and forwarded to the NX layer which dispatches the touchscreen
151  * events in window-relative positions to the correct NX window.
152  *
153  * When disabled, touchscreen data is not forwarded to NX, but is instead captured
154  * and made available for touchscreen calibration. The touchscreen driver is
155  * initially disabled and must be specifically enabled be begin normal processing.
156  * Normal processing also requires calibration data (see method setCalibrationData)
157  *
158  * @param capture. True enables capture mode; false disables.
159  */
160 
161  inline void setEnabled(bool enable)
162  {
163  // Set the capture flag. m_calibrated must also be set to get to normal
164  // mode where touchscreen data is forwarded to NX.
165 
166  m_enabled = enable;
167  }
168 
169  /**
170  * Is the touchscreen calibrated?
171  *
172  * @return True if the touchscreen has been calibrated.
173  */
174 
175  inline bool isCalibrated(void) const
176  {
177  return m_calibrated;
178  }
179 
180  /**
181  * Provide touchscreen calibration data. If calibration data is received (and
182  * the touchscreen is enabled), then received touchscreen data will be scaled
183  * using the calibration data and forward to the NX layer which dispatches the
184  * touchscreen events in window-relative positions to the correct NX window.
185  *
186  * @param data. A reference to the touchscreen data.
187  */
188 
189  void setCalibrationData(const struct SCalibrationData &caldata);
190 
191  /**
192  * Recover the calibration data so that it can be saved to non-volatile storage.
193  *
194  * @param data. A reference to the touchscreen data.
195  * @return True if calibration data was successfully returned.
196  */
197 
198  inline bool getCalibrationData(struct SCalibrationData &caldata) const
199  {
200  if (m_calibrated)
201  {
202  caldata = m_calibData;
203  }
204  return m_calibrated;
205  }
206 
207  /**
208  * Capture raw driver data. This method will capture mode one raw touchscreen
209  * input. The normal use of this method is for touchscreen calibration.
210  *
211  * This function is not re-entrant: There may be only one thread waiting for
212  * raw touchscreen data.
213  *
214  * @return True if the raw touchscreen data was sucessfully obtained
215  */
216 
217  bool waitRawTouchData(struct touch_sample_s *touch);
218  };
219 }
220 
221 #endif // __INCLUDE_CTOUCHSCREEN_HXX
bool getCalibrationData(struct SCalibrationData &caldata) const
volatile bool m_enabled
struct touch_sample_s * m_touch
NXWidgets::CNxServer * m_server
volatile bool m_capture
bool waitRawTouchData(struct touch_sample_s *touch)
void setEnabled(bool enable)
volatile bool m_calibrated
struct SCalibrationData m_calibData
enum EListenerState m_state
static FAR void * listener(FAR void *arg)
void handleMouseInput(struct touch_sample_s *sample)
bool isCalibrated(void) const
CTouchscreen(NXWidgets::CNxServer *server, struct nxgl_size_s *windowSize)
struct nxgl_size_s m_windowSize
void setCalibrationData(const struct SCalibrationData &caldata)
struct touch_sample_s m_sample