|
| 1 | +/* |
| 2 | + * tslib/plugins/crop.c |
| 3 | + * |
| 4 | + * Copyright (C) 2024 Martin Kepplinger-Novaković |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Lesser General Public License as published by |
| 8 | + * the Free Software Foundation, either version 2 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + * |
| 19 | + * SPDX-License-Identifier: LGPL-2.1 |
| 20 | + */ |
| 21 | +#include <errno.h> |
| 22 | +#include <stdlib.h> |
| 23 | +#include <string.h> |
| 24 | +#include <limits.h> |
| 25 | +#include <sys/stat.h> |
| 26 | +#include <stdint.h> |
| 27 | +#include <stdio.h> |
| 28 | + |
| 29 | +#include "config.h" |
| 30 | +#include "tslib-private.h" |
| 31 | + |
| 32 | +struct tslib_crop { |
| 33 | + struct tslib_module_info module; |
| 34 | + int32_t *last_tid; |
| 35 | + uint32_t last_pressure; |
| 36 | + int a[7]; |
| 37 | + /* fb res from calibration-time */ |
| 38 | + uint32_t cal_res_x; |
| 39 | + uint32_t cal_res_y; |
| 40 | + uint32_t rot; |
| 41 | +}; |
| 42 | + |
| 43 | +static int crop_read(struct tslib_module_info *info, struct ts_sample *samp, |
| 44 | + int nr) |
| 45 | +{ |
| 46 | + struct tslib_crop *crop = (struct tslib_crop *)info; |
| 47 | + int ret; |
| 48 | + int nread = 0; |
| 49 | + |
| 50 | + while (nread < nr) { |
| 51 | + struct ts_sample cur; |
| 52 | + |
| 53 | + ret = info->next->ops->read(info->next, &cur, 1); |
| 54 | + if (ret < 0) |
| 55 | + return ret; |
| 56 | + |
| 57 | + if (cur.x >= crop->cal_res_x || |
| 58 | + cur.x < 0 || |
| 59 | + cur.y >= crop->cal_res_y || |
| 60 | + cur.y < 0) { |
| 61 | + if (cur.pressure == 0) { |
| 62 | + if (crop->last_pressure == 0) |
| 63 | + continue; |
| 64 | + } else { |
| 65 | + continue; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + samp[nread++] = cur; |
| 70 | + crop->last_pressure = cur.pressure; |
| 71 | + } |
| 72 | + |
| 73 | + return nread; |
| 74 | +} |
| 75 | + |
| 76 | +static int crop_read_mt(struct tslib_module_info *info, |
| 77 | + struct ts_sample_mt **samp, int max_slots, int nr) |
| 78 | +{ |
| 79 | + struct tslib_crop *crop = (struct tslib_crop *)info; |
| 80 | + int32_t ret; |
| 81 | + int32_t i, j; |
| 82 | + |
| 83 | + if (!info->next->ops->read_mt) |
| 84 | + return -ENOSYS; |
| 85 | + |
| 86 | + ret = info->next->ops->read_mt(info->next, samp, max_slots, nr); |
| 87 | + if (ret < 0) |
| 88 | + return ret; |
| 89 | + |
| 90 | + if (!crop->last_tid) { |
| 91 | + free(crop->last_tid); |
| 92 | + |
| 93 | + crop->last_tid = calloc(max_slots, sizeof(int32_t)); |
| 94 | + if (!crop->last_tid) |
| 95 | + return -ENOMEM; |
| 96 | + } |
| 97 | + |
| 98 | + for (i = 0; i < ret; i++) { |
| 99 | + for (j = 0; j < max_slots; j++) { |
| 100 | + if (!(samp[i][j].valid & TSLIB_MT_VALID)) |
| 101 | + continue; |
| 102 | + |
| 103 | + /* assume the input device uses 0..(fb-1) value. */ |
| 104 | + |
| 105 | + if (samp[i][j].x >= crop->cal_res_x || |
| 106 | + samp[i][j].x < 0 || |
| 107 | + samp[i][j].y >= crop->cal_res_y || |
| 108 | + samp[i][j].y < 0) { |
| 109 | + if (samp[i][j].tracking_id == -1) { |
| 110 | + /* |
| 111 | + * don't drop except last seen tid also -1 |
| 112 | + * otherwise, tid would get filtered |
| 113 | + * out and new x/y would reach the app |
| 114 | + */ |
| 115 | + if (crop->last_tid[j] == -1) { |
| 116 | + samp[i][j].valid &= ~TSLIB_MT_VALID; |
| 117 | + } |
| 118 | + } else { |
| 119 | + /* drop */ |
| 120 | + samp[i][j].valid &= ~TSLIB_MT_VALID; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /* save the last not-dropped tid value */ |
| 125 | + if (samp[i][j].valid & TSLIB_MT_VALID) |
| 126 | + crop->last_tid[j] = samp[i][j].tracking_id; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return ret; |
| 131 | +} |
| 132 | + |
| 133 | +static int crop_fini(struct tslib_module_info *info) |
| 134 | +{ |
| 135 | + struct tslib_crop *crop = (struct tslib_crop *)info; |
| 136 | + |
| 137 | + free(crop->last_tid); |
| 138 | + free(info); |
| 139 | + |
| 140 | + return 0; |
| 141 | +} |
| 142 | + |
| 143 | +static const struct tslib_ops crop_ops = { |
| 144 | + .read = crop_read, |
| 145 | + .read_mt = crop_read_mt, |
| 146 | + .fini = crop_fini, |
| 147 | +}; |
| 148 | + |
| 149 | +TSAPI struct tslib_module_info *crop_mod_init(__attribute__ ((unused)) struct tsdev *dev, |
| 150 | + const char *params) |
| 151 | +{ |
| 152 | + struct tslib_crop *crop; |
| 153 | + struct stat sbuf; |
| 154 | + FILE *pcal_fd; |
| 155 | + int index; |
| 156 | + char *calfile; |
| 157 | + |
| 158 | + crop = malloc(sizeof(struct tslib_crop)); |
| 159 | + if (crop == NULL) |
| 160 | + return NULL; |
| 161 | + |
| 162 | + memset(crop, 0, sizeof(struct tslib_crop)); |
| 163 | + crop->module.ops = &crop_ops; |
| 164 | + |
| 165 | + crop->last_tid = NULL; |
| 166 | + |
| 167 | + /* |
| 168 | + * Get resolution from calibration file |
| 169 | + */ |
| 170 | + if ((calfile = getenv("TSLIB_CALIBFILE")) == NULL) |
| 171 | + calfile = TS_POINTERCAL; |
| 172 | + |
| 173 | + if (stat(calfile, &sbuf) == 0) { |
| 174 | + pcal_fd = fopen(calfile, "r"); |
| 175 | + if (!pcal_fd) { |
| 176 | + free(crop); |
| 177 | + perror("fopen"); |
| 178 | + return NULL; |
| 179 | + } |
| 180 | + |
| 181 | + for (index = 0; index < 7; index++) |
| 182 | + if (fscanf(pcal_fd, "%d", &crop->a[index]) != 1) |
| 183 | + break; |
| 184 | + |
| 185 | + if (!fscanf(pcal_fd, "%d %d", |
| 186 | + &crop->cal_res_x, &crop->cal_res_y)) { |
| 187 | + fprintf(stderr, |
| 188 | + "CROP: Couldn't read resolution values\n"); |
| 189 | + } |
| 190 | + |
| 191 | + if (!fscanf(pcal_fd, "%d", &crop->rot)) { |
| 192 | + fprintf(stderr, "CROP: Couldn't read rotation value\n"); |
| 193 | + } |
| 194 | + |
| 195 | + fclose(pcal_fd); |
| 196 | + } |
| 197 | + return &crop->module; |
| 198 | +} |
| 199 | + |
| 200 | +#ifndef TSLIB_STATIC_CROP_MODULE |
| 201 | + TSLIB_MODULE_INIT(crop_mod_init); |
| 202 | +#endif |
0 commit comments