Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kiyoon/a0221cb3fe0c7e922cf4503f4e91d049 to your computer and use it in GitHub Desktop.
Save kiyoon/a0221cb3fe0c7e922cf4503f4e91d049 to your computer and use it in GitHub Desktop.
Getting mouse clicks on xterm.
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
enum {
KEYSTATE_NONE = 1024,
KEYSTATE_ESCAPE,
KEYSTATE_CONTROL,
KEYSTATE_MOUSE_PROPS
};
enum {
KEYSTATE_MOUSE_PRESSED,
KEYSTATE_MOUSE_RELEASED,
KEYSTATE_MOUSE_GOT_STATE,
KEYSTATE_MOUSE_GOT_X,
KEYSTATE_MOUSE_GOT_Y
};
int mygetch(void)
{
struct termios oldt,newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}
int get_terminal_width(){
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
return w.ws_col;
}
int get_terminal_height(){
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
return w.ws_row;
}
int twidth, theight;
int mouse_state, mouse_x, mouse_y;
int main() {
twidth = get_terminal_width();
theight = get_terminal_height()-5;
printf("\033[?1000h");
//printf("\033[?1006h");
//printf("\e]12;black\a");
printf ("lines %d\n", get_terminal_height());
printf ("columns \e[41m%d\e[0m\n", get_terminal_width());
char c;
int special=KEYSTATE_NONE;
while (c = mygetch()) {
if (special == KEYSTATE_NONE && c == 27) { special = KEYSTATE_ESCAPE; continue; }
if (special == KEYSTATE_ESCAPE && c == 91) { special = KEYSTATE_CONTROL; continue; }
if (special == KEYSTATE_CONTROL) switch (c) {
case 'A': printf("up\n"); special = KEYSTATE_NONE; continue; break;
case 'B': printf("down \n"); special = KEYSTATE_NONE; continue; break;
case 'D': printf("right\n"); special = KEYSTATE_NONE; continue; break;
case 'C': printf("left\n"); special = KEYSTATE_NONE; continue; break;
}
if (c == 'M') { printf("mouse "); special = KEYSTATE_MOUSE_PROPS; continue;}
if (special==KEYSTATE_MOUSE_PROPS) {
if (c == 32) mouse_state = KEYSTATE_MOUSE_PRESSED;
if (c == 35) mouse_state = KEYSTATE_MOUSE_RELEASED;
special = KEYSTATE_MOUSE_GOT_STATE;
continue;
}
if (special==KEYSTATE_MOUSE_GOT_STATE) {
mouse_x = c - 32;
if (mouse_x < 0) mouse_x += 255;
special = KEYSTATE_MOUSE_GOT_X;
continue;
}
if (special==KEYSTATE_MOUSE_GOT_X) {
mouse_y = c - 32;
if (mouse_y < 0) mouse_y += 255;
special = KEYSTATE_MOUSE_GOT_Y;
}
if (special == KEYSTATE_MOUSE_GOT_Y) {
special = KEYSTATE_NONE;
printf("Mouse: STATE=%d\tX=%d\tY=%d\n", mouse_state, mouse_x, mouse_y);
continue;
}
printf("Char is ");
if (c>='A') printf("%c (", c);
printf("%d", c);
if (c>='A') printf(")");
printf("\n");
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment