Reading the Device Status Report ANSI escape sequence reply

前端 未结 4 1169
独厮守ぢ
独厮守ぢ 2020-12-19 02:08

I\'m trying to retrieve the coordinates of cursor in a VT100 terminal using the following code:

void getCursor(int* x, int* y) {
  printf(\"\\033[6n\");
   s         


        
4条回答
  •  庸人自扰
    2020-12-19 02:37

    I believe that you really get the expected response in stdin. But imagine what happens actually:

    • you send a request as escape sequence to stdout
    • the terminal receives it and formulates a corresponding answer as escape sequence as well
    • the answer is sent to stdin
    • scanf is called and stdin is redirected through the shell where the readline library is used for interactive and editable user input
    • readline captures the escape sequence rather than passing it to the terminal
    • readline re-formulates it with no ESC character to prevent execution of the control sequence but rather makes it readable by only using printable characters
    • the quirked answer reaches scanf but its too late
    • the quirked answer is also echoed to stdout so that the user can instantaneously see what she typed.

    To avoid this use a getc() (==fgetc(stdin)) loop instead. If you encounter an ESC (0x1B) than dump the following characters in a string until you find the final delimiter of the ESC sequence (in your case 'n'). After that you may use sscanf(esqString, formatString, ...).

    But before you encounter the loop you need to change with termios to raw mode (look at the code example below). Else nothing would be different.

提交回复
热议问题