SDL_androidpen.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifdef SDL_VIDEO_DRIVER_ANDROID
  20. #include "SDL_androidpen.h"
  21. #include "../../events/SDL_pen_c.h"
  22. #include "../../core/android/SDL_android.h"
  23. #define ACTION_DOWN 0
  24. #define ACTION_UP 1
  25. #define ACTION_CANCEL 3
  26. #define ACTION_POINTER_DOWN 5
  27. #define ACTION_POINTER_UP 6
  28. #define ACTION_HOVER_EXIT 10
  29. void Android_OnPen(SDL_Window *window, int pen_id_in, SDL_PenDeviceType device_type, int button, int action, float x, float y, float p)
  30. {
  31. if (!window) {
  32. return;
  33. }
  34. // pointer index starts from zero.
  35. pen_id_in++;
  36. SDL_PenID pen = SDL_FindPenByHandle((void *) (size_t) pen_id_in);
  37. if (!pen) {
  38. // TODO: Query JNI for pen device info
  39. SDL_PenInfo peninfo;
  40. SDL_zero(peninfo);
  41. peninfo.capabilities = SDL_PEN_CAPABILITY_PRESSURE | SDL_PEN_CAPABILITY_ERASER;
  42. peninfo.num_buttons = 2;
  43. peninfo.subtype = SDL_PEN_TYPE_PEN;
  44. peninfo.device_type = device_type;
  45. pen = SDL_AddPenDevice(0, NULL, window, &peninfo, (void *) (size_t) pen_id_in);
  46. if (!pen) {
  47. SDL_Log("error: can't add a pen device %d", pen_id_in);
  48. return;
  49. }
  50. }
  51. SDL_SendPenMotion(0, pen, window, x, y);
  52. SDL_SendPenAxis(0, pen, window, SDL_PEN_AXIS_PRESSURE, p);
  53. // TODO: add more axis
  54. SDL_PenInputFlags current = SDL_GetPenStatus(pen, NULL, 0);
  55. int diff = current ^ button;
  56. if (diff != 0) {
  57. // Android only exposes BUTTON_STYLUS_PRIMARY and BUTTON_STYLUS_SECONDARY
  58. if (diff & SDL_PEN_INPUT_BUTTON_1)
  59. SDL_SendPenButton(0, pen, window, 1, (button & SDL_PEN_INPUT_BUTTON_1) != 0);
  60. if (diff & SDL_PEN_INPUT_BUTTON_2)
  61. SDL_SendPenButton(0, pen, window, 2, (button & SDL_PEN_INPUT_BUTTON_2) != 0);
  62. }
  63. // button contains DOWN/ERASER_TIP on DOWN/UP regardless of pressed state, use action to distinguish
  64. // we don't compare tip flags above because MotionEvent.getButtonState doesn't return stylus tip/eraser state.
  65. switch (action) {
  66. case ACTION_CANCEL:
  67. case ACTION_HOVER_EXIT:
  68. SDL_RemovePenDevice(0, window, pen);
  69. break;
  70. case ACTION_DOWN:
  71. case ACTION_POINTER_DOWN:
  72. SDL_SendPenTouch(0, pen, window, (button & SDL_PEN_INPUT_ERASER_TIP) != 0, true);
  73. break;
  74. case ACTION_UP:
  75. case ACTION_POINTER_UP:
  76. SDL_SendPenTouch(0, pen, window, (button & SDL_PEN_INPUT_ERASER_TIP) != 0, false);
  77. break;
  78. default:
  79. break;
  80. }
  81. }
  82. #endif // SDL_VIDEO_DRIVER_ANDROID