src/video/android/SDL_androidtouch.c
changeset 9441 fc7522030729
parent 9439 0d01b53cdbea
child 9449 a65088766e35
equal deleted inserted replaced
9440:a72c15020efa 9441:fc7522030729
    48     SDL_GetWindowSize(Android_Window, &window_w, &window_h);
    48     SDL_GetWindowSize(Android_Window, &window_w, &window_h);
    49     *window_x = (int)(x * window_w);
    49     *window_x = (int)(x * window_w);
    50     *window_y = (int)(y * window_h);
    50     *window_y = (int)(y * window_h);
    51 }
    51 }
    52 
    52 
       
    53 static volatile SDL_bool separate_mouse_and_touch = SDL_FALSE;
       
    54 
       
    55 static void
       
    56 SeparateEventsHintWatcher(void *userdata, const char *name,
       
    57                           const char *oldValue, const char *newValue)
       
    58 {
       
    59     separate_mouse_and_touch = (newValue && (SDL_strcmp(newValue, "1") == 0));
       
    60 }
       
    61 
    53 void Android_InitTouch(void)
    62 void Android_InitTouch(void)
    54 {
    63 {
    55     int i;
    64     int i;
    56     int* ids;
    65     int* ids;
    57     int number = Android_JNI_GetTouchDeviceIds(&ids);
    66     const int number = Android_JNI_GetTouchDeviceIds(&ids);
       
    67 
       
    68     SDL_AddHintCallback(SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH,
       
    69                         SeparateEventsHintWatcher, NULL);
       
    70 
    58     if (0 < number) {
    71     if (0 < number) {
    59         for (i = 0; i < number; ++i) {
    72         for (i = 0; i < number; ++i) {
    60             SDL_AddTouch((SDL_TouchID) ids[i], ""); /* no error handling */
    73             SDL_AddTouch((SDL_TouchID) ids[i], ""); /* no error handling */
    61         }
    74         }
    62         SDL_free(ids);
    75         SDL_free(ids);
    63     }
    76     }
    64 }
    77 }
    65 
    78 
       
    79 void Android_QuitTouch(void)
       
    80 {
       
    81     SDL_DelHintCallback(SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH,
       
    82                         SeparateEventsHintWatcher, NULL);
       
    83     separate_mouse_and_touch = SDL_FALSE;
       
    84 }
       
    85 
    66 void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p)
    86 void Android_OnTouch(int touch_device_id_in, int pointer_finger_id_in, int action, float x, float y, float p)
    67 {
    87 {
    68     SDL_TouchID touchDeviceId = 0;
    88     SDL_TouchID touchDeviceId = 0;
    69     SDL_FingerID fingerId = 0;
    89     SDL_FingerID fingerId = 0;
    70     int window_x, window_y;
    90     int window_x, window_y;
    71     const char * hint;
       
    72     static SDL_FingerID pointerFingerID = 0;
    91     static SDL_FingerID pointerFingerID = 0;
    73 
    92 
    74     if (!Android_Window) {
    93     if (!Android_Window) {
    75         return;
    94         return;
    76     }
    95     }
    79     if (SDL_AddTouch(touchDeviceId, "") < 0) {
    98     if (SDL_AddTouch(touchDeviceId, "") < 0) {
    80         SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
    99         SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
    81     }
   100     }
    82 
   101 
    83     fingerId = (SDL_FingerID)pointer_finger_id_in;
   102     fingerId = (SDL_FingerID)pointer_finger_id_in;
    84     hint = SDL_GetHint("SDL_ANDROID_SEPARATE_MOUSE_AND_TOUCH");
       
    85     switch (action) {
   103     switch (action) {
    86         case ACTION_DOWN:
   104         case ACTION_DOWN:
    87             /* Primary pointer down */
   105             /* Primary pointer down */
    88             Android_GetWindowCoordinates(x, y, &window_x, &window_y);
   106             Android_GetWindowCoordinates(x, y, &window_x, &window_y);
    89             if ((!hint) || (hint && SDL_strcmp(hint, "1") != 0)) {
   107             if (!separate_mouse_and_touch) {
    90                 /* send moved event */
   108                 /* send moved event */
    91                 SDL_SendMouseMotion(Android_Window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
   109                 SDL_SendMouseMotion(Android_Window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
    92                 /* send mouse down event */
   110                 /* send mouse down event */
    93                 SDL_SendMouseButton(Android_Window, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
   111                 SDL_SendMouseButton(Android_Window, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
    94             }
   112             }
    99             break;
   117             break;
   100 
   118 
   101         case ACTION_MOVE:
   119         case ACTION_MOVE:
   102             if (!pointerFingerID) {
   120             if (!pointerFingerID) {
   103                 Android_GetWindowCoordinates(x, y, &window_x, &window_y);
   121                 Android_GetWindowCoordinates(x, y, &window_x, &window_y);
   104 
   122                 if (!separate_mouse_and_touch) {
   105                 if ((!hint) || (hint && SDL_strcmp(hint, "1") != 0)) {
       
   106                     /* send moved event */
   123                     /* send moved event */
   107                     SDL_SendMouseMotion(Android_Window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
   124                     SDL_SendMouseMotion(Android_Window, SDL_TOUCH_MOUSEID, 0, window_x, window_y);
   108                 }
   125                 }
   109             }
   126             }
   110             SDL_SendTouchMotion(touchDeviceId, fingerId, x, y, p);
   127             SDL_SendTouchMotion(touchDeviceId, fingerId, x, y, p);
   111             break;
   128             break;
   112 
   129 
   113         case ACTION_UP:
   130         case ACTION_UP:
   114             /* Primary pointer up */
   131             /* Primary pointer up */
   115             if ((!hint) || (hint && SDL_strcmp(hint, "1") != 0)) {
   132             if (!separate_mouse_and_touch) {
   116                 /* send mouse up */
   133                 /* send mouse up */
   117                 pointerFingerID = (SDL_FingerID) 0;
   134                 pointerFingerID = (SDL_FingerID) 0;
   118                 SDL_SendMouseButton(Android_Window, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
   135                 SDL_SendMouseButton(Android_Window, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
   119             }
   136             }
   120         case ACTION_POINTER_UP:
   137         case ACTION_POINTER_UP: