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" |
18 | #include "../plugins/dfinput/main.h" |
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; |
22 | static int (*pts_read)(struct tsdev *dev, struct ts_sample *sample, int nr); |
23 | |
24 | #define limit(v, min, max) \ |
25 | if (v < min) v = min; \ |
26 | else if (v > max) v = max |
27 | |
28 | void pl_gun_ts_update(struct tsdev *ts, int *x, int *y, int *in) |
29 | { |
30 | struct ts_sample sample; |
31 | int sx = 0, sy = 0, sp = 0, updated = 0; |
32 | |
33 | if (ts != NULL) { |
34 | while (pts_read(ts, &sample, 1) > 0) { |
35 | sx = sample.x; |
36 | sy = sample.y; |
37 | sp = sample.pressure; |
38 | updated = 1; |
39 | } |
40 | |
41 | if (updated) { |
42 | gun_x = (sx - ts_offs_x) * ts_multiplier_x >> 10; |
43 | gun_y = (sy - ts_offs_y) * ts_multiplier_y >> 10; |
44 | limit(gun_x, 0, 1023); |
45 | limit(gun_y, 0, 1023); |
46 | if (sp && !(g_opts & OPT_TSGUN_NOTRIGGER)) |
47 | gun_in |= GUNIN_TRIGGER; |
48 | else |
49 | gun_in &= ~GUNIN_TRIGGER; |
50 | } |
51 | } |
52 | |
53 | *x = gun_x; |
54 | *y = gun_y; |
55 | *in = gun_in | in_state_gun; |
56 | } |
57 | |
58 | void pl_set_gun_rect(int x, int y, int w, int h) |
59 | { |
60 | ts_offs_x = x; |
61 | ts_offs_y = y; |
62 | ts_multiplier_x = (1<<20) / w; |
63 | ts_multiplier_y = (1<<20) / h; |
64 | } |
65 | |
66 | struct tsdev *pl_gun_ts_init(void) |
67 | { |
68 | struct tsdev *(*pts_open)(const char *dev_name, int nonblock) = NULL; |
69 | int (*pts_config)(struct tsdev *) = NULL; |
70 | int (*pts_close)(struct tsdev *) = NULL; |
71 | const char *tsdevname; |
72 | struct tsdev *ts; |
73 | void *ltsh; |
74 | |
75 | tsdevname = getenv("TSLIB_TSDEVICE"); |
76 | if (tsdevname == NULL) |
77 | tsdevname = "/dev/input/touchscreen0"; |
78 | |
79 | // avoid hard dep on tslib |
80 | ltsh = dlopen("/usr/lib/libts-1.0.so.0", RTLD_LAZY); |
81 | if (ltsh == NULL) |
82 | ltsh = dlopen("/usr/lib/libts-0.0.so.0", RTLD_LAZY); |
83 | if (ltsh == NULL) { |
84 | fprintf(stderr, "%s\n", dlerror()); |
85 | goto fail; |
86 | } |
87 | |
88 | pts_open = dlsym(ltsh, "ts_open"); |
89 | pts_config = dlsym(ltsh, "ts_config"); |
90 | pts_read = dlsym(ltsh, "ts_read"); |
91 | pts_close = dlsym(ltsh, "ts_close"); |
92 | if (pts_open == NULL || pts_config == NULL || pts_read == NULL || pts_close == NULL) { |
93 | fprintf(stderr, "%s\n", dlerror()); |
94 | goto fail_dlsym; |
95 | } |
96 | |
97 | ts = pts_open(tsdevname, 1); |
98 | if (ts == NULL) |
99 | goto fail_open; |
100 | if (pts_config(ts) != 0) |
101 | goto fail_config; |
102 | |
103 | // FIXME: we should be able to get this somewhere |
104 | // the problem is this doesn't always match resolution due to different display modes |
105 | pl_set_gun_rect(0, 0, 800, 480); |
106 | return ts; |
107 | |
108 | fail_config: |
366631aa |
109 | pts_close(ts); |
4c08b9e7 |
110 | fail_open: |
111 | fail_dlsym: |
112 | dlclose(ltsh); |
113 | ltsh = NULL; |
114 | fail: |
115 | fprintf(stderr, "Could not open touchscreen\n"); |
116 | return NULL; |
117 | } |
118 | |