02-22-2026, 03:58 PM
Well I'll go ahead and post a snippet of how I worked around that. The shell script was gleaned from Stackoverflow. The results are copied to the clipboard and then accessed by you program. Of course the snippet was bracketed by a meta condition of it being Linux.
There is also a way to do it in C. This is not my code, but I tested it and it does work.
Code: (Select All)
'In linux, there is no easy way to get output of the
'cursor position query "ESC[6n". One has to resort to
'dirty tricks to get the output.
DIM AS STRING script, clipboardTemp
clipboardTemp = _CLIPBOARD$
'If shell script doesn't exist create it
IF NOT _FILEEXISTS("rc.sh") THEN
f = FREEFILE
script = script + "#!/bin/sh" + CHR$(10)
script = script + "if [ -t 0 ] && [ -t 1 ]; then" + CHR$(10)
'Save the terminal setting into a temp variable
script = script + " old_settings=$(stty -g) || exit" + CHR$(10)
'In case of problem, restore settings
script = script + " trap 'stty " + CHR$(34) + "$(old_settings)" + CHR$(34) + "' INT TERM QUIT ALRM" + CHR$(10)
'Set the terminal settings to receive the output of the query
script = script + " stty -icanon -echo min 0 time 3 || exit" + CHR$(10)
'Make the query
script = script + " printf '\033[6n'" + CHR$(10)
'Perform black magic to get the results of query
script = script + " pos=$(dd count=1 2> /dev/null)" + CHR$(10)
script = script + " pos=${pos%R*}" + CHR$(10)
script = script + " pos=${pos##*\[}" + CHR$(10)
script = script + " x=${pos##*;} y=${pos%%;*}" + CHR$(10)
'Restore terminal state
script = script + " stty " + CHR$(34) + "$old_settings" + CHR$(34) + CHR$(10)
script = script + " trap - INT TERM QUIT ALRM" + CHR$(10)
script = script + "fi" + CHR$(10)
'Spit results to clipboard
$IF MAC THEN
script = script + "echo " + CHR$(34) + "$x:$y" + CHR$(34) + " | pbcopy" + CHR$(10)
$ELSE
script = script + "echo " + CHR$(34) + "$x:$y" + CHR$(34) + " | xclip -selection clipboard" + CHR$(10)
$END IF
OPEN "rc.sh" FOR OUTPUT AS f
PRINT #f, script;
CLOSE f
'Make the script executable
SHELL _HIDE "chmod +x rc.sh"
END IF
SHELL "./rc.sh"
c$ = _CLIPBOARD$
_CLIPBOARD$ = clipboardTemp
y$ = LEFT$(c$, INSTR(c$, ":"))
x$ = MID$(c$, INSTR(c$, ":") + 1)
y = VAL(y$): x = VAL(x$)
There is also a way to do it in C. This is not my code, but I tested it and it does work.
Code: (Select All)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
int getCursorPosition(int *x, int *y) {
struct termios old_termios;
char response[32];
int i = 0;
char c;
// Save current terminal settings
tcgetattr(STDIN_FILENO, &old_termios);
// Set terminal to non-canonical mode
struct termios new_termios = old_termios;
new_termios.c_lflag &= ~(ICANON | ECHO);
new_termios.c_cc[VMIN] = 0;
new_termios.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &new_termios);
// Send cursor position request
printf("\033[6n");
fflush(stdout);
// Read the response
while (i < sizeof(response) - 1) {
if (read(STDIN_FILENO, &c, 1) > 0) {
response[i++] = c;
if (c == 'R') break;
}
}
response[i] = '\0';
// Restore terminal settings
tcsetattr(STDIN_FILENO, TCSANOW, &old_termios);
// Parse response: ESC[<row>;<col>R
if (i > 3 && response[0] == '\033' && response[1] == '[') {
char *ptr = response + 2;
char *end;
*y = strtol(ptr, &end, 10);
if (*end == ';') {
*x = strtol(end + 1, NULL, 10);
return 1;
}
}
return 0;
}
int main() {
int x, y;
printf("Current cursor position:\n");
if (getCursorPosition(&x, &y)) {
printf("X = %d, Y = %d\n", x, y);
} else {
printf("Could not determine cursor position\n");
}
return 0;
}
2D physics engine https://github.com/mechatronic3000/fzxNGN
QB Pool https://github.com/mechatronic3000/QBPool
QB Pool https://github.com/mechatronic3000/QBPool


