SDL_androidpen.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 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_ENTER 9
  29. #define ACTION_HOVER_EXIT 10
  30. void Android_OnPen(SDL_Window *window, int pen_id_in, SDL_PenDeviceType device_type, int button, int action, float x, float y, float p)
  31. {
  32. if (!window) {
  33. return;
  34. }
  35. // pointer index starts from zero.
  36. pen_id_in++;
  37. SDL_PenID pen = SDL_FindPenByHandle((void *) (size_t) pen_id_in);
  38. if (!pen) {
  39. // TODO: Query JNI for pen device info
  40. SDL_PenInfo peninfo;
  41. SDL_zero(peninfo);
  42. peninfo.capabilities = SDL_PEN_CAPABILITY_PRESSURE | SDL_PEN_CAPABILITY_ERASER;
  43. peninfo.num_buttons = 2;
  44. peninfo.subtype = SDL_PEN_TYPE_PEN;
  45. peninfo.device_type = device_type;
  46. pen = SDL_AddPenDevice(0, NULL, window, &peninfo, (void *) (size_t) pen_id_in, true);
  47. if (!pen) {
  48. SDL_Log("error: can't add a pen device %d", pen_id_in);
  49. return;
  50. }
  51. }
  52. SDL_SendPenMotion(0, pen, window, x, y);
  53. SDL_SendPenAxis(0, pen, window, SDL_PEN_AXIS_PRESSURE, p);
  54. // TODO: add more axis
  55. SDL_PenInputFlags current = SDL_GetPenStatus(pen, NULL, 0);
  56. int diff = current ^ button;
  57. if (diff != 0) {
  58. for (Uint8 i = 1; i <= 5; ++i) {
  59. Uint8 mask = (1 << i);
  60. if (diff & mask) {
  61. SDL_SendPenButton(0, pen, window, i, (button & mask) != 0);
  62. }
  63. }
  64. }
  65. // button contains DOWN/ERASER_TIP on DOWN/UP regardless of pressed state, use action to distinguish
  66. // we don't compare tip flags above because MotionEvent.getButtonState doesn't return stylus tip/eraser state.
  67. switch (action) {
  68. case ACTION_HOVER_ENTER:
  69. SDL_SendPenProximity(0, pen, window, true, true);
  70. break;
  71. case ACTION_CANCEL:
  72. case ACTION_HOVER_EXIT: // strictly speaking, this can mean both "proximity out" and "left the View" but close enough.
  73. SDL_SendPenProximity(0, pen, window, false, false);
  74. break;
  75. case ACTION_DOWN:
  76. case ACTION_POINTER_DOWN:
  77. SDL_SendPenTouch(0, pen, window, (button & SDL_PEN_INPUT_ERASER_TIP) != 0, true);
  78. break;
  79. case ACTION_UP:
  80. case ACTION_POINTER_UP:
  81. SDL_SendPenTouch(0, pen, window, (button & SDL_PEN_INPUT_ERASER_TIP) != 0, false);
  82. break;
  83. default:
  84. break;
  85. }
  86. }
  87. #endif // SDL_VIDEO_DRIVER_ANDROID