Skip to content

Commit

Permalink
Graphical console keyboard refactoring: Step 2
Browse files Browse the repository at this point in the history
  • Loading branch information
green-green-avk committed Jan 20, 2022
1 parent dc0922a commit ecdd0a1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected void onCreate(@Nullable final Bundle savedInstanceState) {
mCkv.setFont(fp); // Old Android devices have no glyphs for some special symbols

mCkv.setMode(GraphicsConsoleKeyboardView.MODE_HW_ONLY);
mCkv.setAnsiMode(false);
mCkv.setTextMode(false);

setSessionTitle(mSession.compositor.title);

Expand Down Expand Up @@ -226,13 +226,13 @@ public void onSwitchIme(final View view) {
}

private void updateImeTextModeUi() {
wImeTextMode.setImageState(mCkv.isAnsiMode() ?
wImeTextMode.setImageState(mCkv.isTextMode() ?
new int[]{android.R.attr.state_checked} : new int[]{},
true);
}

public void onSwitchImeTextMode(final View view) {
mCkv.setAnsiMode(!mCkv.isAnsiMode());
mCkv.setTextMode(!mCkv.isTextMode());
updateImeTextModeUi();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package green_green_avk.anotherterm;

import androidx.annotation.NonNull;

/**
* Stub yet...
*/
public final class GraphicsConsoleLedsInput {
public IConsoleOutput consoleOutput = null;

public boolean numLed = false;
public boolean capsLed = false;
public boolean scrollLed = false;

public interface OnInvalidateSink {
void onInvalidateSink();
}

public void addOnInvalidateSink(@NonNull final OnInvalidateSink h) {
}

public void removeOnInvalidateSink(@NonNull final OnInvalidateSink h) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public void handleMessage(@NonNull final Message msg) {
final KeyTouchState keyState = mTouchedKeys.getRepeatable();
if (keyState != null && keyState.isPressed && mAutoRepeatAllowed && getAutoRepeat()) {
sendEmptyMessageDelayed(MSG_REPEAT, mAutoRepeatInterval);
sendKeyUp(keyState.key, mModifiers);
sendKeyKey(keyState, mModifiers);
}
break;
}
Expand Down Expand Up @@ -400,27 +400,36 @@ private static ExtKeyboard.KeyFcn getKeyFcn(@NonNull final ExtKeyboard.Key key,
return fcn;
}

protected void sendKeyDown(@NonNull final ExtKeyboard.Key key, final int modifiers) {
protected void sendKeyDown(@NonNull final KeyTouchState keyState, final int modifiers) {
if (mKeyboardActionListener == null) return;
final ExtKeyboard.KeyFcn fcn = getKeyFcn(key, modifiers);
if (fcn != null && fcn.code != ExtKeyboard.KEYCODE_NONE)
final ExtKeyboard.KeyFcn fcn = getKeyFcn(keyState.key, modifiers);
if (fcn != null && fcn.code != ExtKeyboard.KEYCODE_NONE) {
keyState.toRelease = fcn.code;
mKeyboardActionListener.onPress(fcn.code);
}
}

protected void sendKeyUp(@NonNull final ExtKeyboard.Key key, final int modifiers) {
protected void sendKeyKey(@NonNull final KeyTouchState keyState, final int modifiers) {
if (mKeyboardActionListener == null) return;
final ExtKeyboard.KeyFcn fcn = getKeyFcn(key, modifiers);
final ExtKeyboard.KeyFcn fcn = getKeyFcn(keyState.key, modifiers);
if (fcn != null) {
if (fcn.code != ExtKeyboard.KEYCODE_NONE) {
mKeyboardActionListener.onKey(fcn.code,
fcn.modifiers, fcn.modifiersMask);
mKeyboardActionListener.onRelease(fcn.code);
}
if (fcn.text != null)
mKeyboardActionListener.onText(fcn.text);
}
}

protected void sendKeyUp(@NonNull final KeyTouchState keyState) {
if (mKeyboardActionListener == null) return;
if (keyState.toRelease != ExtKeyboard.KEYCODE_NONE) {
mKeyboardActionListener.onRelease(keyState.toRelease);
keyState.toRelease = ExtKeyboard.KEYCODE_NONE;
}
}

@Nullable
protected ExtKeyboard.Key getKey(final float x, final float y) {
return mKeyboard.getKey(
Expand Down Expand Up @@ -506,8 +515,9 @@ public void invalidate() {
protected static final class KeyTouchState {
public ExtKeyboard.Key key = null;
public final PointF coords = new PointF();
public boolean isPressed = true;
public boolean isPressed = true; // for rendering
public Popup popup = null;
public int toRelease = ExtKeyboard.KEYCODE_NONE; // for press/release events
}

protected interface KeyTouchStateFilter {
Expand All @@ -526,8 +536,9 @@ protected void cancelKeys() {
protected void releaseKey(@NonNull final KeyTouchState keyState) {
final ExtKeyboard.KeyFcn fcn = keyState.popup.getAltKeyFcn();
if (fcn != null) {
sendKeyUp(keyState.key, fcn.modifiers);
sendKeyKey(keyState, fcn.modifiers);
}
sendKeyUp(keyState);
}

@Override
Expand All @@ -541,14 +552,15 @@ public boolean onTouchEvent(final MotionEvent event) {
if (key == null || key.type != ExtKeyboard.Key.KEY)
return super.onTouchEvent(event);
final boolean first = !mTouchedKeys.isPressed(key);
mTouchedKeys.put(event.getPointerId(index),
key, event.getX(index), event.getY(index));
final KeyTouchState keyState =
mTouchedKeys.put(event.getPointerId(index), key,
event.getX(index), event.getY(index));
if (mTouchedKeys.getRepeatable() != null) {
mHandler.removeMessages(MSG_REPEAT);
mHandler.sendEmptyMessageDelayed(MSG_REPEAT, mAutoRepeatDelay);
}
if (first)
sendKeyDown(key, mModifiers);
sendKeyDown(keyState, mModifiers);
invalidateKey(key, true);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.os.Build;
import android.text.InputType;
import android.util.AttributeSet;
Expand All @@ -30,15 +29,15 @@
import androidx.annotation.RequiresApi;
import androidx.core.view.ViewCompat;

import green_green_avk.anotherterm.AnsiConsoleInput;
import green_green_avk.anotherterm.GraphicsConsoleLedsInput;
import green_green_avk.anotherterm.HwKeyMapManager;
import green_green_avk.anotherterm.IConsoleOutput;
import green_green_avk.anotherterm.R;
import green_green_avk.anotherterm.utils.KeyIntervalDetector;

public class GraphicsConsoleKeyboardView extends ExtKeyboardView implements
ExtKeyboardView.OnKeyboardActionListener, AnsiConsoleInput.OnInvalidateSink {
protected AnsiConsoleInput consoleInput = null;
ExtKeyboardView.OnKeyboardActionListener, GraphicsConsoleLedsInput.OnInvalidateSink {
protected GraphicsConsoleLedsInput consoleInput = null;
protected IConsoleOutput consoleOutput = null;

protected boolean ctrl = false;
Expand All @@ -55,7 +54,7 @@ public class GraphicsConsoleKeyboardView extends ExtKeyboardView implements
protected int mode = MODE_VISIBLE;
protected int prevMode = mode;
protected int currMode = mode;
protected boolean ansiMode = false; // For teh text input!1
protected boolean textMode = false; // For teh text input!1

protected int keyHeightDp = 0;
@NonNull
Expand All @@ -67,16 +66,16 @@ public class GraphicsConsoleKeyboardView extends ExtKeyboardView implements

public static class State {
private int mode = MODE_UNKNOWN;
private boolean ansiNode = false;
private boolean textMode = false;

public void save(@NonNull final GraphicsConsoleKeyboardView v) {
mode = v.getMode();
ansiNode = v.ansiMode;
textMode = v.textMode;
}

public void apply(@NonNull final GraphicsConsoleKeyboardView v) {
if (mode == MODE_UNKNOWN) return;
v.ansiMode = ansiNode;
v.textMode = textMode;
v.setMode(mode);
}
}
Expand Down Expand Up @@ -142,17 +141,13 @@ protected void doInvalidateSink() {
}

@Override
public void onInvalidateSink(@Nullable final Rect rect) {
public void onInvalidateSink() {
if (!invalidating) {
invalidating = true;
ViewCompat.postOnAnimation(this, this::doInvalidateSink);
}
}

@Override
public void onInvalidateSinkResize(final int cols, final int rows) {
}

@SuppressLint("ResourceType")
private void applyConfig(@NonNull final Configuration cfg) {
final Resources res = getContext().getResources();
Expand Down Expand Up @@ -180,20 +175,21 @@ private void applyConfig(@NonNull final Configuration cfg) {
applyMode(cfg);
}

public void setConsoleInput(@NonNull final AnsiConsoleInput consoleInput) {
public void setConsoleInput(@NonNull final GraphicsConsoleLedsInput consoleInput) {
this.consoleInput = consoleInput;
this.consoleOutput = this.consoleInput.consoleOutput;
this.consoleInput.addOnInvalidateSink(this);
onInvalidateSink(null);
onInvalidateSink();
}

public void setConsoleOutputOnly(@NonNull final IConsoleOutput consoleOutput) {
this.consoleOutput = consoleOutput;
onInvalidateSink(null);
onInvalidateSink();
}

public void unsetConsoleInput() {
if (consoleInput != null) consoleInput.removeOnInvalidateSink(this);
if (consoleInput != null)
consoleInput.removeOnInvalidateSink(this);
consoleOutput = null;
consoleInput = null;
}
Expand Down Expand Up @@ -293,13 +289,13 @@ public void setMode(final int mode) {
applyMode(getResources().getConfiguration());
}

public boolean isAnsiMode() {
return ansiMode;
public boolean isTextMode() {
return textMode;
}

public void setAnsiMode(final boolean v) {
if (ansiMode != v) {
ansiMode = v;
public void setTextMode(final boolean v) {
if (textMode != v) {
textMode = v;
//reapplyMode();
}
}
Expand Down Expand Up @@ -565,7 +561,7 @@ public InputConnection onCreateInputConnection(final EditorInfo outAttrs) {

@Override
public boolean onKeyDown(final int keyCode, final KeyEvent event) {
if (!isAnsiMode()) {
if (!isTextMode()) {
if (consoleOutput != null)
consoleOutput.feed(keyCode, true);
return true;
Expand Down Expand Up @@ -631,7 +627,7 @@ public boolean onKeyLongPress(final int keyCode, final KeyEvent event) {

@Override
public boolean onKeyUp(final int keyCode, final KeyEvent event) {
if (!isAnsiMode()) {
if (!isTextMode()) {
if (consoleOutput != null)
consoleOutput.feed(keyCode, false);
return true;
Expand All @@ -658,7 +654,7 @@ protected void onConfigurationChanged(final Configuration newConfig) {
public void onPress(final int primaryCode) {
if (primaryCode == ExtKeyboard.KEYCODE_NONE)
return;
if (isAnsiMode()) {
if (isTextMode()) {
switch (primaryCode) {
case KeyEvent.KEYCODE_SHIFT_LEFT:
case KeyEvent.KEYCODE_SHIFT_RIGHT:
Expand Down Expand Up @@ -711,7 +707,7 @@ public void onPress(final int primaryCode) {
public void onKey(final int primaryCode, final int modifiers, final int modifiersMask) {
if (primaryCode == ExtKeyboard.KEYCODE_NONE)
return;
if (isAnsiMode()) {
if (isTextMode()) {
switch (primaryCode) {
case KeyEvent.KEYCODE_SHIFT_LEFT:
case KeyEvent.KEYCODE_SHIFT_RIGHT:
Expand Down Expand Up @@ -756,7 +752,7 @@ public void onKey(final int primaryCode, final int modifiers, final int modifier
public void onRelease(final int primaryCode) {
if (primaryCode == ExtKeyboard.KEYCODE_NONE)
return;
if (!isAnsiMode()) {
if (!isTextMode()) {
switch (primaryCode) {
case KeyEvent.KEYCODE_SHIFT_LEFT:
case KeyEvent.KEYCODE_SHIFT_RIGHT:
Expand Down

0 comments on commit ecdd0a1

Please sign in to comment.