input_power.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.power"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.h>
  16. struct input_event_cap
  17. {
  18. rt_uint16_t code;
  19. rt_int32_t value;
  20. rt_int32_t current;
  21. };
  22. static rt_bool_t power_input_identify(struct rt_input_handler *handler,
  23. struct rt_input_device *idev)
  24. {
  25. struct input_event_cap *cap = handler->priv;
  26. if (rt_bitmap_test_bit(idev->key_map, cap->code))
  27. {
  28. if (cap->value == cap->current)
  29. {
  30. return RT_TRUE;
  31. }
  32. ++cap->current;
  33. }
  34. return RT_FALSE;
  35. }
  36. static rt_bool_t power_input_callback(struct rt_input_handler *handler,
  37. struct rt_input_event *ev)
  38. {
  39. if (ev->code == KEY_POWER)
  40. {
  41. if (ev->value == 0)
  42. {
  43. rt_hw_cpu_shutdown();
  44. }
  45. return RT_TRUE;
  46. }
  47. else if (ev->code == KEY_RESTART)
  48. {
  49. if (ev->value == 0)
  50. {
  51. rt_hw_cpu_reset();
  52. }
  53. return RT_TRUE;
  54. }
  55. return RT_FALSE;
  56. }
  57. static rt_bool_t power_test_cap(struct rt_input_handler *handler,
  58. struct rt_input_device *idev)
  59. {
  60. struct input_event_cap *cap = handler->priv;
  61. if (rt_bitmap_test_bit(idev->key_map, cap->code))
  62. {
  63. ++cap->value;
  64. }
  65. return RT_FALSE;
  66. }
  67. static void power_handler_install(rt_uint16_t code)
  68. {
  69. rt_uint32_t idev_count;
  70. struct input_event_cap cap;
  71. struct rt_input_handler tmp_handler, *handlers;
  72. cap.code = code;
  73. cap.value = 0;
  74. tmp_handler.idev = RT_NULL;
  75. tmp_handler.identify = &power_test_cap;
  76. tmp_handler.callback = &power_input_callback;
  77. tmp_handler.priv = &cap;
  78. rt_input_add_handler(&tmp_handler); /* Just test */
  79. if (!(idev_count = cap.value))
  80. {
  81. return;
  82. }
  83. handlers = rt_calloc(idev_count, sizeof(*handlers));
  84. if (!handlers)
  85. {
  86. LOG_E("No memory to install power handler");
  87. return;
  88. }
  89. for (int i = 0; i < idev_count; ++i, ++handlers)
  90. {
  91. cap.value = i;
  92. cap.current = 0;
  93. handlers->identify = &power_input_identify;
  94. handlers->callback = &power_input_callback;
  95. handlers->priv = &cap;
  96. rt_input_add_handler(handlers);
  97. handlers->priv = RT_NULL;
  98. }
  99. }
  100. static int input_event_power_init(void)
  101. {
  102. power_handler_install(KEY_POWER);
  103. power_handler_install(KEY_RESTART);
  104. return 0;
  105. }
  106. INIT_ENV_EXPORT(input_event_power_init);