lv_sdl_drv_input.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* MIT License
  2. *
  3. * Copyright (c) [2020] [Ryan Wendland]
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. #include "luat_base.h"
  24. #ifdef LUAT_USE_LVGL_SDL2
  25. #include <stdio.h>
  26. #include <SDL2/SDL.h>
  27. #include "lvgl.h"
  28. #include "lv_conf.h"
  29. #include "lv_sdl_drv_input.h"
  30. static quit_event_t quit_event = false;
  31. static bool sdl_input_read(lv_indev_drv_t *drv, lv_indev_data_t *data)
  32. {
  33. (void)drv;
  34. data->key = 0;
  35. static SDL_Event e;
  36. if (SDL_PollEvent(&e))
  37. {
  38. if (e.type == SDL_MOUSEBUTTONDOWN) {
  39. if (e.button.button == SDL_BUTTON_LEFT) {
  40. data->state = LV_INDEV_STATE_PR;
  41. data->point.x = e.button.x;
  42. data->point.y = e.button.y;
  43. }
  44. }
  45. else if (e.type == SDL_MOUSEBUTTONUP) {
  46. if (e.button.button == SDL_BUTTON_LEFT) {
  47. data->state = LV_INDEV_STATE_REL;
  48. data->point.x = e.button.x;
  49. data->point.y = e.button.y;
  50. }
  51. }
  52. else if (e.type == SDL_MOUSEMOTION) {
  53. // 处理鼠标移动事件,支持拖动检测
  54. if (e.motion.state & SDL_BUTTON_LMASK) {
  55. // 左键按下时的移动,设置为按下状态
  56. data->state = LV_INDEV_STATE_PR;
  57. } else {
  58. // 左键未按下时的移动,设置为释放状态
  59. data->state = LV_INDEV_STATE_REL;
  60. }
  61. data->point.x = e.motion.x;
  62. data->point.y = e.motion.y;
  63. }
  64. if (e.type == SDL_CONTROLLERBUTTONDOWN || e.type == SDL_KEYDOWN)
  65. data->state = LV_INDEV_STATE_PR;
  66. if (e.type == SDL_CONTROLLERBUTTONUP || e.type == SDL_KEYUP)
  67. data->state = LV_INDEV_STATE_REL;
  68. if (e.type == SDL_CONTROLLERDEVICEADDED)
  69. SDL_GameControllerOpen(e.cdevice.which);
  70. if (e.type == SDL_CONTROLLERDEVICEREMOVED)
  71. SDL_GameControllerClose(SDL_GameControllerFromInstanceID(e.cdevice.which));
  72. //Gamecontroller event
  73. if (e.type == SDL_CONTROLLERBUTTONDOWN || e.type == SDL_CONTROLLERBUTTONUP)
  74. {
  75. switch (e.cbutton.button)
  76. {
  77. case SDL_CONTROLLER_BUTTON_A: data->key = LV_KEY_ENTER; break;
  78. case SDL_CONTROLLER_BUTTON_B: data->key = LV_KEY_ESC; break;
  79. case SDL_CONTROLLER_BUTTON_X: data->key = LV_KEY_BACKSPACE; break;
  80. case SDL_CONTROLLER_BUTTON_Y: data->key = LV_KEY_HOME; break;
  81. case SDL_CONTROLLER_BUTTON_BACK: data->key = LV_KEY_PREV; break;
  82. case SDL_CONTROLLER_BUTTON_START: data->key = LV_KEY_NEXT; break;
  83. case SDL_CONTROLLER_BUTTON_LEFTSTICK: data->key = LV_KEY_PREV; break;
  84. case SDL_CONTROLLER_BUTTON_RIGHTSTICK: data->key = LV_KEY_NEXT; break;
  85. case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: data->key = LV_KEY_PREV; break;
  86. case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: data->key = LV_KEY_NEXT; break;
  87. case SDL_CONTROLLER_BUTTON_DPAD_UP: data->key = LV_KEY_UP; break;
  88. case SDL_CONTROLLER_BUTTON_DPAD_DOWN: data->key = LV_KEY_DOWN; break;
  89. case SDL_CONTROLLER_BUTTON_DPAD_LEFT: data->key = LV_KEY_LEFT; break;
  90. case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: data->key = LV_KEY_RIGHT; break;
  91. }
  92. }
  93. //Keyboard event
  94. if (e.type == SDL_KEYDOWN || e.type == SDL_KEYUP)
  95. {
  96. switch (e.key.keysym.sym)
  97. {
  98. case SDLK_ESCAPE: data->key = LV_KEY_ESC; break;
  99. case SDLK_BACKSPACE: data->key = LV_KEY_BACKSPACE; break;
  100. case SDLK_HOME: data->key = LV_KEY_HOME; break;
  101. case SDLK_RETURN: data->key = LV_KEY_ENTER; break;
  102. case SDLK_PAGEDOWN: data->key = LV_KEY_PREV; break;
  103. case SDLK_TAB: data->key = LV_KEY_NEXT; break;
  104. case SDLK_PAGEUP: data->key = LV_KEY_NEXT; break;
  105. case SDLK_UP: data->key = LV_KEY_UP; break;
  106. case SDLK_DOWN: data->key = LV_KEY_DOWN; break;
  107. case SDLK_LEFT: data->key = LV_KEY_LEFT; break;
  108. case SDLK_RIGHT: data->key = LV_KEY_RIGHT; break;
  109. }
  110. }
  111. //Quit event
  112. if((e.type == SDL_WINDOWEVENT && e.window.event == SDL_WINDOWEVENT_CLOSE) ||
  113. e.type == SDL_QUIT) {
  114. quit_event = true;
  115. exit(0);
  116. }
  117. }
  118. if (SDL_PollEvent(NULL))
  119. return true; //There's more events to handle
  120. else
  121. return false;
  122. }
  123. quit_event_t get_quit_event(void)
  124. {
  125. return quit_event;
  126. }
  127. void set_quit_event(quit_event_t quit)
  128. {
  129. quit_event = quit;
  130. }
  131. lv_indev_t *lv_sdl_init_input(void)
  132. {
  133. if (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) != 0)
  134. printf("SDL_InitSubSystem failed: %s\n", SDL_GetError());
  135. SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
  136. for (int i = 0; i < SDL_NumJoysticks(); i++)
  137. {
  138. SDL_GameControllerOpen(i);
  139. }
  140. lv_indev_drv_t indev_drv;
  141. lv_indev_drv_init(&indev_drv);
  142. indev_drv.type = LV_INDEV_TYPE_KEYPAD;
  143. indev_drv.read_cb = sdl_input_read;
  144. lv_indev_drv_register(&indev_drv);
  145. lv_indev_drv_init(&indev_drv);
  146. indev_drv.type = LV_INDEV_TYPE_POINTER;
  147. indev_drv.read_cb = sdl_input_read;
  148. lv_indev_drv_register(&indev_drv);
  149. return NULL;
  150. }
  151. void lv_sdl_deinit_input(void)
  152. {
  153. SDL_GameControllerClose(0);
  154. SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
  155. }
  156. #endif