4c08b9e7 |
1 | /* |
2 | * (C) GraÅžvydas "notaz" Ignotas, 2011 |
3 | * |
4 | * This work is licensed under the terms of any of these licenses |
5 | * (at your option): |
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. |
9 | */ |
10 | |
11 | #include <stdio.h> |
12 | #include <stdlib.h> |
13 | #include <dlfcn.h> |
14 | #include <tslib.h> |
15 | #include "plugin_lib.h" |
16 | #include "pl_gun_ts.h" |
17 | #include "menu.h" |
e4c83ca6 |
18 | #include "../plugins/dfinput/externals.h" |
4c08b9e7 |
19 | |
20 | static int gun_x, gun_y, gun_in; |
21 | static int ts_multiplier_x, ts_multiplier_y, ts_offs_x, ts_offs_y; |
50306d8d |
22 | static int (*pts_read)(struct tsdev *dev, struct ts_sample *sample, int nr); |
23 | static int (*pts_fd)(struct tsdev *dev); |
4c08b9e7 |
24 | |
25 | #define limit(v, min, max) \ |
26 | if (v < min) v = min; \ |
27 | else if (v > max) v = max |
28 | |
50306d8d |
29 | int pl_gun_ts_update_raw(struct tsdev *ts, int *x, int *y, int *p) |
4c08b9e7 |
30 | { |
31 | struct ts_sample sample; |
32 | int sx = 0, sy = 0, sp = 0, updated = 0; |
33 | |
34 | if (ts != NULL) { |
35 | while (pts_read(ts, &sample, 1) > 0) { |
36 | sx = sample.x; |
37 | sy = sample.y; |
38 | sp = sample.pressure; |
39 | updated = 1; |
40 | } |
41 | |
42 | if (updated) { |
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; |
49 | else |
50 | gun_in &= ~GUNIN_TRIGGER; |
51 | } |
52 | } |
53 | |
50306d8d |
54 | if (updated) { |
55 | if (x) *x = sx; |
56 | if (y) *y = sy; |
57 | if (p) *p = sp; |
58 | return 1; |
59 | } |
60 | |
61 | return 0; |
62 | } |
63 | |
e4c83ca6 |
64 | /* returns x, y in range 0..1023 (normalized to visible layer) */ |
50306d8d |
65 | void pl_gun_ts_update(struct tsdev *ts, int *x, int *y, int *in) |
66 | { |
67 | pl_gun_ts_update_raw(ts, NULL, NULL, NULL); |
68 | |
4c08b9e7 |
69 | *x = gun_x; |
70 | *y = gun_y; |
71 | *in = gun_in | in_state_gun; |
72 | } |
73 | |
74 | void pl_set_gun_rect(int x, int y, int w, int h) |
75 | { |
76 | ts_offs_x = x; |
77 | ts_offs_y = y; |
78 | ts_multiplier_x = (1<<20) / w; |
79 | ts_multiplier_y = (1<<20) / h; |
80 | } |
81 | |
50306d8d |
82 | int pl_gun_ts_get_fd(struct tsdev *ts) |
83 | { |
84 | if (ts != NULL && pts_fd != NULL) |
85 | return pts_fd(ts); |
86 | |
87 | return -1; |
88 | } |
89 | |
4c08b9e7 |
90 | struct tsdev *pl_gun_ts_init(void) |
91 | { |
92 | struct tsdev *(*pts_open)(const char *dev_name, int nonblock) = NULL; |
93 | int (*pts_config)(struct tsdev *) = NULL; |
94 | int (*pts_close)(struct tsdev *) = NULL; |
95 | const char *tsdevname; |
96 | struct tsdev *ts; |
97 | void *ltsh; |
98 | |
99 | tsdevname = getenv("TSLIB_TSDEVICE"); |
100 | if (tsdevname == NULL) |
101 | tsdevname = "/dev/input/touchscreen0"; |
102 | |
103 | // avoid hard dep on tslib |
faf2b2aa |
104 | ltsh = dlopen("/usr/lib/libts-1.0.so.0", RTLD_NOW|RTLD_GLOBAL); |
4c08b9e7 |
105 | if (ltsh == NULL) |
faf2b2aa |
106 | ltsh = dlopen("/usr/lib/libts-0.0.so.0", RTLD_NOW|RTLD_GLOBAL); |
50306d8d |
107 | if (ltsh == NULL) |
108 | ltsh = dlopen("/lib/libts-0.0.so.0", RTLD_NOW|RTLD_GLOBAL); |
4c08b9e7 |
109 | if (ltsh == NULL) { |
110 | fprintf(stderr, "%s\n", dlerror()); |
111 | goto fail; |
112 | } |
113 | |
114 | pts_open = dlsym(ltsh, "ts_open"); |
115 | pts_config = dlsym(ltsh, "ts_config"); |
116 | pts_read = dlsym(ltsh, "ts_read"); |
faf2b2aa |
117 | pts_fd = dlsym(ltsh, "ts_fd"); |
4c08b9e7 |
118 | pts_close = dlsym(ltsh, "ts_close"); |
faf2b2aa |
119 | if (pts_open == NULL || pts_config == NULL || pts_read == NULL |
120 | || pts_fd == NULL || pts_close == NULL) { |
4c08b9e7 |
121 | fprintf(stderr, "%s\n", dlerror()); |
122 | goto fail_dlsym; |
123 | } |
124 | |
125 | ts = pts_open(tsdevname, 1); |
126 | if (ts == NULL) |
127 | goto fail_open; |
128 | if (pts_config(ts) != 0) |
129 | goto fail_config; |
130 | |
131 | // FIXME: we should be able to get this somewhere |
132 | // the problem is this doesn't always match resolution due to different display modes |
55b0eeea |
133 | #ifdef __ARM_ARCH_7A__ |
4c08b9e7 |
134 | pl_set_gun_rect(0, 0, 800, 480); |
55b0eeea |
135 | #else |
136 | pl_set_gun_rect(0, 0, 320, 240); |
137 | #endif |
4c08b9e7 |
138 | return ts; |
139 | |
140 | fail_config: |
366631aa |
141 | pts_close(ts); |
4c08b9e7 |
142 | fail_open: |
143 | fail_dlsym: |
144 | dlclose(ltsh); |
145 | ltsh = NULL; |
146 | fail: |
147 | fprintf(stderr, "Could not open touchscreen\n"); |
148 | return NULL; |
149 | } |
150 | |