Mercurial > 510Connectbot
annotate src/com/five_ten_sg/connectbot/service/TerminalMonitor.java @ 19:b3d0d806cbe2
cleaner url for MS vk_ key documentation
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Tue, 03 Jun 2014 08:48:14 -0700 |
parents | 49fc5fba28f3 |
children | 01d939969b10 |
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; | |
7 import android.os.IBinder; | |
8 import android.util.Log; | |
9 import android.view.KeyEvent; | |
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 |
23 public class TerminalMonitor { | |
24 public final static String TAG = "ConnectBot.TerminalMonitor"; | |
25 | |
26 public static final char MONITOR_CMD_INIT = 0; | |
27 public static final char MONITOR_CMD_ACTIVATE = 1; | |
28 public static final char MONITOR_CMD_HOSTDATA = 2; | |
29 public static final char MONITOR_CMD_CURSORMOVE = 3; | |
30 public static final char MONITOR_CMD_SCREENCHANGE = 4; | |
31 public static final char MONITOR_CMD_FIELDVALUE = 5; | |
32 public static final char MONITOR_CMD_SETFIELD = 5; | |
33 public static final char MONITOR_CMD_GETFIELD = 6; | |
34 public static final char MONITOR_CMD_SCREENWATCH = 7; | |
35 public static final char MONITOR_CMD_DEPRESS = 8; | |
36 | |
37 private static final int MONITORPORT = 6000; | |
38 private static final String LOCALHOST = "127.0.0.1"; | |
39 | |
40 private Context parent = null; | |
41 private vt320 buffer = null; | |
42 private TerminalKeyListener keyListener = null; | |
43 private View view = null; | |
44 private String init = null; | |
45 private int start_line = 0; // monitor part of the screen for changes | |
46 private int end_line = 500; // "" | |
47 private int start_column = 0; // "" | |
48 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
|
49 private boolean modified = false; // used to delay screen change notifications |
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
50 private boolean moved = false; // used to delay cursor moved notifications |
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
51 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
|
52 private int to_column = 0; // "" |
0 | 53 private HashMap<Integer,Integer> keymap = null; |
54 private IBinder bound = null; | |
55 private Socket monitor_socket = null; | |
56 private InputStream monitor_in = null; | |
57 private OutputStream monitor_out = null; | |
58 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
|
59 private BlockingQueue<char[]> pending_commands = new ArrayBlockingQueue<char[]>(100); |
0 | 60 private MyServiceConnection monitor_connection = new MyServiceConnection(); |
61 | |
62 class MyReader extends Thread { | |
63 private InputStream monitor_in; | |
64 private byte[] b; | |
65 private boolean is_closing = false; | |
66 | |
67 public MyReader(InputStream monitor_in) { | |
68 this.monitor_in = monitor_in; | |
69 b = new byte[100]; | |
70 Log.i(TAG, "MyReader constructor"); | |
71 } | |
72 | |
73 public void closing() { | |
74 is_closing = true; | |
75 } | |
76 | |
77 private char[] forceRead(int len) throws IOException { | |
78 int len2 = len * 2; | |
79 int off = 0; | |
80 | |
81 if (b.length < len2) b = new byte[len2]; | |
82 | |
83 while (off < len2) { | |
84 int l = monitor_in.read(b, off, len2 - off); | |
85 | |
86 if (l < 0) throw new IOException("eof"); | |
87 | |
88 off += l; | |
89 } | |
90 | |
91 return bytesToChars(b, len2); | |
92 } | |
93 | |
94 public void run() { | |
95 while (true) { | |
96 try { | |
97 char[] len = forceRead(1); | |
98 char[] packet = forceRead(len[0]); | |
99 char cmd = packet[0]; | |
100 Log.i(TAG, String.format("received %d command", (int)cmd)); | |
101 | |
102 switch (cmd) { | |
103 case MONITOR_CMD_SETFIELD: | |
104 if (packet.length >= 4) | |
105 setField(packet[1], packet[2], packet, 3); | |
106 break; | |
107 | |
108 case MONITOR_CMD_GETFIELD: | |
109 if (packet.length == 4) | |
110 getField(packet[1], packet[2], packet[3]); | |
111 break; | |
112 | |
113 case MONITOR_CMD_SCREENWATCH: | |
114 if (packet.length == 4) | |
115 screenWatch(packet[1], packet[2], packet[3]); | |
116 break; | |
117 | |
118 case MONITOR_CMD_DEPRESS: | |
119 if (packet.length == 2) | |
120 depress(packet[1]); | |
121 break; | |
122 | |
123 default: | |
124 break; | |
125 } | |
126 } | |
127 catch (IOException e) { | |
128 if (!is_closing) Log.e(TAG, "exception in MyReader.run()", e); | |
129 | |
130 break; | |
131 } | |
132 } | |
133 } | |
134 } | |
135 | |
136 class MyServiceConnection implements ServiceConnection { | |
137 public void onServiceConnected(ComponentName className, IBinder service) { | |
138 bound = service; | |
139 Log.i(TAG, "bound to service"); | |
140 | |
141 try { | |
142 InetAddress serverAddr = InetAddress.getByName(LOCALHOST); | |
143 monitor_socket = new Socket(serverAddr, MONITORPORT); | |
144 monitor_in = monitor_socket.getInputStream(); | |
145 monitor_out = monitor_socket.getOutputStream(); | |
146 Log.i(TAG, "connected to monitor socket, send init " + init); | |
147 monitor_reader = new MyReader(monitor_in); | |
148 monitor_reader.start(); | |
149 String x = " " + init; | |
150 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
|
151 char [] c; |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
152 while (true) { |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
153 c = pending_commands.poll(); |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
154 if (c == null) break; |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
155 monitorWrite(c[1], c); |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
156 } |
0 | 157 } |
158 catch (IOException e) { | |
159 Log.e(TAG, "exception in onServiceConnected()", e); | |
160 } | |
161 } | |
162 public void onServiceDisconnected(ComponentName classNam) { | |
163 bound = null; | |
164 Log.i(TAG, "unbound from service"); | |
165 } | |
166 }; | |
167 | |
168 | |
169 public TerminalMonitor(Context parent, vt320 buffer, TerminalKeyListener keyListener, View view, String init) { | |
170 this.parent = parent; | |
171 this.buffer = buffer; | |
172 this.keyListener = keyListener; | |
173 this.view = view; | |
174 this.init = init; | |
175 | |
176 // 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
|
177 // http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731 |
0 | 178 // http://developer.android.com/reference/android/view/KeyEvent.html |
179 keymap = new HashMap<Integer,Integer>(); | |
180 keymap.put(0x08, KeyEvent.KEYCODE_BACK); // vk_back | |
181 keymap.put(0x09, KeyEvent.KEYCODE_TAB); // vk_tab | |
182 keymap.put(0x0d, KeyEvent.KEYCODE_ENTER); // vk_return | |
183 keymap.put(0x1b, KeyEvent.KEYCODE_ESCAPE); // vk_escape | |
184 keymap.put(0x21, KeyEvent.KEYCODE_PAGE_UP); // vk_prior | |
185 keymap.put(0x22, KeyEvent.KEYCODE_PAGE_DOWN); // vk_next | |
186 keymap.put(0x23, KeyEvent.KEYCODE_MOVE_END); // vk_end | |
187 keymap.put(0x24, KeyEvent.KEYCODE_MOVE_HOME); // vk_home | |
188 keymap.put(0x25, KeyEvent.KEYCODE_DPAD_LEFT); // vk_left | |
189 keymap.put(0x26, KeyEvent.KEYCODE_DPAD_UP); // vk_up | |
190 keymap.put(0x27, KeyEvent.KEYCODE_DPAD_RIGHT); // vk_right | |
191 keymap.put(0x28, KeyEvent.KEYCODE_DPAD_DOWN); // vk_down | |
192 keymap.put(0x2d, KeyEvent.KEYCODE_INSERT); // vk_insert | |
193 keymap.put(0x2e, KeyEvent.KEYCODE_DEL); // vk_delete | |
194 keymap.put(0x70, KeyEvent.KEYCODE_F1); // vk_f1 | |
195 keymap.put(0x71, KeyEvent.KEYCODE_F2); // vk_f2 | |
196 keymap.put(0x72, KeyEvent.KEYCODE_F3); // vk_f3 | |
197 keymap.put(0x73, KeyEvent.KEYCODE_F4); // vk_f4 | |
198 keymap.put(0x74, KeyEvent.KEYCODE_F5); // vk_f5 | |
199 keymap.put(0x75, KeyEvent.KEYCODE_F6); // vk_f6 | |
200 keymap.put(0x76, KeyEvent.KEYCODE_F7); // vk_f7 | |
201 keymap.put(0x77, KeyEvent.KEYCODE_F8); // vk_f8 | |
202 keymap.put(0x78, KeyEvent.KEYCODE_F9); // vk_f9 | |
203 keymap.put(0x79, KeyEvent.KEYCODE_F10); // vk_f10 | |
204 keymap.put(0x7a, KeyEvent.KEYCODE_F11); // vk_f11 | |
205 keymap.put(0x7b, KeyEvent.KEYCODE_F12); // vk_f12 | |
206 | |
207 // bind to the monitor service | |
208 Intent intent = new Intent("com.five_ten_sg.connectbot.monitor.MonitorService"); | |
209 parent.bindService(intent, monitor_connection, Context.BIND_AUTO_CREATE); | |
210 Log.i(TAG, "constructor"); | |
211 } | |
212 | |
213 | |
214 public void Disconnect() { | |
215 if (monitor_reader != null) monitor_reader.closing(); | |
216 | |
217 try { | |
218 if (monitor_out != null) monitor_out.close(); | |
219 | |
220 if (monitor_in != null) monitor_in.close(); | |
221 | |
222 if (monitor_socket != null) monitor_socket.close(); | |
223 | |
224 Log.i(TAG, "disconnected from monitor socket"); | |
225 } | |
226 catch (IOException e) { | |
227 Log.e(TAG, "exception in Disconnect() closing sockets", e); | |
228 } | |
229 | |
230 monitor_reader = null; | |
231 monitor_out = null; | |
232 monitor_in = null; | |
233 monitor_socket = null; | |
234 | |
235 if (bound != null) parent.unbindService(monitor_connection); | |
236 | |
237 monitor_connection = null; | |
238 } | |
239 | |
240 | |
241 public char[] bytesToChars(byte[] b, int len) { | |
242 char[] c = new char[len >> 1]; | |
243 int bp = 0; | |
244 | |
245 for (int i = 0; i < c.length; i++) { | |
246 byte b1 = b[bp++]; | |
247 byte b2 = b[bp++]; | |
248 c[i] = (char)(((b1 & 0x00FF) << 8) + (b2 & 0x00FF)); | |
249 } | |
250 | |
251 return c; | |
252 } | |
253 | |
254 | |
255 public byte[] charsToBytes(char[] c) { | |
256 byte[] b = new byte[c.length << 1]; | |
257 int bp = 0; | |
258 | |
259 for (int i = 0; i < c.length; i++) { | |
260 b[bp++] = (byte)((c[i] & 0xff00) >> 8); | |
261 b[bp++] = (byte)(c[i] & 0x00ff); | |
262 } | |
263 | |
264 return b; | |
265 } | |
266 | |
267 | |
268 public synchronized void monitorWrite(char cmd, char[] c) { | |
269 try { | |
270 if (monitor_out != null) { | |
271 c[0] = (char)(c.length - 1); // number of chars following | |
272 c[1] = cmd; | |
273 //Log.i(TAG, String.format("sending %d command", (int)cmd)); | |
274 monitor_out.write(charsToBytes(c)); | |
275 } | |
15
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
276 else { |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
277 c[1] = cmd; |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
278 pending_commands.put(c); |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
279 } |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
280 } |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
281 catch (InterruptedException e) { |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
282 Log.e(TAG, "exception in monitorWrite()", e); |
0 | 283 } |
284 catch (IOException e) { | |
285 Log.i(TAG, "exception in monitorWrite(), monitor died or closed the socket", e); | |
286 | |
287 try { | |
288 monitor_out.close(); | |
289 } | |
290 catch (IOException ee) { | |
291 Log.e(TAG, "exception in monitorWrite() closing output stream", ee); | |
292 } | |
293 | |
294 monitor_out = null; | |
295 } | |
296 }; | |
297 | |
298 public void sendScreen(char cmd) { | |
299 char lines = (char)(buffer.height & 0x0000ffff); | |
300 char columns = (char)(buffer.width & 0x0000ffff); | |
301 char[] arg = new char[4 + lines * columns]; | |
302 arg[2] = lines; | |
303 arg[3] = columns; | |
304 int base = 4; | |
305 for (int i = 0; i < lines; i++) { | |
306 System.arraycopy(buffer.charArray[buffer.screenBase + i], 0, arg, base, columns); | |
307 base += columns; | |
308 } | |
309 monitorWrite(cmd, arg); | |
310 } | |
311 | |
312 public synchronized void activate() { | |
313 sendScreen(MONITOR_CMD_ACTIVATE); | |
314 } | |
315 | |
316 public synchronized void hostData(byte[] b) { | |
317 for (int i = 0; i < b.length; i++) { | |
318 hostData((int)b[i] & 0xff); | |
319 } | |
320 } | |
321 | |
322 public synchronized void hostData(int b) { | |
323 char[] arg = new char[3]; | |
324 arg[2] = (char)(b & 0x0000ffff); | |
325 monitorWrite(MONITOR_CMD_HOSTDATA, arg); | |
326 } | |
327 | |
328 public synchronized void cursorMove(int l, int c) { | |
16
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
329 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
|
330 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
|
331 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
|
332 } |
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
333 |
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
334 public void cursorMoved() { |
0 | 335 char[] arg = new char[4]; |
18
49fc5fba28f3
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
336 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
|
337 arg[3] = (char)(to_column & 0x0000ffff); |
0 | 338 monitorWrite(MONITOR_CMD_CURSORMOVE, arg); |
339 } | |
340 | |
341 public synchronized void testChanged() { | |
342 if (modified) { | |
343 modified = false; | |
344 sendScreen(MONITOR_CMD_SCREENCHANGE); | |
345 } | |
16
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
346 if (moved) { |
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
347 moved = false; |
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
348 cursorMoved(); |
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
349 } |
0 | 350 } |
351 | |
352 public synchronized void screenChanged(int llow, int lhigh, int clow, int chigh) { | |
353 if ((start_line <= lhigh) && (llow <= end_line) && (start_column <= chigh) && (clow <= end_column)) { | |
354 modified = true; | |
355 } | |
356 } | |
357 | |
358 public synchronized void screenChanged(int l, int c) { | |
359 screenChanged(l, l, c, c); | |
360 } | |
361 | |
362 public synchronized void setField(int l, int c, char[] data, int offset) { | |
363 Log.i(TAG, "setField()"); | |
15
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
364 byte[] b = new byte[data.length - offset]; |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
365 int i; |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
366 for (i=0; i<b.length; i++) { |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
367 b[i] = (byte)(data[i+offset] & 0x00ff); |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
368 } |
0 | 369 buffer.write(b); |
370 } | |
371 | |
372 public synchronized void getField(int l, int c, int len) { | |
373 Log.i(TAG, "getField()"); | |
374 char[] arg2 = new char[4 + len]; | |
375 arg2[2] = (char)(l & 0x0000ffff); | |
376 arg2[3] = (char)(c & 0x0000ffff); | |
377 int base = 4; | |
378 System.arraycopy(buffer.charArray[buffer.screenBase + l], c, arg2, base, len); | |
379 monitorWrite(MONITOR_CMD_FIELDVALUE, arg2); | |
380 } | |
381 | |
382 public synchronized void screenWatch(int l, int c, int len) { | |
383 Log.i(TAG, "screenWatch()"); | |
384 start_line = l; | |
385 end_line = l; | |
386 start_column = c; | |
387 end_column = c + len - 1; | |
388 } | |
389 | |
390 public synchronized void depress(int vk_key) { | |
391 Log.i(TAG, String.format("depress() %d", vk_key)); | |
392 Integer x = keymap.get(new Integer(vk_key)); | |
393 if (x != null) { | |
394 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, x.intValue()); | |
395 keyListener.onKey(view, event.getKeyCode(), event); | |
396 } | |
397 } | |
398 } |