[qwaq] Read mouse movements

Many thanks to https://gist.github.com/sylt/93d3f7b77e7f3a881603 for the
necessary escape sequence to get xterm reporting mouse movement events.
This commit is contained in:
Bill Currie 2020-02-27 21:22:10 +09:00
parent a3ed5926b9
commit dd25bf5dfe
2 changed files with 29 additions and 7 deletions

View file

@ -6,6 +6,7 @@ void initialize (void) = #0;
window_t create_window (int xpos, int ypos, int xlen, int ylen) = #0;
void destroy_window (window_t win) = #0;
void wprintf (window_t win, string fmt, ...) = #0;
void mvwprintf (window_t win, int x, int y, string fmt, ...) = #0;
int wgetch (window_t win) = #0;
void process_input (void) = #0;
@ -25,14 +26,14 @@ int main (int argc, string *argv)
if (get_event (&event)) {
if (event.event_type == qe_key) {
ch = event.e.key;
wprintf (win, "key: %d\n", ch);
mvwprintf (win, 1, 1, "key: %d\n", ch);
} else if (event.event_type == qe_mouse) {
wprintf (win, "mouse: %d %d %d\n",
event.e.mouse.x,
event.e.mouse.y,
event.e.mouse.buttons);
mvwprintf (win, 1, 2, "mouse: %d %d %d\n",
event.e.mouse.x,
event.e.mouse.y,
event.e.mouse.buttons);
}
}
} while (ch != 27);
} while (ch != 'q' && ch != 'Q');
return 0;
}

View file

@ -34,6 +34,7 @@
#include <curses.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "QF/dstring.h"
#include "QF/progs.h"
@ -45,6 +46,7 @@
#define always_inline inline __attribute__((__always_inline__))
#define QUEUE_SIZE 16 // must be power of 2 greater than 1
#define QUEUE_MASK (QUEUE_SIZE - 1)
#define MOUSE_MOVES "\033[?1003h" // Make the terminal report mouse movements
typedef struct window_s {
WINDOW *win;
@ -123,7 +125,8 @@ bi_initialize (progs_t *pr)
noecho ();
nonl ();
nodelay (stdscr, TRUE);
mousemask(ALL_MOUSE_EVENTS, NULL);
mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
write(1, MOUSE_MOVES, sizeof (MOUSE_MOVES) - 1);
}
static void
@ -165,6 +168,23 @@ bi_wprintf (progs_t *pr)
wrefresh (window->win);
}
static void
bi_mvwprintf (progs_t *pr)
{
qwaq_resources_t *res = PR_Resources_Find (pr, "qwaq");
window_t *window = get_window (res, __FUNCTION__, P_INT (pr, 0));
int x = P_INT (pr, 1);
int y = P_INT (pr, 2);
const char *fmt = P_GSTRING (pr, 3);
int count = pr->pr_argc - 4;
pr_type_t **args = pr->pr_params + 4;
dstring_clearstr (res->print_buffer);
PR_Sprintf (pr, res->print_buffer, "bi_wprintf", fmt, count, args);
mvwaddstr (window->win, y, x, res->print_buffer->str);
wrefresh (window->win);
}
static void
bi_wgetch (progs_t *pr)
{
@ -261,6 +281,7 @@ static builtin_t builtins[] = {
{"create_window", bi_create_window, -1},
{"destroy_window", bi_destroy_window, -1},
{"wprintf", bi_wprintf, -1},
{"mvwprintf", bi_mvwprintf, -1},
{"wgetch", bi_wgetch, -1},
{"process_input", bi_process_input, -1},
{"get_event", bi_get_event, -1},