<commit_before># This file is part of NIT (http://www.nitlanguage.org).
#
# Copyright 2012-2014 <NAME> <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Pointer and hardware key events
module input_events

import mnit_input
import android

in "C header" `{
	#include <android/log.h>
	#include <android_native_app_glue.h>

	#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "mnit", __VA_ARGS__))
	#ifdef DEBUG
		#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "mnit", __VA_ARGS__))
	#else
		#define LOGI(...) (void)0
	#endif
`}

in "C" `{
	/* Handle inputs from the Android platform and sort them before
	   sending them in the Nit App */
	static int32_t mnit_handle_input(struct android_app* app, AInputEvent* event) {
		App nit_app = app->userData;
		LOGI("handle input %i", (int)pthread_self());
		if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_KEY) {
			LOGI("key");
			return App_native_input_key(nit_app, event);
		}
		else if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {
			LOGI("motion");
			return App_native_input_motion(nit_app, event);
		}

		return 0;
	}
`}

private extern class NativeAndroidMotionEvent `{AInputEvent *`}

	fun pointers_count: Int `{
		return AMotionEvent_getPointerCount(self);
	`}

	# Did this motion event just started?
	fun just_went_down: Bool `{
		return (AMotionEvent_getAction(self) & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_DOWN;
	`}

	fun edge: Int `{
		return AMotionEvent_getEdgeFlags(self);
	`}

	# Get the non-primary pointer id that just went down (returns -1 or > 0)
	fun index_down_pointer: Int `{
		int a = AMotionEvent_getAction(self);
		if ((a & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN)
			return (a & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
		else return -1;
	`}

	fun action: AMotionEventAction `{ return AMotionEvent_getAction(self); `}
end

private extern class AMotionEventAction `{ int32_t `}
	fun action: Int `{ return self & AMOTION_EVENT_ACTION_MASK; `}

	fun is_down: Bool do return action == 0
	fun is_up: Bool do return action == 1
	fun is_move: Bool do return action == 2
	fun is_cancel: Bool do return action == 3
	fun is_outside: Bool do return action == 4
	fun is_pointer_down: Bool do return action == 5
	fun is_pointer_up: Bool do return action == 6
end

# An input event on Android
interface AndroidInputEvent
	super InputEvent
end

# A motion event concerning a single or more `pointers`
class AndroidMotionEvent
	super AndroidInputEvent
	super MotionEvent

	private var native: NativeAndroidMotionEvent

	# Pointers (or fingers) composing this motion event
	var pointers: Array[AndroidPointerEvent] is lazy do
		return [for i in native.pointers_count.times do new AndroidPointerEvent(self, i)]
	end

	redef fun just_went_down: Bool do return native.just_went_down

	# Was the top edge of the screen intersected by this event?
	fun touch_to_edge: Bool do return native.edge == 1

	# Was the bottom edge of the screen intersected by this event?
	fun touch_bottom_edge: Bool do return native.edge == 2

	# Was the left edge of the screen intersected by this event?
	fun touch_left_edge: Bool do return native.edge == 4

	# Was the right edge of the screen intersected by this event?
	fun touch_right_edge: Bool do return native.edge == 8
<commit_msg>lib/android: intro: `acting_pointer` and fix warnings on `just_went_down`<commit_after># This file is part of NIT (http://www.nitlanguage.org).
#
# Copyright 2012-2014 <NAME> <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Pointer and hardware key events
module input_events

import mnit_input
import android

in "C header" `{
	#include <android/log.h>
	#include <android_native_app_glue.h>

	#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "mnit", __VA_ARGS__))
	#ifdef DEBUG
		#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "mnit", __VA_ARGS__))
	#else
		#define LOGI(...) (void)0
	#endif
`}

in "C" `{
	/* Handle inputs from the Android platform and sort them before
	   sending them in the Nit App */
	static int32_t mnit_handle_input(struct android_app* app, AInputEvent* event) {
		App nit_app = app->userData;
		LOGI("handle input %i", (int)pthread_self());
		if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_KEY) {
			LOGI("key");
			return App_native_input_key(nit_app, event);
		}
		else if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {
			LOGI("motion");
			return App_native_input_motion(nit_app, event);
		}

		return 0;
	}
`}

private extern class NativeAndroidMotionEvent `{AInputEvent *`}

	fun pointers_count: Int `{
		return AMotionEvent_getPointerCount(self);
	`}

	# Did this motion event just started?
	fun just_went_down: Bool `{
		return (AMotionEvent_getAction(self) & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_DOWN;
	`}

	fun edge: Int `{
		return AMotionEvent_getEdgeFlags(self);
	`}

	# Get the non-primary pointer id that just went down (returns -1 or > 0)
	fun index_down_pointer: Int `{
		int a = AMotionEvent_getAction(self);
		if ((a & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN)
			return (a & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
		else return -1;
	`}

	fun action: AMotionEventAction `{ return AMotionEvent_getAction(self); `}
end

private extern class AMotionEventAction `{ int32_t `}
	fun action: Int `{ return self & AMOTION_EVENT_ACTION_MASK; `}

	# Pointer index concerned by this action
	#
	# Require: `is_pointer_down or is_pointer_up`
	fun pointer_index: Int `{
		return (self & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
	`}

	fun is_down: Bool do return action == 0
	fun is_up: Bool do return action == 1
	fun is_move: Bool do return action == 2
	fun is_cancel: Bool do return action == 3
	fun is_outside: Bool do return action == 4
	fun is_pointer_down: Bool do return action == 5
	fun is_pointer_up: Bool do return action == 6
end

# An input event on Android
interface AndroidInputEvent
	super InputEvent
end

# A motion event concerning a single or more `pointers`
class AndroidMotionEvent
	super AndroidInputEvent
	super MotionEvent

	private var native: NativeAndroidMotionEvent

	# Pointers (or fingers) composing this motion event
	var pointers: Array[AndroidPointerEvent] is lazy do
		return [for i in native.pointers_count.times do new AndroidPointerEvent(self, i)]
	end

	# The pointer (or finger) causing this event
	var acting_pointer: AndroidPointerEvent is lazy do
		var action = native.action
		var index = 0

		if action.is_pointer_down or action.is_pointer_up then
			index = native.action.pointer_index
		end

		return new AndroidPointerEvent(self, index)
	end

	redef fun just_went_down do return native.just_went_down

	# Was the top edge of the screen intersected by this event?
	fun touch_to_edge: Bool do return native.edge == 1

	# Was the bottom edge of the screen intersected by this event?
	fun touch_bottom_edge: Bool do return native.edge == 2

	# Was the left edge of the screen intersected by this event?
	fun touch_left_edge: Bool do return native.edge == 4

	# Was the right edge of the screen intersected by this event?
	fun touch_right_edge: Bool do return native.edge == 8

{'fraction_non_alphanumeric': 0.08492159227985525, 'fraction_numerical': 0.007840772014475271, 'mean_word_length': 4.7656467315716275, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '7459277', 'n_tokens_mistral': 2839, 'n_tokens_neox': 2645, 'n_words': 915}