<commit_before>#include <hx/CFFI.h>
#include <Display.h>
#include <Surface.h>
#include <KeyCodes.h>
#include <Utils.h>
#include <jni.h>
#include <ByteArray.h>

#include <android/log.h>

JavaVM *gJVM=0;
JNIEnv *GetEnv()
{
   JNIEnv *env = 0;
   gJVM->AttachCurrentThread(&env, NULL);
   return env;
}


namespace nme
{

static class AndroidStage *sStage = 0;
static class AndroidFrame *sFrame = 0;
static FrameCreationCallback sOnFrame = 0;
static bool sCloseActivity = false;

static int sgNMEResult = 0;

enum { NO_TOUCH = -1 };

int GetResult()
{
   if (sCloseActivity)
   {
      sCloseActivity = false;
      return -1;
   }
   int r = sgNMEResult;
   sgNMEResult = 0;
   return r;
}

class AndroidStage : public Stage
{
public:
   AndroidStage(int inWidth,int inHeight,int inFlags) : Stage(true)
   {
      mHardwareContext = HardwareContext::CreateOpenGL(0,0);
      mHardwareContext->IncRef();
      mHardwareContext->SetWindowSize(inWidth,inHeight);
      mHardwareSurface = new HardwareSurface(mHardwareContext);
      mHardwareSurface->IncRef();
      mMultiTouch = true;
      mSingleTouchID = NO_TOUCH;
      mDX = 0;
      mDY = 0;

      // Click detection
      mDownX = 0;
      mDownY = 0;
   }
   ~AndroidStage()
   {
      mHardwareSurface->DecRef();
      mHardwareContext->DecRef();
   }

   void Flip() { }
   void GetMouse()
   {
   }
   Surface *GetPrimarySurface() { return mHardwareSurface; }
   bool isOpenGL() const { return true; }
   virtual void SetCursor(Cursor inCursor) { }

	void OnPoll()
   {
      Event evt(etPoll);
      HandleEvent(evt);
   }

   void onActivityEvent(int inVal)
   {
      __android_log_print(ANDROID_LOG_INFO, "NME", "Activity action %d", inVal);
      if (inVal==1 || inVal==2)
      {
         Event evt( inVal==1 ? etActivate : etDeactivate );
         HandleEvent(evt);
      }
   }
 

   void OnRender()
   {
      Event evt(etRedraw);
      HandleEvent(evt);
   }
   void Resize(int inWidth,int inHeight)
   {
      ResetHardwareContext();
      mHardwareContext->SetWindowSize(inWidth,inHeight);
      Event evt(etResize, inWidth, inHeight);
      HandleEvent(evt);
   }

   void OnKey(int inCode, bool inDown)
   {
      __android_log_print(ANDROID_LOG_ERROR, "NME", "OnKey %d %d", inCode, inDown);
      Event key( inDown ? etKeyDown : etKeyUp );
      key.code = inCode;
      key.value = inCode;
      HandleEvent(key);
   }

   void OnTrackball(double inX, double inY)
   {
      // __android_log_print(ANDROID_LOG_INFO, "NME", "Trackball %f %f", inX, inY);
   }

   void OnTouch(int inType,double inX, double inY, int inID)
   {
         if (mSingleTouchID==NO_TOUCH || inID==mSingleTouchID || mMultiTouch)
         {
            EventType type = (EventType)inType;
            if (!mMultiTouch)
            {
               switch(inType)
               {
                  case  etTouchBegin: type = etMouseDown; break;
                  case  etTouchEnd:   type = etMouseUp; break;
                  case  etTouchMove : type = etMouseMove; break;
                  case  etTouchTap:   return; break;
               }
            }

               Event mouse(type, inX, inY);
               if (mSingleTouchID==NO_TOUCH || inID==mSingleTouchID || !mMultiTouch)
                  mouse.flags |= efPrimaryTouch;

               if (inType==etTouchBegin)
               {
                  if (mSingleTouchID==NO_TOUCH)
                     mSingleTouchID = inID;
                  mouse.flags |= efLeftDown;
                  mDownX = inX;
                  mDownY = inY;
               }
               else if (inType==etTouchEnd)
               {
                  if (mSingleTouchID==inID)
                     mSingleTouchID = NO_TOUCH;
               }
               else if (inType==etTouchMove)
               {
                  mouse.flags |= efLeftDown;
               }
               mouse.value = inID;

               //if (inType==etTouchBegin)
                  //ELOG("DOWN %d %f,%f (%s)", inID, inX, inY, (mouse.flags & efPrimaryTouch) ? "P":"S" );

               //if (inType==etTouchEnd)
                  //ELOG("UP %d %f,%f (%s)", inID, inX, inY, (mouse.flags & efPrimaryTouch) ? "P":"S" );

               HandleEvent(mouse);
         }
   }

   void OnDeviceOrientationUpdate(int orientation)
   {
      currentDeviceOrientation = orientation;
      //__android_log_print(ANDROID_LOG_INFO, "NME", "Device Orientation %d", currentDeviceOrientation);
   }

   void OnNormalOrientationFound(int orientation)
   {
      normalOrientation = orientation;
      //__android_log_print(ANDROID_LOG_INFO, "NME", "Normal Orientation %d", normalOrientation);
   }

   void OnOrientationUpdate(double inX, double inY, double inZ)
   {
      mOrientationX = inX;
      mOrientationY = inY;
      mOrientationZ = inZ;
      //__android_log_print(ANDROID_LOG_INFO, "NME", "Orientation %f %f %f", inX, inY, inZ);
   }
   
   void OnAccelerate(double inX, double inY, double inZ)
   {
      mAccX = inX / 9.80665;
      mAccY = inY / 9.80665;
      mAccZ = inZ / 9.80665;
      //__android_log_print(ANDROID_LOG_INFO, "NME", "Accelerometer %f %f %f", inX, inY, inZ);
   }

   void EnablePopupKeyboard(bool inEnable)
   {
      JNIEnv *env = GetEnv();
      jclass cls = env->FindClass("org/haxe/nme/GameActivity");
      jmethodID mid = env->GetStaticMethodID(cls, "showKeyboard", "(Z)V");
      if (mid == 0)
        return;<commit_msg>account for normal orientation in accelerometer signal to match iOS devices. in this way, apps can use yaw/pitch/roll in common, tied absolutely to the orientation of the app for accurate simulated gravity. Tested on 2.3 - 4.x<commit_after>#include <hx/CFFI.h>
#include <Display.h>
#include <Surface.h>
#include <KeyCodes.h>
#include <Utils.h>
#include <jni.h>
#include <ByteArray.h>

#include <android/log.h>

JavaVM *gJVM=0;
JNIEnv *GetEnv()
{
   JNIEnv *env = 0;
   gJVM->AttachCurrentThread(&env, NULL);
   return env;
}


namespace nme
{

static class AndroidStage *sStage = 0;
static class AndroidFrame *sFrame = 0;
static FrameCreationCallback sOnFrame = 0;
static bool sCloseActivity = false;

static int sgNMEResult = 0;

enum { NO_TOUCH = -1 };

int GetResult()
{
   if (sCloseActivity)
   {
      sCloseActivity = false;
      return -1;
   }
   int r = sgNMEResult;
   sgNMEResult = 0;
   return r;
}

class AndroidStage : public Stage
{
public:
   AndroidStage(int inWidth,int inHeight,int inFlags) : Stage(true)
   {
      mHardwareContext = HardwareContext::CreateOpenGL(0,0);
      mHardwareContext->IncRef();
      mHardwareContext->SetWindowSize(inWidth,inHeight);
      mHardwareSurface = new HardwareSurface(mHardwareContext);
      mHardwareSurface->IncRef();
      mMultiTouch = true;
      mSingleTouchID = NO_TOUCH;
      mDX = 0;
      mDY = 0;

      // Click detection
      mDownX = 0;
      mDownY = 0;

      normalOrientation = 0;
   }
   ~AndroidStage()
   {
      mHardwareSurface->DecRef();
      mHardwareContext->DecRef();
   }

   void Flip() { }
   void GetMouse()
   {
   }
   Surface *GetPrimarySurface() { return mHardwareSurface; }
   bool isOpenGL() const { return true; }
   virtual void SetCursor(Cursor inCursor) { }

	void OnPoll()
   {
      Event evt(etPoll);
      HandleEvent(evt);
   }

   void onActivityEvent(int inVal)
   {
      __android_log_print(ANDROID_LOG_INFO, "NME", "Activity action %d", inVal);
      if (inVal==1 || inVal==2)
      {
         Event evt( inVal==1 ? etActivate : etDeactivate );
         HandleEvent(evt);
      }
   }
 

   void OnRender()
   {
      Event evt(etRedraw);
      HandleEvent(evt);
   }
   void Resize(int inWidth,int inHeight)
   {
      ResetHardwareContext();
      mHardwareContext->SetWindowSize(inWidth,inHeight);
      Event evt(etResize, inWidth, inHeight);
      HandleEvent(evt);
   }

   void OnKey(int inCode, bool inDown)
   {
      __android_log_print(ANDROID_LOG_ERROR, "NME", "OnKey %d %d", inCode, inDown);
      Event key( inDown ? etKeyDown : etKeyUp );
      key.code = inCode;
      key.value = inCode;
      HandleEvent(key);
   }

   void OnTrackball(double inX, double inY)
   {
      // __android_log_print(ANDROID_LOG_INFO, "NME", "Trackball %f %f", inX, inY);
   }

   void OnTouch(int inType,double inX, double inY, int inID)
   {
         if (mSingleTouchID==NO_TOUCH || inID==mSingleTouchID || mMultiTouch)
         {
            EventType type = (EventType)inType;
            if (!mMultiTouch)
            {
               switch(inType)
               {
                  case  etTouchBegin: type = etMouseDown; break;
                  case  etTouchEnd:   type = etMouseUp; break;
                  case  etTouchMove : type = etMouseMove; break;
                  case  etTouchTap:   return; break;
               }
            }

               Event mouse(type, inX, inY);
               if (mSingleTouchID==NO_TOUCH || inID==mSingleTouchID || !mMultiTouch)
                  mouse.flags |= efPrimaryTouch;

               if (inType==etTouchBegin)
               {
                  if (mSingleTouchID==NO_TOUCH)
                     mSingleTouchID = inID;
                  mouse.flags |= efLeftDown;
                  mDownX = inX;
                  mDownY = inY;
               }
               else if (inType==etTouchEnd)
               {
                  if (mSingleTouchID==inID)
                     mSingleTouchID = NO_TOUCH;
               }
               else if (inType==etTouchMove)
               {
                  mouse.flags |= efLeftDown;
               }
               mouse.value = inID;

               //if (inType==etTouchBegin)
                  //ELOG("DOWN %d %f,%f (%s)", inID, inX, inY, (mouse.flags & efPrimaryTouch) ? "P":"S" );

               //if (inType==etTouchEnd)
                  //ELOG("UP %d %f,%f (%s)", inID, inX, inY, (mouse.flags & efPrimaryTouch) ? "P":"S" );

               HandleEvent(mouse);
         }
   }

   void OnDeviceOrientationUpdate(int orientation)
   {
      currentDeviceOrientation = orientation;
      //__android_log_print(ANDROID_LOG_INFO, "NME", "Device Orientation %d", currentDeviceOrientation);
   }

   void OnNormalOrientationFound(int orientation)
   {
      normalOrientation = orientation;
      //__android_log_print(ANDROID_LOG_INFO, "NME", "Normal Orientation %d", normalOrientation);
   }

   void OnOrientationUpdate(double inX, double inY, double inZ)
   {
      mOrientationX = inX;
      mOrientationY = inY;
      mOrientationZ = inZ;
      //__android_log_print(ANDROID_LOG_INFO, "NME", "Orientation %f %f %f", inX, inY, inZ);
   }
   
   void OnAccelerate(double inX, double inY, double inZ)
   {
      if (normalOrientation == 0 || normalOrientation == 1) { // UNKNOWN || PORTRAIT
         mAccX = -inX / 9.80665;
         mAccY = -inY / 9.80665;
         mAccZ = -inZ / 9.80665;
      } else { // 4 || LANDSCAPE_LEFT
         mAccX = inY / 9.80665;
         mAccY = -inX / 9.80665;
         mAccZ = -inZ / 9.80665;
      }
      //__android_log_print(ANDROID_LOG_INFO, "NME", "Accelerometer %f %f %f", inX, inY, inZ);
   }

   void EnablePopupKeyboard(bool inEnable)
   {
      JNIEnv *env = GetEnv();
      jclass cls = env->FindClass("org/haxe/nme/GameActivity");
      jmethodID mid = env->GetStaticMethodID(cls, "showKeyboard", "(Z)V");
      if (mid == 0)
        return;
{'fraction_non_alphanumeric': 0.11625441696113074, 'fraction_numerical': 0.008745583038869258, 'mean_word_length': 2.1421038023868997, 'pattern_counts': {'":': 4, '<': 19, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4101006', 'n_tokens_mistral': 3916, 'n_tokens_neox': 3748, 'n_words': 813}