aboutsummaryrefslogtreecommitdiffstats
path: root/startw
blob: ce235ffc9cddc4fa8c5e1cc65d7b05370854ec24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/env bash

# This file roughly emulates the `startx' command in that it setups up the
# environment for a Wayland session based upon what is specificed in the
# `~/.winitrc' file.

VERSION=1.1.1
CONFIG= # default is in .winitrc file

# Keyboard Config String particulars
# - it is assummed that the string will be of the format:
#   "layout;model;variant;options", e.g.
#   "us;;;ctrl:nocaps" for a US keyboard with no caps-lock
DIVIDER=";" # this what we use to tokenise the string

# Some helper functions
error_log()
{
    echo "${@}" >&2
}

# FIXME We should not be flooding the environment like this, we should
#       be passing the tokens to the config file and let the user
#       decide how to interpret these.
parse_keyboard_string()
{
    local keyboards="${1}"
    local tokens=( $(echo "${keyboards}" | tr "${DIVIDER}" "\n") )
    export XKB_DEFAULT_LAYOUT="${tokens[0]}"
    export XKB_DEFAULT_MODEL="${tokens[1]}"
    export XKB_DEFAULT_VARIANT="${tokens[2]}"
    export XKB_DEFAULT_OPTIONS="${tokens[3]}"
    unset keyboards
    unset tokens
}

# Parse arguments
while getopts ":hvc:k:" opt; do
    case ${opt} in
        h)
            echo "Usage: ${0} [-h] [-v] [-c CONFIG] [-k KEYBOARDCONF]"
            exit 0
            ;;
        v)
            echo "${0} v. ${VERSION}"
            exit 0
            ;;
        c)
            CONFIG="${OPTARG}"
            ;;
        k)
            parse_keyboard_string "${OPTARG}"
            ;;
        \?)
            error_log "Invalid option: ${OPTARG}!"
            exit -1
            ;;
        :)
            error_log "Option -${OPTARG} requires an argument!"
            exit -1
            ;;
    esac
done

# Sanity checks
# - make sure we aren't in X/Wayland session
VT="$(tty)"
if [[ ${VT:5:1} != "t" ]]; then
    error_log "Unable start as we are in a X/Wayland session or PTS!"
    exit -1
fi

# execute the .winitrc file
if [[ ! -z "$CONFIG" ]]; then
    source "$CONFIG"
else
    source "${HOME}/.winitrc"
fi