summaryrefslogtreecommitdiffstats
path: root/src/com/android/terminal/TerminalView.java
blob: 0246167244438caf19158b287f979f4dfb17219c (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.terminal;

import static com.android.terminal.Terminal.TAG;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Typeface;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.BaseInputConnection;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.android.terminal.Terminal.CellRun;
import com.android.terminal.Terminal.TerminalClient;

/**
 * Rendered contents of a {@link Terminal} session.
 */
public class TerminalView extends ListView {
    private static final boolean LOGD = true;

    private static final boolean SCROLL_ON_DAMAGE = false;
    private static final boolean SCROLL_ON_INPUT = true;

    private Terminal mTerm;

    private boolean mScrolled;

    private int mRows;
    private int mCols;
    private int mScrollRows;

    private final TerminalMetrics mMetrics = new TerminalMetrics();
    private final TerminalKeys mTermKeys = new TerminalKeys();

    /**
     * Metrics shared between all {@link TerminalLineView} children. Locking
     * provided by main thread.
     */
    static class TerminalMetrics {
        private static final int MAX_RUN_LENGTH = 128;

        final Paint bgPaint = new Paint();
        final Paint textPaint = new Paint();
        final Paint cursorPaint = new Paint();

        /** Run of cells used when drawing */
        final CellRun run;
        /** Screen coordinates to draw chars into */
        final float[] pos;

        int charTop;
        int charWidth;
        int charHeight;

        public TerminalMetrics() {
            run = new Terminal.CellRun();
            run.data = new char[MAX_RUN_LENGTH];

            // Positions of each possible cell
            // TODO: make sure this works with surrogate pairs
            pos = new float[MAX_RUN_LENGTH * 2];
            textPaint.setTypeface(Typeface.MONOSPACE);
            textPaint.setAntiAlias(true);
        }

        public void setTextSize(float textSize) {
            textPaint.setTextSize(textSize);

            // Read metrics to get exact pixel dimensions
            final FontMetrics fm = textPaint.getFontMetrics();
            charTop = (int) Math.ceil(fm.top);

            final float[] widths = new float[1];
            textPaint.getTextWidths("X", widths);
            charWidth = (int) Math.ceil(widths[0]);
            charHeight = (int) Math.ceil(fm.descent - fm.top);

            // Update drawing positions
            for (int i = 0; i < MAX_RUN_LENGTH; i++) {
                pos[i * 2] = i * charWidth;
                pos[(i * 2) + 1] = -charTop;
            }
        }
    }

    private void toggleFullscreenMode() {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext());
        boolean oldVal = sp.getBoolean(TerminalSettingsActivity.KEY_FULLSCREEN_MODE, false);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(TerminalSettingsActivity.KEY_FULLSCREEN_MODE, !oldVal);
        editor.commit();
        TerminalActivity activity = (TerminalActivity) getContext();
        activity.updatePreferences();
    }

    private final AdapterView.OnItemClickListener mClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
            // Clicking on top half of view toggles fullscreen mode
            if (pos - mScrollRows < mRows / 2) {
                toggleFullscreenMode();
                return;
            }
            // Clicking on bottom half of view shows soft keyboard
            if (parent.requestFocus()) {
                InputMethodManager imm = (InputMethodManager) parent.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(parent, InputMethodManager.SHOW_IMPLICIT);
            }
        }
    };

    private final Runnable mDamageRunnable = new Runnable() {
        @Override
        public void run() {
            invalidateViews();
            if (SCROLL_ON_DAMAGE) {
                scrollToBottom(true);
            }
        }
    };

    private final float PT_PER_INCH = 72.0f;
    private float ptToDp(float pt) {
        return (pt / PT_PER_INCH) * (float)DisplayMetrics.DENSITY_DEFAULT;
    }

    public TerminalView(Context context) {
        this(context, null);
    }

    public TerminalView(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.listViewStyle);
    }

    public TerminalView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        setBackground(null);
        setDivider(null);

        setFocusable(true);
        setFocusableInTouchMode(true);

        setAdapter(mAdapter);
        setOnKeyListener(mKeyListener);

        setOnItemClickListener(mClickListener);
    }

    private final BaseAdapter mAdapter = new BaseAdapter() {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final TerminalLineView view;
            if (convertView != null) {
                view = (TerminalLineView) convertView;
            } else {
                view = new TerminalLineView(parent.getContext(), mTerm, mMetrics);
            }

            view.pos = position;
            view.row = posToRow(position);
            view.cols = mCols;
            return view;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public int getCount() {
            if (mTerm != null) {
                return mRows + mScrollRows;
            } else {
                return 0;
            }
        }
    };

    private TerminalClient mClient = new TerminalClient() {
        @Override
        public void onDamage(final int startRow, final int endRow, int startCol, int endCol) {
            post(mDamageRunnable);
        }

        @Override
        public void onMoveRect(int destStartRow, int destEndRow, int destStartCol, int destEndCol,
                int srcStartRow, int srcEndRow, int srcStartCol, int srcEndCol) {
            post(mDamageRunnable);
        }

        @Override
        public void onMoveCursor(int posRow, int posCol, int oldPosRow, int oldPosCol, int visible) {
            post(mDamageRunnable);
        }

        @Override
        public void onBell() {
            Log.i(TAG, "DING!");
        }
    };

    private int rowToPos(int row) {
        return row + mScrollRows;
    }

    private int posToRow(int pos) {
        return pos - mScrollRows;
    }

    private View.OnKeyListener mKeyListener = new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            final boolean res = mTermKeys.onKey(v, keyCode, event);
            if (res && SCROLL_ON_INPUT) {
                scrollToBottom(true);
            }
            return res;
        }
    };

    @Override
    public void onRestoreInstanceState(Parcelable state) {
        super.onRestoreInstanceState(state);
        mScrolled = true;
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (!mScrolled) {
            scrollToBottom(false);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        final int rows = h / mMetrics.charHeight;
        final int cols = w / mMetrics.charWidth;
        final int scrollRows = mScrollRows;

        final boolean sizeChanged = (rows != mRows || cols != mCols || scrollRows != mScrollRows);
        if (mTerm != null && sizeChanged) {
            mTerm.resize(rows, cols, scrollRows);

            mRows = rows;
            mCols = cols;
            mScrollRows = scrollRows;

            mAdapter.notifyDataSetChanged();
        }
    }

    public void scrollToBottom(boolean animate) {
        final int dur = animate ? 250 : 0;
        smoothScrollToPositionFromTop(getCount(), 0, dur);
        mScrolled = true;
    }

    public void setTerminal(Terminal term) {
        final Terminal orig = mTerm;
        if (orig != null) {
            orig.setClient(null);
        }
        mTerm = term;
        mScrolled = false;
        if (term != null) {
            term.setClient(mClient);
            mTermKeys.setTerminal(term);

            updatePreferences();

            // Populate any current settings
            mRows = mTerm.getRows();
            mCols = mTerm.getCols();
            mScrollRows = mTerm.getScrollRows();
            mAdapter.notifyDataSetChanged();
        }
    }

    public Terminal getTerminal() {
        return mTerm;
    }

    public void setTextSize(float textSize) {
        mMetrics.setTextSize(textSize);

        // Layout will kick off terminal resize when needed
        requestLayout();
    }

    @Override
    public boolean onCheckIsTextEditor() {
        return true;
    }

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        outAttrs.imeOptions |=
            EditorInfo.IME_FLAG_NO_EXTRACT_UI |
            EditorInfo.IME_FLAG_NO_ENTER_ACTION |
            EditorInfo.IME_ACTION_NONE;
        outAttrs.inputType = EditorInfo.TYPE_NULL;
        return new BaseInputConnection(this, false) {
            @Override
            public boolean deleteSurroundingText (int leftLength, int rightLength) {
                KeyEvent k;
                if (rightLength == 0 && leftLength == 0) {
                    k = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL);
                    return this.sendKeyEvent(k);
                }
                for (int i = 0; i < leftLength; i++) {
                    k = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL);
                    this.sendKeyEvent(k);
                }
                for (int i = 0; i < rightLength; i++) {
                    k = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_FORWARD_DEL);
                    this.sendKeyEvent(k);
                }
                return true;
            }
        };
    }

    public void updatePreferences() {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext());
        String val;

        val = sp.getString(TerminalSettingsActivity.KEY_FONT_SIZE, "12");
        mMetrics.setTextSize(ptToDp(Float.parseFloat(val)));

        val = sp.getString(TerminalSettingsActivity.KEY_TEXT_COLORS, "white/black");
        int fg = 0xfafafa;
        int bg = 0x212121;
        int idx = val.indexOf('/');
        if (idx != -1) {
            try {
                fg = Color.parseColor(val.substring(0, idx));
                bg = Color.parseColor(val.substring(idx+1));
            }
            catch (IllegalArgumentException e) {
                // Ignore
            }
        }
        mTerm.setColors(fg, bg);
        mMetrics.run.fg = fg;
        mMetrics.run.bg = bg;
        mMetrics.cursorPaint.setColor(fg);
    }
}