Browsing sigrid/riow at commit HEAD: /riow

view raw
#!/bin/rc
rfork ne

# these are shown on every desktop
sticky=(bar 'cat clock' clock desktop faces kbmap stats winwatch)

# window move/resize step (in pixels)
step=96
stepsmall=8

alt=0
ctl=0
shift=0
curdesktop=1

fn scrsize {
	s=`{dd -quiet 1 -if /dev/screen -bs 60 -count 1 >[2]/dev/null}
	screenw=$s(4)
	screenh=$s(5)
}

fn winread {
	dd -quiet 1 -if $1/wctl -bs 120 -count 1 >[2]/dev/null
}

fn winlabel {
	cat $1/label
}

fn curwindow {
	cur=''
	for(f in /dev/wsys/*){
		a=`{winread $f}
		if(~ $a(5) current)
			cur=`{basename $f}
	}
	echo -n $cur
}

fn togglefullscreen {
	winid=`{curwindow}
	if(! ~ $#winid 0){
		f=/dev/wsys/^$winid
		a=`{winread $f}
		scrsize
		if(~ $a(1) 0 && ~ $a(2) 0 && ~ $a(3) $screenw && ~ $a(4) $screenh){
			if(test -f /env/winsize_^$winid)
				echo resize -r `{cat /env/winsize_^$winid} >$f/wctl >[2]/dev/null
		}
		if not {
			echo -n $a(1 2 3 4) >/env/winsize_^$winid
			echo resize -r 0 0 9999 9999 >$f/wctl >[2]/dev/null
		}
	}
}

fn desktop {
	# find and remember the current window
	if(~ $shift 0){
		winid=`{curwindow}
		if(! ~ $#winid 0)
			echo -n $winid >/env/windeskcur_^$curdesktop
		if not
			rm -f /env/windeskcur_^$curdesktop
	}

	unhide=()
	hide=()
	for(f in /dev/wsys/*){
		winid=`{basename $f}
		if(! ~ `{winlabel $f} $sticky && ! test -f /env/winsticky_^$winid){
			a=`{winread $f}

			if(~ $shift 1){ # moving the current window elsewhere
				if(~ $a(5) current){
					echo -n $1 >/env/windesk_^$winid
					hide=($hide $f/wctl)
				}
			}
			if not {
				# go through all visible windows
				if(~ $a(6) visible){
					# assign to the current one
					echo -n $curdesktop >/env/windesk_^$winid
					# it was visible, make sure riow doesn't think otherwise later on
					rm -f /env/winhidden_^$winid
					# and hide later
					hide=($hide $f/wctl)
				}
				if not { # hidden
					windesk=`{test -f /env/windesk_^$winid && cat /env/windesk_^$winid}
					if(~ $windesk $1){ # unhide if should be shown now
						# unless it's supposed to stay hidden, of course
						if(! test -f /env/winhidden_^$winid)
							unhide=($unhide $f/wctl)
					}
					# and remember it was hidden on the desktop we're still on
					if(~ $windesk $curdesktop && ! test -f /env/winhidden_^$winid)
						touch /env/winhidden_^$winid
				}
			}
		}
	}
	if(~ $shift 0){
		winid=()
		if(test -f /env/windeskcur_^$1){
			winid=`{cat /env/windeskcur_^$1}
			if(test -d /dev/wsys/^$winid && test -f /env/windesk_^$winid && ~ `{cat /env/windesk_^$winid} $1)
				;
			if not
				winid=()
		}
		for(w in $unhide)
			echo unhide >$w
		if(! ~ $#winid 0){
			echo top >/dev/wsys/^$winid^/wctl >[2]/dev/null
			echo current >/dev/wsys/^$winid^/wctl >[2]/dev/null
		}
		curdesktop=$1
	}
	for(w in $hide)
		echo hide >$w &
}

fn togglesticky {
	winid=`{curwindow}
	if(! ~ $#winid 0){
		if(test -f /env/winsticky_^$winid)
			rm -f /env/winsticky_^$winid
		if not
			touch /env/winsticky_^$winid
	}
}

fn arrows {
	winid=`{curwindow}
	if(! ~ $#winid 0){
		f=/dev/wsys/^$winid
		x=+0
		y=+0
		if(~ $2 0)
			s=$step
		if not
			s=$stepsmall
		~ $1 up    && y=-$s
		~ $1 right && x=+$s
		~ $1 down  && y=+$s
		~ $1 left  && x=-$s

		if(~ $3 0 && ~ $4 0)
			echo move -minx $x -miny $y >> $f/wctl
		if not {
			if(~ $4 0)
				echo resize -maxx $x -maxy $y >> $f/wctl
			if not
				echo resize -minx $x -miny $y >> $f/wctl
		}
	}
}

fn handle {
	# K is key down
	# k is key up
	# c is key (and repeat) with the shift applied

	key='' # depending on the shift state (1 → !)
	ukey='' # stays the same regardless of the shift state
	m=$1
	state=0
	if(~ $1 k)
		state=1
	shift
	if(~ $m K && ~ $#* 0){
		alt=0
		ctl=0
		shift=0
	}
	if not while(! ~ $#* 0){
		if(~ $1 alt || ~ $1 ctl || ~ $1 shift)
			eval '$1=$state'
		if not if(~ $m c) key=$1
		if not if(~ $m k) ukey=$1
		shift
	}
	if(~ $ctl 0 && ~ $alt 0){
		if(~ $shift 0){
			if(~ $key enter)    window >[2]/dev/null
			if not if(~ $key f) togglefullscreen
			if not if(~ $key s) togglesticky
		}
		if(~ $ukey [0-9] && ! ~ $ukey $curdesktop)
			desktop $ukey
	}
	arrows $key $shift $ctl $alt
}

scrsize

fn work {
	echo $curdesktop
	while(s=`{read}){
		olddesktop=$curdesktop
		handle $s
		if(! ~ $olddesktop $curdesktop)
			echo $curdesktop
	}
}

fn display {
	bar || aux/statusmsg -k -w `{echo $screenw-100|bc}^,`{echo $screenh-60|bc}^,$screenw^,$screenh desktop
}

gkbd < `{echo $wsys | sed 's/rio\./riogkbd./'} | work | display