#!/bin/sh
#=============================
# titty - tiny tty (v1.4)
# display tiny text on eink
# requirements: titty.png.
# usage:
#   titty
#     cat self
#   titty [x y] [-k] [--] -|STRING|-f FILE
#     displays "STDIN", "echo STRING" or "cat FILE"
#       [x y]  initialize cursor at x y
#       [-k]   clear lines
#       [--]   treat as string
#   titty -s Y H S
#     move H-S lines S lines up, beginning with line Y+S
#   titty -mx|-my
#     return maximal x y coordinates
#   titty -v
#     return version
# hint: This runs at TTY speed.
#------------------------------
# Copyright (C) 2012 geekmaster
# MIT License: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#------------------------------
# Revision History (last change first):
# =ver= =date===== =author===== =description============================
# 1.4   2012-04-05 geekmaster   fix "end of page" bug
# 1.3   2012-04-05 kaminkatze   scrolling and options
# 1.2   2012-04-04 kaminkatze   k3 support and per line hexdump
# 1.1   2012-04-01 geekmaster   first release

# WARNING: If you don't stop using titty, you will go blind! :D

#===========================
# initvar - init global vars
#---------------------------
initvar() {
  V=1.4 # version
  eips -g "$0.png" # char map image
  DN=/dev/null DF=/dev/fb0 # devices
  set $(eips -i|grep res:); FW=$2 FH=$4 # fb0 width, height
  set $(eips -i|grep line); FS=$4 # fb0 stride
  CW=$((4*FS/FW)) CH=6 LH=7 # char width, height, line height
  LM=2 TM=$((CH*2)) # left, top margin px
  MX=$((FW*FS/FW-CW)) MY=$((FH-LH)) # max X, Y
  CX=$LM CY=$TM # cursor coords
  DD="dd if=$DF of=$DF bs=1 count=$CW" # ddblit
  DL="dd if=$DF of=$DF bs=1 count=$((FS*CH)) skip=$((FS*CH))" # clear line
  Q=false # quit flag
  K=false # clear line flag
  JFS=$IFS # normal field separator
}

#====================
# usage: scroll y h s
#--------------------
scroll() {
  dd if=$DF of=$DF bs=1 count=$((FS*LH*($2-$3))) \
    skip=$((FS*(LH*($1+$3)+TM))) seek=$((FS*(LH*$1+TM))) 2>$DN
  eips ''
}

#=================
# titty - tiny tty
# displays STDIN
#------------------
titty() {
  until $Q ;do IFS='\n'; read L || Q=true; IFS=$JFS
    for A in $(echo -n "$L" | hexdump -ve '/1 "%d\n"'); do
      if [[ $A -ne 32 ]]; then
        RO=$(((A-32)*CW)) FO=$((CY*FS+CX))
        for y in $(seq 1 $CH);do
          $DD skip=$RO seek=$FO 2>$DN; RO=$((RO+FS)) FO=$((FO+FS))
        done
      fi; CX=$((CX+CW)); [[ $((CX%64)) -eq $LM ]] && eips ''
      if [[ $CX -gt $MX ]]; then CX=$LM CY=$((CY+LH))
        [[ $CY -gt $MY ]] && CY=$TM CX=$LM
        $K && $DL seek=$((CY*FS)) 2>$DN; fi
    done; eips ''
    CX=$LM CY=$((CY+LH)); [[ $CY -gt $MY ]] && break
    $K && $DL seek=$((CY*FS)) 2>$DN
  done
}

# Do stuff!  :-)
initvar
if [[ "$1" == "" ]]; then cat "$0"|titty
else while [[ "$1" ]]; do
  case "$1" in
    -s) scroll $2 $3 $4; shift;shift;shift;shift;;
    -mx) echo $((FS/CW-CW)); shift;;
    -my) echo $ML; shift;;
    -f) cat "$2"|titty; shift;shift;;
    -) titty; shift;;
    -k) if [[ $CX -eq $LM ]]; then $DL seek=$((CY*FS)) 2>$DN
        else for y in $(seq 0 $((CH-1))); do
          dd if=$DF of=$DF bs=1 count=$((FS-CX)) \
            skip=$((FS*CH)) seek=$(((CY+y)*FS+CX)) 2>$DN
        done; fi; K=true; shift;;
    --) echo -n "$2"|titty; shift;shift;;
    -v) echo $V; shift;;
    *) if [[ -n "$3" ]]
       then CX=$((LM+$1*CW)) CY=$((TM+$2*LH)); shift;shift
       else echo -n "$1"|titty; shift
       fi;;
  esac
done; fi
