libretro: rename targets to be picked by menu
[pcsx_rearmed.git] / frontend / pl_gun_ts.c
CommitLineData
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"
a76fd953
PI
19#include "../plugins/dfinput/main.h"
20
21#ifdef MAEMO
22#define N900_TSMAX_X 4096
23#define N900_TSOFFSET_X 0
24#define N900_TSMAX_Y 4096
25#define N900_TSOFFSET_Y 0
26#endif
4c08b9e7 27
28static int gun_x, gun_y, gun_in;
29static int ts_multiplier_x, ts_multiplier_y, ts_offs_x, ts_offs_y;
50306d8d 30static int (*pts_read)(struct tsdev *dev, struct ts_sample *sample, int nr);
31static int (*pts_fd)(struct tsdev *dev);
4c08b9e7 32
33#define limit(v, min, max) \
34 if (v < min) v = min; \
35 else if (v > max) v = max
36
50306d8d 37int pl_gun_ts_update_raw(struct tsdev *ts, int *x, int *y, int *p)
4c08b9e7 38{
39 struct ts_sample sample;
40 int sx = 0, sy = 0, sp = 0, updated = 0;
41
42 if (ts != NULL) {
43 while (pts_read(ts, &sample, 1) > 0) {
44 sx = sample.x;
a76fd953
PI
45#ifdef MAEMO
46 sy = N900_TSMAX_Y - sample.y;
47#else
4c08b9e7 48 sy = sample.y;
a76fd953 49#endif
4c08b9e7 50 sp = sample.pressure;
51 updated = 1;
52 }
53
54 if (updated) {
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;
61 else
62 gun_in &= ~GUNIN_TRIGGER;
63 }
64 }
65
50306d8d 66 if (updated) {
67 if (x) *x = sx;
68 if (y) *y = sy;
69 if (p) *p = sp;
70 return 1;
71 }
72
73 return 0;
74}
75
e4c83ca6 76/* returns x, y in range 0..1023 (normalized to visible layer) */
50306d8d 77void pl_gun_ts_update(struct tsdev *ts, int *x, int *y, int *in)
78{
79 pl_gun_ts_update_raw(ts, NULL, NULL, NULL);
80
4c08b9e7 81 *x = gun_x;
82 *y = gun_y;
83 *in = gun_in | in_state_gun;
84}
85
86void pl_set_gun_rect(int x, int y, int w, int h)
87{
88 ts_offs_x = x;
89 ts_offs_y = y;
90 ts_multiplier_x = (1<<20) / w;
91 ts_multiplier_y = (1<<20) / h;
92}
93
50306d8d 94int pl_gun_ts_get_fd(struct tsdev *ts)
95{
96 if (ts != NULL && pts_fd != NULL)
97 return pts_fd(ts);
98
99 return -1;
100}
101
4c08b9e7 102struct tsdev *pl_gun_ts_init(void)
103{
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;
108 struct tsdev *ts;
109 void *ltsh;
110
a76fd953
PI
111#ifdef MAEMO
112 tsdevname = "/dev/input/ts";
113#else
4c08b9e7 114 tsdevname = getenv("TSLIB_TSDEVICE");
115 if (tsdevname == NULL)
116 tsdevname = "/dev/input/touchscreen0";
a76fd953 117#endif
4c08b9e7 118
119 // avoid hard dep on tslib
faf2b2aa 120 ltsh = dlopen("/usr/lib/libts-1.0.so.0", RTLD_NOW|RTLD_GLOBAL);
4c08b9e7 121 if (ltsh == NULL)
faf2b2aa 122 ltsh = dlopen("/usr/lib/libts-0.0.so.0", RTLD_NOW|RTLD_GLOBAL);
50306d8d 123 if (ltsh == NULL)
124 ltsh = dlopen("/lib/libts-0.0.so.0", RTLD_NOW|RTLD_GLOBAL);
4c08b9e7 125 if (ltsh == NULL) {
126 fprintf(stderr, "%s\n", dlerror());
127 goto fail;
128 }
129
130 pts_open = dlsym(ltsh, "ts_open");
131 pts_config = dlsym(ltsh, "ts_config");
132 pts_read = dlsym(ltsh, "ts_read");
faf2b2aa 133 pts_fd = dlsym(ltsh, "ts_fd");
4c08b9e7 134 pts_close = dlsym(ltsh, "ts_close");
faf2b2aa 135 if (pts_open == NULL || pts_config == NULL || pts_read == NULL
136 || pts_fd == NULL || pts_close == NULL) {
4c08b9e7 137 fprintf(stderr, "%s\n", dlerror());
138 goto fail_dlsym;
139 }
140
141 ts = pts_open(tsdevname, 1);
a76fd953
PI
142 if (ts == NULL){
143 printf("Failed pts_open, check permission on %s\n", tsdevname);
4c08b9e7 144 goto fail_open;
a76fd953
PI
145 }
146 if (pts_config(ts) != 0){
147 printf("Failed pts_config\n");
4c08b9e7 148 goto fail_config;
a76fd953 149 }
4c08b9e7 150
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
a76fd953
PI
153#ifdef MAEMO
154 pl_set_gun_rect(N900_TSOFFSET_X, N900_TSOFFSET_Y, N900_TSMAX_X, N900_TSMAX_Y);
155#else
55b0eeea 156#ifdef __ARM_ARCH_7A__
4c08b9e7 157 pl_set_gun_rect(0, 0, 800, 480);
55b0eeea 158#else
159 pl_set_gun_rect(0, 0, 320, 240);
160#endif
a76fd953
PI
161#endif
162 printf("Touchscreen configured, device=%s\n", tsdevname);
4c08b9e7 163 return ts;
164
165fail_config:
366631aa 166 pts_close(ts);
4c08b9e7 167fail_open:
168fail_dlsym:
169 dlclose(ltsh);
170 ltsh = NULL;
171fail:
172 fprintf(stderr, "Could not open touchscreen\n");
173 return NULL;
174}
175