abouttreesummaryrefslogcommitdiff
path: root/srcs/juloo.keyboard2/KeyboardData.java
diff options
context:
space:
mode:
authorJules Aguillon2023-06-03 09:37:59 +0200
committerJules Aguillon2023-06-03 09:37:59 +0200
commit22d407c46a56231efc3d1192616d7923c9228bf1 (patch)
tree7513d162d0e5152dba1547c7760275651069d0a2 /srcs/juloo.keyboard2/KeyboardData.java
parentd2a92795e982151da66ca700168f4fbc946aa4b3 (diff)
downloadunexpected-keyboard-22d407c46a56231efc3d1192616d7923c9228bf1.tar.gz
unexpected-keyboard-22d407c46a56231efc3d1192616d7923c9228bf1.zip
Per-layout shift modmap
Specify the behavior of shift for a layout. This is intended for locales that use the same alphabet but have different capital letters (eg. Bengali). The modmap is defined like this: <keyboard> <modmap> <shift a="a" b="A"/> </modmap> </keyboard>
Diffstat (limited to 'srcs/juloo.keyboard2/KeyboardData.java')
-rw-r--r--srcs/juloo.keyboard2/KeyboardData.java72
1 files changed, 62 insertions, 10 deletions
diff --git a/srcs/juloo.keyboard2/KeyboardData.java b/srcs/juloo.keyboard2/KeyboardData.java
index c008682..bc78f23 100644
--- a/srcs/juloo.keyboard2/KeyboardData.java
+++ b/srcs/juloo.keyboard2/KeyboardData.java
@@ -21,13 +21,15 @@ class KeyboardData
public final float keysWidth;
/** Total height of the keyboard. */
public final float keysHeight;
+ /** Might be null. */
+ public final Modmap modmap;
public KeyboardData mapKeys(MapKey f)
{
ArrayList<Row> rows_ = new ArrayList<Row>();
for (Row r : rows)
rows_.add(r.mapKeys(f));
- return new KeyboardData(rows_, keysWidth);
+ return new KeyboardData(rows_, keysWidth, modmap);
}
/** Add keys from the given iterator into the keyboard. Extra keys are added
@@ -47,7 +49,7 @@ class KeyboardData
for (int c = 1; c <= 4; c++)
addExtraKeys_to_row(rows, k, r, c);
}
- return new KeyboardData(rows, keysWidth);
+ return new KeyboardData(rows, keysWidth, modmap);
}
public KeyboardData addNumPad()
@@ -70,14 +72,15 @@ class KeyboardData
}
extendedRows.add(new Row(keys, row.height, row.shift));
}
- return new KeyboardData(extendedRows, compute_max_width(extendedRows));
+ return new
+ KeyboardData(extendedRows, compute_max_width(extendedRows), modmap);
}
public KeyboardData addNumberRow()
{
ArrayList<Row> rows_ = new ArrayList<Row>(this.rows);
rows_.add(0, number_row.updateWidth(keysWidth));
- return new KeyboardData(rows_, keysWidth);
+ return new KeyboardData(rows_, keysWidth, modmap);
}
public Key findKeyWithValue(KeyValue kv)
@@ -173,12 +176,25 @@ class KeyboardData
boolean add_bottom_row = attribute_bool(parser, "bottom_row", true);
float specified_kw = attribute_float(parser, "width", 0f);
ArrayList<Row> rows = new ArrayList<Row>();
- while (expect_tag(parser, "row"))
- rows.add(Row.parse(parser));
+ Modmap modmap = null;
+ while (next_tag(parser))
+ {
+ switch (parser.getName())
+ {
+ case "row":
+ rows.add(Row.parse(parser));
+ break;
+ case "modmap":
+ modmap = Modmap.parse(parser);
+ break;
+ default:
+ throw new Exception("Unknown tag: " + parser.getName());
+ }
+ }
float kw = (specified_kw != 0f) ? specified_kw : compute_max_width(rows);
if (add_bottom_row)
rows.add(bottom_row.updateWidth(kw));
- return new KeyboardData(rows, kw);
+ return new KeyboardData(rows, kw, modmap);
}
private static float compute_max_width(List<Row> rows)
@@ -196,12 +212,13 @@ class KeyboardData
return Row.parse(parser);
}
- protected KeyboardData(List<Row> rows_, float kw)
+ protected KeyboardData(List<Row> rows_, float kw, Modmap mm)
{
float kh = 0.f;
for (Row r : rows_)
kh += r.height + r.shift;
rows = rows_;
+ modmap = mm;
keysWidth = kw;
keysHeight = kh;
}
@@ -415,10 +432,37 @@ class KeyboardData
}
}
+ public static class Modmap
+ {
+ public final Map<KeyValue, KeyValue> shift;
+
+ public Modmap(Map<KeyValue, KeyValue> s)
+ {
+ shift = s;
+ }
+
+ public static Modmap parse(XmlPullParser parser) throws Exception
+ {
+ HashMap<KeyValue, KeyValue> shift = new HashMap<KeyValue, KeyValue>();
+ while (expect_tag(parser, "shift"))
+ parse_mapping(parser, shift);
+ return new Modmap(shift);
+ }
+
+ 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);
+ }
+ }
+
/** Parsing utils */
/** Returns [false] on [END_DOCUMENT] or [END_TAG], [true] otherwise. */
- private static boolean expect_tag(XmlPullParser parser, String name) throws Exception
+ private static boolean next_tag(XmlPullParser parser) throws Exception
{
int status;
do
@@ -428,8 +472,16 @@ class KeyboardData
return false;
}
while (status != XmlPullParser.START_TAG);
+ return true;
+ }
+
+ /** Returns [false] on [END_DOCUMENT] or [END_TAG], [true] otherwise. */
+ private static boolean expect_tag(XmlPullParser parser, String name) throws Exception
+ {
+ if (!next_tag(parser))
+ return false;
if (!parser.getName().equals(name))
- throw new Exception("Unknow tag: " + parser.getName());
+ throw new Exception("Unknown tag: " + parser.getName());
return true;
}