4e2cef027ba4e2bac10ce0df2a64a98e8c7b7c22
[pcsx_rearmed.git] / frontend / in_tsbutton.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 <tslib.h>
13
14 #include "common/input.h"
15 #include "in_tsbutton.h"
16
17 #define IN_TSBUTTON_PREFIX "tsbutton:"
18 #define IN_TSBUTTON_COUNT 4
19 static int tsbutton_down_id;
20 static int last_tsbutton_id;
21
22 #define TS_WIDTH 320
23 #define TS_HEIGHT 240
24
25 // HACK: stealing this from plugin_lib
26 extern void *tsdev;
27 extern int (*pts_read)(struct tsdev *dev, struct ts_sample *sample, int nr);
28 extern int (*pts_fd)(struct tsdev *dev);
29
30 static const char * const in_tsbutton_keys[IN_TSBUTTON_COUNT] = {
31         "TS1", "TS2", "TS3", "TS4",
32 };
33
34 static void in_tsbutton_probe(void)
35 {
36         struct tsdev *dev = tsdev;
37         if (dev == NULL) {
38                 fprintf(stderr, "in_tsbutton_probe: missing tsdev\n");
39                 return;
40         }
41
42         in_register(IN_TSBUTTON_PREFIX "touchscreen as buttons",
43                 pts_fd(dev), NULL, IN_TSBUTTON_COUNT, in_tsbutton_keys, 0);
44 }
45
46 static const char * const *
47 in_tsbutton_get_key_names(int *count)
48 {
49         *count = IN_TSBUTTON_COUNT;
50         return in_tsbutton_keys;
51 }
52
53 static int update_button(void)
54 {
55         struct tsdev *dev = tsdev;
56         struct ts_sample sample;
57         int sx = 0, sy = 0, sp = 0, updated = 0;
58
59         if (dev == NULL)
60                 return -1;
61
62         while (pts_read(dev, &sample, 1) > 0) {
63                 sx = sample.x;
64                 sy = sample.y;
65                 sp = sample.pressure;
66                 updated = 1;
67         }
68
69         if (updated) {
70                 if (sp == 0)
71                         tsbutton_down_id = -1;
72                 else {
73                         // 0 1
74                         // 2 3
75                         tsbutton_down_id = 0;
76                         if (sx > TS_WIDTH / 2)
77                                 tsbutton_down_id++;
78                         if (sy > TS_HEIGHT / 2)
79                                 tsbutton_down_id += 2;
80                 }
81         }
82
83         return 0;
84 }
85
86 static int in_tsbutton_update(void *drv_data, const int *binds, int *result)
87 {
88         int ret, t;
89         
90         ret = update_button();
91         if (ret != 0)
92                 return ret;
93
94         if (tsbutton_down_id >= 0)
95                 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
96                         result[t] |= binds[IN_BIND_OFFS(tsbutton_down_id, t)];
97
98         return 0;
99 }
100
101 static int in_tsbutton_update_keycode(void *data, int *is_down)
102 {
103         int ret, ret_kc = -1, ret_down = 0;
104
105         ret = update_button();
106         if (ret != 0)
107                 return ret;
108
109         if (tsbutton_down_id == last_tsbutton_id)
110                 return -1;
111
112         if (tsbutton_down_id >= 0) {
113                 if (last_tsbutton_id >= 0) {
114                         ret_kc = last_tsbutton_id;
115                         last_tsbutton_id = -1;
116                 }
117                 else {
118                         ret_down = 1;
119                         ret_kc = tsbutton_down_id;
120                         last_tsbutton_id = tsbutton_down_id;
121                 }
122         }
123         else {
124                 ret_kc = last_tsbutton_id;
125                 last_tsbutton_id = -1;
126         }
127
128         if (is_down != NULL)
129                 *is_down = ret_down;
130
131         return ret_kc;
132 }
133
134 static const in_drv_t in_tsbutton_drv = {
135         .prefix         = IN_TSBUTTON_PREFIX,
136         .probe          = in_tsbutton_probe,
137         .get_key_names  = in_tsbutton_get_key_names,
138         .update         = in_tsbutton_update,
139         .update_keycode = in_tsbutton_update_keycode,
140 };
141
142 void in_tsbutton_init(void)
143 {
144         tsbutton_down_id = last_tsbutton_id = -1;
145         in_register_driver(&in_tsbutton_drv);
146 }
147