cleaned impl by switching to multimap

This commit is contained in:
Adrian Cole 2012-01-30 00:29:41 +08:00
parent 6dda77b5c2
commit ef97bef786
3 changed files with 129 additions and 159 deletions

View File

@ -21,7 +21,6 @@ package org.jclouds.virtualbox.functions;
import static com.google.common.base.Predicates.in; import static com.google.common.base.Predicates.in;
import static com.google.common.base.Throwables.propagate; import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Iterables.any; import static com.google.common.collect.Iterables.any;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Lists.partition; import static com.google.common.collect.Lists.partition;
import static org.jclouds.compute.reference.ComputeServiceConstants.COMPUTE_LOGGER; import static org.jclouds.compute.reference.ComputeServiceConstants.COMPUTE_LOGGER;
import static org.jclouds.virtualbox.settings.KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP_LIST; import static org.jclouds.virtualbox.settings.KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP_LIST;
@ -35,11 +34,8 @@ import org.jclouds.logging.Logger;
import org.virtualbox_4_1.ISession; import org.virtualbox_4_1.ISession;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
class SendScancodes implements Function<ISession, Void> { class SendScancodes implements Function<ISession, Void> {
static final ImmutableSet<Integer> SPECIAL_SCANCODES = ImmutableSet.copyOf(concat(SPECIAL_KEYBOARD_BUTTON_MAP_LIST
.values()));
@Resource @Resource
@Named(COMPUTE_LOGGER) @Named(COMPUTE_LOGGER)
@ -59,7 +55,7 @@ class SendScancodes implements Function<ISession, Void> {
long codesSent = iSession.getConsole().getKeyboard().putScancodes(maxOrLess); long codesSent = iSession.getConsole().getKeyboard().putScancodes(maxOrLess);
logger.debug("List of scancodes sent: ", maxOrLess); logger.debug("List of scancodes sent: ", maxOrLess);
assert (codesSent == maxOrLess.size()); assert (codesSent == maxOrLess.size());
if (any(maxOrLess, in(SPECIAL_SCANCODES))) { if (any(maxOrLess, in(SPECIAL_KEYBOARD_BUTTON_MAP_LIST.values()))) {
sleepOrPropagateInterrupt(300); sleepOrPropagateInterrupt(300);
} else { } else {
sleepOrPropagateInterrupt(50); sleepOrPropagateInterrupt(50);

View File

@ -19,6 +19,7 @@
package org.jclouds.virtualbox.functions; package org.jclouds.virtualbox.functions;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import org.jclouds.virtualbox.settings.KeyboardScancodes; import org.jclouds.virtualbox.settings.KeyboardScancodes;
@ -44,7 +45,7 @@ public class StringToKeyCode implements Function<String, List<Integer>> {
private List<Integer> transformStandardCharacterIntoKeycodes(String s) { private List<Integer> transformStandardCharacterIntoKeycodes(String s) {
List<Integer> values = new ArrayList<Integer>(); List<Integer> values = new ArrayList<Integer>();
for (String digit : Splitter.fixedLength(1).split(s)) { for (String digit : Splitter.fixedLength(1).split(s)) {
List<Integer> hex = KeyboardScancodes.NORMAL_KEYBOARD_BUTTON_MAP_LIST.get(digit); Collection<Integer> hex = KeyboardScancodes.NORMAL_KEYBOARD_BUTTON_MAP_LIST.get(digit);
if (hex != null) if (hex != null)
values.addAll(hex); values.addAll(hex);
} }
@ -55,7 +56,7 @@ public class StringToKeyCode implements Function<String, List<Integer>> {
private List<Integer> transforSpecialCharIntoKeycodes(String s) { private List<Integer> transforSpecialCharIntoKeycodes(String s) {
List<Integer> values = new ArrayList<Integer>(); List<Integer> values = new ArrayList<Integer>();
for (String special : s.split("<")) { for (String special : s.split("<")) {
List<Integer> value = KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP_LIST.get("<" + special); Collection<Integer> value = KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP_LIST.get("<" + special);
if (value != null) if (value != null)
values.addAll(value); values.addAll(value);
} }

View File

@ -19,174 +19,147 @@
package org.jclouds.virtualbox.settings; package org.jclouds.virtualbox.settings;
import java.util.ArrayList; import com.google.common.collect.ImmutableMultimap;
import java.util.Collections; import com.google.common.collect.Multimap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* @see <a href="http://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html" />
*
*/
public class KeyboardScancodes { public class KeyboardScancodes {
// http://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html public static final Multimap<String, Integer> NORMAL_KEYBOARD_BUTTON_MAP_LIST = ImmutableMultimap
public static final Map<String, List<Integer>> SPECIAL_KEYBOARD_BUTTON_MAP_LIST = createSpecialCodeMap(); .<String, Integer> builder()
public static final Map<String, List<Integer>> NORMAL_KEYBOARD_BUTTON_MAP_LIST = createNormalCodeMap();
public static CharToIntegersMapBuilder builder() { .putAll("1", 0x02, 0x82)
return new CharToIntegersMapBuilder(); .putAll("2", 0x03, 0x83)
} .putAll("3", 0x04, 0x84)
.putAll("4", 0x05, 0x85)
.putAll("5", 0x06, 0x86)
.putAll("6", 0x07, 0x87)
.putAll("7", 0x08, 0x88)
.putAll("8", 0x09, 0x89)
.putAll("9", 0x0a, 0x8a)
.putAll("0", 0x0b, 0x8b)
private static Map<String, List<Integer>> createNormalCodeMap() { .putAll("-", 0x0c, 0x8c)
Map<String, List<Integer>> alphaToHex = KeyboardScancodes.builder() .putAll("=", 0x0d, 0x8d)
.put("1", 0x02, 0x82) .putAll("Tab", 0x0f, 0x8f)
.put("2", 0x03, 0x83) .putAll("q", 0x10, 0x90)
.put("3", 0x04, 0x84) .putAll("w", 0x11, 0x91)
.put("4", 0x05, 0x85) .putAll("e", 0x12, 0x92)
.put("5", 0x06, 0x86) .putAll("r", 0x13, 0x93)
.put("6", 0x07, 0x87) .putAll("t", 0x14, 0x94)
.put("7", 0x08, 0x88) .putAll("y", 0x15, 0x95)
.put("8", 0x09, 0x89) .putAll("u", 0x16, 0x96)
.put("9", 0x0a, 0x8a) .putAll("i", 0x17, 0x97)
.put("0", 0x0b, 0x8b) .putAll("o", 0x18, 0x98)
.putAll("p", 0x19, 0x99)
.put("-", 0x0c, 0x8c) .putAll("Q", 0x2a, 0x10, 0xaa)
.put("=", 0x0d, 0x8d) .putAll("W", 0x2a, 0x11, 0xaa)
.put("Tab", 0x0f, 0x8f) .putAll("E", 0x2a, 0x12, 0xaa)
.put("q", 0x10, 0x90) .putAll("R", 0x2a, 0x13, 0xaa)
.put("w", 0x11, 0x91) .putAll("T", 0x2a, 0x14, 0xaa)
.put("e", 0x12, 0x92) .putAll("Y", 0x2a, 0x15, 0xaa)
.put("r", 0x13, 0x93) .putAll("U", 0x2a, 0x16, 0xaa)
.put("t", 0x14, 0x94) .putAll("I", 0x2a, 0x17, 0xaa)
.put("y", 0x15, 0x95) .putAll("O", 0x2a, 0x18, 0xaa)
.put("u", 0x16, 0x96) .putAll("P", 0x2a, 0x19, 0xaa)
.put("i", 0x17, 0x97)
.put("o", 0x18, 0x98)
.put("p", 0x19, 0x99)
.put("Q", 0x2a, 0x10, 0xaa) .putAll("a", 0x1e, 0x9e)
.put("W", 0x2a, 0x11, 0xaa) .putAll("s", 0x1f, 0x9f)
.put("E", 0x2a, 0x12, 0xaa) .putAll("d", 0x20, 0xa0)
.put("R", 0x2a, 0x13, 0xaa) .putAll("f", 0x21, 0xa1)
.put("T", 0x2a, 0x14, 0xaa) .putAll("g", 0x22, 0xa2)
.put("Y", 0x2a, 0x15, 0xaa) .putAll("h", 0x23, 0xa3)
.put("U", 0x2a, 0x16, 0xaa) .putAll("j", 0x24, 0xa4)
.put("I", 0x2a, 0x17, 0xaa) .putAll("k", 0x25, 0xa5)
.put("O", 0x2a, 0x18, 0xaa) .putAll("l", 0x26, 0xa6)
.put("P", 0x2a, 0x19, 0xaa)
.put("a", 0x1e, 0x9e) .putAll("A", 0x2a, 0x1e, 0xaa, 0x9e)
.put("s", 0x1f, 0x9f) .putAll("S", 0x2a, 0x1f, 0xaa, 0x9f)
.put("d", 0x20, 0xa0) .putAll("D", 0x2a, 0x20, 0xaa, 0xa0)
.put("f", 0x21, 0xa1) .putAll("F", 0x2a, 0x21, 0xaa, 0xa1)
.put("g", 0x22, 0xa2) .putAll("G", 0x2a, 0x22, 0xaa, 0xa2)
.put("h", 0x23, 0xa3) .putAll("H", 0x2a, 0x23, 0xaa, 0xa3)
.put("j", 0x24, 0xa4) .putAll("J", 0x2a, 0x24, 0xaa, 0xa4)
.put("k", 0x25, 0xa5) .putAll("K", 0x2a, 0x25, 0xaa, 0xa5)
.put("l", 0x26, 0xa6) .putAll("L", 0x2a, 0x26, 0xaa, 0xa6)
.put("A", 0x2a, 0x1e, 0xaa, 0x9e) .putAll(") ", 0x27, 0xa7)
.put("S", 0x2a, 0x1f, 0xaa, 0x9f) .putAll("\"", 0x2a, 0x28, 0xaa, 0xa8)
.put("D", 0x2a, 0x20, 0xaa, 0xa0) .putAll("\"", 0x28, 0xa8)
.put("F", 0x2a, 0x21, 0xaa, 0xa1) .putAll("\\", 0x2b, 0xab)
.put("G", 0x2a, 0x22, 0xaa, 0xa2) .putAll("|", 0x2a, 0x2b, 0xaa, 0x8b)
.put("H", 0x2a, 0x23, 0xaa, 0xa3) .putAll("[", 0x1a, 0x9a)
.put("J", 0x2a, 0x24, 0xaa, 0xa4) .putAll("", 0x1b, 0x9b)
.put("K", 0x2a, 0x25, 0xaa, 0xa5) .putAll("<", 0x2a, 0x33, 0xaa, 0xb3)
.put("L", 0x2a, 0x26, 0xaa, 0xa6) .putAll(">", 0x2a, 0x34, 0xaa, 0xb4)
.putAll("$", 0x2a, 0x05, 0xaa, 0x85)
.putAll("+", 0x2a, 0x0d, 0xaa, 0x8d)
.put(") ", 0x27, 0xa7) .putAll("z", 0x2c, 0xac)
.put("\"", 0x2a, 0x28, 0xaa, 0xa8) .putAll("x", 0x2d, 0xad)
.put("\"", 0x28, 0xa8) .putAll("c", 0x2e, 0xae)
.put("\\", 0x2b, 0xab) .putAll("v", 0x2f, 0xaf)
.put("|", 0x2a, 0x2b, 0xaa, 0x8b) .putAll("b", 0x30, 0xb0)
.put("[", 0x1a, 0x9a) .putAll("n", 0x31, 0xb1)
.put("", 0x1b, 0x9b) .putAll("m", 0x32, 0xb2)
.put("<", 0x2a, 0x33, 0xaa, 0xb3) .putAll("Z", 0x2a, 0x2c, 0xaa, 0xac)
.put(">", 0x2a, 0x34, 0xaa, 0xb4) .putAll("X", 0x2a, 0x2d, 0xaa, 0xad)
.put("$", 0x2a, 0x05, 0xaa, 0x85) .putAll("C", 0x2a, 0x2e, 0xaa, 0xae)
.put("+", 0x2a, 0x0d, 0xaa, 0x8d) .putAll("V", 0x2a, 0x2f, 0xaa, 0xaf)
.putAll("B", 0x2a, 0x30, 0xaa, 0xb0)
.putAll("N", 0x2a, 0x31, 0xaa, 0xb1)
.putAll("M", 0x2a, 0x32, 0xaa, 0xb2)
.put("z", 0x2c, 0xac) .putAll(",", 0x33, 0xb3)
.put("x", 0x2d, 0xad) .putAll(".", 0x34, 0xb4)
.put("c", 0x2e, 0xae) .putAll("/", 0x35, 0xb5)
.put("v", 0x2f, 0xaf) .putAll(":", 0x2a, 0x27, 0xaa, 0xa7)
.put("b", 0x30, 0xb0) .putAll("%", 0x2a, 0x06, 0xaa, 0x86)
.put("n", 0x31, 0xb1) .putAll("_", 0x2a, 0x0c, 0xaa, 0x8c)
.put("m", 0x32, 0xb2) .putAll("&", 0x2a, 0x08, 0xaa, 0x88)
.put("Z", 0x2a, 0x2c, 0xaa, 0xac) .putAll("(", 0x2a, 0x0a, 0xaa, 0x8a)
.put("X", 0x2a, 0x2d, 0xaa, 0xad) .putAll(")", 0x2a, 0x0b, 0xaa, 0x8b)
.put("C", 0x2a, 0x2e, 0xaa, 0xae)
.put("V", 0x2a, 0x2f, 0xaa, 0xaf)
.put("B", 0x2a, 0x30, 0xaa, 0xb0)
.put("N", 0x2a, 0x31, 0xaa, 0xb1)
.put("M", 0x2a, 0x32, 0xaa, 0xb2)
.put(",", 0x33, 0xb3)
.put(".", 0x34, 0xb4)
.put("/", 0x35, 0xb5)
.put(":", 0x2a, 0x27, 0xaa, 0xa7)
.put("%", 0x2a, 0x06, 0xaa, 0x86)
.put("_", 0x2a, 0x0c, 0xaa, 0x8c)
.put("&", 0x2a, 0x08, 0xaa, 0x88)
.put("(", 0x2a, 0x0a, 0xaa, 0x8a)
.put(")", 0x2a, 0x0b, 0xaa, 0x8b)
.build(); .build();
return Collections.unmodifiableMap(alphaToHex);
}
private static Map<String, List<Integer>> createSpecialCodeMap() { public static final Multimap<String, Integer> SPECIAL_KEYBOARD_BUTTON_MAP_LIST = ImmutableMultimap
Map<String, List<Integer>> special = KeyboardScancodes .<String, Integer> builder()
.builder()
.put("<Enter>", 0x1c, 0x9c)
.put("<Backspace>", 0x0e, 0x8e)
.put("<Spacebar>", 0x39, 0xb9)
.put("<Return>", 0x1c, 0x9c)
.put("<Esc>", 0x01, 0x81)
.put("<Tab>", 0x0f, 0x8f)
.put("<KillX>", 0x1d, 0x38, 0x0e)
.put("<Up>", 0x48, 0xc8) .putAll("<Enter>", 0x1c, 0x9c)
.put("<Down>", 0x50, 0xd0) .putAll("<Backspace>", 0x0e, 0x8e)
.put("<PageUp>", 0x49, 0xc9) .putAll("<Spacebar>", 0x39, 0xb9)
.put("<PageDown>", 0x51, 0xd1) .putAll("<Return>", 0x1c, 0x9c)
.put("<End>", 0x4f, 0xcf) .putAll("<Esc>", 0x01, 0x81)
.put("<Insert>", 0x52, 0xd2) .putAll("<Tab>", 0x0f, 0x8f)
.put("<Delete>", 0x53, 0xd3) .putAll("<KillX>", 0x1d, 0x38, 0x0e)
.put("<Left>", 0x4b, 0xcb)
.put("<Right>", 0x4d, 0xcd)
.put("<Home>", 0x47, 0xc7)
.put("<F1>", 0x3b) .putAll("<Up>", 0x48, 0xc8)
.put("<F2>", 0x3c) .putAll("<Down>", 0x50, 0xd0)
.put("<F3>", 0x3d) .putAll("<PageUp>", 0x49, 0xc9)
.put("<F4>", 0x3e) .putAll("<PageDown>", 0x51, 0xd1)
.put("<F5>", 0x3f) .putAll("<End>", 0x4f, 0xcf)
.put("<F6>", 0x40) .putAll("<Insert>", 0x52, 0xd2)
.put("<F7>", 0x41) .putAll("<Delete>", 0x53, 0xd3)
.put("<F8>", 0x42) .putAll("<Left>", 0x4b, 0xcb)
.put("<F9>", 0x43) .putAll("<Right>", 0x4d, 0xcd)
.put("<F10>", 0x44) .putAll("<Home>", 0x47, 0xc7)
.putAll("<F1>", 0x3b)
.putAll("<F2>", 0x3c)
.putAll("<F3>", 0x3d)
.putAll("<F4>", 0x3e)
.putAll("<F5>", 0x3f)
.putAll("<F6>", 0x40)
.putAll("<F7>", 0x41)
.putAll("<F8>", 0x42)
.putAll("<F9>", 0x43)
.putAll("<F10>", 0x44)
.build(); .build();
return Collections.unmodifiableMap(special);
}
public static class CharToIntegersMapBuilder {
private Map<String, List<Integer>> mappings = new HashMap<String, List<Integer>>();
public CharToIntegersMapBuilder put(String str, int... mapping) {
List<Integer> arrayList = new ArrayList<Integer>();
for (int i : mapping) {
arrayList.add(i);
}
mappings.put(str, arrayList);
return this;
}
public Map<String, List<Integer>> build() {
return mappings;
}
} }
}