aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/theb/ssh/JCTerminalView.java
blob: 47d12aa95c9004e7f0006f376ee030a23f072a1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 * Copyright (C) 2007 Kenny Root (kenny at the-b.org)
 * 
 * This file is part of Connectbot.
 *
 *  Connectbot is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Connectbot is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Connectbot.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.theb.ssh;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelXorXfermode;
import android.graphics.Typeface;
import android.graphics.Paint.FontMetricsInt;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import com.jcraft.jcterm.Emulator;
import com.jcraft.jcterm.EmulatorVT100;
import com.jcraft.jcterm.Term;

public class JCTerminalView extends View implements Term, Terminal {
	private final Paint mPaint;
	private Bitmap mBitmap;
	private Canvas mCanvas;

	private final Paint mCursorPaint;
	
	private Emulator emulator = null;
	
	private boolean mBold = false;
	private boolean mUnderline = false;
	private boolean mReverse = false;
	
	private int mDefaultForeground = Color.WHITE;
	private int mDefaultBackground = Color.BLACK;
	private int mForeground = Color.WHITE;
	private int mBackground = Color.BLACK;
	
	private boolean mAntialias = true;
	
	private int mTermWidth = 80;
	private int mTermHeight = 24;
	
	private int mCharHeight;
	private int mCharWidth;
	private int mDescent;
	
	
	// Cursor location
	private int x = 0;
	private int y = 0;
	
	private final Object[] mColors = {Color.BLACK, Color.RED, Color.GREEN, Color.YELLOW,
			Color.BLUE, Color.MAGENTA, Color.CYAN, Color.WHITE};
	
	public JCTerminalView(Context c) {
		super(c);
		mPaint = new Paint();
		mPaint.setAntiAlias(mAntialias);
		mPaint.setColor(mDefaultForeground);
		
		mCursorPaint = new Paint();
		mCursorPaint.setAntiAlias(mAntialias);
		mCursorPaint.setColor(mDefaultForeground);
		mCursorPaint.setXfermode(new PixelXorXfermode(mDefaultBackground));
		
		setFont(Typeface.MONOSPACE);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (mBitmap != null) {
			canvas.drawBitmap(mBitmap, 0, 0, null);
			
			if (mCharHeight > 0 && y > mCharHeight) {
				// Invert pixels for cursor position.
				canvas.drawRect(x, y - mCharHeight, x + mCharWidth, y, mCursorPaint);
			}
		}
	}
 
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		Log.d("SSH/TerminalView", "onSizeChanged called");
		Bitmap newBitmap = Bitmap.createBitmap(w, h, false);
		Canvas newCanvas = new Canvas();
		
		newCanvas.setDevice(newBitmap);
		
		if (mBitmap != null)
			newCanvas.drawBitmap(mBitmap, 0, 0, mPaint);
		
		mBitmap = newBitmap;
		mCanvas = newCanvas;
		
		setSize(w, h);
	}
	
	private void setSize(int w, int h) {
		int column = w / getCharWidth();
		int row = h / getCharHeight();
		
		mTermWidth = column;
		mTermHeight = row;
		
		if (emulator != null)
			emulator.reset();
		
		clear_area(0, 0, w, h);
		
		// TODO: finish this method
	}

	private void setFont(Typeface typeface) {
		mPaint.setTypeface(typeface);
		mPaint.setTextSize(8);
		FontMetricsInt fm = mPaint.getFontMetricsInt();
		mDescent = fm.descent;
		
		float[] widths = new float[1];
		mPaint.getTextWidths("X", widths);
		mCharWidth = (int)widths[0];
		
		// Is this right?
		mCharHeight = Math.abs(fm.top) + Math.abs(fm.descent);
		Log.d("SSH", "character height is " + mCharHeight);
		// mCharHeight += mLineSpace * 2;
		// mDescent += mLineSpace;
	}

	public void beep() {
		// TODO Auto-generated method stub
		
	}

	public void clear() {
		mPaint.setColor(getBackgroundColor());
		mCanvas.drawRect(0, 0, mCanvas.getBitmapWidth(),
				mCanvas.getBitmapHeight(), mPaint);
		mPaint.setColor(getForegroundColor());
	}

	private int getBackgroundColor() {
		if (mReverse)
			return mForeground;
		return mBackground;
	}
	
	private int getForegroundColor() {
		if (mReverse)
			return mBackground;
		return mForeground;
	}

	public void clear_area(int x1, int y1, int x2, int y2) {
		mPaint.setColor(getBackgroundColor());
		if (mCanvas != null)
			mCanvas.drawRect(x1, y1, x2, y2, mPaint);
		mPaint.setColor(getForegroundColor());
	}

	public void drawBytes(byte[] buf, int s, int len, int x, int y) {
		String chars = null;
		try {
			chars = new String(buf, "ASCII");
			drawString(chars.substring(s, s+len), x, y);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			Log.e("SSH", "Can't convert bytes to ASCII");
		}
	}

	public void drawString(String str, int x, int y) {
		mPaint.setFakeBoldText(mBold);
		mPaint.setUnderlineText(mUnderline);
		mCanvas.drawText(str, x, y - mDescent, mPaint);
	}

	public void draw_cursor() {
		postInvalidate();
	}

	public int getCharHeight() {
		return mCharHeight;
	}

	public int getCharWidth() {
		return mCharWidth;
	}

	public Object getColor(int index) {
		if (mColors == null || index < 0 || mColors.length <= index)
			return null;
		return mColors[index];
	}

	public int getColumnCount() {
		return mTermWidth;
	}

	public int getRowCount() {
		return mTermHeight;
	}

	public int getTermHeight() {
		return mTermHeight * mCharHeight;
	}

	public int getTermWidth() {
		return mTermWidth * mCharWidth;
	}

	public void redraw(int x, int y, int width, int height) {
		//invalidate(x, y, x+width, y+height);
		postInvalidate();
	}

	public void resetAllAttributes() {
		mBold = false;
		mUnderline = false;
		mReverse = false;
		
		mBackground = mDefaultBackground;
		mForeground = mDefaultForeground;
		
		if (mPaint != null)
			mPaint.setColor(mForeground);
	}

	public void scroll_area(int x, int y, int w, int h, int dx, int dy) {
		// TODO: make scrolling more efficient (memory-wise)
		mCanvas.drawBitmap(Bitmap.createBitmap(mBitmap, x, y, w, h), x+dx, y+dy, null);
	}

	private int toColor(Object o) {
		if (o instanceof Integer) {
			return ((Integer)o).intValue();
		}
		
		if (o instanceof String) {
			return Color.parseColor((String)o);
		}
		
		return Color.WHITE;
	}
	
	public void setBackGround(Object background) {
		mBackground = toColor(background);
	}

	public void setBold() {
		mBold = true;
	}

	public void setCursor(int x, int y) {
		// Make sure we don't go outside the bounds of the window.
		this.x = Math.max(
				Math.min(x, getWidth() - mCharWidth),
				0);
		this.y = Math.max(
				Math.min(y, getHeight()),
				mCharHeight);
	}

	public void setDefaultBackGround(Object background) {
		mDefaultBackground = toColor(background);
	}

	public void setDefaultForeGround(Object foreground) {
		mDefaultForeground = toColor(foreground);
	}

	public void setForeGround(Object foreground) {
		mForeground = toColor(foreground);
	}

	public void setReverse() {
		mReverse = true;
		if (mPaint != null)
			mPaint.setColor(getForegroundColor());
	}

	public void setUnderline() {
		mUnderline = true;
	}

	public void start(InputStream in, OutputStream out) {
		emulator = new EmulatorVT100(this, in);
		emulator.reset();
		emulator.start();
		
		clear();
	}

	public byte[] getKeyCode(int keyCode, int meta) {
		if (keyCode == KeyEvent.KEYCODE_NEWLINE)
			return emulator.getCodeENTER();
		else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT)
			return emulator.getCodeLEFT();
		else if (keyCode == KeyEvent.KEYCODE_DPAD_UP)
			return emulator.getCodeUP();
		else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN)
			return emulator.getCodeDOWN();
		else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)
			return emulator.getCodeRIGHT();
		else
			return null;
	}
}