logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

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. set -eEuo pipefail
  19. R=$(git rev-parse --show-toplevel)
  20. cd "${R}"
  21. rm -rf .build/layouts
  22. mkdir -p .build/layouts
  23. D="$(realpath .build/layouts)"
  24. binary="${D}/keymap"
  25. source="${binary}.c"
  26. header="quantum/keycode.h"
  27. printf "#include <stdio.h>\n" >"$source"
  28. printf "#include \"%s\"\n\n" "${header}" >>"$source"
  29. echo "int main(int argc, char **argv) {" >>"$source"
  30. grep '^ KC_' "$header" |
  31. cut -d ' ' -f5 |
  32. cut -d ',' -f1 |
  33. while read -r keycode; do
  34. name=$(echo "${keycode}" | cut -d '_' -f2-)
  35. printf " printf(\"%s,0x%%04X\\\n\", $keycode);\n" "${name}" >>"$source"
  36. done
  37. printf "\n return 0;\n}\n" >>"$source"
  38. gcc -I. "$source" -o "$binary"
  39. "${binary}" | tee "${D}/keymap.csv"
  40. cd keyboards
  41. for board in system76/launch_*; do
  42. file="$board/$(basename "$board").h"
  43. if [ ! -e "$file" ]; then
  44. continue
  45. fi
  46. echo "# ${board}"
  47. mkdir -p "${D}/${board}"
  48. cp "${D}/keymap.csv" "${D}/${board}"
  49. row=0
  50. rg \
  51. --multiline \
  52. --multiline-dotall \
  53. --regexp '#define LAYOUT\(.*\) \{.*\}' \
  54. "$file" |
  55. grep --only-matching '\{.*\}' |
  56. sed 's/^{ //' |
  57. sed 's/ }$//' |
  58. sed 's/, / /g' |
  59. while read -r line; do
  60. col=0
  61. for word in $line; do
  62. if [[ "${word}" != "___" ]]; then
  63. echo "${word},${row},${col}"
  64. fi
  65. col=$((col + 1))
  66. done
  67. row=$((row + 1))
  68. done |
  69. sort -n |
  70. tee "${D}/${board}/layout.csv"
  71. done