2015-07-31 20:48:19 +02:00
|
|
|
package juloo.keyboard2;
|
|
|
|
|
2022-02-07 00:55:32 +01:00
|
|
|
import android.content.res.Resources;
|
2022-11-26 22:30:45 +01:00
|
|
|
import android.content.res.XmlResourceParser;
|
|
|
|
import android.util.Xml;
|
|
|
|
import java.io.StringReader;
|
2015-07-31 20:48:19 +02:00
|
|
|
import java.util.ArrayList;
|
2023-03-03 19:44:05 +01:00
|
|
|
import java.util.Arrays;
|
2022-02-07 00:55:32 +01:00
|
|
|
import java.util.HashMap;
|
2022-05-29 12:27:46 +02:00
|
|
|
import java.util.Iterator;
|
2021-04-15 23:23:31 +02:00
|
|
|
import java.util.List;
|
2022-02-07 00:55:32 +01:00
|
|
|
import java.util.Map;
|
2023-01-30 23:54:39 +01:00
|
|
|
import java.util.Set;
|
2022-11-26 22:16:48 +01:00
|
|
|
import java.util.function.Function;
|
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
2015-07-31 20:48:19 +02:00
|
|
|
|
|
|
|
class KeyboardData
|
|
|
|
{
|
2021-12-19 19:44:27 +01:00
|
|
|
public final List<Row> rows;
|
2022-06-06 01:02:30 +02:00
|
|
|
/** Total width of the keyboard. */
|
2021-04-29 00:59:19 +02:00
|
|
|
public final float keysWidth;
|
2022-06-06 01:02:30 +02:00
|
|
|
/** Total height of the keyboard. */
|
2021-04-29 00:59:19 +02:00
|
|
|
public final float keysHeight;
|
2023-06-03 09:37:59 +02:00
|
|
|
/** Might be null. */
|
|
|
|
public final Modmap modmap;
|
2023-06-10 10:59:25 +02:00
|
|
|
/** Might be null. */
|
|
|
|
public final String script;
|
2015-07-31 20:48:19 +02:00
|
|
|
|
2022-05-29 12:27:46 +02:00
|
|
|
public KeyboardData mapKeys(MapKey f)
|
2021-04-15 23:23:31 +02:00
|
|
|
{
|
2022-02-07 00:55:32 +01:00
|
|
|
ArrayList<Row> rows_ = new ArrayList<Row>();
|
|
|
|
for (Row r : rows)
|
2022-05-29 12:27:46 +02:00
|
|
|
rows_.add(r.mapKeys(f));
|
2023-06-10 10:59:25 +02:00
|
|
|
return new KeyboardData(rows_, keysWidth, modmap, script);
|
2021-04-15 23:23:31 +02:00
|
|
|
}
|
|
|
|
|
2022-05-29 12:27:46 +02:00
|
|
|
/** Add keys from the given iterator into the keyboard. Extra keys are added
|
|
|
|
* on the empty key4 corner of the second row, from right to left. If there's
|
|
|
|
* not enough room, key3 of the second row is tried then key2 and key1 of the
|
|
|
|
* third row. */
|
|
|
|
public KeyboardData addExtraKeys(Iterator<KeyValue> k)
|
|
|
|
{
|
|
|
|
ArrayList<Row> rows = new ArrayList<Row>(this.rows);
|
|
|
|
addExtraKeys_to_row(rows, k, 1, 4);
|
|
|
|
addExtraKeys_to_row(rows, k, 1, 3);
|
|
|
|
addExtraKeys_to_row(rows, k, 2, 2);
|
|
|
|
addExtraKeys_to_row(rows, k, 2, 1);
|
2022-09-24 15:19:45 +02:00
|
|
|
if (k.hasNext())
|
|
|
|
{
|
|
|
|
for (int r = 0; r < rows.size(); r++)
|
|
|
|
for (int c = 1; c <= 4; c++)
|
|
|
|
addExtraKeys_to_row(rows, k, r, c);
|
|
|
|
}
|
2023-06-10 10:59:25 +02:00
|
|
|
return new KeyboardData(rows, keysWidth, modmap, script);
|
2022-09-25 02:23:33 +02:00
|
|
|
}
|
|
|
|
|
2023-09-03 23:36:49 +02:00
|
|
|
public KeyboardData addNumPad(KeyboardData num_pad)
|
2022-09-25 02:23:33 +02:00
|
|
|
{
|
|
|
|
ArrayList<Row> extendedRows = new ArrayList<Row>();
|
2023-01-30 23:54:39 +01:00
|
|
|
Iterator<Row> iterNumPadRows = num_pad.rows.iterator();
|
2022-09-25 02:23:33 +02:00
|
|
|
for (Row row : rows)
|
|
|
|
{
|
|
|
|
ArrayList<KeyboardData.Key> keys = new ArrayList<Key>(row.keys);
|
|
|
|
if (iterNumPadRows.hasNext())
|
|
|
|
{
|
|
|
|
Row numPadRow = iterNumPadRows.next();
|
|
|
|
List<Key> nps = numPadRow.keys;
|
|
|
|
if (nps.size() > 0) {
|
|
|
|
float firstNumPadShift = 0.5f + keysWidth - row.keysWidth;
|
|
|
|
keys.add(nps.get(0).withShift(firstNumPadShift));
|
|
|
|
for (int i = 1; i < nps.size(); i++)
|
|
|
|
keys.add(nps.get(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extendedRows.add(new Row(keys, row.height, row.shift));
|
|
|
|
}
|
2023-06-03 09:37:59 +02:00
|
|
|
return new
|
2023-06-10 10:59:25 +02:00
|
|
|
KeyboardData(extendedRows, compute_max_width(extendedRows), modmap, script);
|
2022-05-29 12:27:46 +02:00
|
|
|
}
|
|
|
|
|
2023-01-30 23:58:47 +01:00
|
|
|
public KeyboardData addNumberRow()
|
|
|
|
{
|
2023-02-26 11:31:08 +01:00
|
|
|
ArrayList<Row> rows_ = new ArrayList<Row>(this.rows);
|
|
|
|
rows_.add(0, number_row.updateWidth(keysWidth));
|
2023-06-10 10:59:25 +02:00
|
|
|
return new KeyboardData(rows_, keysWidth, modmap, script);
|
2023-01-30 23:58:47 +01:00
|
|
|
}
|
|
|
|
|
2022-07-24 20:02:48 +02:00
|
|
|
public Key findKeyWithValue(KeyValue kv)
|
|
|
|
{
|
|
|
|
for (Row r : rows)
|
|
|
|
{
|
|
|
|
Key k = r.findKeyWithValue(kv);
|
|
|
|
if (k != null)
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-01-30 23:54:39 +01:00
|
|
|
public void getKeys(Set<KeyValue> dst)
|
|
|
|
{
|
|
|
|
for (Row r : rows)
|
|
|
|
r.getKeys(dst);
|
|
|
|
}
|
|
|
|
|
2022-05-29 12:27:46 +02:00
|
|
|
private static void addExtraKeys_to_row(ArrayList<Row> rows, final Iterator<KeyValue> extra_keys, int row_i, final int d)
|
|
|
|
{
|
2023-06-28 17:50:32 +02:00
|
|
|
if (!extra_keys.hasNext() || row_i >= rows.size())
|
2022-05-29 12:27:46 +02:00
|
|
|
return;
|
|
|
|
rows.set(row_i, rows.get(row_i).mapKeys(new MapKey(){
|
|
|
|
public Key apply(Key k) {
|
|
|
|
if (k.getKeyValue(d) == null && extra_keys.hasNext())
|
|
|
|
return k.withKeyValue(d, extra_keys.next());
|
|
|
|
else
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2023-01-30 23:54:39 +01:00
|
|
|
public static Row bottom_row;
|
2023-01-30 23:58:47 +01:00
|
|
|
public static Row number_row;
|
2023-01-30 23:54:39 +01:00
|
|
|
public static KeyboardData num_pad;
|
2022-02-07 00:55:32 +01:00
|
|
|
private static Map<Integer, KeyboardData> _layoutCache = new HashMap<Integer, KeyboardData>();
|
2015-07-31 20:48:19 +02:00
|
|
|
|
2022-11-26 22:12:40 +01:00
|
|
|
public static void init(Resources res)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2023-01-30 23:58:47 +01:00
|
|
|
bottom_row = parse_row(res.getXml(R.xml.bottom_row));
|
|
|
|
number_row = parse_row(res.getXml(R.xml.number_row));
|
2023-01-30 23:54:39 +01:00
|
|
|
num_pad = parse_keyboard(res.getXml(R.xml.numpad));
|
2022-11-26 22:12:40 +01:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-26 22:30:45 +01:00
|
|
|
/** Load a layout from a resource ID. Returns [null] on error. */
|
2022-02-07 00:55:32 +01:00
|
|
|
public static KeyboardData load(Resources res, int id)
|
|
|
|
{
|
|
|
|
KeyboardData l = _layoutCache.get(id);
|
|
|
|
if (l == null)
|
2021-12-19 19:44:27 +01:00
|
|
|
{
|
2022-02-07 00:55:32 +01:00
|
|
|
try
|
2021-12-19 19:44:27 +01:00
|
|
|
{
|
2022-11-26 22:30:45 +01:00
|
|
|
XmlResourceParser parser = res.getXml(id);
|
|
|
|
l = parse_keyboard(parser);
|
|
|
|
parser.close();
|
2022-02-07 00:55:32 +01:00
|
|
|
_layoutCache.put(id, l);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
2021-12-19 19:44:27 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-07 00:55:32 +01:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2022-11-26 22:30:45 +01:00
|
|
|
/** Load a layout from a string. Returns [null] on error. */
|
|
|
|
public static KeyboardData load_string(String src)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
XmlPullParser parser = Xml.newPullParser();
|
|
|
|
parser.setInput(new StringReader(src));
|
|
|
|
return parse_keyboard(parser);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-26 22:16:48 +01:00
|
|
|
private static KeyboardData parse_keyboard(XmlPullParser parser) throws Exception
|
2022-02-07 00:55:32 +01:00
|
|
|
{
|
|
|
|
if (!expect_tag(parser, "keyboard"))
|
|
|
|
throw new Exception("Empty layout file");
|
2023-01-30 23:54:39 +01:00
|
|
|
boolean add_bottom_row = attribute_bool(parser, "bottom_row", true);
|
2022-11-26 22:16:48 +01:00
|
|
|
float specified_kw = attribute_float(parser, "width", 0f);
|
2023-06-10 10:59:25 +02:00
|
|
|
String script = parser.getAttributeValue(null, "script");
|
2022-02-07 00:55:32 +01:00
|
|
|
ArrayList<Row> rows = new ArrayList<Row>();
|
2023-06-03 09:37:59 +02:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
2022-10-24 00:17:55 +02:00
|
|
|
float kw = (specified_kw != 0f) ? specified_kw : compute_max_width(rows);
|
2023-01-30 23:54:39 +01:00
|
|
|
if (add_bottom_row)
|
|
|
|
rows.add(bottom_row.updateWidth(kw));
|
2023-06-10 10:59:25 +02:00
|
|
|
return new KeyboardData(rows, kw, modmap, script);
|
2022-02-13 13:20:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static float compute_max_width(List<Row> rows)
|
|
|
|
{
|
|
|
|
float w = 0.f;
|
|
|
|
for (Row r : rows)
|
|
|
|
w = Math.max(w, r.keysWidth);
|
|
|
|
return w;
|
2021-12-19 19:44:27 +01:00
|
|
|
}
|
2015-07-31 20:48:19 +02:00
|
|
|
|
2023-01-30 23:58:47 +01:00
|
|
|
private static Row parse_row(XmlPullParser parser) throws Exception
|
2021-04-13 02:00:08 +02:00
|
|
|
{
|
2022-02-07 00:55:32 +01:00
|
|
|
if (!expect_tag(parser, "row"))
|
2023-01-30 23:58:47 +01:00
|
|
|
throw new Exception("Failed to parse row");
|
2022-02-07 00:55:32 +01:00
|
|
|
return Row.parse(parser);
|
|
|
|
}
|
|
|
|
|
2023-06-10 10:59:25 +02:00
|
|
|
protected KeyboardData(List<Row> rows_, float kw, Modmap mm, String sc)
|
2022-02-07 00:55:32 +01:00
|
|
|
{
|
|
|
|
float kh = 0.f;
|
|
|
|
for (Row r : rows_)
|
|
|
|
kh += r.height + r.shift;
|
|
|
|
rows = rows_;
|
2023-06-03 09:37:59 +02:00
|
|
|
modmap = mm;
|
2023-06-10 10:59:25 +02:00
|
|
|
script = sc;
|
2022-02-07 00:55:32 +01:00
|
|
|
keysWidth = kw;
|
|
|
|
keysHeight = kh;
|
2021-04-13 02:00:08 +02:00
|
|
|
}
|
|
|
|
|
2021-12-19 19:44:27 +01:00
|
|
|
public static class Row
|
|
|
|
{
|
2021-04-29 00:59:19 +02:00
|
|
|
public final List<Key> keys;
|
2022-06-06 01:02:30 +02:00
|
|
|
/** Height of the row, without 'shift'. */
|
2021-04-29 00:59:19 +02:00
|
|
|
public final float height;
|
2022-06-06 01:02:30 +02:00
|
|
|
/** Extra empty space on the top. */
|
2021-04-29 00:59:19 +02:00
|
|
|
public final float shift;
|
2022-06-06 01:02:30 +02:00
|
|
|
/** Total width of the row. */
|
2022-09-25 02:23:33 +02:00
|
|
|
public final float keysWidth;
|
2015-07-31 20:48:19 +02:00
|
|
|
|
2022-02-07 00:55:32 +01:00
|
|
|
protected Row(List<Key> keys_, float h, float s)
|
2021-04-15 23:23:31 +02:00
|
|
|
{
|
|
|
|
float kw = 0.f;
|
2021-04-29 00:59:19 +02:00
|
|
|
for (Key k : keys_) kw += k.width + k.shift;
|
|
|
|
keys = keys_;
|
|
|
|
height = h;
|
|
|
|
shift = s;
|
|
|
|
keysWidth = kw;
|
2021-04-15 23:23:31 +02:00
|
|
|
}
|
2015-07-31 20:48:19 +02:00
|
|
|
|
2022-11-26 22:16:48 +01:00
|
|
|
public static Row parse(XmlPullParser parser) throws Exception
|
2021-12-19 19:44:27 +01:00
|
|
|
{
|
2021-04-15 23:23:31 +02:00
|
|
|
ArrayList<Key> keys = new ArrayList<Key>();
|
2021-12-19 19:44:27 +01:00
|
|
|
int status;
|
2022-11-26 22:16:48 +01:00
|
|
|
float h = attribute_float(parser, "height", 1f);
|
|
|
|
float shift = attribute_float(parser, "shift", 0f);
|
2022-02-07 00:55:32 +01:00
|
|
|
while (expect_tag(parser, "key"))
|
|
|
|
keys.add(Key.parse(parser));
|
2021-04-29 00:59:19 +02:00
|
|
|
return new Row(keys, h, shift);
|
2021-12-19 19:44:27 +01:00
|
|
|
}
|
2015-07-31 20:48:19 +02:00
|
|
|
|
2023-01-30 23:54:39 +01:00
|
|
|
public void getKeys(Set<KeyValue> dst)
|
|
|
|
{
|
|
|
|
for (Key k : keys)
|
|
|
|
k.getKeys(dst);
|
|
|
|
}
|
|
|
|
|
2022-05-29 12:27:46 +02:00
|
|
|
public Row mapKeys(MapKey f)
|
2021-04-15 23:23:31 +02:00
|
|
|
{
|
2021-04-29 00:59:19 +02:00
|
|
|
ArrayList<Key> keys_ = new ArrayList<Key>();
|
|
|
|
for (Key k : keys)
|
2022-05-29 12:27:46 +02:00
|
|
|
keys_.add(f.apply(k));
|
2021-04-29 00:59:19 +02:00
|
|
|
return new Row(keys_, height, shift);
|
2021-04-15 23:23:31 +02:00
|
|
|
}
|
2022-02-13 13:20:22 +01:00
|
|
|
|
|
|
|
/** Change the width of every keys so that the row is 's' units wide. */
|
|
|
|
public Row updateWidth(float newWidth)
|
|
|
|
{
|
2022-05-29 12:27:46 +02:00
|
|
|
final float s = newWidth / keysWidth;
|
|
|
|
return mapKeys(new MapKey(){
|
|
|
|
public Key apply(Key k) { return k.scaleWidth(s); }
|
|
|
|
});
|
2022-02-13 13:20:22 +01:00
|
|
|
}
|
2022-07-24 20:02:48 +02:00
|
|
|
|
|
|
|
public Key findKeyWithValue(KeyValue kv)
|
|
|
|
{
|
|
|
|
for (Key k : keys)
|
|
|
|
if (k.hasValue(kv))
|
|
|
|
return k;
|
|
|
|
return null;
|
|
|
|
}
|
2021-12-19 19:44:27 +01:00
|
|
|
}
|
2021-04-15 23:23:31 +02:00
|
|
|
|
|
|
|
public static class Key
|
|
|
|
{
|
2023-03-03 19:44:05 +01:00
|
|
|
/**
|
|
|
|
* 1 7 2
|
|
|
|
* 5 0 6
|
|
|
|
* 3 8 4
|
2021-04-15 23:23:31 +02:00
|
|
|
*/
|
2023-03-05 23:08:35 +01:00
|
|
|
public final KeyValue[] keys;
|
|
|
|
/** Pack flags for every key values. Flags are: [F_LOC]. */
|
|
|
|
private final int keysflags;
|
2022-06-06 01:02:30 +02:00
|
|
|
/** Key width in relative unit. */
|
2021-04-15 23:23:31 +02:00
|
|
|
public final float width;
|
2022-06-06 01:02:30 +02:00
|
|
|
/** Extra empty space on the left of the key. */
|
2021-04-17 23:56:51 +02:00
|
|
|
public final float shift;
|
2023-01-22 23:03:30 +01:00
|
|
|
/** Keys 2 and 3 are repeated as the finger moves laterally on the key.
|
|
|
|
Used for the left and right arrow keys on the space bar. */
|
|
|
|
public final boolean slider;
|
|
|
|
/** String printed on the keys. It has no other effect. */
|
2022-10-24 00:27:49 +02:00
|
|
|
public final String indication;
|
2021-04-15 23:23:31 +02:00
|
|
|
|
2023-03-05 23:08:35 +01:00
|
|
|
/** Whether a key was declared with the 'loc' prefix. */
|
|
|
|
public static final int F_LOC = 1;
|
|
|
|
public static final int ALL_FLAGS = F_LOC;
|
|
|
|
|
|
|
|
protected Key(KeyValue[] ks, int f, float w, float s, boolean sl, String i)
|
2021-04-15 23:23:31 +02:00
|
|
|
{
|
2023-03-03 19:44:05 +01:00
|
|
|
keys = ks;
|
2023-03-05 23:08:35 +01:00
|
|
|
keysflags = f;
|
2021-04-15 23:23:31 +02:00
|
|
|
width = w;
|
2021-04-17 23:56:51 +02:00
|
|
|
shift = s;
|
2023-01-22 23:03:30 +01:00
|
|
|
slider = sl;
|
2022-10-24 00:27:49 +02:00
|
|
|
indication = i;
|
2022-02-02 21:46:23 +01:00
|
|
|
}
|
|
|
|
|
2023-03-05 23:08:35 +01:00
|
|
|
/** Write the parsed key into [ks] at [index]. Doesn't write if the
|
|
|
|
attribute is not present. Return flags that can be aggregated into the
|
|
|
|
value for [keysflags]. */
|
|
|
|
static int parse_key_attr(XmlPullParser parser, String attr, KeyValue[] ks,
|
|
|
|
int index)
|
|
|
|
throws Exception
|
|
|
|
{
|
|
|
|
String name = parser.getAttributeValue(null, attr);
|
|
|
|
int flags = 0;
|
|
|
|
if (name == null)
|
|
|
|
return 0;
|
|
|
|
String name_loc = stripPrefix(name, "loc ");
|
|
|
|
if (name_loc != null)
|
|
|
|
{
|
|
|
|
flags |= F_LOC;
|
|
|
|
name = name_loc;
|
|
|
|
}
|
|
|
|
ks[index] = KeyValue.getKeyByName(name);
|
|
|
|
return (flags << index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static String stripPrefix(String s, String prefix)
|
|
|
|
{
|
|
|
|
return s.startsWith(prefix) ? s.substring(prefix.length()) : null;
|
|
|
|
}
|
|
|
|
|
2022-11-26 22:16:48 +01:00
|
|
|
public static Key parse(XmlPullParser parser) throws Exception
|
2021-04-15 23:23:31 +02:00
|
|
|
{
|
2023-03-05 23:08:35 +01:00
|
|
|
KeyValue[] ks = new KeyValue[9];
|
|
|
|
int keysflags = 0;
|
|
|
|
keysflags |= parse_key_attr(parser, "key0", ks, 0);
|
|
|
|
keysflags |= parse_key_attr(parser, "key1", ks, 1);
|
|
|
|
keysflags |= parse_key_attr(parser, "key2", ks, 2);
|
|
|
|
keysflags |= parse_key_attr(parser, "key3", ks, 3);
|
|
|
|
keysflags |= parse_key_attr(parser, "key4", ks, 4);
|
|
|
|
keysflags |= parse_key_attr(parser, "key5", ks, 5);
|
|
|
|
keysflags |= parse_key_attr(parser, "key6", ks, 6);
|
|
|
|
keysflags |= parse_key_attr(parser, "key7", ks, 7);
|
|
|
|
keysflags |= parse_key_attr(parser, "key8", ks, 8);
|
2022-11-26 22:16:48 +01:00
|
|
|
float width = attribute_float(parser, "width", 1f);
|
|
|
|
float shift = attribute_float(parser, "shift", 0.f);
|
2023-01-22 23:03:30 +01:00
|
|
|
boolean slider = attribute_bool(parser, "slider", false);
|
2022-10-24 00:27:49 +02:00
|
|
|
String indication = parser.getAttributeValue(null, "indication");
|
2022-11-26 22:16:48 +01:00
|
|
|
while (parser.next() != XmlPullParser.END_TAG)
|
2023-03-05 23:08:35 +01:00
|
|
|
continue;
|
|
|
|
return new Key(ks, keysflags, width, shift, slider, indication);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Whether key at [index] as [flag]. */
|
|
|
|
public boolean keyHasFlag(int index, int flag)
|
|
|
|
{
|
|
|
|
return (keysflags & (flag << index)) != 0;
|
2021-04-15 23:23:31 +02:00
|
|
|
}
|
|
|
|
|
2022-02-13 13:20:22 +01:00
|
|
|
/** New key with the width multiplied by 's'. */
|
|
|
|
public Key scaleWidth(float s)
|
|
|
|
{
|
2023-03-05 23:08:35 +01:00
|
|
|
return new Key(keys, keysflags, width * s, shift, slider, indication);
|
2022-02-13 13:20:22 +01:00
|
|
|
}
|
2022-03-19 15:39:20 +01:00
|
|
|
|
2023-01-30 23:54:39 +01:00
|
|
|
public void getKeys(Set<KeyValue> dst)
|
|
|
|
{
|
2023-03-03 19:44:05 +01:00
|
|
|
for (int i = 0; i < keys.length; i++)
|
|
|
|
if (keys[i] != null)
|
2023-03-05 23:08:35 +01:00
|
|
|
dst.add(keys[i]);
|
2023-01-30 23:54:39 +01:00
|
|
|
}
|
|
|
|
|
2022-05-29 12:27:46 +02:00
|
|
|
public KeyValue getKeyValue(int i)
|
|
|
|
{
|
2023-03-05 23:08:35 +01:00
|
|
|
return keys[i];
|
2022-05-29 12:27:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Key withKeyValue(int i, KeyValue kv)
|
|
|
|
{
|
2023-07-05 18:58:03 +02:00
|
|
|
KeyValue[] ks = new KeyValue[keys.length];
|
|
|
|
for (int j = 0; j < keys.length; j++) ks[j] = keys[j];
|
2023-03-05 23:08:35 +01:00
|
|
|
ks[i] = kv;
|
|
|
|
int flags = (keysflags & ~(ALL_FLAGS << i));
|
|
|
|
return new Key(ks, flags, width, shift, slider, indication);
|
2022-05-29 12:27:46 +02:00
|
|
|
}
|
|
|
|
|
2022-09-25 02:23:33 +02:00
|
|
|
public Key withShift(float s)
|
|
|
|
{
|
2023-03-05 23:08:35 +01:00
|
|
|
return new Key(keys, keysflags, width, s, slider, indication);
|
2022-09-25 02:23:33 +02:00
|
|
|
}
|
|
|
|
|
2023-03-03 19:44:05 +01:00
|
|
|
public boolean hasValue(KeyValue kv)
|
2022-03-19 15:39:20 +01:00
|
|
|
{
|
2023-03-03 19:44:05 +01:00
|
|
|
for (int i = 0; i < keys.length; i++)
|
2023-03-05 23:08:35 +01:00
|
|
|
if (keys[i] != null && keys[i].equals(kv))
|
2023-03-03 19:44:05 +01:00
|
|
|
return true;
|
|
|
|
return false;
|
2022-06-24 20:26:27 +02:00
|
|
|
}
|
2021-04-15 23:23:31 +02:00
|
|
|
}
|
|
|
|
|
2022-04-16 23:36:54 +02:00
|
|
|
// Not using Function<KeyValue, KeyValue> to keep compatibility with Android 6.
|
2022-05-29 12:27:46 +02:00
|
|
|
public static abstract interface MapKey {
|
|
|
|
public Key apply(Key k);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static abstract class MapKeyValues implements MapKey {
|
2022-06-24 20:26:27 +02:00
|
|
|
abstract public KeyValue apply(KeyValue c, boolean localized);
|
2022-05-29 12:27:46 +02:00
|
|
|
|
|
|
|
public Key apply(Key k)
|
|
|
|
{
|
2023-03-05 23:08:35 +01:00
|
|
|
KeyValue[] ks = new KeyValue[k.keys.length];
|
2023-03-03 19:44:05 +01:00
|
|
|
for (int i = 0; i < ks.length; i++)
|
|
|
|
if (k.keys[i] != null)
|
2023-03-05 23:08:35 +01:00
|
|
|
ks[i] = apply(k.keys[i], k.keyHasFlag(i, Key.F_LOC));
|
|
|
|
return new Key(ks, k.keysflags, k.width, k.shift, k.slider, k.indication);
|
2022-06-24 20:26:27 +02:00
|
|
|
}
|
2022-04-16 23:36:54 +02:00
|
|
|
}
|
2022-02-07 00:55:32 +01:00
|
|
|
|
2023-06-03 09:37:59 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 00:55:32 +01:00
|
|
|
/** Parsing utils */
|
|
|
|
|
|
|
|
/** Returns [false] on [END_DOCUMENT] or [END_TAG], [true] otherwise. */
|
2023-06-03 09:37:59 +02:00
|
|
|
private static boolean next_tag(XmlPullParser parser) throws Exception
|
2022-02-07 00:55:32 +01:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
status = parser.next();
|
2022-11-26 22:16:48 +01:00
|
|
|
if (status == XmlPullParser.END_DOCUMENT || status == XmlPullParser.END_TAG)
|
2022-02-07 00:55:32 +01:00
|
|
|
return false;
|
|
|
|
}
|
2022-11-26 22:16:48 +01:00
|
|
|
while (status != XmlPullParser.START_TAG);
|
2023-06-03 09:37:59 +02:00
|
|
|
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;
|
2022-02-07 00:55:32 +01:00
|
|
|
if (!parser.getName().equals(name))
|
2023-06-03 09:37:59 +02:00
|
|
|
throw new Exception("Unknown tag: " + parser.getName());
|
2022-02-07 00:55:32 +01:00
|
|
|
return true;
|
|
|
|
}
|
2022-11-26 22:16:48 +01:00
|
|
|
|
|
|
|
private static boolean attribute_bool(XmlPullParser parser, String attr, boolean default_val)
|
|
|
|
{
|
|
|
|
String val = parser.getAttributeValue(null, attr);
|
|
|
|
if (val == null)
|
|
|
|
return default_val;
|
|
|
|
return val.equals("true");
|
|
|
|
}
|
|
|
|
|
|
|
|
private static float attribute_float(XmlPullParser parser, String attr, float default_val)
|
|
|
|
{
|
|
|
|
String val = parser.getAttributeValue(null, attr);
|
|
|
|
if (val == null)
|
|
|
|
return default_val;
|
|
|
|
return Float.parseFloat(val);
|
|
|
|
}
|
2015-07-31 20:48:19 +02:00
|
|
|
}
|