<commit_before>/*
 * Copyright 2011-2021 <NAME>. All rights reserved.
 * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
 */

#include "entry_p.h"

#if ENTRY_CONFIG_USE_NATIVE && BX_PLATFORM_ANDROID

#include <bgfx/platform.h>

#include <bx/thread.h>
#include <bx/file.h>

#include <android/input.h>
#include <android/log.h>
#include <android/looper.h>
#include <android/window.h>
#include <android_native_app_glue.h>
#include <android/native_window.h>

extern "C"
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include <android_native_app_glue.c>
#pragma GCC diagnostic pop
} // extern "C"

namespace entry
{
	///
	inline void androidSetWindow(::ANativeWindow* _window)
	{
		bgfx::PlatformData pd;
		pd.ndt          = NULL;
		pd.nwh          = _window;
		pd.context      = NULL;
		pd.backBuffer   = NULL;
		pd.backBufferDS = NULL;
		bgfx::setPlatformData(pd);
	}

	struct GamepadRemap
	{
		uint16_t  m_keyCode;
		Key::Enum m_key;
	};

	static GamepadRemap s_gamepadRemap[] =
	{
		{ AKEYCODE_DPAD_UP,       Key::GamepadUp        },
		{ AKEYCODE_DPAD_DOWN,     Key::GamepadDown      },
		{ AKEYCODE_DPAD_LEFT,     Key::GamepadLeft      },
		{ AKEYCODE_DPAD_RIGHT,    Key::GamepadRight     },
		{ AKEYCODE_BUTTON_START,  Key::GamepadStart     },
		{ AKEYCODE_BACK,          Key::GamepadBack      },
		{ AKEYCODE_BUTTON_THUMBL, Key::GamepadThumbL    },
		{ AKEYCODE_BUTTON_THUMBR, Key::GamepadThumbR    },
		{ AKEYCODE_BUTTON_L1,     Key::GamepadShoulderL },
		{ AKEYCODE_BUTTON_R1,     Key::GamepadShoulderR },
		{ AKEYCODE_GUIDE,         Key::GamepadGuide     },
		{ AKEYCODE_BUTTON_A,      Key::GamepadA         },
		{ AKEYCODE_BUTTON_B,      Key::GamepadB         },
		{ AKEYCODE_BUTTON_X,      Key::GamepadX         },
		{ AKEYCODE_BUTTON_Y,      Key::GamepadY         },
	};

	struct GamepadAxisRemap
	{
		int32_t m_event;
		GamepadAxis::Enum m_axis;
		bool m_convert;
	};

	static GamepadAxisRemap s_translateAxis[] =
	{
		{ AMOTION_EVENT_AXIS_X,        GamepadAxis::LeftX,  false },
		{ AMOTION_EVENT_AXIS_Y,        GamepadAxis::LeftY,  false },
		{ AMOTION_EVENT_AXIS_LTRIGGER, GamepadAxis::LeftZ,  false },
		{ AMOTION_EVENT_AXIS_Z,        GamepadAxis::RightX, true  },
		{ AMOTION_EVENT_AXIS_RZ,       GamepadAxis::RightY, false },
		{ AMOTION_EVENT_AXIS_RTRIGGER, GamepadAxis::RightZ, false },
	};

	struct MainThreadEntry
	{
		int m_argc;
		const char* const* m_argv;

		static int32_t threadFunc(bx::Thread* _thread, void* _userData);
	};

	class FileReaderAndroid : public bx::FileReaderI
	{
	public:
		FileReaderAndroid(AAssetManager* _assetManager, AAsset* _file)
			: m_assetManager(_assetManager)
			, m_file(_file)
			, m_open(false)
		{
		}

		virtual ~FileReaderAndroid()
		{
			close();
		}

		virtual bool open(const bx::FilePath& _filePath, bx::Error* _err) override
		{
			BX_ASSERT(NULL != _err, "Reader/Writer interface calling functions must handle errors.");

			if (NULL != m_file)
			{
				BX_ERROR_SET(_err, bx::kErrorReaderWriterAlreadyOpen, "FileReader: File is already open.");
				return false;
			}

			m_file = AAssetManager_open(m_assetManager, _filePath.getCPtr(), AASSET_MODE_RANDOM);
			if (NULL == m_file)
			{
				BX_ERROR_SET(_err, bx::kErrorReaderWriterOpen, "FileReader: Failed to open file.");
				return false;
			}

			m_open = true;
			return true;
		}

		virtual void close() override
		{
			if (m_open
			&&  NULL != m_file)
			{
				AAsset_close(m_file);
				m_file = NULL;
			}
		}

		virtual int64_t seek(int64_t _offset, bx::Whence::Enum _whence) override
		{
			BX_ASSERT(NULL != m_file, "Reader/Writer file is not open.");
			return AAsset_seek64(m_file, _offset, _whence);

		}

		virtual int32_t read(void* _data, int32_t _size, bx::Error* _err) override
		{
			BX_ASSERT(NULL != m_file, "Reader/Writer file is not open.");
			BX_ASSERT(NULL != _err, "Reader/Writer interface calling functions must handle errors.");

			int32_t size = (int32_t)AAsset_read(m_file, _data, _size);
			if (size != _size)
			{
				if (0 == AAsset_getRemainingLength(m_file) )
				{
					BX_ERROR_SET(_err, bx::kErrorRederWriterEof, "FileReader: EOF.");
				}

				return size >= 0 ? size : 0;
			}
<commit_msg>Update entry_android.cpp (#2375)<commit_after>/*
 * Copyright 2011-2021 <NAME>. All rights reserved.
 * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
 */

#include "entry_p.h"

#if ENTRY_CONFIG_USE_NATIVE && BX_PLATFORM_ANDROID

#include <bgfx/platform.h>

#include <bx/thread.h>
#include <bx/file.h>

#include <android/input.h>
#include <android/log.h>
#include <android/looper.h>
#include <android/window.h>
#include <android_native_app_glue.h>
#include <android/native_window.h>

extern "C"
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include <android_native_app_glue.c>
#pragma GCC diagnostic pop
} // extern "C"

namespace entry
{
	///
	inline void androidSetWindow(::ANativeWindow* _window)
	{
		bgfx::PlatformData pd;
		pd.ndt          = NULL;
		pd.nwh          = _window;
		pd.context      = NULL;
		pd.backBuffer   = NULL;
		pd.backBufferDS = NULL;
		bgfx::setPlatformData(pd);
	}

	struct GamepadRemap
	{
		uint16_t  m_keyCode;
		Key::Enum m_key;
	};

	static GamepadRemap s_gamepadRemap[] =
	{
		{ AKEYCODE_DPAD_UP,       Key::GamepadUp        },
		{ AKEYCODE_DPAD_DOWN,     Key::GamepadDown      },
		{ AKEYCODE_DPAD_LEFT,     Key::GamepadLeft      },
		{ AKEYCODE_DPAD_RIGHT,    Key::GamepadRight     },
		{ AKEYCODE_BUTTON_START,  Key::GamepadStart     },
		{ AKEYCODE_BACK,          Key::GamepadBack      },
		{ AKEYCODE_BUTTON_THUMBL, Key::GamepadThumbL    },
		{ AKEYCODE_BUTTON_THUMBR, Key::GamepadThumbR    },
		{ AKEYCODE_BUTTON_L1,     Key::GamepadShoulderL },
		{ AKEYCODE_BUTTON_R1,     Key::GamepadShoulderR },
		{ AKEYCODE_GUIDE,         Key::GamepadGuide     },
		{ AKEYCODE_BUTTON_A,      Key::GamepadA         },
		{ AKEYCODE_BUTTON_B,      Key::GamepadB         },
		{ AKEYCODE_BUTTON_X,      Key::GamepadX         },
		{ AKEYCODE_BUTTON_Y,      Key::GamepadY         },
	};

	struct GamepadAxisRemap
	{
		int32_t m_event;
		GamepadAxis::Enum m_axis;
		bool m_convert;
	};

	static GamepadAxisRemap s_translateAxis[] =
	{
		{ AMOTION_EVENT_AXIS_X,        GamepadAxis::LeftX,  false },
		{ AMOTION_EVENT_AXIS_Y,        GamepadAxis::LeftY,  false },
		{ AMOTION_EVENT_AXIS_LTRIGGER, GamepadAxis::LeftZ,  false },
		{ AMOTION_EVENT_AXIS_Z,        GamepadAxis::RightX, true  },
		{ AMOTION_EVENT_AXIS_RZ,       GamepadAxis::RightY, false },
		{ AMOTION_EVENT_AXIS_RTRIGGER, GamepadAxis::RightZ, false },
	};

	struct MainThreadEntry
	{
		int m_argc;
		const char* const* m_argv;

		static int32_t threadFunc(bx::Thread* _thread, void* _userData);
	};

	class FileReaderAndroid : public bx::FileReaderI
	{
	public:
		FileReaderAndroid(AAssetManager* _assetManager, AAsset* _file)
			: m_assetManager(_assetManager)
			, m_file(_file)
			, m_open(false)
		{
		}

		virtual ~FileReaderAndroid()
		{
			close();
		}

		virtual bool open(const bx::FilePath& _filePath, bx::Error* _err) override
		{
			BX_ASSERT(NULL != _err, "Reader/Writer interface calling functions must handle errors.");

			if (NULL != m_file)
			{
				BX_ERROR_SET(_err, bx::kErrorReaderWriterAlreadyOpen, "FileReader: File is already open.");
				return false;
			}

			m_file = AAssetManager_open(m_assetManager, _filePath.getCPtr(), AASSET_MODE_RANDOM);
			if (NULL == m_file)
			{
				BX_ERROR_SET(_err, bx::kErrorReaderWriterOpen, "FileReader: Failed to open file.");
				return false;
			}

			m_open = true;
			return true;
		}

		virtual void close() override
		{
			if (m_open
			&&  NULL != m_file)
			{
				AAsset_close(m_file);
				m_file = NULL;
			}
		}

		virtual int64_t seek(int64_t _offset, bx::Whence::Enum _whence) override
		{
			BX_ASSERT(NULL != m_file, "Reader/Writer file is not open.");
			return AAsset_seek64(m_file, _offset, _whence);

		}

		virtual int32_t read(void* _data, int32_t _size, bx::Error* _err) override
		{
			BX_ASSERT(NULL != m_file, "Reader/Writer file is not open.");
			BX_ASSERT(NULL != _err, "Reader/Writer interface calling functions must handle errors.");

			int32_t size = (int32_t)AAsset_read(m_file, _data, _size);
			if (size != _size)
			{
				if (0 == AAsset_getRemainingLength(m_file) )
				{
					BX_ERROR_SET(_err, bx::kErrorReaderWriterEof, "FileReader: EOF.");
				}

				return size >= 0 ? size : 0;
			}

{'fraction_non_alphanumeric': 0.12134884825457136, 'fraction_numerical': 0.008549038233198766, 'mean_word_length': 3.509100642398287, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, 'https://': 2, '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': '4214685', 'n_tokens_mistral': 3542, 'n_tokens_neox': 3189, 'n_words': 582}