converter.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. """Functions to convert to and from QMK formats
  2. """
  3. from milc import cli
  4. def kle2qmk(kle):
  5. """Convert a KLE layout to QMK's layout format.
  6. """
  7. layout = []
  8. top_left_corner = None
  9. # Iterate through the KLE classifying keys by layout
  10. for row in kle:
  11. for key in row:
  12. if key['decal']:
  13. continue
  14. if key['label_style'] in [0, 4]:
  15. key_name = key['name'].split('\n')
  16. if len(key_name) == 7:
  17. matrix, _, _, alt_layout, layout_name, _, keycode = key_name
  18. elif len(key_name) == 5:
  19. matrix, _, _, alt_layout, layout_name = key_name
  20. cli.log.warning('Missing Keycode for key at matrix %s layout %s.', matrix, alt_layout)
  21. else:
  22. cli.log.error('Unknown label format: %s', repr(key['name']))
  23. continue
  24. else:
  25. cli.log.error('Unknown label style: %s', key['label_style'])
  26. continue
  27. matrix = list(map(int, matrix.split(',', 1)))
  28. qmk_key = {
  29. 'label': keycode,
  30. 'x': key['column'],
  31. 'y': key['row'],
  32. 'matrix': matrix,
  33. }
  34. if not top_left_corner and (not alt_layout or alt_layout.endswith(',0')):
  35. top_left_corner = key['column'], key['row']
  36. # Figure out what layout this key is part of
  37. # FIXME(skullydazed): In the future this will populate `layout_options` in info.json
  38. if alt_layout:
  39. alt_group, layout_index = map(int, alt_layout.split(',', 1))
  40. if layout_index != 0:
  41. continue
  42. # Set the key size
  43. if key['width'] != 1:
  44. qmk_key['w'] = key['width']
  45. if key['height'] != 1:
  46. qmk_key['h'] = key['height']
  47. layout.append(qmk_key)
  48. # Adjust the keys to account for the top-left corner
  49. for key in layout:
  50. key['x'] -= top_left_corner[0]
  51. key['y'] -= top_left_corner[1]
  52. return layout