Skip to content

Commit

Permalink
clib: Fix Linux cursor positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
richardg867 committed Oct 11, 2023
1 parent 1631f4d commit b395001
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions clib/clib_term.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# include <stdio.h>
# ifdef __GNUC__
# include <sys/ioctl.h>
# include <termios.h>
# include <unistd.h>
# endif
#endif
Expand Down Expand Up @@ -90,36 +91,40 @@ term_get_size_y()
int
term_get_cursor_pos(uint8_t *x, uint8_t *y)
{
char ch;
int rx, ry, n;
struct termios attrs;

fputs("\033[6n", stderr);
fflush(stderr);
if (getc(stdin) != 0x1b)
/* Capturing then uncapturing stdin like this is weird, but it'll have to do. */
if (tcgetattr(STDOUT_FILENO, &attrs))
return 0;
if (getc(stdin) != '[')
int old_lflag = attrs.c_lflag;
attrs.c_lflag &= ~(ECHO | ICANON);
if (tcsetattr(STDOUT_FILENO, TCSANOW, &attrs))
return 0;
*x = *y = 0;
while (1) {
ch = getc(stdin);
if ((ch < '0') || (ch > '9'))
break;
*x = (*x * 10) + (ch - '0');
}
while (1) {
ch = getc(stdin);
if ((ch < '0') || (ch > '9'))
break;
*y = (*y * 10) + (ch - '0');

/* Send CPR request. */
fputs("\033[6n", stderr);
fflush(stderr);

/* Read CPR response. */
n = scanf("\033[%d;%dR", &ry, &rx);
if (n >= 2) {
*x = rx - 1;
*y = ry - 1;
}
*x -= 1;
*y -= 1;
return 1;

/* Uncapture stdin. */
attrs.c_lflag = old_lflag;
tcsetattr(STDOUT_FILENO, TCSANOW, &attrs);

return n >= 2;
}

int
term_set_cursor_pos(uint8_t x, uint8_t y)
{
fprintf(stderr, "\033[%d;%dH", x + 1, y + 1);
fprintf(stdout, "\033[%d;%dH", y + 1, x + 1);
fflush(stdout);
return 1;
}
#else
Expand Down

0 comments on commit b395001

Please sign in to comment.