shithub: riow

ref: 67b94a1422e25ad2e19ad89206772958d7bb6a72
dir: riow/riow

View raw version
#!/bin/rc
rfork ne

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

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

fn scrsize {
	s=`{syscall -o read 0 buf 60 </dev/screen >[2]/dev/null}
	screenw=$s(4)
	screenh=$s(5)
}

fn winread {
	syscall -o read 0 buf 120 <$1/wctl >[2]/dev/null
}

fn winlabel {
	cat $1/label
}

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

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
		}
		if not {
			echo -n $a(1 2 3 4) >/env/winsize_^$winid
			echo resize -r 0 0 9999 9999 >$f/wctl
		}
	}
}

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
	}

	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)
							echo 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){
		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)
				echo current >/dev/wsys/^$winid^/wctl
		}
		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 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(~ $m k) ukey=$1
		}
		shift
	}
	if(~ $ctl 0 && ~ $alt 0){
		if(~ $shift 0){
			if(~ $key enter) window
			if(~ $key f)     togglefullscreen
			if(~ $key s)     togglesticky
		}
		if(~ $ukey [0-9] && ! ~ $ukey $curdesktop)
			desktop $ukey
	}
}

scrsize

fn work {
	echo '    '^$curdesktop
	while(s=`{read}){
		handle $s
		echo '    '^$curdesktop
	}
}

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