com_android_terminal_Terminal.cpp revision ef946f3ae80556ab221265c0bf1c560683ea27f6
1ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey/*
2ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey * Copyright (C) 2013 The Android Open Source Project
3ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey *
4ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License");
5ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey * you may not use this file except in compliance with the License.
6ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey * You may obtain a copy of the License at
7ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey *
8ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey *      http://www.apache.org/licenses/LICENSE-2.0
9ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey *
10ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey * Unless required by applicable law or agreed to in writing, software
11ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS,
12ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey * See the License for the specific language governing permissions and
14ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey * limitations under the License.
15ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey */
16ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
17ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey#define LOG_TAG "Terminal"
18ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
19ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey#include <utils/Log.h>
20ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey#include "jni.h"
21ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey#include "JNIHelp.h"
22ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
23ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey#include <vterm.h>
24ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
25ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey#include <string.h>
26ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
27ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkeynamespace android {
28ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
29ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkeyclass Terminal;
30ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
31ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey/*
32ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey * VTerm event handlers
33ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey */
34ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
35ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkeystatic int term_damage(VTermRect rect, void *user) {
36ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    Terminal* term = reinterpret_cast<Terminal*>(user);
37ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    ALOGW("term_damage");
38ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    return 1;
39ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey}
40ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
41ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkeystatic int term_prescroll(VTermRect rect, void *user) {
42ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    Terminal* term = reinterpret_cast<Terminal*>(user);
43ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    ALOGW("term_prescroll");
44ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    return 1;
45ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey}
46ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
47ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkeystatic int term_moverect(VTermRect dest, VTermRect src, void *user) {
48ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    Terminal* term = reinterpret_cast<Terminal*>(user);
49ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    ALOGW("term_moverect");
50ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    return 1;
51ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey}
52ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
53ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkeystatic int term_movecursor(VTermPos pos, VTermPos oldpos, int visible, void *user) {
54ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    Terminal* term = reinterpret_cast<Terminal*>(user);
55ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    ALOGW("term_movecursor");
56ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    return 1;
57ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey}
58ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
59ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkeystatic int term_settermprop(VTermProp prop, VTermValue *val, void *user) {
60ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    Terminal* term = reinterpret_cast<Terminal*>(user);
61ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    ALOGW("term_settermprop");
62ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    return 1;
63ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey}
64ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
65ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkeystatic int term_setmousefunc(VTermMouseFunc func, void *data, void *user) {
66ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    Terminal* term = reinterpret_cast<Terminal*>(user);
67ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    ALOGW("term_setmousefunc");
68ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    return 1;
69ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey}
70ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
71ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkeystatic int term_bell(void *user) {
72ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    Terminal* term = reinterpret_cast<Terminal*>(user);
73ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    ALOGW("term_bell");
74ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    return 1;
75ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey}
76ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
77ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkeystatic int term_resize(int rows, int cols, void *user) {
78ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    Terminal* term = reinterpret_cast<Terminal*>(user);
79ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    ALOGW("term_resize");
80ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    return 1;
81ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey}
82ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
83ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkeystatic VTermScreenCallbacks cb = {
84ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    .damage = term_damage,
85ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    .prescroll = term_prescroll,
86ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    .moverect = term_moverect,
87ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    .movecursor = term_movecursor,
88ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    .settermprop = term_settermprop,
89ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    .setmousefunc = term_setmousefunc,
90ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    .bell = term_bell,
91ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    .resize = term_resize,
92ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey};
93ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
94ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey/*
95ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey * Terminal session
96ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey */
97ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
98ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkeyclass Terminal {
99ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkeypublic:
100ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    Terminal(int rows, int cols);
101ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    ~Terminal();
102ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
103ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    int getRows();
104ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
105ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkeyprivate:
106ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    VTerm *mVt;
107ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    VTermScreen *mVts;
108ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
109ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    int mRows;
110ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    int mCols;
111ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey};
112ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
113ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff SharkeyTerminal::Terminal(int rows, int cols) : mRows(rows), mCols(cols) {
114ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey//    pt->writefn = NULL;
115ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey//    pt->resizedfn = NULL;
116ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
117ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    /* Create VTerm */
118ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    mVt = vterm_new(rows, cols);
119ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    vterm_parser_set_utf8(mVt, 1);
120ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
121ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    /* Set up screen */
122ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    mVts = vterm_obtain_screen(mVt);
123ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    vterm_screen_enable_altscreen(mVts, 1);
124ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    vterm_screen_set_callbacks(mVts, &cb, this);
125ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    vterm_screen_set_damage_merge(mVts, VTERM_DAMAGE_SCROLL);
126ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
127ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    // TODO: finish setup and forkpty() here
128ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey}
129ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
130ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff SharkeyTerminal::~Terminal() {
131ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    vterm_free(mVt);
132ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey}
133ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
134ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkeyint Terminal::getRows() {
135ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    return mRows;
136ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey}
137ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
138ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey/*
139ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey * JNI glue
140ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey */
141ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
142ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkeystatic jint com_android_terminal_Terminal_nativeInit(JNIEnv* env, jclass clazz,
143ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey        jint rows, jint cols) {
144ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    return reinterpret_cast<jint>(new Terminal(rows, cols));
145ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey}
146ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
147ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkeystatic jint com_android_terminal_Terminal_nativeGetRows(JNIEnv* env, jclass clazz, jint ptr) {
148ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    Terminal* term = reinterpret_cast<Terminal*>(ptr);
149ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    return term->getRows();
150ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey}
151ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
152ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkeystatic JNINativeMethod gMethods[] = {
153ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    { "nativeInit", "(II)I", (void*)com_android_terminal_Terminal_nativeInit },
154ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    { "nativeGetRows", "(I)I", (void*)com_android_terminal_Terminal_nativeGetRows },
155ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey};
156ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
157ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkeyint register_com_android_terminal_Terminal(JNIEnv* env) {
158ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey    return jniRegisterNativeMethods(env, "com/android/terminal/Terminal",
159ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey            gMethods, NELEM(gMethods));
160ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey}
161ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey
162ef946f3ae80556ab221265c0bf1c560683ea27f6Jeff Sharkey} /* namespace android */
163