rnd-20131217-2-src
[rocksndiamonds.git] / src / events.c
index 122311d1a527345b924ce8bca12673dca7d216e8..0be6815b798acfd855e5644d1e745e3c721efcc9 100644 (file)
@@ -24,7 +24,7 @@
 #include "network.h"
 
 
-#define        DEBUG_EVENTS            0
+#define        DEBUG_EVENTS            1
 
 
 static boolean cursor_inside_playfield = FALSE;
@@ -39,7 +39,7 @@ static unsigned int playfield_cursor_delay = 0;
 /* event filter addition for SDL2: as SDL2 does not have a function to enable
    or disable keyboard auto-repeat, filter repeated keyboard events instead */
 
-int FilterEvents(const Event *event)
+static int FilterEventsExt(const Event *event)
 {
   MotionEvent *motion;
 
@@ -74,6 +74,18 @@ int FilterEvents(const Event *event)
   return 1;
 }
 
+#if defined(TARGET_SDL2)
+int FilterEvents(void *userdata, Event *event)
+{
+  return FilterEventsExt(event);
+}
+#else
+int FilterEvents(const Event *event)
+{
+  return FilterEventsExt(event);
+}
+#endif
+
 /* to prevent delay problems, skip mouse motion events if the very next
    event is also a mouse motion event (and therefore effectively only
    handling the last of a row of mouse motion events in the event queue) */
@@ -115,7 +127,7 @@ static boolean NextValidEvent(Event *event)
 
     NextEvent(event);
 
-    if (FilterMouseMotionEvents(event))
+    if (FilterEventsExt(event))
       handle_this_event = TRUE;
 
     if (SkipPressedMouseMotionEvent(event))
@@ -148,7 +160,15 @@ void EventLoop(void)
          case EVENT_MOTIONNOTIFY:
            HandleMotionEvent((MotionEvent *) &event);
            break;
-  
+
+#if defined(TARGET_SDL2)
+         case EVENT_FINGERPRESS:
+         case EVENT_FINGERRELEASE:
+         case EVENT_FINGERMOTION:
+           HandleFingerEvent((FingerEvent *) &event);
+           break;
+#endif
+
          case EVENT_KEYPRESS:
          case EVENT_KEYRELEASE:
            HandleKeyEvent((KeyEvent *) &event);
@@ -338,8 +358,10 @@ void HandleExposeEvent(ExposeEvent *event)
 void HandleButtonEvent(ButtonEvent *event)
 {
 #if DEBUG_EVENTS
-  printf("::: BUTTON EVENT: button %d %s\n", event->button,
-        event->type == EVENT_BUTTONPRESS ? "pressed" : "released");
+  Error(ERR_DEBUG, "BUTTON EVENT: button %d %s, x/y %d/%d\n",
+       event->button,
+       event->type == EVENT_BUTTONPRESS ? "pressed" : "released",
+       event->x, event->y);
 #endif
 
   motion_status = FALSE;
@@ -362,19 +384,123 @@ void HandleMotionEvent(MotionEvent *event)
 
   motion_status = TRUE;
 
+  Error(ERR_DEBUG, "MOTION EVENT: button %d moved, x/y %d/%d\n",
+       button_status, event->x, event->y);
+
   HandleButton(event->x, event->y, button_status, button_status);
 }
 
+#if defined(TARGET_SDL2)
+void HandleFingerEvent(FingerEvent *event)
+{
+#if 0
+  static int num_events = 0;
+  int max_events = 10;
+#endif
+
+#if DEBUG_EVENTS
+  Error(ERR_DEBUG, "FINGER EVENT: finger was %s, touch ID %lld, finger ID %lld, x/y %f/%f, dx/dy %f/%f, pressure %f",
+       event->type == EVENT_FINGERPRESS ? "pressed" :
+       event->type == EVENT_FINGERRELEASE ? "released" : "moved",
+       event->touchId,
+       event->fingerId,
+       event->x, event->y,
+       event->dx, event->dy,
+       event->pressure);
+#endif
+
+#if 0
+  int x = (int)(event->x * video.width);
+  int y = (int)(event->y * video.height);
+  int button = MB_LEFTBUTTON;
+
+  Error(ERR_DEBUG, "=> screen x/y %d/%d", x, y);
+#endif
+
+#if 0
+  if (++num_events >= max_events)
+    CloseAllAndExit(0);
+#endif
+
+#if 1
+#if 0
+  if (event->type == EVENT_FINGERPRESS ||
+      event->type == EVENT_FINGERMOTION)
+    button_status = button;
+  else
+    button_status = MB_RELEASED;
+
+  int max_x = SX + SXSIZE;
+  int max_y = SY + SYSIZE;
+#endif
+
+#if 1
+  if (game_status == GAME_MODE_PLAYING)
+#else
+  if (game_status == GAME_MODE_PLAYING &&
+      x < max_x)
+#endif
+  {
+    int key_status = (event->type == EVENT_FINGERRELEASE ? KEY_RELEASED :
+                     KEY_PRESSED);
+#if 1
+    Key key = (event->y < 1.0 / 3.0 ? setup.input[0].key.up :
+              event->y > 2.0 / 3.0 ? setup.input[0].key.down :
+              event->x < 1.0 / 3.0 ? setup.input[0].key.left :
+              event->x > 2.0 / 3.0 ? setup.input[0].key.right :
+              setup.input[0].key.drop);
+#else
+    Key key = (y <     max_y / 3 ? setup.input[0].key.up :
+              y > 2 * max_y / 3 ? setup.input[0].key.down :
+              x <     max_x / 3 ? setup.input[0].key.left :
+              x > 2 * max_x / 3 ? setup.input[0].key.right :
+              setup.input[0].key.drop);
+#endif
+
+    Error(ERR_DEBUG, "=> key == %d, key_status == %d", key, key_status);
+
+    HandleKey(key, key_status);
+  }
+  else
+  {
+#if 0
+    Error(ERR_DEBUG, "::: button_status == %d, button == %d\n",
+         button_status, button);
+
+    HandleButton(x, y, button_status, button);
+#endif
+  }
+#endif
+}
+#endif
+
 void HandleKeyEvent(KeyEvent *event)
 {
-  int key_status = (event->type==EVENT_KEYPRESS ? KEY_PRESSED : KEY_RELEASED);
+  int key_status = (event->type == EVENT_KEYPRESS ? KEY_PRESSED : KEY_RELEASED);
   boolean with_modifiers = (game_status == GAME_MODE_PLAYING ? FALSE : TRUE);
   Key key = GetEventKey(event, with_modifiers);
   Key keymod = (with_modifiers ? GetEventKey(event, FALSE) : key);
 
 #if DEBUG_EVENTS
-  printf("::: KEY EVENT: %d %s\n", GetEventKey(event, TRUE),
-        event->type == EVENT_KEYPRESS ? "pressed" : "released");
+  Error(ERR_DEBUG, "KEY EVENT: key was %s, keysym.scancode == %d, keysym.sym == %d, resulting key == %d (%s)",
+       event->type == EVENT_KEYPRESS ? "pressed" : "released",
+       event->keysym.scancode,
+       event->keysym.sym,
+       GetEventKey(event, TRUE),
+       getKeyNameFromKey(key));
+#endif
+
+#if 0
+  if (key == KSYM_Menu)
+    Error(ERR_DEBUG, "menu key pressed");
+  else if (key == KSYM_Back)
+    Error(ERR_DEBUG, "back key pressed");
+#endif
+
+#if defined(PLATFORM_ANDROID)
+  // always map the "back" button to the "escape" key on Android devices
+  if (key == KSYM_Back)
+    key = KSYM_Escape;
 #endif
 
   HandleKeyModState(keymod, key_status);
@@ -461,6 +587,8 @@ void HandleButton(int mx, int my, int button, int button_nr)
   if (IS_WHEEL_BUTTON(button_nr))
     return;
 
+  Error(ERR_DEBUG, "::: game_status == %d", game_status);
+
   switch (game_status)
   {
     case GAME_MODE_TITLE:
@@ -546,7 +674,7 @@ static void HandleKeysSpecial(Key key)
   cheat_input[cheat_input_len] = '\0';
 
 #if DEBUG_EVENTS
-  printf("::: '%s' [%d]\n", cheat_input, cheat_input_len);
+  Error(ERR_DEBUG, "SPECIAL KEY '%s' [%d]\n", cheat_input, cheat_input_len);
 #endif
 
   if (game_status == GAME_MODE_MAIN)