Mercurial > 510Connectbot
annotate src/com/five_ten_sg/connectbot/service/TerminalMonitor.java @ 248:0181323d0d33
add host preference for fixed screen size
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Mon, 14 Jul 2014 11:20:42 -0700 |
parents | ea49747c5447 |
children | 2bf2724d8610 |
rev | line source |
---|---|
0 | 1 package com.five_ten_sg.connectbot.service; |
2 | |
3 import android.content.ComponentName; | |
4 import android.content.Context; | |
5 import android.content.Intent; | |
6 import android.content.ServiceConnection; | |
155 | 7 import android.net.Uri; |
0 | 8 import android.os.IBinder; |
9 import android.util.Log; | |
10 import android.view.View; | |
11 import de.mud.terminal.vt320; | |
12 import java.io.IOException; | |
13 import java.io.InputStream; | |
14 import java.io.OutputStream; | |
15 import java.net.InetAddress; | |
16 import java.net.Socket; | |
17 import java.nio.charset.Charset; | |
18 import java.util.HashMap; | |
15
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
19 import java.util.concurrent.ArrayBlockingQueue; |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
20 import java.util.concurrent.BlockingQueue; |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
21 import java.util.concurrent.ConcurrentHashMap; |
0 | 22 |
174
b010f9dc801f
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
173
diff
changeset
|
23 import com.five_ten_sg.connectbot.ConsoleActivity; |
b010f9dc801f
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
173
diff
changeset
|
24 import com.five_ten_sg.connectbot.bean.HostBean; |
b010f9dc801f
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
173
diff
changeset
|
25 |
b010f9dc801f
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
173
diff
changeset
|
26 |
0 | 27 public class TerminalMonitor { |
28 public final static String TAG = "ConnectBot.TerminalMonitor"; | |
29 | |
172
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
30 public static final char MONITOR_CMD_INIT = 0; |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
31 public static final char MONITOR_CMD_ACTIVATE = 1; |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
32 public static final char MONITOR_CMD_KEYSTATE = 2; |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
33 public static final char MONITOR_CMD_CURSORMOVE = 3; |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
34 public static final char MONITOR_CMD_SCREENCHANGE = 4; |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
35 public static final char MONITOR_CMD_FIELDVALUE = 5; |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
36 public static final char MONITOR_CMD_SETFIELD = 5; |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
37 public static final char MONITOR_CMD_GETFIELD = 6; |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
38 public static final char MONITOR_CMD_SCREENWATCH = 7; |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
39 public static final char MONITOR_CMD_DEPRESS = 8; |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
40 public static final char MONITOR_CMD_SHOWURL = 9; |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
41 public static final char MONITOR_CMD_SWITCHSESSION = 10; |
205
f86f1e37b504
add cursor request command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
176
diff
changeset
|
42 public static final char MONITOR_CMD_CURSORREQUEST = 11; |
0 | 43 |
229
594101a0876a
add why argument on cursor updates
Carl Byington <carl@five-ten-sg.com>
parents:
227
diff
changeset
|
44 public static final char CURSOR_REQUESTED = 0; |
594101a0876a
add why argument on cursor updates
Carl Byington <carl@five-ten-sg.com>
parents:
227
diff
changeset
|
45 public static final char CURSOR_SCREEN_CHANGE = 1; |
594101a0876a
add why argument on cursor updates
Carl Byington <carl@five-ten-sg.com>
parents:
227
diff
changeset
|
46 public static final char CURSOR_USER_KEY = 2; |
594101a0876a
add why argument on cursor updates
Carl Byington <carl@five-ten-sg.com>
parents:
227
diff
changeset
|
47 |
0 | 48 private static final int MONITORPORT = 6000; |
49 private static final String LOCALHOST = "127.0.0.1"; | |
50 | |
51 private Context parent = null; | |
52 private vt320 buffer = null; | |
53 private View view = null; | |
174
b010f9dc801f
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
173
diff
changeset
|
54 private HostBean host = null; |
0 | 55 private String init = null; |
56 private int start_line = 0; // monitor part of the screen for changes | |
57 private int end_line = 500; // "" | |
58 private int start_column = 0; // "" | |
59 private int end_column = 500; // "" | |
16
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
60 private boolean modified = false; // used to delay screen change notifications |
229
594101a0876a
add why argument on cursor updates
Carl Byington <carl@five-ten-sg.com>
parents:
227
diff
changeset
|
61 private boolean moved = false; // used to delay cursor moved notifications |
16
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
62 private int to_line = 0; // "" |
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
63 private int to_column = 0; // "" |
113
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
64 private HashMap<Integer, Integer> keymap = null; // map MS VK_ keys to vt320 virtual keys |
0 | 65 private IBinder bound = null; |
66 private Socket monitor_socket = null; | |
67 private InputStream monitor_in = null; | |
68 private OutputStream monitor_out = null; | |
69 private MyReader monitor_reader = null; | |
15
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
70 private BlockingQueue<char[]> pending_commands = new ArrayBlockingQueue<char[]>(100); |
0 | 71 private MyServiceConnection monitor_connection = new MyServiceConnection(); |
72 | |
73 class MyReader extends Thread { | |
74 private InputStream monitor_in; | |
75 private byte[] b; | |
76 private boolean is_closing = false; | |
77 | |
78 public MyReader(InputStream monitor_in) { | |
79 this.monitor_in = monitor_in; | |
80 b = new byte[100]; | |
81 Log.i(TAG, "MyReader constructor"); | |
82 } | |
83 | |
84 public void closing() { | |
85 is_closing = true; | |
86 } | |
87 | |
88 private char[] forceRead(int len) throws IOException { | |
89 int len2 = len * 2; | |
90 int off = 0; | |
91 | |
92 if (b.length < len2) b = new byte[len2]; | |
93 | |
94 while (off < len2) { | |
95 int l = monitor_in.read(b, off, len2 - off); | |
96 | |
97 if (l < 0) throw new IOException("eof"); | |
98 | |
99 off += l; | |
100 } | |
101 | |
102 return bytesToChars(b, len2); | |
103 } | |
104 | |
105 public void run() { | |
106 while (true) { | |
107 try { | |
108 char[] len = forceRead(1); | |
109 char[] packet = forceRead(len[0]); | |
110 char cmd = packet[0]; | |
111 Log.i(TAG, String.format("received %d command", (int)cmd)); | |
112 | |
113 switch (cmd) { | |
114 case MONITOR_CMD_SETFIELD: | |
153
3ca280646f2d
allow zero length setfield
Carl Byington <carl@five-ten-sg.com>
parents:
148
diff
changeset
|
115 if (packet.length >= 3) |
0 | 116 setField(packet[1], packet[2], packet, 3); |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
117 |
0 | 118 break; |
119 | |
120 case MONITOR_CMD_GETFIELD: | |
121 if (packet.length == 4) | |
122 getField(packet[1], packet[2], packet[3]); | |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
123 |
0 | 124 break; |
125 | |
126 case MONITOR_CMD_SCREENWATCH: | |
127 if (packet.length == 4) | |
128 screenWatch(packet[1], packet[2], packet[3]); | |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
129 |
0 | 130 break; |
131 | |
132 case MONITOR_CMD_DEPRESS: | |
133 if (packet.length == 2) | |
134 depress(packet[1]); | |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
135 |
0 | 136 break; |
137 | |
155 | 138 case MONITOR_CMD_SHOWURL: |
139 if (packet.length > 1) | |
140 showUrl(packet, 1); | |
141 | |
142 break; | |
143 | |
173
5f26d0ba6abd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
172
diff
changeset
|
144 case MONITOR_CMD_SWITCHSESSION: |
172
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
145 if (packet.length == 1) |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
146 switchSession(); |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
147 |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
148 break; |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
149 |
205
f86f1e37b504
add cursor request command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
176
diff
changeset
|
150 case MONITOR_CMD_CURSORREQUEST: |
f86f1e37b504
add cursor request command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
176
diff
changeset
|
151 if (packet.length == 1) |
f86f1e37b504
add cursor request command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
176
diff
changeset
|
152 cursorRequest(); |
f86f1e37b504
add cursor request command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
176
diff
changeset
|
153 |
f86f1e37b504
add cursor request command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
176
diff
changeset
|
154 break; |
f86f1e37b504
add cursor request command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
176
diff
changeset
|
155 |
0 | 156 default: |
157 break; | |
158 } | |
159 } | |
160 catch (IOException e) { | |
161 if (!is_closing) Log.e(TAG, "exception in MyReader.run()", e); | |
162 | |
163 break; | |
164 } | |
165 } | |
166 } | |
167 } | |
168 | |
169 class MyServiceConnection implements ServiceConnection { | |
170 public void onServiceConnected(ComponentName className, IBinder service) { | |
171 bound = service; | |
172 Log.i(TAG, "bound to service"); | |
173 | |
174 try { | |
175 InetAddress serverAddr = InetAddress.getByName(LOCALHOST); | |
176 monitor_socket = new Socket(serverAddr, MONITORPORT); | |
177 monitor_in = monitor_socket.getInputStream(); | |
178 monitor_out = monitor_socket.getOutputStream(); | |
179 Log.i(TAG, "connected to monitor socket, send init " + init); | |
180 monitor_reader = new MyReader(monitor_in); | |
181 monitor_reader.start(); | |
182 String x = " " + init; | |
183 monitorWrite(MONITOR_CMD_INIT, x.toCharArray()); | |
15
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
184 char [] c; |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
185 |
15
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
186 while (true) { |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
187 c = pending_commands.poll(); |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
188 |
15
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
189 if (c == null) break; |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
190 |
15
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
191 monitorWrite(c[1], c); |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
192 } |
0 | 193 } |
194 catch (IOException e) { | |
195 Log.e(TAG, "exception in onServiceConnected()", e); | |
196 } | |
197 } | |
198 public void onServiceDisconnected(ComponentName classNam) { | |
199 bound = null; | |
200 Log.i(TAG, "unbound from service"); | |
201 } | |
202 }; | |
203 | |
204 | |
172
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
205 public TerminalMonitor(Context parent, vt320 buffer, View view, HostBean host, String init) { |
0 | 206 this.parent = parent; |
207 this.buffer = buffer; | |
208 this.view = view; | |
172
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
209 this.host = host; |
0 | 210 this.init = init; |
211 // setup the windows->android keymapping | |
19
b3d0d806cbe2
cleaner url for MS vk_ key documentation
Carl Byington <carl@five-ten-sg.com>
parents:
18
diff
changeset
|
212 // http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731 |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
213 keymap = new HashMap<Integer, Integer>(); |
113
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
214 keymap.put(0x08, vt320.KEY_BACK_SPACE); // vk_back |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
215 keymap.put(0x09, vt320.KEY_TAB); // vk_tab |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
216 keymap.put(0x0d, vt320.KEY_ENTER); // vk_return |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
217 keymap.put(0x1b, vt320.KEY_ESCAPE); // vk_escape |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
218 keymap.put(0x21, vt320.KEY_PAGE_UP); // vk_prior |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
219 keymap.put(0x22, vt320.KEY_PAGE_DOWN); // vk_next |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
220 keymap.put(0x23, vt320.KEY_END); // vk_end |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
221 keymap.put(0x24, vt320.KEY_HOME); // vk_home |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
222 keymap.put(0x25, vt320.KEY_LEFT); // vk_left |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
223 keymap.put(0x26, vt320.KEY_UP); // vk_up |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
224 keymap.put(0x27, vt320.KEY_RIGHT); // vk_right |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
225 keymap.put(0x28, vt320.KEY_DOWN); // vk_down |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
226 keymap.put(0x2d, vt320.KEY_INSERT); // vk_insert |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
227 keymap.put(0x2e, vt320.KEY_DELETE); // vk_delete |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
228 keymap.put(0x70, vt320.KEY_F1); // vk_f1 |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
229 keymap.put(0x71, vt320.KEY_F2); // vk_f2 |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
230 keymap.put(0x72, vt320.KEY_F3); // vk_f3 |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
231 keymap.put(0x73, vt320.KEY_F4); // vk_f4 |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
232 keymap.put(0x74, vt320.KEY_F5); // vk_f5 |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
233 keymap.put(0x75, vt320.KEY_F6); // vk_f6 |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
234 keymap.put(0x76, vt320.KEY_F7); // vk_f7 |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
235 keymap.put(0x77, vt320.KEY_F8); // vk_f8 |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
236 keymap.put(0x78, vt320.KEY_F9); // vk_f9 |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
237 keymap.put(0x79, vt320.KEY_F10); // vk_f10 |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
238 keymap.put(0x7a, vt320.KEY_F11); // vk_f11 |
cb3b9b660b3d
depress() keys from the terminal monitor go straight thru buffer.keyPressed() rather than detour though the key listener
Carl Byington <carl@five-ten-sg.com>
parents:
112
diff
changeset
|
239 keymap.put(0x7b, vt320.KEY_F12); // vk_f12 |
176 | 240 keymap.put(0x7c, vt320.KEY_F13); // vk_f13 |
241 keymap.put(0x7d, vt320.KEY_F14); // vk_f14 | |
242 keymap.put(0x7e, vt320.KEY_F15); // vk_f15 | |
243 keymap.put(0x7f, vt320.KEY_F16); // vk_f16 | |
244 keymap.put(0x80, vt320.KEY_F17); // vk_f17 | |
245 keymap.put(0x81, vt320.KEY_F18); // vk_f18 | |
246 keymap.put(0x82, vt320.KEY_F19); // vk_f19 | |
247 keymap.put(0x83, vt320.KEY_F20); // vk_f20 | |
248 keymap.put(0x84, vt320.KEY_F21); // vk_f21 | |
249 keymap.put(0x85, vt320.KEY_F22); // vk_f22 | |
250 keymap.put(0x86, vt320.KEY_F23); // vk_f23 | |
251 keymap.put(0x87, vt320.KEY_F24); // vk_f24 | |
0 | 252 // bind to the monitor service |
253 Intent intent = new Intent("com.five_ten_sg.connectbot.monitor.MonitorService"); | |
254 parent.bindService(intent, monitor_connection, Context.BIND_AUTO_CREATE); | |
255 Log.i(TAG, "constructor"); | |
256 } | |
257 | |
258 | |
259 public void Disconnect() { | |
260 if (monitor_reader != null) monitor_reader.closing(); | |
261 | |
262 try { | |
263 if (monitor_out != null) monitor_out.close(); | |
264 | |
265 if (monitor_in != null) monitor_in.close(); | |
266 | |
267 if (monitor_socket != null) monitor_socket.close(); | |
268 | |
269 Log.i(TAG, "disconnected from monitor socket"); | |
270 } | |
271 catch (IOException e) { | |
272 Log.e(TAG, "exception in Disconnect() closing sockets", e); | |
273 } | |
274 | |
275 monitor_reader = null; | |
276 monitor_out = null; | |
277 monitor_in = null; | |
278 monitor_socket = null; | |
279 | |
280 if (bound != null) parent.unbindService(monitor_connection); | |
281 | |
282 monitor_connection = null; | |
283 } | |
284 | |
285 | |
286 public char[] bytesToChars(byte[] b, int len) { | |
287 char[] c = new char[len >> 1]; | |
288 int bp = 0; | |
289 | |
290 for (int i = 0; i < c.length; i++) { | |
291 byte b1 = b[bp++]; | |
292 byte b2 = b[bp++]; | |
293 c[i] = (char)(((b1 & 0x00FF) << 8) + (b2 & 0x00FF)); | |
294 } | |
295 | |
296 return c; | |
297 } | |
298 | |
299 | |
300 public byte[] charsToBytes(char[] c) { | |
301 byte[] b = new byte[c.length << 1]; | |
302 int bp = 0; | |
303 | |
304 for (int i = 0; i < c.length; i++) { | |
305 b[bp++] = (byte)((c[i] & 0xff00) >> 8); | |
306 b[bp++] = (byte)(c[i] & 0x00ff); | |
307 } | |
308 | |
309 return b; | |
310 } | |
311 | |
312 | |
313 public synchronized void monitorWrite(char cmd, char[] c) { | |
314 try { | |
315 if (monitor_out != null) { | |
316 c[0] = (char)(c.length - 1); // number of chars following | |
317 c[1] = cmd; | |
318 //Log.i(TAG, String.format("sending %d command", (int)cmd)); | |
319 monitor_out.write(charsToBytes(c)); | |
320 } | |
15
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
321 else { |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
322 c[1] = cmd; |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
323 pending_commands.put(c); |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
324 } |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
325 } |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
326 catch (InterruptedException e) { |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
327 Log.e(TAG, "exception in monitorWrite()", e); |
0 | 328 } |
329 catch (IOException e) { | |
330 Log.i(TAG, "exception in monitorWrite(), monitor died or closed the socket", e); | |
331 | |
332 try { | |
333 monitor_out.close(); | |
334 } | |
335 catch (IOException ee) { | |
336 Log.e(TAG, "exception in monitorWrite() closing output stream", ee); | |
337 } | |
338 | |
339 monitor_out = null; | |
340 } | |
341 }; | |
342 | |
227
2dd627df4dfb
delay testChanged() by 10ms for async transports; sendScreen resets watch area to the entire screen
Carl Byington <carl@five-ten-sg.com>
parents:
212
diff
changeset
|
343 public void resetWatch() { |
2dd627df4dfb
delay testChanged() by 10ms for async transports; sendScreen resets watch area to the entire screen
Carl Byington <carl@five-ten-sg.com>
parents:
212
diff
changeset
|
344 start_line = 0; |
2dd627df4dfb
delay testChanged() by 10ms for async transports; sendScreen resets watch area to the entire screen
Carl Byington <carl@five-ten-sg.com>
parents:
212
diff
changeset
|
345 end_line = 500; |
2dd627df4dfb
delay testChanged() by 10ms for async transports; sendScreen resets watch area to the entire screen
Carl Byington <carl@five-ten-sg.com>
parents:
212
diff
changeset
|
346 start_column = 0; |
2dd627df4dfb
delay testChanged() by 10ms for async transports; sendScreen resets watch area to the entire screen
Carl Byington <carl@five-ten-sg.com>
parents:
212
diff
changeset
|
347 end_column = 500; |
2dd627df4dfb
delay testChanged() by 10ms for async transports; sendScreen resets watch area to the entire screen
Carl Byington <carl@five-ten-sg.com>
parents:
212
diff
changeset
|
348 }; |
2dd627df4dfb
delay testChanged() by 10ms for async transports; sendScreen resets watch area to the entire screen
Carl Byington <carl@five-ten-sg.com>
parents:
212
diff
changeset
|
349 |
0 | 350 public void sendScreen(char cmd) { |
351 char lines = (char)(buffer.height & 0x0000ffff); | |
352 char columns = (char)(buffer.width & 0x0000ffff); | |
353 char[] arg = new char[4 + lines * columns]; | |
354 arg[2] = lines; | |
355 arg[3] = columns; | |
356 int base = 4; | |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
357 |
0 | 358 for (int i = 0; i < lines; i++) { |
359 System.arraycopy(buffer.charArray[buffer.screenBase + i], 0, arg, base, columns); | |
360 base += columns; | |
361 } | |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
362 |
0 | 363 monitorWrite(cmd, arg); |
227
2dd627df4dfb
delay testChanged() by 10ms for async transports; sendScreen resets watch area to the entire screen
Carl Byington <carl@five-ten-sg.com>
parents:
212
diff
changeset
|
364 resetWatch(); |
0 | 365 } |
366 | |
367 public synchronized void activate() { | |
368 sendScreen(MONITOR_CMD_ACTIVATE); | |
235
ea49747c5447
activate needs to send a cursor update
Carl Byington <carl@five-ten-sg.com>
parents:
229
diff
changeset
|
369 cursorMoved(CURSOR_SCREEN_CHANGE); |
0 | 370 } |
371 | |
148
69333ca1563c
add ptt button p2 preference
Carl Byington <carl@five-ten-sg.com>
parents:
147
diff
changeset
|
372 public synchronized void keyState(boolean down) { |
0 | 373 char[] arg = new char[3]; |
148
69333ca1563c
add ptt button p2 preference
Carl Byington <carl@five-ten-sg.com>
parents:
147
diff
changeset
|
374 arg[2] = (char)((down) ? 1 : 0); |
147
1350adb077b1
monitor key state tracking
Carl Byington <carl@five-ten-sg.com>
parents:
145
diff
changeset
|
375 monitorWrite(MONITOR_CMD_KEYSTATE, arg); |
0 | 376 } |
377 | |
378 public synchronized void cursorMove(int l, int c) { | |
229
594101a0876a
add why argument on cursor updates
Carl Byington <carl@five-ten-sg.com>
parents:
227
diff
changeset
|
379 if ((to_line != l) || (to_column != c)) moved = true; |
17
02717d15de9b
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
16
diff
changeset
|
380 to_line = l; |
16
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
381 to_column = c; |
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
382 } |
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
383 |
229
594101a0876a
add why argument on cursor updates
Carl Byington <carl@five-ten-sg.com>
parents:
227
diff
changeset
|
384 public void cursorMoved(char why) { |
594101a0876a
add why argument on cursor updates
Carl Byington <carl@five-ten-sg.com>
parents:
227
diff
changeset
|
385 char[] arg = new char[5]; |
18
49fc5fba28f3
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
386 arg[2] = (char)(to_line & 0x0000ffff); |
49fc5fba28f3
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
387 arg[3] = (char)(to_column & 0x0000ffff); |
229
594101a0876a
add why argument on cursor updates
Carl Byington <carl@five-ten-sg.com>
parents:
227
diff
changeset
|
388 arg[4] = why; |
0 | 389 monitorWrite(MONITOR_CMD_CURSORMOVE, arg); |
229
594101a0876a
add why argument on cursor updates
Carl Byington <carl@five-ten-sg.com>
parents:
227
diff
changeset
|
390 moved = false; |
594101a0876a
add why argument on cursor updates
Carl Byington <carl@five-ten-sg.com>
parents:
227
diff
changeset
|
391 } |
594101a0876a
add why argument on cursor updates
Carl Byington <carl@five-ten-sg.com>
parents:
227
diff
changeset
|
392 |
594101a0876a
add why argument on cursor updates
Carl Byington <carl@five-ten-sg.com>
parents:
227
diff
changeset
|
393 public void testMoved() { |
594101a0876a
add why argument on cursor updates
Carl Byington <carl@five-ten-sg.com>
parents:
227
diff
changeset
|
394 if (moved) cursorMoved(CURSOR_USER_KEY); |
0 | 395 } |
396 | |
397 public synchronized void testChanged() { | |
398 if (modified) { | |
399 modified = false; | |
400 sendScreen(MONITOR_CMD_SCREENCHANGE); | |
229
594101a0876a
add why argument on cursor updates
Carl Byington <carl@five-ten-sg.com>
parents:
227
diff
changeset
|
401 cursorMoved(CURSOR_SCREEN_CHANGE); |
594101a0876a
add why argument on cursor updates
Carl Byington <carl@five-ten-sg.com>
parents:
227
diff
changeset
|
402 } |
594101a0876a
add why argument on cursor updates
Carl Byington <carl@five-ten-sg.com>
parents:
227
diff
changeset
|
403 else { |
594101a0876a
add why argument on cursor updates
Carl Byington <carl@five-ten-sg.com>
parents:
227
diff
changeset
|
404 if (moved) cursorMoved(CURSOR_SCREEN_CHANGE); |
16
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
405 } |
0 | 406 } |
407 | |
408 public synchronized void screenChanged(int llow, int lhigh, int clow, int chigh) { | |
409 if ((start_line <= lhigh) && (llow <= end_line) && (start_column <= chigh) && (clow <= end_column)) { | |
410 modified = true; | |
411 } | |
412 } | |
413 | |
414 public synchronized void screenChanged(int l, int c) { | |
415 screenChanged(l, l, c, c); | |
416 } | |
417 | |
418 public synchronized void setField(int l, int c, char[] data, int offset) { | |
419 Log.i(TAG, "setField()"); | |
101 | 420 char[] da = new char[data.length - offset]; |
210
af235340fcde
use array copy rather than loops
Carl Byington <carl@five-ten-sg.com>
parents:
205
diff
changeset
|
421 System.arraycopy(data, offset, da, 0, data.length-offset); |
211
7e4352b53f24
unsigned short -1 is 65535
Carl Byington <carl@five-ten-sg.com>
parents:
210
diff
changeset
|
422 if (l > 60000) l = -1; |
7e4352b53f24
unsigned short -1 is 65535
Carl Byington <carl@five-ten-sg.com>
parents:
210
diff
changeset
|
423 if (c > 60000) c = -1; |
100 | 424 buffer.setField(l, c, da); |
0 | 425 } |
426 | |
155 | 427 public synchronized void showUrl(char [] data, int offset) { |
428 Log.i(TAG, "setField()"); | |
429 char[] da = new char[data.length - offset]; | |
210
af235340fcde
use array copy rather than loops
Carl Byington <carl@five-ten-sg.com>
parents:
205
diff
changeset
|
430 System.arraycopy(data, offset, da, 0, data.length-offset); |
155 | 431 String url = new String(da); |
432 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); | |
433 parent.startActivity(intent); | |
434 } | |
435 | |
0 | 436 public synchronized void getField(int l, int c, int len) { |
437 Log.i(TAG, "getField()"); | |
438 char[] arg2 = new char[4 + len]; | |
439 arg2[2] = (char)(l & 0x0000ffff); | |
440 arg2[3] = (char)(c & 0x0000ffff); | |
441 int base = 4; | |
442 System.arraycopy(buffer.charArray[buffer.screenBase + l], c, arg2, base, len); | |
443 monitorWrite(MONITOR_CMD_FIELDVALUE, arg2); | |
444 } | |
445 | |
446 public synchronized void screenWatch(int l, int c, int len) { | |
447 Log.i(TAG, "screenWatch()"); | |
448 start_line = l; | |
449 end_line = l; | |
450 start_column = c; | |
451 end_column = c + len - 1; | |
452 } | |
453 | |
454 public synchronized void depress(int vk_key) { | |
455 Log.i(TAG, String.format("depress() %d", vk_key)); | |
456 Integer x = keymap.get(new Integer(vk_key)); | |
175
2a7199ad90be
send cursor movement caused by user keystrokes to the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
174
diff
changeset
|
457 if (x != null) buffer.keyDepressed(x, ' ', 0); |
0 | 458 } |
172
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
459 |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
460 public synchronized void switchSession() { |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
461 Log.i(TAG, "switchSession()"); |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
462 Intent intent = new Intent(parent, ConsoleActivity.class); |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
463 intent.setAction(Intent.ACTION_VIEW); |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
464 intent.setData(host.getUri()); |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
465 parent.startActivity(intent); |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
466 } |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
467 |
205
f86f1e37b504
add cursor request command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
176
diff
changeset
|
468 |
f86f1e37b504
add cursor request command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
176
diff
changeset
|
469 public synchronized void cursorRequest() { |
f86f1e37b504
add cursor request command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
176
diff
changeset
|
470 Log.i(TAG, "cursorRequest()"); |
229
594101a0876a
add why argument on cursor updates
Carl Byington <carl@five-ten-sg.com>
parents:
227
diff
changeset
|
471 cursorMoved(CURSOR_REQUESTED); |
205
f86f1e37b504
add cursor request command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
176
diff
changeset
|
472 } |
f86f1e37b504
add cursor request command to the TE
Carl Byington <carl@five-ten-sg.com>
parents:
176
diff
changeset
|
473 |
0 | 474 } |