blob: b6e981eee577ccac742e0c740e20118c6e6136c9 [file] [log] [blame]
Jeff Sharkeyef946f32013-02-19 17:29:18 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.terminal;
18
Jeff Sharkey410e0da2013-02-19 23:10:46 -080019import android.graphics.Color;
Kenny Root8c040332013-02-18 23:31:29 -080020
Jeff Sharkey410e0da2013-02-19 23:10:46 -080021/**
22 * Single terminal session backed by a pseudo terminal on the local device.
23 */
24public class Terminal {
Jeff Sharkey00b00812013-04-14 16:16:49 -070025 public static final String TAG = "Terminal";
26
27 public final int key;
Jeff Sharkey479bd642013-02-21 17:45:16 -080028
Jeff Sharkeyde15e792013-02-23 15:42:10 -080029 private static int sNumber = 0;
30
Jeff Sharkeyef946f32013-02-19 17:29:18 -080031 static {
32 System.loadLibrary("jni_terminal");
33 }
34
Jeff Sharkey9cae0a92013-02-20 21:41:29 -080035 /**
36 * Represents a run of one or more {@code VTermScreenCell} which all have
37 * the same formatting.
38 */
39 public static class CellRun {
40 char[] data;
41 int dataSize;
42 int colSize;
Jeff Sharkey410e0da2013-02-19 23:10:46 -080043
44 boolean bold;
45 int underline;
46 boolean blink;
47 boolean reverse;
48 boolean strike;
49 int font;
50
Jeff Sharkey00b00812013-04-14 16:16:49 -070051 int fg = Color.CYAN;
52 int bg = Color.DKGRAY;
Jeff Sharkey410e0da2013-02-19 23:10:46 -080053 }
54
Jeff Sharkey00b00812013-04-14 16:16:49 -070055 // NOTE: clients must not call back into terminal while handling a callback,
56 // since native mutex isn't reentrant.
Jeff Sharkey410e0da2013-02-19 23:10:46 -080057 public interface TerminalClient {
Jeff Sharkey00b00812013-04-14 16:16:49 -070058 public void onDamage(int startRow, int endRow, int startCol, int endCol);
59 public void onMoveRect(int destStartRow, int destEndRow, int destStartCol, int destEndCol,
Jeff Sharkey6a142b62013-02-23 12:27:57 -080060 int srcStartRow, int srcEndRow, int srcStartCol, int srcEndCol);
Tom Marshall5b68e8a2014-12-31 10:51:01 -080061 public void onMoveCursor(int posRow, int posCol, int oldPosRow, int oldPosCol, int visible);
Jeff Sharkey00b00812013-04-14 16:16:49 -070062 public void onBell();
Jeff Sharkey410e0da2013-02-19 23:10:46 -080063 }
64
Colin Cross96403af2014-04-08 15:15:34 -070065 private final long mNativePtr;
Jeff Sharkey479bd642013-02-21 17:45:16 -080066 private final Thread mThread;
67
Jeff Sharkeyde15e792013-02-23 15:42:10 -080068 private String mTitle;
69
Jeff Sharkey410e0da2013-02-19 23:10:46 -080070 private TerminalClient mClient;
71
Tom Marshall5b68e8a2014-12-31 10:51:01 -080072 private boolean mCursorVisible;
73 private int mCursorRow;
74 private int mCursorCol;
75
Jeff Sharkey410e0da2013-02-19 23:10:46 -080076 private final TerminalCallbacks mCallbacks = new TerminalCallbacks() {
77 @Override
78 public int damage(int startRow, int endRow, int startCol, int endCol) {
79 if (mClient != null) {
Jeff Sharkey00b00812013-04-14 16:16:49 -070080 mClient.onDamage(startRow, endRow, startCol, endCol);
Jeff Sharkey410e0da2013-02-19 23:10:46 -080081 }
82 return 1;
83 }
84
85 @Override
Jeff Sharkeya76e3382013-02-21 19:09:55 -080086 public int moveRect(int destStartRow, int destEndRow, int destStartCol, int destEndCol,
87 int srcStartRow, int srcEndRow, int srcStartCol, int srcEndCol) {
Jeff Sharkeya76e3382013-02-21 19:09:55 -080088 if (mClient != null) {
Jeff Sharkey00b00812013-04-14 16:16:49 -070089 mClient.onMoveRect(destStartRow, destEndRow, destStartCol, destEndCol, srcStartRow,
Jeff Sharkey6a142b62013-02-23 12:27:57 -080090 srcEndRow, srcStartCol, srcEndCol);
Jeff Sharkeya76e3382013-02-21 19:09:55 -080091 }
92 return 1;
93 }
94
95 @Override
Tom Marshall5b68e8a2014-12-31 10:51:01 -080096 public int moveCursor(int posRow, int posCol, int oldPosRow, int oldPosCol, int visible) {
97 mCursorVisible = (visible != 0);
98 mCursorRow = posRow;
99 mCursorCol = posCol;
100 if (mClient != null) {
101 mClient.onMoveCursor(posRow, posCol, oldPosRow, oldPosCol, visible);
102 }
103 return 1;
104 }
105
106 @Override
Jeff Sharkey410e0da2013-02-19 23:10:46 -0800107 public int bell() {
108 if (mClient != null) {
Jeff Sharkey00b00812013-04-14 16:16:49 -0700109 mClient.onBell();
Jeff Sharkey410e0da2013-02-19 23:10:46 -0800110 }
111 return 1;
112 }
113 };
Jeff Sharkeyef946f32013-02-19 17:29:18 -0800114
115 public Terminal() {
Tom Marshalld3090cb2014-12-31 10:42:02 -0800116 mNativePtr = nativeInit(mCallbacks);
Jeff Sharkey00b00812013-04-14 16:16:49 -0700117 key = sNumber++;
118 mTitle = TAG + " " + key;
119 mThread = new Thread(mTitle) {
Jeff Sharkeyd439bac2013-02-20 22:17:51 -0800120 @Override
121 public void run() {
Jeff Sharkey479bd642013-02-21 17:45:16 -0800122 nativeRun(mNativePtr);
Jeff Sharkeyd439bac2013-02-20 22:17:51 -0800123 }
Jeff Sharkey479bd642013-02-21 17:45:16 -0800124 };
125 }
126
127 /**
128 * Start thread which internally forks and manages the pseudo terminal.
129 */
130 public void start() {
131 mThread.start();
Jeff Sharkeyef946f32013-02-19 17:29:18 -0800132 }
133
Jeff Sharkey00b00812013-04-14 16:16:49 -0700134 public void destroy() {
135 if (nativeDestroy(mNativePtr) != 0) {
136 throw new IllegalStateException("destroy failed");
Jeff Sharkeyde15e792013-02-23 15:42:10 -0800137 }
138 }
139
Jeff Sharkey410e0da2013-02-19 23:10:46 -0800140 public void setClient(TerminalClient client) {
141 mClient = client;
142 }
143
Jeff Sharkey00b00812013-04-14 16:16:49 -0700144 public void resize(int rows, int cols, int scrollRows) {
145 if (nativeResize(mNativePtr, rows, cols, scrollRows) != 0) {
Jeff Sharkey410e0da2013-02-19 23:10:46 -0800146 throw new IllegalStateException("resize failed");
147 }
Jeff Sharkey5d4b3952013-02-19 20:34:29 -0800148 }
149
Jeff Sharkeyef946f32013-02-19 17:29:18 -0800150 public int getRows() {
151 return nativeGetRows(mNativePtr);
152 }
153
Jeff Sharkey410e0da2013-02-19 23:10:46 -0800154 public int getCols() {
155 return nativeGetCols(mNativePtr);
156 }
157
Jeff Sharkey00b00812013-04-14 16:16:49 -0700158 public int getScrollRows() {
159 return nativeGetScrollRows(mNativePtr);
160 }
161
Jeff Sharkey9cae0a92013-02-20 21:41:29 -0800162 public void getCellRun(int row, int col, CellRun run) {
163 if (nativeGetCellRun(mNativePtr, row, col, run) != 0) {
Jeff Sharkey410e0da2013-02-19 23:10:46 -0800164 throw new IllegalStateException("getCell failed");
165 }
166 }
167
Tom Marshall5b68e8a2014-12-31 10:51:01 -0800168 public boolean getCursorVisible() {
169 return mCursorVisible;
170 }
171
172 public int getCursorRow() {
173 return mCursorRow;
174 }
175
176 public int getCursorCol() {
177 return mCursorCol;
178 }
179
Jeff Sharkeyde15e792013-02-23 15:42:10 -0800180 public String getTitle() {
181 // TODO: hook up to title passed through termprop
182 return mTitle;
183 }
184
Michael Wright007efde2013-02-25 11:22:30 -0800185 public boolean dispatchKey(int modifiers, int key) {
Michael Wrightc8be1792013-02-26 11:09:12 -0800186 return nativeDispatchKey(mNativePtr, modifiers, key);
Michael Wright007efde2013-02-25 11:22:30 -0800187 }
188
189 public boolean dispatchCharacter(int modifiers, int character) {
Michael Wrightc8be1792013-02-26 11:09:12 -0800190 return nativeDispatchCharacter(mNativePtr, modifiers, character);
Michael Wright007efde2013-02-25 11:22:30 -0800191 }
192
Tom Marshalld3090cb2014-12-31 10:42:02 -0800193 private static native long nativeInit(TerminalCallbacks callbacks);
Colin Cross96403af2014-04-08 15:15:34 -0700194 private static native int nativeDestroy(long ptr);
Jeff Sharkey479bd642013-02-21 17:45:16 -0800195
Colin Cross96403af2014-04-08 15:15:34 -0700196 private static native int nativeRun(long ptr);
197 private static native int nativeResize(long ptr, int rows, int cols, int scrollRows);
198 private static native int nativeGetCellRun(long ptr, int row, int col, CellRun run);
199 private static native int nativeGetRows(long ptr);
200 private static native int nativeGetCols(long ptr);
201 private static native int nativeGetScrollRows(long ptr);
Michael Wright007efde2013-02-25 11:22:30 -0800202
Colin Cross96403af2014-04-08 15:15:34 -0700203 private static native boolean nativeDispatchKey(long ptr, int modifiers, int key);
204 private static native boolean nativeDispatchCharacter(long ptr, int modifiers, int character);
Jeff Sharkeyef946f32013-02-19 17:29:18 -0800205}