layouts.sh (2118B)
1 #!/usr/bin/env bash 2 # 3 # This script produces layout data for the System76 Keyboard Configurator. 4 # 5 # Copyright (C) 2021 System76 6 # 7 # This program is free software: you can redistribute it and/or modify 8 # it under the terms of the GNU General Public License as published by 9 # the Free Software Foundation, version 3. 10 # 11 # This program is distributed in the hope that it will be useful, 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 # GNU General Public License for more details. 15 # 16 # You should have received a copy of the GNU General Public License 17 # along with this program. If not, see <https://www.gnu.org/licenses/>. 18 19 set -eEuo pipefail 20 21 R=$(git rev-parse --show-toplevel) 22 cd "${R}" 23 rm -rf .build/layouts 24 mkdir -p .build/layouts 25 D="$(realpath .build/layouts)" 26 27 binary="${D}/keymap" 28 source="${binary}.c" 29 header="quantum/keycode.h" 30 printf "#include <stdio.h>\n" >"$source" 31 printf "#include \"%s\"\n\n" "${header}" >>"$source" 32 echo "int main(int argc, char **argv) {" >>"$source" 33 grep '^ KC_' "$header" | 34 cut -d ' ' -f5 | 35 cut -d ',' -f1 | 36 while read -r keycode; do 37 name=$(echo "${keycode}" | cut -d '_' -f2-) 38 printf " printf(\"%s,0x%%04X\\\n\", $keycode);\n" "${name}" >>"$source" 39 done 40 printf "\n return 0;\n}\n" >>"$source" 41 gcc -I. "$source" -o "$binary" 42 "${binary}" | tee "${D}/keymap.csv" 43 44 cd keyboards 45 for board in system76/launch_*; do 46 file="$board/$(basename "$board").h" 47 if [ ! -e "$file" ]; then 48 continue 49 fi 50 echo "# ${board}" 51 mkdir -p "${D}/${board}" 52 cp "${D}/keymap.csv" "${D}/${board}" 53 row=0 54 rg \ 55 --multiline \ 56 --multiline-dotall \ 57 --regexp '#define LAYOUT\(.*\) \{.*\}' \ 58 "$file" | 59 grep --only-matching '\{.*\}' | 60 sed 's/^{ //' | 61 sed 's/ }$//' | 62 sed 's/, / /g' | 63 while read -r line; do 64 col=0 65 for word in $line; do 66 if [[ "${word}" != "___" ]]; then 67 echo "${word},${row},${col}" 68 fi 69 col=$((col + 1)) 70 done 71 row=$((row + 1)) 72 done | 73 sort -n | 74 tee "${D}/${board}/layout.csv" 75 done