diff options
| author | Andrés Cuervo <andrescuervor@gmail.com> | 2023-11-28 23:19:37 +0000 |
|---|---|---|
| committer | Andrés Cuervo <andrescuervor@gmail.com> | 2023-11-28 23:19:37 +0000 |
| commit | 27b34e6a38a7a3d7c5482c022ec0bdaa2dff86d1 (patch) | |
| tree | 9da514eacc0cd38e55979b01b05cceb82d44618f /virtual-programs | |
| parent | Merge branch 'main' into ac/editor (diff) | |
| download | folk-27b34e6a38a7a3d7c5482c022ec0bdaa2dff86d1.tar.gz folk-27b34e6a38a7a3d7c5482c022ec0bdaa2dff86d1.zip | |
Remove old reference code
Diffstat (limited to 'virtual-programs')
| -rw-r--r-- | virtual-programs/keyboard.folk | 276 |
1 files changed, 11 insertions, 265 deletions
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 |
