Generate layouts arrays used in settings

`gen_layouts.py` lists the layouts in `res/xml` and generate the
`pref_layout_values` and `pref_layout_entries` arrays into
`res/values/layouts.xml`.

These arrays are hard to maintain as the order has to match, which is
fragile.

This relies on every layouts having a `name` attribute.
This commit is contained in:
Jules Aguillon 2023-06-25 15:52:24 +02:00
parent f36864533c
commit 8ba82d2555
41 changed files with 191 additions and 120 deletions

View File

@ -31,10 +31,14 @@ sync_translations:
check_layouts:
python check_layout.py $(wildcard res/xml/*.xml) > check_layout.output
# Will modify the source tree.
runtest: rebuild_special_font sync_translations check_layouts
gen_layouts:
python gen_layouts.py
.PHONY: release debug installd clean rebuild_special_font check_layouts sync_translations runtest
# Will modify the source tree.
runtest: rebuild_special_font sync_translations check_layouts gen_layouts
.PHONY: release debug installd clean rebuild_special_font check_layouts \
sync_translations runtest gen_layouts
$(shell mkdir -p _build)

64
gen_layouts.py Normal file
View File

@ -0,0 +1,64 @@
#!/usr/bin/env python
# Generates the list of layouts in res/values/layouts.xml from the layout files
# in res/xml. Every layouts must have a 'name' attribute to be listed.
import itertools as it
import sys, os, glob
import xml.etree.ElementTree as XML
# Layouts first in the list (these are the programming layouts). Other layouts
# are sorted alphabetically.
FIRST_LAYOUTS = [ "latn_qwerty_us", "latn_colemak", "latn_dvorak" ]
# Read a layout from a file. Returns [None] if [fname] is not a layout.
def read_layout(fname):
root = XML.parse(fname).getroot()
if root.tag != "keyboard":
return None
return { "name": root.get("name") }
# Yields the id (based on the file name) and the display name for every layouts
def read_layouts(files):
for layout_file in files:
layout_id, _ = os.path.splitext(os.path.basename(layout_file))
layout = read_layout(layout_file)
if layout == None:
print("Not a layout file: %s" % layout_file)
elif layout["name"] == None:
print("Layout doesn't have a name: %s" % layout_id)
else:
yield (layout_id, layout["name"])
# Sort layouts alphabetically, except for layouts in FIRST_LAYOUTS, which are
# placed at the top.
# Returns a list. 'layouts' can be an iterator.
def sort_layouts(layouts):
layouts = dict(layouts)
head = [ (lid, layouts.pop(lid)) for lid in FIRST_LAYOUTS ]
return head + sorted(layouts.items())
# Write the XML arrays used in the preferences.
def generate_arrays(out, layouts):
def add_items(parent, strings):
for s in strings:
item = XML.Element("item")
item.text = s
parent.append(item)
none_item = [ ("none", "None") ]
custom_item = [ ("custom", "@string/pref_layout_e_custom") ]
lids, names = zip(*(none_item + layouts + custom_item)) # unzip
values = XML.Element("string-array", name="pref_layout_values")
add_items(values, lids)
entries = XML.Element("string-array", name="pref_layout_entries")
add_items(entries, names)
root = XML.Element("resources")
root.append(XML.Comment(text="DO NOT EDIT. This file is generated, see gen_layouts.py."))
root.append(values)
root.append(entries)
XML.indent(root)
XML.ElementTree(element=root).write(out, encoding="unicode", xml_declaration=True)
layouts = sort_layouts(read_layouts(glob.glob("res/xml/*.xml")))
with open("res/values/layouts.xml", "w") as out:
generate_arrays(out, layouts)

View File

@ -1,85 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="pref_layout_values">
<item>none</item>
<item>latn_qwerty_us</item>
<item>latn_colemak</item>
<item>latn_dvorak</item>
<item>arab_pc_ckb</item>
<item>arab_pc</item>
<item>arab_alt</item>
<item>arab_pc_ir</item>
<item>beng_national</item>
<item>beng_provat</item>
<item>cyrl_jcuken_ru</item>
<item>cyrl_jcuken_uk</item>
<item>cyrl_yaverti</item>
<item>deva_inscript</item>
<item>deva_alt</item>
<item>grek_qwerty</item>
<item>hang_dubeolsik_kr</item>
<item>hebr_1_il</item>
<item>hebr_2_il</item>
<item>latn_azerty_fr</item>
<item>latn_bone</item>
<item>latn_neo2</item>
<item>latn_qwerty_hu</item>
<item>latn_qwerty_br</item>
<item>latn_qwerty_es</item>
<item>latn_qwerty_lv</item>
<item>latn_qwerty_no</item>
<item>latn_qwerty_pl</item>
<item>latn_qwerty_se</item>
<item>latn_qwerty_tr</item>
<item>latn_qwerty_vi</item>
<item>latn_qwerty_ro</item>
<item>latn_qwertz</item>
<item>latn_qwertz_cz</item>
<item>latn_qwertz_de</item>
<item>latn_qwertz_hu</item>
<item>latn_qwertz_sk</item>
<item>custom</item>
</string-array>
<string-array name="pref_layout_entries">
<item>None</item>
<item>QWERTY (US)</item>
<item>Colemak</item>
<item>Dvorak</item>
<item>Arabic (کوردی)</item>
<item>Arabic PC</item>
<item>Arabic Alt</item>
<item>Persian PC</item>
<item>বাংলা (জাতীয়)</item>
<item>বাংলা (প্রভাত)</item>
<item>ЙЦУКЕН (Русский)</item>
<item>ЙЦУКЕН (Українська)</item>
<item>ЯВЕРТЪ</item>
<item>देवनागरी (हिंदी)-1</item>
<item>देवनागरी (हिंदी)-2</item>
<item>QWERTY (Greek)</item>
<item>두벌식 (Korean)</item>
<item>Hebrew 1</item>
<item>Hebrew 2</item>
<item>AZERTY (Français)</item>
<item>Bone</item>
<item>Neo 2</item>
<item>QWERTY (Magyar)</item>
<item>QWERTY (Brasileiro)</item>
<item>QWERTY (Español)</item>
<item>QWERTY (Latvian)</item>
<item>QWERTY (Norwegian)</item>
<item>QWERTY (Polski)</item>
<item>QWERTY (Swedish)</item>
<item>QWERTY (Türkçe)</item>
<item>QWERTY (Vietnamese)</item>
<item>QWERTY (Română)</item>
<item>QWERTZ</item>
<item>QWERTZ (Czech)</item>
<item>QWERTZ (Deutsch)</item>
<item>QWERTZ (Magyar)</item>
<item>QWERTZ (Slovak)</item>
<item>@string/pref_layout_e_custom</item>
</string-array>
<string-array name="pref_accents_entries">
<item>@string/pref_accents_e_all_installed</item>
<item>@string/pref_accents_e_selected</item>

84
res/values/layouts.xml Normal file
View File

@ -0,0 +1,84 @@
<?xml version='1.0' encoding='UTF-8'?>
<resources>
<!--DO NOT EDIT. This file is generated, see gen_layouts.py.-->
<string-array name="pref_layout_values">
<item>none</item>
<item>latn_qwerty_us</item>
<item>latn_colemak</item>
<item>latn_dvorak</item>
<item>arab_alt</item>
<item>arab_pc</item>
<item>arab_pc_ckb</item>
<item>arab_pc_ir</item>
<item>beng_national</item>
<item>beng_provat</item>
<item>cyrl_jcuken_ru</item>
<item>cyrl_jcuken_uk</item>
<item>cyrl_yaverti</item>
<item>deva_alt</item>
<item>deva_inscript</item>
<item>grek_qwerty</item>
<item>hang_dubeolsik_kr</item>
<item>hebr_1_il</item>
<item>hebr_2_il</item>
<item>latn_azerty_fr</item>
<item>latn_bone</item>
<item>latn_neo2</item>
<item>latn_qwerty_br</item>
<item>latn_qwerty_es</item>
<item>latn_qwerty_hu</item>
<item>latn_qwerty_lv</item>
<item>latn_qwerty_no</item>
<item>latn_qwerty_pl</item>
<item>latn_qwerty_ro</item>
<item>latn_qwerty_se</item>
<item>latn_qwerty_tr</item>
<item>latn_qwerty_vi</item>
<item>latn_qwertz</item>
<item>latn_qwertz_cz</item>
<item>latn_qwertz_de</item>
<item>latn_qwertz_hu</item>
<item>latn_qwertz_sk</item>
<item>custom</item>
</string-array>
<string-array name="pref_layout_entries">
<item>None</item>
<item>QWERTY (US)</item>
<item>Colemak</item>
<item>Dvorak</item>
<item>Arabic Alt</item>
<item>Arabic PC</item>
<item>Arabic (کوردی)</item>
<item>Persian PC</item>
<item>বাংলা (জাতীয়)</item>
<item>বাংলা (প্রভাত)</item>
<item>ЙЦУКЕН (Русский)</item>
<item>ЙЦУКЕН (Українська)</item>
<item>ЯВЕРТЪ</item>
<item>देवनागरी (हिंदी)-2</item>
<item>देवनागरी (हिंदी)-1</item>
<item>QWERTY (Greek)</item>
<item>두벌식 (Korean)</item>
<item>Hebrew 1</item>
<item>Hebrew 2</item>
<item>AZERTY (Français)</item>
<item>Bone</item>
<item>Neo 2</item>
<item>QWERTY (Brasileiro)</item>
<item>QWERTY (Español)</item>
<item>QWERTY (Magyar)</item>
<item>QWERTY (Latvian)</item>
<item>QWERTY (Norwegian)</item>
<item>QWERTY (Polski)</item>
<item>QWERTY (Română)</item>
<item>QWERTY (Swedish)</item>
<item>QWERTY (Türkçe)</item>
<item>QWERTY (Vietnamese)</item>
<item>QWERTZ</item>
<item>QWERTZ (Czech)</item>
<item>QWERTZ (Deutsch)</item>
<item>QWERTZ (Magyar)</item>
<item>QWERTZ (Slovak)</item>
<item>@string/pref_layout_e_custom</item>
</string-array>
</resources>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="arabic">
<keyboard name="Arabic Alt" script="arabic">
<row>
<key key0="ض" key2="١" key3="`" key4="esc"/>
<key key0="ص" key2="٢" key3="\@"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="arabic">
<keyboard name="Arabic PC" script="arabic">
<row>
<key key0="ض" key1="&#1614;" key2="١" key3="`" key4="esc"/>
<key key0="ص" key1="&#1611;" key2="٢" key3="\@"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="arabic">
<keyboard name="Arabic (کوردی)" script="arabic">
<row>
<key key0="ق" key1="ٯ" key2="١" key3="esc"/>
<key key0="و" key1="وو" key2="٢" key3="\@"/>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Adapted from standard SIRI 9147 -->
<keyboard script="arabic">
<keyboard name="Persian PC" script="arabic">
<row>
<key key0="ض" key2="۱"/>
<key key0="ص" key2="۲"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard>
<keyboard name="বাংলা (জাতীয়)">
<row>
<key key0="ঙ" key2="ং" key3="esc" key4="১"/>
<key key0="য" key2="য়" key3="¶" key4="২"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<keyboard>
<keyboard name="বাংলা (প্রভাত)">
<row>
<key key0="দ" key1="ধ" key2="১" key3="!" key5="esc" />
<key key0="ূ" key1="ঊ" key2="২" key3="\@" />

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="cyrillic">
<keyboard name="ЙЦУКЕН (Русский)" script="cyrillic">
<row>
<key key0="й" key2="1" key4="esc"/>
<key key0="ц" key1="loc ї" key2="2" key3="\@" key4="~"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="cyrillic">
<keyboard name="ЙЦУКЕН (Українська)" script="cyrillic">
<row>
<key shift="0.1 " key0="й" key2="1" key4="esc"/>
<key key0="ц" key1="~" key2="2" key3="\@"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="cyrillic">
<keyboard name="ЯВЕРТЪ" script="cyrillic">
<row>
<key key0="я" key2="1" key4="esc"/>
<key key0="в" key1="!" key2="2" key3="\@" key4="ч"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="devanagari">
<keyboard name="देवनागरी (हिंदी)-2" script="devanagari">
<row>
<key shift="0.35" width="0.9" key0="क" key1="ख" key2="घ" key3="ङ" key4="ग"/>
<key width="0.9" key0="च" key1="छ" key2="झ" key3="ञ" key4="ज"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="devanagari">
<keyboard name="देवनागरी (हिंदी)-1" script="devanagari">
<row>
<key key0="ौ" key1="औ" key2="₹" key3="esc" key4="१"/>
<key key0="ै" key1="ऐ" key2="ऍ" key3="¶" key4="२"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="latin">
<keyboard name="QWERTY (Greek)" script="latin">
<row>
<key key0=";" key2="1" key4="esc"/>
<key key0="ς" key1="~" key2="2" key3="\@"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="hangul">
<keyboard name="두벌식 (Korean)" script="hangul">
<row>
<key key0="ㅂ" key2="1" key4="esc"/>
<key key0="ㅈ" key1="~" key2="2" key3="\@"/>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Based on standard SI-1452-1 -->
<keyboard script="hebrew">
<keyboard name="Hebrew 1" script="hebrew">
<row>
<key key0="/" key2="1" key3="!" key4="esc"/>
<key key0="'" key2="2" key3="\@" key1=";"/>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Based on standard SI-1452-2 -->
<keyboard script="hebrew">
<keyboard name="Hebrew 2" script="hebrew">
<row>
<key key0="ץ" key2="1" key3="!" key4="esc"/>
<key key0="ן" key2="2" key3="\@" key1=";"/>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Adapted from standard NF Z71-300 -->
<!-- https://norme-azerty.fr/ -->
<keyboard script="latin">
<keyboard name="AZERTY (Français)" script="latin">
<row>
<key key0="a" key2="1" key4="esc"/>
<key key0="z" key2="2" key3="&amp;" key4="~"/>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://neo-layout.org/Layouts/bone/ -->
<keyboard bottom_row="false" script="latin">
<keyboard name="Bone" bottom_row="false" script="latin">
<row>
<key key0="j" key2="esc" key4="…"/>
<key key0="d" key2="°" key4="_"/>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://colemak.com/ -->
<keyboard script="latin">
<keyboard name="Colemak" script="latin">
<row>
<key key0="q" key1="esc" key2="loc accent_aigu" key3="loc accent_tilde" shift="0.5"/>
<key key0="w" key1="loc accent_grave" key2=" ́" key3="~" />

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="latin">
<keyboard name="Dvorak" script="latin">
<row>
<key key0="shift" width="1.5" key2="esc" key4="tab"/>
<key key0="p" key1="loc accent_ring" key2="." key3="&lt;" key4="f11_placeholder"/>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://neo-layout.org/Layouts/neo/ -->
<keyboard bottom_row="false" script="latin">
<keyboard name="Neo 2" bottom_row="false" script="latin">
<row>
<key key0="x" key1="°" key2="1" key4="…"/>
<key key0="v" key1="§" key2="2" key4="_"/>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Adapted from standard ABNT NBR 10346 -->
<keyboard script="latin">
<keyboard name="QWERTY (Brasileiro)" script="latin">
<row>
<key key0="q" key2="1" key4="esc"/>
<key key0="w" key1="~" key2="2" key3="\@" key4="!"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="latin">
<keyboard name="QWERTY (Español)" script="latin">
<row>
<key key0="q" key2="1" key4="esc"/>
<key key0="w" key1="~" key2="2" key3="\@"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="latin">
<keyboard name="QWERTY (Magyar)" script="latin">
<row>
<key key0="q" key2="0" key4="esc"/>
<key key0="w" key1="'" key2="1" key3="~" key4="|"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="latin">
<keyboard name="QWERTY (Latvian)" script="latin">
<row>
<key key0="q" key2="1" key4="esc"/>
<key key0="w" key1="~" key2="2" key3="\@" key4="!"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="latin">
<keyboard name="QWERTY (Norwegian)" script="latin">
<row>
<key key0="q" key2="1" key4="esc"/>
<key key0="w" key1="~" key2="2" key3="\@"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="latin">
<keyboard name="QWERTY (Polski)" script="latin">
<row>
<key key0="q" key2="1" key4="esc"/>
<key key0="w" key1="~" key2="2" key3="\@"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="latin">
<keyboard name="QWERTY (Română)" script="latin">
<row>
<key key0="q" key2="1" key4="esc"/>
<key key0="w" key1="~" key2="2" key3="\@"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="latin">
<keyboard name="QWERTY (Swedish)" script="latin">
<row>
<key key0="q" key4="esc" key2="1"/>
<key key0="w" key2="2" key3="`" key4="\@"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="latin">
<keyboard name="QWERTY (Türkçe)" script="latin">
<row>
<key key0="q" key2="1" key4="esc"/>
<key key0="w" key1="~" key2="2" key3="\@"/>

View File

@ -20,7 +20,7 @@ See bottom_row.xml for the definition of the bottom row and neo2.xml for a
layout that re-defines it.
See srcs/juloo.keyboard2/KeyValue.java for the keys that have a special meaning.
-->
<keyboard script="latin">
<keyboard name="QWERTY (US)" script="latin">
<row>
<key key0="q" key2="1" key4="esc"/>
<key key0="w" key1="~" key2="2" key3="\@"/>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Adapted from standard TCVN 6064-->
<keyboard script="latin">
<keyboard name="QWERTY (Vietnamese)" script="latin">
<row>
<key key0="q" key1="~" key2="1" key3="`" key4="esc"/>
<key key0="w" key1="accent_horn" key2="2" key3="\@"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="latin">
<keyboard name="QWERTZ" script="latin">
<row>
<key key0="q" key2="1" key4="esc"/>
<key key0="w" key1="^" key2="2" key3="&quot;" key4="\@"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="latin">
<keyboard name="QWERTZ (Czech)" script="latin">
<row>
<key key0="q" key1="1" key4="esc"/>
<key key0="w" key1="2" key3="~"/>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Adapted from standard DIN 2137-1 -->
<keyboard script="latin">
<keyboard name="QWERTZ (Deutsch)" script="latin">
<row>
<key key0="q" key2="1" key4="esc"/>
<key key0="w" key1="^" key2="2" key3="&quot;" key4="\@"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="latin">
<keyboard name="QWERTZ (Magyar)" script="latin">
<row>
<key key0="q" key2="0" key4="esc"/>
<key key0="w" key1="'" key2="1" key3="~" key4="|"/>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<keyboard script="latin">
<keyboard name="QWERTZ (Slovak)" script="latin">
<row>
<key key0="q" key1="1" key4="esc"/>
<key key0="w" key1="2"/>

View File

@ -1,5 +1,4 @@
import xml.etree.ElementTree as ET
from xml.sax import saxutils
import glob
# Edit every strings.xml files: