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