Dm.sh

From wikinotes

I wrote a shellscript dm inspired by cdm.
Just like cdm, you login on tty1 and are prompted with various environment choices.
This has several advantages:

  • login before xorg is started lets you alter how it is started (ex: different xorg.conf files etc)
  • music, wallpapers, and mounts are all preserved through chosen WM cycles
  • it is much more painless to test games in different configurations
# dm.sh -- call this from your shell's profile and choose window-manager and Xorg config to start.
#
# customize '$CHOICES' to suit your various login environments.
#
{
    # Format:  ${MENU_ITEM} ${COMMAND} ...
    CHOICES=(
        "i3      (norm)"
            "/bin/startx /bin/i3 -- -config xorg.conf-desktop"

        "i3      (wide)"
            "/bin/startx /bin/i3 -- -config xorg.conf-widescreen-gaming"

        "openbox (normal)"
            "(sleep 1.5 && DISPLAY=:0 ~/.autostart && DISPLAY=:0 st) & /bin/startx /bin/openbox -- -config xorg.conf-desktop"

        "openbox (wide)"
            "(sleep 1.5 && DISPLAY=:0 ~/.autostart && DISPLAY=:0 st) & /bin/startx /bin/openbox -- -config xorg.conf-widescreen-gaming"
    )
    # NOTE: normally, openbox-session is used but it stops mpd and other services.
    #       using openbox directly requires some additional setup.
    #       launching 'st' in advance is a trick to force Xorg to redraw itself.
    
    
    show_menu() {
        # """ prints menu w/ choices
        # """
        clear
        echo "Choose Desktop:"
        for ((i=1; i<=${#CHOICES[@]}; i+=2)); do
            local index="$(expr $i / 2)"
            local name="${CHOICES[$i]}"
            echo "    ${index}) ${name}" 
        done
    }

    
    print_error() {
        # """ prints an error with a sleep
        # """
        message="$1"
        echo
        echo
        echo $message
        sleep 2
    }
    
    
    choose_menu_index() {
        # """ requests an integer with the menu choice you'd like to load
        # """
        local num_choices="$(expr $(expr ${#CHOICES[@]} / 2) - 1)"
        echo
        echo -n "Press (0-${num_choices} [default:0]): " && read index
    
        # defaults to 0
        test -z "$index" && index=0

        # must be integer
        if ! [ "$index" -eq "$index" ] 2> /dev/null ; then
            print_error "Expected Integer. Received: ${index}"
            return 1
        fi

        # reject numbers above $num_choices
        if test "$index" -gt "$num_choices" ; then
            print_error "Expected Integer <${num_choices}. Received: ${index}"
            return 1
        fi
    
        # exec chosen menu, after newline
        local array_index=$((2*$(expr $index + 1)))

        eval "${CHOICES[$array_index]}"
    }
    
    
    mainloop() {
        # only on tty1, infinite loop selecting WMs once logged in.
        # Ctrl+C to logout
        if [[ $(tty) == /dev/tty1 ]] ; then
            while true; do
                show_menu
                choose_menu_index
            done
        fi
    }


    clean_exit() {
        # logs out of current shell on ctrl-c etc
        exit 0
    }
    
    
    trap clean_exit SIGHUP SIGINT SIGQUIT SIGABRT
    mainloop $* 
}