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 "pl_gun_ts.h"
16 #include "in_tsbutton.h"
18 #define IN_TSBUTTON_PREFIX "tsbutton:"
19 #define IN_TSBUTTON_COUNT 4
20 static int tsbutton_down_id;
21 static int last_tsbutton_id;
26 // HACK: stealing this from plugin_lib
29 static const char * const in_tsbutton_keys[IN_TSBUTTON_COUNT] = {
30 "TS1", "TS2", "TS3", "TS4",
33 static void in_tsbutton_probe(void)
35 struct tsdev *dev = tsdev;
37 fprintf(stderr, "in_tsbutton_probe: missing tsdev\n");
41 in_register(IN_TSBUTTON_PREFIX "touchscreen as buttons",
42 pl_gun_ts_get_fd(dev), NULL, IN_TSBUTTON_COUNT, in_tsbutton_keys, 0);
45 static const char * const *
46 in_tsbutton_get_key_names(int *count)
48 *count = IN_TSBUTTON_COUNT;
49 return in_tsbutton_keys;
52 static int update_button(void)
54 struct tsdev *dev = tsdev;
55 int sx = 0, sy = 0, sp = 0;
60 if (pl_gun_ts_update_raw(dev, &sx, &sy, &sp)) {
62 tsbutton_down_id = -1;
67 if (sx > TS_WIDTH / 2)
69 if (sy > TS_HEIGHT / 2)
70 tsbutton_down_id += 2;
77 static int in_tsbutton_update(void *drv_data, const int *binds, int *result)
81 ret = update_button();
85 if (tsbutton_down_id >= 0)
86 for (t = 0; t < IN_BINDTYPE_COUNT; t++)
87 result[t] |= binds[IN_BIND_OFFS(tsbutton_down_id, t)];
92 static int in_tsbutton_update_keycode(void *data, int *is_down)
94 int ret, ret_kc = -1, ret_down = 0;
96 ret = update_button();
100 if (tsbutton_down_id == last_tsbutton_id)
103 if (tsbutton_down_id >= 0) {
104 if (last_tsbutton_id >= 0) {
105 ret_kc = last_tsbutton_id;
106 last_tsbutton_id = -1;
110 ret_kc = tsbutton_down_id;
111 last_tsbutton_id = tsbutton_down_id;
115 ret_kc = last_tsbutton_id;
116 last_tsbutton_id = -1;
125 static const in_drv_t in_tsbutton_drv = {
126 .prefix = IN_TSBUTTON_PREFIX,
127 .probe = in_tsbutton_probe,
128 .get_key_names = in_tsbutton_get_key_names,
129 .update = in_tsbutton_update,
130 .update_keycode = in_tsbutton_update_keycode,
133 void in_tsbutton_init(void)
135 tsbutton_down_id = last_tsbutton_id = -1;
136 in_register_driver(&in_tsbutton_drv);