2c022513c812f46774066b934731e6da08f4bfc4
[pcsx_rearmed.git] / frontend / pl_gun_ts.c
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 int (*pts_read)(struct tsdev *dev, struct ts_sample *sample, int nr);
23 int (*pts_fd)(struct tsdev *dev);
24
25 #define limit(v, min, max) \
26         if (v < min) v = min; \
27         else if (v > max) v = max
28
29 void pl_gun_ts_update(struct tsdev *ts, int *x, int *y, int *in)
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
54         *x = gun_x;
55         *y = gun_y;
56         *in = gun_in | in_state_gun;
57 }
58
59 void pl_set_gun_rect(int x, int y, int w, int h)
60 {
61         ts_offs_x = x;
62         ts_offs_y = y;
63         ts_multiplier_x = (1<<20) / w;
64         ts_multiplier_y = (1<<20) / h;
65 }
66
67 struct tsdev *pl_gun_ts_init(void)
68 {
69         struct tsdev *(*pts_open)(const char *dev_name, int nonblock) = NULL;
70         int (*pts_config)(struct tsdev *) = NULL;
71         int (*pts_close)(struct tsdev *) = NULL;
72         const char *tsdevname;
73         struct tsdev *ts;
74         void *ltsh;
75
76         tsdevname = getenv("TSLIB_TSDEVICE");
77         if (tsdevname == NULL)
78                 tsdevname = "/dev/input/touchscreen0";
79
80         // avoid hard dep on tslib
81         ltsh = dlopen("/usr/lib/libts-1.0.so.0", RTLD_NOW|RTLD_GLOBAL);
82         if (ltsh == NULL)
83                 ltsh = dlopen("/usr/lib/libts-0.0.so.0", RTLD_NOW|RTLD_GLOBAL);
84         if (ltsh == NULL) {
85                 fprintf(stderr, "%s\n", dlerror());
86                 goto fail;
87         }
88
89         pts_open = dlsym(ltsh, "ts_open");
90         pts_config = dlsym(ltsh, "ts_config");
91         pts_read = dlsym(ltsh, "ts_read");
92         pts_fd = dlsym(ltsh, "ts_fd");
93         pts_close = dlsym(ltsh, "ts_close");
94         if (pts_open == NULL || pts_config == NULL || pts_read == NULL
95             || pts_fd == NULL || pts_close == NULL) {
96                 fprintf(stderr, "%s\n", dlerror());
97                 goto fail_dlsym;
98         }
99
100         ts = pts_open(tsdevname, 1);
101         if (ts == NULL)
102                 goto fail_open;
103         if (pts_config(ts) != 0)
104                 goto fail_config;
105
106         // FIXME: we should be able to get this somewhere
107         // the problem is this doesn't always match resolution due to different display modes
108 #ifdef __ARM_ARCH_7A__
109         pl_set_gun_rect(0, 0, 800, 480);
110 #else
111         pl_set_gun_rect(0, 0, 320, 240);
112 #endif
113         return ts;
114
115 fail_config:
116         pts_close(ts);
117 fail_open:
118 fail_dlsym:
119         dlclose(ltsh);
120         ltsh = NULL;
121 fail:
122         fprintf(stderr, "Could not open touchscreen\n");
123         return NULL;
124 }
125