2 * (C) GraÅžvydas "notaz" Ignotas, 2011
4 * This work is licensed under the terms of any of these licenses
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.
14 #include "common/input.h"
15 #include "in_tsbutton.h"
17 #define IN_TSBUTTON_PREFIX "tsbutton:"
18 #define IN_TSBUTTON_COUNT 4
19 static int tsbutton_down_id;
20 static int last_tsbutton_id;
25 // HACK: stealing this from plugin_lib
27 extern int (*pts_read)(struct tsdev *dev, struct ts_sample *sample, int nr);
28 extern int (*pts_fd)(struct tsdev *dev);
30 static const char * const in_tsbutton_keys[IN_TSBUTTON_COUNT] = {
31 "TS1", "TS2", "TS3", "TS4",
34 static void in_tsbutton_probe(void)
36 struct tsdev *dev = tsdev;
38 fprintf(stderr, "in_tsbutton_probe: missing tsdev\n");
42 in_register(IN_TSBUTTON_PREFIX "touchscreen as buttons",
43 pts_fd(dev), NULL, IN_TSBUTTON_COUNT, in_tsbutton_keys, 0);
46 static const char * const *
47 in_tsbutton_get_key_names(int *count)
49 *count = IN_TSBUTTON_COUNT;
50 return in_tsbutton_keys;
53 static int update_button(void)
55 struct tsdev *dev = tsdev;
56 struct ts_sample sample;
57 int sx = 0, sy = 0, sp = 0, updated = 0;
62 while (pts_read(dev, &sample, 1) > 0) {
71 tsbutton_down_id = -1;
76 if (sx > TS_WIDTH / 2)
78 if (sy > TS_HEIGHT / 2)
79 tsbutton_down_id += 2;
86 static int in_tsbutton_update(void *drv_data, const int *binds, int *result)
90 ret = update_button();
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)];
101 static int in_tsbutton_update_keycode(void *data, int *is_down)
103 int ret, ret_kc = -1, ret_down = 0;
105 ret = update_button();
109 if (tsbutton_down_id == last_tsbutton_id)
112 if (tsbutton_down_id >= 0) {
113 if (last_tsbutton_id >= 0) {
114 ret_kc = last_tsbutton_id;
115 last_tsbutton_id = -1;
119 ret_kc = tsbutton_down_id;
120 last_tsbutton_id = tsbutton_down_id;
124 ret_kc = last_tsbutton_id;
125 last_tsbutton_id = -1;
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,
142 void in_tsbutton_init(void)
144 tsbutton_down_id = last_tsbutton_id = -1;
145 in_register_driver(&in_tsbutton_drv);