Skip to content

Add Linux support for both wayland and x11 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,14 @@
5. Go to System Settings > Keyboard Layouts

6. Click the + button, and search for Graphite


# Linux

1. Run `sudo ./linux/install.sh`

2. Log out, log back in (or reboot)

3. Run `./linux/activate.sh` or manually set the layout in your window manager

4. To remove the layout, run `./linux/uninstall.sh`
28 changes: 28 additions & 0 deletions linux/activate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash

if [ "$XDG_CURRENT_DESKTOP" == "GNOME" ]; then
gsettings set org.gnome.desktop.input-sources sources "[('xkb', 'us+graphite')]"
#gsettings set org.gnome.desktop.input-sources xkb-options "['ctrl:nocaps']"

elif [ "$XDG_CURRENT_DESKTOP" == "KDE" ]; then
kwriteconfig6 --file kxkbrc --group Layout --key LayoutList us
kwriteconfig6 --file kxkbrc --group Layout --key VariantList graphite
#kwriteconfig6 --file kxkbrc --group Layout --key Options caps:backspace # Uncomment for CAPS to be BKSP
#kwriteconfig6 --file kxkbrc --group Layout --key Options ctrl:nocaps # Uncomment for CAPS to be CTRL
dbus-send --session --type=signal --reply-timeout=100 --dest=org.kde.keyboard /Layouts org.kde.keyboard.reloadConfig

else
if [ "$XDG_SESSION_TYPE" == "wayland" ]; then
echo "You're running Wayland with a compositor other than Gnome or KDE"
echo "I don't know how to enable the layout for your compositor."
echo "You will need to manually enable it in your window manager."
exit 1
else
setxkbmap us -variant graphite
#setxkbmap us -variant graphite -option ctrl:nocaps
fi
fi

echo ""
echo "Graphite Layout should now be the active layout"
echo ""
54 changes: 54 additions & 0 deletions linux/graphite
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

partial alphanumeric_keys
xkb_symbols "graphite" {
include "us"
name[Group1]= "English (Graphite)";

key <TLDE> { [ grave, asciitilde ] };
key <AE01> { [ 1, exclam ] };
key <AE02> { [ 2, at ] };
key <AE03> { [ 3, numbersign ] };
key <AE04> { [ 4, dollar ] };
key <AE05> { [ 5, percent ] };
key <AE06> { [ 6, asciicircum ] };
key <AE07> { [ 7, ampersand ] };
key <AE08> { [ 8, asterisk ] };
key <AE09> { [ 9, parenleft ] };
key <AE10> { [ 0, parenright ] };
key <AE11> { [ bracketleft, braceleft ] };
key <AE12> { [ bracketright, braceright ] };
key <AD01> { [ b, B ] };
key <AD02> { [ l, L ] };
key <AD03> { [ d, D ] };
key <AD04> { [ w, W ] };
key <AD05> { [ z, Z ] };
key <AD06> { [ apostrophe, underscore ] };
key <AD07> { [ f, F ] };
key <AD08> { [ o, O ] };
key <AD09> { [ u, U ] };
key <AD10> { [ j, J ] };
key <AD11> { [ semicolon, colon ] };
key <AD12> { [ equal, plus ] };
key <BKSL> { [ backslash, bar ] };
key <AC01> { [ n, N ] };
key <AC02> { [ r, R ] };
key <AC03> { [ t, T ] };
key <AC04> { [ s, S ] };
key <AC05> { [ g, G ] };
key <AC06> { [ y, Y ] };
key <AC07> { [ h, H ] };
key <AC08> { [ a, A ] };
key <AC09> { [ e, E ] };
key <AC10> { [ i, I ] };
key <AC11> { [ comma, question ] };
key <AB01> { [ q, Q ] };
key <AB02> { [ x, X ] };
key <AB03> { [ m, M ] };
key <AB04> { [ c, C ] };
key <AB05> { [ v, V ] };
key <AB06> { [ k, K ] };
key <AB07> { [ p, P ] };
key <AB08> { [ period, greater ] };
key <AB09> { [ minus, quotedbl ] };
key <AB10> { [ slash, less ] };
};
41 changes: 41 additions & 0 deletions linux/graphite.xslt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" doctype-system="xkb.dtd"/>

<!-- Identity transform: copies everything as is -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>

<!-- Match the layout element with the description "English (US)" -->
<xsl:template match="layout[configItem/name='us' and configItem/description='English (US)']/variantList">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
<xsl:text disable-output-escaping="yes">&#10;</xsl:text>
<xsl:comment>GRAPHITE BEGIN</xsl:comment>
<xsl:text disable-output-escaping="yes">&#10; </xsl:text>
<variant>
<xsl:text disable-output-escaping="yes">&#10; </xsl:text>
<configItem>
<xsl:text disable-output-escaping="yes">&#10; </xsl:text>
<name>graphite</name>
<xsl:text disable-output-escaping="yes">&#10; </xsl:text>
<description>English (Graphite)</description>
<xsl:text disable-output-escaping="yes">&#10; </xsl:text>
</configItem>
<xsl:text disable-output-escaping="yes">&#10; </xsl:text>
</variant>
<xsl:text disable-output-escaping="yes">&#10;</xsl:text>
<xsl:comment>GRAPHITE END</xsl:comment>
<xsl:text disable-output-escaping="yes">&#10; </xsl:text>

</xsl:copy>
</xsl:template>
</xsl:stylesheet>





94 changes: 94 additions & 0 deletions linux/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/env bash
#
# Installs or reinstalls the Graphite keyboard layout (X11 or Wayland)
# Once installed, you may need to logout or reboot for the layout to be available.
#
# Linux (and especially wayland) is not set up to add custom keyboard layouts easily.
# setxkbmap can use a user-writable directory to add layous, but it isn't available
# for wayland. Alternative approaches such as xremap and kmonad will work too, but
# I needed a native mechanism without those dependencies, because not all systems
# have access to the internet.
#
# This script uses xslt (via the `xsltproc` tool) to modify the system xkb registry
# xml file. I expect most distros include this tool as part of their base install.
#

#set -x
set -e

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
LAYOUT_FILE=${SCRIPT_DIR}/graphite
XSLT_FILE=${SCRIPT_DIR}/graphite.xslt
XKB_DIR=/usr/share/X11/xkb
SYMBOLS_DIR=${XKB_DIR}/symbols
RULES_DIR=${XKB_DIR}/rules
EVDEV_XML=${RULES_DIR}/evdev.xml

add_layout_to_registry() {
# Backup the system's xkb evdev.xml file if we haven't already, just in case
if ! test -f ${EVDEV_XML}.bak; then
echo "Backing up evdev.xml file"
cp ${EVDEV_XML} ${EVDEV_XML}.bak
fi

# Add the layout to evdev.xml and store the result in /tmp/evdev.xml
TMP_FILE=$(mktemp -q /tmp/evdev.XXXXXX)
#echo "Modifying xkb evdev.xml file and storing temporarily at ${TMP_FILE}"
pushd ${RULES_DIR} >/dev/null
xsltproc --nodtdattr -o ${TMP_FILE} ${XSLT_FILE} evdev.xml
if ! [ "$?" == "0" ]; then
echo "Failed to update the xkb registry";
popd
exit 1
fi
popd >/dev/null

# Now copy it over the top of the system's xkb evdev file
cp ${TMP_FILE} ${EVDEV_XML}
rm ${TMP_FILE}
echo "Updated xkb registry"
}

add_layout_symbols() {
# Append the layout to the end of the 'us' symbols file
#echo "Appending contents of ${LAYOUT_FILE} to ${SYMBOLS_DIR}/us"
echo "//---GRAPHITE BEGIN---" >> ${SYMBOLS_DIR}/us
cat ${LAYOUT_FILE} >> ${SYMBOLS_DIR}/us
echo "//---GRAPHITE END---" >> ${SYMBOLS_DIR}/us
echo "Added Graphite as US layout variant"
}

install_layout() {
add_layout_symbols
add_layout_to_registry
}

uninstall_layout() {
sed -i '/^\/\/---GRAPHITE BEGIN---/,/^\/\/---GRAPHITE END---/d' ${SYMBOLS_DIR}/us
sed -i '/GRAPHITE BEGIN/,/GRAPHITE END/d' ${EVDEV_XML}
}

verify_user_is_root() {
if [ ! "${EUID:-$(id -u)}" -eq 0 ]; then
echo "This script must unfortunately be run with sudo"
exit 1
fi
}

verify_tools_available() {
if ! command -v xsltproc >/dev/null 2>&1; then
echo "This script requires that xsltproc is available. Please install it first."
exit 1
fi
}


verify_tools_available
verify_user_is_root
uninstall_layout
install_layout

echo ""
echo "Successfully installed!"
echo "You may need to logout or reboot before the layout will become available."
echo ""
30 changes: 30 additions & 0 deletions linux/uninstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash
#
# Uninstalls the Graphite keyboard layout
#

#set -x
set -e

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
XKB_DIR=/usr/share/X11/xkb
SYMBOLS_DIR=${XKB_DIR}/symbols
RULES_DIR=${XKB_DIR}/rules
EVDEV_XML=${RULES_DIR}/evdev.xml

uninstall_layout() {
sed -i '/^\/\/---GRAPHITE BEGIN---/,/^\/\/---GRAPHITE END---/d' ${SYMBOLS_DIR}/us
echo "Removed Graphite layout symbols"
sed -i '/GRAPHITE BEGIN/,/GRAPHITE END/d' ${EVDEV_XML}
echo "Removed Graphite from xkb registry"
}

verify_user_is_root() {
if [ ! "${EUID:-$(id -u)}" -eq 0 ]; then
echo "This script must unfortunately be run with sudo";
exit 1
fi
}

verify_user_is_root
uninstall_layout