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.Throwables.propagate;
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 org.jclouds.compute.reference.ComputeServiceConstants.COMPUTE_LOGGER;
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 com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
class SendScancodes implements Function<ISession, Void> {
static final ImmutableSet<Integer> SPECIAL_SCANCODES = ImmutableSet.copyOf(concat(SPECIAL_KEYBOARD_BUTTON_MAP_LIST
.values()));
@Resource
@Named(COMPUTE_LOGGER)
@ -59,7 +55,7 @@ class SendScancodes implements Function<ISession, Void> {
long codesSent = iSession.getConsole().getKeyboard().putScancodes(maxOrLess);
logger.debug("List of scancodes sent: ", maxOrLess);
assert (codesSent == maxOrLess.size());
if (any(maxOrLess, in(SPECIAL_SCANCODES))) {
if (any(maxOrLess, in(SPECIAL_KEYBOARD_BUTTON_MAP_LIST.values()))) {
sleepOrPropagateInterrupt(300);
} else {
sleepOrPropagateInterrupt(50);

View File

@ -19,6 +19,7 @@
package org.jclouds.virtualbox.functions;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.jclouds.virtualbox.settings.KeyboardScancodes;
@ -44,7 +45,7 @@ public class StringToKeyCode implements Function<String, List<Integer>> {
private List<Integer> transformStandardCharacterIntoKeycodes(String s) {
List<Integer> values = new ArrayList<Integer>();
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)
values.addAll(hex);
}
@ -55,7 +56,7 @@ public class StringToKeyCode implements Function<String, List<Integer>> {
private List<Integer> transforSpecialCharIntoKeycodes(String s) {
List<Integer> values = new ArrayList<Integer>();
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)
values.addAll(value);
}

View File

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