abouttreesummaryrefslogcommitdiff
diff options
context:
space:
mode:
authorJules Aguillon2025-04-15 00:05:52 +0200
committerJules Aguillon2025-04-15 00:05:52 +0200
commit460e5214a20a381af9b18273372477389927f3fc (patch)
tree184aeaaecba4794a6e1df5700131f8ae397f9e65
parent5ae3ec05e7dbf5ad023e5121a5f12900d14098a0 (diff)
downloadunexpected-keyboard-460e5214a20a381af9b18273372477389927f3fc.tar.gz
unexpected-keyboard-460e5214a20a381af9b18273372477389927f3fc.zip
Add class Modmap and test KeyModifier
-rw-r--r--srcs/juloo.keyboard2/KeyModifier.java10
-rw-r--r--srcs/juloo.keyboard2/KeyboardData.java65
-rw-r--r--srcs/juloo.keyboard2/Modmap.java33
-rw-r--r--test/juloo.keyboard2/ModmapTest.java47
4 files changed, 108 insertions, 47 deletions
diff --git a/srcs/juloo.keyboard2/KeyModifier.java b/srcs/juloo.keyboard2/KeyModifier.java
index e512521..18fe2d7 100644
--- a/srcs/juloo.keyboard2/KeyModifier.java
+++ b/srcs/juloo.keyboard2/KeyModifier.java
@@ -8,8 +8,8 @@ public final class KeyModifier
{
/** The optional modmap takes priority over modifiers usual behaviors. Set to
[null] to disable. */
- private static KeyboardData.Modmap _modmap = null;
- public static void set_modmap(KeyboardData.Modmap mm)
+ private static Modmap _modmap = null;
+ public static void set_modmap(Modmap mm)
{
_modmap = mm;
}
@@ -190,7 +190,7 @@ public final class KeyModifier
{
if (_modmap != null)
{
- KeyValue mapped = _modmap.shift.get(k);
+ KeyValue mapped = _modmap.get(Modmap.M.Shift, k);
if (mapped != null)
return mapped;
}
@@ -215,7 +215,7 @@ public final class KeyModifier
{
if (_modmap != null)
{
- KeyValue mapped = _modmap.fn.get(k);
+ KeyValue mapped = _modmap.get(Modmap.M.Fn, k);
if (mapped != null)
return mapped;
}
@@ -289,7 +289,7 @@ public final class KeyModifier
{
if (_modmap != null)
{
- KeyValue mapped = _modmap.ctrl.get(k);
+ KeyValue mapped = _modmap.get(Modmap.M.Ctrl, k);
// Do not return the modified character right away, first turn it into a
// key event.
if (mapped != null)
diff --git a/srcs/juloo.keyboard2/KeyboardData.java b/srcs/juloo.keyboard2/KeyboardData.java
index e4a7506..f5cc87e 100644
--- a/srcs/juloo.keyboard2/KeyboardData.java
+++ b/srcs/juloo.keyboard2/KeyboardData.java
@@ -260,7 +260,7 @@ public final class KeyboardData
case "modmap":
if (modmap != null)
throw error(parser, "Multiple '<modmap>' are not allowed");
- modmap = Modmap.parse(parser);
+ modmap = parse_modmap(parser);
break;
default:
throw error(parser, "Expecting tag <row>, got <" + parser.getName() + ">");
@@ -555,53 +555,34 @@ public final class KeyboardData
}
}
- public static class Modmap
+ public static Modmap parse_modmap(XmlPullParser parser) throws Exception
{
- public final Map<KeyValue, KeyValue> shift;
- public final Map<KeyValue, KeyValue> fn;
- public final Map<KeyValue, KeyValue> ctrl;
-
- public Modmap(Map<KeyValue, KeyValue> s, Map<KeyValue, KeyValue> f, Map<KeyValue, KeyValue> c)
- {
- shift = s;
- fn = f;
- ctrl = c;
- }
-
- public static Modmap parse(XmlPullParser parser) throws Exception
+ Modmap mm = new Modmap();
+ while (next_tag(parser))
{
- HashMap<KeyValue, KeyValue> shift = new HashMap<KeyValue, KeyValue>();
- HashMap<KeyValue, KeyValue> fn = new HashMap<KeyValue, KeyValue>();
- HashMap<KeyValue, KeyValue> ctrl = new HashMap<KeyValue, KeyValue>();
- while (next_tag(parser))
+ Modmap.M m;
+ switch (parser.getName())
{
- switch (parser.getName())
- {
- case "shift":
- parse_mapping(parser, shift);
- break;
- case "fn":
- parse_mapping(parser, fn);
- break;
- case "ctrl":
- parse_mapping(parser, ctrl);
- break;
- default:
- throw error(parser, "Expecting tag <shift> or <fn>, got <" + parser.getName() + ">");
- }
+ case "shift": m = Modmap.M.Shift; break;
+ case "fn": m = Modmap.M.Fn; break;
+ case "ctrl": m = Modmap.M.Ctrl; break;
+ default:
+ throw error(parser, "Expecting tag <shift> or <fn>, got <" +
+ parser.getName() + ">");
}
-
- return new Modmap(shift, fn, ctrl);
+ parse_modmap_mapping(parser, mm, m);
}
+ return mm;
+ }
- private static void parse_mapping(XmlPullParser parser, Map<KeyValue, KeyValue> dst) throws Exception
- {
- KeyValue a = KeyValue.getKeyByName(parser.getAttributeValue(null, "a"));
- KeyValue b = KeyValue.getKeyByName(parser.getAttributeValue(null, "b"));
- while (parser.next() != XmlPullParser.END_TAG)
- continue;
- dst.put(a, b);
- }
+ private static void parse_modmap_mapping(XmlPullParser parser, Modmap mm,
+ Modmap.M m) throws Exception
+ {
+ KeyValue a = KeyValue.getKeyByName(parser.getAttributeValue(null, "a"));
+ KeyValue b = KeyValue.getKeyByName(parser.getAttributeValue(null, "b"));
+ while (parser.next() != XmlPullParser.END_TAG)
+ continue;
+ mm.add(m, a, b);
}
/** Position of a key on the layout. */
diff --git a/srcs/juloo.keyboard2/Modmap.java b/srcs/juloo.keyboard2/Modmap.java
new file mode 100644
index 0000000..77ee64d
--- /dev/null
+++ b/srcs/juloo.keyboard2/Modmap.java
@@ -0,0 +1,33 @@
+package juloo.keyboard2;
+
+import java.lang.reflect.Array;
+import java.util.Map;
+import java.util.TreeMap;
+
+/** Stores key combinations that are applied by [KeyModifier]. */
+public final class Modmap
+{
+ public enum M { Shift, Fn, Ctrl }
+
+ Map<KeyValue, KeyValue>[] _map;
+
+ public Modmap()
+ {
+ _map = (Map<KeyValue, KeyValue>[])Array.newInstance(TreeMap.class,
+ M.values().length);
+ }
+
+ public void add(M m, KeyValue a, KeyValue b)
+ {
+ int i = m.ordinal();
+ if (_map[i] == null)
+ _map[i] = new TreeMap<KeyValue, KeyValue>();
+ _map[i].put(a, b);
+ }
+
+ public KeyValue get(M m, KeyValue a)
+ {
+ Map<KeyValue, KeyValue> mm = _map[m.ordinal()];
+ return (mm == null) ? null : mm.get(a);
+ }
+}
diff --git a/test/juloo.keyboard2/ModmapTest.java b/test/juloo.keyboard2/ModmapTest.java
new file mode 100644
index 0000000..3457c3b
--- /dev/null
+++ b/test/juloo.keyboard2/ModmapTest.java
@@ -0,0 +1,47 @@
+package juloo.keyboard2;
+
+import android.view.KeyEvent;
+import juloo.keyboard2.*;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class ModmapTest
+{
+ public ModmapTest() {}
+
+ @Test
+ public void test()
+ {
+ Modmap mm = new Modmap();
+ mm.add(Modmap.M.Shift, KeyValue.getKeyByName("a"), KeyValue.getKeyByName("b"));
+ mm.add(Modmap.M.Fn, KeyValue.getKeyByName("c"), KeyValue.getKeyByName("d"));
+ Utils.apply(mm, "a", KeyValue.Modifier.SHIFT, "b");
+ Utils.apply(mm, "a", KeyValue.Modifier.FN, "æ");
+ Utils.apply(mm, "c", KeyValue.Modifier.FN, "d");
+ }
+
+ @Test
+ public void keyevent_mappings()
+ {
+ Modmap mm = new Modmap();
+ mm.add(Modmap.M.Ctrl, KeyValue.getKeyByName("љ"), KeyValue.getKeyByName("љ:q"));
+ Utils.apply(mm, "a", KeyValue.Modifier.CTRL, KeyValue.getKeyByName("a").withKeyevent(29));
+ Utils.apply(mm, "љ", KeyValue.Modifier.CTRL, KeyValue.getKeyByName("љ").withKeyevent(45));
+ }
+
+ static class Utils
+ {
+ static void apply(Modmap mm, String a, KeyValue.Modifier mod, String expected)
+ {
+ apply(mm, a, mod, KeyValue.getKeyByName(expected));
+ }
+
+ static void apply(Modmap mm, String a, KeyValue.Modifier mod, KeyValue expected)
+ {
+ KeyModifier.set_modmap(mm);
+ KeyValue b = KeyModifier.modify(KeyValue.getKeyByName(a), mod);
+ KeyModifier.set_modmap(null);
+ assertEquals(b, expected);
+ }
+ }
+}