Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Doppler committed Aug 13, 2015
0 parents commit 618e8e7
Show file tree
Hide file tree
Showing 9 changed files with 414 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
MKDIR=mkdir -p
CP=cp
PREFIX=/usr/local
SH=/bin/sh

all: none

none:
@echo "Nothing to be done, run `make install` instead"

install: bin bashcomp zshcomp scripts

bin: tdm tdmctl
$(MKDIR) $(PREFIX)/bin
$(CP) $^ $(PREFIX)/bin

bashcomp: tdmctl.bashcomp
$(MKDIR) $(PREFIX)/share/bash-completion/completions
$(CP) $^ $(PREFIX)/share/bash-completion/completions/tdmctl

zshcomp: _tdmctl
$(MKDIR) $(PREFIX)/share/zsh/site-functions
$(CP) $^ $(PREFIX)/share/zsh/site-functions

scripts: tdmexit tdminit
$(MKDIR) $(PREFIX)/share/tdm
$(CP) $^ $(PREFIX)/share/tdm
$(MKDIR) $(PREFIX)/share/tdm/sessions
$(SH) ./links.sh /usr/bin $(PREFIX)/share/tdm/sessions
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tdm
===

This is not the official TDM source, but since I can't find it,
I provide my own version for my AUR package.
30 changes: 30 additions & 0 deletions _tdmctl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#compdef tdmctl

_tdmctl(){
local curcontext="$curcontext" state line
typeset -A opt_args

_arguments \
'1: :->actions'\
'*: :->options'

case $state in
actions)
_arguments "1:Actions:(init list cache check default add enable disable)"
;;
*)
case $words[2] in
check|default|disable)
compadd "$@" $(tdmctl list)
;;
enable)
compadd "$@" $(tdmctl cache)
;;
*)
;;
esac
;;
esac
}

_tdmctl "$@"
15 changes: 15 additions & 0 deletions links.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#! /bin/sh

# $1 is the binaries directory
# $2 is the symlinks directory

if [ -z "$2/$1" ] ; then exit 1 ; fi
if [ -z "$2/$2" ] ; then exit 1 ; fi

ln -s "$1/enlightenment_start" "$2/E17"
ln -s "$1/startfluxbox" "$2/Fluxbox"
ln -s "$1/gnome-session" "$2/GNOME"
ln -s "$1/icewm-session" "$2/ICEWM"
ln -s "$1/pekwm" "$2/PekWM"
ln -s "$1/startxfce4" "$2/XFCE4"
ln -s "$1/openbox-session" "$2/openbox"
172 changes: 172 additions & 0 deletions tdm
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#!/bin/bash

# TDM, tbk display manager, or tiny display manager,
# is a session selector after login.
# It links the starting script to default and start
# the startx script.
VERSION=0.4.0

# directory settings
CONFDIR="$HOME/.tdm"
SESSIONS=${CONFDIR}/sessions
EXTRA=${CONFDIR}/extra
SAVELAST=1

nulltype(){
type "$1" > /dev/null 2>/dev/null
}

fallback(){
if [[ -n $1 ]]; then
echo -e "\033[31;1m$1\033[0m"
fi
exec $SHELL
}

get_needs(){
local _type=$(file -p --mime-type "$1" | awk '{print $2}' | cut -d'/' -f1)
if [ ${_type} == "text" ]; then
grep '#[ \t]*needs[ \t]*:' "$1"|sed -e 's/#.*://g'
fi
}

valid_item(){
local needs
needs=($(get_needs "$1"))
for _i in ${needs[@]}
do
nulltype ${_i} || return 1
done
return 0
}

# started from startx, so start session
if [[ -n $1 && $1 = "--xstart" ]]; then
if [[ -f "${CONFDIR}/tdmexit" ]]; then
. "${CONFDIR}/tdmexit"
fi
if [[ -x "/tmp/tdmdefault" ]]; then
exec /tmp/tdmdefault
else
exec "${CONFDIR}/default"
fi
fi

clear
# check for a 'good' tty
(basename $(tty)|grep -q tty) || fallback "Invalid tty"

# X started, exit
pgrep X>/dev/null&&fallback 'X started.'

# build confdir
if [ ! -d "${CONFDIR}" ]; then
tdmctl init
fi

# otherwise, run as the session chosen script
if [[ -f "${CONFDIR}/tdminit" ]]; then
source "${CONFDIR}/tdminit"
fi

# XID: X session Number
# TOTAL: Number of items
# sid : id that user selects

let XID=0
let TOTAL=0
xsessions=()
prglist=()
DEFOPT=""

if [[ -x "${CONFDIR}/default" ]]; then
SDEFAULT=$(readlink "${CONFDIR}/default")
DEFAULTWM=$(basename ${SDEFAULT})
else
SDEFAULT=
DEFAULTWM=
fi

if [ -d ${SESSIONS} ]; then
for script in "${SESSIONS}"/*; do
if [ -x "${script}" ] && valid_item "${script}" ; then
xsessions[$XID]="${script}"
NAME=$(basename ${script})
prglist=(${prglist[@]} ${XID} ${NAME})
if [ "${NAME}" == ${DEFAULTWM} ]; then
DEFOPT="--default-item ${XID}"
fi
let XID=$(($XID+1))
let TOTAL=$(($TOTAL+1))
fi
done
else
echo "${SESSIONS} doesn't exist."
echo "making this directory."
mkdir -p ${SESSIONS}
fi

if [ -d ${EXTRA} ]; then
for script in "${EXTRA}"/*; do
if [ -x "${script}" ] && valid_item "${script}" ; then
xsessions[$TOTAL]="${script}"
NAME="extra/$(basename ${script})"
prglist=(${prglist[@]} ${TOTAL} ${NAME})
let TOTAL=$(($TOTAL+1))
fi
done
fi

if [ $TOTAL -eq 0 ]; then
fallback "No sessions found."
fi

tdm_curses(){
sid=$(dialog --stdout ${DEFOPT} --menu "TDM ${VERSION}" 0 0 0 ${prglist[@]})
[ -n "$sid" ]||fallback "Falling back to shell."
}

tdm_text(){
echo "This is TDM ${VERSION}, a tiny display manager."
echo "Please select from the following: (default ${DEFAULTWM})"

local _i=0
while [ ${_i} -lt ${TOTAL} ]
do
echo ${_i} ${prglist[${_i}*2+1]}
let _i=${_i}+1
done

echo -n "Program ID: "
read sid
}

if ! nulltype dialog; then
#no dialog program, force to use tdm_text
TDMUI=tdm_text
elif [ ! "${TDMUI}" == "tdm_text" ]; then
TDMUI=tdm_curses
fi
${TDMUI}

rm -f /tmp/tdmdefault
if [[ (-n $sid) && ($sid -lt $TOTAL) && ($sid -ge $XID) ]]; then
exec ${xsessions[$sid]}
elif [[ (-n $sid) && ($sid -lt $XID) && ($sid -ge 0) ]]; then
if [[ ${SAVELAST} -ne 0 ]]; then
ln -sf ${xsessions[${sid}]} "${CONFDIR}/default"
else
ln -sf ${xsessions[${sid}]} "/tmp/tdmdefault"
fi
startx
logout
else
echo "Unknown value,load default."
if [ -x "${CONFDIR}/default" ]; then
startx
logout
else
fallback "Session not defined,fallback."
fi
fi

113 changes: 113 additions & 0 deletions tdmctl
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/bin/bash
#tdmctl: a tool to configure tdm

CONFDIR="$HOME/.tdm"
CACHEDIR="${CONFDIR}/cache"
PREFIX=/usr

init(){
[[ "$1" = "-f" || "$1" = "--force" ]]&&rm -rf "${CONFDIR}"
# build the directory tree if not exist
if [[ ! -d "${CONFDIR}" ]]; then
cp -Rv "${PREFIX}/share/tdm" "${CONFDIR}"
else
echo "Nothing done."
fi
}

usage(){
echo "tdmctl init: initialize the config directory."
echo "tdmctl list: list available X sessions."
echo "tdmctl cache: list cached files."
echo "tdmctl check <session>: see what <session> is."
echo "tdmctl default [session]: show/set default X session."
echo "tdmctl add <name> <path> [X(default)/extra]: add a session."
echo "tdmctl enable/disable <session>: enable/disable session."
exit
}

check(){
readlink "$1" || cat "$1"
}

if [ ! -n "$1" ]; then
usage
exit
fi


case "$1" in
init)
shift
init "$@"
;;
list)
for session in "$CONFDIR/sessions"/*; do
[ -x "$session" ] && echo $(basename "$session")
done
;;
cache)
for file in "$CACHEDIR"/*; do
fn=$(basename "$file")
echo ${fn:1}
done
;;
default)
if [ ! -n "$2" ]; then
echo "Checking $(readlink "$CONFDIR/default")"
check $(readlink "$CONFDIR/default")
elif [ -x "$CONFDIR/sessions/$2" ]; then
echo "Setting default to $2"
ln -sf "$CONFDIR/sessions/$2" "$CONFDIR/default"
else
echo "tdmctl error: $2 is not available"
fi
;;
check)
if [ ! -n "$2" ]; then
usage
fi
FILE="$CONFDIR/sessions/$2"
if [ -f "$FILE" ]; then
check "$FILE"
else
echo "$2 not exist!"
exit 1
fi
;;
add)
[ -n "$3" ]||usage
if [[ "$4" == "X" || "$4" == "" ]]; then
ln -s "$3" "${CONFDIR}/sessions/$2"
elif [ "$4" == "extra" ]; then
ln -s "$3" "${CONFDIR}/extra/$2"
else
usage
fi
;;
enable)
if [ -f "${CACHEDIR}/X$2" ]; then
cp -v "${CACHEDIR}/X$2" "${CONFDIR}/sessions/$2"
fi
if [ -f "${CACHEDIR}/E$2" ]; then
mv -v "${CACHEDIR}/E$2" "${CONFDIR}/extra/$2"
fi
;;
disable)
if [ ! -d "${CACHEDIR}" ]; then
mkdir -p "${CACHEDIR}"
fi
# backup to cache
if [ -f "${CONFDIR}/sessions/$2" ]; then
mv -v "${CONFDIR}/sessions/$2" "${CACHEDIR}/X$2"
fi
if [ -f "${CONFDIR}/extra/$2" ]; then
mv -v "${CONFDIR}/extra/$2" "${CONFDIR}/E$2"
fi
;;
*)
usage
;;
esac


Loading

0 comments on commit 618e8e7

Please sign in to comment.