idf_monitor: make Ctrl-] work when running on WSL (v3) #1324
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Because the TIOCSTI termios ioctl is not supported on WSL (bash on
windows) at this time, another way for avoiding (or unblocking) a
blocking read on stdin is needed. Also, setting a timeout using
termios.c_cc[VMIN] = 0 with termios.c_cc[VTIME] = 1 does not currently
work for WSL.
A first approach to mimic the Windows kludge based on kbhit() by using
ioctl(TIOCINQ) for peeking at the input queue before calling read()
basically worked but failed badly for fast multi character input like
escape sequences sent back by the terminal. This can be easily
reproduced using the console example (examples/system/console). The root
cause was identified to be the buffering involved reading and decoding
stdin in python.
The second approach used pthread_kill() via ctypes (python 2) to unblock
getkey() in ConsoleReader which seemed to be working well. However,
even in this case stdin must have been re-opened without buffering.
Otherwise, the read() may be restarted internally after seeing EINTR.
Additionally, the solution felt at least a bit unclean.
After having identified the importance of making stdin unbuffered, it
became clear that in this case select() (or other I/O completion
methods) should also work correctly. Therefore, the current solution was
implemented, which looks a lot cleaner. It should also work on other
POSIX systems (e.g., plain Linux), but this has not been tested, so far.
The change has been tested successfully on WSL with the console example,
escape sequences (UP, DOWN keys), Ctrl-] to leave the monitor and
Ctrl-T/Ctrl-A for re-flashing.