* apps/graphics/nxwm/src/ctouchscreen.cxx
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
********************************************************************************************/
* Included Files
********************************************************************************************/
#include <nuttx/config.h>
#include <cinttypes>
#include <cerrno>
#include <sys/prctl.h>
#include <fcntl.h>
#include <sched.h>
#include <pthread.h>
#include <assert.h>
#include <debug.h>
#include <unistd.h>
#include <nuttx/sched.h>
#include <nuttx/nx/nxglib.h>
#include "graphics/nxwidgets/nxconfig.hxx"
#include "graphics/nxwidgets/cwidgetcontrol.hxx"
#include "graphics/nxwidgets/cgraphicsport.hxx"
#include "graphics/nxwm/nxwmconfig.hxx"
#include "graphics/nxglyphs.hxx"
#include "graphics/nxwm/ctouchscreen.hxx"
* CTouchscreen Method Implementations
********************************************************************************************/
using namespace NxWM;
* CTouchscreen Constructor
*
* @param server. An instance of the NX server. This will be needed for
* injecting touchscreen data.
* @param windowSize. The size of the physical window in pixels. This
* is needed for touchscreen scaling.
*/
CTouchscreen::CTouchscreen(NXWidgets::CNxServer *server, struct nxgl_size_s *windowSize)
{
m_server = server;
m_touchFd = -1;
m_state = LISTENER_NOTRUNNING;
m_enabled = false;
m_capture = false;
m_calibrated = false;
m_windowSize = *windowSize;
m_touch = &m_sample;
sem_init(&m_waitSem, 0, 0);
}
* CTouchscreen Destructor
*/
CTouchscreen::~CTouchscreen(void)
{
m_state = LISTENER_STOPREQUESTED;
pthread_kill(m_thread, CONFIG_NXWM_TOUCHSCREEN_SIGNO);
if (m_touchFd >= 0)
{
close(m_touchFd);
}
sem_destroy(&m_waitSem);
}
* Start the touchscreen listener thread.
*
* @return True if the touchscreen listener thread was correctly started.
*/
bool CTouchscreen::start(void)
{
pthread_attr_t attr;
_info("Starting listener\n");
pthread_attr_init(&attr);
struct sched_param param;
param.sched_priority = CONFIG_NXWM_TOUCHSCREEN_LISTENERPRIO;
pthread_attr_setschedparam(&attr, ¶m);
pthread_attr_setstacksize(&attr, CONFIG_NXWM_TOUCHSCREEN_LISTENERSTACK);
m_state = LISTENER_STARTED;
int ret = pthread_create(&m_thread, &attr, listener, (FAR void *)this);
if (ret != 0)
{
ginfo("CTouchscreen::start: pthread_create failed: %d\n", ret);
return false;
}
pthread_detach(m_thread);
while (m_state == LISTENER_STARTED)
{
sem_wait(&m_waitSem);
}
_info("Listener m_state=%d\n", (int)m_state);
return m_state == LISTENER_RUNNING;
}
* Provide touchscreen calibration data. If calibration data is received (and
* the touchscreen is enabled), then received touchscreen data will be scaled
* using the calibration data and forward to the NX layer which dispatches the
* touchscreen events in window-relative positions to the correct NX window.
*
* @param data. A reference to the touchscreen data.
*/
void CTouchscreen::setCalibrationData(const struct SCalibrationData &caldata)
{
m_calibData = caldata;
m_calibrated = true;
pthread_kill(m_thread, CONFIG_NXWM_TOUCHSCREEN_SIGNO);
}
* Capture raw driver data. This method will capture mode one raw touchscreen
* input. The normal use of this method is for touchscreen calibration.
*
* This function is not re-entrant: There may be only one thread waiting for
* raw touchscreen data.
*
* @return True if the raw touchscreen data was successfully obtained
*/
bool CTouchscreen::waitRawTouchData(struct touch_sample_s *touch)
{
_info("Capturing touch input\n");
sched_lock();
m_touch = touch;
m_capture = true;
pthread_kill(m_thread, CONFIG_NXWM_TOUCHSCREEN_SIGNO);
int ret = OK;
while (m_capture)
{
ret = sem_wait(&m_waitSem);
DEBUGASSERT(ret == 0 || errno == EINTR);
}
sched_unlock();
_info("Returning touch input: %d\n", ret);
return ret == OK;
}
* The touchscreen listener thread. This is the entry point of a thread that
* listeners for and dispatches touchscreen events to the NX server.
*
* @param arg. The CTouchscreen 'this' pointer cast to a void*.
* @return This function normally does not return but may return NULL on
* error conditions.
*/
FAR void *CTouchscreen::listener(FAR void *arg)
{
CTouchscreen *This = (CTouchscreen *)arg;
#if CONFIG_TASK_NAME_SIZE > 0
prctl(PR_SET_NAME, "CTouchScreen::listener", 0);
#endif
_info("Listener started\n");
This->m_touchFd = open(CONFIG_NXWM_TOUCHSCREEN_DEVPATH, O_RDONLY);
if (This->m_touchFd < 0)
{
gerr("ERROR Failed to open %s for reading: %d\n",
CONFIG_NXWM_TOUCHSCREEN_DEVPATH, errno);
This->m_state = LISTENER_FAILED;
sem_post(&This->m_waitSem);
return (FAR void *)0;
}
This->m_state = LISTENER_RUNNING;
sem_post(&This->m_waitSem);
while (This->m_state == LISTENER_RUNNING)
{
while ((!This->m_enabled || !This->m_calibrated) && !This->m_capture)
{
sleep(1);
}
struct touch_sample_s *sample = This->m_touch;
_info("Listening for sample %p\n", sample);
DEBUGASSERT(sample);
ssize_t nbytes = read(This->m_touchFd, sample,
sizeof(struct touch_sample_s));
if (nbytes < 0)
{
#if defined(CONFIG_DEBUG_GRAPHICS_ERROR) || defined(CONFIG_DEBUG_ASSERTIONS)
int errval = errno;
gerr("ERROR: read %s failed: %d\n",
CONFIG_NXWM_TOUCHSCREEN_DEVPATH, errval);
DEBUGASSERT(errval == EINTR);
#endif
}
else if (nbytes >= (ssize_t)sizeof(struct touch_sample_s))
{
This->handleMouseInput(sample);
}
else
{
gerr("ERROR Unexpected read size=%d, expected=%d\n",
nbytes, sizeof(struct touch_sample_s));
}
}
_info("Listener exiting\n");
This->m_state = LISTENER_TERMINATED;
return (FAR void *)0;
}
* Inject touchscreen data into NX as mouse input
*/
void CTouchscreen::handleMouseInput(struct touch_sample_s *sample)
{
_info("Touch id: %d flags: %02x x: %d y: %d h: %d w: %d pressure: %d\n",
sample->point[0].id, sample->point[0].flags, sample->point[0].x,
sample->point[0].y, sample->point[0].h, sample->point[0].w,
sample->point[0].pressure);
if (sample->npoints < 1 ||
((sample->point[0].flags & TOUCH_POS_VALID) == 0 &&
(sample->point[0].flags & TOUCH_UP) == 0))
{
return;
}
if (m_capture && sample != &m_sample)
{
m_touch = &m_sample;
m_capture = false;
sem_post(&m_waitSem);
return;
}
DEBUGASSERT(sample == &m_sample);
if (!m_enabled || !m_calibrated)
{
return;
}
uint8_t buttons;
if ((sample->point[0].flags & (TOUCH_DOWN | TOUCH_MOVE)) != 0)
{
buttons = NX_MOUSE_LEFTBUTTON;
}
else if ((sample->point[0].flags & TOUCH_UP) != 0)
{
buttons = NX_MOUSE_NOBUTTONS;
}
else
{
return;
}
nxgl_coord_t x;
nxgl_coord_t y;
if ((sample->point[0].flags & TOUCH_POS_VALID) == 0)
{
x = 0;
y = 0;
}
else
{
#ifdef CONFIG_NXWM_CALIBRATION_ANISOTROPIC
float rawX = (float)sample->point[0].x;
float rawY = (float)sample->point[0].y;
float leftX = rawY * m_calibData.left.slope + m_calibData.left.offset;
float rightX = rawY * m_calibData.right.slope + m_calibData.right.offset;
struct SCalibrationLine xLine;
xLine.slope = (float)((int)m_calibData.rightX - (int)m_calibData.leftX) / (rightX - leftX);
xLine.offset = (float)m_calibData.leftX - leftX * xLine.slope;
float topY = rawX * m_calibData.top.slope + m_calibData.top.offset;
float bottomY = rawX * m_calibData.bottom.slope + m_calibData.bottom.offset;
struct SCalibrationLine yLine;
yLine.slope = (float)((int)m_calibData.bottomY - (int)m_calibData.topY) / (bottomY - topY);
yLine.offset = (float)m_calibData.topY - topY * yLine.slope;
float scaledX = rawX * xLine.slope + xLine.offset;
float scaledY = rawY * yLine.slope + yLine.offset;
x = (nxgl_coord_t)scaledX;
y = (nxgl_coord_t)scaledY;
_info("raw: (%6.2f, %6.2f) scaled: (%6.2f, %6.2f) (%d, %d)\n",
rawX, rawY, scaledX, scaledY, x, y);
#else
uint32_t rawX = (uint32_t)sample->point[0].x;
uint32_t rawY = (uint32_t)sample->point[0].y;
b16_t scaledX = rawX * m_calibData.xSlope + m_calibData.xOffset;
b16_t scaledY = rawY * m_calibData.ySlope + m_calibData.yOffset;
int32_t bigX = b16toi(scaledX + b16HALF);
int32_t bigY = b16toi(scaledY + b16HALF);
if (bigX < 0)
{
x = 0;
}
else if (bigX >= m_windowSize.w)
{
x = m_windowSize.w - 1;
}
else
{
x = (nxgl_coord_t)bigX;
}
if (bigY < 0)
{
y = 0;
}
else if (bigY >= m_windowSize.h)
{
y = m_windowSize.h - 1;
}
else
{
y = (nxgl_coord_t)bigY;
}
_info("raw: (%" PRId32 ", %" PRId32 ") scaled: (%d, %d)\n",
rawX, rawY, x, y);
#endif
}
NXHANDLE handle = m_server->getServer();
nx_mousein(handle, x, y, buttons);
}