changeset 496:f698820bffdf

add socket2 monitor protocol
author Carl Byington <carl@five-ten-sg.com>
date Sun, 05 Jun 2022 14:46:41 -0700
parents c7a947e291db
children 73fa7329dc87
files app/src/main/AndroidManifest.xml app/src/main/java/com/five_ten_sg/connectbot/ConsoleActivity.java app/src/main/java/com/five_ten_sg/connectbot/service/TerminalManager.java app/src/main/java/com/five_ten_sg/connectbot/service/TerminalMonitor.java app/src/main/java/com/five_ten_sg/connectbot/transport/AbsTransport.java app/src/main/java/com/five_ten_sg/connectbot/transport/TN5250.java app/src/main/java/de/mud/terminal/vt320.java
diffstat 7 files changed, 125 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/app/src/main/AndroidManifest.xml	Sun Jun 05 12:00:10 2022 -0700
+++ b/app/src/main/AndroidManifest.xml	Sun Jun 05 14:46:41 2022 -0700
@@ -51,7 +51,7 @@
 		android:backupAgent=".service.BackupAgent"
 		android:killAfterRestore="true">
 
-		<activity android:name=".HostListActivity" >
+		<activity android:name=".HostListActivity" android:exported="true">
 			<intent-filter>
 				<action android:name="android.intent.action.MAIN" />
 				<category android:name="android.intent.category.LAUNCHER" />
@@ -70,7 +70,7 @@
 			</intent-filter>
 		</activity>
 
-		<activity android:name=".PubkeyListActivity" android:configChanges="keyboardHidden|orientation" >
+		<activity android:name=".PubkeyListActivity" android:exported="false" android:configChanges="keyboardHidden|orientation" >
 			<intent-filter>
 				<action android:name="org.openintents.ssh.agent.IDENTITY_ADMIN" />
 				<category android:name="android.intent.category.DEFAULT" />
@@ -92,13 +92,14 @@
 
 		<service android:name="com.five_ten_sg.connectbot.service.AuthAgentService"
 			android:description="@string/auth_agent_service_desc"
-			android:permission="org.openintents.ssh.permission.ACCESS_SSH_AGENT">
+			android:permission="org.openintents.ssh.permission.ACCESS_SSH_AGENT"
+			android:exported="true">
 			<intent-filter>
 				<action android:name="org.openintents.ssh.BIND_SSH_AGENT_SERVICE" />
 			</intent-filter>
 		</service>
 
-		<activity android:name=".ConsoleActivity" android:configChanges="keyboardHidden|orientation"
+		<activity android:name=".ConsoleActivity" android:exported="true" android:configChanges="keyboardHidden|orientation"
 			android:theme="@style/NoTitle" android:windowSoftInputMode="stateAlwaysVisible|adjustResize"
 			android:launchMode="singleTop" android:hardwareAccelerated="false">
 			<intent-filter>
--- a/app/src/main/java/com/five_ten_sg/connectbot/ConsoleActivity.java	Sun Jun 05 12:00:10 2022 -0700
+++ b/app/src/main/java/com/five_ten_sg/connectbot/ConsoleActivity.java	Sun Jun 05 14:46:41 2022 -0700
@@ -92,9 +92,10 @@
     private static final int SHIFT_LEFT = 0;
     private static final int SHIFT_RIGHT = 1;
 
-    protected ViewFlipper flip = null;
-    protected TerminalManager bound = null;
-    protected LayoutInflater inflater = null;
+    protected ConsoleActivity activity = null;
+    protected ViewFlipper     flip     = null;
+    protected TerminalManager bound    = null;
+    protected LayoutInflater  inflater = null;
 
     private SharedPreferences prefs = null;
 
@@ -150,6 +151,8 @@
             bound = ((TerminalManager.TerminalBinder) service).getService();
             // let manager know about our event handling services
             bound.disconnectHandler = disconnectHandler;
+            // let manager know about this activity
+            bound.activity = activity;
             Log.d(TAG, String.format("Connected to TerminalManager and found bridges.size=%d", bound.bridges.size()));
             bound.setResizeAllowed(true);
             bound.hardKeyboardHidden = (getResources().getConfiguration().hardKeyboardHidden ==
@@ -268,6 +271,10 @@
         }
     }
 
+    public View getCurrentView() {
+        return flip.getCurrentView();
+    }
+
     protected View findCurrentView(int id) {
         View view = flip.getCurrentView();
 
@@ -337,6 +344,7 @@
     @Override
     public void onCreate(Bundle icicle) {
         super.onCreate(icicle);
+        activity = this;
         configureStrictMode();
         hardKeyboard = getResources().getConfiguration().keyboard ==
                        Configuration.KEYBOARD_QWERTY;
--- a/app/src/main/java/com/five_ten_sg/connectbot/service/TerminalManager.java	Sun Jun 05 12:00:10 2022 -0700
+++ b/app/src/main/java/com/five_ten_sg/connectbot/service/TerminalManager.java	Sun Jun 05 14:46:41 2022 -0700
@@ -32,6 +32,8 @@
 import java.util.TimerTask;
 
 import com.five_ten_sg.connectbot.R;
+import com.five_ten_sg.connectbot.ConsoleActivity;
+import com.five_ten_sg.connectbot.service.TerminalBridge;
 import com.five_ten_sg.connectbot.bean.HostBean;
 import com.five_ten_sg.connectbot.bean.PubkeyBean;
 import com.five_ten_sg.connectbot.transport.TransportFactory;
@@ -75,6 +77,8 @@
     public Map<String, WeakReference<TerminalBridge>> mNicknameBridgeMap =
         new HashMap<String, WeakReference<TerminalBridge>>();
 
+    public ConsoleActivity activity = null;
+
     public TerminalBridge defaultBridge = null;
 
     public List<HostBean> disconnected = new LinkedList<HostBean>();
--- 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);
+    }
+
 }
--- a/app/src/main/java/com/five_ten_sg/connectbot/transport/AbsTransport.java	Sun Jun 05 12:00:10 2022 -0700
+++ b/app/src/main/java/com/five_ten_sg/connectbot/transport/AbsTransport.java	Sun Jun 05 14:46:41 2022 -0700
@@ -55,6 +55,16 @@
         //public void setField(int l, int c, char [] data)
         // implementation in the base vt320
 
+        // monitor simulating function key depress
+        //@Override
+        //public void keyDepressed(int keyCode) {
+        // implementation in the base vt320
+
+        // monitor simulating ascii key depress
+        //@Override
+        //public void keyUnicodeDepressed(int keyCode) {
+        // implementation in the base vt320
+
         // terminal key listener found special key, send notification to monitor
         @Override
         public void monitorKey(boolean down) {
--- a/app/src/main/java/com/five_ten_sg/connectbot/transport/TN5250.java	Sun Jun 05 12:00:10 2022 -0700
+++ b/app/src/main/java/com/five_ten_sg/connectbot/transport/TN5250.java	Sun Jun 05 14:46:41 2022 -0700
@@ -158,14 +158,15 @@
             screen52.setField(l, c, data);
         }
 
-        // monitor simulating key depress
+        // monitor simulating function key depress
         @Override
-        public void keyDepressed(int keyCode, char keyChar, int modifiers) {
-            if (mnemonics.containsKey(keyCode)) {
-                String s = mnemonics.get(keyCode);
+        public void keyDepressed(int keyCode) {
+            keyPressed(keyCode, ' ', 0);
+        }
 
-                if (s != "") screen52.sendKeys(s);
-            }
+        // monitor simulating ascii key depress
+        public void keyUnicodeDepressed(int keyCode) {
+            write((byte)(keyCode & 0x00ff));
         }
 
         // terminal key listener found special key, send notification to monitor
@@ -188,9 +189,17 @@
 
             if (bridge.monitor != null) bridge.monitor.testMoved();
         }
+        /**
+         * main keytyping event handler for all the special function and modifier keys
+         * the normal keys are processed by write(byte b);
+         */
         @Override
         public void keyPressed(int keyCode, char keyChar, int modifiers) {
-            keyDepressed(keyCode, keyChar, modifiers);
+            if (mnemonics.containsKey(keyCode)) {
+                String s = mnemonics.get(keyCode);
+
+                if (s != "") screen52.sendKeys(s);
+            }
 
             if (bridge.monitor != null) bridge.monitor.testMoved();
         }
--- a/app/src/main/java/de/mud/terminal/vt320.java	Sun Jun 05 12:00:10 2022 -0700
+++ b/app/src/main/java/de/mud/terminal/vt320.java	Sun Jun 05 14:46:41 2022 -0700
@@ -65,9 +65,7 @@
         /* do nothing by default */
     }
 
-    /**
-     * inject field contents as if typed
-     */
+    // monitor injecting a field
     public void setField(int l, int c, char [] d) {
         // ignore line and column, just send the bytes to the host.
         int n = d.length;
@@ -78,14 +76,20 @@
         write(b);
     }
 
+    // monitor simulating function key depress
+    public void keyDepressed(int keyCode) {
+        keyPressed(keyCode, ' ', 0);
+    }
+
+    // monitor simulating ascii key depress
+    public void keyUnicodeDepressed(int keyCode) {
+        write((byte)(keyCode & 0x00ff));
+    }
+
     public void monitorKey(boolean down) {
         // do nothing
     }
 
-    public void keyDepressed(int keyCode, char keyChar, int modifiers) {
-        keyPressed(keyCode, keyChar, modifiers);
-    }
-
     /**
      * Play the beep sound ...
      */