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/externals.h"
19 #include "../plugins/dfinput/main.h"
22 #define N900_TSMAX_X 4096
23 #define N900_TSOFFSET_X 0
24 #define N900_TSMAX_Y 4096
25 #define N900_TSOFFSET_Y 0
28 static int gun_x, gun_y, gun_in;
29 static int ts_multiplier_x, ts_multiplier_y, ts_offs_x, ts_offs_y;
30 static int (*pts_read)(struct tsdev *dev, struct ts_sample *sample, int nr);
31 static int (*pts_fd)(struct tsdev *dev);
33 #define limit(v, min, max) \
34 if (v < min) v = min; \
35 else if (v > max) v = max
37 int pl_gun_ts_update_raw(struct tsdev *ts, int *x, int *y, int *p)
39 struct ts_sample sample;
40 int sx = 0, sy = 0, sp = 0, updated = 0;
43 while (pts_read(ts, &sample, 1) > 0) {
46 sy = N900_TSMAX_Y - sample.y;
55 gun_x = (sx - ts_offs_x) * ts_multiplier_x >> 10;
56 gun_y = (sy - ts_offs_y) * ts_multiplier_y >> 10;
57 limit(gun_x, 0, 1023);
58 limit(gun_y, 0, 1023);
59 if (sp && !(g_opts & OPT_TSGUN_NOTRIGGER))
60 gun_in |= GUNIN_TRIGGER;
62 gun_in &= ~GUNIN_TRIGGER;
76 /* returns x, y in range 0..1023 (normalized to visible layer) */
77 void pl_gun_ts_update(struct tsdev *ts, int *x, int *y, int *in)
79 pl_gun_ts_update_raw(ts, NULL, NULL, NULL);
83 *in = gun_in | in_state_gun;
86 void pl_set_gun_rect(int x, int y, int w, int h)
90 ts_multiplier_x = (1<<20) / w;
91 ts_multiplier_y = (1<<20) / h;
94 int pl_gun_ts_get_fd(struct tsdev *ts)
96 if (ts != NULL && pts_fd != NULL)
102 struct tsdev *pl_gun_ts_init(void)
104 struct tsdev *(*pts_open)(const char *dev_name, int nonblock) = NULL;
105 int (*pts_config)(struct tsdev *) = NULL;
106 int (*pts_close)(struct tsdev *) = NULL;
107 const char *tsdevname;
112 tsdevname = "/dev/input/ts";
114 tsdevname = getenv("TSLIB_TSDEVICE");
115 if (tsdevname == NULL)
116 tsdevname = "/dev/input/touchscreen0";
119 // avoid hard dep on tslib
120 ltsh = dlopen("/usr/lib/libts-1.0.so.0", RTLD_NOW|RTLD_GLOBAL);
122 ltsh = dlopen("/usr/lib/libts-0.0.so.0", RTLD_NOW|RTLD_GLOBAL);
124 ltsh = dlopen("/lib/libts-0.0.so.0", RTLD_NOW|RTLD_GLOBAL);
126 fprintf(stderr, "%s\n", dlerror());
130 pts_open = dlsym(ltsh, "ts_open");
131 pts_config = dlsym(ltsh, "ts_config");
132 pts_read = dlsym(ltsh, "ts_read");
133 pts_fd = dlsym(ltsh, "ts_fd");
134 pts_close = dlsym(ltsh, "ts_close");
135 if (pts_open == NULL || pts_config == NULL || pts_read == NULL
136 || pts_fd == NULL || pts_close == NULL) {
137 fprintf(stderr, "%s\n", dlerror());
141 ts = pts_open(tsdevname, 1);
143 printf("Failed pts_open, check permission on %s\n", tsdevname);
146 if (pts_config(ts) != 0){
147 printf("Failed pts_config\n");
151 // FIXME: we should be able to get this somewhere
152 // the problem is this doesn't always match resolution due to different display modes
154 pl_set_gun_rect(N900_TSOFFSET_X, N900_TSOFFSET_Y, N900_TSMAX_X, N900_TSMAX_Y);
156 #ifdef __ARM_ARCH_7A__
157 pl_set_gun_rect(0, 0, 800, 480);
159 pl_set_gun_rect(0, 0, 320, 240);
162 printf("Touchscreen configured, device=%s\n", tsdevname);
172 fprintf(stderr, "Could not open touchscreen\n");