|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2022, RT-Thread Development Team |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + * 2022-3-08 GuEe-GUI the first version |
| 9 | + */ |
| 10 | + |
| 11 | +#ifndef __INPUT_H__ |
| 12 | +#define __INPUT_H__ |
| 13 | + |
| 14 | +#include <rtthread.h> |
| 15 | + |
| 16 | +#include <bitmap.h> |
| 17 | +#include <drivers/dev_touch.h> |
| 18 | +#include <dt-bindings/input/event-codes.h> |
| 19 | + |
| 20 | +struct rt_input_poller; |
| 21 | +struct rt_input_absinfo; |
| 22 | + |
| 23 | +struct rt_input_event |
| 24 | +{ |
| 25 | + rt_tick_t tick; |
| 26 | + |
| 27 | + rt_uint16_t type; |
| 28 | + rt_uint16_t code; |
| 29 | + rt_int32_t value; |
| 30 | +}; |
| 31 | + |
| 32 | +struct rt_input_device |
| 33 | +{ |
| 34 | + struct rt_device parent; |
| 35 | + |
| 36 | + RT_BITMAP_DECLARE(cap, EV_CNT); |
| 37 | + RT_BITMAP_DECLARE(key_map, KEY_CNT); |
| 38 | + RT_BITMAP_DECLARE(rel_map, REL_CNT); |
| 39 | + RT_BITMAP_DECLARE(abs_map, ABS_CNT); |
| 40 | + |
| 41 | + rt_list_t list; |
| 42 | + rt_list_t handler_nodes; |
| 43 | + struct rt_spinlock lock; |
| 44 | + |
| 45 | + rt_err_t (*trigger)(struct rt_input_device *idev, |
| 46 | + rt_uint16_t type, rt_uint16_t code, rt_int32_t value); |
| 47 | + |
| 48 | + struct rt_input_poller *poller; |
| 49 | + struct rt_input_absinfo *absinfo; |
| 50 | +#ifdef RT_INPUT_TOUCHSCREEN |
| 51 | + void *touch; |
| 52 | +#endif |
| 53 | +#ifdef RT_INPUT_UAPI |
| 54 | + void *uapi; |
| 55 | +#endif |
| 56 | +}; |
| 57 | + |
| 58 | +struct rt_input_handler |
| 59 | +{ |
| 60 | + rt_list_t list; |
| 61 | + struct rt_input_device *idev; |
| 62 | + |
| 63 | + rt_bool_t (*identify)(struct rt_input_handler *handler, struct rt_input_device *idev); |
| 64 | + rt_bool_t (*callback)(struct rt_input_handler *handler, struct rt_input_event *ev); |
| 65 | + |
| 66 | + void *priv; |
| 67 | +}; |
| 68 | + |
| 69 | +struct rt_input_poller |
| 70 | +{ |
| 71 | +#define RT_INPUT_POLL_INTERVAL_DEFAULT 17 /* 60fps */ |
| 72 | + rt_uint32_t interval; |
| 73 | + struct rt_timer timer; |
| 74 | + |
| 75 | + void (*poll)(struct rt_input_device *idev); |
| 76 | +}; |
| 77 | + |
| 78 | +struct rt_input_absinfo |
| 79 | +{ |
| 80 | + rt_int32_t value; |
| 81 | + rt_int32_t minimum; |
| 82 | + rt_int32_t maximum; |
| 83 | + rt_int32_t fuzz; |
| 84 | + rt_int32_t flat; |
| 85 | + rt_int32_t resolution; |
| 86 | +}; |
| 87 | + |
| 88 | +rt_err_t rt_input_device_register(struct rt_input_device *idev); |
| 89 | +rt_err_t rt_input_device_unregister(struct rt_input_device *idev); |
| 90 | + |
| 91 | +rt_err_t rt_input_set_capability(struct rt_input_device *idev, |
| 92 | + rt_uint16_t type, rt_uint16_t code); |
| 93 | + |
| 94 | +rt_err_t rt_input_set_absinfo(struct rt_input_device *idev, rt_uint32_t axis, |
| 95 | + rt_int32_t min, rt_int32_t max, rt_int32_t fuzz, rt_int32_t flat); |
| 96 | + |
| 97 | +rt_err_t rt_input_setup_touch(struct rt_input_device *idev, |
| 98 | + rt_uint32_t num_slots, struct rt_touch_info *info); |
| 99 | +rt_err_t rt_input_parse_touch_position(struct rt_input_device *idev, |
| 100 | + rt_uint32_t *out_x, rt_uint32_t *out_y); |
| 101 | + |
| 102 | +rt_err_t rt_input_setup_polling(struct rt_input_device *idev, |
| 103 | + void (*work)(struct rt_input_device *idev)); |
| 104 | +rt_err_t rt_input_set_poll_interval(struct rt_input_device *idev, |
| 105 | + rt_uint32_t interval_ms); |
| 106 | + |
| 107 | +void rt_input_remove_config(struct rt_input_device *idev); |
| 108 | + |
| 109 | +rt_err_t rt_input_trigger(struct rt_input_device *idev, |
| 110 | + rt_uint16_t type, rt_uint16_t code, rt_int32_t value); |
| 111 | + |
| 112 | +void rt_input_event(struct rt_input_device *idev, |
| 113 | + rt_uint16_t type, rt_uint16_t code, rt_int32_t value); |
| 114 | + |
| 115 | +rt_inline void rt_input_report_key(struct rt_input_device *idev, |
| 116 | + rt_uint16_t code, rt_int32_t value) |
| 117 | +{ |
| 118 | + rt_input_event(idev, EV_KEY, code, !!value); |
| 119 | +} |
| 120 | + |
| 121 | +rt_inline void rt_input_report_rel(struct rt_input_device *idev, |
| 122 | + rt_uint16_t code, rt_int32_t value) |
| 123 | +{ |
| 124 | + rt_input_event(idev, EV_REL, code, value); |
| 125 | +} |
| 126 | + |
| 127 | +rt_inline void rt_input_report_abs(struct rt_input_device *idev, |
| 128 | + rt_uint16_t code, rt_int32_t value) |
| 129 | +{ |
| 130 | + rt_input_event(idev, EV_ABS, code, value); |
| 131 | +} |
| 132 | + |
| 133 | +rt_inline void rt_input_report_touch_slot(struct rt_input_device *idev, |
| 134 | + rt_uint32_t slot) |
| 135 | +{ |
| 136 | + rt_input_event(idev, EV_ABS, ABS_MT_SLOT, slot); |
| 137 | +} |
| 138 | + |
| 139 | +rt_bool_t rt_input_report_touch_inactive(struct rt_input_device *idev, |
| 140 | + rt_bool_t active); |
| 141 | + |
| 142 | +void rt_input_report_touch_position(struct rt_input_device *idev, |
| 143 | + rt_uint32_t x, rt_uint32_t y, rt_bool_t multitouch); |
| 144 | + |
| 145 | +rt_inline void rt_input_sync(struct rt_input_device *idev) |
| 146 | +{ |
| 147 | + rt_input_event(idev, EV_SYN, SYN_REPORT, 0); |
| 148 | +} |
| 149 | + |
| 150 | +rt_err_t rt_input_add_handler(struct rt_input_handler *handler); |
| 151 | +rt_err_t rt_input_del_handler(struct rt_input_handler *handler); |
| 152 | + |
| 153 | +#endif /* __INPUT_H__ */ |
0 commit comments