diff app/src/main/java/com/five_ten_sg/connectbot/service/TerminalMonitor.java @ 496:f698820bffdf

add socket2 monitor protocol
author Carl Byington <carl@five-ten-sg.com>
date Sun, 05 Jun 2022 14:46:41 -0700
parents e88d48be89a8
children b8cc360e1550
line wrap: on
line diff
--- a/app/src/main/java/com/five_ten_sg/connectbot/service/TerminalMonitor.java	Sun Jun 05 12:00:10 2022 -0700
+++ b/app/src/main/java/com/five_ten_sg/connectbot/service/TerminalMonitor.java	Sun Jun 05 14:46:41 2022 -0700
@@ -1,5 +1,6 @@
 package com.five_ten_sg.connectbot.service;
 
+import android.app.ActivityManager;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
@@ -8,6 +9,7 @@
 import android.os.IBinder;
 import android.util.Log;
 import android.view.View;
+import android.view.inputmethod.InputMethodManager;
 import de.mud.terminal.vt320;
 import java.io.IOException;
 import java.io.InputStream;
@@ -42,6 +44,10 @@
     public  static final char MONITOR_CMD_SWITCHSESSION = 10;
     public  static final char MONITOR_CMD_CURSORREQUEST = 11;
     public  static final char MONITOR_CMD_SAYSTRING     = 12;
+    public  static final char MONITOR_CMD_SETFOCUS      = 13;
+    public  static final char MONITOR_CMD_KEYBOARD      = 14;
+    public  static final char MONITOR_CMD_DEPRESSUNICODE= 15;
+    public  static final char MONITOR_CMD_FOREGROUND    = 16;
 
     public  static final String[] commands = {
         "cmd_init",
@@ -56,7 +62,11 @@
         "cmd_showurl",
         "cmd_switchsession",
         "cmd_cursorrequest",
-        "cmd_saystring"
+        "cmd_saystring",
+        "cmd_setfocus",
+        "cmd_keyboard",
+        "cmd_depressunicode",
+        "cmd_foreground"
     };
 
     public  static final char CURSOR_REQUESTED      = 0;
@@ -64,12 +74,14 @@
     public  static final char CURSOR_USER_KEY       = 2;
 
     private static final int    MONITORPORT       = 6000;
+    private static final int    MONITORPORT2      = 6001;   // protocol version 2
 
-    private Context             parent       = null;
+    private TerminalManager     parent       = null;
     private vt320               buffer       = null;
     private View                view         = null;
     private HostBean            host         = null;
     private Uri                 uri          = null;
+    private int                 port         = 0;
     private String              target       = null;
     private String              init         = null;
     private int                 start_line   = 0;       // monitor part of the screen for changes
@@ -172,6 +184,30 @@
 
                             break;
 
+                        case MONITOR_CMD_SETFOCUS:
+                            if (packet.length == 3)
+                                setFocus(packet[1], packet[2]);
+
+                            break;
+
+                        case MONITOR_CMD_KEYBOARD:
+                            if (packet.length == 2)
+                                keyboard(packet[1]);
+
+                            break;
+
+                        case MONITOR_CMD_DEPRESSUNICODE:
+                            if (packet.length == 2)
+                                depressunicode(packet[1]);
+
+                            break;
+
+                        case MONITOR_CMD_FOREGROUND:
+                            if (packet.length == 1)
+                                foreground();
+
+                            break;
+
                         default:
                             break;
                     }
@@ -198,7 +234,7 @@
                 while (tries < 10) {
                     try {
                         Thread.sleep(100);
-                        monitor_socket = new Socket(serverAddr, MONITORPORT);
+                        monitor_socket = new Socket(serverAddr, port);
                         break;
                     }
                     catch (Exception e) {
@@ -238,12 +274,13 @@
     };
 
 
-    public TerminalMonitor(Context parent, vt320 buffer, View view, HostBean host, String url) {
+    public TerminalMonitor(TerminalManager parent, vt320 buffer, View view, HostBean host, String url) {
         this.parent      = parent;
         this.buffer      = buffer;
         this.view        = view;
         this.host        = host;
         this.uri         = Uri.parse(url);
+        this.port        = (uri.getScheme().equals("socket2")) ? MONITORPORT2 : MONITORPORT;
         this.target      = uri.getHost();
         this.init        = uri.getPath();
         // setup the windows->android keymapping
@@ -515,10 +552,10 @@
     }
 
     public synchronized void depress(int vk_key) {
-        Log.i(TAG, String.format("depress(%d)", vk_key));
+        Log.i(TAG, String.format("depress(%04x)", vk_key));
         Integer x = keymap.get(new Integer(vk_key));
 
-        if (x != null) buffer.keyDepressed(x, ' ', 0);
+        if (x != null) buffer.keyDepressed(x);
     }
 
     public synchronized void switchSession() {
@@ -534,4 +571,29 @@
         cursorMoved(CURSOR_REQUESTED);
     }
 
+    public synchronized void setFocus(int l, int c) {
+        buffer.setField(l, c, new char[0]);
+    }
+
+    public synchronized void keyboard(int show) {
+        InputMethodManager inputMethodManager = (InputMethodManager)parent.getSystemService(Context.INPUT_METHOD_SERVICE);
+        View view = parent.activity.getCurrentView();
+        if (view == null) return;
+        if (show != 0) {
+            inputMethodManager.showSoftInput(view, 0);
+        } else {
+            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
+        }
+    }
+
+    public synchronized void depressunicode(int ascii) {
+        Log.i(TAG, String.format("depressunicode(%04x)", ascii));
+        buffer.keyUnicodeDepressed(ascii);
+    }
+
+    public synchronized void foreground() {
+        ActivityManager activityManager = (ActivityManager) parent.getSystemService(Context.ACTIVITY_SERVICE);
+        activityManager.moveTaskToFront(parent.activity.getTaskId(), 0);
+    }
+
 }