Pointer and hardware key events

Introduced classes

interface AndroidInputEvent

android :: AndroidInputEvent

An input event on Android
extern class AndroidKeyEvent

android :: AndroidKeyEvent

An hardware key event
class AndroidMotionEvent

android :: AndroidMotionEvent

A motion event concerning a single or more pointers
class AndroidPointerEvent

android :: AndroidPointerEvent

A pointer event

Redefined classes

redef class App

android :: input_events $ App

App subclasses are cross-platform applications

All class definitions

interface AndroidInputEvent

android $ AndroidInputEvent

An input event on Android
extern class AndroidKeyEvent

android $ AndroidKeyEvent

An hardware key event
class AndroidMotionEvent

android $ AndroidMotionEvent

A motion event concerning a single or more pointers
redef class App

android :: input_events $ App

App subclasses are cross-platform applications
package_diagram android::input_events input_events mnit::input input android::input_events->mnit::input android::game game android::input_events->android::game core core mnit::input->core android::native_app_glue native_app_glue android::game->android::native_app_glue android::assets assets android::game->android::assets ...core ... ...core->core ...android::native_app_glue ... ...android::native_app_glue->android::native_app_glue ...android::assets ... ...android::assets->android::assets android::gamepad gamepad android::gamepad->android::input_events gamnit::gamnit_android gamnit_android gamnit::gamnit_android->android::input_events a_star-m a_star-m a_star-m->android::gamepad a_star-m... ... a_star-m...->a_star-m android::sensors sensors android::sensors->gamnit::gamnit_android gamnit::android19 android19 gamnit::android19->gamnit::gamnit_android gamnit::camera_control_android camera_control_android gamnit::camera_control_android->gamnit::gamnit_android android::sensors... ... android::sensors...->android::sensors gamnit::android19... ... gamnit::android19...->gamnit::android19 gamnit::camera_control_android... ... gamnit::camera_control_android...->gamnit::camera_control_android


module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module activities

android :: activities

Android Activities wrapper
module app

app :: app

app.nit is a framework to create cross-platform applications
module app_base

app :: app_base

Base of the app.nit framework, defines App
module array

core :: array

This module introduces the standard array structure.
module assets

app :: assets

Portable services to load resources from the assets folder
module assets

android :: assets

Implementation of app::assets
module assets_and_resources

android :: assets_and_resources

Android Assets and Resources Management
module aware

android :: aware

Android compatibility module
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module collections

java :: collections

Basic Java collections
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module dalvik

android :: dalvik

Java related services specific to Android and its Dalvik VM
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module ffi_support

java :: ffi_support

Core supporting services for the FFI with Java
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module io

java :: io

Services from the java.io package
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module java

java :: java

Supporting services for the FFI with Java and to access Java libraries
module jvm

jvm :: jvm

Java Virtual Machine invocation API and others services from the JNI C API
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module log

android :: log

Advanced Android logging services
module math

core :: math

Mathematical operations
module native

core :: native

Native structures for text and bytes
module native_app_glue

android :: native_app_glue

Wrapper of the Android native_app_glue framework to implement app.nit
module numeric

core :: numeric

Advanced services for Numeric types
module platform

android :: platform

Triggers compilation for the android platform
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O


module game

android :: game

Android services and implementation of app.nit for gamnit and mnit
module input

mnit :: input

Defines abstract classes for user and general inputs to the application.


module gamepad

android :: gamepad

Support for gamepad events (over Bluetooth or USB)
module gamnit_android

gamnit :: gamnit_android

Support services for Gamnit on Android


module a_star-m


module android19

gamnit :: android19

Variation using features from Android API 19
module camera_control_android

gamnit :: camera_control_android

Two fingers camera manipulation, pinch to zoom and slide to scroll
module sensors

android :: sensors

Access Android sensors
# Pointer and hardware key events
module input_events

import mnit::input
import android::game

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__))
		#define LOGI(...) (void)0

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) {
			return App_native_input_key(nit_app, event);
		else if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {
			return App_native_input_motion(nit_app, event);

		return 0;

private extern class NativeAndroidMotionEvent `{AInputEvent *`}

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

	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);
		else return -1;

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

	fun native_down_time: Int `{ return AMotionEvent_getDownTime(self); `}

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 `{

	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

# An input event on Android
interface AndroidInputEvent
	super InputEvent

# 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)]

	# 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

		return new AndroidPointerEvent(self, index)

	redef fun just_went_down do return native.action.is_down or native.action.is_pointer_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

	redef fun down_pointer: nullable AndroidPointerEvent
		if just_went_down then
			# The primary pointer went down
			return pointers[0]

		var i = native.index_down_pointer
		if i > 0 then
			# A secondary pointer went down
			return pointers[i]
			return null

	# Time when the user originally pressed down to start a stream of position events
	# The return value is in the `java.lang.System.nanoTime()` time base.
	fun down_time: Int do return native.native_down_time

# A pointer event
class AndroidPointerEvent
	super PointerEvent
	super AndroidInputEvent

	private var motion_event: AndroidMotionEvent

	private var pointer_index: Int

	redef fun x do return native_x(motion_event.native, pointer_index)

	private fun native_x(motion_event: NativeAndroidMotionEvent, pointer_index: Int): Float `{
		return AMotionEvent_getX(motion_event, pointer_index);

	redef fun y do return native_y(motion_event.native, pointer_index)

	private fun native_y(motion_event: NativeAndroidMotionEvent, pointer_index: Int): Float `{
		return AMotionEvent_getY(motion_event, pointer_index);

	# Pressure applied by this pointer
	fun pressure: Float do return native_pressure(motion_event.native, pointer_index)

	private fun native_pressure(motion_event: NativeAndroidMotionEvent, pointer_index: Int): Float `{
		return AMotionEvent_getPressure(motion_event, pointer_index);

	redef fun pressed
		var action = motion_event.native.action
		return action.is_down or action.is_move or action.is_pointer_down

	redef fun is_move do return motion_event.acting_pointer == self and

	# Does this pointer just began touching the screen?
	fun just_went_down: Bool do return motion_event.acting_pointer == self and

	# Unique id of this pointer since the beginning of the gesture
	redef fun pointer_id do return native_pointer_id(motion_event.native, pointer_index)

	private fun native_pointer_id(motion_event: NativeAndroidMotionEvent, pointer_index: Int): Int `{
		return AMotionEvent_getPointerId(motion_event, pointer_index);

# An hardware key event
extern class AndroidKeyEvent `{AInputEvent *`}
	super KeyEvent
	super AndroidInputEvent

	private fun action: Int `{ return AKeyEvent_getAction(self); `}

	redef fun is_down do return action == 0
	redef fun is_up do return action == 1

	# Hardware code of the key raising this event
	fun key_code: Int `{ return AKeyEvent_getKeyCode(self); `}

	redef fun to_c
		var i = native_to_c
		if i == 0 then return null
		return i.code_point

	private fun native_to_c: Int `{
		int code = AKeyEvent_getKeyCode(self);
		if (code >= AKEYCODE_0 && code <= AKEYCODE_9)
			return '0'+code-AKEYCODE_0;
		if (code >= AKEYCODE_A && code <= AKEYCODE_Z)
			return 'a'+code-AKEYCODE_A;
		return 0;

	redef fun name do return key_code.to_s

	# Was this event raised by the back key?
	fun is_back_key: Bool do return key_code == 4

	# Was this event raised by the menu key?
	fun is_menu_key: Bool do return key_code == 82

	# Was this event raised by the search key?
	fun is_search_key: Bool do return key_code == 84

	# Was this event raised by the volume up key?
	fun is_volume_up: Bool do return key_code == 24

	# Was this event raised by the volume down key?
	fun is_volume_down: Bool do return key_code == 25

redef class App

	redef fun init_window
		set_as_input_handler native_app_glue

	private fun set_as_input_handler(app_glue: NativeAppGlue)
	import native_input_key, native_input_motion `{
		app_glue->onInputEvent = mnit_handle_input;

	# these are used as a callback from native to type incoming events
	private fun native_input_key(event: AndroidKeyEvent): Bool is abstract

	private fun native_input_motion(event: NativeAndroidMotionEvent): Bool is abstract