From 0d638c2e572a7e5177506e6290842bc43b0a0426 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 24 Dec 2003 15:35:07 +0000 Subject: [PATCH] * src/io.c: + Mouse support in the X11 driver. --- src/caca_internals.h | 1 + src/graphics.c | 6 ++++-- src/io.c | 30 +++++++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/caca_internals.h b/src/caca_internals.h index 36719d8..6df5d91 100644 --- a/src/caca_internals.h +++ b/src/caca_internals.h @@ -67,6 +67,7 @@ extern enum caca_feature _caca_antialiasing; #include extern Display *x11_dpy; extern Window x11_window; +extern int x11_font_width, x11_font_height; #endif #endif /* __CACA_INTERNALS_H__ */ diff --git a/src/graphics.c b/src/graphics.c index a59425f..132d3e9 100644 --- a/src/graphics.c +++ b/src/graphics.c @@ -81,13 +81,14 @@ static char *conio_screen; #if defined(USE_X11) Display *x11_dpy; Window x11_window; +int x11_font_width, x11_font_height; static GC x11_gc; static Pixmap x11_pixmap; static int *x11_screen; static int x11_colors[16]; static Font x11_font; static XFontStruct *x11_font_struct; -static int x11_font_width, x11_font_height, x11_font_offset; +static int x11_font_offset; #endif static char *_caca_empty_line; @@ -604,7 +605,8 @@ int _caca_init_graphics(void) break; } - XSelectInput(x11_dpy, x11_window, KeyPressMask); + XSelectInput(x11_dpy, x11_window, + KeyPressMask | ButtonPressMask | PointerMotionMask); XSync(x11_dpy, False); diff --git a/src/io.c b/src/io.c index 720134b..56caf0f 100644 --- a/src/io.c +++ b/src/io.c @@ -128,6 +128,11 @@ unsigned int caca_get_event(void) } #endif + /* If it's already a special event, return it */ + if((keybuf[0] & ~0xff) != 0) + return _pop_key(); + + /* If it's not an escape sequence, return the key */ if(keybuf[0] != '\x1b') return CACA_EVENT_KEY_PRESS | _pop_key(); @@ -247,6 +252,8 @@ static unsigned int _read_key(void) #endif #if defined(USE_X11) XEvent event; + static int x11_x = 0, x11_y = 0; + long int event_mask = KeyPressMask | ButtonPressMask | PointerMotionMask; char key; #endif @@ -267,11 +274,32 @@ static unsigned int _read_key(void) #endif #if defined(USE_X11) case CACA_DRIVER_X11: - while(XCheckWindowEvent(x11_dpy, x11_window, KeyPressMask, &event) + while(XCheckWindowEvent(x11_dpy, x11_window, event_mask, &event) == True) { KeySym keysym; + if(event.type == MotionNotify) + { + x11_x = event.xmotion.x; + x11_y = event.xmotion.y; + continue; + } + + if(event.type == ButtonPress) + { + unsigned int x = x11_x / x11_font_width; + unsigned int y = x11_y / x11_font_height; + + if(x >= _caca_width) + x = _caca_width - 1; + if(y >= _caca_height) + y = _caca_height - 1; + + return CACA_EVENT_MOUSE_CLICK + | (1 << 16) | (x << 8) | (y << 0); + } + if(event.type != KeyPress) continue;