Skip to content

Instantly share code, notes, and snippets.

@Mononofu
Last active August 21, 2021 13:41
Show Gist options
  • Save Mononofu/5170ee3fdfab861c38a6828323839ee9 to your computer and use it in GitHub Desktop.
Save Mononofu/5170ee3fdfab861c38a6828323839ee9 to your computer and use it in GitHub Desktop.
Converts Moonlander unicode entry macros from MacOS format (as generated by Oryx) to the corresponding Linux key-combinations.
#!/usr/bin/python3
import argparse
import os
import re
import subprocess
import zipfile
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--source", required=True, help="The firmware source downloaded from Oryx."
)
parser.add_argument(
"--firmware_root",
required=True,
help="QMK / Moonlander firmware root directory.",
)
args = parser.parse_args()
with zipfile.ZipFile(args.source) as zip:
# Parse the source .zip file downloaded from Oryx.
for path in zip.namelist():
match = re.match(r"moonlander_([\w-]+)_source/.+", path)
if not match:
continue
# Determine the name of the layout and its target path in the
# firmware directory.
name = match.group(1)
layout_dir = os.path.join(
args.firmware_root, "keyboards/moonlander/keymaps", name
)
if not os.path.exists(layout_dir):
os.makedirs(layout_dir)
with open(os.path.join(layout_dir, os.path.basename(path)), "w") as f:
contents = zip.read(path).decode('utf-8')
# Rewrite MacOS unicode input to X11/Linux unicode input.
contents = re.sub(r'SEND_STRING\(SS_LALT\(((SS_TAP\(X_[0-9A-F]\) )+)\)\);',
r'SEND_STRING(SS_LCTL(SS_LSFT(SS_TAP(X_U))) \1 SS_TAP(X_ENTER));',
contents)
f.write(contents)
subprocess.run(f'qmk compile -j {os.cpu_count()} -kb moonlander -km {name}', shell=True)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment