2022-02-20 13:09:39 +01:00
|
|
|
package juloo.keyboard2;
|
|
|
|
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Message;
|
|
|
|
import java.util.ArrayList;
|
2024-01-26 00:17:51 +01:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.NoSuchElementException;
|
2022-02-20 13:09:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Manage pointers (fingers) on the screen and long presses.
|
|
|
|
* Call back to IPointerEventHandler.
|
|
|
|
*/
|
|
|
|
public final class Pointers implements Handler.Callback
|
|
|
|
{
|
2024-03-11 00:10:12 +01:00
|
|
|
public static final int FLAG_P_LATCHABLE = 1;
|
|
|
|
public static final int FLAG_P_LATCHED = (1 << 1);
|
|
|
|
public static final int FLAG_P_FAKE = (1 << 2);
|
|
|
|
public static final int FLAG_P_LOCKABLE = (1 << 3);
|
|
|
|
public static final int FLAG_P_LOCKED = (1 << 4);
|
2024-03-11 00:38:37 +01:00
|
|
|
public static final int FLAG_P_SLIDING = (1 << 5);
|
2024-03-18 01:00:22 +01:00
|
|
|
/** Clear latched (only if also FLAG_P_LATCHABLE set). */
|
|
|
|
public static final int FLAG_P_CLEAR_LATCHED = (1 << 6);
|
|
|
|
/** Can't be locked, even when long pressing. */
|
|
|
|
public static final int FLAG_P_CANT_LOCK = (1 << 7);
|
2024-03-11 00:10:12 +01:00
|
|
|
|
2022-02-20 13:09:39 +01:00
|
|
|
private Handler _keyrepeat_handler;
|
|
|
|
private ArrayList<Pointer> _ptrs = new ArrayList<Pointer>();
|
|
|
|
private IPointerEventHandler _handler;
|
|
|
|
private Config _config;
|
|
|
|
|
|
|
|
public Pointers(IPointerEventHandler h, Config c)
|
|
|
|
{
|
|
|
|
_keyrepeat_handler = new Handler(this);
|
|
|
|
_handler = h;
|
|
|
|
_config = c;
|
|
|
|
}
|
|
|
|
|
2022-06-05 01:38:42 +02:00
|
|
|
/** Return the list of modifiers currently activated. */
|
|
|
|
public Modifiers getModifiers()
|
2022-04-30 23:36:17 +02:00
|
|
|
{
|
2022-06-05 01:38:42 +02:00
|
|
|
return getModifiers(false);
|
2022-04-30 23:36:17 +02:00
|
|
|
}
|
|
|
|
|
2022-06-05 01:38:42 +02:00
|
|
|
/** When [skip_latched] is true, don't take flags of latched keys into account. */
|
|
|
|
private Modifiers getModifiers(boolean skip_latched)
|
2022-02-20 13:09:39 +01:00
|
|
|
{
|
2022-06-05 19:30:53 +02:00
|
|
|
int n_ptrs = _ptrs.size();
|
2024-03-18 00:14:19 +01:00
|
|
|
KeyValue[] mods = new KeyValue[n_ptrs];
|
2022-06-05 19:30:53 +02:00
|
|
|
int n_mods = 0;
|
|
|
|
for (int i = 0; i < n_ptrs; i++)
|
2022-04-30 23:36:17 +02:00
|
|
|
{
|
2022-06-05 01:38:42 +02:00
|
|
|
Pointer p = _ptrs.get(i);
|
2024-03-18 00:14:19 +01:00
|
|
|
if (p.value != null
|
2024-03-11 00:29:12 +01:00
|
|
|
&& !(skip_latched && p.hasFlagsAny(FLAG_P_LATCHED)
|
2024-03-11 00:10:12 +01:00
|
|
|
&& (p.flags & FLAG_P_LOCKED) == 0))
|
2024-03-18 00:14:19 +01:00
|
|
|
mods[n_mods++] = p.value;
|
2022-04-30 23:36:17 +02:00
|
|
|
}
|
2022-06-05 19:30:53 +02:00
|
|
|
return Modifiers.ofArray(mods, n_mods);
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void clear()
|
|
|
|
{
|
2022-12-04 18:21:59 +01:00
|
|
|
for (Pointer p : _ptrs)
|
|
|
|
stopKeyRepeat(p);
|
2022-02-20 13:09:39 +01:00
|
|
|
_ptrs.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isKeyDown(KeyboardData.Key k)
|
|
|
|
{
|
|
|
|
for (Pointer p : _ptrs)
|
|
|
|
if (p.key == k)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-03-11 00:10:12 +01:00
|
|
|
/** See [FLAG_P_*] flags. Returns [-1] if the key is not pressed. */
|
2022-02-20 13:09:39 +01:00
|
|
|
public int getKeyFlags(KeyValue kv)
|
|
|
|
{
|
|
|
|
for (Pointer p : _ptrs)
|
2022-06-06 00:23:45 +02:00
|
|
|
if (p.value != null && p.value.equals(kv))
|
2022-02-20 13:09:39 +01:00
|
|
|
return p.flags;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-03-03 17:08:25 +01:00
|
|
|
/** The key must not be already latched . */
|
|
|
|
void add_fake_pointer(KeyboardData.Key key, KeyValue kv, boolean locked)
|
2022-07-24 20:02:48 +02:00
|
|
|
{
|
2024-03-03 17:08:25 +01:00
|
|
|
Pointer ptr = new Pointer(-1, key, kv, 0.f, 0.f, Modifiers.EMPTY);
|
2024-03-11 00:29:12 +01:00
|
|
|
ptr.flags = FLAG_P_FAKE | FLAG_P_LATCHED;
|
2022-10-23 21:34:05 +02:00
|
|
|
if (locked)
|
2024-03-11 00:10:12 +01:00
|
|
|
ptr.flags |= FLAG_P_LOCKED;
|
2022-07-24 20:02:48 +02:00
|
|
|
_ptrs.add(ptr);
|
2024-01-26 00:17:51 +01:00
|
|
|
_handler.onPointerFlagsChanged(false);
|
2022-07-24 20:02:48 +02:00
|
|
|
}
|
|
|
|
|
2024-03-03 17:08:25 +01:00
|
|
|
/** Set whether a key is latched or locked by adding a "fake" pointer, a
|
|
|
|
pointer that is not due to user interaction.
|
|
|
|
This is used by auto-capitalisation.
|
|
|
|
|
|
|
|
When [lock] is true, [latched] control whether the modifier is locked or disabled.
|
|
|
|
When [lock] is false, an existing locked pointer is not affected. */
|
|
|
|
public void set_fake_pointer_state(KeyboardData.Key key, KeyValue kv,
|
|
|
|
boolean latched, boolean lock)
|
2022-07-24 20:02:48 +02:00
|
|
|
{
|
|
|
|
Pointer ptr = getLatched(key, kv);
|
2024-03-03 17:08:25 +01:00
|
|
|
if (ptr == null)
|
|
|
|
{
|
|
|
|
// No existing pointer, latch the key.
|
|
|
|
if (latched)
|
|
|
|
add_fake_pointer(key, kv, lock);
|
|
|
|
}
|
2024-03-11 00:10:12 +01:00
|
|
|
else if ((ptr.flags & FLAG_P_FAKE) != 0)
|
2024-03-03 17:08:25 +01:00
|
|
|
{} // Key already latched but not by a fake ptr, do nothing.
|
|
|
|
else if (lock)
|
|
|
|
{
|
|
|
|
// Acting on locked modifiers, replace the pointer each time.
|
2022-07-24 20:02:48 +02:00
|
|
|
removePtr(ptr);
|
2024-03-03 17:08:25 +01:00
|
|
|
if (latched)
|
|
|
|
add_fake_pointer(key, kv, lock);
|
|
|
|
}
|
2024-03-11 00:10:12 +01:00
|
|
|
else if ((ptr.flags & FLAG_P_LOCKED) != 0)
|
2024-03-03 17:08:25 +01:00
|
|
|
{} // Existing ptr is locked but [lock] is false, do not continue.
|
|
|
|
else if (!latched)
|
|
|
|
{
|
|
|
|
// Key is latched by a fake ptr. Unlatch if requested.
|
|
|
|
removePtr(ptr);
|
|
|
|
_handler.onPointerFlagsChanged(false);
|
|
|
|
}
|
2022-07-24 20:02:48 +02:00
|
|
|
}
|
|
|
|
|
2022-02-20 13:09:39 +01:00
|
|
|
// Receiving events
|
|
|
|
|
|
|
|
public void onTouchUp(int pointerId)
|
|
|
|
{
|
|
|
|
Pointer ptr = getPtr(pointerId);
|
|
|
|
if (ptr == null)
|
|
|
|
return;
|
2024-03-11 00:38:37 +01:00
|
|
|
if (ptr.hasFlagsAny(FLAG_P_SLIDING))
|
2023-01-29 18:49:44 +01:00
|
|
|
{
|
2023-04-08 20:10:41 +02:00
|
|
|
clearLatched();
|
2024-05-02 19:31:48 +02:00
|
|
|
ptr.sliding.onTouchUp(ptr);
|
2023-01-29 18:49:44 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-02-20 13:09:39 +01:00
|
|
|
stopKeyRepeat(ptr);
|
2022-03-19 15:39:20 +01:00
|
|
|
Pointer latched = getLatched(ptr);
|
2022-02-20 13:09:39 +01:00
|
|
|
if (latched != null) // Already latched
|
|
|
|
{
|
|
|
|
removePtr(ptr); // Remove dupplicate
|
2024-03-11 00:10:12 +01:00
|
|
|
if ((latched.flags & FLAG_P_LOCKABLE) != 0) // Toggle lockable key
|
2022-07-24 23:55:00 +02:00
|
|
|
lockPointer(latched, false);
|
2022-02-20 13:09:39 +01:00
|
|
|
else // Otherwise, unlatch
|
|
|
|
{
|
|
|
|
removePtr(latched);
|
2022-06-05 01:38:42 +02:00
|
|
|
_handler.onPointerUp(ptr.value, ptr.modifiers);
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
}
|
2024-03-11 00:10:12 +01:00
|
|
|
else if ((ptr.flags & FLAG_P_LATCHABLE) != 0)
|
2022-02-20 13:09:39 +01:00
|
|
|
{
|
2024-03-18 01:00:22 +01:00
|
|
|
// Latchable but non-special keys must clear latched.
|
|
|
|
if ((ptr.flags & FLAG_P_CLEAR_LATCHED) != 0)
|
|
|
|
clearLatched();
|
2024-03-11 00:29:12 +01:00
|
|
|
ptr.flags |= FLAG_P_LATCHED;
|
|
|
|
ptr.pointerId = -1;
|
2022-07-24 23:55:00 +02:00
|
|
|
_handler.onPointerFlagsChanged(false);
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
clearLatched();
|
|
|
|
removePtr(ptr);
|
2022-06-05 01:38:42 +02:00
|
|
|
_handler.onPointerUp(ptr.value, ptr.modifiers);
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:21:59 +01:00
|
|
|
public void onTouchCancel()
|
2022-03-15 20:44:02 +01:00
|
|
|
{
|
2022-12-04 18:21:59 +01:00
|
|
|
clear();
|
2022-07-24 23:55:00 +02:00
|
|
|
_handler.onPointerFlagsChanged(true);
|
2022-03-15 20:44:02 +01:00
|
|
|
}
|
|
|
|
|
2022-04-30 23:36:17 +02:00
|
|
|
/* Whether an other pointer is down on a non-special key. */
|
|
|
|
private boolean isOtherPointerDown()
|
|
|
|
{
|
|
|
|
for (Pointer p : _ptrs)
|
2024-03-11 00:29:12 +01:00
|
|
|
if (!p.hasFlagsAny(FLAG_P_LATCHED) &&
|
2024-03-11 00:10:12 +01:00
|
|
|
(p.value == null || !p.value.hasFlagsAny(KeyValue.FLAG_SPECIAL)))
|
2022-04-30 23:36:17 +02:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-20 13:09:39 +01:00
|
|
|
public void onTouchDown(float x, float y, int pointerId, KeyboardData.Key key)
|
|
|
|
{
|
2023-01-22 23:03:30 +01:00
|
|
|
// Ignore new presses while a sliding key is active. On some devices, ghost
|
|
|
|
// touch events can happen while the pointer travels on top of other keys.
|
|
|
|
if (isSliding())
|
|
|
|
return;
|
2022-06-05 01:38:42 +02:00
|
|
|
// Don't take latched modifiers into account if an other key is pressed.
|
|
|
|
// The other key already "own" the latched modifiers and will clear them.
|
|
|
|
Modifiers mods = getModifiers(isOtherPointerDown());
|
2023-03-05 23:08:35 +01:00
|
|
|
KeyValue value = _handler.modifyKey(key.keys[0], mods);
|
2022-06-05 01:38:42 +02:00
|
|
|
Pointer ptr = new Pointer(pointerId, key, value, x, y, mods);
|
2022-02-20 13:09:39 +01:00
|
|
|
_ptrs.add(ptr);
|
2022-07-24 23:55:00 +02:00
|
|
|
startKeyRepeat(ptr);
|
2023-08-26 23:37:22 +02:00
|
|
|
_handler.onPointerDown(value, false);
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
|
2023-03-03 19:44:05 +01:00
|
|
|
static final int[] DIRECTION_TO_INDEX = new int[]{
|
|
|
|
7, 2, 2, 6, 6, 4, 4, 8, 8, 3, 3, 5, 5, 1, 1, 7
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [direction] is an int between [0] and [15] that represent 16 sections of a
|
|
|
|
* circle, clockwise, starting at the top.
|
|
|
|
*/
|
|
|
|
KeyValue getKeyAtDirection(KeyboardData.Key k, int direction)
|
|
|
|
{
|
2023-03-05 23:08:35 +01:00
|
|
|
return k.keys[DIRECTION_TO_INDEX[direction]];
|
2023-03-03 19:44:05 +01:00
|
|
|
}
|
|
|
|
|
2022-05-08 16:38:44 +02:00
|
|
|
/*
|
2023-03-03 19:44:05 +01:00
|
|
|
* Get the KeyValue at the given direction. In case of swipe (direction !=
|
|
|
|
* null), get the nearest KeyValue that is not key0.
|
2022-05-08 16:38:44 +02:00
|
|
|
* Take care of applying [_handler.onPointerSwipe] to the selected key, this
|
|
|
|
* must be done at the same time to be sure to treat removed keys correctly.
|
|
|
|
* Return [null] if no key could be found in the given direction or if the
|
|
|
|
* selected key didn't change.
|
|
|
|
*/
|
2023-03-03 19:44:05 +01:00
|
|
|
private KeyValue getNearestKeyAtDirection(Pointer ptr, Integer direction)
|
2022-05-08 16:38:44 +02:00
|
|
|
{
|
2023-03-03 19:44:05 +01:00
|
|
|
if (direction == null)
|
2023-03-05 23:08:35 +01:00
|
|
|
return _handler.modifyKey(ptr.key.keys[0], ptr.modifiers);
|
2022-05-08 16:38:44 +02:00
|
|
|
KeyValue k;
|
2023-03-03 19:44:05 +01:00
|
|
|
// [i] is [0, -1, 1, -2, 2, ...]
|
|
|
|
for (int i = 0; i > -4; i = (~i>>31) - i)
|
2022-05-08 16:38:44 +02:00
|
|
|
{
|
2023-03-13 03:06:43 +01:00
|
|
|
int d = (direction + i + 16) % 16;
|
2022-05-08 16:38:44 +02:00
|
|
|
// Don't make the difference between a key that doesn't exist and a key
|
|
|
|
// that is removed by [_handler]. Triggers side effects.
|
2023-03-03 19:44:05 +01:00
|
|
|
k = _handler.modifyKey(getKeyAtDirection(ptr.key, d), ptr.modifiers);
|
2022-05-08 16:38:44 +02:00
|
|
|
if (k != null)
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-02-20 13:09:39 +01:00
|
|
|
public void onTouchMove(float x, float y, int pointerId)
|
|
|
|
{
|
|
|
|
Pointer ptr = getPtr(pointerId);
|
|
|
|
if (ptr == null)
|
|
|
|
return;
|
2024-05-02 19:31:48 +02:00
|
|
|
if (ptr.hasFlagsAny(FLAG_P_SLIDING))
|
|
|
|
{
|
|
|
|
ptr.sliding.onTouchMove(ptr, x);
|
|
|
|
return;
|
|
|
|
}
|
2022-05-06 18:38:43 +02:00
|
|
|
|
|
|
|
// The position in a IME windows is clampled to view.
|
|
|
|
// For a better up swipe behaviour, set the y position to a negative value when clamped.
|
|
|
|
if (y == 0.0) y = -400;
|
2022-02-20 13:09:39 +01:00
|
|
|
float dx = x - ptr.downX;
|
|
|
|
float dy = y - ptr.downY;
|
2022-05-07 00:08:20 +02:00
|
|
|
|
2023-01-22 23:03:30 +01:00
|
|
|
float dist = Math.abs(dx) + Math.abs(dy);
|
2023-03-03 19:44:05 +01:00
|
|
|
Integer direction;
|
2022-02-20 13:09:39 +01:00
|
|
|
if (dist < _config.swipe_dist_px)
|
|
|
|
{
|
2023-03-03 19:44:05 +01:00
|
|
|
direction = null;
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
2022-05-07 00:08:20 +02:00
|
|
|
else
|
|
|
|
{
|
2023-03-03 19:44:05 +01:00
|
|
|
// See [getKeyAtDirection()] for the meaning. The starting point on the
|
|
|
|
// circle is the top direction.
|
2023-03-13 03:06:43 +01:00
|
|
|
double a = Math.atan2(dy, dx) + Math.PI;
|
|
|
|
// a is between 0 and 2pi, 0 is pointing to the left
|
|
|
|
// add 12 to align 0 to the top
|
|
|
|
direction = ((int)(a * 8 / Math.PI) + 12) % 16;
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
2022-05-07 00:08:20 +02:00
|
|
|
|
2022-05-08 16:38:44 +02:00
|
|
|
if (direction != ptr.selected_direction)
|
2022-02-20 13:09:39 +01:00
|
|
|
{
|
2022-05-08 16:38:44 +02:00
|
|
|
ptr.selected_direction = direction;
|
2023-03-03 19:44:05 +01:00
|
|
|
KeyValue newValue = getNearestKeyAtDirection(ptr, direction);
|
|
|
|
if (newValue != null && !newValue.equals(ptr.value))
|
2022-02-21 00:24:57 +01:00
|
|
|
{
|
2022-03-19 15:39:20 +01:00
|
|
|
ptr.value = newValue;
|
2024-03-11 00:10:12 +01:00
|
|
|
ptr.flags = pointer_flags_of_kv(newValue);
|
2023-03-03 19:44:05 +01:00
|
|
|
// Sliding mode is entered when key5 or key6 is down on a slider key.
|
|
|
|
if (ptr.key.slider &&
|
2023-03-05 23:08:35 +01:00
|
|
|
(newValue.equals(ptr.key.getKeyValue(5))
|
|
|
|
|| newValue.equals(ptr.key.getKeyValue(6))))
|
2023-01-22 23:03:30 +01:00
|
|
|
{
|
2024-05-02 19:31:48 +02:00
|
|
|
startSliding(ptr, x);
|
2023-01-22 23:03:30 +01:00
|
|
|
}
|
2023-08-26 23:37:22 +02:00
|
|
|
_handler.onPointerDown(newValue, true);
|
2022-02-21 00:24:57 +01:00
|
|
|
}
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pointers management
|
|
|
|
|
|
|
|
private Pointer getPtr(int pointerId)
|
|
|
|
{
|
|
|
|
for (Pointer p : _ptrs)
|
|
|
|
if (p.pointerId == pointerId)
|
|
|
|
return p;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void removePtr(Pointer ptr)
|
|
|
|
{
|
|
|
|
_ptrs.remove(ptr);
|
|
|
|
}
|
|
|
|
|
2022-03-19 15:39:20 +01:00
|
|
|
private Pointer getLatched(Pointer target)
|
2022-02-20 13:09:39 +01:00
|
|
|
{
|
2022-07-24 20:02:48 +02:00
|
|
|
return getLatched(target.key, target.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Pointer getLatched(KeyboardData.Key k, KeyValue v)
|
|
|
|
{
|
2022-05-01 00:11:52 +02:00
|
|
|
if (v == null)
|
|
|
|
return null;
|
2022-02-20 13:09:39 +01:00
|
|
|
for (Pointer p : _ptrs)
|
2024-03-11 00:29:12 +01:00
|
|
|
if (p.key == k && p.hasFlagsAny(FLAG_P_LATCHED)
|
|
|
|
&& p.value != null && p.value.equals(v))
|
2022-02-20 13:09:39 +01:00
|
|
|
return p;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void clearLatched()
|
|
|
|
{
|
|
|
|
for (int i = _ptrs.size() - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
Pointer ptr = _ptrs.get(i);
|
|
|
|
// Latched and not locked, remove
|
2024-03-11 00:29:12 +01:00
|
|
|
if (ptr.hasFlagsAny(FLAG_P_LATCHED) && (ptr.flags & FLAG_P_LOCKED) == 0)
|
2022-02-20 13:09:39 +01:00
|
|
|
_ptrs.remove(i);
|
2022-07-24 23:55:00 +02:00
|
|
|
// Not latched but pressed, don't latch once released and stop long press.
|
2024-03-11 00:10:12 +01:00
|
|
|
else if ((ptr.flags & FLAG_P_LATCHABLE) != 0)
|
|
|
|
ptr.flags &= ~FLAG_P_LATCHABLE;
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 23:55:00 +02:00
|
|
|
/** Make a pointer into the locked state. */
|
|
|
|
private void lockPointer(Pointer ptr, boolean shouldVibrate)
|
|
|
|
{
|
2024-03-11 00:10:12 +01:00
|
|
|
ptr.flags = (ptr.flags & ~FLAG_P_LOCKABLE) | FLAG_P_LOCKED;
|
2022-07-24 23:55:00 +02:00
|
|
|
_handler.onPointerFlagsChanged(shouldVibrate);
|
|
|
|
}
|
|
|
|
|
2023-01-22 23:03:30 +01:00
|
|
|
boolean isSliding()
|
|
|
|
{
|
|
|
|
for (Pointer ptr : _ptrs)
|
2024-03-11 00:38:37 +01:00
|
|
|
if (ptr.hasFlagsAny(FLAG_P_SLIDING))
|
2023-01-22 23:03:30 +01:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-20 13:09:39 +01:00
|
|
|
// Key repeat
|
|
|
|
|
|
|
|
/** Message from [_keyrepeat_handler]. */
|
|
|
|
@Override
|
|
|
|
public boolean handleMessage(Message msg)
|
|
|
|
{
|
|
|
|
for (Pointer ptr : _ptrs)
|
|
|
|
{
|
|
|
|
if (ptr.timeoutWhat == msg.what)
|
|
|
|
{
|
2022-07-24 23:55:00 +02:00
|
|
|
if (handleKeyRepeat(ptr))
|
2023-01-22 23:13:30 +01:00
|
|
|
_keyrepeat_handler.sendEmptyMessageDelayed(msg.what,
|
|
|
|
_config.longPressInterval);
|
2022-07-24 23:55:00 +02:00
|
|
|
else
|
|
|
|
ptr.timeoutWhat = -1;
|
|
|
|
return true;
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
}
|
2022-07-24 23:55:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-20 13:09:39 +01:00
|
|
|
private static int uniqueTimeoutWhat = 0;
|
|
|
|
|
|
|
|
private void startKeyRepeat(Pointer ptr)
|
|
|
|
{
|
|
|
|
int what = (uniqueTimeoutWhat++);
|
|
|
|
ptr.timeoutWhat = what;
|
2023-01-22 23:13:30 +01:00
|
|
|
_keyrepeat_handler.sendEmptyMessageDelayed(what, _config.longPressTimeout);
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void stopKeyRepeat(Pointer ptr)
|
|
|
|
{
|
|
|
|
if (ptr.timeoutWhat != -1)
|
|
|
|
{
|
|
|
|
_keyrepeat_handler.removeMessages(ptr.timeoutWhat);
|
|
|
|
ptr.timeoutWhat = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 23:55:00 +02:00
|
|
|
/** A pointer is repeating. Returns [true] if repeat should continue. */
|
|
|
|
private boolean handleKeyRepeat(Pointer ptr)
|
|
|
|
{
|
|
|
|
// Long press toggle lock on modifiers
|
2024-03-11 00:10:12 +01:00
|
|
|
if ((ptr.flags & FLAG_P_LATCHABLE) != 0)
|
2022-07-24 23:55:00 +02:00
|
|
|
{
|
2024-03-18 01:00:22 +01:00
|
|
|
if (!ptr.hasFlagsAny(FLAG_P_CANT_LOCK))
|
|
|
|
lockPointer(ptr, true);
|
2022-07-24 23:55:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
2023-02-12 23:14:57 +01:00
|
|
|
// Stop repeating: Latched key, no key
|
2024-03-11 00:29:12 +01:00
|
|
|
if (ptr.hasFlagsAny(FLAG_P_LATCHED) || ptr.value == null)
|
2022-07-24 23:55:00 +02:00
|
|
|
return false;
|
2023-02-12 23:14:57 +01:00
|
|
|
KeyValue kv = KeyModifier.modify_long_press(ptr.value);
|
|
|
|
if (!kv.equals(ptr.value))
|
|
|
|
{
|
|
|
|
ptr.value = kv;
|
2023-08-26 23:37:22 +02:00
|
|
|
_handler.onPointerDown(kv, true);
|
2023-02-12 23:14:57 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Stop repeating: Special keys
|
2024-02-17 19:31:52 +01:00
|
|
|
if (kv.hasFlagsAny(KeyValue.FLAG_SPECIAL))
|
2023-02-12 23:14:57 +01:00
|
|
|
return false;
|
|
|
|
_handler.onPointerHold(kv, ptr.modifiers);
|
2022-07-24 23:55:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-22 23:03:30 +01:00
|
|
|
// Sliding
|
|
|
|
|
2024-05-02 19:31:48 +02:00
|
|
|
void startSliding(Pointer ptr, float x)
|
2023-01-22 23:03:30 +01:00
|
|
|
{
|
|
|
|
stopKeyRepeat(ptr);
|
2024-03-11 00:38:37 +01:00
|
|
|
ptr.flags |= FLAG_P_SLIDING;
|
2024-05-02 19:31:48 +02:00
|
|
|
ptr.sliding = new Sliding(x);
|
2023-01-22 23:03:30 +01:00
|
|
|
}
|
2022-02-21 00:24:57 +01:00
|
|
|
|
2024-03-11 00:10:12 +01:00
|
|
|
/** Return the [FLAG_P_*] flags that correspond to pressing [kv]. */
|
|
|
|
static int pointer_flags_of_kv(KeyValue kv)
|
|
|
|
{
|
|
|
|
int flags = 0;
|
|
|
|
if (kv.hasFlagsAny(KeyValue.FLAG_LATCH))
|
2024-03-18 01:00:22 +01:00
|
|
|
{
|
|
|
|
// Non-special latchable key must clear modifiers and can't be locked
|
|
|
|
if (!kv.hasFlagsAny(KeyValue.FLAG_SPECIAL))
|
|
|
|
flags |= FLAG_P_CLEAR_LATCHED | FLAG_P_CANT_LOCK;
|
2024-03-11 00:10:12 +01:00
|
|
|
flags |= FLAG_P_LATCHABLE;
|
2024-03-18 01:00:22 +01:00
|
|
|
}
|
2024-03-11 00:10:12 +01:00
|
|
|
if (kv.hasFlagsAny(KeyValue.FLAG_LOCK))
|
|
|
|
flags |= FLAG_P_LOCKABLE;
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2022-06-05 01:38:42 +02:00
|
|
|
private static final class Pointer
|
2022-02-20 13:09:39 +01:00
|
|
|
{
|
|
|
|
/** -1 when latched. */
|
|
|
|
public int pointerId;
|
2022-05-07 00:08:20 +02:00
|
|
|
/** The Key pressed by this Pointer */
|
2022-03-19 15:39:20 +01:00
|
|
|
public final KeyboardData.Key key;
|
2023-03-03 19:44:05 +01:00
|
|
|
/** Current direction. [null] means not swiping. */
|
|
|
|
public Integer selected_direction;
|
2022-06-05 01:38:42 +02:00
|
|
|
/** Selected value with [modifiers] applied. */
|
2022-02-20 13:09:39 +01:00
|
|
|
public KeyValue value;
|
|
|
|
public float downX;
|
|
|
|
public float downY;
|
2022-04-30 23:17:20 +02:00
|
|
|
/** Modifier flags at the time the key was pressed. */
|
2022-06-05 01:38:42 +02:00
|
|
|
public Modifiers modifiers;
|
2024-03-11 00:10:12 +01:00
|
|
|
/** See [FLAG_P_*] flags. */
|
2022-02-20 13:09:39 +01:00
|
|
|
public int flags;
|
|
|
|
/** Identify timeout messages. */
|
|
|
|
public int timeoutWhat;
|
2024-05-02 19:31:48 +02:00
|
|
|
/** [null] when not in sliding mode. */
|
|
|
|
public Sliding sliding;
|
2022-02-20 13:09:39 +01:00
|
|
|
|
2022-06-05 01:38:42 +02:00
|
|
|
public Pointer(int p, KeyboardData.Key k, KeyValue v, float x, float y, Modifiers m)
|
2022-02-20 13:09:39 +01:00
|
|
|
{
|
|
|
|
pointerId = p;
|
|
|
|
key = k;
|
2023-03-03 19:44:05 +01:00
|
|
|
selected_direction = null;
|
2022-02-20 13:09:39 +01:00
|
|
|
value = v;
|
|
|
|
downX = x;
|
|
|
|
downY = y;
|
2022-06-05 01:38:42 +02:00
|
|
|
modifiers = m;
|
2024-03-11 00:10:12 +01:00
|
|
|
flags = (v == null) ? 0 : pointer_flags_of_kv(v);
|
2022-02-20 13:09:39 +01:00
|
|
|
timeoutWhat = -1;
|
2024-05-02 19:31:48 +02:00
|
|
|
sliding = null;
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
2024-03-11 00:29:12 +01:00
|
|
|
|
|
|
|
public boolean hasFlagsAny(int has)
|
|
|
|
{
|
|
|
|
return ((flags & has) != 0);
|
|
|
|
}
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
|
2024-05-02 19:31:48 +02:00
|
|
|
public final class Sliding
|
|
|
|
{
|
|
|
|
/** Accumulated distance since last event. */
|
|
|
|
float d = 0.f;
|
|
|
|
/** The slider speed changes depending on the pointer speed. */
|
|
|
|
float speed = 1.f;
|
|
|
|
/** Coordinate of the last move. */
|
|
|
|
float last_x;
|
|
|
|
/** [System.currentTimeMillis()] at the time of the last move. */
|
|
|
|
long last_move_ms;
|
|
|
|
|
|
|
|
public Sliding(float x)
|
|
|
|
{
|
|
|
|
last_x = x;
|
|
|
|
last_move_ms = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
|
|
|
|
static final float SPEED_SMOOTHING = 0.7f;
|
|
|
|
/** Avoid absurdly large values. */
|
|
|
|
static final float SPEED_MAX = 4.f;
|
|
|
|
|
|
|
|
public void onTouchMove(Pointer ptr, float x)
|
|
|
|
{
|
|
|
|
d += (x - last_x) * speed / _config.slide_step_px;
|
|
|
|
update_speed(x);
|
|
|
|
// Send an event when [abs(d)] exceeds [1].
|
|
|
|
int d_ = (int)d;
|
|
|
|
if (d_ != 0)
|
|
|
|
{
|
|
|
|
d -= d_;
|
|
|
|
int key_index = (d_ < 0) ? 5 : 6;
|
|
|
|
ptr.value = _handler.modifyKey(ptr.key.keys[key_index], ptr.modifiers);
|
|
|
|
send_key(ptr, Math.abs(d_));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Handle a sliding pointer going up. Latched modifiers are not
|
|
|
|
cleared to allow easy adjustments to the cursors. The pointer is
|
|
|
|
cancelled. */
|
|
|
|
public void onTouchUp(Pointer ptr)
|
|
|
|
{
|
|
|
|
removePtr(ptr);
|
|
|
|
_handler.onPointerFlagsChanged(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Send the pressed key [n] times. */
|
|
|
|
void send_key(Pointer ptr, int n)
|
|
|
|
{
|
|
|
|
if (ptr.value == null)
|
|
|
|
return;
|
|
|
|
// Avoid looping if possible to avoid lag while sliding fast
|
|
|
|
KeyValue multiplied = multiply_key(ptr.value, n);
|
|
|
|
if (multiplied != null)
|
|
|
|
_handler.onPointerHold(multiplied, ptr.modifiers);
|
|
|
|
else
|
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
_handler.onPointerHold(ptr.value, ptr.modifiers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return a key performing the same action as [kv] but [n] times. Returns
|
|
|
|
[null] if [kv] cannot be multiplied. */
|
|
|
|
KeyValue multiply_key(KeyValue kv, int n)
|
|
|
|
{
|
|
|
|
switch (kv.getKind())
|
|
|
|
{
|
|
|
|
case Cursor_move:
|
|
|
|
return KeyValue.cursorMoveKey(kv.getCursorMove() * n);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** [speed] is computed from the elapsed time and distance traveled
|
|
|
|
between two move events. Exponential smoothing is used to smooth out
|
|
|
|
the noise. Sets [last_move_ms] and [last_x]. */
|
|
|
|
void update_speed(float x)
|
|
|
|
{
|
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
float instant_speed = Math.min(SPEED_MAX,
|
|
|
|
Math.abs(x - last_x) / (float)(now - last_move_ms) + 1.f);
|
|
|
|
speed = speed + (instant_speed - speed) * SPEED_SMOOTHING;
|
|
|
|
last_move_ms = now;
|
|
|
|
last_x = x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 01:38:42 +02:00
|
|
|
/** Represent modifiers currently activated.
|
|
|
|
Sorted in the order they should be evaluated. */
|
|
|
|
public static final class Modifiers
|
|
|
|
{
|
2024-03-18 00:14:19 +01:00
|
|
|
private final KeyValue[] _mods;
|
2022-06-05 01:38:42 +02:00
|
|
|
private final int _size;
|
|
|
|
|
2024-03-18 00:14:19 +01:00
|
|
|
private Modifiers(KeyValue[] m, int s)
|
2022-06-05 19:30:53 +02:00
|
|
|
{
|
|
|
|
_mods = m; _size = s;
|
|
|
|
}
|
2022-06-05 01:38:42 +02:00
|
|
|
|
2024-03-18 00:14:19 +01:00
|
|
|
public KeyValue get(int i) { return _mods[_size - 1 - i]; }
|
2022-06-05 01:38:42 +02:00
|
|
|
public int size() { return _size; }
|
2023-06-03 09:37:59 +02:00
|
|
|
public boolean has(KeyValue.Modifier m)
|
|
|
|
{
|
2024-03-18 00:14:19 +01:00
|
|
|
for (int i = 0; i < _size; i++)
|
|
|
|
{
|
|
|
|
KeyValue kv = _mods[i];
|
|
|
|
switch (kv.getKind())
|
|
|
|
{
|
|
|
|
case Modifier:
|
|
|
|
if (kv.getModifier().equals(m))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2023-06-03 09:37:59 +02:00
|
|
|
}
|
2022-06-05 01:38:42 +02:00
|
|
|
|
2024-01-26 00:17:51 +01:00
|
|
|
/** Returns the activated modifiers that are not in [m2]. */
|
2024-03-18 00:14:19 +01:00
|
|
|
public Iterator<KeyValue> diff(Modifiers m2)
|
2024-01-26 00:17:51 +01:00
|
|
|
{
|
|
|
|
return new ModifiersDiffIterator(this, m2);
|
|
|
|
}
|
|
|
|
|
2022-06-05 01:38:42 +02:00
|
|
|
@Override
|
|
|
|
public int hashCode() { return Arrays.hashCode(_mods); }
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj)
|
|
|
|
{
|
|
|
|
return Arrays.equals(_mods, ((Modifiers)obj)._mods);
|
|
|
|
}
|
|
|
|
|
2022-06-05 19:30:53 +02:00
|
|
|
public static final Modifiers EMPTY =
|
2024-03-18 00:14:19 +01:00
|
|
|
new Modifiers(new KeyValue[0], 0);
|
2022-06-05 01:38:42 +02:00
|
|
|
|
2024-03-18 00:14:19 +01:00
|
|
|
protected static Modifiers ofArray(KeyValue[] mods, int size)
|
2022-06-05 01:38:42 +02:00
|
|
|
{
|
2022-06-05 19:30:53 +02:00
|
|
|
// Sort and remove duplicates and nulls.
|
2022-06-05 01:38:42 +02:00
|
|
|
if (size > 1)
|
|
|
|
{
|
2022-06-05 19:30:53 +02:00
|
|
|
Arrays.sort(mods, 0, size);
|
2022-06-05 01:38:42 +02:00
|
|
|
int j = 0;
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
{
|
2024-03-18 00:14:19 +01:00
|
|
|
KeyValue m = mods[i];
|
2022-06-05 19:30:53 +02:00
|
|
|
if (m != null && (i + 1 >= size || m != mods[i + 1]))
|
2022-06-05 01:38:42 +02:00
|
|
|
{
|
|
|
|
mods[j] = m;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size = j;
|
|
|
|
}
|
|
|
|
return new Modifiers(mods, size);
|
|
|
|
}
|
2024-01-26 00:17:51 +01:00
|
|
|
|
|
|
|
/** Returns modifiers that are in [m1_] but not in [m2_]. */
|
|
|
|
static final class ModifiersDiffIterator
|
2024-03-18 00:14:19 +01:00
|
|
|
implements Iterator<KeyValue>
|
2024-01-26 00:17:51 +01:00
|
|
|
{
|
|
|
|
Modifiers m1;
|
|
|
|
int i1 = 0;
|
|
|
|
Modifiers m2;
|
|
|
|
int i2 = 0;
|
|
|
|
|
|
|
|
public ModifiersDiffIterator(Modifiers m1_, Modifiers m2_)
|
|
|
|
{
|
|
|
|
m1 = m1_;
|
|
|
|
m2 = m2_;
|
|
|
|
advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasNext()
|
|
|
|
{
|
|
|
|
return i1 < m1._size;
|
|
|
|
}
|
|
|
|
|
2024-03-18 00:14:19 +01:00
|
|
|
public KeyValue next()
|
2024-01-26 00:17:51 +01:00
|
|
|
{
|
|
|
|
if (i1 >= m1._size)
|
|
|
|
throw new NoSuchElementException();
|
2024-03-18 00:14:19 +01:00
|
|
|
KeyValue m = m1._mods[i1];
|
2024-01-26 00:17:51 +01:00
|
|
|
i1++;
|
|
|
|
advance();
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Advance to the next element if [i1] is not a valid element. The end
|
|
|
|
is reached when [i1 = m1.size()]. */
|
|
|
|
void advance()
|
|
|
|
{
|
|
|
|
while (i1 < m1.size())
|
|
|
|
{
|
2024-03-18 00:14:19 +01:00
|
|
|
KeyValue m = m1._mods[i1];
|
2024-01-26 00:17:51 +01:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (i2 >= m2._size)
|
|
|
|
return;
|
|
|
|
int d = m.compareTo(m2._mods[i2]);
|
|
|
|
if (d < 0)
|
|
|
|
return;
|
|
|
|
i2++;
|
|
|
|
if (d == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i1++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-05 01:38:42 +02:00
|
|
|
}
|
|
|
|
|
2022-02-20 13:09:39 +01:00
|
|
|
public interface IPointerEventHandler
|
|
|
|
{
|
2022-05-08 16:53:33 +02:00
|
|
|
/** Key can be modified or removed by returning [null]. */
|
2024-03-11 00:10:12 +01:00
|
|
|
public KeyValue modifyKey(KeyValue k, Modifiers mods);
|
2022-03-19 15:39:20 +01:00
|
|
|
|
2022-06-05 01:38:42 +02:00
|
|
|
/** A key is pressed. [getModifiers()] is uptodate. Might be called after a
|
2023-08-26 23:37:22 +02:00
|
|
|
press or a swipe to a different value. Down events are not paired with
|
|
|
|
up events. */
|
|
|
|
public void onPointerDown(KeyValue k, boolean isSwipe);
|
2022-03-19 15:39:20 +01:00
|
|
|
|
2022-05-08 16:53:33 +02:00
|
|
|
/** Key is released. [k] is the key that was returned by
|
|
|
|
[modifySelectedKey] or [modifySelectedKey]. */
|
2024-03-11 00:10:12 +01:00
|
|
|
public void onPointerUp(KeyValue k, Modifiers mods);
|
2022-03-19 15:39:20 +01:00
|
|
|
|
|
|
|
/** Flags changed because latched or locked keys or cancelled pointers. */
|
2022-07-24 23:55:00 +02:00
|
|
|
public void onPointerFlagsChanged(boolean shouldVibrate);
|
2022-03-19 15:39:20 +01:00
|
|
|
|
|
|
|
/** Key is repeating. */
|
2024-03-11 00:10:12 +01:00
|
|
|
public void onPointerHold(KeyValue k, Modifiers mods);
|
2022-02-20 13:09:39 +01:00
|
|
|
}
|
|
|
|
}
|