Created
November 24, 2023 12:15
-
-
Save hym3242/27940217b54be7a36f5ea9fd555e2145 to your computer and use it in GitHub Desktop.
a c program that showcases how to utilize the all event mouse reporting of terminals to create a hover effect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Copyleft 2023 hym3242 | |
//No rights reserved | |
//please forgive my bad code | |
//tested pass on FreeBSD 13.2 | |
//inspired by vttest(1) and xterm manual concerning ctrl sequences (https://invisible-island.net/xterm/ctlseqs/ctlseqs.html) | |
//Usage: stty -echo -icanon; stdbuf -o0 ./mouse 2>debug_output.txt | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <signal.h> | |
#include <stdlib.h> | |
void handler(int signal){ | |
(void)signal; | |
write(1,"\033[?1003l",8); //disable mouse reporting before exit | |
exit(1); | |
} | |
int main(){ | |
int status=0; | |
int changed=0; | |
int shouldbe=0; | |
unsigned char buf[8]; | |
signal(SIGINT,handler); | |
write(1,"\033[?1003h",8); //enable all-event mouse reporting | |
write(1,"\033[10;10HHELLO",13); | |
while(1){ | |
read(0,buf ,1);//^[ | |
read(0,buf+1,1);//[ | |
read(0,buf+2,1);//M | |
read(0,buf+3,1);//C | |
read(0,buf+4,1); | |
read(0,buf+5,1); | |
changed=0; | |
if((buf[4]>=42 && buf[4]<=46) && buf[5]==42){ | |
shouldbe=1; | |
}else{shouldbe=0;} | |
dprintf(2,"status=%d, shouldbe=%d, ",status,shouldbe); | |
changed = status==shouldbe ? 0:1; | |
dprintf(2,"changed=%d\n",changed); | |
if(changed){ | |
if(shouldbe){ | |
printf("\033[10;10H\033[7mHELLO\033[0m"); | |
dprintf(2,"status %d -> 1\n",status); | |
status=1; | |
}else{ | |
printf("\033[10;10H\033[0mHELLO"); | |
dprintf(2,"status %d -> 0\n",status); | |
status=0; | |
} | |
} | |
printf("\033[%d;%dH",buf[5]-32,buf[4]-32); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment