comparison 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
comparison
equal deleted inserted replaced
495:c7a947e291db 496:f698820bffdf
1 package com.five_ten_sg.connectbot.service; 1 package com.five_ten_sg.connectbot.service;
2 2
3 import android.app.ActivityManager;
3 import android.content.ComponentName; 4 import android.content.ComponentName;
4 import android.content.Context; 5 import android.content.Context;
5 import android.content.Intent; 6 import android.content.Intent;
6 import android.content.ServiceConnection; 7 import android.content.ServiceConnection;
7 import android.net.Uri; 8 import android.net.Uri;
8 import android.os.IBinder; 9 import android.os.IBinder;
9 import android.util.Log; 10 import android.util.Log;
10 import android.view.View; 11 import android.view.View;
12 import android.view.inputmethod.InputMethodManager;
11 import de.mud.terminal.vt320; 13 import de.mud.terminal.vt320;
12 import java.io.IOException; 14 import java.io.IOException;
13 import java.io.InputStream; 15 import java.io.InputStream;
14 import java.io.OutputStream; 16 import java.io.OutputStream;
15 import java.net.InetAddress; 17 import java.net.InetAddress;
40 public static final char MONITOR_CMD_DEPRESS = 8; 42 public static final char MONITOR_CMD_DEPRESS = 8;
41 public static final char MONITOR_CMD_SHOWURL = 9; 43 public static final char MONITOR_CMD_SHOWURL = 9;
42 public static final char MONITOR_CMD_SWITCHSESSION = 10; 44 public static final char MONITOR_CMD_SWITCHSESSION = 10;
43 public static final char MONITOR_CMD_CURSORREQUEST = 11; 45 public static final char MONITOR_CMD_CURSORREQUEST = 11;
44 public static final char MONITOR_CMD_SAYSTRING = 12; 46 public static final char MONITOR_CMD_SAYSTRING = 12;
47 public static final char MONITOR_CMD_SETFOCUS = 13;
48 public static final char MONITOR_CMD_KEYBOARD = 14;
49 public static final char MONITOR_CMD_DEPRESSUNICODE= 15;
50 public static final char MONITOR_CMD_FOREGROUND = 16;
45 51
46 public static final String[] commands = { 52 public static final String[] commands = {
47 "cmd_init", 53 "cmd_init",
48 "cmd_activate", 54 "cmd_activate",
49 "cmd_keystate", 55 "cmd_keystate",
54 "cmd_screenwatch", 60 "cmd_screenwatch",
55 "cmd_depress", 61 "cmd_depress",
56 "cmd_showurl", 62 "cmd_showurl",
57 "cmd_switchsession", 63 "cmd_switchsession",
58 "cmd_cursorrequest", 64 "cmd_cursorrequest",
59 "cmd_saystring" 65 "cmd_saystring",
66 "cmd_setfocus",
67 "cmd_keyboard",
68 "cmd_depressunicode",
69 "cmd_foreground"
60 }; 70 };
61 71
62 public static final char CURSOR_REQUESTED = 0; 72 public static final char CURSOR_REQUESTED = 0;
63 public static final char CURSOR_SCREEN_CHANGE = 1; 73 public static final char CURSOR_SCREEN_CHANGE = 1;
64 public static final char CURSOR_USER_KEY = 2; 74 public static final char CURSOR_USER_KEY = 2;
65 75
66 private static final int MONITORPORT = 6000; 76 private static final int MONITORPORT = 6000;
67 77 private static final int MONITORPORT2 = 6001; // protocol version 2
68 private Context parent = null; 78
79 private TerminalManager parent = null;
69 private vt320 buffer = null; 80 private vt320 buffer = null;
70 private View view = null; 81 private View view = null;
71 private HostBean host = null; 82 private HostBean host = null;
72 private Uri uri = null; 83 private Uri uri = null;
84 private int port = 0;
73 private String target = null; 85 private String target = null;
74 private String init = null; 86 private String init = null;
75 private int start_line = 0; // monitor part of the screen for changes 87 private int start_line = 0; // monitor part of the screen for changes
76 private int end_line = 500; // "" 88 private int end_line = 500; // ""
77 private int start_column = 0; // "" 89 private int start_column = 0; // ""
170 if (packet.length == 1) 182 if (packet.length == 1)
171 cursorRequest(); 183 cursorRequest();
172 184
173 break; 185 break;
174 186
187 case MONITOR_CMD_SETFOCUS:
188 if (packet.length == 3)
189 setFocus(packet[1], packet[2]);
190
191 break;
192
193 case MONITOR_CMD_KEYBOARD:
194 if (packet.length == 2)
195 keyboard(packet[1]);
196
197 break;
198
199 case MONITOR_CMD_DEPRESSUNICODE:
200 if (packet.length == 2)
201 depressunicode(packet[1]);
202
203 break;
204
205 case MONITOR_CMD_FOREGROUND:
206 if (packet.length == 1)
207 foreground();
208
209 break;
210
175 default: 211 default:
176 break; 212 break;
177 } 213 }
178 } 214 }
179 catch (IOException e) { 215 catch (IOException e) {
196 InetAddress serverAddr = InetAddress.getByName(target); 232 InetAddress serverAddr = InetAddress.getByName(target);
197 int tries = 0; 233 int tries = 0;
198 while (tries < 10) { 234 while (tries < 10) {
199 try { 235 try {
200 Thread.sleep(100); 236 Thread.sleep(100);
201 monitor_socket = new Socket(serverAddr, MONITORPORT); 237 monitor_socket = new Socket(serverAddr, port);
202 break; 238 break;
203 } 239 }
204 catch (Exception e) { 240 catch (Exception e) {
205 monitor_socket = null; 241 monitor_socket = null;
206 Log.e(TAG, "exception connecting to monitor socket", e); 242 Log.e(TAG, "exception connecting to monitor socket", e);
236 Log.i(TAG, "unbound from service"); 272 Log.i(TAG, "unbound from service");
237 } 273 }
238 }; 274 };
239 275
240 276
241 public TerminalMonitor(Context parent, vt320 buffer, View view, HostBean host, String url) { 277 public TerminalMonitor(TerminalManager parent, vt320 buffer, View view, HostBean host, String url) {
242 this.parent = parent; 278 this.parent = parent;
243 this.buffer = buffer; 279 this.buffer = buffer;
244 this.view = view; 280 this.view = view;
245 this.host = host; 281 this.host = host;
246 this.uri = Uri.parse(url); 282 this.uri = Uri.parse(url);
283 this.port = (uri.getScheme().equals("socket2")) ? MONITORPORT2 : MONITORPORT;
247 this.target = uri.getHost(); 284 this.target = uri.getHost();
248 this.init = uri.getPath(); 285 this.init = uri.getPath();
249 // setup the windows->android keymapping 286 // setup the windows->android keymapping
250 // http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731 287 // http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731
251 keymap = new HashMap<Integer, Integer>(); 288 keymap = new HashMap<Integer, Integer>();
513 start_column = c; 550 start_column = c;
514 end_column = c + len - 1; 551 end_column = c + len - 1;
515 } 552 }
516 553
517 public synchronized void depress(int vk_key) { 554 public synchronized void depress(int vk_key) {
518 Log.i(TAG, String.format("depress(%d)", vk_key)); 555 Log.i(TAG, String.format("depress(%04x)", vk_key));
519 Integer x = keymap.get(new Integer(vk_key)); 556 Integer x = keymap.get(new Integer(vk_key));
520 557
521 if (x != null) buffer.keyDepressed(x, ' ', 0); 558 if (x != null) buffer.keyDepressed(x);
522 } 559 }
523 560
524 public synchronized void switchSession() { 561 public synchronized void switchSession() {
525 Log.i(TAG, "switchSession()"); 562 Log.i(TAG, "switchSession()");
526 Intent intent = new Intent(parent, ConsoleActivity.class); 563 Intent intent = new Intent(parent, ConsoleActivity.class);
532 569
533 public synchronized void cursorRequest() { 570 public synchronized void cursorRequest() {
534 cursorMoved(CURSOR_REQUESTED); 571 cursorMoved(CURSOR_REQUESTED);
535 } 572 }
536 573
574 public synchronized void setFocus(int l, int c) {
575 buffer.setField(l, c, new char[0]);
576 }
577
578 public synchronized void keyboard(int show) {
579 InputMethodManager inputMethodManager = (InputMethodManager)parent.getSystemService(Context.INPUT_METHOD_SERVICE);
580 View view = parent.activity.getCurrentView();
581 if (view == null) return;
582 if (show != 0) {
583 inputMethodManager.showSoftInput(view, 0);
584 } else {
585 inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
586 }
587 }
588
589 public synchronized void depressunicode(int ascii) {
590 Log.i(TAG, String.format("depressunicode(%04x)", ascii));
591 buffer.keyUnicodeDepressed(ascii);
592 }
593
594 public synchronized void foreground() {
595 ActivityManager activityManager = (ActivityManager) parent.getSystemService(Context.ACTIVITY_SERVICE);
596 activityManager.moveTaskToFront(parent.activity.getTaskId(), 0);
597 }
598
537 } 599 }