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"
18 #include "../plugins/dfinput/main.h"
20 static int gun_x, gun_y, gun_in;
21 static int ts_multiplier_x, ts_multiplier_y, ts_offs_x, ts_offs_y;
22 static int (*pts_read)(struct tsdev *dev, struct ts_sample *sample, int nr);
23 static int (*pts_fd)(struct tsdev *dev);
25 #define limit(v, min, max) \
26 if (v < min) v = min; \
27 else if (v > max) v = max
29 int pl_gun_ts_update_raw(struct tsdev *ts, int *x, int *y, int *p)
31 struct ts_sample sample;
32 int sx = 0, sy = 0, sp = 0, updated = 0;
35 while (pts_read(ts, &sample, 1) > 0) {
43 gun_x = (sx - ts_offs_x) * ts_multiplier_x >> 10;
44 gun_y = (sy - ts_offs_y) * ts_multiplier_y >> 10;
45 limit(gun_x, 0, 1023);
46 limit(gun_y, 0, 1023);
47 if (sp && !(g_opts & OPT_TSGUN_NOTRIGGER))
48 gun_in |= GUNIN_TRIGGER;
50 gun_in &= ~GUNIN_TRIGGER;
64 void pl_gun_ts_update(struct tsdev *ts, int *x, int *y, int *in)
66 pl_gun_ts_update_raw(ts, NULL, NULL, NULL);
70 *in = gun_in | in_state_gun;
73 void pl_set_gun_rect(int x, int y, int w, int h)
77 ts_multiplier_x = (1<<20) / w;
78 ts_multiplier_y = (1<<20) / h;
81 int pl_gun_ts_get_fd(struct tsdev *ts)
83 if (ts != NULL && pts_fd != NULL)
89 struct tsdev *pl_gun_ts_init(void)
91 struct tsdev *(*pts_open)(const char *dev_name, int nonblock) = NULL;
92 int (*pts_config)(struct tsdev *) = NULL;
93 int (*pts_close)(struct tsdev *) = NULL;
94 const char *tsdevname;
98 tsdevname = getenv("TSLIB_TSDEVICE");
99 if (tsdevname == NULL)
100 tsdevname = "/dev/input/touchscreen0";
102 // avoid hard dep on tslib
103 ltsh = dlopen("/usr/lib/libts-1.0.so.0", RTLD_NOW|RTLD_GLOBAL);
105 ltsh = dlopen("/usr/lib/libts-0.0.so.0", RTLD_NOW|RTLD_GLOBAL);
107 ltsh = dlopen("/lib/libts-0.0.so.0", RTLD_NOW|RTLD_GLOBAL);
109 fprintf(stderr, "%s\n", dlerror());
113 pts_open = dlsym(ltsh, "ts_open");
114 pts_config = dlsym(ltsh, "ts_config");
115 pts_read = dlsym(ltsh, "ts_read");
116 pts_fd = dlsym(ltsh, "ts_fd");
117 pts_close = dlsym(ltsh, "ts_close");
118 if (pts_open == NULL || pts_config == NULL || pts_read == NULL
119 || pts_fd == NULL || pts_close == NULL) {
120 fprintf(stderr, "%s\n", dlerror());
124 ts = pts_open(tsdevname, 1);
127 if (pts_config(ts) != 0)
130 // FIXME: we should be able to get this somewhere
131 // the problem is this doesn't always match resolution due to different display modes
132 #ifdef __ARM_ARCH_7A__
133 pl_set_gun_rect(0, 0, 800, 480);
135 pl_set_gun_rect(0, 0, 320, 240);
146 fprintf(stderr, "Could not open touchscreen\n");