Skip to content

Commit

Permalink
Merge pull request irssiconnectbot#16 from pfn/master
Browse files Browse the repository at this point in the history
Implement external keyboard support (ESC, TAB, CTRL and ALT); also fix StrictMode
  • Loading branch information
kruton committed Jul 1, 2011
2 parents 5c5353e + db57beb commit 80e8791
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 18 deletions.
33 changes: 27 additions & 6 deletions src/org/connectbot/ConsoleActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ public class ConsoleActivity extends Activity {

private SharedPreferences prefs = null;

// determines whether or not menuitem accelerators are bound
// otherwise they collide with an external keyboard's CTRL-char
private boolean hardKeyboard = false;

protected Uri requested;

protected ClipboardManager clipboard;
Expand Down Expand Up @@ -255,9 +259,20 @@ protected void hideAllPrompts() {
booleanPromptGroup.setVisibility(View.GONE);
}

// more like configureLaxMode -- enable network IO on UI thread
private void configureStrictMode() {
try {
Class.forName("android.os.StrictMode");
StrictModeSetup.run();
} catch (ClassNotFoundException e) {
}
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
configureStrictMode();
hardKeyboard = getResources().getConfiguration().keyboard ==
Configuration.KEYBOARD_QWERTY;

this.setContentView(R.layout.act_console);

Expand Down Expand Up @@ -620,7 +635,8 @@ public boolean onCreateOptionsMenu(Menu menu) {
menu.setQwertyMode(true);

disconnect = menu.add(R.string.list_host_disconnect);
disconnect.setAlphabeticShortcut('w');
if (hardKeyboard)
disconnect.setAlphabeticShortcut('w');
if (!sessionOpen && disconnected)
disconnect.setTitle(R.string.console_menu_close);
disconnect.setEnabled(activeTerminal);
Expand All @@ -637,7 +653,8 @@ public boolean onMenuItemClick(MenuItem item) {
});

copy = menu.add(R.string.console_menu_copy);
copy.setAlphabeticShortcut('c');
if (hardKeyboard)
copy.setAlphabeticShortcut('c');
copy.setIcon(android.R.drawable.ic_menu_set_as);
copy.setEnabled(activeTerminal);
copy.setOnMenuItemClickListener(new OnMenuItemClickListener() {
Expand All @@ -661,7 +678,8 @@ public boolean onMenuItemClick(MenuItem item) {
});

paste = menu.add(R.string.console_menu_paste);
paste.setAlphabeticShortcut('v');
if (hardKeyboard)
paste.setAlphabeticShortcut('v');
paste.setIcon(android.R.drawable.ic_menu_edit);
paste.setEnabled(clipboard.hasText() && sessionOpen);
paste.setOnMenuItemClickListener(new OnMenuItemClickListener() {
Expand All @@ -679,7 +697,8 @@ public boolean onMenuItemClick(MenuItem item) {
});

portForward = menu.add(R.string.console_menu_portforwards);
portForward.setAlphabeticShortcut('f');
if (hardKeyboard)
portForward.setAlphabeticShortcut('f');
portForward.setIcon(android.R.drawable.ic_menu_manage);
portForward.setEnabled(sessionOpen && canForwardPorts);
portForward.setOnMenuItemClickListener(new OnMenuItemClickListener() {
Expand All @@ -695,7 +714,8 @@ public boolean onMenuItemClick(MenuItem item) {
});

urlscan = menu.add(R.string.console_menu_urlscan);
urlscan.setAlphabeticShortcut('u');
if (hardKeyboard)
urlscan.setAlphabeticShortcut('u');
urlscan.setIcon(android.R.drawable.ic_menu_search);
urlscan.setEnabled(activeTerminal);
urlscan.setOnMenuItemClickListener(new OnMenuItemClickListener() {
Expand All @@ -720,7 +740,8 @@ public boolean onMenuItemClick(MenuItem item) {
});

resize = menu.add(R.string.console_menu_resize);
resize.setAlphabeticShortcut('s');
if (hardKeyboard)
resize.setAlphabeticShortcut('s');
resize.setIcon(android.R.drawable.ic_menu_crop);
resize.setEnabled(sessionOpen);
resize.setOnMenuItemClickListener(new OnMenuItemClickListener() {
Expand Down
23 changes: 23 additions & 0 deletions src/org/connectbot/StrictModeSetup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* ConnectBot: simple, powerful, open-source SSH client for Android
* Copyright 2007 Kenny Root, Jeffrey Sharkey
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.connectbot;
import android.os.StrictMode;
public class StrictModeSetup {
public static void run() {
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.LAX);
}
}
69 changes: 57 additions & 12 deletions src/org/connectbot/service/TerminalKeyListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public class TerminalKeyListener implements OnKeyListener, OnSharedPreferenceCha
public final static int META_ALT_MASK = META_ALT_ON | META_ALT_LOCK;
public final static int META_SHIFT_MASK = META_SHIFT_ON | META_SHIFT_LOCK;

// backport constants from api level 11
public final static int KEYCODE_ESCAPE = 111;
public final static int HC_META_CTRL_ON = 4096;

// All the transient key codes
public final static int META_TRANSIENT = META_CTRL_ON | META_ALT_ON
| META_SHIFT_ON;
Expand Down Expand Up @@ -171,6 +175,7 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
}

int curMetaState = event.getMetaState();
final int orgMetaState = curMetaState;

if ((metaState & META_SHIFT_MASK) != 0) {
curMetaState |= KeyEvent.META_SHIFT_ON;
Expand All @@ -181,6 +186,11 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
}

int key = event.getUnicodeChar(curMetaState);
// no hard keyboard? ALT-k should pass through to below
if ((orgMetaState & KeyEvent.META_ALT_ON) != 0 &&
(!hardKeyboard || hardKeyboardHidden)) {
key = 0;
}

if ((key & KeyCharacterMap.COMBINING_ACCENT) != 0) {
mDeadKey = key & KeyCharacterMap.COMBINING_ACCENT_MASK;
Expand Down Expand Up @@ -216,18 +226,7 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
&& sendFunctionKey(keyCode))
return true;

// Support CTRL-a through CTRL-z
if (key >= 0x61 && key <= 0x7A)
key -= 0x60;
// Support CTRL-A through CTRL-_
else if (key >= 0x41 && key <= 0x5F)
key -= 0x40;
// CTRL-space sends NULL
else if (key == 0x20)
key = 0x00;
// CTRL-? sends DEL
else if (key == 0x3F)
key = 0x7F;
key = keyAsControl(key);
}

// handle pressing f-keys
Expand All @@ -246,6 +245,30 @@ && sendFunctionKey(keyCode))
return true;
}

// send ctrl and meta-keys as appropriate
if (!hardKeyboard || hardKeyboardHidden) {
int k = event.getUnicodeChar(0);
int k0 = k;
boolean sendCtrl = false;
boolean sendMeta = false;
if (k != 0) {
if ((orgMetaState & HC_META_CTRL_ON) != 0) {
k = keyAsControl(k);
if (k != k0)
sendCtrl = true;
// send F1-F10 via CTRL-1 through CTRL-0
if (!sendCtrl && sendFunctionKey(keyCode))
return true;
} else if ((orgMetaState & KeyEvent.META_ALT_ON) != 0) {
sendMeta = true;
sendEscape();
}
if (sendMeta || sendCtrl) {
bridge.transport.write(k);
return true;
}
}
}
// try handling keymode shortcuts
if (hardKeyboard && !hardKeyboardHidden &&
event.getRepeatCount() == 0) {
Expand Down Expand Up @@ -295,6 +318,12 @@ && sendFunctionKey(keyCode))

// look for special chars
switch(keyCode) {
case KEYCODE_ESCAPE:
sendEscape();
return true;
case KeyEvent.KEYCODE_TAB:
bridge.transport.write(0x09);
return true;
case KeyEvent.KEYCODE_CAMERA:

// check to see which shortcut the camera button triggers
Expand Down Expand Up @@ -421,6 +450,22 @@ && sendFunctionKey(keyCode))
return false;
}

public int keyAsControl(int key) {
// Support CTRL-a through CTRL-z
if (key >= 0x61 && key <= 0x7A)
key -= 0x60;
// Support CTRL-A through CTRL-_
else if (key >= 0x41 && key <= 0x5F)
key -= 0x40;
// CTRL-space sends NULL
else if (key == 0x20)
key = 0x00;
// CTRL-? sends DEL
else if (key == 0x3F)
key = 0x7F;
return key;
}

public void sendEscape() {
((vt320)buffer).keyTyped(vt320.KEY_ESCAPE, ' ', 0);
}
Expand Down

0 comments on commit 80e8791

Please sign in to comment.