1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
/*
* tslib/plugins/pthres.c
*
* Copyright (C) 2016 Martin Kepplinger <martin.kepplinger@ginzinger.com>
* Copyright (C) 2003 Texas Instruments, Inc.
*
* Based on:
* tslib/plugins/linear.c
* Copyright (C) 2001 Russell King.
*
* This file is placed under the LGPL. Please see the file
* COPYING for more details.
*
* SPDX-License-Identifier: LGPL-2.1
*
*
* Ensure a release is always pressure 0, and that a press is always >=1.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include "config.h"
#include "tslib.h"
#include "tslib-filter.h"
struct tslib_pthres {
struct tslib_module_info module;
unsigned int pmin;
unsigned int pmax;
int *xsave;
int *ysave;
int *press;
int current_max_slots;
};
static int pthres_read(struct tslib_module_info *info, struct ts_sample *samp,
int nr_samples)
{
struct tslib_pthres *p = (struct tslib_pthres *)info;
int ret;
static int xsave, ysave;
static int press;
ret = info->next->ops->read(info->next, samp, nr_samples);
if (ret >= 0) {
int nr = 0, i;
struct ts_sample *s;
for (s = samp, i = 0; i < ret; i++, s++) {
if (s->pressure < p->pmin) {
if (press != 0) {
/* release */
press = 0;
s->pressure = 0;
s->x = xsave;
s->y = ysave;
} else {
/* release with no press,
* outside bounds, dropping
*/
int left = ret - nr - 1;
if (left > 0) {
memmove(s,
s + 1,
left * sizeof(struct ts_sample));
s--;
continue;
}
break;
}
} else {
if (s->pressure > p->pmax) {
/* pressure outside bounds, dropping */
int left = ret - nr - 1;
if (left > 0) {
memmove(s,
s + 1,
left * sizeof(struct ts_sample));
s--;
continue;
}
break;
}
/* press */
press = 1;
xsave = s->x;
ysave = s->y;
}
nr++;
}
return nr;
}
return ret;
}
static int pthres_read_mt(struct tslib_module_info *info,
struct ts_sample_mt **samp, int max_slots,
int nr_samples)
{
struct tslib_pthres *p = (struct tslib_pthres *)info;
int ret;
int i, j;
if (p->xsave == NULL || max_slots > p->current_max_slots) {
if (p->xsave) {
free(p->xsave);
p->xsave = NULL;
}
p->xsave = calloc(max_slots, sizeof(int));
if (!p->xsave)
return -ENOMEM;
p->current_max_slots = max_slots;
}
if (p->ysave == NULL || max_slots > p->current_max_slots) {
if (p->ysave) {
free(p->ysave);
p->ysave = NULL;
}
p->ysave = calloc(max_slots, sizeof(int));
if (!p->ysave)
return -ENOMEM;
p->current_max_slots = max_slots;
}
if (p->press == NULL || max_slots > p->current_max_slots) {
if (p->press) {
free(p->press);
p->press = NULL;
}
p->press = calloc(max_slots, sizeof(int));
if (!p->press)
return -ENOMEM;
p->current_max_slots = max_slots;
}
if (!info->next->ops->read_mt)
return -ENOSYS;
ret = info->next->ops->read_mt(info->next, samp, max_slots, nr_samples);
if (ret < 0)
return ret;
#ifdef DEBUG
printf("PTHRES: read %d samples (mem: %d nr x %d slots)\n",
ret, nr_samples, max_slots);
#endif
for (i = 0; i < ret; i++) {
for (j = 0; j < max_slots; j++) {
if (!(samp[i][j].valid & TSLIB_MT_VALID))
continue;
if (samp[i][j].pressure < p->pmin) {
if (p->press[j] != 0) {
/* release */
p->press[j] = 0;
samp[i][j].pressure = 0;
samp[i][j].x = p->xsave[j];
samp[i][j].y = p->ysave[j];
} else {
/* release with no press,
* outside bounds, dropping
*/
samp[i][j].valid = 0;
}
} else if (samp[i][j].pressure > p->pmax) {
/* pressure outside bounds, dropping */
samp[i][j].valid = 0;
} else {
/* press */
p->press[j] = 1;
p->xsave[j] = samp[i][j].x;
p->ysave[j] = samp[i][j].y;
}
}
}
return ret;
}
static int pthres_fini(struct tslib_module_info *info)
{
struct tslib_pthres *p = (struct tslib_pthres *)info;
if (p->xsave)
free(p->xsave);
if (p->ysave)
free(p->ysave);
if (p->press)
free(p->press);
free(info);
return 0;
}
static const struct tslib_ops pthres_ops = {
.read = pthres_read,
.read_mt = pthres_read_mt,
.fini = pthres_fini,
};
static int threshold_vars(struct tslib_module_info *inf, char *str, void *data)
{
struct tslib_pthres *p = (struct tslib_pthres *)inf;
unsigned long v;
int err = errno;
v = strtoul(str, NULL, 0);
if (v == ULONG_MAX && errno == ERANGE)
return -1;
errno = err;
switch ((int)(intptr_t)data) {
case 0:
p->pmin = v;
break;
case 1:
p->pmax = v;
break;
default:
return -1;
}
return 0;
}
static const struct tslib_vars pthres_vars[] = {
{ "pmin", (void *)0, threshold_vars },
{ "pmax", (void *)1, threshold_vars }
};
#define NR_VARS (sizeof(pthres_vars) / sizeof(pthres_vars[0]))
TSAPI struct tslib_module_info *pthres_mod_init(__attribute__ ((unused)) struct tsdev *dev,
const char *params)
{
struct tslib_pthres *p;
p = malloc(sizeof(struct tslib_pthres));
if (p == NULL)
return NULL;
p->module.ops = &pthres_ops;
p->pmin = 1;
p->pmax = INT_MAX;
p->xsave = NULL;
p->ysave = NULL;
p->press = NULL;
p->current_max_slots = 0;
/*
* Parse the parameters.
*/
if (tslib_parse_vars(&p->module, pthres_vars, NR_VARS, params)) {
free(p);
return NULL;
}
return &p->module;
}
#ifndef TSLIB_STATIC_PTHRES_MODULE
TSLIB_MODULE_INIT(pthres_mod_init);
#endif
|