lv_sdl_drv_input.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. if (e.type == SDL_CONTROLLERBUTTONDOWN || e.type == SDL_KEYDOWN)
  53. data->state = LV_INDEV_STATE_PR;
  54. if (e.type == SDL_CONTROLLERBUTTONUP || e.type == SDL_KEYUP)
  55. data->state = LV_INDEV_STATE_REL;
  56. if (e.type == SDL_CONTROLLERDEVICEADDED)
  57. SDL_GameControllerOpen(e.cdevice.which);
  58. if (e.type == SDL_CONTROLLERDEVICEREMOVED)
  59. SDL_GameControllerClose(SDL_GameControllerFromInstanceID(e.cdevice.which));
  60. //Gamecontroller event
  61. if (e.type == SDL_CONTROLLERBUTTONDOWN || e.type == SDL_CONTROLLERBUTTONUP)
  62. {
  63. switch (e.cbutton.button)
  64. {
  65. case SDL_CONTROLLER_BUTTON_A: data->key = LV_KEY_ENTER; break;
  66. case SDL_CONTROLLER_BUTTON_B: data->key = LV_KEY_ESC; break;
  67. case SDL_CONTROLLER_BUTTON_X: data->key = LV_KEY_BACKSPACE; break;
  68. case SDL_CONTROLLER_BUTTON_Y: data->key = LV_KEY_HOME; break;
  69. case SDL_CONTROLLER_BUTTON_BACK: data->key = LV_KEY_PREV; break;
  70. case SDL_CONTROLLER_BUTTON_START: data->key = LV_KEY_NEXT; break;
  71. case SDL_CONTROLLER_BUTTON_LEFTSTICK: data->key = LV_KEY_PREV; break;
  72. case SDL_CONTROLLER_BUTTON_RIGHTSTICK: data->key = LV_KEY_NEXT; break;
  73. case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: data->key = LV_KEY_PREV; break;
  74. case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: data->key = LV_KEY_NEXT; break;
  75. case SDL_CONTROLLER_BUTTON_DPAD_UP: data->key = LV_KEY_UP; break;
  76. case SDL_CONTROLLER_BUTTON_DPAD_DOWN: data->key = LV_KEY_DOWN; break;
  77. case SDL_CONTROLLER_BUTTON_DPAD_LEFT: data->key = LV_KEY_LEFT; break;
  78. case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: data->key = LV_KEY_RIGHT; break;
  79. }
  80. }
  81. //Keyboard event
  82. if (e.type == SDL_KEYDOWN || e.type == SDL_KEYUP)
  83. {
  84. switch (e.key.keysym.sym)
  85. {
  86. case SDLK_ESCAPE: data->key = LV_KEY_ESC; break;
  87. case SDLK_BACKSPACE: data->key = LV_KEY_BACKSPACE; break;
  88. case SDLK_HOME: data->key = LV_KEY_HOME; break;
  89. case SDLK_RETURN: data->key = LV_KEY_ENTER; break;
  90. case SDLK_PAGEDOWN: data->key = LV_KEY_PREV; break;
  91. case SDLK_TAB: data->key = LV_KEY_NEXT; break;
  92. case SDLK_PAGEUP: data->key = LV_KEY_NEXT; break;
  93. case SDLK_UP: data->key = LV_KEY_UP; break;
  94. case SDLK_DOWN: data->key = LV_KEY_DOWN; break;
  95. case SDLK_LEFT: data->key = LV_KEY_LEFT; break;
  96. case SDLK_RIGHT: data->key = LV_KEY_RIGHT; break;
  97. }
  98. }
  99. //Quit event
  100. if((e.type == SDL_WINDOWEVENT && e.window.event == SDL_WINDOWEVENT_CLOSE) ||
  101. e.type == SDL_QUIT) {
  102. quit_event = true;
  103. exit(0);
  104. }
  105. }
  106. if (SDL_PollEvent(NULL))
  107. return true; //There's more events to handle
  108. else
  109. return false;
  110. }
  111. quit_event_t get_quit_event(void)
  112. {
  113. return quit_event;
  114. }
  115. void set_quit_event(quit_event_t quit)
  116. {
  117. quit_event = quit;
  118. }
  119. lv_indev_t *lv_sdl_init_input(void)
  120. {
  121. if (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) != 0)
  122. printf("SDL_InitSubSystem failed: %s\n", SDL_GetError());
  123. SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
  124. for (int i = 0; i < SDL_NumJoysticks(); i++)
  125. {
  126. SDL_GameControllerOpen(i);
  127. }
  128. lv_indev_drv_t indev_drv;
  129. lv_indev_drv_init(&indev_drv);
  130. indev_drv.type = LV_INDEV_TYPE_KEYPAD;
  131. indev_drv.read_cb = sdl_input_read;
  132. lv_indev_drv_register(&indev_drv);
  133. lv_indev_drv_init(&indev_drv);
  134. indev_drv.type = LV_INDEV_TYPE_POINTER;
  135. indev_drv.read_cb = sdl_input_read;
  136. lv_indev_drv_register(&indev_drv);
  137. return NULL;
  138. }
  139. void lv_sdl_deinit_input(void)
  140. {
  141. SDL_GameControllerClose(0);
  142. SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
  143. }
  144. #endif