#!/usr/bin/env python

"""
This script sets my keyboard repeat settings via `xset`, because the
GNOME Shell desktop environment doesn't give any configurable setting
to control the key repeat speed and I don't wanna fight it all the time.

I set this script to run on auto-start and set my key repeat setting.
"""

import subprocess
import os

# My settings.
settings = dict(
    delay=200,  # Milliseconds
    rate=100,   # Characters per second?
)

if __name__ == "__main__":
    # Only do this for GNOME.
    current_desktop = os.environ.get("XDG_CURRENT_DESKTOP")
    if current_desktop is None or current_desktop.upper() == "GNOME":
        print("Setting delay to {delay} and rate {rate}".format(**settings))
        subprocess.call("xset r rate {delay} {rate}".format(**settings), shell=True)
    else:
        subprocess.call([
            "notify-send",
            "-u", "critical",
            "-i", "preferences-desktop-keyboard",
            "keyboard-repeat: Error - Are you on GNOME? "
            "I detected {}".format(current_desktop)
        ])