diff --git a/app/src/main/java/com/termux/app/terminal/TermuxTerminalSessionClient.java b/app/src/main/java/com/termux/app/terminal/TermuxTerminalSessionClient.java index 4e836aef38..8b4ec00062 100644 --- a/app/src/main/java/com/termux/app/terminal/TermuxTerminalSessionClient.java +++ b/app/src/main/java/com/termux/app/terminal/TermuxTerminalSessionClient.java @@ -207,6 +207,13 @@ public void onTerminalCursorStateChange(boolean enabled) { + @Override + public Integer getTerminalCursorStyle() { + return mActivity.getProperties().getTerminalCursorStyle(); + } + + + /** Initialize and get mBellSoundPool */ private synchronized SoundPool getBellSoundPool() { if (mBellSoundPool == null) { diff --git a/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java b/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java index 7c04488d97..1fe8f0a9ca 100644 --- a/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java +++ b/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java @@ -38,10 +38,6 @@ public final class TerminalEmulator { public static final int MOUSE_WHEELUP_BUTTON = 64; public static final int MOUSE_WHEELDOWN_BUTTON = 65; - public static final int CURSOR_STYLE_BLOCK = 0; - public static final int CURSOR_STYLE_UNDERLINE = 1; - public static final int CURSOR_STYLE_BAR = 2; - /** Used for invalid data - http://en.wikipedia.org/wiki/Replacement_character#Replacement_character */ public static final int UNICODE_REPLACEMENT_CHAR = 0xFFFD; @@ -126,14 +122,14 @@ public final class TerminalEmulator { /** Not really DECSET bit... - http://www.vt100.net/docs/vt510-rm/DECSACE */ private static final int DECSET_BIT_RECTANGULAR_CHANGEATTRIBUTE = 1 << 12; + private String mTitle; private final Stack mTitleStack = new Stack<>(); + /** The cursor position. Between (0,0) and (mRows-1, mColumns-1). */ private int mCursorRow, mCursorCol; - private int mCursorStyle = CURSOR_STYLE_BLOCK; - /** The number of character rows and columns in the terminal screen. */ public int mRows, mColumns; @@ -142,6 +138,19 @@ public final class TerminalEmulator { public static final int TERMINAL_TRANSCRIPT_ROWS_MAX = 50000; public static final int DEFAULT_TERMINAL_TRANSCRIPT_ROWS = 2000; + + /* The supported terminal cursor styles. */ + + public static final int TERMINAL_CURSOR_STYLE_BLOCK = 0; + public static final int TERMINAL_CURSOR_STYLE_UNDERLINE = 1; + public static final int TERMINAL_CURSOR_STYLE_BAR = 2; + public static final int DEFAULT_TERMINAL_CURSOR_STYLE = TERMINAL_CURSOR_STYLE_BLOCK; + public static final Integer[] TERMINAL_CURSOR_STYLES_LIST = new Integer[]{TERMINAL_CURSOR_STYLE_BLOCK, TERMINAL_CURSOR_STYLE_UNDERLINE, TERMINAL_CURSOR_STYLE_BAR}; + + /** The terminal cursor styles. */ + private int mCursorStyle = DEFAULT_TERMINAL_CURSOR_STYLE; + + /** The normal screen buffer. Stores the characters that appear on the screen of the emulated terminal. */ private final TerminalBuffer mMainBuffer; /** @@ -312,6 +321,7 @@ public TerminalEmulator(TerminalOutput session, int columns, int rows, Integer t public void updateTerminalSessionClient(TerminalSessionClient client) { mClient = client; + setCursorStyle(); } public TerminalBuffer getScreen() { @@ -396,11 +406,24 @@ public int getCursorCol() { return mCursorCol; } - /** {@link #CURSOR_STYLE_BAR}, {@link #CURSOR_STYLE_BLOCK} or {@link #CURSOR_STYLE_UNDERLINE} */ + /** Get the terminal cursor style. It will be one of {@link #TERMINAL_CURSOR_STYLES_LIST} */ public int getCursorStyle() { return mCursorStyle; } + /** Set the terminal cursor style. */ + public void setCursorStyle() { + Integer cursorStyle = null; + + if (mClient != null) + cursorStyle = mClient.getTerminalCursorStyle(); + + if (cursorStyle == null || !Arrays.asList(TERMINAL_CURSOR_STYLES_LIST).contains(cursorStyle)) + mCursorStyle = DEFAULT_TERMINAL_CURSOR_STYLE; + else + mCursorStyle = cursorStyle; + } + public boolean isReverseVideo() { return isDecsetInternalBitSet(DECSET_BIT_REVERSE_VIDEO); } @@ -818,15 +841,15 @@ public void processCodePoint(int b) { case 0: // Blinking block. case 1: // Blinking block. case 2: // Steady block. - mCursorStyle = CURSOR_STYLE_BLOCK; + mCursorStyle = TERMINAL_CURSOR_STYLE_BLOCK; break; case 3: // Blinking underline. case 4: // Steady underline. - mCursorStyle = CURSOR_STYLE_UNDERLINE; + mCursorStyle = TERMINAL_CURSOR_STYLE_UNDERLINE; break; case 5: // Blinking bar (xterm addition). case 6: // Steady bar (xterm addition). - mCursorStyle = CURSOR_STYLE_BAR; + mCursorStyle = TERMINAL_CURSOR_STYLE_BAR; break; } break; @@ -2342,7 +2365,7 @@ public void clearScrollCounter() { /** Reset terminal state so user can interact with it regardless of present state. */ public void reset() { - mCursorStyle = CURSOR_STYLE_BLOCK; + setCursorStyle(); mArgIndex = 0; mContinueSequence = false; mEscapeState = ESC_NONE; diff --git a/terminal-emulator/src/main/java/com/termux/terminal/TerminalSessionClient.java b/terminal-emulator/src/main/java/com/termux/terminal/TerminalSessionClient.java index 9c99803023..f8d83d061e 100644 --- a/terminal-emulator/src/main/java/com/termux/terminal/TerminalSessionClient.java +++ b/terminal-emulator/src/main/java/com/termux/terminal/TerminalSessionClient.java @@ -22,6 +22,11 @@ public interface TerminalSessionClient { void onTerminalCursorStateChange(boolean state); + + Integer getTerminalCursorStyle(); + + + void logError(String tag, String message); void logWarn(String tag, String message); diff --git a/terminal-emulator/src/test/java/com/termux/terminal/TerminalTest.java b/terminal-emulator/src/test/java/com/termux/terminal/TerminalTest.java index af63fda104..390c0496ec 100644 --- a/terminal-emulator/src/test/java/com/termux/terminal/TerminalTest.java +++ b/terminal-emulator/src/test/java/com/termux/terminal/TerminalTest.java @@ -103,23 +103,23 @@ public void testDeviceStatusReport() throws Exception { /** Test the cursor shape changes using DECSCUSR. */ public void testSetCursorStyle() throws Exception { withTerminalSized(5, 5); - assertEquals(TerminalEmulator.CURSOR_STYLE_BLOCK, mTerminal.getCursorStyle()); + assertEquals(TerminalEmulator.TERMINAL_CURSOR_STYLE_BLOCK, mTerminal.getCursorStyle()); enterString("\033[3 q"); - assertEquals(TerminalEmulator.CURSOR_STYLE_UNDERLINE, mTerminal.getCursorStyle()); + assertEquals(TerminalEmulator.TERMINAL_CURSOR_STYLE_UNDERLINE, mTerminal.getCursorStyle()); enterString("\033[5 q"); - assertEquals(TerminalEmulator.CURSOR_STYLE_BAR, mTerminal.getCursorStyle()); + assertEquals(TerminalEmulator.TERMINAL_CURSOR_STYLE_BAR, mTerminal.getCursorStyle()); enterString("\033[0 q"); - assertEquals(TerminalEmulator.CURSOR_STYLE_BLOCK, mTerminal.getCursorStyle()); + assertEquals(TerminalEmulator.TERMINAL_CURSOR_STYLE_BLOCK, mTerminal.getCursorStyle()); enterString("\033[6 q"); - assertEquals(TerminalEmulator.CURSOR_STYLE_BAR, mTerminal.getCursorStyle()); + assertEquals(TerminalEmulator.TERMINAL_CURSOR_STYLE_BAR, mTerminal.getCursorStyle()); enterString("\033[4 q"); - assertEquals(TerminalEmulator.CURSOR_STYLE_UNDERLINE, mTerminal.getCursorStyle()); + assertEquals(TerminalEmulator.TERMINAL_CURSOR_STYLE_UNDERLINE, mTerminal.getCursorStyle()); enterString("\033[1 q"); - assertEquals(TerminalEmulator.CURSOR_STYLE_BLOCK, mTerminal.getCursorStyle()); + assertEquals(TerminalEmulator.TERMINAL_CURSOR_STYLE_BLOCK, mTerminal.getCursorStyle()); enterString("\033[4 q"); - assertEquals(TerminalEmulator.CURSOR_STYLE_UNDERLINE, mTerminal.getCursorStyle()); + assertEquals(TerminalEmulator.TERMINAL_CURSOR_STYLE_UNDERLINE, mTerminal.getCursorStyle()); enterString("\033[2 q"); - assertEquals(TerminalEmulator.CURSOR_STYLE_BLOCK, mTerminal.getCursorStyle()); + assertEquals(TerminalEmulator.TERMINAL_CURSOR_STYLE_BLOCK, mTerminal.getCursorStyle()); } public void testPaste() { diff --git a/terminal-view/src/main/java/com/termux/view/TerminalRenderer.java b/terminal-view/src/main/java/com/termux/view/TerminalRenderer.java index 6189dd6712..f9c5b53aab 100644 --- a/terminal-view/src/main/java/com/termux/view/TerminalRenderer.java +++ b/terminal-view/src/main/java/com/termux/view/TerminalRenderer.java @@ -200,8 +200,8 @@ private void drawTextRun(Canvas canvas, char[] text, int[] palette, float y, int if (cursor != 0) { mTextPaint.setColor(cursor); float cursorHeight = mFontLineSpacingAndAscent - mFontAscent; - if (cursorStyle == TerminalEmulator.CURSOR_STYLE_UNDERLINE) cursorHeight /= 4.; - else if (cursorStyle == TerminalEmulator.CURSOR_STYLE_BAR) right -= ((right - left) * 3) / 4.; + if (cursorStyle == TerminalEmulator.TERMINAL_CURSOR_STYLE_UNDERLINE) cursorHeight /= 4.; + else if (cursorStyle == TerminalEmulator.TERMINAL_CURSOR_STYLE_BAR) right -= ((right - left) * 3) / 4.; canvas.drawRect(left, y - cursorHeight, right, y, mTextPaint); } diff --git a/termux-shared/src/main/java/com/termux/shared/settings/properties/TermuxPropertyConstants.java b/termux-shared/src/main/java/com/termux/shared/settings/properties/TermuxPropertyConstants.java index 3d1ae2823a..02fe3ed1a3 100644 --- a/termux-shared/src/main/java/com/termux/shared/settings/properties/TermuxPropertyConstants.java +++ b/termux-shared/src/main/java/com/termux/shared/settings/properties/TermuxPropertyConstants.java @@ -12,7 +12,7 @@ import java.util.Set; /* - * Version: v0.11.0 + * Version: v0.12.0 * * Changelog * @@ -53,6 +53,8 @@ * - 0.11.0 (2021-06-10) * - Add `*KEY_TERMINAL_TRANSCRIPT_ROWS*`. * + * - 0.12.0 (2021-06-10) + * - Add `*KEY_TERMINAL_CURSOR_STYLE*`. */ /** @@ -135,6 +137,28 @@ public final class TermuxPropertyConstants { + /** Defines the key for the terminal cursor style */ + public static final String KEY_TERMINAL_CURSOR_STYLE = "terminal-cursor-style"; // Default: "terminal-cursor-style" + + public static final String VALUE_TERMINAL_CURSOR_STYLE_BLOCK = "block"; + public static final String VALUE_TERMINAL_CURSOR_STYLE_UNDERLINE = "underline"; + public static final String VALUE_TERMINAL_CURSOR_STYLE_BAR = "bar"; + + public static final int IVALUE_TERMINAL_CURSOR_STYLE_BLOCK = TerminalEmulator.TERMINAL_CURSOR_STYLE_BLOCK; + public static final int IVALUE_TERMINAL_CURSOR_STYLE_UNDERLINE = TerminalEmulator.TERMINAL_CURSOR_STYLE_UNDERLINE; + public static final int IVALUE_TERMINAL_CURSOR_STYLE_BAR = TerminalEmulator.TERMINAL_CURSOR_STYLE_BAR; + public static final int DEFAULT_IVALUE_TERMINAL_CURSOR_STYLE = TerminalEmulator.DEFAULT_TERMINAL_CURSOR_STYLE; + + /** Defines the bidirectional map for terminal cursor styles and their internal values */ + public static final ImmutableBiMap MAP_TERMINAL_CURSOR_STYLE = + new ImmutableBiMap.Builder() + .put(VALUE_TERMINAL_CURSOR_STYLE_BLOCK, IVALUE_TERMINAL_CURSOR_STYLE_BLOCK) + .put(VALUE_TERMINAL_CURSOR_STYLE_UNDERLINE, IVALUE_TERMINAL_CURSOR_STYLE_UNDERLINE) + .put(VALUE_TERMINAL_CURSOR_STYLE_BAR, IVALUE_TERMINAL_CURSOR_STYLE_BAR) + .build(); + + + /** Defines the key for the terminal transcript rows */ public static final String KEY_TERMINAL_TRANSCRIPT_ROWS = "terminal-transcript-rows"; // Default: "terminal-transcript-rows" public static final int IVALUE_TERMINAL_TRANSCRIPT_ROWS_MIN = TerminalEmulator.TERMINAL_TRANSCRIPT_ROWS_MIN; @@ -271,6 +295,7 @@ public final class TermuxPropertyConstants { /* int */ KEY_BELL_BEHAVIOUR, KEY_TERMINAL_CURSOR_BLINK_RATE, + KEY_TERMINAL_CURSOR_STYLE, KEY_TERMINAL_TRANSCRIPT_ROWS, /* float */ diff --git a/termux-shared/src/main/java/com/termux/shared/settings/properties/TermuxSharedProperties.java b/termux-shared/src/main/java/com/termux/shared/settings/properties/TermuxSharedProperties.java index 16d0ce9c24..1ffc8f3bb0 100644 --- a/termux-shared/src/main/java/com/termux/shared/settings/properties/TermuxSharedProperties.java +++ b/termux-shared/src/main/java/com/termux/shared/settings/properties/TermuxSharedProperties.java @@ -217,6 +217,8 @@ public static Object getInternalTermuxPropertyValueFromValue(Context context, St return (int) getBellBehaviourInternalPropertyValueFromValue(value); case TermuxPropertyConstants.KEY_TERMINAL_CURSOR_BLINK_RATE: return (int) getTerminalCursorBlinkRateInternalPropertyValueFromValue(value); + case TermuxPropertyConstants.KEY_TERMINAL_CURSOR_STYLE: + return (int) getTerminalCursorStyleInternalPropertyValueFromValue(value); case TermuxPropertyConstants.KEY_TERMINAL_TRANSCRIPT_ROWS: return (int) getTerminalTranscriptRowsInternalPropertyValueFromValue(value); @@ -277,7 +279,7 @@ public static boolean getUseBlackUIInternalPropertyValueFromValue(Context contex /** * Returns the internal value after mapping it based on * {@code TermuxPropertyConstants#MAP_BELL_BEHAVIOUR} if the value is not {@code null} - * and is valid, otherwise returns {@code TermuxPropertyConstants#DEFAULT_IVALUE_BELL_BEHAVIOUR}. + * and is valid, otherwise returns {@link TermuxPropertyConstants#DEFAULT_IVALUE_BELL_BEHAVIOUR}. * * @param value The {@link String} value to convert. * @return Returns the internal value for value. @@ -288,14 +290,14 @@ public static int getBellBehaviourInternalPropertyValueFromValue(String value) { /** * Returns the int for the value if its not null and is between - * {@code TermuxPropertyConstants#IVALUE_TERMINAL_CURSOR_BLINK_RATE_MIN} and - * {@code TermuxPropertyConstants#IVALUE_TERMINAL_CURSOR_BLINK_RATE_MAX}, - * otherwise returns {@code TermuxPropertyConstants#DEFAULT_IVALUE_TERMINAL_CURSOR_BLINK_RATE}. + * {@link TermuxPropertyConstants#IVALUE_TERMINAL_CURSOR_BLINK_RATE_MIN} and + * {@link TermuxPropertyConstants#IVALUE_TERMINAL_CURSOR_BLINK_RATE_MAX}, + * otherwise returns {@link TermuxPropertyConstants#DEFAULT_IVALUE_TERMINAL_CURSOR_BLINK_RATE}. * * @param value The {@link String} value to convert. * @return Returns the internal value for value. */ - public static float getTerminalCursorBlinkRateInternalPropertyValueFromValue(String value) { + public static int getTerminalCursorBlinkRateInternalPropertyValueFromValue(String value) { return SharedProperties.getDefaultIfNotInRange(TermuxPropertyConstants.KEY_TERMINAL_CURSOR_BLINK_RATE, DataUtils.getIntFromString(value, TermuxPropertyConstants.DEFAULT_IVALUE_TERMINAL_CURSOR_BLINK_RATE), TermuxPropertyConstants.DEFAULT_IVALUE_TERMINAL_CURSOR_BLINK_RATE, @@ -304,16 +306,28 @@ public static float getTerminalCursorBlinkRateInternalPropertyValueFromValue(Str true, true, LOG_TAG); } + /** + * Returns the internal value after mapping it based on + * {@link TermuxPropertyConstants#MAP_TERMINAL_CURSOR_STYLE} if the value is not {@code null} + * and is valid, otherwise returns {@link TermuxPropertyConstants#DEFAULT_IVALUE_TERMINAL_CURSOR_STYLE}. + * + * @param value The {@link String} value to convert. + * @return Returns the internal value for value. + */ + public static int getTerminalCursorStyleInternalPropertyValueFromValue(String value) { + return (int) SharedProperties.getDefaultIfNotInMap(TermuxPropertyConstants.KEY_TERMINAL_CURSOR_STYLE, TermuxPropertyConstants.MAP_TERMINAL_CURSOR_STYLE, SharedProperties.toLowerCase(value), TermuxPropertyConstants.DEFAULT_IVALUE_TERMINAL_CURSOR_STYLE, true, LOG_TAG); + } + /** * Returns the int for the value if its not null and is between - * {@code TermuxPropertyConstants#IVALUE_TERMINAL_TRANSCRIPT_ROWS_MIN} and - * {@code TermuxPropertyConstants#IVALUE_TERMINAL_TRANSCRIPT_ROWS_MAX}, - * otherwise returns {@code TermuxPropertyConstants#DEFAULT_IVALUE_TERMINAL_TRANSCRIPT_ROWS}. + * {@link TermuxPropertyConstants#IVALUE_TERMINAL_TRANSCRIPT_ROWS_MIN} and + * {@link TermuxPropertyConstants#IVALUE_TERMINAL_TRANSCRIPT_ROWS_MAX}, + * otherwise returns {@link TermuxPropertyConstants#DEFAULT_IVALUE_TERMINAL_TRANSCRIPT_ROWS}. * * @param value The {@link String} value to convert. * @return Returns the internal value for value. */ - public static float getTerminalTranscriptRowsInternalPropertyValueFromValue(String value) { + public static int getTerminalTranscriptRowsInternalPropertyValueFromValue(String value) { return SharedProperties.getDefaultIfNotInRange(TermuxPropertyConstants.KEY_TERMINAL_TRANSCRIPT_ROWS, DataUtils.getIntFromString(value, TermuxPropertyConstants.DEFAULT_IVALUE_TERMINAL_TRANSCRIPT_ROWS), TermuxPropertyConstants.DEFAULT_IVALUE_TERMINAL_TRANSCRIPT_ROWS, @@ -324,9 +338,9 @@ public static float getTerminalTranscriptRowsInternalPropertyValueFromValue(Stri /** * Returns the int for the value if its not null and is between - * {@code TermuxPropertyConstants#IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MIN} and - * {@code TermuxPropertyConstants#IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MAX}, - * otherwise returns {@code TermuxPropertyConstants#DEFAULT_IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR}. + * {@link TermuxPropertyConstants#IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MIN} and + * {@link TermuxPropertyConstants#IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MAX}, + * otherwise returns {@link TermuxPropertyConstants#DEFAULT_IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR}. * * @param value The {@link String} value to convert. * @return Returns the internal value for value. @@ -478,6 +492,10 @@ public int getTerminalCursorBlinkRate() { return (int) getInternalPropertyValue(TermuxPropertyConstants.KEY_TERMINAL_CURSOR_BLINK_RATE, true); } + public int getTerminalCursorStyle() { + return (int) getInternalPropertyValue(TermuxPropertyConstants.KEY_TERMINAL_CURSOR_STYLE, true); + } + public int getTerminalTranscriptRows() { return (int) getInternalPropertyValue(TermuxPropertyConstants.KEY_TERMINAL_TRANSCRIPT_ROWS, true); } diff --git a/termux-shared/src/main/java/com/termux/shared/terminal/TermuxTerminalSessionClientBase.java b/termux-shared/src/main/java/com/termux/shared/terminal/TermuxTerminalSessionClientBase.java index 6261972b8a..6df69a723f 100644 --- a/termux-shared/src/main/java/com/termux/shared/terminal/TermuxTerminalSessionClientBase.java +++ b/termux-shared/src/main/java/com/termux/shared/terminal/TermuxTerminalSessionClientBase.java @@ -39,6 +39,13 @@ public void onTerminalCursorStateChange(boolean state) { + @Override + public Integer getTerminalCursorStyle() { + return null; + } + + + @Override public void logError(String tag, String message) { Logger.logError(tag, message);