2 * (C) GraÅžvydas "notaz" Ignotas, 2011
4 * This work is licensed under the terms of any of these licenses
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
8 * See the COPYING file in the top-level directory.
15 #include "plugin_lib.h"
16 #include "pl_gun_ts.h"
20 #define N900_TSMAX_X 4096
21 #define N900_TSOFFSET_X 0
22 #define N900_TSMAX_Y 4096
23 #define N900_TSOFFSET_Y 0
26 static int gun_x, gun_y, gun_in;
27 static int ts_multiplier_x, ts_multiplier_y, ts_offs_x, ts_offs_y;
28 static int (*pts_read)(struct tsdev *dev, struct ts_sample *sample, int nr);
29 static int (*pts_fd)(struct tsdev *dev);
31 #define limit(v, min, max) \
32 if (v < min) v = min; \
33 else if (v > max) v = max
35 int pl_gun_ts_update_raw(struct tsdev *ts, int *x, int *y, int *p)
37 struct ts_sample sample;
38 int sx = 0, sy = 0, sp = 0, updated = 0;
41 while (pts_read(ts, &sample, 1) > 0) {
44 sy = N900_TSMAX_Y - sample.y;
53 gun_x = (sx - ts_offs_x) * ts_multiplier_x >> 10;
54 gun_y = (sy - ts_offs_y) * ts_multiplier_y >> 10;
55 limit(gun_x, 0, 1023);
56 limit(gun_y, 0, 1023);
57 if (sp && !(g_opts & OPT_TSGUN_NOTRIGGER))
74 /* returns x, y in range 0..1023 (normalized to visible layer) */
75 void pl_gun_ts_update(struct tsdev *ts, int *x, int *y, int *in)
77 pl_gun_ts_update_raw(ts, NULL, NULL, NULL);
81 *in = gun_in | in_state_gun;
84 void pl_set_gun_rect(int x, int y, int w, int h)
88 ts_multiplier_x = (1<<20) / w;
89 ts_multiplier_y = (1<<20) / h;
92 int pl_gun_ts_get_fd(struct tsdev *ts)
94 if (ts != NULL && pts_fd != NULL)
100 struct tsdev *pl_gun_ts_init(void)
102 struct tsdev *(*pts_open)(const char *dev_name, int nonblock) = NULL;
103 int (*pts_config)(struct tsdev *) = NULL;
104 int (*pts_close)(struct tsdev *) = NULL;
105 const char *tsdevname;
110 tsdevname = "/dev/input/ts";
112 tsdevname = getenv("TSLIB_TSDEVICE");
113 if (tsdevname == NULL)
114 tsdevname = "/dev/input/touchscreen0";
117 // avoid hard dep on tslib
118 ltsh = dlopen("/usr/lib/libts-1.0.so.0", RTLD_NOW|RTLD_GLOBAL);
120 ltsh = dlopen("/usr/lib/libts-0.0.so.0", RTLD_NOW|RTLD_GLOBAL);
122 ltsh = dlopen("/lib/libts-0.0.so.0", RTLD_NOW|RTLD_GLOBAL);
124 fprintf(stderr, "%s\n", dlerror());
128 pts_open = dlsym(ltsh, "ts_open");
129 pts_config = dlsym(ltsh, "ts_config");
130 pts_read = dlsym(ltsh, "ts_read");
131 pts_fd = dlsym(ltsh, "ts_fd");
132 pts_close = dlsym(ltsh, "ts_close");
133 if (pts_open == NULL || pts_config == NULL || pts_read == NULL
134 || pts_fd == NULL || pts_close == NULL) {
135 fprintf(stderr, "%s\n", dlerror());
139 ts = pts_open(tsdevname, 1);
141 printf("Failed pts_open, check permission on %s\n", tsdevname);
144 if (pts_config(ts) != 0){
145 printf("Failed pts_config\n");
149 // FIXME: we should be able to get this somewhere
150 // the problem is this doesn't always match resolution due to different display modes
152 pl_set_gun_rect(N900_TSOFFSET_X, N900_TSOFFSET_Y, N900_TSMAX_X, N900_TSMAX_Y);
154 #ifdef __ARM_ARCH_7A__
155 pl_set_gun_rect(0, 0, 800, 480);
157 pl_set_gun_rect(0, 0, 320, 240);
160 printf("Touchscreen configured, device=%s\n", tsdevname);
170 fprintf(stderr, "Could not open touchscreen\n");