view src/com/five_ten_sg/connectbot/service/TerminalMonitor.java @ 188:cf677a6f586d

use floating point font size, change size by scaling factor rather than linear addition
author Carl Byington <carl@five-ten-sg.com>
date Wed, 02 Jul 2014 16:10:20 -0700
parents 6e07ea418b90
children f86f1e37b504
line wrap: on
line source

package com.five_ten_sg.connectbot.service;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import de.mud.terminal.vt320;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;

import com.five_ten_sg.connectbot.ConsoleActivity;
import com.five_ten_sg.connectbot.bean.HostBean;


public class TerminalMonitor {
    public final static String TAG = "ConnectBot.TerminalMonitor";

    public  static final char MONITOR_CMD_INIT          = 0;
    public  static final char MONITOR_CMD_ACTIVATE      = 1;
    public  static final char MONITOR_CMD_KEYSTATE      = 2;
    public  static final char MONITOR_CMD_CURSORMOVE    = 3;
    public  static final char MONITOR_CMD_SCREENCHANGE  = 4;
    public  static final char MONITOR_CMD_FIELDVALUE    = 5;
    public  static final char MONITOR_CMD_SETFIELD      = 5;
    public  static final char MONITOR_CMD_GETFIELD      = 6;
    public  static final char MONITOR_CMD_SCREENWATCH   = 7;
    public  static final char MONITOR_CMD_DEPRESS       = 8;
    public  static final char MONITOR_CMD_SHOWURL       = 9;
    public  static final char MONITOR_CMD_SWITCHSESSION = 10;

    private static final int    MONITORPORT       = 6000;
    private static final String LOCALHOST         = "127.0.0.1";

    private Context             parent       = null;
    private vt320               buffer       = null;
    private View                view         = null;
    private HostBean            host         = null;
    private String              init         = null;
    private int                 start_line   = 0;       // monitor part of the screen for changes
    private int                 end_line     = 500;     // ""
    private int                 start_column = 0;       // ""
    private int                 end_column   = 500;     // ""
    private boolean             modified     = false;   // used to delay screen change notifications
    private boolean             moved        = false;   // used to delay cursor moved notifications
    private int                 to_line      = 0;       // ""
    private int                 to_column    = 0;       // ""
    private HashMap<Integer, Integer>  keymap = null;   // map MS VK_ keys to vt320 virtual keys
    private IBinder             bound        = null;
    private Socket              monitor_socket = null;
    private InputStream         monitor_in     = null;
    private OutputStream        monitor_out    = null;
    private MyReader            monitor_reader = null;
    private BlockingQueue<char[]> pending_commands = new ArrayBlockingQueue<char[]>(100);
    private MyServiceConnection monitor_connection = new MyServiceConnection();

    class MyReader extends Thread {
        private InputStream monitor_in;
        private byte[] b;
        private boolean is_closing = false;

        public MyReader(InputStream monitor_in) {
            this.monitor_in = monitor_in;
            b = new byte[100];
            Log.i(TAG, "MyReader constructor");
        }

        public void closing() {
            is_closing = true;
        }

        private char[] forceRead(int len) throws IOException {
            int len2 = len * 2;
            int off  = 0;

            if (b.length < len2) b = new byte[len2];

            while (off < len2) {
                int l = monitor_in.read(b, off, len2 - off);

                if (l < 0) throw new IOException("eof");

                off += l;
            }

            return bytesToChars(b, len2);
        }

        public void run() {
            while (true) {
                try {
                    char[] len = forceRead(1);
                    char[] packet = forceRead(len[0]);
                    char cmd = packet[0];
                    Log.i(TAG, String.format("received %d command", (int)cmd));

                    switch (cmd) {
                        case MONITOR_CMD_SETFIELD:
                            if (packet.length >= 3)
                                setField(packet[1], packet[2], packet, 3);

                            break;

                        case MONITOR_CMD_GETFIELD:
                            if (packet.length == 4)
                                getField(packet[1], packet[2], packet[3]);

                            break;

                        case MONITOR_CMD_SCREENWATCH:
                            if (packet.length == 4)
                                screenWatch(packet[1], packet[2], packet[3]);

                            break;

                        case MONITOR_CMD_DEPRESS:
                            if (packet.length == 2)
                                depress(packet[1]);

                            break;

                        case MONITOR_CMD_SHOWURL:
                            if (packet.length > 1)
                                showUrl(packet, 1);

                            break;

                        case MONITOR_CMD_SWITCHSESSION:
                            if (packet.length == 1)
                                switchSession();

                            break;

                        default:
                            break;
                    }
                }
                catch (IOException e) {
                    if (!is_closing) Log.e(TAG, "exception in MyReader.run()", e);

                    break;
                }
            }
        }
    }

    class MyServiceConnection implements ServiceConnection {
        public void onServiceConnected(ComponentName className, IBinder service) {
            bound = service;
            Log.i(TAG, "bound to service");

            try {
                InetAddress serverAddr = InetAddress.getByName(LOCALHOST);
                monitor_socket = new Socket(serverAddr, MONITORPORT);
                monitor_in     = monitor_socket.getInputStream();
                monitor_out    = monitor_socket.getOutputStream();
                Log.i(TAG, "connected to monitor socket, send init " + init);
                monitor_reader = new MyReader(monitor_in);
                monitor_reader.start();
                String x = "  " + init;
                monitorWrite(MONITOR_CMD_INIT, x.toCharArray());
                char [] c;

                while (true) {
                    c = pending_commands.poll();

                    if (c == null) break;

                    monitorWrite(c[1], c);
                }
            }
            catch (IOException e) {
                Log.e(TAG, "exception in onServiceConnected()", e);
            }
        }
        public void onServiceDisconnected(ComponentName classNam) {
            bound = null;
            Log.i(TAG, "unbound from service");
        }
    };


    public TerminalMonitor(Context parent, vt320 buffer, View view, HostBean host, String init) {
        this.parent      = parent;
        this.buffer      = buffer;
        this.view        = view;
        this.host        = host;
        this.init        = init;
        // setup the windows->android keymapping
        // http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731
        keymap      = new HashMap<Integer, Integer>();
        keymap.put(0x08, vt320.KEY_BACK_SPACE);  // vk_back
        keymap.put(0x09, vt320.KEY_TAB);         // vk_tab
        keymap.put(0x0d, vt320.KEY_ENTER);       // vk_return
        keymap.put(0x1b, vt320.KEY_ESCAPE);      // vk_escape
        keymap.put(0x21, vt320.KEY_PAGE_UP);     // vk_prior
        keymap.put(0x22, vt320.KEY_PAGE_DOWN);   // vk_next
        keymap.put(0x23, vt320.KEY_END);         // vk_end
        keymap.put(0x24, vt320.KEY_HOME);        // vk_home
        keymap.put(0x25, vt320.KEY_LEFT);        // vk_left
        keymap.put(0x26, vt320.KEY_UP);          // vk_up
        keymap.put(0x27, vt320.KEY_RIGHT);       // vk_right
        keymap.put(0x28, vt320.KEY_DOWN);        // vk_down
        keymap.put(0x2d, vt320.KEY_INSERT);      // vk_insert
        keymap.put(0x2e, vt320.KEY_DELETE);      // vk_delete
        keymap.put(0x70, vt320.KEY_F1);          // vk_f1
        keymap.put(0x71, vt320.KEY_F2);          // vk_f2
        keymap.put(0x72, vt320.KEY_F3);          // vk_f3
        keymap.put(0x73, vt320.KEY_F4);          // vk_f4
        keymap.put(0x74, vt320.KEY_F5);          // vk_f5
        keymap.put(0x75, vt320.KEY_F6);          // vk_f6
        keymap.put(0x76, vt320.KEY_F7);          // vk_f7
        keymap.put(0x77, vt320.KEY_F8);          // vk_f8
        keymap.put(0x78, vt320.KEY_F9);          // vk_f9
        keymap.put(0x79, vt320.KEY_F10);         // vk_f10
        keymap.put(0x7a, vt320.KEY_F11);         // vk_f11
        keymap.put(0x7b, vt320.KEY_F12);         // vk_f12
        keymap.put(0x7c, vt320.KEY_F13);         // vk_f13
        keymap.put(0x7d, vt320.KEY_F14);         // vk_f14
        keymap.put(0x7e, vt320.KEY_F15);         // vk_f15
        keymap.put(0x7f, vt320.KEY_F16);         // vk_f16
        keymap.put(0x80, vt320.KEY_F17);         // vk_f17
        keymap.put(0x81, vt320.KEY_F18);         // vk_f18
        keymap.put(0x82, vt320.KEY_F19);         // vk_f19
        keymap.put(0x83, vt320.KEY_F20);         // vk_f20
        keymap.put(0x84, vt320.KEY_F21);         // vk_f21
        keymap.put(0x85, vt320.KEY_F22);         // vk_f22
        keymap.put(0x86, vt320.KEY_F23);         // vk_f23
        keymap.put(0x87, vt320.KEY_F24);         // vk_f24
        // bind to the monitor service
        Intent intent = new Intent("com.five_ten_sg.connectbot.monitor.MonitorService");
        parent.bindService(intent, monitor_connection, Context.BIND_AUTO_CREATE);
        Log.i(TAG, "constructor");
    }


    public void Disconnect() {
        if (monitor_reader != null) monitor_reader.closing();

        try {
            if (monitor_out    != null) monitor_out.close();

            if (monitor_in     != null) monitor_in.close();

            if (monitor_socket != null) monitor_socket.close();

            Log.i(TAG, "disconnected from monitor socket");
        }
        catch (IOException e) {
            Log.e(TAG, "exception in Disconnect() closing sockets", e);
        }

        monitor_reader = null;
        monitor_out    = null;
        monitor_in     = null;
        monitor_socket = null;

        if (bound != null) parent.unbindService(monitor_connection);

        monitor_connection = null;
    }


    public char[] bytesToChars(byte[] b, int len) {
        char[] c = new char[len >> 1];
        int bp = 0;

        for (int i = 0; i < c.length; i++) {
            byte b1 = b[bp++];
            byte b2 = b[bp++];
            c[i] = (char)(((b1 & 0x00FF) << 8) + (b2 & 0x00FF));
        }

        return c;
    }


    public byte[] charsToBytes(char[] c) {
        byte[] b = new byte[c.length << 1];
        int bp = 0;

        for (int i = 0; i < c.length; i++) {
            b[bp++] = (byte)((c[i] & 0xff00) >> 8);
            b[bp++] = (byte)(c[i] & 0x00ff);
        }

        return b;
    }


    public synchronized void monitorWrite(char cmd, char[] c) {
        try {
            if (monitor_out != null) {
                c[0] = (char)(c.length - 1);    // number of chars following
                c[1] = cmd;
                //Log.i(TAG, String.format("sending %d command", (int)cmd));
                monitor_out.write(charsToBytes(c));
            }
            else {
                c[1] = cmd;
                pending_commands.put(c);
            }
        }
        catch (InterruptedException e) {
            Log.e(TAG, "exception in monitorWrite()", e);
        }
        catch (IOException e) {
            Log.i(TAG, "exception in monitorWrite(), monitor died or closed the socket", e);

            try {
                monitor_out.close();
            }
            catch (IOException ee) {
                Log.e(TAG, "exception in monitorWrite() closing output stream", ee);
            }

            monitor_out = null;
        }
    };

    public void sendScreen(char cmd) {
        char lines   = (char)(buffer.height & 0x0000ffff);
        char columns = (char)(buffer.width & 0x0000ffff);
        char[] arg = new char[4 + lines * columns];
        arg[2] = lines;
        arg[3] = columns;
        int base = 4;

        for (int i = 0; i < lines; i++) {
            System.arraycopy(buffer.charArray[buffer.screenBase + i], 0, arg, base, columns);
            base += columns;
        }

        monitorWrite(cmd, arg);
    }

    public synchronized void activate() {
        sendScreen(MONITOR_CMD_ACTIVATE);
    }

    public synchronized void keyState(boolean down) {
        char[] arg = new char[3];
        arg[2] = (char)((down) ? 1 : 0);
        monitorWrite(MONITOR_CMD_KEYSTATE, arg);
    }

    public synchronized void cursorMove(int l, int c) {
        moved = true;
        to_line = l;
        to_column = c;
    }

    public void cursorMoved() {
        char[] arg = new char[4];
        arg[2] = (char)(to_line & 0x0000ffff);
        arg[3] = (char)(to_column & 0x0000ffff);
        monitorWrite(MONITOR_CMD_CURSORMOVE, arg);
    }

    public synchronized void testChanged() {
        if (modified) {
            modified = false;
            sendScreen(MONITOR_CMD_SCREENCHANGE);
        }

        if (moved) {
            moved = false;
            cursorMoved();
        }
    }

    public synchronized void screenChanged(int llow, int lhigh, int clow, int chigh) {
        if ((start_line <= lhigh) && (llow <= end_line) && (start_column <= chigh) && (clow <= end_column)) {
            modified = true;
        }
    }

    public synchronized void screenChanged(int l, int c) {
        screenChanged(l, l, c, c);
    }

    public synchronized void setField(int l, int c, char[] data, int offset) {
        Log.i(TAG, "setField()");
        char[] da = new char[data.length - offset];
        int i;

        for (i = 0; i < da.length; i++) {
            da[i] = data[i + offset];
        }

        buffer.setField(l, c, da);
    }

    public synchronized void showUrl(char [] data, int offset) {
        Log.i(TAG, "setField()");
        char[] da = new char[data.length - offset];
        int i;

        for (i = 0; i < da.length; i++) {
            da[i] = data[i + offset];
        }
        String url = new String(da);
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        parent.startActivity(intent);
    }

    public synchronized void getField(int l, int c, int len) {
        Log.i(TAG, "getField()");
        char[] arg2 = new char[4 + len];
        arg2[2] = (char)(l & 0x0000ffff);
        arg2[3] = (char)(c & 0x0000ffff);
        int base = 4;
        System.arraycopy(buffer.charArray[buffer.screenBase + l], c, arg2, base, len);
        monitorWrite(MONITOR_CMD_FIELDVALUE, arg2);
    }

    public synchronized void screenWatch(int l, int c, int len) {
        Log.i(TAG, "screenWatch()");
        start_line   = l;
        end_line     = l;
        start_column = c;
        end_column   = c + len - 1;
    }

    public synchronized void depress(int vk_key) {
        Log.i(TAG, String.format("depress() %d", vk_key));
        Integer x = keymap.get(new Integer(vk_key));
        if (x != null) buffer.keyDepressed(x, ' ', 0);
    }

    public synchronized void switchSession() {
        Log.i(TAG, "switchSession()");
        Intent intent = new Intent(parent, ConsoleActivity.class);
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(host.getUri());
        parent.startActivity(intent);
    }

}