shithub: riow

ref: ad83212cd0e26c68f769037fdf787b891a509ee3
dir: riow/riow

View raw version
#!/bin/rc
rfork ne

# these are shown on every desktop
sticky=(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 fullscreen {
	scrsize
	for(f in /dev/wsys/*){
		a=`{winread $f}
		if(~ $a(5) current){
			winid=`{basename $f}
			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){
		for(f in /dev/wsys/*){
			a=`{winread $f}
			winid=`{basename $f}
			if(~ $a(5) current)
				echo -n $winid >/env/windeskcur_^$curdesktop
		}
	}

	for(f in /dev/wsys/*){
		if(! ~ `{winlabel $f} $sticky){
			a=`{winread $f}
			winid=`{basename $f}

			if(~ $shift 1){ # moving the current window elsewhere
				if(~ $a(5) current){
					echo -n $1 >/env/windesk_^$winid
					echo 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
					echo 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)
						touch /env/winhidden_^$winid
				}
			}
		}
	}
	if(~ $shift 0){
		if(test -f /env/windeskcur_^$1){
			winid=`{cat /env/windeskcur_^$1}
			if(test -f /env/windesk_^$winid && ~ `{cat /env/windesk_^$winid} $1)
				echo current >/dev/wsys/^$winid^/wctl
		}
		curdesktop=$1
	}
}

fn handle {
	# remember old states
	oalt=$alt
	octl=$ctl
	oshift=$shift

	m=$1
	state=0
	if(~ $1 k)
		state=1
	shift

	key=''
	ukey=''
	if(~ $state 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(~ $alt 0 && ~ $ctl 0 && ~ $shift 0){
		if(~ $key enter)
			window
		if(~ $key f)
			fullscreen
	}
	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