From 5945ad7d0966851bcaf236b1d5caed66948e570b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Wed, 13 Sep 2023 16:52:29 -0400 Subject: Add debugging info and fix editor file to match terminal keyboard --- virtual-programs/editor.folk | 196 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 virtual-programs/editor.folk (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk new file mode 100644 index 00000000..418e9cec --- /dev/null +++ b/virtual-programs/editor.folk @@ -0,0 +1,196 @@ +# TODO: Make this not-hard-coded +# In fact, this shouldn't be "editor-1" at all but +# probably should be the MAC address or some Bluetooth +# thread ID? +set id "editor-1" + +##### +# To match to this I have another program in folk0 with: +# Claim $this is editor editor-1 +##### +When /page/ is editor /n/ & /page/ has region /r/ { + Wish $page is outlined white + Claim $id has region [region move $r up 450px] +} + +When $id has region /r/ { + Wish $id is outlined white +} + +proc checkForModifier {key} { + set r "" + switch $key { + LEFTSHIFT { set r "shift" } + RIGHTSHIFT { set r "shift" } + LEFTCTRL { set r "control" } + RIGHTCTRL { set r "control" } + } + return $r +} + +proc updateCursor {oldCursor updates} { + set newCursor $oldCursor + if {[dict exists $updates x]} { + lset newCursor 0 [expr {max(0, [dict get $updates x] + [lindex $oldCursor 0])}] + } + if {[dict exists $updates y]} { + lset newCursor 1 [expr {max(0, [dict get $updates y] + [lindex $oldCursor 1])}] + } + # TODO: Remove this before merging, it's just for debugging + puts "newCursor: $newCursor" + return $newCursor +} + +# NOTE: Should this go into a common-functions file? +proc x {vector} { lindex $vector 0 } +proc y {vector} { lindex $vector 1 } + +Commit cursor { Claim cursor is [list 0 0]} + +proc insertCharacter {code newCharacter cursor} { + set lines [split $code "\n"] + lassign $cursor x y + set x [expr {$x - 1}] + set line [lindex $lines $y] + set character [string cat [string index $line $x] $newCharacter] + set line [string replace $line $x $x $character] + lset lines $y $line + return [join $lines "\n"] +} + +proc swapCharacter {code newCharacter cursor} { + set lines [split $code "\n"] + lassign $cursor x y + set line [lindex $lines $y] + set line [string replace $line $x $x $newCharacter] + lset lines $y $line + return [join $lines "\n"] +} + +proc deleteCharacter {code cursor} { + set lines [split $code "\n"] + lassign $cursor x y + if {$x == 0 && $y > 0} { + set previousLine [lindex $lines [expr {$y - 1}]] + set thisLine [lindex $lines $y] + set mergedLine [string cat $previousLine $thisLine] + lset lines [expr {$y - 1}] $mergedLine + lreplace lines $y $y + } else { + set line [lindex $lines $y] + set line [string replace $line [expr {$x - 1}] [expr {$x - 1}] ""] + lset lines $y $line + } + return [join $lines "\n"] +} + +proc insertNewline {code cursor} { + set lines [split $code "\n"] + lassign $cursor x y + set line [lindex $lines $y] + set beforeCursor [string range $line 0 [expr {$x - 1}]] + set afterCursor [string range $line $x end] + set newLines [list $beforeCursor $afterCursor] + lset lines $y [join $newLines "\n"] + return [join $lines "\n"] +} + +proc insertCursor {code cursor count} { + if {$count % 2 == 0} { + return [swapCharacter $code " " $cursor] + } else { + return $code + } +} + +proc newRegion {program x y w h} { + set vertices [list [list [expr {$x+$w}] $y] \ + [list $x $y] \ + [list $x [expr {$y+$h}]] \ + [list [expr {$x+$w}] [expr {$y+$h}]]] + set edges [list [list 0 1] [list 1 2] [list 2 3] [list 3 0]] + Claim $program has region [region create $vertices $edges] +} + +# NOTE: This is convenient for me debugging (gets triggered with Ctrl + R) +# but should probably be "Claim this has halo message editor" or something +# not as intrusive as outlining to start? Just a thought. +set baseCode "Wish $id is outlined blue" +Commit code { Claim $id has program code $baseCode } + +proc drawText {text cursor count region} { + set lineNumber 1 + set showCursor [expr {$count % 2 == 0}] + set corner [lindex $region 0 1] + lassign $corner cornerX cornerY + foreach line [split $text "\n"] { + Display::text $cornerX $cornerY 5 $line 0 + } +} + +When $id has program code /code/ & /node/ has step count /count/ & cursor is /cursor/ & $id has region /r/ { + # NOTE: This is a useful debugging line as it flashes + # the actual letter corresponding to the cursor + # position. The current virtual cursor () + drawText [insertCursor $code $cursor $count] $cursor $count $r + # drawText $code $cursor $count $r + # Wish $id has halo message "$count | $cursor" + When keyboard claims key /key/ is down with modifiers /modifier/ { + Wish $id has halo message "k: $key | m: $modifier" + } +} + +# Must use `Every time` but it's incompatible with the keyboard claim or something, as written? Confer with Omar ... +Every time keyboard claims key /currentCharacter/ is down with modifiers /modifier/ & cursor is /cursor/ & $id has program code /code/ { + Commit cursor { + switch $currentCharacter { + UP { Claim cursor is [updateCursor $cursor {y -1}] } + DOWN { Claim cursor is [updateCursor $cursor {y 1}] } + RIGHT { Claim cursor is [updateCursor $cursor {x 1}] } + LEFT { Claim cursor is [updateCursor $cursor {x -1}] } + BACKSPACE { + Claim cursor is [updateCursor $cursor {x -1}] + Commit code { Claim $id has program code [deleteCharacter $code $cursor] } + } + SPACE { + Claim cursor is [updateCursor $cursor {x 1}] + Commit code { Claim $id has program code [insertCharacter $code " " $cursor] } + } + ENTER { + # Note: Still need to figure out the exact & text manipulation methods for all the + # different cases of enter. This is just a simple one for now. + # Claim cursor is [updateCursor $cursor {x [expr {-1 * [x $cursor]}] y 1}] + Claim cursor is [updateCursor $cursor {x -1 y 1}] + # NOTE: Useful debugging print line, still figuring out the math. + # Unforuntately this doesn't have consistent cursor spacing as the + # X-coordinate increases. + puts "calc: [expr {-1 * [x $cursor]}]" + Commit code { Claim $id has program code [insertNewline $code $cursor] } + } + LEFTSHIFT - + RIGHTSHIFT - + LEFTCTRL - + RIGHTCTRL { + Claim cursor is $cursor + } + default { + puts "currentCharacter: $currentCharacter" + Claim cursor is [updateCursor $cursor {x 1}] + # Unneccessary b/c of properly handled stuff?? + # if {$modifier == "shift"} { + # set currentCharacter [string toupper $currentCharacter] + # } + if {$modifier == "ctrl" & $currentCharacter == "p"} { + Wish to print $code with job id [expr {rand()}] + return + } + if {$modifier == "ctrl" & $currentCharacter == "r"} { + Commit code { Claim $id has program code $baseCode } + Claim cursor is [list 0 0] + return + } + Commit code { Claim $id has program code [insertCharacter $code $currentCharacter $cursor] } + } + } + } +} \ No newline at end of file -- cgit v1.2.3 From 1995b73386ec1dc4844c63fbb0a756c450e1e194 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Mon, 18 Sep 2023 17:21:10 -0400 Subject: Correctly angle editor text --- virtual-programs/editor.folk | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 418e9cec..2a557e55 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -115,16 +115,18 @@ proc newRegion {program x y w h} { # NOTE: This is convenient for me debugging (gets triggered with Ctrl + R) # but should probably be "Claim this has halo message editor" or something # not as intrusive as outlining to start? Just a thought. -set baseCode "Wish $id is outlined blue" +set baseCode "Wish $id is outlined blue\nWish $this is labelled hello" Commit code { Claim $id has program code $baseCode } proc drawText {text cursor count region} { set lineNumber 1 set showCursor [expr {$count % 2 == 0}] - set corner [lindex $region 0 1] + set corner [lindex $region 0 0] + set angle [region angle $region] lassign $corner cornerX cornerY foreach line [split $text "\n"] { - Display::text $cornerX $cornerY 5 $line 0 + Display::text $cornerX $cornerY 2 $line $angle + set lineNumber [+ $lineNumber 1] } } @@ -160,7 +162,7 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi # Note: Still need to figure out the exact & text manipulation methods for all the # different cases of enter. This is just a simple one for now. # Claim cursor is [updateCursor $cursor {x [expr {-1 * [x $cursor]}] y 1}] - Claim cursor is [updateCursor $cursor {x -1 y 1}] + Claim cursor is [updateCursor $cursor {x 0 y 1}] # NOTE: Useful debugging print line, still figuring out the math. # Unforuntately this doesn't have consistent cursor spacing as the # X-coordinate increases. -- cgit v1.2.3 From 10788f96d3de1b9530df047d4560b3d1e964c1e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Mon, 18 Sep 2023 17:54:43 -0400 Subject: Left align the lines in edited virtual program --- virtual-programs/editor.folk | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 2a557e55..b00a1ff8 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -9,14 +9,10 @@ set id "editor-1" # Claim $this is editor editor-1 ##### When /page/ is editor /n/ & /page/ has region /r/ { - Wish $page is outlined white + Wish $page is outlined gray Claim $id has region [region move $r up 450px] } -When $id has region /r/ { - Wish $id is outlined white -} - proc checkForModifier {key} { set r "" switch $key { @@ -37,7 +33,7 @@ proc updateCursor {oldCursor updates} { lset newCursor 1 [expr {max(0, [dict get $updates y] + [lindex $oldCursor 1])}] } # TODO: Remove this before merging, it's just for debugging - puts "newCursor: $newCursor" + # puts "newCursor: $newCursor" return $newCursor } @@ -121,11 +117,16 @@ Commit code { Claim $id has program code $baseCode } proc drawText {text cursor count region} { set lineNumber 1 set showCursor [expr {$count % 2 == 0}] - set corner [lindex $region 0 0] + set corner [region topleft $region] set angle [region angle $region] lassign $corner cornerX cornerY foreach line [split $text "\n"] { - Display::text $cornerX $cornerY 2 $line $angle + set lineCornerY [- $cornerY [* $lineNumber 50]] + # subtract $cornerX by half the length of the line to left-align it + set lineCornerX [- $cornerX [* [string length $line] 9]] + Display::text $lineCornerX $lineCornerY 2 $line $angle + Display::circle $cornerX $cornerY 50 5 gold + Display::circle $lineCornerX $lineCornerY 50 5 white set lineNumber [+ $lineNumber 1] } } @@ -166,7 +167,7 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi # NOTE: Useful debugging print line, still figuring out the math. # Unforuntately this doesn't have consistent cursor spacing as the # X-coordinate increases. - puts "calc: [expr {-1 * [x $cursor]}]" + # puts "calc: [expr {-1 * [x $cursor]}]" Commit code { Claim $id has program code [insertNewline $code $cursor] } } LEFTSHIFT - @@ -176,7 +177,6 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi Claim cursor is $cursor } default { - puts "currentCharacter: $currentCharacter" Claim cursor is [updateCursor $cursor {x 1}] # Unneccessary b/c of properly handled stuff?? # if {$modifier == "shift"} { -- cgit v1.2.3 From 1e51884c562badb265a111297d35b506af90930a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Wed, 20 Sep 2023 13:27:04 -0400 Subject: Remove keyboard program, add editor demo --- virtual-programs/editor.folk | 11 ++++++++--- virtual-programs/keyboard.folk | 36 ------------------------------------ 2 files changed, 8 insertions(+), 39 deletions(-) delete mode 100644 virtual-programs/keyboard.folk (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index b00a1ff8..ea9a177a 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -5,7 +5,7 @@ set id "editor-1" ##### -# To match to this I have another program in folk0 with: +# To match to this I have another program with: # Claim $this is editor editor-1 ##### When /page/ is editor /n/ & /page/ has region /r/ { @@ -162,8 +162,8 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi ENTER { # Note: Still need to figure out the exact & text manipulation methods for all the # different cases of enter. This is just a simple one for now. - # Claim cursor is [updateCursor $cursor {x [expr {-1 * [x $cursor]}] y 1}] - Claim cursor is [updateCursor $cursor {x 0 y 1}] + Claim cursor is [updateCursor $cursor {x [expr {-1 * [x $cursor]}] y 1}] + # Claim cursor is [updateCursor $cursor {x 0 y 1}] # NOTE: Useful debugging print line, still figuring out the math. # Unforuntately this doesn't have consistent cursor spacing as the # X-coordinate increases. @@ -195,4 +195,9 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi } } } +} + +Claim $this has demo { + # Tape this program underneath the main keyboard of your system to try it out + Claim $this is editor editor-1 } \ No newline at end of file diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk deleted file mode 100644 index a49ac29e..00000000 --- a/virtual-programs/keyboard.folk +++ /dev/null @@ -1,36 +0,0 @@ -return -if {!$::isLaptop} { return } -Wish $this is outlined skyblue - -Wish $this has neighbors - -global pageOrigin neighborAbove - -set ::neighborAbove none - -When $this has region /r/ { - lassign [lindex $r 0] one - set ::pageOrigin $one -} - - -When $this has neighbor /n/ { - Wish $n is outlined thick papayawhip - # Wish $this is labelled $::pageOrigin - When $n has region /r/ { - set neighborOrigin [lindex [lindex $r 0] 0] - lassign $neighborOrigin _ nOriginY - lassign $pageOrigin _ pageOriginY - if {$nOriginY < $pageOriginY} { - set ::neighborAbove $n - variable neighborCode none - When $neighborAbove has program code /c/ { - set neighborCode $c - Wish $neighborAbove is labelled "$neighborCode" - } - When the keyboard character log is /k/ { - Wish $this is labelled $k - } - } - } -} \ No newline at end of file -- cgit v1.2.3 From 17de4795a40ad5c743187b7e017ce6fa581fccf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Wed, 20 Sep 2023 13:54:33 -0400 Subject: Add initial cursor implementation --- virtual-programs/editor.folk | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index ea9a177a..5284948c 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -111,7 +111,7 @@ proc newRegion {program x y w h} { # NOTE: This is convenient for me debugging (gets triggered with Ctrl + R) # but should probably be "Claim this has halo message editor" or something # not as intrusive as outlining to start? Just a thought. -set baseCode "Wish $id is outlined blue\nWish $this is labelled hello" +set baseCode "Wish $id is outlined green\nWish $id is labelled hello" Commit code { Claim $id has program code $baseCode } proc drawText {text cursor count region} { @@ -122,22 +122,34 @@ proc drawText {text cursor count region} { lassign $corner cornerX cornerY foreach line [split $text "\n"] { set lineCornerY [- $cornerY [* $lineNumber 50]] - # subtract $cornerX by half the length of the line to left-align it set lineCornerX [- $cornerX [* [string length $line] 9]] Display::text $lineCornerX $lineCornerY 2 $line $angle - Display::circle $cornerX $cornerY 50 5 gold - Display::circle $lineCornerX $lineCornerY 50 5 white set lineNumber [+ $lineNumber 1] } } -When $id has program code /code/ & /node/ has step count /count/ & cursor is /cursor/ & $id has region /r/ { +proc drawCursor {cursorPosition region} { + # calculate relative to topLeft of region + set corner [region topleft $region] + + set cpX [* 1 [lindex $corner 0]] + set cpY [* 1 [lindex $corner 1]] + set cp [list $cpX $cpY] + set cursorTop [list $cpX [expr {$cpY - 50}]] + + Display::stroke [list $cp $cursorTop] 2 gray +} + +When $id has program code /code/ & the clock time is /t/ & cursor is /cursor/ & $id has region /r/ { # NOTE: This is a useful debugging line as it flashes # the actual letter corresponding to the cursor # position. The current virtual cursor () - drawText [insertCursor $code $cursor $count] $cursor $count $r - # drawText $code $cursor $count $r - # Wish $id has halo message "$count | $cursor" + set intTime [expr {int($t * 10)}] + drawText [insertCursor $code $cursor $intTime] $cursor $intTime $r + # Hide cursor, TODO: fix to calculate accurate position based on cursor position + # drawCursor $cursor $r + + # DEBUG INFO When keyboard claims key /key/ is down with modifiers /modifier/ { Wish $id has halo message "k: $key | m: $modifier" } -- cgit v1.2.3 From c8c0a48f635f2fd9dedd823dfbdceb0104d2c05e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Wed, 20 Sep 2023 15:51:25 -0400 Subject: Claim the cursor is ... --- virtual-programs/editor.folk | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 5284948c..f4856de5 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -32,8 +32,6 @@ proc updateCursor {oldCursor updates} { if {[dict exists $updates y]} { lset newCursor 1 [expr {max(0, [dict get $updates y] + [lindex $oldCursor 1])}] } - # TODO: Remove this before merging, it's just for debugging - # puts "newCursor: $newCursor" return $newCursor } @@ -41,7 +39,8 @@ proc updateCursor {oldCursor updates} { proc x {vector} { lindex $vector 0 } proc y {vector} { lindex $vector 1 } -Commit cursor { Claim cursor is [list 0 0]} +# TODO: Scope to "When the editor is out" or something +Commit cursor { Claim the cursor is [list 0 0]} proc insertCharacter {code newCharacter cursor} { set lines [split $code "\n"] @@ -111,7 +110,7 @@ proc newRegion {program x y w h} { # NOTE: This is convenient for me debugging (gets triggered with Ctrl + R) # but should probably be "Claim this has halo message editor" or something # not as intrusive as outlining to start? Just a thought. -set baseCode "Wish $id is outlined green\nWish $id is labelled hello" +set baseCode "Wish \$this is outlined green\nClaim first name is Andres" Commit code { Claim $id has program code $baseCode } proc drawText {text cursor count region} { @@ -140,7 +139,7 @@ proc drawCursor {cursorPosition region} { Display::stroke [list $cp $cursorTop] 2 gray } -When $id has program code /code/ & the clock time is /t/ & cursor is /cursor/ & $id has region /r/ { +When $id has program code /code/ & the clock time is /t/ & the cursor is /cursor/ & $id has region /r/ { # NOTE: This is a useful debugging line as it flashes # the actual letter corresponding to the cursor # position. The current virtual cursor () @@ -156,26 +155,26 @@ When $id has program code /code/ & the clock time is /t/ & cursor is /cursor/ & } # Must use `Every time` but it's incompatible with the keyboard claim or something, as written? Confer with Omar ... -Every time keyboard claims key /currentCharacter/ is down with modifiers /modifier/ & cursor is /cursor/ & $id has program code /code/ { +Every time keyboard claims key /currentCharacter/ is down with modifiers /modifier/ & the cursor is /cursor/ & $id has program code /code/ { Commit cursor { switch $currentCharacter { - UP { Claim cursor is [updateCursor $cursor {y -1}] } - DOWN { Claim cursor is [updateCursor $cursor {y 1}] } - RIGHT { Claim cursor is [updateCursor $cursor {x 1}] } - LEFT { Claim cursor is [updateCursor $cursor {x -1}] } + UP { Claim the cursor is [updateCursor $cursor {y -1}] } + DOWN { Claim the cursor is [updateCursor $cursor {y 1}] } + RIGHT { Claim the cursor is [updateCursor $cursor {x 1}] } + LEFT { Claim the cursor is [updateCursor $cursor {x -1}] } BACKSPACE { - Claim cursor is [updateCursor $cursor {x -1}] + Claim the cursor is [updateCursor $cursor {x -1}] Commit code { Claim $id has program code [deleteCharacter $code $cursor] } } SPACE { - Claim cursor is [updateCursor $cursor {x 1}] + Claim the cursor is [updateCursor $cursor {x 1}] Commit code { Claim $id has program code [insertCharacter $code " " $cursor] } } ENTER { # Note: Still need to figure out the exact & text manipulation methods for all the # different cases of enter. This is just a simple one for now. - Claim cursor is [updateCursor $cursor {x [expr {-1 * [x $cursor]}] y 1}] - # Claim cursor is [updateCursor $cursor {x 0 y 1}] + Claim the cursor is [updateCursor $cursor {x [expr {-1 * [x $cursor]}] y 1}] + # Claim the cursor is [updateCursor $cursor {x 0 y 1}] # NOTE: Useful debugging print line, still figuring out the math. # Unforuntately this doesn't have consistent cursor spacing as the # X-coordinate increases. @@ -186,10 +185,10 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi RIGHTSHIFT - LEFTCTRL - RIGHTCTRL { - Claim cursor is $cursor + Claim the cursor is $cursor } default { - Claim cursor is [updateCursor $cursor {x 1}] + Claim the cursor is [updateCursor $cursor {x 1}] # Unneccessary b/c of properly handled stuff?? # if {$modifier == "shift"} { # set currentCharacter [string toupper $currentCharacter] @@ -200,7 +199,7 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi } if {$modifier == "ctrl" & $currentCharacter == "r"} { Commit code { Claim $id has program code $baseCode } - Claim cursor is [list 0 0] + Claim the cursor is [list 0 0] return } Commit code { Claim $id has program code [insertCharacter $code $currentCharacter $cursor] } -- cgit v1.2.3 From 4776a0a6d2474ff5b04a553f9e04b92e25526ed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Wed, 20 Sep 2023 17:05:53 -0400 Subject: Fix up/down key behavior --- virtual-programs/editor.folk | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index f4856de5..aa39795a 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -110,7 +110,8 @@ proc newRegion {program x y w h} { # NOTE: This is convenient for me debugging (gets triggered with Ctrl + R) # but should probably be "Claim this has halo message editor" or something # not as intrusive as outlining to start? Just a thought. -set baseCode "Wish \$this is outlined green\nClaim first name is Andres" +# set baseCode "Wish \$this is outlined green\nClaim first name is Andres" +set baseCode "Claim\nWish \$this is outlined gold\nClaim this is a very looooooong line\n# Comment" Commit code { Claim $id has program code $baseCode } proc drawText {text cursor count region} { @@ -149,18 +150,44 @@ When $id has program code /code/ & the clock time is /t/ & the cursor is /cursor # drawCursor $cursor $r # DEBUG INFO - When keyboard claims key /key/ is down with modifiers /modifier/ { - Wish $id has halo message "k: $key | m: $modifier" + When the cursor is /cursor/ { + Wish $id has halo message "cursor: $cursor" } } +proc getCurrentLineLength {lines cursor} { + set splitLines [split $lines "\n"] + set currentLine [lindex $splitLines [lindex $cursor 1]] + string length $currentLine +} + # Must use `Every time` but it's incompatible with the keyboard claim or something, as written? Confer with Omar ... Every time keyboard claims key /currentCharacter/ is down with modifiers /modifier/ & the cursor is /cursor/ & $id has program code /code/ { Commit cursor { switch $currentCharacter { - UP { Claim the cursor is [updateCursor $cursor {y -1}] } - DOWN { Claim the cursor is [updateCursor $cursor {y 1}] } - RIGHT { Claim the cursor is [updateCursor $cursor {x 1}] } + UP { + set updatedCursor [updateCursor $cursor {y -1}] + set currentLineLength [getCurrentLineLength $code $updatedCursor] + if {[lindex $updatedCursor 0] > $currentLineLength} { + Claim the cursor is [list $currentLineLength [lindex $updatedCursor 1]] + } else { + Claim the cursor is $updatedCursor + } + } + DOWN { + set updatedCursor [updateCursor $cursor {y 1}] + set currentLineLength [getCurrentLineLength $code $updatedCursor] + if {[lindex $updatedCursor 0] > $currentLineLength} { + Claim the cursor is [list $currentLineLength [lindex $updatedCursor 1]] + } else { + Claim the cursor is $updatedCursor + } + } + RIGHT { + set currentLineLength [getCurrentLineLength $code $cursor] + # TODO: Check if the cursor is at the end of the line, if so cap it + Claim the cursor is [updateCursor $cursor {x 1}] + } LEFT { Claim the cursor is [updateCursor $cursor {x -1}] } BACKSPACE { Claim the cursor is [updateCursor $cursor {x -1}] -- cgit v1.2.3 From 1a652efefde2ff68866dc787c7e87188bb6270d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Fri, 22 Sep 2023 13:09:47 -0400 Subject: Add absolute cursor, prep for refactor --- virtual-programs/editor.folk | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index aa39795a..6387c363 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -128,16 +128,18 @@ proc drawText {text cursor count region} { } } +# TODO: refactor this to not reply on absolute region position but instead to compose as 0, 0 +# and then translate and rotate according to the virtual region as the final step proc drawCursor {cursorPosition region} { - # calculate relative to topLeft of region set corner [region topleft $region] - - set cpX [* 1 [lindex $corner 0]] - set cpY [* 1 [lindex $corner 1]] - set cp [list $cpX $cpY] - set cursorTop [list $cpX [expr {$cpY - 50}]] - - Display::stroke [list $cp $cursorTop] 2 gray + lassign $corner cornerX cornerY + lassign $cursorPosition cursorX cursorY + set cursorX [int [- $cornerX [* $cursorX 20]]] + set cursorY [int [- [- $cornerY [* $cursorY 65]] 20]] + set cursorX2 $cursorX + set cursorY2 [- $cursorY 50] + puts " $cursorPosition => $cursorX $cursorY" + Display::stroke [list [list $cursorX $cursorY] [list $cursorX2 $cursorY2]] 3 white } When $id has program code /code/ & the clock time is /t/ & the cursor is /cursor/ & $id has region /r/ { @@ -147,7 +149,7 @@ When $id has program code /code/ & the clock time is /t/ & the cursor is /cursor set intTime [expr {int($t * 10)}] drawText [insertCursor $code $cursor $intTime] $cursor $intTime $r # Hide cursor, TODO: fix to calculate accurate position based on cursor position - # drawCursor $cursor $r + drawCursor $cursor $r # DEBUG INFO When the cursor is /cursor/ { -- cgit v1.2.3 From 86d072c4205fec692176dc3a67c5b1799e3faa43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Fri, 22 Sep 2023 13:23:06 -0400 Subject: Fix halo angle, begin fixing editor text position --- virtual-programs/editor.folk | 10 +--------- virtual-programs/label.folk | 5 +++-- 2 files changed, 4 insertions(+), 11 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 6387c363..81b635c4 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -117,15 +117,7 @@ Commit code { Claim $id has program code $baseCode } proc drawText {text cursor count region} { set lineNumber 1 set showCursor [expr {$count % 2 == 0}] - set corner [region topleft $region] - set angle [region angle $region] - lassign $corner cornerX cornerY - foreach line [split $text "\n"] { - set lineCornerY [- $cornerY [* $lineNumber 50]] - set lineCornerX [- $cornerX [* [string length $line] 9]] - Display::text $lineCornerX $lineCornerY 2 $line $angle - set lineNumber [+ $lineNumber 1] - } + Display::text {*}[region centroid $region] 2 $text [region angle $region] } # TODO: refactor this to not reply on absolute region position but instead to compose as 0, 0 diff --git a/virtual-programs/label.folk b/virtual-programs/label.folk index ad5e406f..624ae8ad 100644 --- a/virtual-programs/label.folk +++ b/virtual-programs/label.folk @@ -19,7 +19,8 @@ fn text {coords text angle} { When /anyone/ wishes /p/ has halo message /message/ & /p/ has region /r/ { lassign [lindex $r 0] a b c d + set angle [region angle $r] lassign $a x y - text [list [lindex $a 0] [expr {[lindex $a 1] - 50}]] $message 0 - text [list [lindex $d 0] [expr {[lindex $d 1] + 50}]] $message 3.1415 + text [list [lindex $a 0] [expr {[lindex $a 1] - 50}]] $message $angle + text [list [lindex $d 0] [expr {[lindex $d 1] + 50}]] $message [+ 3.1415 $angle] } -- cgit v1.2.3 From 7a7a1e26487890b2bceb402e70440e1370cc11b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Fri, 22 Sep 2023 16:29:07 -0400 Subject: Add off-by-a-bit cursor box --- virtual-programs/editor.folk | 86 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 72 insertions(+), 14 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 81b635c4..5343abb8 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -111,7 +111,7 @@ proc newRegion {program x y w h} { # but should probably be "Claim this has halo message editor" or something # not as intrusive as outlining to start? Just a thought. # set baseCode "Wish \$this is outlined green\nClaim first name is Andres" -set baseCode "Claim\nWish \$this is outlined gold\nClaim this is a very looooooong line\n# Comment" +set baseCode "Claim\nWish \$this is outlined gold\n";#Claim this is a very looooooong line\n";## Comment\nset lorem 0\nset ipsum 0" Commit code { Claim $id has program code $baseCode } proc drawText {text cursor count region} { @@ -120,18 +120,76 @@ proc drawText {text cursor count region} { Display::text {*}[region centroid $region] 2 $text [region angle $region] } -# TODO: refactor this to not reply on absolute region position but instead to compose as 0, 0 -# and then translate and rotate according to the virtual region as the final step -proc drawCursor {cursorPosition region} { - set corner [region topleft $region] - lassign $corner cornerX cornerY +######## +# For reference, from folk/lib/math.tcl#L206:217 +####### +# proc rotate {r angle} { +# set theta [angle $r] +# set c [centroid $r] +# set r' [mapVertices v $r { +# set v [vec2 sub $v $c] +# set v [vec2 rotate $v $angle] +# set v [vec2 add $v $c] +# set v +# }] +# lset r' 2 [+ $theta $angle] +# set r' +# } +####################################### +# +# In order to construct a cursor that's relative to the virtual keyboard you must +# know three things: +# 1. The center of the virtual region +# 2. The angle of the virtual region +# 3. The position of the cursor relative to the virtual region (this is rather involved because it requires calcualting from the center and then taking cursor units from that point) +# +####################################### + +proc rotateStroke {points center angle} { + lmap v $points { + set v [vec2 sub $v $center] + set v [vec2 rotate $v $angle] + set v [vec2 add $v $center] + set v + } +} + +proc getLineShift {lines} { + set longestLineLength 0 + foreach line [split $lines "\n"] { + set lineLength [string length $line] + if {$lineLength > $longestLineLength} { + set longestLineLength $lineLength + } + } + return [list [expr {$longestLineLength / 2}] [expr {[llength $lines] / 2}]] +} + +proc drawCursor {cursorPosition lineShift region} { + set angle [region angle $region] + set anchor [region centroid $region] + # cursor position is relative to the center of the region: + # x: shift it from center by half width of the longest line of text in the editor * the character width + # y: shift it from center by half the number of lines in the editor * the character height + + + set cursorMultiplierX -20 + set cursorMultiplierY -30 + + set multipliedLineShift [list [* $cursorMultiplierX [lindex $lineShift 0]] [* $cursorMultiplierY [lindex $lineShift 1]]] + set anchor [sub $anchor $multipliedLineShift] + lassign $cursorPosition cursorX cursorY - set cursorX [int [- $cornerX [* $cursorX 20]]] - set cursorY [int [- [- $cornerY [* $cursorY 65]] 20]] - set cursorX2 $cursorX - set cursorY2 [- $cursorY 50] - puts " $cursorPosition => $cursorX $cursorY" - Display::stroke [list [list $cursorX $cursorY] [list $cursorX2 $cursorY2]] 3 white + + set cursorMultiplied [list [* $cursorMultiplierX $cursorX] [* $cursorMultiplierY $cursorY]] + set anchor [add $anchor $cursorMultiplied] + + set anchor_up [add $anchor [list 0 $cursorMultiplierY]] + set anchor_right [sub $anchor [list $cursorMultiplierX 0]] + set anchor_right_up [add $anchor_right [list 0 $cursorMultiplierY]] + + set cursorPoints [list $anchor_up $anchor $anchor_right $anchor_right_up $anchor_up] + Display::stroke [rotateStroke $cursorPoints $anchor $angle] 1 green } When $id has program code /code/ & the clock time is /t/ & the cursor is /cursor/ & $id has region /r/ { @@ -140,8 +198,8 @@ When $id has program code /code/ & the clock time is /t/ & the cursor is /cursor # position. The current virtual cursor () set intTime [expr {int($t * 10)}] drawText [insertCursor $code $cursor $intTime] $cursor $intTime $r - # Hide cursor, TODO: fix to calculate accurate position based on cursor position - drawCursor $cursor $r + set lineShift [getLineShift $code] + drawCursor $cursor $lineShift $r # DEBUG INFO When the cursor is /cursor/ { -- cgit v1.2.3 From 044785e316d06e432b90d67c74e810621f207fcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Fri, 22 Sep 2023 17:57:36 -0400 Subject: Add vec2 mult/div, nearly-there cursorbox --- virtual-programs/editor.folk | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 5343abb8..7f96292c 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -175,13 +175,17 @@ proc drawCursor {cursorPosition lineShift region} { set cursorMultiplierX -20 set cursorMultiplierY -30 + set cursorMultiplier [list $cursorMultiplierX $cursorMultiplierY] set multipliedLineShift [list [* $cursorMultiplierX [lindex $lineShift 0]] [* $cursorMultiplierY [lindex $lineShift 1]]] set anchor [sub $anchor $multipliedLineShift] lassign $cursorPosition cursorX cursorY + # set cursorMultiplied [list [* $cursorMultiplierX $cursorX] [* $cursorMultiplierY $cursorY]] + set cursorMultiplied [vec2 mult $cursorMultiplier $cursorPosition] + # Offset by one? + set cursorMultiplied [add $cursorMultiplied $cursorMultiplier] - set cursorMultiplied [list [* $cursorMultiplierX $cursorX] [* $cursorMultiplierY $cursorY]] set anchor [add $anchor $cursorMultiplied] set anchor_up [add $anchor [list 0 $cursorMultiplierY]] -- cgit v1.2.3 From 387b785a40a6dff13eb1aa1b045925da02c30f5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Mon, 25 Sep 2023 12:32:16 -0400 Subject: Better visual debugging for cursor positioning --- virtual-programs/editor.folk | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 7f96292c..2355901c 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -4,26 +4,11 @@ # thread ID? set id "editor-1" -##### -# To match to this I have another program with: -# Claim $this is editor editor-1 -##### When /page/ is editor /n/ & /page/ has region /r/ { Wish $page is outlined gray Claim $id has region [region move $r up 450px] } -proc checkForModifier {key} { - set r "" - switch $key { - LEFTSHIFT { set r "shift" } - RIGHTSHIFT { set r "shift" } - LEFTCTRL { set r "control" } - RIGHTCTRL { set r "control" } - } - return $r -} - proc updateCursor {oldCursor updates} { set newCursor $oldCursor if {[dict exists $updates x]} { @@ -70,6 +55,9 @@ proc deleteCharacter {code cursor} { set thisLine [lindex $lines $y] set mergedLine [string cat $previousLine $thisLine] lset lines [expr {$y - 1}] $mergedLine + # TODO: + # - delete thisLine from the lines array + # - move cursor to end of previous line lreplace lines $y $y } else { set line [lindex $lines $y] @@ -92,7 +80,7 @@ proc insertNewline {code cursor} { proc insertCursor {code cursor count} { if {$count % 2 == 0} { - return [swapCharacter $code " " $cursor] + return [swapCharacter $code "_" $cursor] } else { return $code } @@ -110,14 +98,15 @@ proc newRegion {program x y w h} { # NOTE: This is convenient for me debugging (gets triggered with Ctrl + R) # but should probably be "Claim this has halo message editor" or something # not as intrusive as outlining to start? Just a thought. -# set baseCode "Wish \$this is outlined green\nClaim first name is Andres" +# set baseCode "Wish \$this is outlined gold" set baseCode "Claim\nWish \$this is outlined gold\n";#Claim this is a very looooooong line\n";## Comment\nset lorem 0\nset ipsum 0" Commit code { Claim $id has program code $baseCode } proc drawText {text cursor count region} { set lineNumber 1 set showCursor [expr {$count % 2 == 0}] - Display::text {*}[region centroid $region] 2 $text [region angle $region] + # TODO: Make the font size change relative to `Wish $this is small|medium|large editor 1` (by default should use small, e.g. fontSize 1) + Display::text {*}[region centroid $region] 1 $text [region angle $region] } ######## @@ -168,10 +157,18 @@ proc getLineShift {lines} { proc drawCursor {cursorPosition lineShift region} { set angle [region angle $region] set anchor [region centroid $region] - # cursor position is relative to the center of the region: - # x: shift it from center by half width of the longest line of text in the editor * the character width - # y: shift it from center by half the number of lines in the editor * the character height - + # Need to figure out the formula for getting from (0, 0) [centeroid of the region] to the cursor position + # - first translate the cursor position from the centroid to the top left of the text region + # - then rotate the stroke to match the angle of its parent region + set multipliedShift [vec2::mult [list 10 10] $lineShift] + set shiftedAnchor [add $anchor $multipliedShift] + set scaledCursor [vec2::mult [list -20 -10] $cursorPosition] + + set finalCursor [add $shiftedAnchor $scaledCursor] + + Display::circle {*}$shiftedAnchor 5 5 red + Display::circle {*}$finalCursor 5 5 green + puts "shiftedAnchor: $shiftedAnchor" set cursorMultiplierX -20 set cursorMultiplierY -30 @@ -193,6 +190,7 @@ proc drawCursor {cursorPosition lineShift region} { set anchor_right_up [add $anchor_right [list 0 $cursorMultiplierY]] set cursorPoints [list $anchor_up $anchor $anchor_right $anchor_right_up $anchor_up] + Display::stroke [rotateStroke $cursorPoints $anchor $angle] 1 green } -- cgit v1.2.3 From 1744f21eafeb120c6a265d6470559022fb805ece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Mon, 25 Sep 2023 16:34:38 -0400 Subject: Add green cursor, make backspace at x=0 work --- virtual-programs/editor.folk | 115 ++++++++++++++----------------------------- 1 file changed, 38 insertions(+), 77 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 2355901c..9595c93e 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -55,8 +55,8 @@ proc deleteCharacter {code cursor} { set thisLine [lindex $lines $y] set mergedLine [string cat $previousLine $thisLine] lset lines [expr {$y - 1}] $mergedLine + lset lines $y "" # TODO: - # - delete thisLine from the lines array # - move cursor to end of previous line lreplace lines $y $y } else { @@ -95,11 +95,7 @@ proc newRegion {program x y w h} { Claim $program has region [region create $vertices $edges] } -# NOTE: This is convenient for me debugging (gets triggered with Ctrl + R) -# but should probably be "Claim this has halo message editor" or something -# not as intrusive as outlining to start? Just a thought. -# set baseCode "Wish \$this is outlined gold" -set baseCode "Claim\nWish \$this is outlined gold\n";#Claim this is a very looooooong line\n";## Comment\nset lorem 0\nset ipsum 0" +set baseCode "Claim\nWish \$this is outlined green" Commit code { Claim $id has program code $baseCode } proc drawText {text cursor count region} { @@ -109,31 +105,13 @@ proc drawText {text cursor count region} { Display::text {*}[region centroid $region] 1 $text [region angle $region] } -######## -# For reference, from folk/lib/math.tcl#L206:217 -####### -# proc rotate {r angle} { -# set theta [angle $r] -# set c [centroid $r] -# set r' [mapVertices v $r { -# set v [vec2 sub $v $c] -# set v [vec2 rotate $v $angle] -# set v [vec2 add $v $c] -# set v -# }] -# lset r' 2 [+ $theta $angle] -# set r' -# } ####################################### -# # In order to construct a cursor that's relative to the virtual keyboard you must # know three things: # 1. The center of the virtual region # 2. The angle of the virtual region # 3. The position of the cursor relative to the virtual region (this is rather involved because it requires calcualting from the center and then taking cursor units from that point) -# ####################################### - proc rotateStroke {points center angle} { lmap v $points { set v [vec2 sub $v $center] @@ -154,59 +132,46 @@ proc getLineShift {lines} { return [list [expr {$longestLineLength / 2}] [expr {[llength $lines] / 2}]] } +proc debug {position color} { + Display::circle {*}$position 5 2 $color +} + proc drawCursor {cursorPosition lineShift region} { - set angle [region angle $region] + set cursorHeight 25 set anchor [region centroid $region] - # Need to figure out the formula for getting from (0, 0) [centeroid of the region] to the cursor position - # - first translate the cursor position from the centroid to the top left of the text region - # - then rotate the stroke to match the angle of its parent region - set multipliedShift [vec2::mult [list 10 10] $lineShift] - set shiftedAnchor [add $anchor $multipliedShift] - set scaledCursor [vec2::mult [list -20 -10] $cursorPosition] - - set finalCursor [add $shiftedAnchor $scaledCursor] - - Display::circle {*}$shiftedAnchor 5 5 red - Display::circle {*}$finalCursor 5 5 green - puts "shiftedAnchor: $shiftedAnchor" - - set cursorMultiplierX -20 - set cursorMultiplierY -30 - set cursorMultiplier [list $cursorMultiplierX $cursorMultiplierY] - - set multipliedLineShift [list [* $cursorMultiplierX [lindex $lineShift 0]] [* $cursorMultiplierY [lindex $lineShift 1]]] - set anchor [sub $anchor $multipliedLineShift] - - lassign $cursorPosition cursorX cursorY - # set cursorMultiplied [list [* $cursorMultiplierX $cursorX] [* $cursorMultiplierY $cursorY]] - set cursorMultiplied [vec2 mult $cursorMultiplier $cursorPosition] - # Offset by one? - set cursorMultiplied [add $cursorMultiplied $cursorMultiplier] + set angle [region angle $region] - set anchor [add $anchor $cursorMultiplied] + # Define constants + set shiftMultiplier [list 10 10] + # 9 is the width of a character, 20 is the height of a character + # These values could theortically be set from drawText in Display.tcl + set cursorScale [list 9 20] - set anchor_up [add $anchor [list 0 $cursorMultiplierY]] - set anchor_right [sub $anchor [list $cursorMultiplierX 0]] - set anchor_right_up [add $anchor_right [list 0 $cursorMultiplierY]] + # Calculate the shift for the anchor point + set lineShiftHalf [list [lindex $lineShift 0] 0] + set multipliedShift [vec2::mult $shiftMultiplier $lineShift] + set adjustedShift [sub $multipliedShift $lineShiftHalf] + set shiftedAnchor [sub $anchor $adjustedShift] - set cursorPoints [list $anchor_up $anchor $anchor_right $anchor_right_up $anchor_up] + # Calculate the final cursor position + set scaledCursor [vec2::mult $cursorScale $cursorPosition] + set finalCursor [add $shiftedAnchor $scaledCursor] - Display::stroke [rotateStroke $cursorPoints $anchor $angle] 1 green + # Rotate and display the cursor + set rotatedFinalCursor [lindex [rotateStroke [list $finalCursor] $anchor $angle] 0] + # debug $rotatedFinalCursor white + Display::stroke [rotateStroke \ + [list $finalCursor \ + [add $finalCursor [list 0 $cursorHeight]] \ + ] \ + $anchor $angle] 2 green } When $id has program code /code/ & the clock time is /t/ & the cursor is /cursor/ & $id has region /r/ { - # NOTE: This is a useful debugging line as it flashes - # the actual letter corresponding to the cursor - # position. The current virtual cursor () set intTime [expr {int($t * 10)}] drawText [insertCursor $code $cursor $intTime] $cursor $intTime $r set lineShift [getLineShift $code] drawCursor $cursor $lineShift $r - - # DEBUG INFO - When the cursor is /cursor/ { - Wish $id has halo message "cursor: $cursor" - } } proc getCurrentLineLength {lines cursor} { @@ -215,7 +180,6 @@ proc getCurrentLineLength {lines cursor} { string length $currentLine } -# Must use `Every time` but it's incompatible with the keyboard claim or something, as written? Confer with Omar ... Every time keyboard claims key /currentCharacter/ is down with modifiers /modifier/ & the cursor is /cursor/ & $id has program code /code/ { Commit cursor { switch $currentCharacter { @@ -244,7 +208,15 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi } LEFT { Claim the cursor is [updateCursor $cursor {x -1}] } BACKSPACE { - Claim the cursor is [updateCursor $cursor {x -1}] + # if cursor is at the beginning of the line, delete the newline + if {[x $cursor] == 0} { + set newCursor [updateCursor $cursor {y -1}] + set previousLineLength [getCurrentLineLength $code $newCursor] + set newCursor [list [expr {$previousLineLength - 1}] [y $newCursor]] + Claim the cursor is $newCursor + } else { + Claim the cursor is [updateCursor $cursor {x -1}] + } Commit code { Claim $id has program code [deleteCharacter $code $cursor] } } SPACE { @@ -252,14 +224,7 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi Commit code { Claim $id has program code [insertCharacter $code " " $cursor] } } ENTER { - # Note: Still need to figure out the exact & text manipulation methods for all the - # different cases of enter. This is just a simple one for now. Claim the cursor is [updateCursor $cursor {x [expr {-1 * [x $cursor]}] y 1}] - # Claim the cursor is [updateCursor $cursor {x 0 y 1}] - # NOTE: Useful debugging print line, still figuring out the math. - # Unforuntately this doesn't have consistent cursor spacing as the - # X-coordinate increases. - # puts "calc: [expr {-1 * [x $cursor]}]" Commit code { Claim $id has program code [insertNewline $code $cursor] } } LEFTSHIFT - @@ -270,10 +235,6 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi } default { Claim the cursor is [updateCursor $cursor {x 1}] - # Unneccessary b/c of properly handled stuff?? - # if {$modifier == "shift"} { - # set currentCharacter [string toupper $currentCharacter] - # } if {$modifier == "ctrl" & $currentCharacter == "p"} { Wish to print $code with job id [expr {rand()}] return -- cgit v1.2.3 From 6749d963d5a72443a112afc553b8fa8d0480c353 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Tue, 10 Oct 2023 16:48:49 -0400 Subject: Add monospace font --- virtual-programs/editor.folk | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 9595c93e..8674fce5 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -80,7 +80,7 @@ proc insertNewline {code cursor} { proc insertCursor {code cursor count} { if {$count % 2 == 0} { - return [swapCharacter $code "_" $cursor] + return [swapCharacter $code "X" $cursor] } else { return $code } @@ -102,7 +102,7 @@ proc drawText {text cursor count region} { set lineNumber 1 set showCursor [expr {$count % 2 == 0}] # TODO: Make the font size change relative to `Wish $this is small|medium|large editor 1` (by default should use small, e.g. fontSize 1) - Display::text {*}[region centroid $region] 1 $text [region angle $region] + Display::text {*}[region centroid $region] 1 $text [region angle $region] CourierPrimeCode } ####################################### -- cgit v1.2.3 From 754e17f102cd04356229d17732ec10caa725aba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Mon, 16 Oct 2023 10:45:39 -0400 Subject: local NYU + font NeomatrixCode --- virtual-programs/editor.folk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 8674fce5..52261f11 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -102,7 +102,7 @@ proc drawText {text cursor count region} { set lineNumber 1 set showCursor [expr {$count % 2 == 0}] # TODO: Make the font size change relative to `Wish $this is small|medium|large editor 1` (by default should use small, e.g. fontSize 1) - Display::text {*}[region centroid $region] 1 $text [region angle $region] CourierPrimeCode + Display::text {*}[region centroid $region] 1 $text [region angle $region] NeomatrixCode } ####################################### -- cgit v1.2.3 From ff5492089a0e4299123c63ed814afb0fcf7e26cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Wed, 18 Oct 2023 16:58:39 -0400 Subject: Fix cursor movement on newline & backspace at x=0 --- virtual-programs/editor.folk | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 52261f11..cf7cec30 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -80,7 +80,7 @@ proc insertNewline {code cursor} { proc insertCursor {code cursor count} { if {$count % 2 == 0} { - return [swapCharacter $code "X" $cursor] + return [swapCharacter $code "_" $cursor] } else { return $code } @@ -212,8 +212,9 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi if {[x $cursor] == 0} { set newCursor [updateCursor $cursor {y -1}] set previousLineLength [getCurrentLineLength $code $newCursor] - set newCursor [list [expr {$previousLineLength - 1}] [y $newCursor]] + set newCursor [list $previousLineLength [y $newCursor]] Claim the cursor is $newCursor + # Delete the old line??? } else { Claim the cursor is [updateCursor $cursor {x -1}] } @@ -224,7 +225,9 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi Commit code { Claim $id has program code [insertCharacter $code " " $cursor] } } ENTER { - Claim the cursor is [updateCursor $cursor {x [expr {-1 * [x $cursor]}] y 1}] + # GOAL: Move cursor to beginning of next line + set updatedCursor [updateCursor $cursor {y 1}] + Claim the cursor is [list 0 [lindex $updatedCursor 1]] Commit code { Claim $id has program code [insertNewline $code $cursor] } } LEFTSHIFT - -- cgit v1.2.3 From 0e437fc3591c9481037f2f84d1a99e8550b0ebc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Fri, 20 Oct 2023 16:15:59 -0400 Subject: Add debugging --- virtual-programs/editor.folk | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index cf7cec30..8461f8f3 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -133,35 +133,56 @@ proc getLineShift {lines} { } proc debug {position color} { - Display::circle {*}$position 5 2 $color + # Display::circle {*}$position 3 2 $color false + Display::circle {*}$position 8 2 $color false } proc drawCursor {cursorPosition lineShift region} { - set cursorHeight 25 + puts "$lineShift | $cursorPosition" set anchor [region centroid $region] set angle [region angle $region] - # Define constants - set shiftMultiplier [list 10 10] # 9 is the width of a character, 20 is the height of a character # These values could theortically be set from drawText in Display.tcl - set cursorScale [list 9 20] + set cursorHeight 20 + set cursorWidth 9 + set shiftMultiplier [list 10 10] + + set cursorScale [list 20 20] ;# OLD + # set cursorScale [list $cursorHeight $cursorWidth] # Calculate the shift for the anchor point set lineShiftHalf [list [lindex $lineShift 0] 0] set multipliedShift [vec2::mult $shiftMultiplier $lineShift] + # This adjusted shift is seemingly wrong .... set adjustedShift [sub $multipliedShift $lineShiftHalf] + # set adjustedShift $multipliedShift set shiftedAnchor [sub $anchor $adjustedShift] - + # add (x * width) to the shiftedAnchor position from the beginning of the line ... # Calculate the final cursor position set scaledCursor [vec2::mult $cursorScale $cursorPosition] + puts "lineShiftHalf | multipliedShift | adjustedShift | shiftedAnchor | scaledCursor" + puts "$lineShiftHalf | $multipliedShift | $adjustedShift | $shiftedAnchor | $scaledCursor" + debug $scaledCursor blue + # Something is going wrong here, I think set finalCursor [add $shiftedAnchor $scaledCursor] - + debug $finalCursor gold # Rotate and display the cursor set rotatedFinalCursor [lindex [rotateStroke [list $finalCursor] $anchor $angle] 0] - # debug $rotatedFinalCursor white + # This is where the cursor point is relative to. + debug $rotatedFinalCursor white + # cursor, green + Display::stroke [rotateStroke \ + [list $finalCursor \ + [add $finalCursor [list 0 $cursorHeight]] \ + ] \ + $anchor $angle] 2 green + + # cursor box Display::stroke [rotateStroke \ [list $finalCursor \ + [add $finalCursor [list $cursorWidth 0]] \ + [add $finalCursor [list [* -3 $cursorWidth] $cursorHeight]] \ [add $finalCursor [list 0 $cursorHeight]] \ ] \ $anchor $angle] 2 green -- cgit v1.2.3 From a0eaf8cffc77ffb98f35403a306bf5d5b718e8ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Fri, 20 Oct 2023 17:45:23 -0400 Subject: Test long default code --- virtual-programs/editor.folk | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 8461f8f3..b7f150e1 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -95,7 +95,9 @@ proc newRegion {program x y w h} { Claim $program has region [region create $vertices $edges] } -set baseCode "Claim\nWish \$this is outlined green" +# set baseCode "Claim\nWish \$this is outlined green" +# long base code +set baseCode "Claim\nWish \$this is outlined green\nClaim this is a super long linelooooooooong line\nClaim the future is good\nClaim lorem ipsum\nClaim bloooooop\nClaim hello" Commit code { Claim $id has program code $baseCode } proc drawText {text cursor count region} { @@ -138,16 +140,14 @@ proc debug {position color} { } proc drawCursor {cursorPosition lineShift region} { - puts "$lineShift | $cursorPosition" set anchor [region centroid $region] set angle [region angle $region] - # 9 is the width of a character, 20 is the height of a character - # These values could theortically be set from drawText in Display.tcl set cursorHeight 20 set cursorWidth 9 set shiftMultiplier [list 10 10] + # 20 20 scale is arbitrary, computing it off of Display or something ..... set cursorScale [list 20 20] ;# OLD # set cursorScale [list $cursorHeight $cursorWidth] @@ -161,12 +161,13 @@ proc drawCursor {cursorPosition lineShift region} { # add (x * width) to the shiftedAnchor position from the beginning of the line ... # Calculate the final cursor position set scaledCursor [vec2::mult $cursorScale $cursorPosition] + puts "lineShiftHalf | multipliedShift | adjustedShift | shiftedAnchor | scaledCursor" puts "$lineShiftHalf | $multipliedShift | $adjustedShift | $shiftedAnchor | $scaledCursor" - debug $scaledCursor blue + # Something is going wrong here, I think set finalCursor [add $shiftedAnchor $scaledCursor] - debug $finalCursor gold + # debug $finalCursor gold # Rotate and display the cursor set rotatedFinalCursor [lindex [rotateStroke [list $finalCursor] $anchor $angle] 0] # This is where the cursor point is relative to. @@ -182,8 +183,6 @@ proc drawCursor {cursorPosition lineShift region} { Display::stroke [rotateStroke \ [list $finalCursor \ [add $finalCursor [list $cursorWidth 0]] \ - [add $finalCursor [list [* -3 $cursorWidth] $cursorHeight]] \ - [add $finalCursor [list 0 $cursorHeight]] \ ] \ $anchor $angle] 2 green } @@ -191,7 +190,9 @@ proc drawCursor {cursorPosition lineShift region} { When $id has program code /code/ & the clock time is /t/ & the cursor is /cursor/ & $id has region /r/ { set intTime [expr {int($t * 10)}] drawText [insertCursor $code $cursor $intTime] $cursor $intTime $r + set lineShift [getLineShift $code] + # Hmmmm, need a fn to go from cursor to pixel positions drawCursor $cursor $lineShift $r } -- cgit v1.2.3 From 8be21e8ed1cc43f92b838c1f1a98ad871f50945c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Sun, 22 Oct 2023 11:47:26 -0400 Subject: Implement ctrl+a and ctrl+e --- virtual-programs/editor.folk | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index b7f150e1..0c62f018 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -123,6 +123,14 @@ proc rotateStroke {points center angle} { } } +proc getLineLength {code cursor} { + set lines [split $code "\n"] + set line [lindex $lines [lindex $cursor 1]] + set ll [string length $line] + puts "linelength: $ll" + return $ll +} + proc getLineShift {lines} { set longestLineLength 0 foreach line [split $lines "\n"] { @@ -148,6 +156,7 @@ proc drawCursor {cursorPosition lineShift region} { set shiftMultiplier [list 10 10] # 20 20 scale is arbitrary, computing it off of Display or something ..... + # TODO: Pull these variables from the Display file set cursorScale [list 20 20] ;# OLD # set cursorScale [list $cursorHeight $cursorWidth] @@ -162,9 +171,6 @@ proc drawCursor {cursorPosition lineShift region} { # Calculate the final cursor position set scaledCursor [vec2::mult $cursorScale $cursorPosition] - puts "lineShiftHalf | multipliedShift | adjustedShift | shiftedAnchor | scaledCursor" - puts "$lineShiftHalf | $multipliedShift | $adjustedShift | $shiftedAnchor | $scaledCursor" - # Something is going wrong here, I think set finalCursor [add $shiftedAnchor $scaledCursor] # debug $finalCursor gold @@ -259,7 +265,6 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi Claim the cursor is $cursor } default { - Claim the cursor is [updateCursor $cursor {x 1}] if {$modifier == "ctrl" & $currentCharacter == "p"} { Wish to print $code with job id [expr {rand()}] return @@ -269,6 +274,19 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi Claim the cursor is [list 0 0] return } + if {$modifier == "ctrl" & $currentCharacter == "a"} { + Commit code { Claim $id has program code $code} + lassign $cursor x y + Claim the cursor is [list 0 $y] + return + } + if {$modifier == "ctrl" & $currentCharacter == "e"} { + Commit code { Claim $id has program code $code} + lassign $cursor x y + Claim the cursor is [list [getLineLength $code $cursor] $y] + return + } + Claim the cursor is [updateCursor $cursor {x 1}] Commit code { Claim $id has program code [insertCharacter $code $currentCharacter $cursor] } } } -- cgit v1.2.3 From d18e890179cd84194c41d9f613a89f1ceba7a2e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Mon, 23 Oct 2023 18:27:20 -0400 Subject: Add debugging & multiple shift counts --- virtual-programs/editor.folk | 43 +++++++++---------------------------------- 1 file changed, 9 insertions(+), 34 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 0c62f018..f8ead355 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -1,7 +1,3 @@ -# TODO: Make this not-hard-coded -# In fact, this shouldn't be "editor-1" at all but -# probably should be the MAC address or some Bluetooth -# thread ID? set id "editor-1" When /page/ is editor /n/ & /page/ has region /r/ { @@ -107,13 +103,6 @@ proc drawText {text cursor count region} { Display::text {*}[region centroid $region] 1 $text [region angle $region] NeomatrixCode } -####################################### -# In order to construct a cursor that's relative to the virtual keyboard you must -# know three things: -# 1. The center of the virtual region -# 2. The angle of the virtual region -# 3. The position of the cursor relative to the virtual region (this is rather involved because it requires calcualting from the center and then taking cursor units from that point) -####################################### proc rotateStroke {points center angle} { lmap v $points { set v [vec2 sub $v $center] @@ -153,44 +142,30 @@ proc drawCursor {cursorPosition lineShift region} { set cursorHeight 20 set cursorWidth 9 - set shiftMultiplier [list 10 10] + set shiftMultiplier [list 12 20] + # set shiftMultiplier [list 14 25] + # set shiftMultiplier [list 14 20] + set cursorScale [list 14 25] ;# OLD - # 20 20 scale is arbitrary, computing it off of Display or something ..... - # TODO: Pull these variables from the Display file - set cursorScale [list 20 20] ;# OLD - # set cursorScale [list $cursorHeight $cursorWidth] - - # Calculate the shift for the anchor point set lineShiftHalf [list [lindex $lineShift 0] 0] set multipliedShift [vec2::mult $shiftMultiplier $lineShift] - # This adjusted shift is seemingly wrong .... - set adjustedShift [sub $multipliedShift $lineShiftHalf] - # set adjustedShift $multipliedShift + # set adjustedShift [sub $multipliedShift $lineShiftHalf] + set adjustedShift $multipliedShift set shiftedAnchor [sub $anchor $adjustedShift] + debug [lindex [rotateStroke [list $shiftedAnchor] $anchor $angle] 0] white # add (x * width) to the shiftedAnchor position from the beginning of the line ... - # Calculate the final cursor position set scaledCursor [vec2::mult $cursorScale $cursorPosition] - # Something is going wrong here, I think set finalCursor [add $shiftedAnchor $scaledCursor] - # debug $finalCursor gold - # Rotate and display the cursor - set rotatedFinalCursor [lindex [rotateStroke [list $finalCursor] $anchor $angle] 0] + set rotatedFenalCursor [lindex [rotateStroke [list $finalCursor] $anchor $angle] 0] + # This is where the cursor point is relative to. - debug $rotatedFinalCursor white # cursor, green Display::stroke [rotateStroke \ [list $finalCursor \ [add $finalCursor [list 0 $cursorHeight]] \ ] \ $anchor $angle] 2 green - - # cursor box - Display::stroke [rotateStroke \ - [list $finalCursor \ - [add $finalCursor [list $cursorWidth 0]] \ - ] \ - $anchor $angle] 2 green } When $id has program code /code/ & the clock time is /t/ & the cursor is /cursor/ & $id has region /r/ { -- cgit v1.2.3 From b89207a03ac9d328fb0f75766d6c41675dea11f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Tue, 24 Oct 2023 17:24:43 -0400 Subject: hide cursor debug, fix deleting at x=0 --- virtual-programs/editor.folk | 84 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 69 insertions(+), 15 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index f8ead355..a912ce20 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -1,4 +1,5 @@ set id "editor-1" +set debug_cursor false When /page/ is editor /n/ & /page/ has region /r/ { Wish $page is outlined gray @@ -26,7 +27,10 @@ Commit cursor { Claim the cursor is [list 0 0]} proc insertCharacter {code newCharacter cursor} { set lines [split $code "\n"] lassign $cursor x y - set x [expr {$x - 1}] + set x [- $x 1] + if {$x < 0} { + set x 0 + } set line [lindex $lines $y] set character [string cat [string index $line $x] $newCharacter] set line [string replace $line $x $x $character] @@ -51,10 +55,17 @@ proc deleteCharacter {code cursor} { set thisLine [lindex $lines $y] set mergedLine [string cat $previousLine $thisLine] lset lines [expr {$y - 1}] $mergedLine - lset lines $y "" + lset lines $y "" ;# this sucks, instead delete the line # TODO: # - move cursor to end of previous line - lreplace lines $y $y + # lreplace lines $y $y + set newLines {} + for {set i 0} {$i < [llength $lines]} {incr i} { + if {$i != $y} { + lappend newLines [lindex $lines $i] + } + } + set lines $newLines } else { set line [lindex $lines $y] set line [string replace $line [expr {$x - 1}] [expr {$x - 1}] ""] @@ -93,7 +104,7 @@ proc newRegion {program x y w h} { # set baseCode "Claim\nWish \$this is outlined green" # long base code -set baseCode "Claim\nWish \$this is outlined green\nClaim this is a super long linelooooooooong line\nClaim the future is good\nClaim lorem ipsum\nClaim bloooooop\nClaim hello" +set baseCode "Claim\nWish \$this is outlined green\nClaim this is a super long linelooooooooong line\nClaim the future is good\nClaim lorem ipsum long long long long long long long long long long long\nClaim bl000000000000000000000000000000_______oooooop\nClaim hello hello hello hello hello hello hello" Commit code { Claim $id has program code $baseCode } proc drawText {text cursor count region} { @@ -116,7 +127,6 @@ proc getLineLength {code cursor} { set lines [split $code "\n"] set line [lindex $lines [lindex $cursor 1]] set ll [string length $line] - puts "linelength: $ll" return $ll } @@ -133,13 +143,17 @@ proc getLineShift {lines} { proc debug {position color} { # Display::circle {*}$position 3 2 $color false - Display::circle {*}$position 8 2 $color false + Display::circle {*}$position 5 2 $color true } proc drawCursor {cursorPosition lineShift region} { set anchor [region centroid $region] set angle [region angle $region] + if {$debug_cursor} { + debug $anchor green + } + set cursorHeight 20 set cursorWidth 9 set shiftMultiplier [list 12 20] @@ -159,13 +173,30 @@ proc drawCursor {cursorPosition lineShift region} { set finalCursor [add $shiftedAnchor $scaledCursor] set rotatedFenalCursor [lindex [rotateStroke [list $finalCursor] $anchor $angle] 0] - # This is where the cursor point is relative to. - # cursor, green - Display::stroke [rotateStroke \ - [list $finalCursor \ - [add $finalCursor [list 0 $cursorHeight]] \ - ] \ - $anchor $angle] 2 green + if {$debug_cursor} { + # This is where the cursor point is relative to. + # cursor, green + Display::stroke [rotateStroke \ + [list $finalCursor \ + [add $finalCursor [list 0 $cursorHeight]] \ + ] \ + $anchor $angle] 2 green + # vertical reticle line + Display::stroke [rotateStroke \ + [list $finalCursor \ + [add $finalCursor [list 0 25]] \ + [add $finalCursor [list 0 -25]] \ + ] \ + $anchor $angle] 2 green + + # horizontal reticle line + Display::stroke [rotateStroke \ + [list $finalCursor \ + [add $finalCursor [list 25 0]] \ + [add $finalCursor [list -25 0]] \ + ] \ + $anchor $angle] 2 green + } } When $id has program code /code/ & the clock time is /t/ & the cursor is /cursor/ & $id has region /r/ { @@ -173,8 +204,31 @@ When $id has program code /code/ & the clock time is /t/ & the cursor is /cursor drawText [insertCursor $code $cursor $intTime] $cursor $intTime $r set lineShift [getLineShift $code] - # Hmmmm, need a fn to go from cursor to pixel positions drawCursor $cursor $lineShift $r + + if {$debug_cursor} { + # draw lines for every line of text + set lineCount [llength [split $code "\n"]] + set horizontalLines [list] + set anchor [region centroid $r] + set angle [region angle $r] + + set vertical_spacer 20 + for {set i -10} { $i < 10 } {incr i} { + lappend verticalLines [add $anchor [list 0 [expr {$i * $vertical_spacer}]]] + lappend verticalLines [add $anchor [list 100 [expr {$i * $vertical_spacer}]]] + lappend verticalLines [add $anchor [list $vertical_spacer [expr {$i * $vertical_spacer}]]] + } + + set horizontal_spacer 14 + for {set i -10} { $i < 10 } {incr i} { + lappend horizontalLines [add $anchor [list [expr {$i * $horizontal_spacer}] 0]] + lappend horizontalLines [add $anchor [list [expr {$i * $horizontal_spacer}] 100 ]] + lappend horizontalLines [add $anchor [list [expr {$i * $horizontal_spacer}] $horizontal_spacer]] + } + Display::stroke [rotateStroke $verticalLines $anchor $angle] 2 green + Display::stroke [rotateStroke $horizontalLines $anchor $angle] 2 white + } } proc getCurrentLineLength {lines cursor} { @@ -190,7 +244,7 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi set updatedCursor [updateCursor $cursor {y -1}] set currentLineLength [getCurrentLineLength $code $updatedCursor] if {[lindex $updatedCursor 0] > $currentLineLength} { - Claim the cursor is [list $currentLineLength [lindex $updatedCursor 1]] + Claim the cursor is [list [- $currentLineLength 1] [lindex $updatedCursor 1]] } else { Claim the cursor is $updatedCursor } -- cgit v1.2.3 From 2a400b190be5cf59b6e0354dd72493407da034ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Tue, 24 Oct 2023 17:48:29 -0400 Subject: Correctly insert chars at x=0 --- virtual-programs/editor.folk | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index a912ce20..5f2028d2 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -1,5 +1,5 @@ set id "editor-1" -set debug_cursor false +set ::debug_cursor false When /page/ is editor /n/ & /page/ has region /r/ { Wish $page is outlined gray @@ -28,14 +28,17 @@ proc insertCharacter {code newCharacter cursor} { set lines [split $code "\n"] lassign $cursor x y set x [- $x 1] + set line [lindex $lines $y] + if {$x < 0} { - set x 0 + lset lines $y [string cat $newCharacter $line] + return [join $lines "\n"] + } else { + set character [string cat [string index $line $x] $newCharacter] + set line [string replace $line $x $x $character] + lset lines $y $line + return [join $lines "\n"] } - set line [lindex $lines $y] - set character [string cat [string index $line $x] $newCharacter] - set line [string replace $line $x $x $character] - lset lines $y $line - return [join $lines "\n"] } proc swapCharacter {code newCharacter cursor} { @@ -150,7 +153,7 @@ proc drawCursor {cursorPosition lineShift region} { set anchor [region centroid $region] set angle [region angle $region] - if {$debug_cursor} { + if {$::debug_cursor} { debug $anchor green } @@ -173,7 +176,7 @@ proc drawCursor {cursorPosition lineShift region} { set finalCursor [add $shiftedAnchor $scaledCursor] set rotatedFenalCursor [lindex [rotateStroke [list $finalCursor] $anchor $angle] 0] - if {$debug_cursor} { + if {$::debug_cursor} { # This is where the cursor point is relative to. # cursor, green Display::stroke [rotateStroke \ @@ -206,7 +209,7 @@ When $id has program code /code/ & the clock time is /t/ & the cursor is /cursor set lineShift [getLineShift $code] drawCursor $cursor $lineShift $r - if {$debug_cursor} { + if {$::debug_cursor} { # draw lines for every line of text set lineCount [llength [split $code "\n"]] set horizontalLines [list] @@ -271,7 +274,6 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi set previousLineLength [getCurrentLineLength $code $newCursor] set newCursor [list $previousLineLength [y $newCursor]] Claim the cursor is $newCursor - # Delete the old line??? } else { Claim the cursor is [updateCursor $cursor {x -1}] } @@ -315,6 +317,7 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi Claim the cursor is [list [getLineLength $code $cursor] $y] return } + puts "adding: $currentCharacter" Claim the cursor is [updateCursor $cursor {x 1}] Commit code { Claim $id has program code [insertCharacter $code $currentCharacter $cursor] } } -- cgit v1.2.3 From 11b459e01cbc690aca9019ef86912154a8d9ef20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Tue, 24 Oct 2023 17:50:34 -0400 Subject: Set short default code --- virtual-programs/editor.folk | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 5f2028d2..24389cf9 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -105,9 +105,9 @@ proc newRegion {program x y w h} { Claim $program has region [region create $vertices $edges] } -# set baseCode "Claim\nWish \$this is outlined green" +set baseCode "Wish \$this is outlined green" # long base code -set baseCode "Claim\nWish \$this is outlined green\nClaim this is a super long linelooooooooong line\nClaim the future is good\nClaim lorem ipsum long long long long long long long long long long long\nClaim bl000000000000000000000000000000_______oooooop\nClaim hello hello hello hello hello hello hello" +# set baseCode "Claim\nWish \$this is outlined green\nClaim this is a super long linelooooooooong line\nClaim the future is good\nClaim lorem ipsum long long long long long long long long long long long\nClaim bl000000000000000000000000000000_______oooooop\nClaim hello hello hello hello hello hello hello" Commit code { Claim $id has program code $baseCode } proc drawText {text cursor count region} { @@ -279,12 +279,14 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi } Commit code { Claim $id has program code [deleteCharacter $code $cursor] } } + DELETE { + # TODO: Implement DELETE, operates like BACKSPACE, but in the opposite direction + } SPACE { Claim the cursor is [updateCursor $cursor {x 1}] Commit code { Claim $id has program code [insertCharacter $code " " $cursor] } } ENTER { - # GOAL: Move cursor to beginning of next line set updatedCursor [updateCursor $cursor {y 1}] Claim the cursor is [list 0 [lindex $updatedCursor 1]] Commit code { Claim $id has program code [insertNewline $code $cursor] } -- cgit v1.2.3 From c71dc0f06075e83ab0cec9f9929baf917ed0ec8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Tue, 24 Oct 2023 17:53:20 -0400 Subject: Remove extraneous debug info --- virtual-programs/editor.folk | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 24389cf9..28648ecd 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -169,7 +169,9 @@ proc drawCursor {cursorPosition lineShift region} { # set adjustedShift [sub $multipliedShift $lineShiftHalf] set adjustedShift $multipliedShift set shiftedAnchor [sub $anchor $adjustedShift] - debug [lindex [rotateStroke [list $shiftedAnchor] $anchor $angle] 0] white + if {$::debug_cursor} { + debug [lindex [rotateStroke [list $shiftedAnchor] $anchor $angle] 0] white + } # add (x * width) to the shiftedAnchor position from the beginning of the line ... set scaledCursor [vec2::mult $cursorScale $cursorPosition] @@ -319,7 +321,6 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi Claim the cursor is [list [getLineLength $code $cursor] $y] return } - puts "adding: $currentCharacter" Claim the cursor is [updateCursor $cursor {x 1}] Commit code { Claim $id has program code [insertCharacter $code $currentCharacter $cursor] } } -- cgit v1.2.3 From d3f14f3e45d709c42b2fdb3711264734ce8d4cb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Tue, 31 Oct 2023 16:36:52 -0400 Subject: Black hole unsupported keys --- virtual-programs/editor.folk | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 28648ecd..c7fb553a 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -58,10 +58,7 @@ proc deleteCharacter {code cursor} { set thisLine [lindex $lines $y] set mergedLine [string cat $previousLine $thisLine] lset lines [expr {$y - 1}] $mergedLine - lset lines $y "" ;# this sucks, instead delete the line - # TODO: - # - move cursor to end of previous line - # lreplace lines $y $y + lset lines $y "" set newLines {} for {set i 0} {$i < [llength $lines]} {incr i} { if {$i != $y} { @@ -77,6 +74,16 @@ proc deleteCharacter {code cursor} { return [join $lines "\n"] } +proc deleteToBeginning {code cursor} { + set lines [split $code "\n"] + lassign $cursor x y + set line [lindex $lines $y] + set newLine [string range $line $x end] + lset lines $y $newLine + return [join $lines "\n"] +} + + proc insertNewline {code cursor} { set lines [split $code "\n"] lassign $cursor x y @@ -281,9 +288,6 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi } Commit code { Claim $id has program code [deleteCharacter $code $cursor] } } - DELETE { - # TODO: Implement DELETE, operates like BACKSPACE, but in the opposite direction - } SPACE { Claim the cursor is [updateCursor $cursor {x 1}] Commit code { Claim $id has program code [insertCharacter $code " " $cursor] } @@ -293,10 +297,21 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi Claim the cursor is [list 0 [lindex $updatedCursor 1]] Commit code { Claim $id has program code [insertNewline $code $cursor] } } + DELETE - + INSERT - + MUTE - + VOLUMEUP - + VOLUMEDOWN - LEFTSHIFT - RIGHTSHIFT - LEFTCTRL - RIGHTCTRL { + # TODO: Implement DELETE, operates like BACKSPACE, but in the opposite direction + # TODO: MUTE VOLUMEUP VOLUMEDOWN + # implement sound.folk that allows a system-wide + # volume setting to be adjusted. + # Perhaps `Wish $system volume is 0.5` or something + Claim the cursor is $cursor } default { @@ -321,6 +336,13 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi Claim the cursor is [list [getLineLength $code $cursor] $y] return } + if {$modifier == "ctrl" & $currentCharacter == "u"} { + # delete from cursor back to 0 and move cursor to 0 + Commit code { Claim $id has program code $code} + lassign $cursor x y + Claim the cursor is [list 0 $y] + return + } Claim the cursor is [updateCursor $cursor {x 1}] Commit code { Claim $id has program code [insertCharacter $code $currentCharacter $cursor] } } @@ -329,6 +351,8 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi } Claim $this has demo { + ## Okay, this needs to be: + # Claim $this is keyboard # Tape this program underneath the main keyboard of your system to try it out Claim $this is editor editor-1 } \ No newline at end of file -- cgit v1.2.3 From 5a82653586c65b25b3eaf74d1cbbee845402c58f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Sat, 4 Nov 2023 11:42:59 -0400 Subject: Fix cursor-out-of-bounds and add debugging --- virtual-programs/editor.folk | 48 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index c7fb553a..17d6f756 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -1,5 +1,6 @@ set id "editor-1" -set ::debug_cursor false +set ::debug_cursor true +set ::debug_print true When /page/ is editor /n/ & /page/ has region /r/ { Wish $page is outlined gray @@ -170,6 +171,7 @@ proc drawCursor {cursorPosition lineShift region} { # set shiftMultiplier [list 14 25] # set shiftMultiplier [list 14 20] set cursorScale [list 14 25] ;# OLD + # projectorToCamera???? set lineShiftHalf [list [lindex $lineShift 0] 0] set multipliedShift [vec2::mult $shiftMultiplier $lineShift] @@ -211,11 +213,23 @@ proc drawCursor {cursorPosition lineShift region} { } } +proc drawWontPrintTwice {id} { + When $id has printed /lastPrintedCode/ & $id has program code /code/ & $id has region /r/ { + if {$lastPrintedCode == $code} { + # TODO: Maybe offset to top right or something??? + Wish $id draws a circle with radius 3 color red filled true center [list 0 0] offset [list 100 100] + } else { + Wish $id draws a circle with radius 3 color color + } + } +} + When $id has program code /code/ & the clock time is /t/ & the cursor is /cursor/ & $id has region /r/ { set intTime [expr {int($t * 10)}] drawText [insertCursor $code $cursor $intTime] $cursor $intTime $r set lineShift [getLineShift $code] + drawWontPrintTwice $id drawCursor $cursor $lineShift $r if {$::debug_cursor} { @@ -262,9 +276,14 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi } } DOWN { + set linecount [llength [split $code "\n"]] set updatedCursor [updateCursor $cursor {y 1}] set currentLineLength [getCurrentLineLength $code $updatedCursor] - if {[lindex $updatedCursor 0] > $currentLineLength} { + + if {[lindex $updatedCursor 1] == $linecount} { + Claim the cursor is $cursor + return + } elseif {[lindex $updatedCursor 0] > $currentLineLength} { Claim the cursor is [list $currentLineLength [lindex $updatedCursor 1]] } else { Claim the cursor is $updatedCursor @@ -315,8 +334,28 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi Claim the cursor is $cursor } default { + # Make "PRINTED statement display as long as code will-not-print-twice is around!" + # VISUAL FEEDBACKKKKKKK if {$modifier == "ctrl" & $currentCharacter == "p"} { - Wish to print $code with job id [expr {rand()}] + When $id has printed /lastPrintedCode/ { + if {$lastPrintedCode == $code} { + Claim the cursor is $cursor + if {$::debug_print} { + puts "Not printing the same code twice" + } + return + } + } + When /nobody/ has printed /lastPrintedCode/ { + if {$::debug_print} { + puts "Printing $code" + } + Commit print { Claim $id has printed $code} + Wish to print $code with job id [expr {rand()}] + return + } + Commit code { Claim $id has program code $code} + Claim the cursor is $cursor return } if {$modifier == "ctrl" & $currentCharacter == "r"} { @@ -352,7 +391,8 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi Claim $this has demo { ## Okay, this needs to be: - # Claim $this is keyboard + # Claim $this is keyboard ABCD:1234:ABCD:1234 + # (MAC address) # Tape this program underneath the main keyboard of your system to try it out Claim $this is editor editor-1 } \ No newline at end of file -- cgit v1.2.3 From cc074ea156b55db7a314b68c11fab8cb6952910c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Mon, 6 Nov 2023 16:59:26 -0500 Subject: Remove twice-print indicator, clean up prints and comments --- virtual-programs/editor.folk | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 17d6f756..e30cfc89 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -153,7 +153,6 @@ proc getLineShift {lines} { } proc debug {position color} { - # Display::circle {*}$position 3 2 $color false Display::circle {*}$position 5 2 $color true } @@ -213,23 +212,11 @@ proc drawCursor {cursorPosition lineShift region} { } } -proc drawWontPrintTwice {id} { - When $id has printed /lastPrintedCode/ & $id has program code /code/ & $id has region /r/ { - if {$lastPrintedCode == $code} { - # TODO: Maybe offset to top right or something??? - Wish $id draws a circle with radius 3 color red filled true center [list 0 0] offset [list 100 100] - } else { - Wish $id draws a circle with radius 3 color color - } - } -} - When $id has program code /code/ & the clock time is /t/ & the cursor is /cursor/ & $id has region /r/ { set intTime [expr {int($t * 10)}] drawText [insertCursor $code $cursor $intTime] $cursor $intTime $r set lineShift [getLineShift $code] - drawWontPrintTwice $id drawCursor $cursor $lineShift $r if {$::debug_cursor} { @@ -334,9 +321,8 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi Claim the cursor is $cursor } default { - # Make "PRINTED statement display as long as code will-not-print-twice is around!" - # VISUAL FEEDBACKKKKKKK if {$modifier == "ctrl" & $currentCharacter == "p"} { + # TODO: Save time instead and prevent printing the same code within the same ... second? When $id has printed /lastPrintedCode/ { if {$lastPrintedCode == $code} { Claim the cursor is $cursor @@ -377,7 +363,7 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi } if {$modifier == "ctrl" & $currentCharacter == "u"} { # delete from cursor back to 0 and move cursor to 0 - Commit code { Claim $id has program code $code} + Commit code { Claim $id has program code [deleteToBeginning $code $cursor]} lassign $cursor x y Claim the cursor is [list 0 $y] return @@ -390,7 +376,7 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi } Claim $this has demo { - ## Okay, this needs to be: + ## Okay, this could be: # Claim $this is keyboard ABCD:1234:ABCD:1234 # (MAC address) # Tape this program underneath the main keyboard of your system to try it out -- cgit v1.2.3 From 15bb88eb10525d633ef3db7700fcac4bacf9fbf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Wed, 8 Nov 2023 14:35:30 -0500 Subject: Add virtual-programs/keyboard.folk --- virtual-programs/editor.folk | 6 ++-- virtual-programs/keyboard.folk | 77 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 virtual-programs/keyboard.folk (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index e30cfc89..8d3e12ad 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -376,9 +376,9 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi } Claim $this has demo { - ## Okay, this could be: - # Claim $this is keyboard ABCD:1234:ABCD:1234 - # (MAC address) # Tape this program underneath the main keyboard of your system to try it out Claim $this is editor editor-1 + + # Ideally: + Claim $this is keyboard with path /dev/input/by-path/platform-i8042-serio-0-event-kbd } \ No newline at end of file diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk new file mode 100644 index 00000000..630ab1f5 --- /dev/null +++ b/virtual-programs/keyboard.folk @@ -0,0 +1,77 @@ +set ::debug_keyboard true + +# TODO: Make this respond to a keyboard being plugged in or unplugged +# TODO: Make this the main keyboard logic and change keyboard.tcl to just use utilities from in here or from some share utils/keyboard.tcl + +When /someone/ claims /keyboardPage/ is keyboard with path /kbPath/ { + # (2023-11-07) on folk0 this is /dev/input/by-path/platform-i8042-serio-0-event-kbd + # (2023-11-08) on folk-convivial this is /dev/input/by-path/pci-0000:04:00.3-usb-0:3:1.0-event-kbd + Wish $keyboardPage is outlined white + Wish $keyboardPage is labelled "\n\n\n\n\n\n$kbPath" + + set keyboardDevices [list] + + # Get udevadm information for the device + set deviceInfo [exec udevadm info --query=property --name=$kbPath] + + # Check if it's a keyboard by looking for "ID_INPUT_KEYBOARD=1" in the udevadm output + if {[string first "ID_INPUT_KEYBOARD=1" $deviceInfo] >= 0} { + # It's a keyboard, add to list of keyboard devices + if {$::debug_keyboard} { + Wish $this is labelled "---------\ngot device $kbPath" + } + + lappend keyboardDevices $kbPath + + if {[file readable $kbPath] == 0} { + puts "Device $kbPath is not readable. Attempting to change permissions." + # Attempt to change permissions so that the file can be read + exec sudo chmod +r $device + } + + Claim $kbPath is a valid keyboard + } +} + +proc getKeyEvent {kb} { + variable evtBytes + variable evtFormat + + # See https://www.kernel.org/doc/Documentation/input/input.txt + # https://www.kernel.org/doc/Documentation/input/event-codes.txt + # https://github.com/torvalds/linux/blob/master/include/uapi/linux/input-event-codes.h + # + # struct input_event { + # struct timeval time; + # unsigned short type; (should be EV_KEY = 0x01) + # unsigned short code; (scancode; for example, 16 = q) + # unsigned int value; (0 for key release, 1 for press, 2 for repeat) + # }; + # + while 1 { + binary scan [read $kb $evtBytes] $evtFormat tvSec tvUsec type code value + if {$type == 0x01} { + return [list $code $value] + } + } +} + +When /keyboard/ is a valid keyboard { + set kb [open $keyboard r] + fconfigure $kb -translation binary + + variable evtBytes 16 + variable evtFormat iissi + if {[exec getconf LONG_BIT] == 64} { + set evtBytes 24 + set evtFormat wwssi + } + + Wish $this is labelled "$kb"; # Hmmmm, this crashes Folk ;# \n---\n [getKeyEvent $kb]" + # write to the statement DB a stream of millisecond timecodes and keys e.g. + # 123450 platform-i8042-serio-0-event-kbd H + # 123451 platform-i8042-serio-0-event-kbd e + # 123452 platform-i8042-serio-0-event-kbd l + # 123453 platform-i8042-serio-0-event-kbd l + # 123454 platform-i8042-serio-0-event-kbd o +} \ No newline at end of file -- cgit v1.2.3 From f6cb5760eb98caae80e76279271c28c40b0eb95b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Fri, 10 Nov 2023 15:58:24 -0500 Subject: Add keyboard progress --- virtual-programs/editor.folk | 10 +++++---- virtual-programs/keyboard.folk | 50 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 6 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 8d3e12ad..705bcd39 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -1,8 +1,10 @@ set id "editor-1" -set ::debug_cursor true -set ::debug_print true +set ::debug_cursor false +set ::debug_print false -When /page/ is editor /n/ & /page/ has region /r/ { +# When /page/ is editor /n/ & /page/ has region /r/ { +When /page/ is a keyboard with path /kbPath/ & /page/ has region /r/ { + # Claim $this is a keyboard with path /dev/input/by-path/platform-i8042-serio-0-event-kbd Wish $page is outlined gray Claim $id has region [region move $r up 450px] } @@ -381,4 +383,4 @@ Claim $this has demo { # Ideally: Claim $this is keyboard with path /dev/input/by-path/platform-i8042-serio-0-event-kbd -} \ No newline at end of file +} diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index 630ab1f5..ef122eec 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -3,7 +3,7 @@ set ::debug_keyboard true # TODO: Make this respond to a keyboard being plugged in or unplugged # TODO: Make this the main keyboard logic and change keyboard.tcl to just use utilities from in here or from some share utils/keyboard.tcl -When /someone/ claims /keyboardPage/ is keyboard with path /kbPath/ { +When /someone/ claims /keyboardPage/ is a keyboard with path /kbPath/ { # (2023-11-07) on folk0 this is /dev/input/by-path/platform-i8042-serio-0-event-kbd # (2023-11-08) on folk-convivial this is /dev/input/by-path/pci-0000:04:00.3-usb-0:3:1.0-event-kbd Wish $keyboardPage is outlined white @@ -74,4 +74,50 @@ When /keyboard/ is a valid keyboard { # 123452 platform-i8042-serio-0-event-kbd l # 123453 platform-i8042-serio-0-event-kbd l # 123454 platform-i8042-serio-0-event-kbd o -} \ No newline at end of file +} + +Claim $this has demo { + # TODO: Move this out into the top of keyboard.folk, I think? (2023-11-09 @cwervo) + # Keyboard detection functions + proc udevadm_properties {device} { + set status [catch {exec udevadm info --query=property --name=$device} result] + if {$status == 0} { + return $result + } else { + return "" + } + } + + proc is_keyboard {device} { + set properties [udevadm_properties $device] + if {$properties eq ""} { + return false + } + # Check if device is a keyboard and not a mouse + set isKeyboard [string match *ID_INPUT_KEYBOARD=1* $properties] + set isMouse [string match *ID_INPUT_MOUSE=1* $properties] + return [expr {$isKeyboard && !$isMouse}] + } + + set result [exec ls /dev/input/by-path] + set keyboards [list] + + foreach device $result { + set fullDevice "/dev/input/by-path/$device" + set keyboardCheck [is_keyboard $fullDevice] + # Only add the device to the list if it is a keyboard + if {$keyboardCheck} { + # lappend keyboards "$device // \n[udevadm_properties $fullDevice]\n" + lappend keyboards $fullDevice + } + } + + # The following lines seem to be pseudo-code or comments, they should be removed or corrected + Wish $this is outlined white + + foreach kb $keyboards { + Wish $this is labelled "----- $kb" + Claim $this is a keyboard with path $kb + Wish $this draws a circle with color navy filled true radius 10 + } +} -- cgit v1.2.3 From bdc71adf4d954357e22e866df9b1fdf2679c5ee1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Wed, 15 Nov 2023 17:17:51 -0500 Subject: Add beginning of multi-keyboard handling in keyboard process --- virtual-programs/editor.folk | 6 -- virtual-programs/keyboard.folk | 140 ++++++----------------------------------- 2 files changed, 20 insertions(+), 126 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 705bcd39..4f9945c3 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -2,9 +2,7 @@ set id "editor-1" set ::debug_cursor false set ::debug_print false -# When /page/ is editor /n/ & /page/ has region /r/ { When /page/ is a keyboard with path /kbPath/ & /page/ has region /r/ { - # Claim $this is a keyboard with path /dev/input/by-path/platform-i8042-serio-0-event-kbd Wish $page is outlined gray Claim $id has region [region move $r up 450px] } @@ -378,9 +376,5 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi } Claim $this has demo { - # Tape this program underneath the main keyboard of your system to try it out - Claim $this is editor editor-1 - - # Ideally: Claim $this is keyboard with path /dev/input/by-path/platform-i8042-serio-0-event-kbd } diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index ef122eec..41b2c1f8 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -1,123 +1,23 @@ set ::debug_keyboard true -# TODO: Make this respond to a keyboard being plugged in or unplugged -# TODO: Make this the main keyboard logic and change keyboard.tcl to just use utilities from in here or from some share utils/keyboard.tcl - -When /someone/ claims /keyboardPage/ is a keyboard with path /kbPath/ { - # (2023-11-07) on folk0 this is /dev/input/by-path/platform-i8042-serio-0-event-kbd - # (2023-11-08) on folk-convivial this is /dev/input/by-path/pci-0000:04:00.3-usb-0:3:1.0-event-kbd - Wish $keyboardPage is outlined white - Wish $keyboardPage is labelled "\n\n\n\n\n\n$kbPath" - - set keyboardDevices [list] - - # Get udevadm information for the device - set deviceInfo [exec udevadm info --query=property --name=$kbPath] - - # Check if it's a keyboard by looking for "ID_INPUT_KEYBOARD=1" in the udevadm output - if {[string first "ID_INPUT_KEYBOARD=1" $deviceInfo] >= 0} { - # It's a keyboard, add to list of keyboard devices - if {$::debug_keyboard} { - Wish $this is labelled "---------\ngot device $kbPath" - } - - lappend keyboardDevices $kbPath - - if {[file readable $kbPath] == 0} { - puts "Device $kbPath is not readable. Attempting to change permissions." - # Attempt to change permissions so that the file can be read - exec sudo chmod +r $device - } - - Claim $kbPath is a valid keyboard +Start process "keyboard" { + Wish $::thisProcess shares statements like \ + [list /someone/ claims /node/ has keyboards /...anything/] + Wish $::thisProcess shares statements like \ + [list /someone/ claims the default keyboard is /defaultKb/] + + source "pi/Keyboard.tcl" + + set keyboardDevices [Keyboard::walkInputEventPaths] + + puts "=========== keyboard info block ==============" + puts "Found [llength $keyboardDevices] keyboards:" + foreach keyboard $keyboardDevices { + puts " * eventpath: [dict get $keyboard eventPath]" + foreach devlink [dict get $keyboard devLinks] { + puts " ** : $devlink" } -} - -proc getKeyEvent {kb} { - variable evtBytes - variable evtFormat - - # See https://www.kernel.org/doc/Documentation/input/input.txt - # https://www.kernel.org/doc/Documentation/input/event-codes.txt - # https://github.com/torvalds/linux/blob/master/include/uapi/linux/input-event-codes.h - # - # struct input_event { - # struct timeval time; - # unsigned short type; (should be EV_KEY = 0x01) - # unsigned short code; (scancode; for example, 16 = q) - # unsigned int value; (0 for key release, 1 for press, 2 for repeat) - # }; - # - while 1 { - binary scan [read $kb $evtBytes] $evtFormat tvSec tvUsec type code value - if {$type == 0x01} { - return [list $code $value] - } - } -} - -When /keyboard/ is a valid keyboard { - set kb [open $keyboard r] - fconfigure $kb -translation binary - - variable evtBytes 16 - variable evtFormat iissi - if {[exec getconf LONG_BIT] == 64} { - set evtBytes 24 - set evtFormat wwssi - } - - Wish $this is labelled "$kb"; # Hmmmm, this crashes Folk ;# \n---\n [getKeyEvent $kb]" - # write to the statement DB a stream of millisecond timecodes and keys e.g. - # 123450 platform-i8042-serio-0-event-kbd H - # 123451 platform-i8042-serio-0-event-kbd e - # 123452 platform-i8042-serio-0-event-kbd l - # 123453 platform-i8042-serio-0-event-kbd l - # 123454 platform-i8042-serio-0-event-kbd o -} - -Claim $this has demo { - # TODO: Move this out into the top of keyboard.folk, I think? (2023-11-09 @cwervo) - # Keyboard detection functions - proc udevadm_properties {device} { - set status [catch {exec udevadm info --query=property --name=$device} result] - if {$status == 0} { - return $result - } else { - return "" - } - } - - proc is_keyboard {device} { - set properties [udevadm_properties $device] - if {$properties eq ""} { - return false - } - # Check if device is a keyboard and not a mouse - set isKeyboard [string match *ID_INPUT_KEYBOARD=1* $properties] - set isMouse [string match *ID_INPUT_MOUSE=1* $properties] - return [expr {$isKeyboard && !$isMouse}] - } - - set result [exec ls /dev/input/by-path] - set keyboards [list] - - foreach device $result { - set fullDevice "/dev/input/by-path/$device" - set keyboardCheck [is_keyboard $fullDevice] - # Only add the device to the list if it is a keyboard - if {$keyboardCheck} { - # lappend keyboards "$device // \n[udevadm_properties $fullDevice]\n" - lappend keyboards $fullDevice - } - } - - # The following lines seem to be pseudo-code or comments, they should be removed or corrected - Wish $this is outlined white - - foreach kb $keyboards { - Wish $this is labelled "----- $kb" - Claim $this is a keyboard with path $kb - Wish $this draws a circle with color navy filled true radius 10 - } -} + } + Claim the default keyboard is [lindex $keyboardDevices 0] + Claim $::thisNode has keyboards $keyboardDevices +} \ No newline at end of file -- cgit v1.2.3 From 73364937792d5813e7fc9855c5841aba12e97569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Thu, 16 Nov 2023 12:02:04 -0500 Subject: Ignore more keyboard modifier keys --- virtual-programs/editor.folk | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 4f9945c3..7400a299 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -308,8 +308,13 @@ Every time keyboard claims key /currentCharacter/ is down with modifiers /modifi MUTE - VOLUMEUP - VOLUMEDOWN - + ESC - + TAB - + CAPSLOCK - LEFTSHIFT - RIGHTSHIFT - + LEFTALT - + RIGHTALT - LEFTCTRL - RIGHTCTRL { # TODO: Implement DELETE, operates like BACKSPACE, but in the opposite direction -- cgit v1.2.3 From 7ebbd77da85a03f99601369c5b75b801fb10e2cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Thu, 16 Nov 2023 17:51:37 -0500 Subject: Remove pi/Keyboard.tcl, begin keyboard.folk in earnest --- virtual-programs/keyboard.folk | 188 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 182 insertions(+), 6 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index 41b2c1f8..fcf31993 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -1,17 +1,191 @@ set ::debug_keyboard true +# Keeping this here for reference, do this in a loop for each keyboard that is Claimed as out :) +# TODO: DELETE THIS, move this logic into virtual-programs/keyboard.folk +while false { + package require Thread + proc errorproc {id errorInfo} {puts "Thread error in $id: $errorInfo"} + thread::errorproc errorproc + + try { + set keyboardThread [thread::create [format { + source "pi/KeyCodes.tcl" + source "lib/c.tcl" + source "pi/cUtils.tcl" + Keyboard::init + puts "Keyboard tid: [getTid]" + + set keyStates [list up down repeat] + set modifiers [dict create \ + shift 0 \ + ctrl 0 \ + alt 0 \ + ] + # TODO: DELETE THIS, move this logic into virtual-programs/keyboard.folk + while false { + lassign [Keyboard::getKeyEvent] keyCode eventType + + set shift [dict get $modifiers shift] + set key [keyFromCode $keyCode $shift] + set keyState [lindex $keyStates $eventType] + + set isDown [expr {$keyState != "up"}] + if {[string match *SHIFT $key]} { + dict set modifiers shift $isDown + } + if {[string match *CTRL $key]} { + dict set modifiers ctrl $isDown + } + if {[string match *ALT $key]} { + dict set modifiers alt $isDown + } + + set heldModifiers [dict keys [dict filter $modifiers value 1]] + + # Use `list` to escape special chars (brackets, quotes, whitespace) + thread::send -async "%s" [subst { + Retract keyboard claims key /k/ is /t/ with modifiers /m/ + Assert keyboard claims key [list $key] is [list $keyState] with modifiers [list $heldModifiers] + }] + } + } [thread::id]]] + puts "Keyboard thread id: $keyboardThread" + } on error error { + puts stderr "Keyboard thread failed: $error" + } +} + Start process "keyboard" { - Wish $::thisProcess shares statements like \ - [list /someone/ claims /node/ has keyboards /...anything/] - Wish $::thisProcess shares statements like \ - [list /someone/ claims the default keyboard is /defaultKb/] + # Wish $::thisProcess shares statements like \ + # [list /someone/ claims /node/ has keyboards /...anything/] + # Wish $::thisProcess shares statements like \ + # [list /someone/ claims the default keyboard is /defaultKb/] + + # Event size depends on sizeof(long). Default to 32-bit longs + variable evtBytes 16 + variable evtFormat iissi + if {[exec getconf LONG_BIT] == 64} { + set evtBytes 24 + set evtFormat wwssi + } + + ############### + # Keyboard Linux device utils + ############### + proc udevadmProperties {device} { + return [exec udevadm info --query=property --name=$device] + } + + proc getDEVLINKS {device} { + set properties [udevadmProperties $device] + if {$properties eq ""} { + return "" + } + set devlinks [list] + foreach line [split $properties \n] { + if {[string match "DEVLINKS=*" $line]} { + set devlinks [string replace $line 0 8] + foreach path [split $devlinks " "] { + lappend devlinks $path + } + } + } + + return $devlinks + } - source "pi/Keyboard.tcl" + proc establishKeyPressListener {eventPath} { + set kb [open $eventPath r] + fconfigure $kb -translation binary + return $kb + } + + # Function to check if the device is a keyboard + proc isKeyboard {device} { + set properties [udevadmProperties $device] + if {$properties eq ""} { + return false + } + set isKeyboard [string match *ID_INPUT_KEYBOARD=1* $properties] + return $isKeyboard + # TODO: Excluding mice would nice to keey the list of keyboard devices short + # Alas, including mice is necessary for the Logitech K400R keyboard + # set isMouse [string match *ID_INPUT_MOUSE=1* $properties] + # return [expr {$isKeyboard && !$isMouse}] + } + + #### + # /dev/input/event* addresses are the ground truth for keyboard devices + # + # This function goes through each of them and checks if they are keyboards + proc walkInputEventPaths {} { + set allDevices [glob -nocomplain "/dev/input/event*"] + set keyboards [list] + foreach device $allDevices { + set devLinks [getDEVLINKS $device] + if {[llength $devLinks] > 0 && [isKeyboard $device]} { + if {[file readable $device] == 0} { + puts "Device $device is not readable. Attempting to change permissions." + # Attempt to change permissions so that the file can be read + exec sudo chmod +r $device + } + lappend keyboards [dict create eventPath $device devLinks $devLinks] + } + } + return $keyboards + } + + # TODO: Parameterize this to be a generic keyboard + proc init {} { + variable kb + variable keyboards + + set keyboardDevices [walkInputEventPaths] + set keyboards $keyboardDevices + + puts "=== Keyboard devices ([llength $keyboardDevices])" + set firstKeyboard [dict get [lindex $keyboardDevices 0] eventPath] + set kb [establishKeyPressListener $firstKeyboard] + puts "=== Opened keyboard device: $firstKeyboard \\ $kb" + } + + # e.g. getKeyEvent /dev/input/event0 + proc getKeyEvent {keyboardSpecifier args} { + puts "getting key event for $keyboardSpecifier" + When /thisProcess/ has keyboards /keyboardsList/ { + # Hmmmm, how to make this a global accessor? Always just pass in the keyboard array tooo???? That'd be dumb idk + set keyboardStream [dict get $keyboardsList $keyboardSpecifier] + + # TODO: Allow keyboardSpecifier to be a keyboard device file + # e.g. /dev/input/by-path/platform-i8042-serio-0-event-kbd + variable evtBytes + variable evtFormat + + # See https://www.kernel.org/doc/Documentation/input/input.txt + # https://www.kernel.org/doc/Documentation/input/event-codes.txt + # https://github.com/torvalds/linux/blob/master/include/uapi/linux/input-event-codes.h + # + # struct input_event { + # struct timeval time; + # unsigned short type; (should be EV_KEY = 0x01) + # unsigned short code; (scancode; for example, 16 = q) + # unsigned int value; (0 for key release, 1 for press, 2 for repeat) + # }; + # + while 1 { + binary scan [read $keyboardStream $evtBytes] $evtFormat tvSec tvUsec type code value + if {$type == 0x01} { + return [list $code $value] + } + } + } + } - set keyboardDevices [Keyboard::walkInputEventPaths] + set keyboardDevices [walkInputEventPaths] puts "=========== keyboard info block ==============" puts "Found [llength $keyboardDevices] keyboards:" + # TODO: Make a process for each eventPath found that's a keyboard! foreach keyboard $keyboardDevices { puts " * eventpath: [dict get $keyboard eventPath]" foreach devlink [dict get $keyboard devLinks] { @@ -20,4 +194,6 @@ Start process "keyboard" { } Claim the default keyboard is [lindex $keyboardDevices 0] Claim $::thisNode has keyboards $keyboardDevices + # Need to modify getKeyPress (define it in here, even, maybe???) to listen to all keyboards hmmmm + # Maybe it should even return a keyboard originator in the return value? } \ No newline at end of file -- cgit v1.2.3 From 38412bd0ec86e4b55ae41dcd829527b8f50c80de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Fri, 17 Nov 2023 18:30:51 -0500 Subject: Add eventPath check for keyboardStreams --- virtual-programs/keyboard.folk | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index fcf31993..47d691d4 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -149,12 +149,25 @@ Start process "keyboard" { puts "=== Opened keyboard device: $firstKeyboard \\ $kb" } + proc findMatchingEventPath {searchList eventPath} { + foreach element $searchList { + set elEventPath [dict get $element eventPath] + if {$eventPath eq $elEventPath} { + return $element + } + } + return 0 + } + # e.g. getKeyEvent /dev/input/event0 proc getKeyEvent {keyboardSpecifier args} { puts "getting key event for $keyboardSpecifier" When /thisProcess/ has keyboards /keyboardsList/ { # Hmmmm, how to make this a global accessor? Always just pass in the keyboard array tooo???? That'd be dumb idk - set keyboardStream [dict get $keyboardsList $keyboardSpecifier] + puts "==== getKeyEvent: $keyboardsList" + puts "==== getKeyEvent: $keyboardSpecifier" + # set keyboardStream [dict get $keyboardsList $keyboardSpecifier] + set keyboardStream [findMatchingEventPath $keyboardsList $keyboardSpecifier] # TODO: Allow keyboardSpecifier to be a keyboard device file # e.g. /dev/input/by-path/platform-i8042-serio-0-event-kbd @@ -172,9 +185,12 @@ Start process "keyboard" { # unsigned int value; (0 for key release, 1 for press, 2 for repeat) # }; # + set kbEventPath [dict get $keyboardStream eventPath] while 1 { - binary scan [read $keyboardStream $evtBytes] $evtFormat tvSec tvUsec type code value + binary scan [read $kbEventPath $evtBytes] $evtFormat tvSec tvUsec type code value if {$type == 0x01} { + Retract keyboard $keyboardSpecifier claims key /k/ is /t/ with modifiers /m/ + Assert keyboard $keyboardSpecifier claims key [list $key] is [list $keyState] with modifiers [list $heldModifiers] return [list $code $value] } } @@ -187,9 +203,13 @@ Start process "keyboard" { puts "Found [llength $keyboardDevices] keyboards:" # TODO: Make a process for each eventPath found that's a keyboard! foreach keyboard $keyboardDevices { - puts " * eventpath: [dict get $keyboard eventPath]" + set eventPath [dict get $keyboard eventPath] + puts " * eventpath: $eventPath" + establishKeyPressListener $eventPath + getKeyEvent $eventPath foreach devlink [dict get $keyboard devLinks] { puts " ** : $devlink" + establishKeyPressListener $devlink } } Claim the default keyboard is [lindex $keyboardDevices 0] -- cgit v1.2.3 From ebfbae49c903e06057a426424250c9d9d812e476 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Mon, 20 Nov 2023 18:08:33 -0500 Subject: Add dummy claims to track down stmt share bug --- virtual-programs/keyboard.folk | 63 +++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 32 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index 47d691d4..a3118377 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -56,10 +56,13 @@ while false { } Start process "keyboard" { - # Wish $::thisProcess shares statements like \ - # [list /someone/ claims /node/ has keyboards /...anything/] - # Wish $::thisProcess shares statements like \ - # [list /someone/ claims the default keyboard is /defaultKb/] + # TODO: Simplify + Wish $::thisProcess shares statements like \ + [list /someone/ claims /node/ has keyboards /keyboardsList/] + Wish $::thisProcess shares statements like \ + [list /someone/ claims the default keyboard is /defaultKb/] + + puts "node: $::thisNode" # Event size depends on sizeof(long). Default to 32-bit longs variable evtBytes 16 @@ -135,20 +138,6 @@ Start process "keyboard" { return $keyboards } - # TODO: Parameterize this to be a generic keyboard - proc init {} { - variable kb - variable keyboards - - set keyboardDevices [walkInputEventPaths] - set keyboards $keyboardDevices - - puts "=== Keyboard devices ([llength $keyboardDevices])" - set firstKeyboard [dict get [lindex $keyboardDevices 0] eventPath] - set kb [establishKeyPressListener $firstKeyboard] - puts "=== Opened keyboard device: $firstKeyboard \\ $kb" - } - proc findMatchingEventPath {searchList eventPath} { foreach element $searchList { set elEventPath [dict get $element eventPath] @@ -162,7 +151,7 @@ Start process "keyboard" { # e.g. getKeyEvent /dev/input/event0 proc getKeyEvent {keyboardSpecifier args} { puts "getting key event for $keyboardSpecifier" - When /thisProcess/ has keyboards /keyboardsList/ { + When /someone/ claims /thisNode/ has keyboards /keyboardsList/ { # Hmmmm, how to make this a global accessor? Always just pass in the keyboard array tooo???? That'd be dumb idk puts "==== getKeyEvent: $keyboardsList" puts "==== getKeyEvent: $keyboardSpecifier" @@ -185,12 +174,13 @@ Start process "keyboard" { # unsigned int value; (0 for key release, 1 for press, 2 for repeat) # }; # - set kbEventPath [dict get $keyboardStream eventPath] + set kbEventPath [open [dict get $keyboardStream eventPath] r] while 1 { binary scan [read $kbEventPath $evtBytes] $evtFormat tvSec tvUsec type code value if {$type == 0x01} { Retract keyboard $keyboardSpecifier claims key /k/ is /t/ with modifiers /m/ Assert keyboard $keyboardSpecifier claims key [list $key] is [list $keyState] with modifiers [list $heldModifiers] + puts "got key event ($keyboardSpecifier): $key $keyState $heldModifiers" return [list $code $value] } } @@ -202,18 +192,27 @@ Start process "keyboard" { puts "=========== keyboard info block ==============" puts "Found [llength $keyboardDevices] keyboards:" # TODO: Make a process for each eventPath found that's a keyboard! - foreach keyboard $keyboardDevices { - set eventPath [dict get $keyboard eventPath] - puts " * eventpath: $eventPath" - establishKeyPressListener $eventPath - getKeyEvent $eventPath - foreach devlink [dict get $keyboard devLinks] { - puts " ** : $devlink" - establishKeyPressListener $devlink - } - } - Claim the default keyboard is [lindex $keyboardDevices 0] - Claim $::thisNode has keyboards $keyboardDevices +# foreach keyboard $keyboardDevices { +# set eventPath [dict get $keyboard eventPath] +# puts " - eventpath: $eventPath" +# establishKeyPressListener $eventPath +# getKeyEvent $eventPath +# foreach devlink [dict get $keyboard devLinks] { +# puts " -- : $devlink" +# establishKeyPressListener $devlink +# } +# } +# puts " <<<<<< " +# puts " keyboardsDevices: $keyboardDevices " +# puts " default keyboard: [lindex $keyboardDevices 0] " +# puts " <<<<<< " +# Claim the default keyboard is [lindex $keyboardDevices 0] +# Claim $::thisNode has keyboards $keyboardDevices +# When /someone/ claims /node/ has keyboards /keyboards/ { +# puts "$node: has keyboard $keyboards" +# } + Claim $::thisNode has keyboards [list "beep"] + Claim the default keyboard is "beep" # Need to modify getKeyPress (define it in here, even, maybe???) to listen to all keyboards hmmmm # Maybe it should even return a keyboard originator in the return value? } \ No newline at end of file -- cgit v1.2.3 From f89760b3f9c9655c8c15ffee8b33da75320f430f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Mon, 27 Nov 2023 13:41:19 -0500 Subject: Add logging on key presses --- virtual-programs/keyboard.folk | 211 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 197 insertions(+), 14 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index a3118377..03abbf88 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -55,6 +55,189 @@ while false { } } +proc udevadmProperties {device} { + return [exec udevadm info --query=property --name=$device] +} + +proc getDEVLINKS {device} { + set properties [udevadmProperties $device] + if {$properties eq ""} { + return "" + } + set devlinks [list] + foreach line [split $properties \n] { + if {[string match "DEVLINKS=*" $line]} { + set devlinks [string replace $line 0 8] + foreach path [split $devlinks " "] { + lappend devlinks $path + } + } + } + + return $devlinks +} + +proc establishKeyPressListener {eventPath} { + set kb [open $eventPath r] + fconfigure $kb -translation binary + return $kb +} + +# Function to check if the device is a keyboard +proc isKeyboard {device} { + set properties [udevadmProperties $device] + if {$properties eq ""} { + return false + } + set isKeyboard [string match *ID_INPUT_KEYBOARD=1* $properties] + return $isKeyboard + # TODO: Excluding mice would nice to keey the list of keyboard devices short + # Alas, including mice is necessary for the Logitech K400R keyboard + # set isMouse [string match *ID_INPUT_MOUSE=1* $properties] + # return [expr {$isKeyboard && !$isMouse}] +} + +#### +# /dev/input/event* addresses are the ground truth for keyboard devices +# +# This function goes through each of them and checks if they are keyboards +proc walkInputEventPaths {} { + set allDevices [glob -nocomplain "/dev/input/event*"] + set keyboards [list] + foreach device $allDevices { + set devLinks [getDEVLINKS $device] + if {[llength $devLinks] > 0 && [isKeyboard $device]} { + if {[file readable $device] == 0} { + puts "Device $device is not readable. Attempting to change permissions." + # Attempt to change permissions so that the file can be read + exec sudo chmod +r $device + } + lappend keyboards [dict create eventPath $device devLinks $devLinks] + } + } + return $keyboards +} + +proc findMatchingEventPath {searchList eventPath} { + foreach element $searchList { + set elEventPath [dict get $element eventPath] + if {$eventPath eq $elEventPath} { + return $element + } + } + return 0 +} + +# e.g. getKeyEvent /dev/input/event0 +proc getKeyEvent {keyboardSpecifier args} { +puts "getting key event for $keyboardSpecifier" +When /someone/ claims /thisNode/ has keyboards /keyboardsList/ { + # Hmmmm, how to make this a global accessor? Always just pass in the keyboard array tooo???? That'd be dumb idk + puts "==== getKeyEvent: $keyboardsList" + puts "==== getKeyEvent: $keyboardSpecifier" + # set keyboardStream [dict get $keyboardsList $keyboardSpecifier] + set keyboardStream [findMatchingEventPath $keyboardsList $keyboardSpecifier] + + # TODO: Allow keyboardSpecifier to be a keyboard device file + # e.g. /dev/input/by-path/platform-i8042-serio-0-event-kbd + variable evtBytes + variable evtFormat + + # See https://www.kernel.org/doc/Documentation/input/input.txt + # https://www.kernel.org/doc/Documentation/input/event-codes.txt + # https://github.com/torvalds/linux/blob/master/include/uapi/linux/input-event-codes.h + # + # struct input_event { + # struct timeval time; + # unsigned short type; (should be EV_KEY = 0x01) + # unsigned short code; (scancode; for example, 16 = q) + # unsigned int value; (0 for key release, 1 for press, 2 for repeat) + # }; + # + set kbEventPath [open [dict get $keyboardStream eventPath] r] + while 1 { + binary scan [read $kbEventPath $evtBytes] $evtFormat tvSec tvUsec type code value + if {$type == 0x01} { + Retract keyboard $keyboardSpecifier claims key /k/ is /t/ with modifiers /m/ + Assert keyboard $keyboardSpecifier claims key [list $key] is [list $keyState] with modifiers [list $heldModifiers] + puts "got key event ($keyboardSpecifier): $key $keyState $heldModifiers" + return [list $code $value] + } + } +} +} + + +set keyboardDevices [walkInputEventPaths] + +# go through each keyboard device and start a process that +foreach keyboard $keyboardDevices { + set eventPath [dict get $keyboard eventPath] + Start process "keyboard-$eventPath" { + variable evtBytes 16 + variable evtFormat iissi + if {[exec getconf LONG_BIT] == 64} { + set evtBytes 24 + set evtFormat wwssi + } + set keyboardSpecifier $eventPath + puts "starting keyboard process for $keyboard" + while 1 { + puts "reading ... ($eventPath)" + binary scan [read [open $eventPath r] $evtBytes] $evtFormat tvSec tvUsec type code value + puts "read: $tvSec $tvUsec $type $code $value" + if {$type == 0x01} { + puts "got key event ($keyboardSpecifier): $code | $value" + return [list $code $value] + } + } + + return + + set kbEventPath [open $eventPath r] + + set keyStates [list up down repeat] + set modifiers [dict create \ + shift 0 \ + ctrl 0 \ + alt 0 \ + ] + + set shift [dict get $modifiers shift] + set key [keyFromCode $keyCode $shift] + set keyState [lindex $keyStates $eventType] + + set isDown [expr {$keyState != "up"}] + if {[string match *SHIFT $key]} { + dict set modifiers shift $isDown + } + if {[string match *CTRL $key]} { + dict set modifiers ctrl $isDown + } + if {[string match *ALT $key]} { + dict set modifiers alt $isDown + } + + set heldModifiers [dict keys [dict filter $modifiers value 1]] + + while 1 { + binary scan [read $kbEventPath $evtBytes] $evtFormat tvSec tvUsec type code value + if {$type == 0x01} { + Retract keyboard $keyboardSpecifier claims key /k/ is /t/ with modifiers /m/ + Assert keyboard $keyboardSpecifier claims key [list $key] is [list $keyState] with modifiers [list $heldModifiers] + puts "got key event ($keyboardSpecifier): $key $keyState $heldModifiers" + return [list $code $value] + } + } + # establishKeyPressListener $eventPath + # foreach devlink [dict get $keyboard devLinks] { + # puts " -- : $devlink" + # establishKeyPressListener $devlink + # } + } +} + +return Start process "keyboard" { # TODO: Simplify Wish $::thisProcess shares statements like \ @@ -192,27 +375,27 @@ Start process "keyboard" { puts "=========== keyboard info block ==============" puts "Found [llength $keyboardDevices] keyboards:" # TODO: Make a process for each eventPath found that's a keyboard! -# foreach keyboard $keyboardDevices { -# set eventPath [dict get $keyboard eventPath] -# puts " - eventpath: $eventPath" -# establishKeyPressListener $eventPath -# getKeyEvent $eventPath -# foreach devlink [dict get $keyboard devLinks] { -# puts " -- : $devlink" -# establishKeyPressListener $devlink -# } -# } + foreach keyboard $keyboardDevices { + set eventPath [dict get $keyboard eventPath] + puts " - eventpath: $eventPath" + establishKeyPressListener $eventPath + # getKeyEvent $eventPath + foreach devlink [dict get $keyboard devLinks] { + puts " -- : $devlink" + establishKeyPressListener $devlink + } + } # puts " <<<<<< " # puts " keyboardsDevices: $keyboardDevices " # puts " default keyboard: [lindex $keyboardDevices 0] " # puts " <<<<<< " -# Claim the default keyboard is [lindex $keyboardDevices 0] -# Claim $::thisNode has keyboards $keyboardDevices + Claim the default keyboard is [lindex $keyboardDevices 0] + Claim $::thisNode has keyboards $keyboardDevices # When /someone/ claims /node/ has keyboards /keyboards/ { # puts "$node: has keyboard $keyboards" # } - Claim $::thisNode has keyboards [list "beep"] - Claim the default keyboard is "beep" +# Claim $::thisNode has keyboards [list "beep"] +# Claim the default keyboard is "beep" # Need to modify getKeyPress (define it in here, even, maybe???) to listen to all keyboards hmmmm # Maybe it should even return a keyboard originator in the return value? } \ No newline at end of file -- cgit v1.2.3 From 8b3a6f9bfd6cc51aa42ba68f3259b78381b38cd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Tue, 28 Nov 2023 16:24:39 -0500 Subject: Add readCount logging --- virtual-programs/keyboard.folk | 134 ++++++++++++++++++++++------------------- 1 file changed, 72 insertions(+), 62 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index 03abbf88..c6dba2a2 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -1,60 +1,5 @@ set ::debug_keyboard true -# Keeping this here for reference, do this in a loop for each keyboard that is Claimed as out :) -# TODO: DELETE THIS, move this logic into virtual-programs/keyboard.folk -while false { - package require Thread - proc errorproc {id errorInfo} {puts "Thread error in $id: $errorInfo"} - thread::errorproc errorproc - - try { - set keyboardThread [thread::create [format { - source "pi/KeyCodes.tcl" - source "lib/c.tcl" - source "pi/cUtils.tcl" - Keyboard::init - puts "Keyboard tid: [getTid]" - - set keyStates [list up down repeat] - set modifiers [dict create \ - shift 0 \ - ctrl 0 \ - alt 0 \ - ] - # TODO: DELETE THIS, move this logic into virtual-programs/keyboard.folk - while false { - lassign [Keyboard::getKeyEvent] keyCode eventType - - set shift [dict get $modifiers shift] - set key [keyFromCode $keyCode $shift] - set keyState [lindex $keyStates $eventType] - - set isDown [expr {$keyState != "up"}] - if {[string match *SHIFT $key]} { - dict set modifiers shift $isDown - } - if {[string match *CTRL $key]} { - dict set modifiers ctrl $isDown - } - if {[string match *ALT $key]} { - dict set modifiers alt $isDown - } - - set heldModifiers [dict keys [dict filter $modifiers value 1]] - - # Use `list` to escape special chars (brackets, quotes, whitespace) - thread::send -async "%s" [subst { - Retract keyboard claims key /k/ is /t/ with modifiers /m/ - Assert keyboard claims key [list $key] is [list $keyState] with modifiers [list $heldModifiers] - }] - } - } [thread::id]]] - puts "Keyboard thread id: $keyboardThread" - } on error error { - puts stderr "Keyboard thread failed: $error" - } -} - proc udevadmProperties {device} { return [exec udevadm info --query=property --name=$device] } @@ -174,21 +119,31 @@ set keyboardDevices [walkInputEventPaths] foreach keyboard $keyboardDevices { set eventPath [dict get $keyboard eventPath] Start process "keyboard-$eventPath" { + source "pi/KeyCodes.tcl" + Wish $::thisProcess shares statements like \ + [list Assert keyboard /k/ claims key /code/ is /value/ /keyboardsList/] + Wish $::thisProcess shares statements like \ + [list keyboard /k/ claims key /code/ is /value/ /keyboardsList/] variable evtBytes 16 variable evtFormat iissi if {[exec getconf LONG_BIT] == 64} { - set evtBytes 24 - set evtFormat wwssi + set evtBytes 24 + set evtFormat wwssi } set keyboardSpecifier $eventPath + set eventPathChannel [open $eventPath r] puts "starting keyboard process for $keyboard" + set readCount 0 while 1 { - puts "reading ... ($eventPath)" - binary scan [read [open $eventPath r] $evtBytes] $evtFormat tvSec tvUsec type code value - puts "read: $tvSec $tvUsec $type $code $value" + set readCount [incr $readCount] + puts "reading ... (readCount: $readCount) ($eventPath) from ($eventPathChannel)" + binary scan [read $eventPathChannel $evtBytes] $evtFormat tvSec tvUsec type code value if {$type == 0x01} { - puts "got key event ($keyboardSpecifier): $code | $value" - return [list $code $value] + puts "read: $tvSec $tvUsec $type ([expr $type == 0x01]) $code $value" + puts "got key event ($keyboardSpecifier): $code => [keyFromCode $code false] | $value" + Retract keyboard $keyboardSpecifier claims key /k/ is /t/ + Assert keyboard $keyboardSpecifier claims key [keyFromCode $code false] is [list $value] + # return [list $code $value] } } @@ -398,4 +353,59 @@ Start process "keyboard" { # Claim the default keyboard is "beep" # Need to modify getKeyPress (define it in here, even, maybe???) to listen to all keyboards hmmmm # Maybe it should even return a keyboard originator in the return value? +} + +# Keeping this here for reference, do this in a loop for each keyboard that is Claimed as out :) +# TODO: DELETE THIS, move this logic into virtual-programs/keyboard.folk +while false { + package require Thread + proc errorproc {id errorInfo} {puts "Thread error in $id: $errorInfo"} + thread::errorproc errorproc + + try { + set keyboardThread [thread::create [format { + source "pi/KeyCodes.tcl" + source "lib/c.tcl" + source "pi/cUtils.tcl" + Keyboard::init + puts "Keyboard tid: [getTid]" + + set keyStates [list up down repeat] + set modifiers [dict create \ + shift 0 \ + ctrl 0 \ + alt 0 \ + ] + # TODO: DELETE THIS, move this logic into virtual-programs/keyboard.folk + while false { + lassign [Keyboard::getKeyEvent] keyCode eventType + + set shift [dict get $modifiers shift] + set key [keyFromCode $keyCode $shift] + set keyState [lindex $keyStates $eventType] + + set isDown [expr {$keyState != "up"}] + if {[string match *SHIFT $key]} { + dict set modifiers shift $isDown + } + if {[string match *CTRL $key]} { + dict set modifiers ctrl $isDown + } + if {[string match *ALT $key]} { + dict set modifiers alt $isDown + } + + set heldModifiers [dict keys [dict filter $modifiers value 1]] + + # Use `list` to escape special chars (brackets, quotes, whitespace) + thread::send -async "%s" [subst { + Retract keyboard claims key /k/ is /t/ with modifiers /m/ + Assert keyboard claims key [list $key] is [list $keyState] with modifiers [list $heldModifiers] + }] + } + } [thread::id]]] + puts "Keyboard thread id: $keyboardThread" + } on error error { + puts stderr "Keyboard thread failed: $error" + } } \ No newline at end of file -- cgit v1.2.3 From 27b34e6a38a7a3d7c5482c022ec0bdaa2dff86d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Tue, 28 Nov 2023 18:19:37 -0500 Subject: Remove old reference code --- virtual-programs/keyboard.folk | 276 ++--------------------------------------- 1 file changed, 11 insertions(+), 265 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index c6dba2a2..edbef9d7 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -73,46 +73,6 @@ proc findMatchingEventPath {searchList eventPath} { return 0 } -# e.g. getKeyEvent /dev/input/event0 -proc getKeyEvent {keyboardSpecifier args} { -puts "getting key event for $keyboardSpecifier" -When /someone/ claims /thisNode/ has keyboards /keyboardsList/ { - # Hmmmm, how to make this a global accessor? Always just pass in the keyboard array tooo???? That'd be dumb idk - puts "==== getKeyEvent: $keyboardsList" - puts "==== getKeyEvent: $keyboardSpecifier" - # set keyboardStream [dict get $keyboardsList $keyboardSpecifier] - set keyboardStream [findMatchingEventPath $keyboardsList $keyboardSpecifier] - - # TODO: Allow keyboardSpecifier to be a keyboard device file - # e.g. /dev/input/by-path/platform-i8042-serio-0-event-kbd - variable evtBytes - variable evtFormat - - # See https://www.kernel.org/doc/Documentation/input/input.txt - # https://www.kernel.org/doc/Documentation/input/event-codes.txt - # https://github.com/torvalds/linux/blob/master/include/uapi/linux/input-event-codes.h - # - # struct input_event { - # struct timeval time; - # unsigned short type; (should be EV_KEY = 0x01) - # unsigned short code; (scancode; for example, 16 = q) - # unsigned int value; (0 for key release, 1 for press, 2 for repeat) - # }; - # - set kbEventPath [open [dict get $keyboardStream eventPath] r] - while 1 { - binary scan [read $kbEventPath $evtBytes] $evtFormat tvSec tvUsec type code value - if {$type == 0x01} { - Retract keyboard $keyboardSpecifier claims key /k/ is /t/ with modifiers /m/ - Assert keyboard $keyboardSpecifier claims key [list $key] is [list $keyState] with modifiers [list $heldModifiers] - puts "got key event ($keyboardSpecifier): $key $keyState $heldModifiers" - return [list $code $value] - } - } -} -} - - set keyboardDevices [walkInputEventPaths] # go through each keyboard device and start a process that @@ -121,9 +81,7 @@ foreach keyboard $keyboardDevices { Start process "keyboard-$eventPath" { source "pi/KeyCodes.tcl" Wish $::thisProcess shares statements like \ - [list Assert keyboard /k/ claims key /code/ is /value/ /keyboardsList/] - Wish $::thisProcess shares statements like \ - [list keyboard /k/ claims key /code/ is /value/ /keyboardsList/] + [list keyboard /k/ claims /...anything/] variable evtBytes 16 variable evtFormat iissi if {[exec getconf LONG_BIT] == 64} { @@ -131,18 +89,24 @@ foreach keyboard $keyboardDevices { set evtFormat wwssi } set keyboardSpecifier $eventPath - set eventPathChannel [open $eventPath r] + variable eventPathChannel [open $eventPath r] puts "starting keyboard process for $keyboard" set readCount 0 while 1 { set readCount [incr $readCount] - puts "reading ... (readCount: $readCount) ($eventPath) from ($eventPathChannel)" + puts "====\nreading ... (readCount: $readCount) ($eventPath) from ($eventPathChannel)" binary scan [read $eventPathChannel $evtBytes] $evtFormat tvSec tvUsec type code value + if {$type > 0x04} { + set eventPathChannel [open $eventPath r] + binary scan [read $eventPathChannel $evtBytes] $evtFormat tvSec tvUsec type code value + } + puts "from scan: (type | $type) (code | $code) $value" if {$type == 0x01} { puts "read: $tvSec $tvUsec $type ([expr $type == 0x01]) $code $value" puts "got key event ($keyboardSpecifier): $code => [keyFromCode $code false] | $value" - Retract keyboard $keyboardSpecifier claims key /k/ is /t/ + Retract keyboard $keyboardSpecifier key /k/ is /t/ Assert keyboard $keyboardSpecifier claims key [keyFromCode $code false] is [list $value] + Claim keyboard $keyboardSpecifier claims thing # return [list $code $value] } } @@ -181,7 +145,7 @@ foreach keyboard $keyboardDevices { Retract keyboard $keyboardSpecifier claims key /k/ is /t/ with modifiers /m/ Assert keyboard $keyboardSpecifier claims key [list $key] is [list $keyState] with modifiers [list $heldModifiers] puts "got key event ($keyboardSpecifier): $key $keyState $heldModifiers" - return [list $code $value] + # return [list $code $value] } } # establishKeyPressListener $eventPath @@ -190,222 +154,4 @@ foreach keyboard $keyboardDevices { # establishKeyPressListener $devlink # } } -} - -return -Start process "keyboard" { - # TODO: Simplify - Wish $::thisProcess shares statements like \ - [list /someone/ claims /node/ has keyboards /keyboardsList/] - Wish $::thisProcess shares statements like \ - [list /someone/ claims the default keyboard is /defaultKb/] - - puts "node: $::thisNode" - - # Event size depends on sizeof(long). Default to 32-bit longs - variable evtBytes 16 - variable evtFormat iissi - if {[exec getconf LONG_BIT] == 64} { - set evtBytes 24 - set evtFormat wwssi - } - - ############### - # Keyboard Linux device utils - ############### - proc udevadmProperties {device} { - return [exec udevadm info --query=property --name=$device] - } - - proc getDEVLINKS {device} { - set properties [udevadmProperties $device] - if {$properties eq ""} { - return "" - } - set devlinks [list] - foreach line [split $properties \n] { - if {[string match "DEVLINKS=*" $line]} { - set devlinks [string replace $line 0 8] - foreach path [split $devlinks " "] { - lappend devlinks $path - } - } - } - - return $devlinks - } - - proc establishKeyPressListener {eventPath} { - set kb [open $eventPath r] - fconfigure $kb -translation binary - return $kb - } - - # Function to check if the device is a keyboard - proc isKeyboard {device} { - set properties [udevadmProperties $device] - if {$properties eq ""} { - return false - } - set isKeyboard [string match *ID_INPUT_KEYBOARD=1* $properties] - return $isKeyboard - # TODO: Excluding mice would nice to keey the list of keyboard devices short - # Alas, including mice is necessary for the Logitech K400R keyboard - # set isMouse [string match *ID_INPUT_MOUSE=1* $properties] - # return [expr {$isKeyboard && !$isMouse}] - } - - #### - # /dev/input/event* addresses are the ground truth for keyboard devices - # - # This function goes through each of them and checks if they are keyboards - proc walkInputEventPaths {} { - set allDevices [glob -nocomplain "/dev/input/event*"] - set keyboards [list] - foreach device $allDevices { - set devLinks [getDEVLINKS $device] - if {[llength $devLinks] > 0 && [isKeyboard $device]} { - if {[file readable $device] == 0} { - puts "Device $device is not readable. Attempting to change permissions." - # Attempt to change permissions so that the file can be read - exec sudo chmod +r $device - } - lappend keyboards [dict create eventPath $device devLinks $devLinks] - } - } - return $keyboards - } - - proc findMatchingEventPath {searchList eventPath} { - foreach element $searchList { - set elEventPath [dict get $element eventPath] - if {$eventPath eq $elEventPath} { - return $element - } - } - return 0 - } - - # e.g. getKeyEvent /dev/input/event0 - proc getKeyEvent {keyboardSpecifier args} { - puts "getting key event for $keyboardSpecifier" - When /someone/ claims /thisNode/ has keyboards /keyboardsList/ { - # Hmmmm, how to make this a global accessor? Always just pass in the keyboard array tooo???? That'd be dumb idk - puts "==== getKeyEvent: $keyboardsList" - puts "==== getKeyEvent: $keyboardSpecifier" - # set keyboardStream [dict get $keyboardsList $keyboardSpecifier] - set keyboardStream [findMatchingEventPath $keyboardsList $keyboardSpecifier] - - # TODO: Allow keyboardSpecifier to be a keyboard device file - # e.g. /dev/input/by-path/platform-i8042-serio-0-event-kbd - variable evtBytes - variable evtFormat - - # See https://www.kernel.org/doc/Documentation/input/input.txt - # https://www.kernel.org/doc/Documentation/input/event-codes.txt - # https://github.com/torvalds/linux/blob/master/include/uapi/linux/input-event-codes.h - # - # struct input_event { - # struct timeval time; - # unsigned short type; (should be EV_KEY = 0x01) - # unsigned short code; (scancode; for example, 16 = q) - # unsigned int value; (0 for key release, 1 for press, 2 for repeat) - # }; - # - set kbEventPath [open [dict get $keyboardStream eventPath] r] - while 1 { - binary scan [read $kbEventPath $evtBytes] $evtFormat tvSec tvUsec type code value - if {$type == 0x01} { - Retract keyboard $keyboardSpecifier claims key /k/ is /t/ with modifiers /m/ - Assert keyboard $keyboardSpecifier claims key [list $key] is [list $keyState] with modifiers [list $heldModifiers] - puts "got key event ($keyboardSpecifier): $key $keyState $heldModifiers" - return [list $code $value] - } - } - } - } - - set keyboardDevices [walkInputEventPaths] - - puts "=========== keyboard info block ==============" - puts "Found [llength $keyboardDevices] keyboards:" - # TODO: Make a process for each eventPath found that's a keyboard! - foreach keyboard $keyboardDevices { - set eventPath [dict get $keyboard eventPath] - puts " - eventpath: $eventPath" - establishKeyPressListener $eventPath - # getKeyEvent $eventPath - foreach devlink [dict get $keyboard devLinks] { - puts " -- : $devlink" - establishKeyPressListener $devlink - } - } -# puts " <<<<<< " -# puts " keyboardsDevices: $keyboardDevices " -# puts " default keyboard: [lindex $keyboardDevices 0] " -# puts " <<<<<< " - Claim the default keyboard is [lindex $keyboardDevices 0] - Claim $::thisNode has keyboards $keyboardDevices -# When /someone/ claims /node/ has keyboards /keyboards/ { -# puts "$node: has keyboard $keyboards" -# } -# Claim $::thisNode has keyboards [list "beep"] -# Claim the default keyboard is "beep" - # Need to modify getKeyPress (define it in here, even, maybe???) to listen to all keyboards hmmmm - # Maybe it should even return a keyboard originator in the return value? -} - -# Keeping this here for reference, do this in a loop for each keyboard that is Claimed as out :) -# TODO: DELETE THIS, move this logic into virtual-programs/keyboard.folk -while false { - package require Thread - proc errorproc {id errorInfo} {puts "Thread error in $id: $errorInfo"} - thread::errorproc errorproc - - try { - set keyboardThread [thread::create [format { - source "pi/KeyCodes.tcl" - source "lib/c.tcl" - source "pi/cUtils.tcl" - Keyboard::init - puts "Keyboard tid: [getTid]" - - set keyStates [list up down repeat] - set modifiers [dict create \ - shift 0 \ - ctrl 0 \ - alt 0 \ - ] - # TODO: DELETE THIS, move this logic into virtual-programs/keyboard.folk - while false { - lassign [Keyboard::getKeyEvent] keyCode eventType - - set shift [dict get $modifiers shift] - set key [keyFromCode $keyCode $shift] - set keyState [lindex $keyStates $eventType] - - set isDown [expr {$keyState != "up"}] - if {[string match *SHIFT $key]} { - dict set modifiers shift $isDown - } - if {[string match *CTRL $key]} { - dict set modifiers ctrl $isDown - } - if {[string match *ALT $key]} { - dict set modifiers alt $isDown - } - - set heldModifiers [dict keys [dict filter $modifiers value 1]] - - # Use `list` to escape special chars (brackets, quotes, whitespace) - thread::send -async "%s" [subst { - Retract keyboard claims key /k/ is /t/ with modifiers /m/ - Assert keyboard claims key [list $key] is [list $keyState] with modifiers [list $heldModifiers] - }] - } - } [thread::id]]] - puts "Keyboard thread id: $keyboardThread" - } on error error { - puts stderr "Keyboard thread failed: $error" - } } \ No newline at end of file -- cgit v1.2.3 From c4300b63dc0c9d9045d7241dc021b0d723b7e856 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Wed, 29 Nov 2023 16:56:16 -0500 Subject: Simplify keyboard logging --- virtual-programs/keyboard.folk | 86 +++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 47 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index edbef9d7..740b7b1c 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -81,77 +81,69 @@ foreach keyboard $keyboardDevices { Start process "keyboard-$eventPath" { source "pi/KeyCodes.tcl" Wish $::thisProcess shares statements like \ - [list keyboard /k/ claims /...anything/] + [list keyboard /k/ claims /k/ is /v/] variable evtBytes 16 variable evtFormat iissi if {[exec getconf LONG_BIT] == 64} { set evtBytes 24 set evtFormat wwssi } + Assert keyboard [rand] claims key A is "boop: [rand]" set keyboardSpecifier $eventPath variable eventPathChannel [open $eventPath r] puts "starting keyboard process for $keyboard" set readCount 0 while 1 { set readCount [incr $readCount] - puts "====\nreading ... (readCount: $readCount) ($eventPath) from ($eventPathChannel)" binary scan [read $eventPathChannel $evtBytes] $evtFormat tvSec tvUsec type code value if {$type > 0x04} { set eventPathChannel [open $eventPath r] binary scan [read $eventPathChannel $evtBytes] $evtFormat tvSec tvUsec type code value } - puts "from scan: (type | $type) (code | $code) $value" - if {$type == 0x01} { - puts "read: $tvSec $tvUsec $type ([expr $type == 0x01]) $code $value" + if {$type == 0x01 && $value == 1} { puts "got key event ($keyboardSpecifier): $code => [keyFromCode $code false] | $value" Retract keyboard $keyboardSpecifier key /k/ is /t/ Assert keyboard $keyboardSpecifier claims key [keyFromCode $code false] is [list $value] - Claim keyboard $keyboardSpecifier claims thing - # return [list $code $value] } } + # set kbEventPath [open $eventPath r] + # set keyStates [list up down repeat] + # set modifiers [dict create \ + # shift 0 \ + # ctrl 0 \ + # alt 0 \ + # ] + + # set shift [dict get $modifiers shift] + # set key [keyFromCode $keyCode $shift] + # set keyState [lindex $keyStates $eventType] + + # set isDown [expr {$keyState != "up"}] + # if {[string match *SHIFT $key]} { + # dict set modifiers shift $isDown + # } + # if {[string match *CTRL $key]} { + # dict set modifiers ctrl $isDown + # } + # if {[string match *ALT $key]} { + # dict set modifiers alt $isDown + # } - return - - set kbEventPath [open $eventPath r] - - set keyStates [list up down repeat] - set modifiers [dict create \ - shift 0 \ - ctrl 0 \ - alt 0 \ - ] - - set shift [dict get $modifiers shift] - set key [keyFromCode $keyCode $shift] - set keyState [lindex $keyStates $eventType] - - set isDown [expr {$keyState != "up"}] - if {[string match *SHIFT $key]} { - dict set modifiers shift $isDown - } - if {[string match *CTRL $key]} { - dict set modifiers ctrl $isDown - } - if {[string match *ALT $key]} { - dict set modifiers alt $isDown - } - - set heldModifiers [dict keys [dict filter $modifiers value 1]] + # set heldModifiers [dict keys [dict filter $modifiers value 1]] - while 1 { - binary scan [read $kbEventPath $evtBytes] $evtFormat tvSec tvUsec type code value - if {$type == 0x01} { - Retract keyboard $keyboardSpecifier claims key /k/ is /t/ with modifiers /m/ - Assert keyboard $keyboardSpecifier claims key [list $key] is [list $keyState] with modifiers [list $heldModifiers] - puts "got key event ($keyboardSpecifier): $key $keyState $heldModifiers" - # return [list $code $value] - } - } - # establishKeyPressListener $eventPath - # foreach devlink [dict get $keyboard devLinks] { - # puts " -- : $devlink" - # establishKeyPressListener $devlink + # while 1 { + # binary scan [read $kbEventPath $evtBytes] $evtFormat tvSec tvUsec type code value + # if {$type == 0x01} { + # Retract keyboard $keyboardSpecifier claims key /k/ is /t/ with modifiers /m/ + # Assert keyboard $keyboardSpecifier claims key [list $key] is [list $keyState] with modifiers [list $heldModifiers] + # puts "got key event ($keyboardSpecifier): $key $keyState $heldModifiers" + # # return [list $code $value] + # } # } + # # establishKeyPressListener $eventPath + # # foreach devlink [dict get $keyboard devLinks] { + # # puts " -- : $devlink" + # # establishKeyPressListener $devlink + # # } } } \ No newline at end of file -- cgit v1.2.3 From d8cdc19431551df214303446d3e1a7efc5d426f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Wed, 29 Nov 2023 17:09:45 -0500 Subject: Add Step, sharing works now --- virtual-programs/keyboard.folk | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index 740b7b1c..eb98f085 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -81,14 +81,13 @@ foreach keyboard $keyboardDevices { Start process "keyboard-$eventPath" { source "pi/KeyCodes.tcl" Wish $::thisProcess shares statements like \ - [list keyboard /k/ claims /k/ is /v/] + [list keyboard /k/ claims key /k/ is /v/] variable evtBytes 16 variable evtFormat iissi if {[exec getconf LONG_BIT] == 64} { set evtBytes 24 set evtFormat wwssi } - Assert keyboard [rand] claims key A is "boop: [rand]" set keyboardSpecifier $eventPath variable eventPathChannel [open $eventPath r] puts "starting keyboard process for $keyboard" @@ -104,6 +103,7 @@ foreach keyboard $keyboardDevices { puts "got key event ($keyboardSpecifier): $code => [keyFromCode $code false] | $value" Retract keyboard $keyboardSpecifier key /k/ is /t/ Assert keyboard $keyboardSpecifier claims key [keyFromCode $code false] is [list $value] + Step } } # set kbEventPath [open $eventPath r] -- cgit v1.2.3 From 0d1ede453b6ae61e11f3800953a5fbe687bbbc5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Thu, 30 Nov 2023 16:39:37 -0500 Subject: Add correct retract statement --- virtual-programs/keyboard.folk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'virtual-programs') diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index eb98f085..c39f9c05 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -101,7 +101,7 @@ foreach keyboard $keyboardDevices { } if {$type == 0x01 && $value == 1} { puts "got key event ($keyboardSpecifier): $code => [keyFromCode $code false] | $value" - Retract keyboard $keyboardSpecifier key /k/ is /t/ + Retract keyboard $keyboardSpecifier claims key /k/ is /t/ Assert keyboard $keyboardSpecifier claims key [keyFromCode $code false] is [list $value] Step } -- cgit v1.2.3 From 3e274206342cd85bf36c0d7c63780c3ad55b39a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Fri, 1 Dec 2023 17:21:38 -0500 Subject: Handle repeating keys --- virtual-programs/keyboard.folk | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index c39f9c05..955624f6 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -99,8 +99,10 @@ foreach keyboard $keyboardDevices { set eventPathChannel [open $eventPath r] binary scan [read $eventPathChannel $evtBytes] $evtFormat tvSec tvUsec type code value } - if {$type == 0x01 && $value == 1} { - puts "got key event ($keyboardSpecifier): $code => [keyFromCode $code false] | $value" + if {$type == 0x01 && ($value == 1) || ($value == 2)} { + set key [keyFromCode $code 0] + # puts "shift: [string match *SHIFT $key] | $key" + puts "got key event ($keyboardSpecifier): $code => $key | $value" Retract keyboard $keyboardSpecifier claims key /k/ is /t/ Assert keyboard $keyboardSpecifier claims key [keyFromCode $code false] is [list $value] Step -- cgit v1.2.3 From ee7dea9face4a782b54c033eb7d5bf22047a1680 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Mon, 4 Dec 2023 16:11:11 -0500 Subject: Remove readCount debugging --- virtual-programs/keyboard.folk | 3 --- 1 file changed, 3 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index 955624f6..8c2c85b3 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -90,10 +90,7 @@ foreach keyboard $keyboardDevices { } set keyboardSpecifier $eventPath variable eventPathChannel [open $eventPath r] - puts "starting keyboard process for $keyboard" - set readCount 0 while 1 { - set readCount [incr $readCount] binary scan [read $eventPathChannel $evtBytes] $evtFormat tvSec tvUsec type code value if {$type > 0x04} { set eventPathChannel [open $eventPath r] -- cgit v1.2.3 From acdc859ed816f71f2b99ed49e0d37112d17171e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Mon, 4 Dec 2023 16:55:18 -0500 Subject: Integrate modifiers, fix esc-restart --- virtual-programs/esc-restart.folk | 2 +- virtual-programs/keyboard.folk | 39 +++++++++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 7 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/esc-restart.folk b/virtual-programs/esc-restart.folk index ee8aa847..d94f2323 100644 --- a/virtual-programs/esc-restart.folk +++ b/virtual-programs/esc-restart.folk @@ -1,3 +1,3 @@ -When keyboard claims key ESC is down with modifiers alt { +When keyboard /k/ claims key ESC is down with modifiers alt { exec sudo systemctl restart folk } diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index 8c2c85b3..080eb679 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -81,7 +81,7 @@ foreach keyboard $keyboardDevices { Start process "keyboard-$eventPath" { source "pi/KeyCodes.tcl" Wish $::thisProcess shares statements like \ - [list keyboard /k/ claims key /k/ is /v/] + [list keyboard /kb/ claims key /k/ is /t/ with modifiers /m/] variable evtBytes 16 variable evtFormat iissi if {[exec getconf LONG_BIT] == 64} { @@ -90,18 +90,45 @@ foreach keyboard $keyboardDevices { } set keyboardSpecifier $eventPath variable eventPathChannel [open $eventPath r] + set keyStates [list up down repeat] + set modifiers [dict create \ + shift 0 \ + ctrl 0 \ + alt 0 \ + ] while 1 { binary scan [read $eventPathChannel $evtBytes] $evtFormat tvSec tvUsec type code value if {$type > 0x04} { set eventPathChannel [open $eventPath r] binary scan [read $eventPathChannel $evtBytes] $evtFormat tvSec tvUsec type code value } - if {$type == 0x01 && ($value == 1) || ($value == 2)} { - set key [keyFromCode $code 0] - # puts "shift: [string match *SHIFT $key] | $key" + if {$type == 0x01} { + set shift [dict get $modifiers shift] + set key [keyFromCode $code $shift] + set keyState [lindex $keyStates $value] + + # dict set modifiers shift 0 + # dict set modifiers alt 0 + # dict set modifiers ctrl 0 + + set isDown [expr {$keyState != "up"}] + if {[string match *SHIFT $key]} { + dict set modifiers shift $isDown + } + if {[string match *CTRL $key]} { + dict set modifiers ctrl $isDown + } + if {[string match *ALT $key]} { + dict set modifiers alt $isDown + } + + set heldModifiers [dict keys [dict filter $modifiers value 1]] + puts "shift: [string match *SHIFT $key] | $key" + puts "modifiers: $heldModifiers" + puts "got key event ($keyboardSpecifier): $code => $key | $value" - Retract keyboard $keyboardSpecifier claims key /k/ is /t/ - Assert keyboard $keyboardSpecifier claims key [keyFromCode $code false] is [list $value] + Retract keyboard $keyboardSpecifier claims key /k/ is /t/ with modifiers /m/ + Assert keyboard $keyboardSpecifier claims key [list $key] is [list $keyState] with modifiers [list $heldModifiers] Step } } -- cgit v1.2.3 From 3653fc15441e45437d6ebd066f0a672593dec8cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Tue, 5 Dec 2023 17:01:42 -0500 Subject: Add (non-differentiated) keypress logic to editor --- virtual-programs/editor.folk | 228 +++++++++++++++++++++-------------------- virtual-programs/keyboard.folk | 7 +- 2 files changed, 120 insertions(+), 115 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 7400a299..0e63cd85 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -250,131 +250,133 @@ proc getCurrentLineLength {lines cursor} { string length $currentLine } -Every time keyboard claims key /currentCharacter/ is down with modifiers /modifier/ & the cursor is /cursor/ & $id has program code /code/ { - Commit cursor { - switch $currentCharacter { - UP { - set updatedCursor [updateCursor $cursor {y -1}] - set currentLineLength [getCurrentLineLength $code $updatedCursor] - if {[lindex $updatedCursor 0] > $currentLineLength} { - Claim the cursor is [list [- $currentLineLength 1] [lindex $updatedCursor 1]] - } else { - Claim the cursor is $updatedCursor +When /page/ is a keyboard with path /kbPath/ { + Every time keyboard $kbPath claims key /currentCharacter/ is down with modifiers /modifier/ & the cursor is /cursor/ & $id has program code /code/ { + Commit cursor { + switch $currentCharacter { + UP { + set updatedCursor [updateCursor $cursor {y -1}] + set currentLineLength [getCurrentLineLength $code $updatedCursor] + if {[lindex $updatedCursor 0] > $currentLineLength} { + Claim the cursor is [list [- $currentLineLength 1] [lindex $updatedCursor 1]] + } else { + Claim the cursor is $updatedCursor + } } - } - DOWN { - set linecount [llength [split $code "\n"]] - set updatedCursor [updateCursor $cursor {y 1}] - set currentLineLength [getCurrentLineLength $code $updatedCursor] + DOWN { + set linecount [llength [split $code "\n"]] + set updatedCursor [updateCursor $cursor {y 1}] + set currentLineLength [getCurrentLineLength $code $updatedCursor] - if {[lindex $updatedCursor 1] == $linecount} { - Claim the cursor is $cursor - return - } elseif {[lindex $updatedCursor 0] > $currentLineLength} { - Claim the cursor is [list $currentLineLength [lindex $updatedCursor 1]] - } else { - Claim the cursor is $updatedCursor + if {[lindex $updatedCursor 1] == $linecount} { + Claim the cursor is $cursor + return + } elseif {[lindex $updatedCursor 0] > $currentLineLength} { + Claim the cursor is [list $currentLineLength [lindex $updatedCursor 1]] + } else { + Claim the cursor is $updatedCursor + } } - } - RIGHT { - set currentLineLength [getCurrentLineLength $code $cursor] - # TODO: Check if the cursor is at the end of the line, if so cap it - Claim the cursor is [updateCursor $cursor {x 1}] - } - LEFT { Claim the cursor is [updateCursor $cursor {x -1}] } - BACKSPACE { - # if cursor is at the beginning of the line, delete the newline - if {[x $cursor] == 0} { - set newCursor [updateCursor $cursor {y -1}] - set previousLineLength [getCurrentLineLength $code $newCursor] - set newCursor [list $previousLineLength [y $newCursor]] - Claim the cursor is $newCursor - } else { - Claim the cursor is [updateCursor $cursor {x -1}] + RIGHT { + set currentLineLength [getCurrentLineLength $code $cursor] + # TODO: Check if the cursor is at the end of the line, if so cap it + Claim the cursor is [updateCursor $cursor {x 1}] } - Commit code { Claim $id has program code [deleteCharacter $code $cursor] } - } - SPACE { - Claim the cursor is [updateCursor $cursor {x 1}] - Commit code { Claim $id has program code [insertCharacter $code " " $cursor] } - } - ENTER { - set updatedCursor [updateCursor $cursor {y 1}] - Claim the cursor is [list 0 [lindex $updatedCursor 1]] - Commit code { Claim $id has program code [insertNewline $code $cursor] } - } - DELETE - - INSERT - - MUTE - - VOLUMEUP - - VOLUMEDOWN - - ESC - - TAB - - CAPSLOCK - - LEFTSHIFT - - RIGHTSHIFT - - LEFTALT - - RIGHTALT - - LEFTCTRL - - RIGHTCTRL { - # TODO: Implement DELETE, operates like BACKSPACE, but in the opposite direction - # TODO: MUTE VOLUMEUP VOLUMEDOWN - # implement sound.folk that allows a system-wide - # volume setting to be adjusted. - # Perhaps `Wish $system volume is 0.5` or something - - Claim the cursor is $cursor - } - default { - if {$modifier == "ctrl" & $currentCharacter == "p"} { - # TODO: Save time instead and prevent printing the same code within the same ... second? - When $id has printed /lastPrintedCode/ { - if {$lastPrintedCode == $code} { - Claim the cursor is $cursor + LEFT { Claim the cursor is [updateCursor $cursor {x -1}] } + BACKSPACE { + # if cursor is at the beginning of the line, delete the newline + if {[x $cursor] == 0} { + set newCursor [updateCursor $cursor {y -1}] + set previousLineLength [getCurrentLineLength $code $newCursor] + set newCursor [list $previousLineLength [y $newCursor]] + Claim the cursor is $newCursor + } else { + Claim the cursor is [updateCursor $cursor {x -1}] + } + Commit code { Claim $id has program code [deleteCharacter $code $cursor] } + } + SPACE { + Claim the cursor is [updateCursor $cursor {x 1}] + Commit code { Claim $id has program code [insertCharacter $code " " $cursor] } + } + ENTER { + set updatedCursor [updateCursor $cursor {y 1}] + Claim the cursor is [list 0 [lindex $updatedCursor 1]] + Commit code { Claim $id has program code [insertNewline $code $cursor] } + } + DELETE - + INSERT - + MUTE - + VOLUMEUP - + VOLUMEDOWN - + ESC - + TAB - + CAPSLOCK - + LEFTSHIFT - + RIGHTSHIFT - + LEFTALT - + RIGHTALT - + LEFTCTRL - + RIGHTCTRL { + # TODO: Implement DELETE, operates like BACKSPACE, but in the opposite direction + # TODO: MUTE VOLUMEUP VOLUMEDOWN + # implement sound.folk that allows a system-wide + # volume setting to be adjusted. + # Perhaps `Wish $system volume is 0.5` or something + + Claim the cursor is $cursor + } + default { + if {$modifier == "ctrl" & $currentCharacter == "p"} { + # TODO: Save time instead and prevent printing the same code within the same ... second? + When $id has printed /lastPrintedCode/ { + if {$lastPrintedCode == $code} { + Claim the cursor is $cursor + if {$::debug_print} { + puts "Not printing the same code twice" + } + return + } + } + When /nobody/ has printed /lastPrintedCode/ { if {$::debug_print} { - puts "Not printing the same code twice" + puts "Printing $code" } + Commit print { Claim $id has printed $code} + Wish to print $code with job id [expr {rand()}] return } + Commit code { Claim $id has program code $code} + Claim the cursor is $cursor + return } - When /nobody/ has printed /lastPrintedCode/ { - if {$::debug_print} { - puts "Printing $code" - } - Commit print { Claim $id has printed $code} - Wish to print $code with job id [expr {rand()}] + if {$modifier == "ctrl" & $currentCharacter == "r"} { + Commit code { Claim $id has program code $baseCode } + Claim the cursor is [list 0 0] return } - Commit code { Claim $id has program code $code} - Claim the cursor is $cursor - return - } - if {$modifier == "ctrl" & $currentCharacter == "r"} { - Commit code { Claim $id has program code $baseCode } - Claim the cursor is [list 0 0] - return - } - if {$modifier == "ctrl" & $currentCharacter == "a"} { - Commit code { Claim $id has program code $code} - lassign $cursor x y - Claim the cursor is [list 0 $y] - return - } - if {$modifier == "ctrl" & $currentCharacter == "e"} { - Commit code { Claim $id has program code $code} - lassign $cursor x y - Claim the cursor is [list [getLineLength $code $cursor] $y] - return - } - if {$modifier == "ctrl" & $currentCharacter == "u"} { - # delete from cursor back to 0 and move cursor to 0 - Commit code { Claim $id has program code [deleteToBeginning $code $cursor]} - lassign $cursor x y - Claim the cursor is [list 0 $y] - return + if {$modifier == "ctrl" & $currentCharacter == "a"} { + Commit code { Claim $id has program code $code} + lassign $cursor x y + Claim the cursor is [list 0 $y] + return + } + if {$modifier == "ctrl" & $currentCharacter == "e"} { + Commit code { Claim $id has program code $code} + lassign $cursor x y + Claim the cursor is [list [getLineLength $code $cursor] $y] + return + } + if {$modifier == "ctrl" & $currentCharacter == "u"} { + # delete from cursor back to 0 and move cursor to 0 + Commit code { Claim $id has program code [deleteToBeginning $code $cursor]} + lassign $cursor x y + Claim the cursor is [list 0 $y] + return + } + Claim the cursor is [updateCursor $cursor {x 1}] + Commit code { Claim $id has program code [insertCharacter $code $currentCharacter $cursor] } } - Claim the cursor is [updateCursor $cursor {x 1}] - Commit code { Claim $id has program code [insertCharacter $code $currentCharacter $cursor] } } } } diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index 080eb679..c3b3db6b 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -47,7 +47,8 @@ proc isKeyboard {device} { # # This function goes through each of them and checks if they are keyboards proc walkInputEventPaths {} { - set allDevices [glob -nocomplain "/dev/input/event*"] + # set allDevices [glob -nocomplain "/dev/input/event*"] + set allDevices [glob -nocomplain "/dev/input/by-path/*"] set keyboards [list] foreach device $allDevices { set devLinks [getDEVLINKS $device] @@ -78,6 +79,7 @@ set keyboardDevices [walkInputEventPaths] # go through each keyboard device and start a process that foreach keyboard $keyboardDevices { set eventPath [dict get $keyboard eventPath] + Claim keyboards are $keyboardDevices Start process "keyboard-$eventPath" { source "pi/KeyCodes.tcl" Wish $::thisProcess shares statements like \ @@ -128,7 +130,8 @@ foreach keyboard $keyboardDevices { puts "got key event ($keyboardSpecifier): $code => $key | $value" Retract keyboard $keyboardSpecifier claims key /k/ is /t/ with modifiers /m/ - Assert keyboard $keyboardSpecifier claims key [list $key] is [list $keyState] with modifiers [list $heldModifiers] + # Assert keyboard $keyboardSpecifier claims key [list $key] is [list $keyState] with modifiers [list $heldModifiers] + Assert keyboard $keyboardSpecifier claims key $key is $keyState with modifiers $heldModifiers Step } } -- cgit v1.2.3 From 701a4f3d6d97e1eac6e85f03635f82637a19e6e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Wed, 6 Dec 2023 18:04:15 -0500 Subject: Simplify keyboards claim --- virtual-programs/keyboard.folk | 98 +++++------------------------------------- 1 file changed, 10 insertions(+), 88 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index c3b3db6b..c52f119c 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -4,26 +4,8 @@ proc udevadmProperties {device} { return [exec udevadm info --query=property --name=$device] } -proc getDEVLINKS {device} { - set properties [udevadmProperties $device] - if {$properties eq ""} { - return "" - } - set devlinks [list] - foreach line [split $properties \n] { - if {[string match "DEVLINKS=*" $line]} { - set devlinks [string replace $line 0 8] - foreach path [split $devlinks " "] { - lappend devlinks $path - } - } - } - - return $devlinks -} - -proc establishKeyPressListener {eventPath} { - set kb [open $eventPath r] +proc establishKeyPressListener {keyboard} { + set kb [open $keyboard r] fconfigure $kb -translation binary return $kb } @@ -51,36 +33,24 @@ proc walkInputEventPaths {} { set allDevices [glob -nocomplain "/dev/input/by-path/*"] set keyboards [list] foreach device $allDevices { - set devLinks [getDEVLINKS $device] - if {[llength $devLinks] > 0 && [isKeyboard $device]} { + if {[isKeyboard $device]} { if {[file readable $device] == 0} { puts "Device $device is not readable. Attempting to change permissions." # Attempt to change permissions so that the file can be read exec sudo chmod +r $device } - lappend keyboards [dict create eventPath $device devLinks $devLinks] + lappend keyboards $device } } return $keyboards } -proc findMatchingEventPath {searchList eventPath} { - foreach element $searchList { - set elEventPath [dict get $element eventPath] - if {$eventPath eq $elEventPath} { - return $element - } - } - return 0 -} - set keyboardDevices [walkInputEventPaths] # go through each keyboard device and start a process that foreach keyboard $keyboardDevices { - set eventPath [dict get $keyboard eventPath] Claim keyboards are $keyboardDevices - Start process "keyboard-$eventPath" { + Start process "keyboard-$keyboard" { source "pi/KeyCodes.tcl" Wish $::thisProcess shares statements like \ [list keyboard /kb/ claims key /k/ is /t/ with modifiers /m/] @@ -90,8 +60,8 @@ foreach keyboard $keyboardDevices { set evtBytes 24 set evtFormat wwssi } - set keyboardSpecifier $eventPath - variable eventPathChannel [open $eventPath r] + set keyboardSpecifier $keyboard + variable keyboardChannel [open $keyboard r] set keyStates [list up down repeat] set modifiers [dict create \ shift 0 \ @@ -99,20 +69,16 @@ foreach keyboard $keyboardDevices { alt 0 \ ] while 1 { - binary scan [read $eventPathChannel $evtBytes] $evtFormat tvSec tvUsec type code value + binary scan [read $keyboardChannel $evtBytes] $evtFormat tvSec tvUsec type code value if {$type > 0x04} { - set eventPathChannel [open $eventPath r] - binary scan [read $eventPathChannel $evtBytes] $evtFormat tvSec tvUsec type code value + set keyboardChannel [open $keyboard r] + binary scan [read $keyboardChannel $evtBytes] $evtFormat tvSec tvUsec type code value } if {$type == 0x01} { set shift [dict get $modifiers shift] set key [keyFromCode $code $shift] set keyState [lindex $keyStates $value] - # dict set modifiers shift 0 - # dict set modifiers alt 0 - # dict set modifiers ctrl 0 - set isDown [expr {$keyState != "up"}] if {[string match *SHIFT $key]} { dict set modifiers shift $isDown @@ -125,54 +91,10 @@ foreach keyboard $keyboardDevices { } set heldModifiers [dict keys [dict filter $modifiers value 1]] - puts "shift: [string match *SHIFT $key] | $key" - puts "modifiers: $heldModifiers" - - puts "got key event ($keyboardSpecifier): $code => $key | $value" Retract keyboard $keyboardSpecifier claims key /k/ is /t/ with modifiers /m/ - # Assert keyboard $keyboardSpecifier claims key [list $key] is [list $keyState] with modifiers [list $heldModifiers] Assert keyboard $keyboardSpecifier claims key $key is $keyState with modifiers $heldModifiers Step } } - # set kbEventPath [open $eventPath r] - # set keyStates [list up down repeat] - # set modifiers [dict create \ - # shift 0 \ - # ctrl 0 \ - # alt 0 \ - # ] - - # set shift [dict get $modifiers shift] - # set key [keyFromCode $keyCode $shift] - # set keyState [lindex $keyStates $eventType] - - # set isDown [expr {$keyState != "up"}] - # if {[string match *SHIFT $key]} { - # dict set modifiers shift $isDown - # } - # if {[string match *CTRL $key]} { - # dict set modifiers ctrl $isDown - # } - # if {[string match *ALT $key]} { - # dict set modifiers alt $isDown - # } - - # set heldModifiers [dict keys [dict filter $modifiers value 1]] - - # while 1 { - # binary scan [read $kbEventPath $evtBytes] $evtFormat tvSec tvUsec type code value - # if {$type == 0x01} { - # Retract keyboard $keyboardSpecifier claims key /k/ is /t/ with modifiers /m/ - # Assert keyboard $keyboardSpecifier claims key [list $key] is [list $keyState] with modifiers [list $heldModifiers] - # puts "got key event ($keyboardSpecifier): $key $keyState $heldModifiers" - # # return [list $code $value] - # } - # } - # # establishKeyPressListener $eventPath - # # foreach devlink [dict get $keyboard devLinks] { - # # puts " -- : $devlink" - # # establishKeyPressListener $devlink - # # } } } \ No newline at end of file -- cgit v1.2.3 From 842c842558745ba46b6be6b1518ae0b2758da005 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Wed, 6 Dec 2023 18:36:08 -0500 Subject: Move baseCode into initial When --- virtual-programs/editor.folk | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 0e63cd85..13aa7876 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -2,9 +2,14 @@ set id "editor-1" set ::debug_cursor false set ::debug_print false +# long base code +# set baseCode "Claim\nWish \$this is outlined green\nClaim this is a super long linelooooooooong line\nClaim the future is good\nClaim lorem ipsum long long long long long long long long long long long\nClaim bl000000000000000000000000000000_______oooooop\nClaim hello hello hello hello hello hello hello" +set baseCode "Wish \$this is outlined green" + When /page/ is a keyboard with path /kbPath/ & /page/ has region /r/ { - Wish $page is outlined gray - Claim $id has region [region move $r up 450px] + Wish $page is outlined gray + Claim $id has region [region move $r up 450px] + Commit code { Claim $id has program code $baseCode } } proc updateCursor {oldCursor updates} { @@ -113,11 +118,6 @@ proc newRegion {program x y w h} { Claim $program has region [region create $vertices $edges] } -set baseCode "Wish \$this is outlined green" -# long base code -# set baseCode "Claim\nWish \$this is outlined green\nClaim this is a super long linelooooooooong line\nClaim the future is good\nClaim lorem ipsum long long long long long long long long long long long\nClaim bl000000000000000000000000000000_______oooooop\nClaim hello hello hello hello hello hello hello" -Commit code { Claim $id has program code $baseCode } - proc drawText {text cursor count region} { set lineNumber 1 set showCursor [expr {$count % 2 == 0}] -- cgit v1.2.3 From 01a8e47497daeafe29a89e3cec2fe10eeca97b25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Thu, 7 Dec 2023 20:00:59 -0500 Subject: Update demo claim, link to guide again --- virtual-programs/editor.folk | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 13aa7876..5ecf7df6 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -383,5 +383,6 @@ When /page/ is a keyboard with path /kbPath/ { } Claim $this has demo { - Claim $this is keyboard with path /dev/input/by-path/platform-i8042-serio-0-event-kbd + # Find your keyboard path with the script in this guide: https://folk.computer/guides/keyboard + Claim $this is a keyboard with path /dev/input/by-path/pci-0000:02:00.0-usb-0:2:1.2-event-mouse } -- cgit v1.2.3 From 64c29796269a0e136979b28a4b7b85802c76362c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Fri, 8 Dec 2023 16:38:28 -0500 Subject: Make keyboard editors unique --- virtual-programs/editor.folk | 129 ++++++++++++++++++++++--------------------- 1 file changed, 66 insertions(+), 63 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 5ecf7df6..41629c6c 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -1,15 +1,12 @@ -set id "editor-1" set ::debug_cursor false set ::debug_print false - -# long base code -# set baseCode "Claim\nWish \$this is outlined green\nClaim this is a super long linelooooooooong line\nClaim the future is good\nClaim lorem ipsum long long long long long long long long long long long\nClaim bl000000000000000000000000000000_______oooooop\nClaim hello hello hello hello hello hello hello" set baseCode "Wish \$this is outlined green" When /page/ is a keyboard with path /kbPath/ & /page/ has region /r/ { + set id "$page$kbPath" Wish $page is outlined gray Claim $id has region [region move $r up 450px] - Commit code { Claim $id has program code $baseCode } + Commit "code$kbPath" { Claim $id has program code $baseCode } } proc updateCursor {oldCursor updates} { @@ -28,7 +25,9 @@ proc x {vector} { lindex $vector 0 } proc y {vector} { lindex $vector 1 } # TODO: Scope to "When the editor is out" or something -Commit cursor { Claim the cursor is [list 0 0]} +When /page/ is a keyboard with path /kbPath/ { + Commit "cursor$kbPath" { Claim the $kbPath cursor is [list 0 0]} +} proc insertCharacter {code newCharacter cursor} { set lines [split $code "\n"] @@ -212,35 +211,38 @@ proc drawCursor {cursorPosition lineShift region} { } } -When $id has program code /code/ & the clock time is /t/ & the cursor is /cursor/ & $id has region /r/ { - set intTime [expr {int($t * 10)}] - drawText [insertCursor $code $cursor $intTime] $cursor $intTime $r - - set lineShift [getLineShift $code] - drawCursor $cursor $lineShift $r - - if {$::debug_cursor} { - # draw lines for every line of text - set lineCount [llength [split $code "\n"]] - set horizontalLines [list] - set anchor [region centroid $r] - set angle [region angle $r] - - set vertical_spacer 20 - for {set i -10} { $i < 10 } {incr i} { - lappend verticalLines [add $anchor [list 0 [expr {$i * $vertical_spacer}]]] - lappend verticalLines [add $anchor [list 100 [expr {$i * $vertical_spacer}]]] - lappend verticalLines [add $anchor [list $vertical_spacer [expr {$i * $vertical_spacer}]]] - } +When /page/ is a keyboard with path /kbPath/ { + set id "$page$kbPath" + When $id has program code /code/ & the clock time is /t/ & the $kbPath cursor is /cursor/ & $id has region /r/ { + set intTime [expr {int($t * 10)}] + drawText [insertCursor $code $cursor $intTime] $cursor $intTime $r + + set lineShift [getLineShift $code] + drawCursor $cursor $lineShift $r + + if {$::debug_cursor} { + # draw lines for every line of text + set lineCount [llength [split $code "\n"]] + set horizontalLines [list] + set anchor [region centroid $r] + set angle [region angle $r] + + set vertical_spacer 20 + for {set i -10} { $i < 10 } {incr i} { + lappend verticalLines [add $anchor [list 0 [expr {$i * $vertical_spacer}]]] + lappend verticalLines [add $anchor [list 100 [expr {$i * $vertical_spacer}]]] + lappend verticalLines [add $anchor [list $vertical_spacer [expr {$i * $vertical_spacer}]]] + } - set horizontal_spacer 14 - for {set i -10} { $i < 10 } {incr i} { - lappend horizontalLines [add $anchor [list [expr {$i * $horizontal_spacer}] 0]] - lappend horizontalLines [add $anchor [list [expr {$i * $horizontal_spacer}] 100 ]] - lappend horizontalLines [add $anchor [list [expr {$i * $horizontal_spacer}] $horizontal_spacer]] + set horizontal_spacer 14 + for {set i -10} { $i < 10 } {incr i} { + lappend horizontalLines [add $anchor [list [expr {$i * $horizontal_spacer}] 0]] + lappend horizontalLines [add $anchor [list [expr {$i * $horizontal_spacer}] 100 ]] + lappend horizontalLines [add $anchor [list [expr {$i * $horizontal_spacer}] $horizontal_spacer]] + } + Display::stroke [rotateStroke $verticalLines $anchor $angle] 2 green + Display::stroke [rotateStroke $horizontalLines $anchor $angle] 2 white } - Display::stroke [rotateStroke $verticalLines $anchor $angle] 2 green - Display::stroke [rotateStroke $horizontalLines $anchor $angle] 2 white } } @@ -251,16 +253,17 @@ proc getCurrentLineLength {lines cursor} { } When /page/ is a keyboard with path /kbPath/ { - Every time keyboard $kbPath claims key /currentCharacter/ is down with modifiers /modifier/ & the cursor is /cursor/ & $id has program code /code/ { - Commit cursor { + set id "$page$kbPath" + Every time keyboard $kbPath claims key /currentCharacter/ is down with modifiers /modifier/ & the $kbPath cursor is /cursor/ & $id has program code /code/ { + Commit "cursor$kbPath" { switch $currentCharacter { UP { set updatedCursor [updateCursor $cursor {y -1}] set currentLineLength [getCurrentLineLength $code $updatedCursor] if {[lindex $updatedCursor 0] > $currentLineLength} { - Claim the cursor is [list [- $currentLineLength 1] [lindex $updatedCursor 1]] + Claim the $kbPath cursor is [list [- $currentLineLength 1] [lindex $updatedCursor 1]] } else { - Claim the cursor is $updatedCursor + Claim the $kbPath cursor is $updatedCursor } } DOWN { @@ -269,40 +272,40 @@ When /page/ is a keyboard with path /kbPath/ { set currentLineLength [getCurrentLineLength $code $updatedCursor] if {[lindex $updatedCursor 1] == $linecount} { - Claim the cursor is $cursor + Claim the $kbPath cursor is $cursor return } elseif {[lindex $updatedCursor 0] > $currentLineLength} { - Claim the cursor is [list $currentLineLength [lindex $updatedCursor 1]] + Claim the $kbPath cursor is [list $currentLineLength [lindex $updatedCursor 1]] } else { - Claim the cursor is $updatedCursor + Claim the $kbPath cursor is $updatedCursor } } RIGHT { set currentLineLength [getCurrentLineLength $code $cursor] # TODO: Check if the cursor is at the end of the line, if so cap it - Claim the cursor is [updateCursor $cursor {x 1}] + Claim the $kbPath cursor is [updateCursor $cursor {x 1}] } - LEFT { Claim the cursor is [updateCursor $cursor {x -1}] } + LEFT { Claim the $kbPath cursor is [updateCursor $cursor {x -1}] } BACKSPACE { # if cursor is at the beginning of the line, delete the newline if {[x $cursor] == 0} { set newCursor [updateCursor $cursor {y -1}] set previousLineLength [getCurrentLineLength $code $newCursor] set newCursor [list $previousLineLength [y $newCursor]] - Claim the cursor is $newCursor + Claim the $kbPath cursor is $newCursor } else { - Claim the cursor is [updateCursor $cursor {x -1}] + Claim the $kbPath cursor is [updateCursor $cursor {x -1}] } - Commit code { Claim $id has program code [deleteCharacter $code $cursor] } + Commit "code$kbPath" { Claim $id has program code [deleteCharacter $code $cursor] } } SPACE { - Claim the cursor is [updateCursor $cursor {x 1}] - Commit code { Claim $id has program code [insertCharacter $code " " $cursor] } + Claim the $kbPath cursor is [updateCursor $cursor {x 1}] + Commit "code$kbPath" { Claim $id has program code [insertCharacter $code " " $cursor] } } ENTER { set updatedCursor [updateCursor $cursor {y 1}] - Claim the cursor is [list 0 [lindex $updatedCursor 1]] - Commit code { Claim $id has program code [insertNewline $code $cursor] } + Claim the $kbPath cursor is [list 0 [lindex $updatedCursor 1]] + Commit "code$kbPath" { Claim $id has program code [insertNewline $code $cursor] } } DELETE - INSERT - @@ -324,14 +327,14 @@ When /page/ is a keyboard with path /kbPath/ { # volume setting to be adjusted. # Perhaps `Wish $system volume is 0.5` or something - Claim the cursor is $cursor + Claim the $kbPath cursor is $cursor } default { if {$modifier == "ctrl" & $currentCharacter == "p"} { # TODO: Save time instead and prevent printing the same code within the same ... second? When $id has printed /lastPrintedCode/ { if {$lastPrintedCode == $code} { - Claim the cursor is $cursor + Claim the $kbPath cursor is $cursor if {$::debug_print} { puts "Not printing the same code twice" } @@ -346,36 +349,36 @@ When /page/ is a keyboard with path /kbPath/ { Wish to print $code with job id [expr {rand()}] return } - Commit code { Claim $id has program code $code} - Claim the cursor is $cursor + Commit "code$kbPath" { Claim $id has program code $code} + Claim the $kbPath cursor is $cursor return } if {$modifier == "ctrl" & $currentCharacter == "r"} { - Commit code { Claim $id has program code $baseCode } - Claim the cursor is [list 0 0] + Commit "code$kbPath" { Claim $id has program code $baseCode } + Claim the $kbPath cursor is [list 0 0] return } if {$modifier == "ctrl" & $currentCharacter == "a"} { - Commit code { Claim $id has program code $code} + Commit "code$kbPath" { Claim $id has program code $code} lassign $cursor x y - Claim the cursor is [list 0 $y] + Claim the $kbPath cursor is [list 0 $y] return } if {$modifier == "ctrl" & $currentCharacter == "e"} { - Commit code { Claim $id has program code $code} + Commit "code$kbPath" { Claim $id has program code $code} lassign $cursor x y - Claim the cursor is [list [getLineLength $code $cursor] $y] + Claim the $kbPath cursor is [list [getLineLength $code $cursor] $y] return } if {$modifier == "ctrl" & $currentCharacter == "u"} { # delete from cursor back to 0 and move cursor to 0 - Commit code { Claim $id has program code [deleteToBeginning $code $cursor]} + Commit "code$kbPath" { Claim $id has program code [deleteToBeginning $code $cursor]} lassign $cursor x y - Claim the cursor is [list 0 $y] + Claim the $kbPath cursor is [list 0 $y] return } - Claim the cursor is [updateCursor $cursor {x 1}] - Commit code { Claim $id has program code [insertCharacter $code $currentCharacter $cursor] } + Claim the $kbPath cursor is [updateCursor $cursor {x 1}] + Commit "code$kbPath" { Claim $id has program code [insertCharacter $code $currentCharacter $cursor] } } } } -- cgit v1.2.3 From a1b69f30b1b27b63f2873af5dd2b5d3019e2b799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Mon, 11 Dec 2023 16:12:59 -0500 Subject: Fix baseCode overwriting --- virtual-programs/editor.folk | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 41629c6c..47e19c56 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -6,7 +6,11 @@ When /page/ is a keyboard with path /kbPath/ & /page/ has region /r/ { set id "$page$kbPath" Wish $page is outlined gray Claim $id has region [region move $r up 450px] - Commit "code$kbPath" { Claim $id has program code $baseCode } + When /nobody/ claims $id has program code /c/ { + Commit "code$kbPath" { + Claim $id has program code $baseCode + } + } } proc updateCursor {oldCursor updates} { -- cgit v1.2.3 From 5e25ea6b9e27dfe1c0bd96bb701ab01cf2c5b969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Tue, 12 Dec 2023 16:35:57 -0500 Subject: Add precise cursor and smaller editor text --- virtual-programs/editor.folk | 96 +++++++++++++------------------------------- 1 file changed, 29 insertions(+), 67 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 47e19c56..d1616d65 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -121,13 +121,6 @@ proc newRegion {program x y w h} { Claim $program has region [region create $vertices $edges] } -proc drawText {text cursor count region} { - set lineNumber 1 - set showCursor [expr {$count % 2 == 0}] - # TODO: Make the font size change relative to `Wish $this is small|medium|large editor 1` (by default should use small, e.g. fontSize 1) - Display::text {*}[region centroid $region] 1 $text [region angle $region] NeomatrixCode -} - proc rotateStroke {points center angle} { lmap v $points { set v [vec2 sub $v $center] @@ -159,70 +152,39 @@ proc debug {position color} { Display::circle {*}$position 5 2 $color true } -proc drawCursor {cursorPosition lineShift region} { - set anchor [region centroid $region] - set angle [region angle $region] - - if {$::debug_cursor} { - debug $anchor green - } - - set cursorHeight 20 - set cursorWidth 9 - set shiftMultiplier [list 12 20] - # set shiftMultiplier [list 14 25] - # set shiftMultiplier [list 14 20] - set cursorScale [list 14 25] ;# OLD - # projectorToCamera???? - - set lineShiftHalf [list [lindex $lineShift 0] 0] - set multipliedShift [vec2::mult $shiftMultiplier $lineShift] - # set adjustedShift [sub $multipliedShift $lineShiftHalf] - set adjustedShift $multipliedShift - set shiftedAnchor [sub $anchor $adjustedShift] - if {$::debug_cursor} { - debug [lindex [rotateStroke [list $shiftedAnchor] $anchor $angle] 0] white - } - # add (x * width) to the shiftedAnchor position from the beginning of the line ... - set scaledCursor [vec2::mult $cursorScale $cursorPosition] - - set finalCursor [add $shiftedAnchor $scaledCursor] - set rotatedFenalCursor [lindex [rotateStroke [list $finalCursor] $anchor $angle] 0] - - if {$::debug_cursor} { - # This is where the cursor point is relative to. - # cursor, green - Display::stroke [rotateStroke \ - [list $finalCursor \ - [add $finalCursor [list 0 $cursorHeight]] \ - ] \ - $anchor $angle] 2 green - # vertical reticle line - Display::stroke [rotateStroke \ - [list $finalCursor \ - [add $finalCursor [list 0 25]] \ - [add $finalCursor [list 0 -25]] \ - ] \ - $anchor $angle] 2 green - - # horizontal reticle line - Display::stroke [rotateStroke \ - [list $finalCursor \ - [add $finalCursor [list 25 0]] \ - [add $finalCursor [list -25 0]] \ - ] \ - $anchor $angle] 2 green - } -} - When /page/ is a keyboard with path /kbPath/ { set id "$page$kbPath" When $id has program code /code/ & the clock time is /t/ & the $kbPath cursor is /cursor/ & $id has region /r/ { set intTime [expr {int($t * 10)}] - drawText [insertCursor $code $cursor $intTime] $cursor $intTime $r - - set lineShift [getLineShift $code] - drawCursor $cursor $lineShift $r + set scale 0.60 + + lassign [region topleft $r] xstart ystart + set em [expr {$scale * 25}] + # From NeomatrixCode.csv + set advance [expr {0.5859375 * $em}] + set margin [expr {$advance * 3 + 10}] + + set p [region topleft [region move $r right ${margin}px down 10px]] + set lp [region topleft [region move $r right 5px down 10px]] + set height [expr {[region height $r] - 25}] + set width [expr {[region width $r] - ($margin + 20)}] + set radians [region angle $r] + + set curs [vec2 scale $cursor $advance $em] + + set x1 [vec2 sub $p $curs] + set x2 [vec2 sub $x1 [list 0 [expr {$em + 4}]]] + + set theta [expr {$radians + 3.14159}] + set x1 [vec2 add [vec2 rotate [vec2 sub $x1 $p] $theta] $p] + set x2 [vec2 add [vec2 rotate [vec2 sub $x2 $p] $theta] $p] + set s [expr {$scale * 4}] + + # Draw text + Wish to draw text with position $p text $code scale $scale anchor topleft radians [region angle $r] font NeomatrixCode + # Draw cursor + Wish to draw a circle with center $x1 radius $s thickness 0 color green filled true + Wish to draw a stroke with points [list $x1 $x2] width $s color green if {$::debug_cursor} { # draw lines for every line of text -- cgit v1.2.3 From 7ee115fac249d0c999caa13e92aaefc0e48a507b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Tue, 12 Dec 2023 16:39:11 -0500 Subject: Bounds check right arrow key --- virtual-programs/editor.folk | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index d1616d65..65d1df2c 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -248,8 +248,11 @@ When /page/ is a keyboard with path /kbPath/ { } RIGHT { set currentLineLength [getCurrentLineLength $code $cursor] - # TODO: Check if the cursor is at the end of the line, if so cap it - Claim the $kbPath cursor is [updateCursor $cursor {x 1}] + if {[lindex $cursor 0] == $currentLineLength} { + Claim the $kbPath cursor is $cursor + } else { + Claim the $kbPath cursor is [updateCursor $cursor {x 1}] + } } LEFT { Claim the $kbPath cursor is [updateCursor $cursor {x -1}] } BACKSPACE { -- cgit v1.2.3 From 688c3b6f8706d3dd8c05bfa6800d9811661a4a13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Tue, 12 Dec 2023 16:48:51 -0500 Subject: Remove debug_cursor --- virtual-programs/editor.folk | 25 ------------------------- 1 file changed, 25 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 65d1df2c..903be4ed 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -1,4 +1,3 @@ -set ::debug_cursor false set ::debug_print false set baseCode "Wish \$this is outlined green" @@ -185,30 +184,6 @@ When /page/ is a keyboard with path /kbPath/ { # Draw cursor Wish to draw a circle with center $x1 radius $s thickness 0 color green filled true Wish to draw a stroke with points [list $x1 $x2] width $s color green - - if {$::debug_cursor} { - # draw lines for every line of text - set lineCount [llength [split $code "\n"]] - set horizontalLines [list] - set anchor [region centroid $r] - set angle [region angle $r] - - set vertical_spacer 20 - for {set i -10} { $i < 10 } {incr i} { - lappend verticalLines [add $anchor [list 0 [expr {$i * $vertical_spacer}]]] - lappend verticalLines [add $anchor [list 100 [expr {$i * $vertical_spacer}]]] - lappend verticalLines [add $anchor [list $vertical_spacer [expr {$i * $vertical_spacer}]]] - } - - set horizontal_spacer 14 - for {set i -10} { $i < 10 } {incr i} { - lappend horizontalLines [add $anchor [list [expr {$i * $horizontal_spacer}] 0]] - lappend horizontalLines [add $anchor [list [expr {$i * $horizontal_spacer}] 100 ]] - lappend horizontalLines [add $anchor [list [expr {$i * $horizontal_spacer}] $horizontal_spacer]] - } - Display::stroke [rotateStroke $verticalLines $anchor $angle] 2 green - Display::stroke [rotateStroke $horizontalLines $anchor $angle] 2 white - } } } -- cgit v1.2.3 From bd0b136ba4fbee0bcc03d92f0cdfd05ed27c067f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Tue, 12 Dec 2023 17:08:58 -0500 Subject: Add linenumbers --- virtual-programs/editor.folk | 77 +++++++++++++------------------------------- 1 file changed, 23 insertions(+), 54 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 903be4ed..796bd77e 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -15,10 +15,10 @@ When /page/ is a keyboard with path /kbPath/ & /page/ has region /r/ { proc updateCursor {oldCursor updates} { set newCursor $oldCursor if {[dict exists $updates x]} { - lset newCursor 0 [expr {max(0, [dict get $updates x] + [lindex $oldCursor 0])}] + lset newCursor 0 [expr {max(0, [dict get $updates x] + [x $oldCursor])}] } if {[dict exists $updates y]} { - lset newCursor 1 [expr {max(0, [dict get $updates y] + [lindex $oldCursor 1])}] + lset newCursor 1 [expr {max(0, [dict get $updates y] + [y $oldCursor])}] } return $newCursor } @@ -49,15 +49,6 @@ proc insertCharacter {code newCharacter cursor} { } } -proc swapCharacter {code newCharacter cursor} { - set lines [split $code "\n"] - lassign $cursor x y - set line [lindex $lines $y] - set line [string replace $line $x $x $newCharacter] - lset lines $y $line - return [join $lines "\n"] -} - proc deleteCharacter {code cursor} { set lines [split $code "\n"] lassign $cursor x y @@ -91,7 +82,6 @@ proc deleteToBeginning {code cursor} { return [join $lines "\n"] } - proc insertNewline {code cursor} { set lines [split $code "\n"] lassign $cursor x y @@ -103,32 +93,6 @@ proc insertNewline {code cursor} { return [join $lines "\n"] } -proc insertCursor {code cursor count} { - if {$count % 2 == 0} { - return [swapCharacter $code "_" $cursor] - } else { - return $code - } -} - -proc newRegion {program x y w h} { - set vertices [list [list [expr {$x+$w}] $y] \ - [list $x $y] \ - [list $x [expr {$y+$h}]] \ - [list [expr {$x+$w}] [expr {$y+$h}]]] - set edges [list [list 0 1] [list 1 2] [list 2 3] [list 3 0]] - Claim $program has region [region create $vertices $edges] -} - -proc rotateStroke {points center angle} { - lmap v $points { - set v [vec2 sub $v $center] - set v [vec2 rotate $v $angle] - set v [vec2 add $v $center] - set v - } -} - proc getLineLength {code cursor} { set lines [split $code "\n"] set line [lindex $lines [lindex $cursor 1]] @@ -136,15 +100,13 @@ proc getLineLength {code cursor} { return $ll } -proc getLineShift {lines} { - set longestLineLength 0 - foreach line [split $lines "\n"] { - set lineLength [string length $line] - if {$lineLength > $longestLineLength} { - set longestLineLength $lineLength - } +proc lineNumberView {ystart linecount} { + set yend [expr {$ystart + $linecount}] + set numbers [list] + for {set i [expr {$ystart + 1}]} {$i <= $yend} {incr i} { + lappend numbers $i } - return [list [expr {$longestLineLength / 2}] [expr {[llength $lines] / 2}]] + join $numbers "\n" } proc debug {position color} { @@ -181,6 +143,13 @@ When /page/ is a keyboard with path /kbPath/ { # Draw text Wish to draw text with position $p text $code scale $scale anchor topleft radians [region angle $r] font NeomatrixCode + + # Draw line numbers + # set linecount [expr {int(floor($height / $em))}] + set linecount [llength [split $code "\n"]] + set linenumbers [lineNumberView 0 $linecount] + Wish to draw text with position $lp text $linenumbers scale $scale anchor topleft radians $radians font NeomatrixCode + # Draw cursor Wish to draw a circle with center $x1 radius $s thickness 0 color green filled true Wish to draw a stroke with points [list $x1 $x2] width $s color green @@ -189,7 +158,7 @@ When /page/ is a keyboard with path /kbPath/ { proc getCurrentLineLength {lines cursor} { set splitLines [split $lines "\n"] - set currentLine [lindex $splitLines [lindex $cursor 1]] + set currentLine [lindex $splitLines [y $cursor]] string length $currentLine } @@ -201,8 +170,8 @@ When /page/ is a keyboard with path /kbPath/ { UP { set updatedCursor [updateCursor $cursor {y -1}] set currentLineLength [getCurrentLineLength $code $updatedCursor] - if {[lindex $updatedCursor 0] > $currentLineLength} { - Claim the $kbPath cursor is [list [- $currentLineLength 1] [lindex $updatedCursor 1]] + if {[x $updatedCursor] > $currentLineLength} { + Claim the $kbPath cursor is [list [- $currentLineLength 1] [y $updatedCursor]] } else { Claim the $kbPath cursor is $updatedCursor } @@ -212,18 +181,18 @@ When /page/ is a keyboard with path /kbPath/ { set updatedCursor [updateCursor $cursor {y 1}] set currentLineLength [getCurrentLineLength $code $updatedCursor] - if {[lindex $updatedCursor 1] == $linecount} { + if {[y $updatedCursor] == $linecount} { Claim the $kbPath cursor is $cursor return - } elseif {[lindex $updatedCursor 0] > $currentLineLength} { - Claim the $kbPath cursor is [list $currentLineLength [lindex $updatedCursor 1]] + } elseif {[x $updatedCursor] > $currentLineLength} { + Claim the $kbPath cursor is [list $currentLineLength [y $updatedCursor]] } else { Claim the $kbPath cursor is $updatedCursor } } RIGHT { set currentLineLength [getCurrentLineLength $code $cursor] - if {[lindex $cursor 0] == $currentLineLength} { + if {[x $cursor] == $currentLineLength} { Claim the $kbPath cursor is $cursor } else { Claim the $kbPath cursor is [updateCursor $cursor {x 1}] @@ -248,7 +217,7 @@ When /page/ is a keyboard with path /kbPath/ { } ENTER { set updatedCursor [updateCursor $cursor {y 1}] - Claim the $kbPath cursor is [list 0 [lindex $updatedCursor 1]] + Claim the $kbPath cursor is [list 0 [y $updatedCursor]] Commit "code$kbPath" { Claim $id has program code [insertNewline $code $cursor] } } DELETE - -- cgit v1.2.3 From 77f8ac7295d62c2275610df531902af4856cb2af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Wed, 13 Dec 2023 15:48:24 -0500 Subject: Add editor qualifier to editor.folk --- virtual-programs/editor.folk | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 796bd77e..38667057 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -1,7 +1,7 @@ set ::debug_print false set baseCode "Wish \$this is outlined green" -When /page/ is a keyboard with path /kbPath/ & /page/ has region /r/ { +When /page/ is a keyboard with path /kbPath/ & /page/ is an editor & /page/ has region /r/ { set id "$page$kbPath" Wish $page is outlined gray Claim $id has region [region move $r up 450px] @@ -27,8 +27,7 @@ proc updateCursor {oldCursor updates} { proc x {vector} { lindex $vector 0 } proc y {vector} { lindex $vector 1 } -# TODO: Scope to "When the editor is out" or something -When /page/ is a keyboard with path /kbPath/ { +When /page/ is a keyboard with path /kbPath/ & /page/ is an editor { Commit "cursor$kbPath" { Claim the $kbPath cursor is [list 0 0]} } @@ -113,7 +112,7 @@ proc debug {position color} { Display::circle {*}$position 5 2 $color true } -When /page/ is a keyboard with path /kbPath/ { +When /page/ is a keyboard with path /kbPath/ & /page/ is an editor { set id "$page$kbPath" When $id has program code /code/ & the clock time is /t/ & the $kbPath cursor is /cursor/ & $id has region /r/ { set intTime [expr {int($t * 10)}] @@ -162,7 +161,7 @@ proc getCurrentLineLength {lines cursor} { string length $currentLine } -When /page/ is a keyboard with path /kbPath/ { +When /page/ is a keyboard with path /kbPath/ & /page/ is an editor { set id "$page$kbPath" Every time keyboard $kbPath claims key /currentCharacter/ is down with modifiers /modifier/ & the $kbPath cursor is /cursor/ & $id has program code /code/ { Commit "cursor$kbPath" { @@ -301,4 +300,5 @@ When /page/ is a keyboard with path /kbPath/ { Claim $this has demo { # Find your keyboard path with the script in this guide: https://folk.computer/guides/keyboard Claim $this is a keyboard with path /dev/input/by-path/pci-0000:02:00.0-usb-0:2:1.2-event-mouse + Claim $this is an editor } -- cgit v1.2.3 From c7626ab755397e3bfae598480c61cb0553125070 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Fri, 15 Dec 2023 16:22:59 -0500 Subject: Separate editing view from region --- virtual-programs/editor.folk | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 38667057..5744e8c9 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -3,8 +3,7 @@ set baseCode "Wish \$this is outlined green" When /page/ is a keyboard with path /kbPath/ & /page/ is an editor & /page/ has region /r/ { set id "$page$kbPath" - Wish $page is outlined gray - Claim $id has region [region move $r up 450px] + Claim $id has region [region move $r up 900px] When /nobody/ claims $id has program code /c/ { Commit "code$kbPath" { Claim $id has program code $baseCode @@ -118,17 +117,20 @@ When /page/ is a keyboard with path /kbPath/ & /page/ is an editor { set intTime [expr {int($t * 10)}] set scale 0.60 + set relativeRegion [region move $r down 450px] + Claim $id' has region $relativeRegion + Wish $id' is outlined white lassign [region topleft $r] xstart ystart set em [expr {$scale * 25}] # From NeomatrixCode.csv set advance [expr {0.5859375 * $em}] set margin [expr {$advance * 3 + 10}] - set p [region topleft [region move $r right ${margin}px down 10px]] - set lp [region topleft [region move $r right 5px down 10px]] - set height [expr {[region height $r] - 25}] - set width [expr {[region width $r] - ($margin + 20)}] - set radians [region angle $r] + set p [region topleft [region move $relativeRegion right ${margin}px down 10px]] + set lp [region topleft [region move $relativeRegion right 5px down 10px]] + set height [expr {[region height $relativeRegion] - 25}] + set width [expr {[region width $relativeRegion] - ($margin + 20)}] + set radians [region angle $relativeRegion] set curs [vec2 scale $cursor $advance $em] @@ -141,7 +143,7 @@ When /page/ is a keyboard with path /kbPath/ & /page/ is an editor { set s [expr {$scale * 4}] # Draw text - Wish to draw text with position $p text $code scale $scale anchor topleft radians [region angle $r] font NeomatrixCode + Wish to draw text with position $p text $code scale $scale anchor topleft radians [region angle $relativeRegion] font NeomatrixCode # Draw line numbers # set linecount [expr {int(floor($height / $em))}] -- cgit v1.2.3 From e5911a7a725e4d7b09539f0cacb51575d9f9401d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Fri, 15 Dec 2023 16:57:18 -0500 Subject: Add editor saving state --- virtual-programs/editor.folk | 73 ++++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 19 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 5744e8c9..58a0678a 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -7,6 +7,7 @@ When /page/ is a keyboard with path /kbPath/ & /page/ is an editor & /page/ has When /nobody/ claims $id has program code /c/ { Commit "code$kbPath" { Claim $id has program code $baseCode + Claim $id has editor code $baseCode } } } @@ -113,7 +114,7 @@ proc debug {position color} { When /page/ is a keyboard with path /kbPath/ & /page/ is an editor { set id "$page$kbPath" - When $id has program code /code/ & the clock time is /t/ & the $kbPath cursor is /cursor/ & $id has region /r/ { + When $id has program code /code/ & $id has editor code /editorCode/ & the clock time is /t/ & the $kbPath cursor is /cursor/ & $id has region /r/ { set intTime [expr {int($t * 10)}] set scale 0.60 @@ -143,11 +144,10 @@ When /page/ is a keyboard with path /kbPath/ & /page/ is an editor { set s [expr {$scale * 4}] # Draw text - Wish to draw text with position $p text $code scale $scale anchor topleft radians [region angle $relativeRegion] font NeomatrixCode + Wish to draw text with position $p text $editorCode scale $scale anchor topleft radians [region angle $relativeRegion] font NeomatrixCode # Draw line numbers - # set linecount [expr {int(floor($height / $em))}] - set linecount [llength [split $code "\n"]] + set linecount [llength [split $editorCode "\n"]] set linenumbers [lineNumberView 0 $linecount] Wish to draw text with position $lp text $linenumbers scale $scale anchor topleft radians $radians font NeomatrixCode @@ -165,12 +165,12 @@ proc getCurrentLineLength {lines cursor} { When /page/ is a keyboard with path /kbPath/ & /page/ is an editor { set id "$page$kbPath" - Every time keyboard $kbPath claims key /currentCharacter/ is down with modifiers /modifier/ & the $kbPath cursor is /cursor/ & $id has program code /code/ { + Every time keyboard $kbPath claims key /currentCharacter/ is down with modifiers /modifier/ & the $kbPath cursor is /cursor/ & $id has program code /code/ & $id has editor code /editorCode/ { Commit "cursor$kbPath" { switch $currentCharacter { UP { set updatedCursor [updateCursor $cursor {y -1}] - set currentLineLength [getCurrentLineLength $code $updatedCursor] + set currentLineLength [getCurrentLineLength $editorCode $updatedCursor] if {[x $updatedCursor] > $currentLineLength} { Claim the $kbPath cursor is [list [- $currentLineLength 1] [y $updatedCursor]] } else { @@ -178,9 +178,9 @@ When /page/ is a keyboard with path /kbPath/ & /page/ is an editor { } } DOWN { - set linecount [llength [split $code "\n"]] + set linecount [llength [split $editorCode "\n"]] set updatedCursor [updateCursor $cursor {y 1}] - set currentLineLength [getCurrentLineLength $code $updatedCursor] + set currentLineLength [getCurrentLineLength $editorCode $updatedCursor] if {[y $updatedCursor] == $linecount} { Claim the $kbPath cursor is $cursor @@ -192,7 +192,7 @@ When /page/ is a keyboard with path /kbPath/ & /page/ is an editor { } } RIGHT { - set currentLineLength [getCurrentLineLength $code $cursor] + set currentLineLength [getCurrentLineLength $editorCode $cursor] if {[x $cursor] == $currentLineLength} { Claim the $kbPath cursor is $cursor } else { @@ -204,22 +204,31 @@ When /page/ is a keyboard with path /kbPath/ & /page/ is an editor { # if cursor is at the beginning of the line, delete the newline if {[x $cursor] == 0} { set newCursor [updateCursor $cursor {y -1}] - set previousLineLength [getCurrentLineLength $code $newCursor] + set previousLineLength [getCurrentLineLength $editorCode $newCursor] set newCursor [list $previousLineLength [y $newCursor]] Claim the $kbPath cursor is $newCursor } else { Claim the $kbPath cursor is [updateCursor $cursor {x -1}] } - Commit "code$kbPath" { Claim $id has program code [deleteCharacter $code $cursor] } + Commit "code$kbPath" { + Claim $id has program code $code + Claim $id has editor code [deleteCharacter $editorCode $cursor] + } } SPACE { Claim the $kbPath cursor is [updateCursor $cursor {x 1}] - Commit "code$kbPath" { Claim $id has program code [insertCharacter $code " " $cursor] } + Commit "code$kbPath" { + Claim $id has program code $code + Claim $id has editor code [insertCharacter $editorCode " " $cursor] + } } ENTER { set updatedCursor [updateCursor $cursor {y 1}] Claim the $kbPath cursor is [list 0 [y $updatedCursor]] - Commit "code$kbPath" { Claim $id has program code [insertNewline $code $cursor] } + Commit "code$kbPath" { + Claim $id has program code $code + Claim $id has editor code [insertNewline $editorCode $cursor] + } } DELETE - INSERT - @@ -263,36 +272,62 @@ When /page/ is a keyboard with path /kbPath/ & /page/ is an editor { Wish to print $code with job id [expr {rand()}] return } - Commit "code$kbPath" { Claim $id has program code $code} + Commit "code$kbPath" { + Claim $id has program code $code + Claim $id has editor code $editorCode + } Claim the $kbPath cursor is $cursor return } if {$modifier == "ctrl" & $currentCharacter == "r"} { - Commit "code$kbPath" { Claim $id has program code $baseCode } + Commit "code$kbPath" { + Claim $id has program code $baseCode + Claim $id has editor code $baseCode + } Claim the $kbPath cursor is [list 0 0] return } + if {$modifier == "ctrl" & $currentCharacter == "s"} { + Commit "code$kbPath" { + Claim $id has program code $editorCode + Claim $id has editor code $editorCode + } + Claim the $kbPath cursor is $cursor + return + } if {$modifier == "ctrl" & $currentCharacter == "a"} { - Commit "code$kbPath" { Claim $id has program code $code} + Commit "code$kbPath" { + Claim $id has program code $code + Claim $id has editor code $editorCode + } lassign $cursor x y Claim the $kbPath cursor is [list 0 $y] return } if {$modifier == "ctrl" & $currentCharacter == "e"} { - Commit "code$kbPath" { Claim $id has program code $code} + Commit "code$kbPath" { + Claim $id has program code $code + Claim $id has editor code $editorCode + } lassign $cursor x y Claim the $kbPath cursor is [list [getLineLength $code $cursor] $y] return } if {$modifier == "ctrl" & $currentCharacter == "u"} { # delete from cursor back to 0 and move cursor to 0 - Commit "code$kbPath" { Claim $id has program code [deleteToBeginning $code $cursor]} + Commit "code$kbPath" { + Claims $id has program code $code + Claim $id has program code [deleteToBeginning $editorCode $cursor] + } lassign $cursor x y Claim the $kbPath cursor is [list 0 $y] return } Claim the $kbPath cursor is [updateCursor $cursor {x 1}] - Commit "code$kbPath" { Claim $id has program code [insertCharacter $code $currentCharacter $cursor] } + Commit "code$kbPath" { + Claim $id has program code $code + Claim $id has editor code [insertCharacter $editorCode $currentCharacter $cursor] + } } } } -- cgit v1.2.3 From 0e129ec2b61b65fce9b44a4718f0f4dbe5bbcd58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Fri, 15 Dec 2023 18:19:55 -0500 Subject: Make direction keys continous --- virtual-programs/editor.folk | 292 +++++++++++++++++++++++-------------------- 1 file changed, 154 insertions(+), 138 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 58a0678a..785d442e 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -165,168 +165,184 @@ proc getCurrentLineLength {lines cursor} { When /page/ is a keyboard with path /kbPath/ & /page/ is an editor { set id "$page$kbPath" - Every time keyboard $kbPath claims key /currentCharacter/ is down with modifiers /modifier/ & the $kbPath cursor is /cursor/ & $id has program code /code/ & $id has editor code /editorCode/ { - Commit "cursor$kbPath" { - switch $currentCharacter { - UP { - set updatedCursor [updateCursor $cursor {y -1}] - set currentLineLength [getCurrentLineLength $editorCode $updatedCursor] - if {[x $updatedCursor] > $currentLineLength} { - Claim the $kbPath cursor is [list [- $currentLineLength 1] [y $updatedCursor]] - } else { - Claim the $kbPath cursor is $updatedCursor + Every time keyboard $kbPath claims key /currentCharacter/ is /keyState/ with modifiers /modifier/ & the $kbPath cursor is /cursor/ & $id has program code /code/ & $id has editor code /editorCode/ { + if {$keyState == "down" || $keyState == "repeat"} { + Commit "cursor$kbPath" { + switch $currentCharacter { + UP { + set updatedCursor [updateCursor $cursor {y -1}] + set currentLineLength [getCurrentLineLength $editorCode $updatedCursor] + if {[x $updatedCursor] > $currentLineLength} { + Claim the $kbPath cursor is [list $currentLineLength [y $updatedCursor]] + } else { + Claim the $kbPath cursor is $updatedCursor + } } - } - DOWN { - set linecount [llength [split $editorCode "\n"]] - set updatedCursor [updateCursor $cursor {y 1}] - set currentLineLength [getCurrentLineLength $editorCode $updatedCursor] + DOWN { + set linecount [llength [split $editorCode "\n"]] + set updatedCursor [updateCursor $cursor {y 1}] + set currentLineLength [getCurrentLineLength $editorCode $updatedCursor] - if {[y $updatedCursor] == $linecount} { - Claim the $kbPath cursor is $cursor - return - } elseif {[x $updatedCursor] > $currentLineLength} { - Claim the $kbPath cursor is [list $currentLineLength [y $updatedCursor]] - } else { - Claim the $kbPath cursor is $updatedCursor + if {[y $updatedCursor] == $linecount} { + Claim the $kbPath cursor is $cursor + return + } elseif {[x $updatedCursor] > $currentLineLength} { + Claim the $kbPath cursor is [list $currentLineLength [y $updatedCursor]] + } else { + Claim the $kbPath cursor is $updatedCursor + } } - } - RIGHT { - set currentLineLength [getCurrentLineLength $editorCode $cursor] - if {[x $cursor] == $currentLineLength} { - Claim the $kbPath cursor is $cursor - } else { - Claim the $kbPath cursor is [updateCursor $cursor {x 1}] + RIGHT { + set currentLineLength [getCurrentLineLength $editorCode $cursor] + if {[x $cursor] == $currentLineLength} { + if {[y $cursor] == [expr {[llength [split $editorCode "\n"]] - 1}]} { + Claim the $kbPath cursor is $cursor + } else { + set newCursor [updateCursor $cursor {y 1}] + Claim the $kbPath cursor is [list 0 [y $newCursor]] + } + } else { + Claim the $kbPath cursor is [updateCursor $cursor {x 1}] + } } - } - LEFT { Claim the $kbPath cursor is [updateCursor $cursor {x -1}] } - BACKSPACE { - # if cursor is at the beginning of the line, delete the newline - if {[x $cursor] == 0} { - set newCursor [updateCursor $cursor {y -1}] - set previousLineLength [getCurrentLineLength $editorCode $newCursor] - set newCursor [list $previousLineLength [y $newCursor]] - Claim the $kbPath cursor is $newCursor - } else { - Claim the $kbPath cursor is [updateCursor $cursor {x -1}] + LEFT { + if {[x $cursor] == 0} { + set newCursor [updateCursor $cursor {y -1}] + set previousLineLength [getCurrentLineLength $editorCode $newCursor] + set newCursor [list $previousLineLength [y $newCursor]] + Claim the $kbPath cursor is $newCursor + } else { + Claim the $kbPath cursor is [updateCursor $cursor {x -1}] + } } - Commit "code$kbPath" { - Claim $id has program code $code - Claim $id has editor code [deleteCharacter $editorCode $cursor] + BACKSPACE { + # if cursor is at the beginning of the line, delete the newline + if {[x $cursor] == 0} { + set newCursor [updateCursor $cursor {y -1}] + set previousLineLength [getCurrentLineLength $editorCode $newCursor] + set newCursor [list $previousLineLength [y $newCursor]] + Claim the $kbPath cursor is $newCursor + } else { + Claim the $kbPath cursor is [updateCursor $cursor {x -1}] + } + Commit "code$kbPath" { + Claim $id has program code $code + Claim $id has editor code [deleteCharacter $editorCode $cursor] + } } - } - SPACE { - Claim the $kbPath cursor is [updateCursor $cursor {x 1}] - Commit "code$kbPath" { - Claim $id has program code $code - Claim $id has editor code [insertCharacter $editorCode " " $cursor] + SPACE { + Claim the $kbPath cursor is [updateCursor $cursor {x 1}] + Commit "code$kbPath" { + Claim $id has program code $code + Claim $id has editor code [insertCharacter $editorCode " " $cursor] + } } - } - ENTER { - set updatedCursor [updateCursor $cursor {y 1}] - Claim the $kbPath cursor is [list 0 [y $updatedCursor]] - Commit "code$kbPath" { - Claim $id has program code $code - Claim $id has editor code [insertNewline $editorCode $cursor] + ENTER { + set updatedCursor [updateCursor $cursor {y 1}] + Claim the $kbPath cursor is [list 0 [y $updatedCursor]] + Commit "code$kbPath" { + Claim $id has program code $code + Claim $id has editor code [insertNewline $editorCode $cursor] + } } - } - DELETE - - INSERT - - MUTE - - VOLUMEUP - - VOLUMEDOWN - - ESC - - TAB - - CAPSLOCK - - LEFTSHIFT - - RIGHTSHIFT - - LEFTALT - - RIGHTALT - - LEFTCTRL - - RIGHTCTRL { - # TODO: Implement DELETE, operates like BACKSPACE, but in the opposite direction - # TODO: MUTE VOLUMEUP VOLUMEDOWN - # implement sound.folk that allows a system-wide - # volume setting to be adjusted. - # Perhaps `Wish $system volume is 0.5` or something + DELETE - + INSERT - + MUTE - + VOLUMEUP - + VOLUMEDOWN - + ESC - + TAB - + CAPSLOCK - + LEFTSHIFT - + RIGHTSHIFT - + LEFTALT - + RIGHTALT - + LEFTCTRL - + RIGHTCTRL { + # TODO: Implement DELETE, operates like BACKSPACE, but in the opposite direction + # TODO: MUTE VOLUMEUP VOLUMEDOWN + # implement sound.folk that allows a system-wide + # volume setting to be adjusted. + # Perhaps `Wish $system volume is 0.5` or something - Claim the $kbPath cursor is $cursor - } - default { - if {$modifier == "ctrl" & $currentCharacter == "p"} { - # TODO: Save time instead and prevent printing the same code within the same ... second? - When $id has printed /lastPrintedCode/ { - if {$lastPrintedCode == $code} { - Claim the $kbPath cursor is $cursor + Claim the $kbPath cursor is $cursor + } + default { + if {$modifier == "ctrl" & $currentCharacter == "p"} { + # TODO: Save time instead and prevent printing the same code within the same ... second? + When $id has printed /lastPrintedCode/ { + if {$lastPrintedCode == $code} { + Claim the $kbPath cursor is $cursor + if {$::debug_print} { + puts "Not printing the same code twice" + } + return + } + } + When /nobody/ has printed /lastPrintedCode/ { if {$::debug_print} { - puts "Not printing the same code twice" + puts "Printing $code" } + Commit print { Claim $id has printed $code} + Wish to print $code with job id [expr {rand()}] return } + Commit "code$kbPath" { + Claim $id has program code $code + Claim $id has editor code $editorCode + } + Claim the $kbPath cursor is $cursor + return } - When /nobody/ has printed /lastPrintedCode/ { - if {$::debug_print} { - puts "Printing $code" + if {$modifier == "ctrl" & $currentCharacter == "r"} { + Commit "code$kbPath" { + Claim $id has program code $baseCode + Claim $id has editor code $baseCode } - Commit print { Claim $id has printed $code} - Wish to print $code with job id [expr {rand()}] + Claim the $kbPath cursor is [list 0 0] return } - Commit "code$kbPath" { - Claim $id has program code $code - Claim $id has editor code $editorCode + if {$modifier == "ctrl" & $currentCharacter == "s"} { + Commit "code$kbPath" { + Claim $id has program code $editorCode + Claim $id has editor code $editorCode + } + Claim the $kbPath cursor is $cursor + return } - Claim the $kbPath cursor is $cursor - return - } - if {$modifier == "ctrl" & $currentCharacter == "r"} { - Commit "code$kbPath" { - Claim $id has program code $baseCode - Claim $id has editor code $baseCode + if {$modifier == "ctrl" & $currentCharacter == "a"} { + Commit "code$kbPath" { + Claim $id has program code $code + Claim $id has editor code $editorCode + } + lassign $cursor x y + Claim the $kbPath cursor is [list 0 $y] + return } - Claim the $kbPath cursor is [list 0 0] - return - } - if {$modifier == "ctrl" & $currentCharacter == "s"} { - Commit "code$kbPath" { - Claim $id has program code $editorCode - Claim $id has editor code $editorCode + if {$modifier == "ctrl" & $currentCharacter == "e"} { + Commit "code$kbPath" { + Claim $id has program code $code + Claim $id has editor code $editorCode + } + lassign $cursor x y + Claim the $kbPath cursor is [list [getLineLength $editorCode $cursor] $y] + return } - Claim the $kbPath cursor is $cursor - return - } - if {$modifier == "ctrl" & $currentCharacter == "a"} { - Commit "code$kbPath" { - Claim $id has program code $code - Claim $id has editor code $editorCode + if {$modifier == "ctrl" & $currentCharacter == "u"} { + # delete from cursor back to 0 and move cursor to 0 + Commit "code$kbPath" { + Claim $id has program code $code + Claim $id has editor code [deleteToBeginning $editorCode $cursor] + } + lassign $cursor x y + Claim the $kbPath cursor is [list 0 $y] + return } - lassign $cursor x y - Claim the $kbPath cursor is [list 0 $y] - return - } - if {$modifier == "ctrl" & $currentCharacter == "e"} { + Claim the $kbPath cursor is [updateCursor $cursor {x 1}] Commit "code$kbPath" { Claim $id has program code $code - Claim $id has editor code $editorCode + Claim $id has editor code [insertCharacter $editorCode $currentCharacter $cursor] } - lassign $cursor x y - Claim the $kbPath cursor is [list [getLineLength $code $cursor] $y] - return - } - if {$modifier == "ctrl" & $currentCharacter == "u"} { - # delete from cursor back to 0 and move cursor to 0 - Commit "code$kbPath" { - Claims $id has program code $code - Claim $id has program code [deleteToBeginning $editorCode $cursor] - } - lassign $cursor x y - Claim the $kbPath cursor is [list 0 $y] - return - } - Claim the $kbPath cursor is [updateCursor $cursor {x 1}] - Commit "code$kbPath" { - Claim $id has program code $code - Claim $id has editor code [insertCharacter $editorCode $currentCharacter $cursor] } } } -- cgit v1.2.3 From 2e69b09b1e86a5a8fab01e0071a2ae311946ea55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Wed, 27 Dec 2023 16:43:37 -0500 Subject: Fix cursor persistence to unmatching & LEFT at origin --- virtual-programs/editor.folk | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 785d442e..f378ec9b 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -28,7 +28,12 @@ proc x {vector} { lindex $vector 0 } proc y {vector} { lindex $vector 1 } When /page/ is a keyboard with path /kbPath/ & /page/ is an editor { - Commit "cursor$kbPath" { Claim the $kbPath cursor is [list 0 0]} + set id "$page$kbPath" + When /nobody/ claims $id has program code /c/ { + Commit "cursor$kbPath" { + Claim the $kbPath cursor is [list 0 0] + } + } } proc insertCharacter {code newCharacter cursor} { @@ -167,7 +172,7 @@ When /page/ is a keyboard with path /kbPath/ & /page/ is an editor { set id "$page$kbPath" Every time keyboard $kbPath claims key /currentCharacter/ is /keyState/ with modifiers /modifier/ & the $kbPath cursor is /cursor/ & $id has program code /code/ & $id has editor code /editorCode/ { if {$keyState == "down" || $keyState == "repeat"} { - Commit "cursor$kbPath" { + Commit "cursor$kbPath" { switch $currentCharacter { UP { set updatedCursor [updateCursor $cursor {y -1}] @@ -206,7 +211,9 @@ When /page/ is a keyboard with path /kbPath/ & /page/ is an editor { } } LEFT { - if {[x $cursor] == 0} { + if {[x $cursor] == 0 && [y $cursor] == 0} { + Claim the $kbPath cursor is $cursor + } elseif {[x $cursor] == 0} { set newCursor [updateCursor $cursor {y -1}] set previousLineLength [getCurrentLineLength $editorCode $newCursor] set newCursor [list $previousLineLength [y $newCursor]] -- cgit v1.2.3 From d4ae18e2e3e792b391a3070a1fcf1f4f1883e991 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Thu, 28 Dec 2023 16:53:44 -0500 Subject: Allow for key repeating --- virtual-programs/editor.folk | 2 +- virtual-programs/keyboard.folk | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index f378ec9b..034d2ade 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -170,7 +170,7 @@ proc getCurrentLineLength {lines cursor} { When /page/ is a keyboard with path /kbPath/ & /page/ is an editor { set id "$page$kbPath" - Every time keyboard $kbPath claims key /currentCharacter/ is /keyState/ with modifiers /modifier/ & the $kbPath cursor is /cursor/ & $id has program code /code/ & $id has editor code /editorCode/ { + Every time keyboard $kbPath claims key /currentCharacter/ is /keyState/ with modifiers /modifier/ with rand /r/ & the $kbPath cursor is /cursor/ & $id has program code /code/ & $id has editor code /editorCode/ { if {$keyState == "down" || $keyState == "repeat"} { Commit "cursor$kbPath" { switch $currentCharacter { diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index c52f119c..83c83a5e 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -53,7 +53,7 @@ foreach keyboard $keyboardDevices { Start process "keyboard-$keyboard" { source "pi/KeyCodes.tcl" Wish $::thisProcess shares statements like \ - [list keyboard /kb/ claims key /k/ is /t/ with modifiers /m/] + [list keyboard /kb/ claims key /k/ is /t/ with modifiers /m/ with rand /r/] variable evtBytes 16 variable evtFormat iissi if {[exec getconf LONG_BIT] == 64} { @@ -91,8 +91,8 @@ foreach keyboard $keyboardDevices { } set heldModifiers [dict keys [dict filter $modifiers value 1]] - Retract keyboard $keyboardSpecifier claims key /k/ is /t/ with modifiers /m/ - Assert keyboard $keyboardSpecifier claims key $key is $keyState with modifiers $heldModifiers + Retract keyboard $keyboardSpecifier claims key /k/ is /t/ with modifiers /m/ with rand /r/ + Assert keyboard $keyboardSpecifier claims key $key is $keyState with modifiers $heldModifiers with rand [rand] Step } } -- cgit v1.2.3 From aa2f2f1cefab62ebb982a568be2be39b10476ceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Thu, 28 Dec 2023 17:01:46 -0500 Subject: Fix Alt+ESC restart --- virtual-programs/esc-restart.folk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'virtual-programs') diff --git a/virtual-programs/esc-restart.folk b/virtual-programs/esc-restart.folk index d94f2323..57cf97b7 100644 --- a/virtual-programs/esc-restart.folk +++ b/virtual-programs/esc-restart.folk @@ -1,3 +1,3 @@ -When keyboard /k/ claims key ESC is down with modifiers alt { +When keyboard /k/ claims key ESC is down with modifiers alt with rand /r/ { exec sudo systemctl restart folk } -- cgit v1.2.3 From e7cbd176f03cf335c8ab6ce9185905a55a095d4d Mon Sep 17 00:00:00 2001 From: Omar Rizwan Date: Mon, 29 Jan 2024 12:53:59 -0500 Subject: keyboard: Use binary translation; fixes event reader going off rails No need to reset on invalid code now. --- virtual-programs/keyboard.folk | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index 83c83a5e..819e6afc 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -52,6 +52,8 @@ foreach keyboard $keyboardDevices { Claim keyboards are $keyboardDevices Start process "keyboard-$keyboard" { source "pi/KeyCodes.tcl" + set KEY_STATES [list up down repeat] + Wish $::thisProcess shares statements like \ [list keyboard /kb/ claims key /k/ is /t/ with modifiers /m/ with rand /r/] variable evtBytes 16 @@ -61,8 +63,10 @@ foreach keyboard $keyboardDevices { set evtFormat wwssi } set keyboardSpecifier $keyboard + variable keyboardChannel [open $keyboard r] - set keyStates [list up down repeat] + chan configure $keyboardChannel -translation binary + set modifiers [dict create \ shift 0 \ ctrl 0 \ @@ -70,14 +74,10 @@ foreach keyboard $keyboardDevices { ] while 1 { binary scan [read $keyboardChannel $evtBytes] $evtFormat tvSec tvUsec type code value - if {$type > 0x04} { - set keyboardChannel [open $keyboard r] - binary scan [read $keyboardChannel $evtBytes] $evtFormat tvSec tvUsec type code value - } - if {$type == 0x01} { + if {$type == 0x01} { ;# EV_KEY set shift [dict get $modifiers shift] set key [keyFromCode $code $shift] - set keyState [lindex $keyStates $value] + set keyState [lindex $KEY_STATES $value] set isDown [expr {$keyState != "up"}] if {[string match *SHIFT $key]} { @@ -97,4 +97,4 @@ foreach keyboard $keyboardDevices { } } } -} \ No newline at end of file +} -- cgit v1.2.3 From ec730dcb1724c289156c15b2a5d42aa460b71f5e Mon Sep 17 00:00:00 2001 From: Omar Rizwan Date: Mon, 29 Jan 2024 13:46:19 -0500 Subject: WIP: Use timestamp instead of rand for keypresses, don't retract This fixes key dropping, but leaks statements all over the place. To do next: retract old key events after a while. --- virtual-programs/editor.folk | 5 ++++- virtual-programs/esc-restart.folk | 2 +- virtual-programs/keyboard.folk | 22 ++++++++++++++++++---- 3 files changed, 23 insertions(+), 6 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 034d2ade..0c7e4275 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -170,7 +170,10 @@ proc getCurrentLineLength {lines cursor} { When /page/ is a keyboard with path /kbPath/ & /page/ is an editor { set id "$page$kbPath" - Every time keyboard $kbPath claims key /currentCharacter/ is /keyState/ with modifiers /modifier/ with rand /r/ & the $kbPath cursor is /cursor/ & $id has program code /code/ & $id has editor code /editorCode/ { + Every time keyboard $kbPath claims key /currentCharacter/ is /keyState/ with modifiers /modifier/ timestamp /timestamp/ &\ + the $kbPath cursor is /cursor/ &\ + $id has program code /code/ &\ + $id has editor code /editorCode/ { if {$keyState == "down" || $keyState == "repeat"} { Commit "cursor$kbPath" { switch $currentCharacter { diff --git a/virtual-programs/esc-restart.folk b/virtual-programs/esc-restart.folk index 57cf97b7..17b56163 100644 --- a/virtual-programs/esc-restart.folk +++ b/virtual-programs/esc-restart.folk @@ -1,3 +1,3 @@ -When keyboard /k/ claims key ESC is down with modifiers alt with rand /r/ { +When keyboard /k/ claims key ESC is down with modifiers alt timestamp /any/ { exec sudo systemctl restart folk } diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index 819e6afc..ffc0ba6a 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -55,7 +55,7 @@ foreach keyboard $keyboardDevices { set KEY_STATES [list up down repeat] Wish $::thisProcess shares statements like \ - [list keyboard /kb/ claims key /k/ is /t/ with modifiers /m/ with rand /r/] + [list keyboard /kb/ claims key /k/ is /t/ with modifiers /m/ timestamp /timestamp/] variable evtBytes 16 variable evtFormat iissi if {[exec getconf LONG_BIT] == 64} { @@ -73,7 +73,9 @@ foreach keyboard $keyboardDevices { alt 0 \ ] while 1 { - binary scan [read $keyboardChannel $evtBytes] $evtFormat tvSec tvUsec type code value + binary scan [read $keyboardChannel $evtBytes] $evtFormat \ + tvSec tvUsec type code value + if {$type == 0x01} { ;# EV_KEY set shift [dict get $modifiers shift] set key [keyFromCode $code $shift] @@ -91,8 +93,20 @@ foreach keyboard $keyboardDevices { } set heldModifiers [dict keys [dict filter $modifiers value 1]] - Retract keyboard $keyboardSpecifier claims key /k/ is /t/ with modifiers /m/ with rand /r/ - Assert keyboard $keyboardSpecifier claims key $key is $keyState with modifiers $heldModifiers with rand [rand] + set now [clock milliseconds] + Assert keyboard $keyboardSpecifier claims key $key is $keyState with \ + modifiers $heldModifiers timestamp $now + + # Retract all key events that are more than 5 seconds old. + set events [Statements::findMatches [list keyboard $keyboardSpecifier claims key /key/ is /keyState/ with modifiers /ms/ timestamp /timestamp/]] + foreach event $events { + dict with event { + if {$timestamp - $now > 5000} { + puts "Retract!!!!" + } + } + } + Step } } -- cgit v1.2.3 From 9cfa7c255abfefa34f1ea504ddccbf531b2a2d23 Mon Sep 17 00:00:00 2001 From: Omar Rizwan Date: Mon, 29 Jan 2024 14:05:42 -0500 Subject: keyboard: Retract old key events. Drops should be fixed (w/o leaks) --- virtual-programs/keyboard.folk | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index ffc0ba6a..fbc895b0 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -101,8 +101,8 @@ foreach keyboard $keyboardDevices { set events [Statements::findMatches [list keyboard $keyboardSpecifier claims key /key/ is /keyState/ with modifiers /ms/ timestamp /timestamp/]] foreach event $events { dict with event { - if {$timestamp - $now > 5000} { - puts "Retract!!!!" + if {$now - $timestamp > 5000} { + Retract keyboard $keyboardSpecifier claims key $key is $keyState with modifiers $ms timestamp $timestamp } } } -- cgit v1.2.3 From 49230a3de9e4ff774fe39296089bacccbcfb8ee8 Mon Sep 17 00:00:00 2001 From: Omar Rizwan Date: Mon, 29 Jan 2024 14:33:23 -0500 Subject: keyboard,KeyCodes: Ignore instead of ? for invalid key codes --- virtual-programs/keyboard.folk | 2 ++ 1 file changed, 2 insertions(+) (limited to 'virtual-programs') diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index fbc895b0..829406c7 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -79,6 +79,8 @@ foreach keyboard $keyboardDevices { if {$type == 0x01} { ;# EV_KEY set shift [dict get $modifiers shift] set key [keyFromCode $code $shift] + if {$key eq ""} { continue } + set keyState [lindex $KEY_STATES $value] set isDown [expr {$keyState != "up"}] -- cgit v1.2.3 From f0afdb831a3736a6d3d24f85f78d35f9e2aa9e3f Mon Sep 17 00:00:00 2001 From: Omar Rizwan Date: Mon, 29 Jan 2024 15:54:20 -0500 Subject: editor: Automatically editor-ize keyboards --- virtual-programs/editor.folk | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 0c7e4275..76536f02 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -1,6 +1,13 @@ set ::debug_print false set baseCode "Wish \$this is outlined green" +# This makes all keyboards into editors automatically, so a keyboard +# doesn't need an extra printed claim to be an editor. May choose to +# change later, or exclude keyboards that opt out. +When /page/ is a keyboard with path /anything/ { + Claim $page is an editor +} + When /page/ is a keyboard with path /kbPath/ & /page/ is an editor & /page/ has region /r/ { set id "$page$kbPath" Claim $id has region [region move $r up 900px] -- cgit v1.2.3 From 3055fe9f3e8fccaccf5410aeabf54e19104f9007 Mon Sep 17 00:00:00 2001 From: Omar Rizwan Date: Mon, 29 Jan 2024 15:54:30 -0500 Subject: keyboard: `the keyboards are` full sentence --- virtual-programs/keyboard.folk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'virtual-programs') diff --git a/virtual-programs/keyboard.folk b/virtual-programs/keyboard.folk index 829406c7..9746af91 100644 --- a/virtual-programs/keyboard.folk +++ b/virtual-programs/keyboard.folk @@ -49,7 +49,7 @@ set keyboardDevices [walkInputEventPaths] # go through each keyboard device and start a process that foreach keyboard $keyboardDevices { - Claim keyboards are $keyboardDevices + Claim the keyboards are $keyboardDevices Start process "keyboard-$keyboard" { source "pi/KeyCodes.tcl" set KEY_STATES [list up down repeat] -- cgit v1.2.3 From d5ce31e6c3f7232cbaae77cd8c2cd33cfeb0de91 Mon Sep 17 00:00:00 2001 From: Omar Rizwan Date: Mon, 29 Jan 2024 17:59:27 -0500 Subject: editor: Use percentages to define regions Should scale better to different-resolution systems? --- virtual-programs/editor.folk | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/editor.folk b/virtual-programs/editor.folk index 76536f02..0b77bc7f 100644 --- a/virtual-programs/editor.folk +++ b/virtual-programs/editor.folk @@ -10,7 +10,7 @@ When /page/ is a keyboard with path /anything/ { When /page/ is a keyboard with path /kbPath/ & /page/ is an editor & /page/ has region /r/ { set id "$page$kbPath" - Claim $id has region [region move $r up 900px] + Claim $id has region [region move $r up 210%] When /nobody/ claims $id has program code /c/ { Commit "code$kbPath" { Claim $id has program code $baseCode @@ -130,7 +130,7 @@ When /page/ is a keyboard with path /kbPath/ & /page/ is an editor { set intTime [expr {int($t * 10)}] set scale 0.60 - set relativeRegion [region move $r down 450px] + set relativeRegion [region move $r down 105%] Claim $id' has region $relativeRegion Wish $id' is outlined white lassign [region topleft $r] xstart ystart -- cgit v1.2.3