input_touch.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #define DBG_TAG "input.touch"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.h>
  16. struct input_touch_device
  17. {
  18. struct rt_touch_device parent;
  19. struct rt_input_handler handler;
  20. rt_bool_t enabled;
  21. rt_uint16_t slot;
  22. rt_uint16_t down;
  23. struct rt_touch_data points[];
  24. };
  25. struct input_touch_properties
  26. {
  27. rt_uint32_t max_x;
  28. rt_uint32_t max_y;
  29. rt_bool_t invert_x;
  30. rt_bool_t invert_y;
  31. rt_bool_t swap_x_y;
  32. rt_uint16_t track_id;
  33. rt_uint32_t num_slots;
  34. struct input_touch_device *touch_dev;
  35. };
  36. static rt_size_t input_touch_readpoint(struct rt_touch_device *touch,
  37. void *buf, rt_size_t touch_num)
  38. {
  39. struct input_touch_device *touch_dev;
  40. touch_dev = rt_container_of(touch, struct input_touch_device, parent);
  41. rt_memcpy(buf, &touch_dev->points[touch_dev->slot], sizeof(struct rt_touch_data));
  42. return 1;
  43. }
  44. static rt_err_t input_touch_control(struct rt_touch_device *touch, int cmd, void *arg)
  45. {
  46. rt_err_t err = RT_EOK;
  47. struct input_touch_device *touch_dev;
  48. if (!arg)
  49. {
  50. return -RT_EINVAL;
  51. }
  52. touch_dev = rt_container_of(touch, struct input_touch_device, parent);
  53. switch (cmd)
  54. {
  55. case RT_TOUCH_CTRL_GET_ID:
  56. *(int *)arg = 0;
  57. break;
  58. case RT_TOUCH_CTRL_GET_INFO:
  59. rt_memcpy(arg, &touch_dev->parent.info, sizeof(touch_dev->parent.info));
  60. break;
  61. case RT_TOUCH_CTRL_SET_X_RANGE:
  62. touch_dev->parent.info.range_x = *(rt_uint16_t *)arg;
  63. break;
  64. case RT_TOUCH_CTRL_SET_Y_RANGE:
  65. touch_dev->parent.info.range_y = *(rt_uint16_t *)arg;
  66. break;
  67. case RT_TOUCH_CTRL_SET_X_TO_Y:
  68. break;
  69. case RT_TOUCH_CTRL_DISABLE_INT:
  70. case RT_TOUCH_CTRL_POWER_OFF:
  71. touch_dev->enabled = RT_FALSE;
  72. break;
  73. case RT_TOUCH_CTRL_ENABLE_INT:
  74. case RT_TOUCH_CTRL_POWER_ON:
  75. touch_dev->enabled = RT_TRUE;
  76. break;
  77. case RT_TOUCH_CTRL_GET_STATUS:
  78. *(int *)arg = touch_dev->enabled;
  79. break;
  80. default:
  81. err = -RT_ENOSYS;
  82. break;
  83. }
  84. return err;
  85. }
  86. const static struct rt_touch_ops input_touch_ops =
  87. {
  88. .touch_readpoint = input_touch_readpoint,
  89. .touch_control = input_touch_control,
  90. };
  91. static rt_bool_t input_touch_cb(struct rt_input_handler *handler,
  92. struct rt_input_event *ev)
  93. {
  94. struct input_touch_device *touch_dev;
  95. touch_dev = rt_container_of(handler, struct input_touch_device, handler);
  96. if (touch_dev->enabled)
  97. {
  98. struct rt_touch_data *point = &touch_dev->points[touch_dev->slot];
  99. if (ev->type == EV_ABS)
  100. {
  101. struct rt_input_device *idev = handler->idev;
  102. struct rt_input_absinfo *absinfo = &idev->absinfo[touch_dev->slot];
  103. switch (ev->code)
  104. {
  105. case ABS_MT_SLOT:
  106. touch_dev->slot = ev->value;
  107. break;
  108. case ABS_MT_TRACKING_ID:
  109. point->timestamp = ev->tick;
  110. if (ev->value == (typeof(ev->code))-1)
  111. {
  112. point->event = RT_TOUCH_EVENT_UP;
  113. touch_dev->down = 0;
  114. rt_hw_touch_isr(&touch_dev->parent);
  115. break;
  116. }
  117. point->track_id = ev->value;
  118. point->event = touch_dev->down ? RT_TOUCH_EVENT_MOVE : RT_TOUCH_EVENT_DOWN;
  119. ++touch_dev->down;
  120. break;
  121. case ABS_MT_POSITION_X:
  122. case ABS_X:
  123. point->x_coordinate = (ev->value * touch_dev->parent.info.range_x) /
  124. (absinfo->maximum - absinfo->minimum);
  125. break;
  126. case ABS_MT_POSITION_Y:
  127. case ABS_Y:
  128. point->y_coordinate = (ev->value * touch_dev->parent.info.range_y) /
  129. (absinfo->maximum - absinfo->minimum);
  130. break;
  131. }
  132. }
  133. else if (ev->type == EV_SYN && ev->code == SYN_REPORT && ev->value == 0)
  134. {
  135. rt_hw_touch_isr(&touch_dev->parent);
  136. }
  137. }
  138. return RT_FALSE;
  139. }
  140. void input_touch_register(struct rt_input_device *idev)
  141. {
  142. const char *dev_name;
  143. struct rt_touch_device *tdev;
  144. struct input_touch_device *touch_dev;
  145. struct input_touch_properties *prop = idev->touch;
  146. /* Only register rt_touch_device */
  147. if (!prop || !prop->touch_dev)
  148. {
  149. return;
  150. }
  151. touch_dev = prop->touch_dev;
  152. tdev = &touch_dev->parent;
  153. tdev->ops = &input_touch_ops;
  154. rt_dm_dev_set_name_auto(&tdev->parent, "touch");
  155. dev_name = rt_dm_dev_get_name(&tdev->parent);
  156. rt_hw_touch_register(tdev, dev_name, RT_DEVICE_FLAG_INT_RX, prop);
  157. touch_dev->enabled = RT_TRUE;
  158. touch_dev->handler.idev = idev;
  159. touch_dev->handler.callback = &input_touch_cb;
  160. rt_input_add_handler(&touch_dev->handler);
  161. }
  162. void input_touch_unregister(struct rt_input_device *idev)
  163. {
  164. struct rt_touch_device *tdev;
  165. struct input_touch_device *touch_dev;
  166. struct input_touch_properties *prop = idev->touch;
  167. if (!prop)
  168. {
  169. return;
  170. }
  171. if (prop->touch_dev)
  172. {
  173. goto _end;
  174. }
  175. touch_dev = prop->touch_dev;
  176. tdev = &touch_dev->parent;
  177. rt_input_del_handler(&touch_dev->handler);
  178. rt_device_unregister(&tdev->parent);
  179. _end:
  180. rt_free(idev->touch);
  181. idev->touch = RT_NULL;
  182. }
  183. static void input_touch_parse(struct rt_input_device *idev,
  184. rt_bool_t multitouch, struct input_touch_properties *prop)
  185. {
  186. rt_bool_t present = RT_TRUE;
  187. rt_uint32_t axis, axis_x, axis_y;
  188. rt_uint32_t minimum, maximum, fuzz;
  189. struct rt_device *dev = &idev->parent;
  190. struct rt_input_absinfo *absinfo = idev->absinfo;
  191. axis_x = multitouch ? ABS_MT_POSITION_X : ABS_X;
  192. axis_y = multitouch ? ABS_MT_POSITION_Y : ABS_Y;
  193. if ((present = rt_dm_dev_prop_read_u32(dev, "touchscreen-min-x", &minimum)))
  194. {
  195. minimum = absinfo[axis_x].minimum;
  196. }
  197. if ((present |= rt_dm_dev_prop_read_u32(dev, "touchscreen-size-x", &maximum)))
  198. {
  199. maximum = absinfo[axis_x].maximum + 1;
  200. }
  201. if ((present |= rt_dm_dev_prop_read_u32(dev, "touchscreen-fuzz-x", &fuzz)))
  202. {
  203. fuzz = absinfo[axis_x].fuzz;
  204. }
  205. if (present)
  206. {
  207. rt_input_set_absinfo(idev, axis_x, minimum, maximum - 1, fuzz, 0);
  208. }
  209. if ((present = rt_dm_dev_prop_read_u32(dev, "touchscreen-min-y", &minimum)))
  210. {
  211. minimum = absinfo[axis_y].minimum;
  212. }
  213. if ((present |= rt_dm_dev_prop_read_u32(dev, "touchscreen-size-y", &maximum)))
  214. {
  215. maximum = absinfo[axis_y].maximum + 1;
  216. }
  217. if ((present |= rt_dm_dev_prop_read_u32(dev, "touchscreen-fuzz-y", &fuzz)))
  218. {
  219. fuzz = absinfo[axis_y].fuzz;
  220. }
  221. if (present)
  222. {
  223. rt_input_set_absinfo(idev, axis_y, minimum, maximum - 1, fuzz, 0);
  224. }
  225. axis = multitouch ? ABS_MT_PRESSURE : ABS_PRESSURE;
  226. if ((present = rt_dm_dev_prop_read_u32(dev, "touchscreen-max-pressure", &maximum)))
  227. {
  228. maximum = absinfo[axis].maximum;
  229. }
  230. if ((present |= rt_dm_dev_prop_read_u32(dev, "touchscreen-fuzz-pressure", &fuzz)))
  231. {
  232. fuzz = absinfo[axis].fuzz;
  233. }
  234. if (present)
  235. {
  236. rt_input_set_absinfo(idev, axis, 0, maximum, fuzz, 0);
  237. }
  238. prop->max_x = absinfo[axis_x].maximum;
  239. prop->max_y = absinfo[axis_y].maximum;
  240. prop->invert_x = rt_dm_dev_prop_read_bool(dev, "touchscreen-inverted-x");
  241. if (prop->invert_x)
  242. {
  243. absinfo[axis_x].maximum -= absinfo[axis_x].minimum;
  244. absinfo[axis_x].minimum = 0;
  245. }
  246. prop->invert_y = rt_dm_dev_prop_read_bool(dev, "touchscreen-inverted-y");
  247. if (prop->invert_y)
  248. {
  249. absinfo[axis_y].maximum -= absinfo[axis_y].minimum;
  250. absinfo[axis_y].minimum = 0;
  251. }
  252. prop->swap_x_y = rt_dm_dev_prop_read_bool(dev, "touchscreen-swapped-x-y");
  253. if (prop->swap_x_y)
  254. {
  255. struct rt_input_absinfo swap_absinfo;
  256. rt_memcpy(&swap_absinfo, &idev->absinfo[axis_x], sizeof(swap_absinfo));
  257. rt_memcpy(&idev->absinfo[axis_x], &idev->absinfo[axis_y], sizeof(swap_absinfo));
  258. rt_memcpy(&idev->absinfo[axis_y], &swap_absinfo, sizeof(swap_absinfo));
  259. }
  260. }
  261. rt_err_t rt_input_setup_touch(struct rt_input_device *idev,
  262. rt_uint32_t num_slots, struct rt_touch_info *info)
  263. {
  264. rt_size_t alloc_size;
  265. rt_bool_t multitouch;
  266. struct input_touch_device *touch_dev;
  267. struct input_touch_properties *prop;
  268. if (!idev || idev->touch)
  269. {
  270. return -RT_EINVAL;
  271. }
  272. multitouch = !!num_slots;
  273. alloc_size = sizeof(*prop);
  274. if (info)
  275. {
  276. alloc_size += sizeof(*touch_dev);
  277. alloc_size += sizeof(*touch_dev->points) * rt_max_t(rt_uint32_t, num_slots, 1);
  278. }
  279. if (!(prop = rt_calloc(1, alloc_size)))
  280. {
  281. return -RT_ENOMEM;
  282. }
  283. idev->touch = prop;
  284. if ((prop->num_slots = num_slots))
  285. {
  286. rt_input_set_absinfo(idev, ABS_MT_SLOT, 0, num_slots - 1, 0, 0);
  287. rt_input_set_absinfo(idev, ABS_MT_TRACKING_ID, 0, RT_UINT16_MAX, 0, 0);
  288. }
  289. input_touch_parse(idev, multitouch, prop);
  290. if (info)
  291. {
  292. rt_size_t points_nr = rt_max_t(rt_uint32_t, num_slots, 1);
  293. touch_dev = (void *)prop + sizeof(*prop);
  294. prop->touch_dev = touch_dev;
  295. for (int i = 0; i < points_nr; ++i)
  296. {
  297. touch_dev->points[i].width = 1;
  298. }
  299. rt_memcpy(&touch_dev->parent.info, info, sizeof(*info));
  300. info = &touch_dev->parent.info;
  301. info->point_num = info->point_num ? : prop->num_slots;
  302. info->range_x = info->range_x ? : prop->max_x;
  303. info->range_y = info->range_y ? : prop->max_y;
  304. }
  305. return RT_EOK;
  306. }
  307. rt_err_t rt_input_parse_touch_position(struct rt_input_device *idev,
  308. rt_uint32_t *out_x, rt_uint32_t *out_y)
  309. {
  310. struct input_touch_properties *prop;
  311. RT_ASSERT(idev != RT_NULL);
  312. RT_ASSERT(out_x != RT_NULL);
  313. RT_ASSERT(out_y != RT_NULL);
  314. prop = idev->touch;
  315. if (prop->invert_x)
  316. {
  317. *out_x = prop->max_x - *out_x;
  318. }
  319. if (prop->invert_y)
  320. {
  321. *out_y = prop->max_y - *out_y;
  322. }
  323. if (prop->swap_x_y)
  324. {
  325. *out_x ^= *out_y;
  326. *out_y ^= *out_x;
  327. *out_x ^= *out_y;
  328. }
  329. return RT_EOK;
  330. }
  331. rt_bool_t rt_input_report_touch_inactive(struct rt_input_device *idev,
  332. rt_bool_t active)
  333. {
  334. struct input_touch_properties *prop;
  335. RT_ASSERT(idev != RT_NULL);
  336. prop = idev->touch;
  337. if (!active)
  338. {
  339. rt_input_event(idev, EV_ABS, ABS_MT_TRACKING_ID, -1);
  340. return RT_FALSE;
  341. }
  342. rt_input_event(idev, EV_ABS, ABS_MT_TRACKING_ID, prop->track_id++);
  343. return RT_TRUE;
  344. }
  345. void rt_input_report_touch_position(struct rt_input_device *idev,
  346. rt_uint32_t x, rt_uint32_t y, rt_bool_t multitouch)
  347. {
  348. RT_ASSERT(idev != RT_NULL);
  349. rt_input_parse_touch_position(idev, &x, &y);
  350. rt_input_report_abs(idev, multitouch ? ABS_MT_POSITION_X : ABS_X, x);
  351. rt_input_report_abs(idev, multitouch ? ABS_MT_POSITION_Y : ABS_Y, y);
  352. }