diff options
Diffstat (limited to 'common/mousekey.c')
| -rw-r--r-- | common/mousekey.c | 120 |
1 files changed, 73 insertions, 47 deletions
diff --git a/common/mousekey.c b/common/mousekey.c index 1d35355b4..7f8e860aa 100644 --- a/common/mousekey.c +++ b/common/mousekey.c | |||
| @@ -26,7 +26,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 26 | 26 | ||
| 27 | 27 | ||
| 28 | static report_mouse_t report; | 28 | static report_mouse_t report; |
| 29 | static report_mouse_t report_prev; | ||
| 30 | 29 | ||
| 31 | static uint8_t mousekey_repeat = 0; | 30 | static uint8_t mousekey_repeat = 0; |
| 32 | 31 | ||
| @@ -38,84 +37,111 @@ static void mousekey_debug(void); | |||
| 38 | * see wikipedia http://en.wikipedia.org/wiki/Mouse_keys | 37 | * see wikipedia http://en.wikipedia.org/wiki/Mouse_keys |
| 39 | */ | 38 | */ |
| 40 | #ifndef MOUSEKEY_DELAY_TIME | 39 | #ifndef MOUSEKEY_DELAY_TIME |
| 41 | # define MOUSEKEY_DELAY_TIME 255 | 40 | # define MOUSEKEY_DELAY_TIME 20 |
| 42 | #endif | 41 | #endif |
| 43 | 42 | ||
| 43 | #define MOUSEKEY_MOVE_INIT 5 | ||
| 44 | #define MOUSEKEY_WHEEL_INIT 1 | ||
| 45 | #define MOUSEKEY_MOVE_ACCEL 5 | ||
| 46 | #define MOUSEKEY_WHEEL_ACCEL 1 | ||
| 47 | |||
| 48 | static uint16_t last_timer = 0; | ||
| 49 | |||
| 44 | // acceleration parameters | 50 | // acceleration parameters |
| 45 | uint8_t mousekey_move_unit = 2; | 51 | //uint8_t mousekey_move_unit = 2; |
| 46 | uint8_t mousekey_resolution = 5; | 52 | //uint8_t mousekey_resolution = 5; |
| 47 | 53 | ||
| 48 | 54 | ||
| 49 | static inline uint8_t move_unit(void) | 55 | static inline uint8_t move_unit(void) |
| 50 | { | 56 | { |
| 51 | uint16_t unit = 5 + mousekey_repeat*2; | 57 | uint16_t unit = 5 + mousekey_repeat*4; |
| 52 | return (unit > 63 ? 63 : unit); | 58 | return (unit > 63 ? 63 : unit); |
| 53 | } | 59 | } |
| 54 | 60 | ||
| 55 | void mousekey_decode(uint8_t code) | 61 | void mousekey_task(void) |
| 56 | { | ||
| 57 | if (code == KB_MS_UP) report.y = -move_unit(); | ||
| 58 | else if (code == KB_MS_DOWN) report.y = move_unit(); | ||
| 59 | else if (code == KB_MS_LEFT) report.x = -move_unit(); | ||
| 60 | else if (code == KB_MS_RIGHT) report.x = move_unit(); | ||
| 61 | else if (code == KB_MS_BTN1) report.buttons |= MOUSE_BTN1; | ||
| 62 | else if (code == KB_MS_BTN2) report.buttons |= MOUSE_BTN2; | ||
| 63 | else if (code == KB_MS_BTN3) report.buttons |= MOUSE_BTN3; | ||
| 64 | else if (code == KB_MS_BTN4) report.buttons |= MOUSE_BTN4; | ||
| 65 | else if (code == KB_MS_BTN5) report.buttons |= MOUSE_BTN5; | ||
| 66 | else if (code == KB_MS_WH_UP) report.v += move_unit()/4; | ||
| 67 | else if (code == KB_MS_WH_DOWN) report.v -= move_unit()/4; | ||
| 68 | else if (code == KB_MS_WH_LEFT) report.h -= move_unit()/4; | ||
| 69 | else if (code == KB_MS_WH_RIGHT)report.h += move_unit()/4; | ||
| 70 | } | ||
| 71 | |||
| 72 | bool mousekey_changed(void) | ||
| 73 | { | ||
| 74 | return (report.buttons != report_prev.buttons || | ||
| 75 | report.x || report.y || report.v || report.h); | ||
| 76 | } | ||
| 77 | |||
| 78 | void mousekey_send(void) | ||
| 79 | { | 62 | { |
| 80 | static uint16_t last_timer = 0; | 63 | if (timer_elapsed(last_timer) < MOUSEKEY_DELAY_TIME) |
| 81 | |||
| 82 | if (!mousekey_changed()) { | ||
| 83 | mousekey_repeat = 0; | ||
| 84 | mousekey_clear_report(); | ||
| 85 | return; | 64 | return; |
| 86 | } | ||
| 87 | 65 | ||
| 88 | // send immediately when buttun state is changed | 66 | if (report.x == 0 && report.y == 0 && report.v == 0 && report.h == 0) |
| 89 | if (report.buttons == report_prev.buttons) { | 67 | return; |
| 90 | if (timer_elapsed(last_timer) < 100) { | ||
| 91 | mousekey_clear_report(); | ||
| 92 | return; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | 68 | ||
| 96 | if (mousekey_repeat != 0xFF) { | 69 | if (mousekey_repeat != UINT8_MAX) |
| 97 | mousekey_repeat++; | 70 | mousekey_repeat++; |
| 98 | } | 71 | |
| 72 | |||
| 73 | if (report.x > 0) report.x = move_unit(); | ||
| 74 | if (report.x < 0) report.x = move_unit() * -1; | ||
| 75 | if (report.y > 0) report.y = move_unit(); | ||
| 76 | if (report.y < 0) report.y = move_unit() * -1; | ||
| 99 | 77 | ||
| 100 | if (report.x && report.y) { | 78 | if (report.x && report.y) { |
| 101 | report.x *= 0.7; | 79 | report.x *= 0.7; |
| 102 | report.y *= 0.7; | 80 | report.y *= 0.7; |
| 103 | } | 81 | } |
| 104 | 82 | ||
| 83 | if (report.v > 0) report.v = move_unit(); | ||
| 84 | if (report.v < 0) report.v = move_unit() * -1; | ||
| 85 | if (report.h > 0) report.h = move_unit(); | ||
| 86 | if (report.h < 0) report.h = move_unit() * -1; | ||
| 87 | |||
| 88 | mousekey_send(); | ||
| 89 | } | ||
| 90 | |||
| 91 | void mousekey_on(uint8_t code) | ||
| 92 | { | ||
| 93 | if (code == KB_MS_UP) report.y = MOUSEKEY_MOVE_INIT * -1; | ||
| 94 | else if (code == KB_MS_DOWN) report.y = MOUSEKEY_MOVE_INIT; | ||
| 95 | else if (code == KB_MS_LEFT) report.x = MOUSEKEY_MOVE_INIT * -1; | ||
| 96 | else if (code == KB_MS_RIGHT) report.x = MOUSEKEY_MOVE_INIT; | ||
| 97 | else if (code == KB_MS_WH_UP) report.v = MOUSEKEY_WHEEL_INIT; | ||
| 98 | else if (code == KB_MS_WH_DOWN) report.v = MOUSEKEY_WHEEL_INIT * -1; | ||
| 99 | else if (code == KB_MS_WH_LEFT) report.h = MOUSEKEY_WHEEL_INIT * -1; | ||
| 100 | else if (code == KB_MS_WH_RIGHT) report.h = MOUSEKEY_WHEEL_INIT; | ||
| 101 | else if (code == KB_MS_BTN1) report.buttons |= MOUSE_BTN1; | ||
| 102 | else if (code == KB_MS_BTN2) report.buttons |= MOUSE_BTN2; | ||
| 103 | else if (code == KB_MS_BTN3) report.buttons |= MOUSE_BTN3; | ||
| 104 | else if (code == KB_MS_BTN4) report.buttons |= MOUSE_BTN4; | ||
| 105 | else if (code == KB_MS_BTN5) report.buttons |= MOUSE_BTN5; | ||
| 106 | } | ||
| 107 | |||
| 108 | void mousekey_off(uint8_t code) | ||
| 109 | { | ||
| 110 | if (code == KB_MS_UP && report.y < 0) report.y = 0; | ||
| 111 | else if (code == KB_MS_DOWN && report.y > 0) report.y = 0; | ||
| 112 | else if (code == KB_MS_LEFT && report.x < 0) report.x = 0; | ||
| 113 | else if (code == KB_MS_RIGHT && report.x > 0) report.x = 0; | ||
| 114 | else if (code == KB_MS_WH_UP && report.v > 0) report.v = 0; | ||
| 115 | else if (code == KB_MS_WH_DOWN && report.v < 0) report.v = 0; | ||
| 116 | else if (code == KB_MS_WH_LEFT && report.h < 0) report.h = 0; | ||
| 117 | else if (code == KB_MS_WH_RIGHT && report.h > 0) report.h = 0; | ||
| 118 | else if (code == KB_MS_BTN1) report.buttons &= ~MOUSE_BTN1; | ||
| 119 | else if (code == KB_MS_BTN2) report.buttons &= ~MOUSE_BTN2; | ||
| 120 | else if (code == KB_MS_BTN3) report.buttons &= ~MOUSE_BTN3; | ||
| 121 | else if (code == KB_MS_BTN4) report.buttons &= ~MOUSE_BTN4; | ||
| 122 | else if (code == KB_MS_BTN5) report.buttons &= ~MOUSE_BTN5; | ||
| 123 | |||
| 124 | if (report.x == 0 && report.y == 0 && report.v == 0 && report.h == 0) | ||
| 125 | mousekey_repeat = 0; | ||
| 126 | } | ||
| 127 | |||
| 128 | void mousekey_send(void) | ||
| 129 | { | ||
| 105 | mousekey_debug(); | 130 | mousekey_debug(); |
| 106 | host_mouse_send(&report); | 131 | host_mouse_send(&report); |
| 107 | report_prev = report; | ||
| 108 | last_timer = timer_read(); | 132 | last_timer = timer_read(); |
| 109 | mousekey_clear_report(); | ||
| 110 | } | 133 | } |
| 111 | 134 | ||
| 112 | void mousekey_clear_report(void) | 135 | void mousekey_clear(void) |
| 113 | { | 136 | { |
| 137 | report = (report_mouse_t){}; | ||
| 138 | /* | ||
| 114 | report.buttons = 0; | 139 | report.buttons = 0; |
| 115 | report.x = 0; | 140 | report.x = 0; |
| 116 | report.y = 0; | 141 | report.y = 0; |
| 117 | report.v = 0; | 142 | report.v = 0; |
| 118 | report.h = 0; | 143 | report.h = 0; |
| 144 | */ | ||
| 119 | } | 145 | } |
| 120 | 146 | ||
| 121 | static void mousekey_debug(void) | 147 | static void mousekey_debug(void) |
