-
-
Notifications
You must be signed in to change notification settings - Fork 292
/
Copy pathts_setup.c
152 lines (127 loc) · 3.08 KB
/
ts_setup.c
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
/*
* tslib/src/ts_setup.c
*
* Copyright (C) 2017 Piotr Figlarek
*
* This file is placed under the LGPL. Please see the file
* COPYING for more details.
*
* SPDX-License-Identifier: LGPL-2.1
*
*
* Find, open and configure a touchscreen device.
*/
#include "tslib.h"
#include "tslib-private.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#if defined (__linux__)
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define DEV_INPUT_EVENT "/dev/input"
#define EVENT_DEV_NAME "event"
/* for old kernel headers */
#ifndef INPUT_PROP_MAX
# define INPUT_PROP_MAX 0x1f
#endif
#ifndef INPUT_PROP_DIRECT
# define INPUT_PROP_DIRECT 0x01
#endif
#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
#define BIT(nr) (1UL << (nr))
#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
#define BITS_PER_BYTE 8
#define BITS_PER_LONG (sizeof(long) * BITS_PER_BYTE)
#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
static int is_event_device(const struct dirent *dir)
{
return strncmp(EVENT_DEV_NAME, dir->d_name, 5) == 0;
}
static char *scan_devices(void)
{
struct dirent **namelist;
int i, ndev;
char *filename = NULL;
long propbit[BITS_TO_LONGS(INPUT_PROP_MAX)] = {0};
#ifdef DEBUG
printf("scanning for devices in %s\n", DEV_INPUT_EVENT);
#endif
ndev = scandir(DEV_INPUT_EVENT, &namelist, is_event_device, alphasort);
if (ndev <= 0)
return NULL;
for (i = 0; i < ndev; i++) {
char fname[512];
int fd = -1;
snprintf(fname, sizeof(fname),
"%s/%s", DEV_INPUT_EVENT, namelist[i]->d_name);
fd = open(fname, O_RDONLY);
if (fd < 0)
continue;
if ((ioctl(fd, EVIOCGPROP(sizeof(propbit)), propbit) < 0) ||
!(propbit[BIT_WORD(INPUT_PROP_DIRECT)] &
BIT_MASK(INPUT_PROP_DIRECT))) {
close(fd);
continue;
} else {
close(fd);
filename = strdup(fname);
break;
}
}
for (i = 0; i < ndev; ++i)
free(namelist[i]);
free(namelist);
return filename;
}
#endif /* __linux__ */
static const char * const ts_name_default[] = {
"/dev/input/ts",
"/dev/input/touchscreen",
"/dev/touchscreen/ucb1x00",
NULL
};
struct tsdev *ts_setup(const char *dev_name, int nonblock)
{
const char * const *defname;
struct tsdev *ts = NULL;
#if defined (__linux__)
char *fname = NULL;
#endif /* __linux__ */
dev_name = dev_name ? dev_name : getenv("TSLIB_TSDEVICE");
if (dev_name != NULL) {
ts = ts_open(dev_name, nonblock);
} else {
defname = &ts_name_default[0];
while (*defname != NULL) {
ts = ts_open(*defname, nonblock);
if (ts != NULL)
break;
++defname;
}
}
#if defined (__linux__)
if (!ts) {
fname = scan_devices();
if (!fname)
return NULL;
ts = ts_open(fname, nonblock);
free(fname);
}
#endif /* __linux__ */
/* if detected try to configure it */
if (ts && ts_config(ts) != 0) {
ts_error("ts_config: %s\n", strerror(errno));
ts_close(ts);
return NULL;
}
return ts;
}