Terminal.java revision 6a142b6d4831c3841b6be1705fc97c9b75a7c9d1
1/*
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
19import android.graphics.Color;
20
21/**
22 * Single terminal session backed by a pseudo terminal on the local device.
23 */
24public class Terminal {
25    private static final String TAG = "Terminal";
26
27    static {
28        System.loadLibrary("jni_terminal");
29    }
30
31    /**
32     * Represents a run of one or more {@code VTermScreenCell} which all have
33     * the same formatting.
34     */
35    public static class CellRun {
36        char[] data;
37        int dataSize;
38        int colSize;
39
40        boolean bold;
41        int underline;
42        boolean blink;
43        boolean reverse;
44        boolean strike;
45        int font;
46
47        int fgColor = Color.RED;
48        int bgColor = Color.BLUE;
49    }
50
51    public interface TerminalClient {
52        public void damage(int startRow, int endRow, int startCol, int endCol);
53        public void moveRect(int destStartRow, int destEndRow, int destStartCol, int destEndCol,
54                int srcStartRow, int srcEndRow, int srcStartCol, int srcEndCol);
55        public void bell();
56    }
57
58    private final int mNativePtr;
59    private final Thread mThread;
60
61    private TerminalClient mClient;
62
63    private final TerminalCallbacks mCallbacks = new TerminalCallbacks() {
64        @Override
65        public int damage(int startRow, int endRow, int startCol, int endCol) {
66            if (mClient != null) {
67                mClient.damage(startRow, endRow, startCol, endCol);
68            }
69            return 1;
70        }
71
72        @Override
73        public int moveRect(int destStartRow, int destEndRow, int destStartCol, int destEndCol,
74                int srcStartRow, int srcEndRow, int srcStartCol, int srcEndCol) {
75            if (mClient != null) {
76                mClient.moveRect(destStartRow, destEndRow, destStartCol, destEndCol, srcStartRow,
77                        srcEndRow, srcStartCol, srcEndCol);
78            }
79            return 1;
80        }
81
82        @Override
83        public int bell() {
84            if (mClient != null) {
85                mClient.bell();
86            }
87            return 1;
88        }
89    };
90
91    public Terminal() {
92        mNativePtr = nativeInit(mCallbacks, 25, 80);
93        mThread = new Thread(TAG) {
94            @Override
95            public void run() {
96                nativeRun(mNativePtr);
97            }
98        };
99    }
100
101    /**
102     * Start thread which internally forks and manages the pseudo terminal.
103     */
104    public void start() {
105        mThread.start();
106    }
107
108    public void setClient(TerminalClient client) {
109        mClient = client;
110    }
111
112    public void flushDamage() {
113        if (nativeFlushDamage(mNativePtr) != 0) {
114            throw new IllegalStateException("flushDamage failed");
115        }
116    }
117
118    public void resize(int rows, int cols) {
119        if (nativeResize(mNativePtr, rows, cols) != 0) {
120            throw new IllegalStateException("resize failed");
121        }
122    }
123
124    public int getRows() {
125        return nativeGetRows(mNativePtr);
126    }
127
128    public int getCols() {
129        return nativeGetCols(mNativePtr);
130    }
131
132    public void getCellRun(int row, int col, CellRun run) {
133        if (nativeGetCellRun(mNativePtr, row, col, run) != 0) {
134            throw new IllegalStateException("getCell failed");
135        }
136    }
137
138    private static native int nativeInit(TerminalCallbacks callbacks, int rows, int cols);
139    private static native int nativeRun(int ptr);
140
141    private static native int nativeFlushDamage(int ptr);
142    private static native int nativeResize(int ptr, int rows, int cols);
143    private static native int nativeGetCellRun(int ptr, int row, int col, CellRun run);
144    private static native int nativeGetRows(int ptr);
145    private static native int nativeGetCols(int ptr);
146}
147