|
| 1 | +/* |
| 2 | + Expost absolute mouse HID descriptor and report |
| 3 | + Taken from @tobozo PR https://github.com/hathach/tinyusb/pull/1363 |
| 4 | + TODO - remove once that PR merged with TinyUSB |
| 5 | +
|
| 6 | + Copyright (c) 2023 Earle F. Philhower, III <earlephilhower@yahoo.com> |
| 7 | +
|
| 8 | + This library is free software; you can redistribute it and/or |
| 9 | + modify it under the terms of the GNU Lesser General Public |
| 10 | + License as published by the Free Software Foundation; either |
| 11 | + version 2.1 of the License, or (at your option) any later version. |
| 12 | +
|
| 13 | + This library is distributed in the hope that it will be useful, |
| 14 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | + Lesser General Public License for more details. |
| 17 | +
|
| 18 | + You should have received a copy of the GNU Lesser General Public |
| 19 | + License along with this library; if not, write to the Free Software |
| 20 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | +*/ |
| 22 | + |
| 23 | +#pragma once |
| 24 | + |
| 25 | +#include "tusb.h" |
| 26 | +#include "class/hid/hid_device.h" |
| 27 | + |
| 28 | +// Absolute Mouse: same as the Standard (relative) Mouse Report but |
| 29 | +// with int16_t instead of int8_t for X and Y coordinates. |
| 30 | +typedef struct TU_ATTR_PACKED { |
| 31 | + uint8_t buttons; /**< buttons mask for currently pressed buttons in the mouse. */ |
| 32 | + int16_t x; /**< Current x position of the mouse. */ |
| 33 | + int16_t y; /**< Current y position of the mouse. */ |
| 34 | + int8_t wheel; /**< Current delta wheel movement on the mouse. */ |
| 35 | + int8_t pan; // using AC Pan |
| 36 | +} hid_abs_mouse_report_t; |
| 37 | + |
| 38 | + |
| 39 | +// Absolute Mouse Report Descriptor Template |
| 40 | +#define TUD_HID_REPORT_DESC_ABSMOUSE(...) \ |
| 41 | + HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ |
| 42 | + HID_USAGE ( HID_USAGE_DESKTOP_MOUSE ) ,\ |
| 43 | + HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\ |
| 44 | + /* Report ID if any */\ |
| 45 | + __VA_ARGS__ \ |
| 46 | + HID_USAGE ( HID_USAGE_DESKTOP_POINTER ) ,\ |
| 47 | + HID_COLLECTION ( HID_COLLECTION_PHYSICAL ) ,\ |
| 48 | + HID_USAGE_PAGE ( HID_USAGE_PAGE_BUTTON ) ,\ |
| 49 | + HID_USAGE_MIN ( 1 ) ,\ |
| 50 | + HID_USAGE_MAX ( 5 ) ,\ |
| 51 | + HID_LOGICAL_MIN ( 0 ) ,\ |
| 52 | + HID_LOGICAL_MAX ( 1 ) ,\ |
| 53 | + /* Left, Right, Middle, Backward, Forward buttons */ \ |
| 54 | + HID_REPORT_COUNT( 5 ) ,\ |
| 55 | + HID_REPORT_SIZE ( 1 ) ,\ |
| 56 | + HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\ |
| 57 | + /* 3 bit padding */ \ |
| 58 | + HID_REPORT_COUNT( 1 ) ,\ |
| 59 | + HID_REPORT_SIZE ( 3 ) ,\ |
| 60 | + HID_INPUT ( HID_CONSTANT ) ,\ |
| 61 | + HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ |
| 62 | + /* X, Y absolute position [0, 32767] */ \ |
| 63 | + HID_USAGE ( HID_USAGE_DESKTOP_X ) ,\ |
| 64 | + HID_USAGE ( HID_USAGE_DESKTOP_Y ) ,\ |
| 65 | + HID_LOGICAL_MIN ( 0x00 ) ,\ |
| 66 | + HID_LOGICAL_MAX_N( 0x7FFF, 2 ) ,\ |
| 67 | + HID_REPORT_SIZE ( 16 ) ,\ |
| 68 | + HID_REPORT_COUNT ( 2 ) ,\ |
| 69 | + HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\ |
| 70 | + /* Vertical wheel scroll [-127, 127] */ \ |
| 71 | + HID_USAGE ( HID_USAGE_DESKTOP_WHEEL ) ,\ |
| 72 | + HID_LOGICAL_MIN ( 0x81 ) ,\ |
| 73 | + HID_LOGICAL_MAX ( 0x7f ) ,\ |
| 74 | + HID_REPORT_COUNT( 1 ) ,\ |
| 75 | + HID_REPORT_SIZE ( 8 ) ,\ |
| 76 | + HID_INPUT ( HID_DATA | HID_VARIABLE | HID_RELATIVE ) ,\ |
| 77 | + HID_USAGE_PAGE ( HID_USAGE_PAGE_CONSUMER ), \ |
| 78 | + /* Horizontal wheel scroll [-127, 127] */ \ |
| 79 | + HID_USAGE_N ( HID_USAGE_CONSUMER_AC_PAN, 2 ), \ |
| 80 | + HID_LOGICAL_MIN ( 0x81 ), \ |
| 81 | + HID_LOGICAL_MAX ( 0x7f ), \ |
| 82 | + HID_REPORT_COUNT( 1 ), \ |
| 83 | + HID_REPORT_SIZE ( 8 ), \ |
| 84 | + HID_INPUT ( HID_DATA | HID_VARIABLE | HID_RELATIVE ), \ |
| 85 | + HID_COLLECTION_END , \ |
| 86 | + HID_COLLECTION_END \ |
| 87 | + |
| 88 | + |
| 89 | +static inline bool tud_hid_abs_mouse_report(uint8_t report_id, |
| 90 | + uint8_t buttons, int16_t x, int16_t y, int8_t vertical, int8_t horizontal) { |
| 91 | + hid_abs_mouse_report_t report = { |
| 92 | + .buttons = buttons, |
| 93 | + .x = x, |
| 94 | + .y = y, |
| 95 | + .wheel = vertical, |
| 96 | + .pan = horizontal |
| 97 | + }; |
| 98 | + |
| 99 | + return tud_hid_n_report(0, report_id, &report, sizeof(report)); |
| 100 | +} |
| 101 | + |
0 commit comments