shithub: riow

Download patch

ref: 89647c0ea4464f090cf054d9a52ceb5d413c93f8
parent: 092f29dc1f95cebedf11cd29aad4f07756b51e74
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Sun Mar 29 16:18:37 EDT 2020

desktop switching and moving windows

--- a/README.md
+++ b/README.md
@@ -22,6 +22,8 @@
 ## Keys
 
 ```
-G-f       toggle fullscreen for the current window
-G-enter   start a new window
+G-f              toggle fullscreen for the current window
+G-enter          start a new window
+G-[0..9]         switch to a specific virtual desktop
+G-shift-[0..9]   move the current window to a specific virtual desktop
 ```
--- a/riow
+++ b/riow
@@ -1,10 +1,13 @@
 #!/bin/rc
 rfork ne
 
+# these are shown on every desktop
+sticky=(kbmap stats winwatch)
+
 alt=0
 ctl=0
-glenda=0
 shift=0
+curdesktop=1
 
 fn scrsize {
 	s=`{syscall -o read 0 buf 60 </dev/screen >[2]/dev/null}
@@ -12,16 +15,26 @@
 	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=`{syscall -o read 0 buf 64 <$f/wctl >[2]/dev/null}
+		a=`{winread $f}
 		if(~ $a(5) current){
 			winid=`{basename $f}
-			if(~ $a(1) 0 && ~ $a(2) 0 && ~ $a(3) $screenw && ~ $a(4) $screenh)
-				echo resize -r `{eval echo '$winsize_'^$winid} >$f/wctl
+			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 {
-				eval 'winsize_'^$winid'=$a(1 2 3 4)'
+				echo -n $a(1 2 3 4) >/env/winsize_^$winid
 				echo resize -r 0 0 9999 9999 >$f/wctl
 			}
 		}
@@ -28,11 +41,44 @@
 	}
 }
 
+fn desktop {
+	for(f in /dev/wsys/*){
+		if(! ~ `{winlabel $f} $sticky){
+			a=`{winread $f}
+			winid=`{basename $f}
+			windesk=`{test -f /env/windesk_^$winid && cat /env/windesk_^$winid}
+
+			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){
+					if(~ $#windesk 0){ # no desktop and visible
+						# assign to the current one
+						echo -n $curdesktop >/env/windesk_^$winid
+					}
+					# and hide
+					echo hide >$f/wctl
+				}
+				if not { # hidden, unhide if should be shown now
+					if(~ $windesk $1)
+						echo unhide >$f/wctl
+				}
+			}
+		}
+	}
+	if(~ $shift 0)
+		curdesktop=$1
+}
+
 fn handle {
 	# remember old states
 	oalt=$alt
 	octl=$ctl
-	oglenda=$glenda
 	oshift=$shift
 
 	m=$1
@@ -42,31 +88,35 @@
 	shift
 
 	key=''
+	ukey=''
 	if(~ $state K && ~ $#* 0){
 		alt=0
 		ctl=0
-		glenda=0
 		shift=0
 	}
 	if not {
 		while(! ~ $#* 0){
-			if(~ $1 alt || ~ $1 ctl || ~ $1 glenda || ~ $1 shift){
+			if(~ $1 alt || ~ $1 ctl || ~ $1 shift)
 				eval '$1=$state'
-			}
 			if not {
-				# only react to 'c'
 				if(~ $m c) 
 					key=$1
+				if(~ $m k)
+					ukey=$1
 			}
 			shift
 		}
 	}
-	if(~ $key enter)
-		window
-	if(~ $key f)
-		fullscreen
+	if(~ $alt 0 && ~ $ctl 0 && ~ $shift 0){
+		if(~ $key enter)
+			window
+		if(~ $key f)
+			fullscreen
+	}
+	if(~ $ukey [0-9] && ! ~ $ukey $curdesktop)
+		desktop $ukey
 }
 
-gkbd | while(s=`{read }){
+gkbd | while(s=`{read}){
 	handle $s
 }