Mercurial > 510Connectbot
annotate src/com/five_ten_sg/connectbot/service/TerminalMonitor.java @ 192:f0b9ea35711a
read deployment.connections on startup for global preferences also
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Wed, 02 Jul 2014 18:06:59 -0700 |
parents | 6e07ea418b90 |
children | f86f1e37b504 |
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; |
0 | 42 |
43 private static final int MONITORPORT = 6000; | |
44 private static final String LOCALHOST = "127.0.0.1"; | |
45 | |
46 private Context parent = null; | |
47 private vt320 buffer = null; | |
48 private View view = null; | |
174
b010f9dc801f
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
173
diff
changeset
|
49 private HostBean host = null; |
0 | 50 private String init = null; |
51 private int start_line = 0; // monitor part of the screen for changes | |
52 private int end_line = 500; // "" | |
53 private int start_column = 0; // "" | |
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 private HashMap<Integer, Integer> keymap = null; // map MS VK_ keys to vt320 virtual keys |
0 | 60 private IBinder bound = null; |
61 private Socket monitor_socket = null; | |
62 private InputStream monitor_in = null; | |
63 private OutputStream monitor_out = null; | |
64 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
|
65 private BlockingQueue<char[]> pending_commands = new ArrayBlockingQueue<char[]>(100); |
0 | 66 private MyServiceConnection monitor_connection = new MyServiceConnection(); |
67 | |
68 class MyReader extends Thread { | |
69 private InputStream monitor_in; | |
70 private byte[] b; | |
71 private boolean is_closing = false; | |
72 | |
73 public MyReader(InputStream monitor_in) { | |
74 this.monitor_in = monitor_in; | |
75 b = new byte[100]; | |
76 Log.i(TAG, "MyReader constructor"); | |
77 } | |
78 | |
79 public void closing() { | |
80 is_closing = true; | |
81 } | |
82 | |
83 private char[] forceRead(int len) throws IOException { | |
84 int len2 = len * 2; | |
85 int off = 0; | |
86 | |
87 if (b.length < len2) b = new byte[len2]; | |
88 | |
89 while (off < len2) { | |
90 int l = monitor_in.read(b, off, len2 - off); | |
91 | |
92 if (l < 0) throw new IOException("eof"); | |
93 | |
94 off += l; | |
95 } | |
96 | |
97 return bytesToChars(b, len2); | |
98 } | |
99 | |
100 public void run() { | |
101 while (true) { | |
102 try { | |
103 char[] len = forceRead(1); | |
104 char[] packet = forceRead(len[0]); | |
105 char cmd = packet[0]; | |
106 Log.i(TAG, String.format("received %d command", (int)cmd)); | |
107 | |
108 switch (cmd) { | |
109 case MONITOR_CMD_SETFIELD: | |
153
3ca280646f2d
allow zero length setfield
Carl Byington <carl@five-ten-sg.com>
parents:
148
diff
changeset
|
110 if (packet.length >= 3) |
0 | 111 setField(packet[1], packet[2], packet, 3); |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
112 |
0 | 113 break; |
114 | |
115 case MONITOR_CMD_GETFIELD: | |
116 if (packet.length == 4) | |
117 getField(packet[1], packet[2], packet[3]); | |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
118 |
0 | 119 break; |
120 | |
121 case MONITOR_CMD_SCREENWATCH: | |
122 if (packet.length == 4) | |
123 screenWatch(packet[1], packet[2], packet[3]); | |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
124 |
0 | 125 break; |
126 | |
127 case MONITOR_CMD_DEPRESS: | |
128 if (packet.length == 2) | |
129 depress(packet[1]); | |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
130 |
0 | 131 break; |
132 | |
155 | 133 case MONITOR_CMD_SHOWURL: |
134 if (packet.length > 1) | |
135 showUrl(packet, 1); | |
136 | |
137 break; | |
138 | |
173
5f26d0ba6abd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
172
diff
changeset
|
139 case MONITOR_CMD_SWITCHSESSION: |
172
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
140 if (packet.length == 1) |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
141 switchSession(); |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
142 |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
143 break; |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
144 |
0 | 145 default: |
146 break; | |
147 } | |
148 } | |
149 catch (IOException e) { | |
150 if (!is_closing) Log.e(TAG, "exception in MyReader.run()", e); | |
151 | |
152 break; | |
153 } | |
154 } | |
155 } | |
156 } | |
157 | |
158 class MyServiceConnection implements ServiceConnection { | |
159 public void onServiceConnected(ComponentName className, IBinder service) { | |
160 bound = service; | |
161 Log.i(TAG, "bound to service"); | |
162 | |
163 try { | |
164 InetAddress serverAddr = InetAddress.getByName(LOCALHOST); | |
165 monitor_socket = new Socket(serverAddr, MONITORPORT); | |
166 monitor_in = monitor_socket.getInputStream(); | |
167 monitor_out = monitor_socket.getOutputStream(); | |
168 Log.i(TAG, "connected to monitor socket, send init " + init); | |
169 monitor_reader = new MyReader(monitor_in); | |
170 monitor_reader.start(); | |
171 String x = " " + init; | |
172 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
|
173 char [] c; |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
174 |
15
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
175 while (true) { |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
176 c = pending_commands.poll(); |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
177 |
15
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
178 if (c == null) break; |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
179 |
15
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
180 monitorWrite(c[1], c); |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
181 } |
0 | 182 } |
183 catch (IOException e) { | |
184 Log.e(TAG, "exception in onServiceConnected()", e); | |
185 } | |
186 } | |
187 public void onServiceDisconnected(ComponentName classNam) { | |
188 bound = null; | |
189 Log.i(TAG, "unbound from service"); | |
190 } | |
191 }; | |
192 | |
193 | |
172
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
194 public TerminalMonitor(Context parent, vt320 buffer, View view, HostBean host, String init) { |
0 | 195 this.parent = parent; |
196 this.buffer = buffer; | |
197 this.view = view; | |
172
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
198 this.host = host; |
0 | 199 this.init = init; |
200 // 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
|
201 // 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
|
202 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
|
203 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
|
204 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
|
205 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
|
206 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
|
207 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
|
208 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
|
209 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
|
210 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
|
211 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
|
212 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
|
213 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
|
214 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
|
215 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
|
216 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
|
217 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
|
218 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
|
219 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
|
220 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
|
221 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
|
222 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
|
223 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
|
224 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
|
225 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
|
226 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
|
227 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
|
228 keymap.put(0x7b, vt320.KEY_F12); // vk_f12 |
176 | 229 keymap.put(0x7c, vt320.KEY_F13); // vk_f13 |
230 keymap.put(0x7d, vt320.KEY_F14); // vk_f14 | |
231 keymap.put(0x7e, vt320.KEY_F15); // vk_f15 | |
232 keymap.put(0x7f, vt320.KEY_F16); // vk_f16 | |
233 keymap.put(0x80, vt320.KEY_F17); // vk_f17 | |
234 keymap.put(0x81, vt320.KEY_F18); // vk_f18 | |
235 keymap.put(0x82, vt320.KEY_F19); // vk_f19 | |
236 keymap.put(0x83, vt320.KEY_F20); // vk_f20 | |
237 keymap.put(0x84, vt320.KEY_F21); // vk_f21 | |
238 keymap.put(0x85, vt320.KEY_F22); // vk_f22 | |
239 keymap.put(0x86, vt320.KEY_F23); // vk_f23 | |
240 keymap.put(0x87, vt320.KEY_F24); // vk_f24 | |
0 | 241 // bind to the monitor service |
242 Intent intent = new Intent("com.five_ten_sg.connectbot.monitor.MonitorService"); | |
243 parent.bindService(intent, monitor_connection, Context.BIND_AUTO_CREATE); | |
244 Log.i(TAG, "constructor"); | |
245 } | |
246 | |
247 | |
248 public void Disconnect() { | |
249 if (monitor_reader != null) monitor_reader.closing(); | |
250 | |
251 try { | |
252 if (monitor_out != null) monitor_out.close(); | |
253 | |
254 if (monitor_in != null) monitor_in.close(); | |
255 | |
256 if (monitor_socket != null) monitor_socket.close(); | |
257 | |
258 Log.i(TAG, "disconnected from monitor socket"); | |
259 } | |
260 catch (IOException e) { | |
261 Log.e(TAG, "exception in Disconnect() closing sockets", e); | |
262 } | |
263 | |
264 monitor_reader = null; | |
265 monitor_out = null; | |
266 monitor_in = null; | |
267 monitor_socket = null; | |
268 | |
269 if (bound != null) parent.unbindService(monitor_connection); | |
270 | |
271 monitor_connection = null; | |
272 } | |
273 | |
274 | |
275 public char[] bytesToChars(byte[] b, int len) { | |
276 char[] c = new char[len >> 1]; | |
277 int bp = 0; | |
278 | |
279 for (int i = 0; i < c.length; i++) { | |
280 byte b1 = b[bp++]; | |
281 byte b2 = b[bp++]; | |
282 c[i] = (char)(((b1 & 0x00FF) << 8) + (b2 & 0x00FF)); | |
283 } | |
284 | |
285 return c; | |
286 } | |
287 | |
288 | |
289 public byte[] charsToBytes(char[] c) { | |
290 byte[] b = new byte[c.length << 1]; | |
291 int bp = 0; | |
292 | |
293 for (int i = 0; i < c.length; i++) { | |
294 b[bp++] = (byte)((c[i] & 0xff00) >> 8); | |
295 b[bp++] = (byte)(c[i] & 0x00ff); | |
296 } | |
297 | |
298 return b; | |
299 } | |
300 | |
301 | |
302 public synchronized void monitorWrite(char cmd, char[] c) { | |
303 try { | |
304 if (monitor_out != null) { | |
305 c[0] = (char)(c.length - 1); // number of chars following | |
306 c[1] = cmd; | |
307 //Log.i(TAG, String.format("sending %d command", (int)cmd)); | |
308 monitor_out.write(charsToBytes(c)); | |
309 } | |
15
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
310 else { |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
311 c[1] = cmd; |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
312 pending_commands.put(c); |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
313 } |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
314 } |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
315 catch (InterruptedException e) { |
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
316 Log.e(TAG, "exception in monitorWrite()", e); |
0 | 317 } |
318 catch (IOException e) { | |
319 Log.i(TAG, "exception in monitorWrite(), monitor died or closed the socket", e); | |
320 | |
321 try { | |
322 monitor_out.close(); | |
323 } | |
324 catch (IOException ee) { | |
325 Log.e(TAG, "exception in monitorWrite() closing output stream", ee); | |
326 } | |
327 | |
328 monitor_out = null; | |
329 } | |
330 }; | |
331 | |
332 public void sendScreen(char cmd) { | |
333 char lines = (char)(buffer.height & 0x0000ffff); | |
334 char columns = (char)(buffer.width & 0x0000ffff); | |
335 char[] arg = new char[4 + lines * columns]; | |
336 arg[2] = lines; | |
337 arg[3] = columns; | |
338 int base = 4; | |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
339 |
0 | 340 for (int i = 0; i < lines; i++) { |
341 System.arraycopy(buffer.charArray[buffer.screenBase + i], 0, arg, base, columns); | |
342 base += columns; | |
343 } | |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
344 |
0 | 345 monitorWrite(cmd, arg); |
346 } | |
347 | |
348 public synchronized void activate() { | |
349 sendScreen(MONITOR_CMD_ACTIVATE); | |
350 } | |
351 | |
148
69333ca1563c
add ptt button p2 preference
Carl Byington <carl@five-ten-sg.com>
parents:
147
diff
changeset
|
352 public synchronized void keyState(boolean down) { |
0 | 353 char[] arg = new char[3]; |
148
69333ca1563c
add ptt button p2 preference
Carl Byington <carl@five-ten-sg.com>
parents:
147
diff
changeset
|
354 arg[2] = (char)((down) ? 1 : 0); |
147
1350adb077b1
monitor key state tracking
Carl Byington <carl@five-ten-sg.com>
parents:
145
diff
changeset
|
355 monitorWrite(MONITOR_CMD_KEYSTATE, arg); |
0 | 356 } |
357 | |
358 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
|
359 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
|
360 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
|
361 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
|
362 } |
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
363 |
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
364 public void cursorMoved() { |
0 | 365 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
|
366 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
|
367 arg[3] = (char)(to_column & 0x0000ffff); |
0 | 368 monitorWrite(MONITOR_CMD_CURSORMOVE, arg); |
369 } | |
370 | |
371 public synchronized void testChanged() { | |
372 if (modified) { | |
373 modified = false; | |
374 sendScreen(MONITOR_CMD_SCREENCHANGE); | |
375 } | |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
376 |
16
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
377 if (moved) { |
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
378 moved = false; |
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
379 cursorMoved(); |
48a8daea9221
delay sending cursor move notifications until the host is quiet
Carl Byington <carl@five-ten-sg.com>
parents:
15
diff
changeset
|
380 } |
0 | 381 } |
382 | |
383 public synchronized void screenChanged(int llow, int lhigh, int clow, int chigh) { | |
384 if ((start_line <= lhigh) && (llow <= end_line) && (start_column <= chigh) && (clow <= end_column)) { | |
385 modified = true; | |
386 } | |
387 } | |
388 | |
389 public synchronized void screenChanged(int l, int c) { | |
390 screenChanged(l, l, c, c); | |
391 } | |
392 | |
393 public synchronized void setField(int l, int c, char[] data, int offset) { | |
394 Log.i(TAG, "setField()"); | |
101 | 395 char[] da = new char[data.length - offset]; |
15
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
396 int i; |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
397 |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
398 for (i = 0; i < da.length; i++) { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
101
diff
changeset
|
399 da[i] = data[i + offset]; |
15
1588e359a972
queue pending monitor commands until socket is open
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
400 } |
145
4dfa4dd791c1
testing setfield functions
Carl Byington <carl@five-ten-sg.com>
parents:
142
diff
changeset
|
401 |
100 | 402 buffer.setField(l, c, da); |
0 | 403 } |
404 | |
155 | 405 public synchronized void showUrl(char [] data, int offset) { |
406 Log.i(TAG, "setField()"); | |
407 char[] da = new char[data.length - offset]; | |
408 int i; | |
409 | |
410 for (i = 0; i < da.length; i++) { | |
411 da[i] = data[i + offset]; | |
412 } | |
413 String url = new String(da); | |
414 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); | |
415 parent.startActivity(intent); | |
416 } | |
417 | |
0 | 418 public synchronized void getField(int l, int c, int len) { |
419 Log.i(TAG, "getField()"); | |
420 char[] arg2 = new char[4 + len]; | |
421 arg2[2] = (char)(l & 0x0000ffff); | |
422 arg2[3] = (char)(c & 0x0000ffff); | |
423 int base = 4; | |
424 System.arraycopy(buffer.charArray[buffer.screenBase + l], c, arg2, base, len); | |
425 monitorWrite(MONITOR_CMD_FIELDVALUE, arg2); | |
426 } | |
427 | |
428 public synchronized void screenWatch(int l, int c, int len) { | |
429 Log.i(TAG, "screenWatch()"); | |
430 start_line = l; | |
431 end_line = l; | |
432 start_column = c; | |
433 end_column = c + len - 1; | |
434 } | |
435 | |
436 public synchronized void depress(int vk_key) { | |
437 Log.i(TAG, String.format("depress() %d", vk_key)); | |
438 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
|
439 if (x != null) buffer.keyDepressed(x, ' ', 0); |
0 | 440 } |
172
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
441 |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
442 public synchronized void switchSession() { |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
443 Log.i(TAG, "switchSession()"); |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
444 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
|
445 intent.setAction(Intent.ACTION_VIEW); |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
446 intent.setData(host.getUri()); |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
447 parent.startActivity(intent); |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
448 } |
cb9e359ea2bd
add switch session command from the monitor
Carl Byington <carl@five-ten-sg.com>
parents:
155
diff
changeset
|
449 |
0 | 450 } |