generate_km.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import layout
  4. import re
  5. # TODO: auto-generate mutex layers
  6. # TODO: auto-generate update_tri_layer (_state)
  7. def gen_uc_iter():
  8. length = len(layout.uc_dict)
  9. for key, value in sorted(layout.uc_dict.items()):
  10. length -= 1
  11. if length:
  12. yield (key, value, False)
  13. else:
  14. yield (key, value, True)
  15. def translate(s):
  16. if re.match("^[0-9]$", s):
  17. return ("KC_{0}".format(s), " {0} ".format(s))
  18. elif re.match("^[a-z]$", s):
  19. return ("KC_{0}".format(s.upper()), " {0} ".format(s))
  20. elif re.match("^[A-Z]$", s):
  21. return ("S(KC_{0})".format(s), " {0} ".format(s))
  22. elif re.match("^F[0-9]{1,2}$", s): # Fn, works from F0 to F99
  23. return ("KC_{0}".format(s), "{0:^7}".format(s))
  24. elif re.match("^DF[0-9]{1,2}$", s): # DFn, works from DF0 to DF99
  25. return ("DF({0})".format(s[2:]), "{0:^7}".format(s))
  26. elif re.match("^MO[0-9]{1,2}$", s): # MOn, works from MO0 to MO99
  27. return ("MO({0})".format(s[2:]), "{0:^7}".format(s))
  28. elif re.match("^OSL[0-9]{1,2}$", s): # OSLn, works from OSL0 to OSL99
  29. return ("OSL({0})".format(s[3:]), "{0:^7}".format(s))
  30. elif re.match("^TG[0-9]{1,2}$", s): # TGn, works from TG0 to TG99
  31. return ("TG({0})".format(s[2:]), "{0:^7}".format(s))
  32. elif re.match("^TO[0-9]{1,2}$", s): # Tn, works from TO0 to TO99
  33. return ("TO({0})".format(s[2:]), "{0:^7}".format(s))
  34. elif re.match("^TT[0-9]{1,2}$", s): # Tn, works from TT0 to TT99
  35. return ("TT({0})".format(s[2:]), "{0:^7}".format(s))
  36. elif s in layout.uc_dict:
  37. return ("X("+s+")", " {0} ".format(chr(int(layout.uc_dict[s], 0))))
  38. elif s in layout.qmk_dict:
  39. return (layout.qmk_dict[s], "{0:^7}".format(s))
  40. elif s == s.upper() and s.startswith("KC_"):
  41. return (s, "{0:^7}".format(s[2:]))
  42. else:
  43. return ("XXXXXXX", " {0} ".format(chr(128165)))
  44. def toKC(s):
  45. (kc, lgd) = translate(s)
  46. return kc
  47. def toLgd(s):
  48. (kc, lgd) = translate(s)
  49. return lgd
  50. if __name__ == "__main__":
  51. template = open("km_template.txt", mode="r")
  52. output = open("keymap.c", mode="w", encoding='utf-8')
  53. doCopy = False
  54. for line in template:
  55. doCopy = True
  56. if line.startswith("//<enum/>"):
  57. doCopy = False
  58. # output.write(str(layout.uc_dict))
  59. for k, v, isLast in gen_uc_iter():
  60. if isLast:
  61. output.write(k + "\n")
  62. else:
  63. output.write(k + ",\n")
  64. if line.startswith("//<uc_map/>"):
  65. doCopy = False
  66. for k, v, isLast in gen_uc_iter():
  67. if isLast:
  68. output.write(u"\t[{0}] = {1} // {2}\n".format(k, v, chr(int(v, 0))))
  69. else:
  70. output.write(u"\t[{0}] = {1}, // {2}\n".format(k, v, chr(int(v, 0))))
  71. if line.startswith("//<keymaps/>"):
  72. doCopy = False
  73. counter = len(layout.layers)
  74. layer = 0
  75. S = layout.static
  76. for L in layout.layers:
  77. counter -= 1
  78. r_counter = 4
  79. output.write("/* Layer %d\n" % layer)
  80. output.write(" * ------------------------------------------------- -------------------------------------------------\n")
  81. output.write(" * |{0}|{1}|{2}|{3}|{4}|{5}| |{6}|{7}|{8}|{9}|{10}|{11}|\n".format(toLgd(L[0][0]), toLgd(L[0][1]), toLgd(L[0][2]), toLgd(L[0][3]), toLgd(L[0][4]), toLgd(L[0][5]), toLgd(L[0][6]), toLgd(L[0][7]), toLgd(L[0][8]), toLgd(L[0][9]), toLgd(L[0][10]), toLgd(L[0][11])))
  82. output.write(" * ------------------------------------------------- -------------------------------------------------\n")
  83. output.write(" * |{0}|{1}|{2}|{3}|{4}|{5}| |{6}|{7}|{8}|{9}|{10}|{11}|\n".format(toLgd(L[1][0]), toLgd(L[1][1]), toLgd(L[1][2]), toLgd(L[1][3]), toLgd(L[1][4]), toLgd(L[1][5]), toLgd(L[1][6]), toLgd(L[1][7]), toLgd(L[1][8]), toLgd(L[1][9]), toLgd(L[1][10]), toLgd(L[1][11])))
  84. output.write(" * ------------------------------------------------- -------------------------------------------------\n")
  85. output.write(" * |{0}|{1}|{2}|{3}|{4}|{5}| |{6}|{7}|{8}|{9}|{10}|{11}|\n".format(toLgd(L[2][0]), toLgd(L[2][1]), toLgd(L[2][2]), toLgd(L[2][3]), toLgd(L[2][4]), toLgd(L[2][5]), toLgd(L[2][6]), toLgd(L[2][7]), toLgd(L[2][8]), toLgd(L[2][9]), toLgd(L[2][10]), toLgd(L[2][11])))
  86. output.write(" * -----------------------------------------------------------------------------------------------------------------\n")
  87. output.write(" * {0} {1} {2} |{3}|{4}|{5}|{6}|{7}|{8}| {9} {10} {11}".format(toLgd(L[3][0]), toLgd(L[3][1]), toLgd(L[3][2]), toLgd(L[3][3]), toLgd(L[3][4]), toLgd(L[3][5]), toLgd(L[3][6]), toLgd(L[3][7]), toLgd(L[3][8]), toLgd(L[3][9]), toLgd(L[3][10]), toLgd(L[3][11])).rstrip()+"\n")
  88. output.write(" * -------------------------------------------------\n")
  89. output.write(" */\n")
  90. l_code = '\tLAYOUT_ortho_4x12(\n'
  91. for r in range(r_counter):
  92. r_counter -= 1
  93. c_counter = 12
  94. l_code += '\t\t'
  95. for c in range(c_counter):
  96. c_counter -= 1
  97. if c != 0:
  98. l_code += " "
  99. l_code += "%s" % toKC(L[r][c])
  100. if r_counter or c_counter:
  101. l_code += ","
  102. l_code += '\n'
  103. if counter:
  104. l_code += "\t),\n\n"
  105. else:
  106. l_code += "\t)\n"
  107. output.write(l_code)
  108. layer += 1
  109. if doCopy:
  110. output.write(line)
  111. template.close()
  112. output.close()