From ca0683a5370b4c00ad35be9c900d7fafc01a6b5e Mon Sep 17 00:00:00 2001 From: WeirdTreeThing Date: Mon, 28 Aug 2023 19:20:58 -0400 Subject: Initial commit --- README.md | 8 ++++ cros-keyboard-map.py | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++ install.sh | 72 +++++++++++++++++++++++++++++++++ 3 files changed, 191 insertions(+) create mode 100644 README.md create mode 100755 cros-keyboard-map.py create mode 100755 install.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..8813662 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# Utility to generate keyd configurations for use on Chromebooks + +## Instructions +1. git clone https://github.com/WeirdTreeThing/cros-keyboard-map +2. cd cros-keyboard-map +3. ./install.sh + +Thanks to rvaiya for creating [keyd](https://github.com/rvaiya/keyd). diff --git a/cros-keyboard-map.py b/cros-keyboard-map.py new file mode 100755 index 0000000..fb27a06 --- /dev/null +++ b/cros-keyboard-map.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python3 + +def vivaldi_scancode_to_keyd(scancode): + match scancode: + case "90": + return "previoussong" + case "91": + return "zoom" + case "92": + return "scale" + case "93": + return "print" + case "94": + return "brightnessdown" + case "95": + return "brightnessup" + case "97": + return "kbdillumdown" + case "98": + return "kbdillumup" + case "99": + return "nextsong" + case "9A": + return "playpause" + case "9B": + return "micmute" + case "9E": + return "kbdillumtoggle" + case "A0": + return "mute" + case "AE": + return "volumedown" + case "B0": + return "volumeup" + case "E9": + return "forward" + case "EA": + return "back" + case "E7": + return "refresh" + +def load_physmap_data(): + try: + with open("/sys/bus/platform/devices/i8042/serio0/function_row_physmap", "r") as file: + return file.read().strip().split() + except FileNotFoundError: + return "" + +def create_keyd_config(physmap): + config = "" + config += """[ids] +0001:0001 + +[main] +""" + # make fn keys act like vivaldi keys when super isn't held + i = 0 + for scancode in physmap: + i += 1 + config += f"f{i} = {vivaldi_scancode_to_keyd(scancode)}\n" + config += "\n" + + # make vivaldi keys act like vivaldi keys when super isn't held + i = 0 + for scancode in physmap: + i += 1 + config += f"{vivaldi_scancode_to_keyd(scancode)} = {vivaldi_scancode_to_keyd(scancode)}\n" + + # make fn keys act like fn keys when super is held + i = 0 + config += "\n[meta]\n" + for scancode in physmap: + i += 1 + config += f"f{i} = f{i}\n" + + # make vivaldi keys act like like fn keys when super is held + i = 0 + config += "\n" + for scancode in physmap: + i += 1 + config += f"{vivaldi_scancode_to_keyd(scancode)} = f{i}\n" + + # Add various extra shortcuts + config += """\n[alt] +backspace = delete +meta = capslock +brightnessdown = kbdillumdown +brightnessup = kbdillumup +f6 = kbdillumdown +f7 = kbdillumup + +[control] +f5 = print +scale = print + +[control+alt] +backspace = C-A-delete""" + + return config + +def main(): + physmap = load_physmap_data() + if not physmap: + print("no function row mapping found, using default mapping") + physmap = ['EA', 'E9', 'E7', '91', '92', '94', '95', 'A0', 'AE', 'B0'] + + config = create_keyd_config(physmap) + with open("cros.conf", "w") as conf: + conf.write(config) + +main() diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..21a65c9 --- /dev/null +++ b/install.sh @@ -0,0 +1,72 @@ +#!/bin/bash + +#alpine arch and suse have packages +#need to build on fedora and deb* + +if [ -f /usr/bin/apt ]; then + distro="deb" +elif [ -f /usr/bin/zypper ]; then + distro="suse" +elif [ -f /usr/bin/pacman ]; then + distro="arch" +elif [ -f /usr/bin/dnf ]; then + distro="fedora" +elif [ -f /usr/bin/apk ]; then + distro="alpine" +fi + +echo "Installing keyd dependencies" +case $distro in + deb) + sudo apt install -y build-essential git + ;; + arch) + sudo pacman -S --noconfirm base-devel git + ;; + fedora) + sudo dnf groupinstall -y "Development Tools" "Development Libraries" + ;; +esac + +echo "Installing keyd" +case $distro in + suse) + sudo zypper --non-interactive install keyd + ;; + arch) + git clone https://aur.archlinux.org/keyd.git + cd keyd + makepkg -si --noconfirm + ;; + alpine) + doas apk add --no-interactive keyd + ;; + *) + git clone https://github.com/rvaiya/keyd + cd keyd + make + sudo make install + cd .. + ;; +esac + +echo "Generating config" +python3 cros-keyboard-map.py + +echo "Installing config" +sudo mkdir -p /etc/keyd +sudo cp cros.conf /etc/keyd + +echo "Enabling keyd" +case $distro in + alpine) + doas rc-update add keyd + doas rc-service keyd restart + ;; + *) + sudo systemctl enable keyd + sudo systemctl restart keyd + ;; +esac + +echo "Done" -- cgit 1.4.1-2-gfad0