| 1 |
/* $Id: mouse.c,v 1.47 2024/12/05 00:37:39 tom Exp $ */ |
| 2 |
|
| 3 |
#include <vttest.h> |
| 4 |
#include <esc.h> |
| 5 |
#include <ttymodes.h> |
| 6 |
|
| 7 |
#define MCHR(c) (unsigned)((unsigned)((c) - ' ') & 0xff) |
| 8 |
|
| 9 |
#define isQuit(c) (((c) == 'q') || ((c) == 'Q')) |
| 10 |
#define isReport(c) (get_level() >= 3 && (((c) == 'r') || ((c) == 'R'))) |
| 11 |
#define isClear(c) ((c) == ' ') |
| 12 |
|
| 13 |
#define ToData(n) vt_move(report_row = 4 + n, report_col = 10) |
| 14 |
|
| 15 |
typedef enum { |
| 16 |
cDFT = 0, |
| 17 |
cUTF = 1005, |
| 18 |
cSGR = 1006, |
| 19 |
cURX = 1015, |
| 20 |
cSGRp = 1016 |
| 21 |
} COORDS; |
| 22 |
|
| 23 |
static int do_ExtCoords; |
| 24 |
static int do_ExtPixels; |
| 25 |
static int do_FocusEvent; |
| 26 |
|
| 27 |
static int chars_high; |
| 28 |
static int chars_wide; |
| 29 |
|
| 30 |
static int pixels_high; |
| 31 |
static int pixels_wide; |
| 32 |
static int pixels_xpos; |
| 33 |
static int pixels_ypos; |
| 34 |
static int pixels_xchr; |
| 35 |
static int pixels_ychr; |
| 36 |
|
| 37 |
/* |
| 38 |
* If the terminal responds to the window-size reports, use that to determine |
| 39 |
* the scaling for pixel-coordinates. |
| 40 |
*/ |
| 41 |
static int |
| 42 |
can_do_pixels(void) |
| 43 |
{ |
| 44 |
static int first = TRUE; |
| 45 |
int result = FALSE; |
| 46 |
if (first) { |
| 47 |
char *report; |
| 48 |
int reply; |
| 49 |
char final; |
| 50 |
char extra; |
| 51 |
|
| 52 |
first = FALSE; |
| 53 |
result = TRUE; |
| 54 |
|
| 55 |
set_tty_raw(TRUE); |
| 56 |
set_tty_echo(FALSE); |
| 57 |
|
| 58 |
brc(13, 't'); |
| 59 |
report = get_reply(); |
| 60 |
if ((report = skip_csi(report)) == NULL |
| 61 |
|| sscanf(report, |
| 62 |
"%d;%d;%d%c%c", |
| 63 |
&reply, |
| 64 |
&pixels_ypos, |
| 65 |
&pixels_xpos, |
| 66 |
&final, |
| 67 |
&extra) != 4 |
| 68 |
|| reply != 3 |
| 69 |
|| pixels_ypos <= 0 |
| 70 |
|| pixels_xpos <= 0 |
| 71 |
|| final != 't') { |
| 72 |
result = FALSE; |
| 73 |
} else { |
| 74 |
brc(14, 't'); |
| 75 |
report = get_reply(); |
| 76 |
if ((report = skip_csi(report)) == NULL |
| 77 |
|| sscanf(report, |
| 78 |
"%d;%d;%d%c%c", |
| 79 |
&reply, |
| 80 |
&pixels_high, |
| 81 |
&pixels_wide, |
| 82 |
&final, |
| 83 |
&extra) != 4 |
| 84 |
|| reply != 4 |
| 85 |
|| pixels_high <= 0 |
| 86 |
|| pixels_wide <= 0 |
| 87 |
|| final != 't') { |
| 88 |
result = FALSE; |
| 89 |
} else { |
| 90 |
brc(16, 't'); |
| 91 |
report = get_reply(); |
| 92 |
if ((report = skip_csi(report)) == NULL |
| 93 |
|| sscanf(report, |
| 94 |
"%d;%d;%d%c%c", |
| 95 |
&reply, |
| 96 |
&pixels_ychr, |
| 97 |
&pixels_xchr, |
| 98 |
&final, |
| 99 |
&extra) != 4 |
| 100 |
|| reply != 6 |
| 101 |
|| pixels_ychr <= 0 |
| 102 |
|| pixels_xchr <= 0 |
| 103 |
|| final != 't') { |
| 104 |
result = FALSE; |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
restore_ttymodes(); |
| 110 |
|
| 111 |
if (result && LOG_ENABLED) { |
| 112 |
fprintf(log_fp, NOTE_STR "screen %dx%d at %d,%d (cell %dx%d)\n", |
| 113 |
pixels_high, pixels_wide, |
| 114 |
pixels_ypos, pixels_xpos, |
| 115 |
pixels_ychr, pixels_xchr); |
| 116 |
} |
| 117 |
} |
| 118 |
return result; |
| 119 |
} |
| 120 |
|
| 121 |
static const char * |
| 122 |
nameOfExtCoords(int code) |
| 123 |
{ |
| 124 |
const char *result; |
| 125 |
|
| 126 |
switch (code) { |
| 127 |
case cUTF: |
| 128 |
result = "UTF-8"; |
| 129 |
break; |
| 130 |
case cSGR: |
| 131 |
result = "SGR"; |
| 132 |
break; |
| 133 |
case cURX: |
| 134 |
result = "urxvt-style"; |
| 135 |
break; |
| 136 |
case cSGRp: |
| 137 |
result = "SGR/pixels"; |
| 138 |
break; |
| 139 |
default: |
| 140 |
result = "normal"; |
| 141 |
break; |
| 142 |
} |
| 143 |
return result; |
| 144 |
} |
| 145 |
|
| 146 |
static void |
| 147 |
show_mousehelp(void) |
| 148 |
{ |
| 149 |
if (get_level() >= 3) |
| 150 |
println("Press 'q' to quit, 'r' to report modes, ' ' to clear."); |
| 151 |
else |
| 152 |
println("Press 'q' to quit, ' ' to clear."); |
| 153 |
} |
| 154 |
|
| 155 |
static unsigned |
| 156 |
xterm_coord(char *source, int *pos) |
| 157 |
{ |
| 158 |
unsigned result; |
| 159 |
|
| 160 |
switch (do_ExtCoords) { |
| 161 |
case cUTF: |
| 162 |
{ |
| 163 |
int used; |
| 164 |
const char *real_src = source + *pos; |
| 165 |
unsigned limit = (unsigned) strlen(real_src); |
| 166 |
|
| 167 |
used = conv_to_utf32((unsigned *) 0, real_src, limit); |
| 168 |
if (used > 0) { |
| 169 |
(void) conv_to_utf32(&result, real_src, limit); |
| 170 |
*pos += used; |
| 171 |
if (result > ' ') |
| 172 |
result -= ' '; |
| 173 |
else |
| 174 |
result = 0; |
| 175 |
} else { |
| 176 |
result = 0; |
| 177 |
} |
| 178 |
} |
| 179 |
break; |
| 180 |
case cSGR: |
| 181 |
case cSGRp: |
| 182 |
result = 0; |
| 183 |
break; |
| 184 |
case cURX: |
| 185 |
result = 0; |
| 186 |
break; |
| 187 |
default: |
| 188 |
{ |
| 189 |
result = MCHR(source[*pos]); |
| 190 |
*pos += 1; |
| 191 |
} |
| 192 |
break; |
| 193 |
} |
| 194 |
return result; |
| 195 |
} |
| 196 |
|
| 197 |
static unsigned |
| 198 |
sgr_param(char **report, int final, unsigned offset) |
| 199 |
{ |
| 200 |
unsigned result = 0; |
| 201 |
|
| 202 |
if (*report != NULL) { |
| 203 |
char *base = *report; |
| 204 |
char *endp = NULL; |
| 205 |
long value = strtol(base, &endp, 10); |
| 206 |
|
| 207 |
if (value >= (long) offset |
| 208 |
&& (endp == NULL |
| 209 |
|| (*endp == 0 || *endp == ';' || *endp == final))) { |
| 210 |
result = (unsigned) value - offset; |
| 211 |
if (endp != NULL) { |
| 212 |
if (*endp == ';') |
| 213 |
++endp; |
| 214 |
*report = endp; |
| 215 |
} else { |
| 216 |
*report = base + strlen(base); |
| 217 |
} |
| 218 |
} else { |
| 219 |
*report = NULL; |
| 220 |
result = offset; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
return result; |
| 225 |
} |
| 226 |
|
| 227 |
static char * |
| 228 |
skip_params(char *report) |
| 229 |
{ |
| 230 |
return report + strspn(report, "0123456789;"); |
| 231 |
} |
| 232 |
|
| 233 |
/* |
| 234 |
* Parse the mouse position report. This is the usual case, containing the 'M' |
| 235 |
* response (as well as variations on it). |
| 236 |
*/ |
| 237 |
static char * |
| 238 |
parse_mouse_M(char *report, unsigned *b, unsigned *x, unsigned *y) |
| 239 |
{ |
| 240 |
char *result = NULL; |
| 241 |
|
| 242 |
if ((report = skip_csi(report)) != NULL) { |
| 243 |
char *finalp; |
| 244 |
|
| 245 |
switch (do_ExtCoords) { |
| 246 |
default: |
| 247 |
case cUTF: |
| 248 |
if (*report == 'M' |
| 249 |
&& strlen(report) >= 4) { |
| 250 |
int pos = 2; |
| 251 |
*b = MCHR(report[1]); |
| 252 |
*x = xterm_coord(report, &pos); |
| 253 |
*y = xterm_coord(report, &pos); |
| 254 |
result = report + pos; |
| 255 |
} |
| 256 |
break; |
| 257 |
|
| 258 |
case cSGR: |
| 259 |
case cSGRp: |
| 260 |
if (*report++ == '<') { |
| 261 |
finalp = skip_params(report); |
| 262 |
if (*finalp == 'M') { |
| 263 |
/* pressed */ |
| 264 |
*b = sgr_param(&report, 'M', 0); |
| 265 |
*x = sgr_param(&report, 'M', 0); |
| 266 |
*y = sgr_param(&report, 'M', 0); |
| 267 |
result = ++finalp; |
| 268 |
} else if (*finalp == 'm') { |
| 269 |
/* released */ |
| 270 |
*b = sgr_param(&report, 'm', 0); |
| 271 |
*x = sgr_param(&report, 'm', 0); |
| 272 |
*y = sgr_param(&report, 'm', 0); |
| 273 |
result = ++finalp; |
| 274 |
} |
| 275 |
} |
| 276 |
break; |
| 277 |
|
| 278 |
case cURX: |
| 279 |
finalp = skip_params(report); |
| 280 |
if (*finalp == 'M') { |
| 281 |
*b = sgr_param(&report, 'M', 32); |
| 282 |
*x = sgr_param(&report, 'M', 0); |
| 283 |
*y = sgr_param(&report, 'M', 0); |
| 284 |
result = ++finalp; |
| 285 |
} |
| 286 |
break; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
return result; |
| 291 |
} |
| 292 |
|
| 293 |
/* |
| 294 |
* Parse the mouse report, looking for the 'T' response, which is part of |
| 295 |
* mouse highlight-tracking. |
| 296 |
*/ |
| 297 |
static char * |
| 298 |
parse_mouse_T(char *report, |
| 299 |
unsigned *start_x, |
| 300 |
unsigned *start_y, |
| 301 |
unsigned *end_x, |
| 302 |
unsigned *end_y, |
| 303 |
unsigned *mouse_x, |
| 304 |
unsigned *mouse_y) |
| 305 |
{ |
| 306 |
char *result = NULL; |
| 307 |
|
| 308 |
if ((report = skip_csi(report)) != NULL) { |
| 309 |
char *finalp; |
| 310 |
|
| 311 |
switch (do_ExtCoords) { |
| 312 |
default: |
| 313 |
case cUTF: |
| 314 |
if (*report == 'M' |
| 315 |
&& strlen(report) >= 7) { |
| 316 |
int pos = 1; |
| 317 |
*start_x = xterm_coord(report, &pos); |
| 318 |
*start_y = xterm_coord(report, &pos); |
| 319 |
*end_x = xterm_coord(report, &pos); |
| 320 |
*end_y = xterm_coord(report, &pos); |
| 321 |
*mouse_x = xterm_coord(report, &pos); |
| 322 |
*mouse_y = xterm_coord(report, &pos); |
| 323 |
result = report + pos; |
| 324 |
} |
| 325 |
break; |
| 326 |
|
| 327 |
case cSGR: |
| 328 |
case cSGRp: |
| 329 |
if (*report++ != '<') |
| 330 |
break; |
| 331 |
/* FALLTHRU */ |
| 332 |
|
| 333 |
case cURX: |
| 334 |
finalp = skip_params(report); |
| 335 |
if (*finalp == 'T') { |
| 336 |
*start_x = sgr_param(&report, 'T', 0); |
| 337 |
*start_y = sgr_param(&report, 'T', 0); |
| 338 |
*end_x = sgr_param(&report, 'T', 0); |
| 339 |
*end_y = sgr_param(&report, 'T', 0); |
| 340 |
*mouse_x = sgr_param(&report, 'T', 0); |
| 341 |
*mouse_y = sgr_param(&report, 'T', 0); |
| 342 |
result = ++finalp; |
| 343 |
} |
| 344 |
break; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
return result; |
| 349 |
} |
| 350 |
|
| 351 |
/* |
| 352 |
* Parse the mouse report, looking for the 't' response, which is part of mouse |
| 353 |
* highlight-tracking. |
| 354 |
*/ |
| 355 |
static char * |
| 356 |
parse_mouse_t(char *report, unsigned *x, unsigned *y) |
| 357 |
{ |
| 358 |
char *result = NULL; |
| 359 |
|
| 360 |
if ((report = skip_csi(report)) != NULL) { |
| 361 |
char *finalp; |
| 362 |
|
| 363 |
switch (do_ExtCoords) { |
| 364 |
default: |
| 365 |
case cUTF: |
| 366 |
if (*report == 't' |
| 367 |
&& strlen(report) >= 3) { |
| 368 |
int pos = 1; |
| 369 |
*x = xterm_coord(report, &pos); |
| 370 |
*y = xterm_coord(report, &pos); |
| 371 |
} |
| 372 |
break; |
| 373 |
|
| 374 |
case cSGR: |
| 375 |
case cSGRp: |
| 376 |
if (*report++ != '<') |
| 377 |
break; |
| 378 |
/* FALLTHRU */ |
| 379 |
|
| 380 |
case cURX: |
| 381 |
finalp = skip_params(report); |
| 382 |
if (*finalp == 't') { |
| 383 |
*x = sgr_param(&report, 't', 0); |
| 384 |
*y = sgr_param(&report, 't', 0); |
| 385 |
result = ++finalp; |
| 386 |
} |
| 387 |
break; |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
return result; |
| 392 |
} |
| 393 |
|
| 394 |
static void |
| 395 |
cat_button(char *dst, const char *src) |
| 396 |
{ |
| 397 |
if (*dst != '\0') |
| 398 |
strcat(dst, ", "); |
| 399 |
strcat(dst, src); |
| 400 |
} |
| 401 |
|
| 402 |
static char * |
| 403 |
locator_button(unsigned b) |
| 404 |
{ |
| 405 |
static char result[80]; |
| 406 |
|
| 407 |
if (b) { |
| 408 |
result[0] = 0; |
| 409 |
if (b & 1) |
| 410 |
cat_button(result, "right"); |
| 411 |
if (b & 2) |
| 412 |
cat_button(result, "middle"); |
| 413 |
if (b & 4) |
| 414 |
cat_button(result, "left"); |
| 415 |
if (b & 8) |
| 416 |
cat_button(result, "M4"); |
| 417 |
} else { |
| 418 |
strcpy(result, "no buttons down"); |
| 419 |
} |
| 420 |
return result; |
| 421 |
} |
| 422 |
|
| 423 |
static const char * |
| 424 |
locator_event(int e) |
| 425 |
{ |
| 426 |
const char *result; |
| 427 |
switch (e) { |
| 428 |
case 0: |
| 429 |
result = "locator unavailable"; |
| 430 |
break; |
| 431 |
case 1: |
| 432 |
result = "request - received a DECRQLP"; |
| 433 |
break; |
| 434 |
case 2: |
| 435 |
result = "left button down"; |
| 436 |
break; |
| 437 |
case 3: |
| 438 |
result = "left button up"; |
| 439 |
break; |
| 440 |
case 4: |
| 441 |
result = "middle button down"; |
| 442 |
break; |
| 443 |
case 5: |
| 444 |
result = "middle button up"; |
| 445 |
break; |
| 446 |
case 6: |
| 447 |
result = "right button down"; |
| 448 |
break; |
| 449 |
case 7: |
| 450 |
result = "right button up"; |
| 451 |
break; |
| 452 |
case 8: |
| 453 |
result = "M4 button down"; |
| 454 |
break; |
| 455 |
case 9: |
| 456 |
result = "M4 button up"; |
| 457 |
break; |
| 458 |
case 10: |
| 459 |
result = "locator outside filter rectangle"; |
| 460 |
break; |
| 461 |
default: |
| 462 |
result = "unknown event"; |
| 463 |
break; |
| 464 |
} |
| 465 |
return result; |
| 466 |
} |
| 467 |
|
| 468 |
static void |
| 469 |
show_click(unsigned y, unsigned x, int c) |
| 470 |
{ |
| 471 |
if (do_ExtPixels) { |
| 472 |
y = (y + (unsigned) pixels_ychr - 1) / (unsigned) pixels_ychr; |
| 473 |
x = (x + (unsigned) pixels_xchr - 1) / (unsigned) pixels_xchr; |
| 474 |
} |
| 475 |
cup((int) y, (int) x); |
| 476 |
putchar(c); |
| 477 |
vt_move((int) y, (int) x); |
| 478 |
fflush(stdout); |
| 479 |
} |
| 480 |
|
| 481 |
/* Print the corners of the highlight-region. Note that xterm doesn't use |
| 482 |
* the last row. |
| 483 |
*/ |
| 484 |
static void |
| 485 |
show_hilite(int first, int last) |
| 486 |
{ |
| 487 |
/* *INDENT-OFF* */ |
| 488 |
vt_move(first, 1); tprintf("+"); |
| 489 |
vt_move(last-1, 1); tprintf("+"); |
| 490 |
vt_move(first, min_cols); tprintf("+"); |
| 491 |
vt_move(last-1, min_cols); tprintf("+"); |
| 492 |
/* *INDENT-ON* */ |
| 493 |
|
| 494 |
fflush(stdout); |
| 495 |
} |
| 496 |
|
| 497 |
static void |
| 498 |
show_locator_rectangle(void) |
| 499 |
{ |
| 500 |
const int first = 10; |
| 501 |
const int last = 20; |
| 502 |
|
| 503 |
decefr(first, 1, last, min_cols); |
| 504 |
show_hilite(first, last); |
| 505 |
} |
| 506 |
|
| 507 |
#define SCALED(value,range) \ |
| 508 |
((value * (unsigned) range + (unsigned) (range - 1)) / (unsigned) range) |
| 509 |
|
| 510 |
static int |
| 511 |
show_locator_report(char *report, int row, int pixels) |
| 512 |
{ |
| 513 |
int Pe, Pb, Pp; |
| 514 |
unsigned Pr, Pc; |
| 515 |
int now = row; |
| 516 |
int report_row, report_col; |
| 517 |
|
| 518 |
ToData(0); |
| 519 |
vt_el(2); |
| 520 |
chrprint2(report, report_row, report_col); |
| 521 |
while ((report = skip_csi(report)) != NULL |
| 522 |
&& (sscanf(report, |
| 523 |
"%d;%d;%u;%u&w", &Pe, &Pb, &Pr, &Pc) == 4 |
| 524 |
|| sscanf(report, |
| 525 |
"%d;%d;%u;%u;%d&w", &Pe, &Pb, &Pr, &Pc, &Pp) == 5)) { |
| 526 |
vt_move(row, 10); |
| 527 |
vt_el(2); |
| 528 |
show_result("%s - %s (%d,%d)", |
| 529 |
locator_event(Pe), |
| 530 |
locator_button((unsigned) Pb), |
| 531 |
Pr, Pc); |
| 532 |
vt_el(0); |
| 533 |
if (pixels) { |
| 534 |
if (pixels_high > 0 && pixels_wide > 0) { |
| 535 |
Pr = SCALED(Pr, pixels_high); |
| 536 |
Pc = SCALED(Pc, pixels_wide); |
| 537 |
show_click(Pr, Pc, '*'); |
| 538 |
} |
| 539 |
} else { |
| 540 |
show_click(Pr, Pc, '*'); |
| 541 |
} |
| 542 |
report = strchr(report, '&') + 2; |
| 543 |
now = row++; |
| 544 |
} |
| 545 |
return now; |
| 546 |
} |
| 547 |
|
| 548 |
static int |
| 549 |
get_screensize(MENU_ARGS) |
| 550 |
{ |
| 551 |
char *report; |
| 552 |
char tmp = 0; |
| 553 |
|
| 554 |
set_tty_raw(TRUE); |
| 555 |
set_tty_echo(FALSE); |
| 556 |
|
| 557 |
brc(14, 't'); /* report window's pixel-size */ |
| 558 |
report = get_reply(); |
| 559 |
if ((report = skip_csi(report)) == NULL |
| 560 |
|| sscanf(report, "4;%d;%d%c", &pixels_high, &pixels_wide, &tmp) != 3 |
| 561 |
|| tmp != 't' |
| 562 |
|| pixels_high <= 0 |
| 563 |
|| pixels_wide <= 0) { |
| 564 |
pixels_high = -1; |
| 565 |
pixels_wide = -1; |
| 566 |
} |
| 567 |
|
| 568 |
brc(18, 't'); /* report window's char-size */ |
| 569 |
report = get_reply(); |
| 570 |
if ((report = skip_csi(report)) == NULL |
| 571 |
|| sscanf(report, "8;%d;%d%c", &chars_high, &chars_wide, &tmp) != 3 |
| 572 |
|| tmp != 't' |
| 573 |
|| chars_high <= 0 |
| 574 |
|| chars_wide <= 0) { |
| 575 |
chars_high = 24; |
| 576 |
chars_wide = 80; |
| 577 |
} |
| 578 |
|
| 579 |
restore_ttymodes(); |
| 580 |
return MENU_NOHOLD; |
| 581 |
} |
| 582 |
|
| 583 |
static void |
| 584 |
show_dec_locator_events(MENU_ARGS, int mode, int pixels) |
| 585 |
{ |
| 586 |
int row, now; |
| 587 |
|
| 588 |
pause_replay(); |
| 589 |
loop: |
| 590 |
vt_move(1, 1); |
| 591 |
ed(0); |
| 592 |
println(the_title); |
| 593 |
show_mousehelp(); |
| 594 |
println("Mouse events will be marked with '*'"); |
| 595 |
|
| 596 |
decelr((mode > 0) ? mode : ((mode == 0) ? 2 : -mode), pixels ? 1 : 2); |
| 597 |
|
| 598 |
if (mode < 0) |
| 599 |
show_locator_rectangle(); |
| 600 |
else if (mode == 0) |
| 601 |
do_csi("'w"); /* see decefr() */ |
| 602 |
|
| 603 |
decsle(1); /* report button-down events */ |
| 604 |
decsle(3); /* report button-up events */ |
| 605 |
set_tty_raw(TRUE); |
| 606 |
set_tty_echo(FALSE); |
| 607 |
|
| 608 |
now = 4; |
| 609 |
for (;;) { |
| 610 |
char *report = instr(); |
| 611 |
if (isQuit(*report)) { |
| 612 |
decrqlp(1); |
| 613 |
report = instr(); |
| 614 |
show_locator_report(report, now + 1, pixels); |
| 615 |
break; |
| 616 |
} else if (isReport(*report)) { |
| 617 |
show_mousemodes(); |
| 618 |
goto loop; |
| 619 |
} else if (isClear(*report)) { |
| 620 |
goto loop; |
| 621 |
} |
| 622 |
row = 4; |
| 623 |
while (now > row) { |
| 624 |
vt_move(now, 1); |
| 625 |
vt_el(2); |
| 626 |
now--; |
| 627 |
} |
| 628 |
now = show_locator_report(report, row, pixels); |
| 629 |
if (mode == 0) { |
| 630 |
decelr(2, pixels ? 1 : 2); |
| 631 |
do_csi("'w"); /* see decefr() */ |
| 632 |
} |
| 633 |
} |
| 634 |
resume_replay(); |
| 635 |
|
| 636 |
decelr(0, 0); |
| 637 |
restore_ttymodes(); |
| 638 |
|
| 639 |
vt_move(max_lines - 2, 1); |
| 640 |
} |
| 641 |
|
| 642 |
/* Normal Mouse Tracking */ |
| 643 |
static void |
| 644 |
show_mouse_tracking(MENU_ARGS, const char *the_mode) |
| 645 |
{ |
| 646 |
unsigned y = 0, x = 0; |
| 647 |
unsigned b, xx, yy; |
| 648 |
int report_row, report_col; |
| 649 |
|
| 650 |
loop: |
| 651 |
vt_move(1, 1); |
| 652 |
ed(0); |
| 653 |
println(the_title); |
| 654 |
show_mousehelp(); |
| 655 |
println("Mouse events will be marked with the button number."); |
| 656 |
|
| 657 |
sm(the_mode); |
| 658 |
set_tty_raw(TRUE); |
| 659 |
set_tty_echo(FALSE); |
| 660 |
|
| 661 |
pause_replay(); |
| 662 |
for (;;) { |
| 663 |
char *report = instr(); |
| 664 |
|
| 665 |
if (isQuit(*report)) { |
| 666 |
break; |
| 667 |
} else if (isReport(*report)) { |
| 668 |
show_mousemodes(); |
| 669 |
goto loop; |
| 670 |
} else if (isClear(*report)) { |
| 671 |
goto loop; |
| 672 |
} |
| 673 |
|
| 674 |
ToData(0); |
| 675 |
chrprint2(report, report_row, report_col); |
| 676 |
|
| 677 |
while ((report = parse_mouse_M(report, &b, &xx, &yy)) != NULL) { |
| 678 |
unsigned adj = 1; |
| 679 |
char result[80]; |
| 680 |
char modifiers[80]; |
| 681 |
|
| 682 |
sprintf(result, "code 0x%x (%u,%u)", b, yy, xx); |
| 683 |
*modifiers = '\0'; |
| 684 |
if (b & (unsigned) (~3)) { |
| 685 |
if (b & 4) |
| 686 |
strcat(modifiers, " shift"); |
| 687 |
if (b & 8) |
| 688 |
strcat(modifiers, " meta"); |
| 689 |
if (b & 16) |
| 690 |
strcat(modifiers, " control"); |
| 691 |
if (b & 32) |
| 692 |
strcat(modifiers, " motion"); |
| 693 |
if (b & 64) |
| 694 |
adj += 3; |
| 695 |
b &= 3; |
| 696 |
} |
| 697 |
if ((b == 3) && (adj == 1)) { |
| 698 |
if (xx != x || yy != y) { |
| 699 |
ToData(1); |
| 700 |
vt_el(2); |
| 701 |
show_result("%s release%s", result, modifiers); |
| 702 |
show_click(yy, xx, '*'); |
| 703 |
} |
| 704 |
} else { |
| 705 |
b += adj; |
| 706 |
ToData(1); |
| 707 |
vt_el(2); |
| 708 |
show_result("%s button %u%s", result, b, modifiers); |
| 709 |
show_click(yy, xx, (int) (b + '0')); |
| 710 |
} |
| 711 |
x = xx; |
| 712 |
y = yy; |
| 713 |
report += 4; |
| 714 |
} |
| 715 |
} |
| 716 |
resume_replay(); |
| 717 |
|
| 718 |
rm(the_mode); |
| 719 |
fflush(stdout); |
| 720 |
restore_ttymodes(); |
| 721 |
|
| 722 |
vt_move(max_lines - 2, 1); |
| 723 |
} |
| 724 |
|
| 725 |
static int |
| 726 |
test_dec_locator_event(MENU_ARGS) |
| 727 |
{ |
| 728 |
show_dec_locator_events(PASS_ARGS, 2, FALSE); |
| 729 |
return MENU_HOLD; |
| 730 |
} |
| 731 |
|
| 732 |
static int |
| 733 |
test_dec_locator_events(MENU_ARGS) |
| 734 |
{ |
| 735 |
show_dec_locator_events(PASS_ARGS, 1, FALSE); |
| 736 |
return MENU_HOLD; |
| 737 |
} |
| 738 |
|
| 739 |
static int |
| 740 |
test_dec_locator_event_p(MENU_ARGS) |
| 741 |
{ |
| 742 |
show_dec_locator_events(PASS_ARGS, 2, TRUE); |
| 743 |
return MENU_HOLD; |
| 744 |
} |
| 745 |
|
| 746 |
static int |
| 747 |
test_dec_locator_events_p(MENU_ARGS) |
| 748 |
{ |
| 749 |
show_dec_locator_events(PASS_ARGS, 1, TRUE); |
| 750 |
return MENU_HOLD; |
| 751 |
} |
| 752 |
|
| 753 |
static int |
| 754 |
test_dec_locator_rectangle(MENU_ARGS) |
| 755 |
{ |
| 756 |
show_dec_locator_events(PASS_ARGS, -2, FALSE); |
| 757 |
return MENU_HOLD; |
| 758 |
} |
| 759 |
|
| 760 |
static int |
| 761 |
test_dec_locator_unfiltered(MENU_ARGS) |
| 762 |
{ |
| 763 |
show_dec_locator_events(PASS_ARGS, 0, FALSE); |
| 764 |
return MENU_HOLD; |
| 765 |
} |
| 766 |
|
| 767 |
/* Any-Event Mouse Tracking */ |
| 768 |
static int |
| 769 |
test_mouse_any_event(MENU_ARGS) |
| 770 |
{ |
| 771 |
show_mouse_tracking(PASS_ARGS, "?1003"); |
| 772 |
return MENU_HOLD; |
| 773 |
} |
| 774 |
|
| 775 |
/* Button-Event Mouse Tracking */ |
| 776 |
static int |
| 777 |
test_mouse_button_event(MENU_ARGS) |
| 778 |
{ |
| 779 |
show_mouse_tracking(PASS_ARGS, "?1002"); |
| 780 |
return MENU_HOLD; |
| 781 |
} |
| 782 |
|
| 783 |
/* Mouse Highlight Tracking */ |
| 784 |
static int |
| 785 |
test_mouse_hilite(MENU_ARGS) |
| 786 |
{ |
| 787 |
const int first = 10; |
| 788 |
const int last = 20; |
| 789 |
unsigned b; |
| 790 |
unsigned y = 0, x = 0; |
| 791 |
unsigned start_x, end_x; |
| 792 |
unsigned start_y, end_y; |
| 793 |
unsigned mouse_y, mouse_x; |
| 794 |
int report_row, report_col; |
| 795 |
|
| 796 |
loop: |
| 797 |
vt_move(1, 1); |
| 798 |
ed(0); |
| 799 |
println(the_title); |
| 800 |
show_mousehelp(); |
| 801 |
println("Mouse events will be marked with the button number."); |
| 802 |
printxx("Highlighting range is [%d..%d)\n", first, last); |
| 803 |
show_hilite(first, last); |
| 804 |
|
| 805 |
sm("?1001"); |
| 806 |
fflush(stdout); |
| 807 |
set_tty_raw(TRUE); |
| 808 |
set_tty_echo(FALSE); |
| 809 |
|
| 810 |
pause_replay(); |
| 811 |
for (;;) { |
| 812 |
char *report = instr(); |
| 813 |
|
| 814 |
if (isQuit(*report)) { |
| 815 |
break; |
| 816 |
} else if (isReport(*report)) { |
| 817 |
show_mousemodes(); |
| 818 |
goto loop; |
| 819 |
} else if (isClear(*report)) { |
| 820 |
goto loop; |
| 821 |
} |
| 822 |
|
| 823 |
show_hilite(first, last); |
| 824 |
ToData(1); |
| 825 |
vt_el(2); |
| 826 |
chrprint2(report, report_row, report_col); |
| 827 |
|
| 828 |
if (parse_mouse_M(report, &b, &x, &y) != NULL) { |
| 829 |
b &= 7; |
| 830 |
if (b != 3) { |
| 831 |
/* send the xterm the highlighting range (it MUST be done first) */ |
| 832 |
do_csi("1;%u;%u;%d;%d;T", x, y, 10, 20); |
| 833 |
/* now, show the mouse-click */ |
| 834 |
if (b < 3) |
| 835 |
b++; |
| 836 |
show_click(y, x, (int) (b + '0')); |
| 837 |
} |
| 838 |
/* interpret the event */ |
| 839 |
ToData(2); |
| 840 |
vt_el(2); |
| 841 |
show_result("tracking: code 0x%x (%d,%d)", b, y, x); |
| 842 |
fflush(stdout); |
| 843 |
} else if (parse_mouse_T(report, |
| 844 |
&start_x, &start_y, |
| 845 |
&end_x, &end_y, |
| 846 |
&mouse_x, &mouse_y)) { |
| 847 |
/* interpret the event */ |
| 848 |
ToData(2); |
| 849 |
vt_el(2); |
| 850 |
|
| 851 |
show_result("done: start(%d,%d), end(%d,%d), mouse(%d,%d)", |
| 852 |
start_y, start_x, |
| 853 |
end_y, end_x, |
| 854 |
mouse_y, mouse_x); |
| 855 |
if (start_y != y |
| 856 |
|| start_x != x) |
| 857 |
show_click(start_y, start_x, 's'); |
| 858 |
if (end_y != y |
| 859 |
|| end_x != x) |
| 860 |
show_click(end_y, end_x, 'e'); |
| 861 |
if (mouse_y != y |
| 862 |
|| mouse_x != x) |
| 863 |
show_click(mouse_y, mouse_x, 'm'); |
| 864 |
} else if (parse_mouse_t(report, &end_x, &end_y)) { |
| 865 |
/* interpret the event */ |
| 866 |
ToData(2); |
| 867 |
vt_el(2); |
| 868 |
|
| 869 |
show_result("done: end(%d,%d)", |
| 870 |
end_y, end_x); |
| 871 |
if (end_y != y |
| 872 |
|| end_x != x) |
| 873 |
show_click(end_y, end_x, 'e'); |
| 874 |
} |
| 875 |
} |
| 876 |
resume_replay(); |
| 877 |
|
| 878 |
rm("?1001"); |
| 879 |
fflush(stdout); |
| 880 |
restore_ttymodes(); |
| 881 |
|
| 882 |
vt_move(max_lines - 2, 1); |
| 883 |
return MENU_HOLD; |
| 884 |
} |
| 885 |
|
| 886 |
/* Normal Mouse Tracking */ |
| 887 |
static int |
| 888 |
test_mouse_normal(MENU_ARGS) |
| 889 |
{ |
| 890 |
show_mouse_tracking(PASS_ARGS, "?1000"); |
| 891 |
return MENU_HOLD; |
| 892 |
} |
| 893 |
|
| 894 |
/* X10 Mouse Compatibility */ |
| 895 |
static int |
| 896 |
test_X10_mouse(MENU_ARGS) |
| 897 |
{ |
| 898 |
unsigned b, x, y; |
| 899 |
int report_row, report_col; |
| 900 |
|
| 901 |
pause_replay(); |
| 902 |
loop: |
| 903 |
vt_move(1, 1); |
| 904 |
ed(0); |
| 905 |
println(the_title); |
| 906 |
show_mousehelp(); |
| 907 |
println("Mouse events will be marked with the button number."); |
| 908 |
|
| 909 |
sm("?9"); |
| 910 |
fflush(stdout); |
| 911 |
set_tty_raw(TRUE); |
| 912 |
set_tty_echo(FALSE); |
| 913 |
|
| 914 |
for (;;) { |
| 915 |
char *report = instr(); |
| 916 |
if (isQuit(*report)) { |
| 917 |
break; |
| 918 |
} else if (isReport(*report)) { |
| 919 |
show_mousemodes(); |
| 920 |
goto loop; |
| 921 |
} else if (isClear(*report)) { |
| 922 |
goto loop; |
| 923 |
} |
| 924 |
ToData(0); |
| 925 |
vt_el(2); |
| 926 |
chrprint2(report, report_row, report_col); |
| 927 |
if (parse_mouse_M(report, &b, &x, &y) != NULL) { |
| 928 |
cup((int) y, (int) x); |
| 929 |
tprintf("%u", b + 1); |
| 930 |
vt_move((int) y, (int) x); |
| 931 |
fflush(stdout); |
| 932 |
} |
| 933 |
} |
| 934 |
resume_replay(); |
| 935 |
|
| 936 |
rm("?9"); |
| 937 |
fflush(stdout); |
| 938 |
restore_ttymodes(); |
| 939 |
|
| 940 |
vt_move(max_lines - 2, 1); |
| 941 |
return MENU_HOLD; |
| 942 |
} |
| 943 |
|
| 944 |
/* |
| 945 |
* DEC locator events are implemented on DECterm, to emulate VT220. |
| 946 |
*/ |
| 947 |
static int |
| 948 |
tst_dec_locator_events(MENU_ARGS) |
| 949 |
{ |
| 950 |
static char pixel_screensize[80]; |
| 951 |
/* *INDENT-OFF* */ |
| 952 |
static MENU my_menu[] = { |
| 953 |
{ "Exit", NULL }, |
| 954 |
{ "One-Shot", test_dec_locator_event }, |
| 955 |
{ "Repeated", test_dec_locator_events }, |
| 956 |
{ "One-Shot (pixels)", test_dec_locator_event_p }, |
| 957 |
{ "Repeated (pixels)", test_dec_locator_events_p }, |
| 958 |
{ "Filter Rectangle", test_dec_locator_rectangle }, |
| 959 |
{ "Filter Rectangle (unfiltered)", test_dec_locator_unfiltered }, |
| 960 |
{ pixel_screensize, get_screensize }, |
| 961 |
{ "", NULL } |
| 962 |
}; |
| 963 |
/* *INDENT-ON* */ |
| 964 |
|
| 965 |
chars_high = 24; |
| 966 |
chars_wide = 80; |
| 967 |
pixels_high = -1; |
| 968 |
pixels_wide = -1; |
| 969 |
do { |
| 970 |
vt_clear(2); |
| 971 |
__(title(0), println("DEC Locator Events")); |
| 972 |
__(title(2), println("Choose test type:")); |
| 973 |
if (pixels_high > 0 && pixels_wide > 0) { |
| 974 |
sprintf(pixel_screensize, |
| 975 |
"XFree86 xterm: screensize %dx%d chars, %dx%d pixels", |
| 976 |
chars_high, chars_wide, pixels_high, pixels_wide); |
| 977 |
} else { |
| 978 |
strcpy(pixel_screensize, "XFree86 xterm: screensize unknown"); |
| 979 |
} |
| 980 |
} while (menu(my_menu)); |
| 981 |
return MENU_NOHOLD; |
| 982 |
} |
| 983 |
|
| 984 |
/* |
| 985 |
* Cycle through the different flavors of mouse-coordinates which are |
| 986 |
* recognized by xterm. |
| 987 |
*/ |
| 988 |
static int |
| 989 |
toggle_ExtCoords(MENU_ARGS) |
| 990 |
{ |
| 991 |
int old_ExtCoords = do_ExtCoords; |
| 992 |
char buffer[80]; |
| 993 |
|
| 994 |
do_ExtPixels = FALSE; |
| 995 |
switch (do_ExtCoords) { |
| 996 |
case cUTF: |
| 997 |
do_ExtCoords = cSGR; |
| 998 |
break; |
| 999 |
case cSGR: |
| 1000 |
do_ExtCoords = cURX; |
| 1001 |
break; |
| 1002 |
case cURX: |
| 1003 |
if (can_do_pixels()) { |
| 1004 |
do_ExtCoords = cSGRp; |
| 1005 |
do_ExtPixels = TRUE; |
| 1006 |
} else { |
| 1007 |
do_ExtCoords = cDFT; |
| 1008 |
} |
| 1009 |
break; |
| 1010 |
case cSGRp: |
| 1011 |
do_ExtCoords = cDFT; |
| 1012 |
break; |
| 1013 |
default: |
| 1014 |
do_ExtCoords = cUTF; |
| 1015 |
break; |
| 1016 |
} |
| 1017 |
|
| 1018 |
if (LOG_ENABLED) { |
| 1019 |
fprintf(log_fp, NOTE_STR "toggle from %s to %s\n", |
| 1020 |
nameOfExtCoords(old_ExtCoords), |
| 1021 |
nameOfExtCoords(do_ExtCoords)); |
| 1022 |
} |
| 1023 |
|
| 1024 |
if (old_ExtCoords) { |
| 1025 |
sprintf(buffer, "?%d", old_ExtCoords); |
| 1026 |
rm(buffer); |
| 1027 |
} |
| 1028 |
|
| 1029 |
if (do_ExtCoords) { |
| 1030 |
sprintf(buffer, "?%d", do_ExtCoords); |
| 1031 |
sm(buffer); |
| 1032 |
} |
| 1033 |
|
| 1034 |
return MENU_NOHOLD; |
| 1035 |
} |
| 1036 |
|
| 1037 |
/* |
| 1038 |
* The "focus-event" control adds an event when entering/leaving the window. |
| 1039 |
*/ |
| 1040 |
static int |
| 1041 |
toggle_FocusEvent(MENU_ARGS) |
| 1042 |
{ |
| 1043 |
do_FocusEvent = !do_FocusEvent; |
| 1044 |
if (do_FocusEvent) |
| 1045 |
sm("?1004"); |
| 1046 |
else |
| 1047 |
rm("?1004"); |
| 1048 |
fflush(stdout); |
| 1049 |
return MENU_NOHOLD; |
| 1050 |
} |
| 1051 |
|
| 1052 |
/* |
| 1053 |
* xterm generally implements mouse escape sequences (except for dtterm and |
| 1054 |
* DECterm). XFree86 xterm (and newer) implements additional controls. |
| 1055 |
*/ |
| 1056 |
int |
| 1057 |
tst_mouse(MENU_ARGS) |
| 1058 |
{ |
| 1059 |
static char txt_Utf8Mouse[80]; |
| 1060 |
static char txt_FocusEvent[80]; |
| 1061 |
/* *INDENT-OFF* */ |
| 1062 |
static MENU my_menu[] = { |
| 1063 |
{ "Exit", NULL }, |
| 1064 |
{ txt_Utf8Mouse, toggle_ExtCoords }, |
| 1065 |
{ txt_FocusEvent, toggle_FocusEvent }, |
| 1066 |
{ "X10 Mouse Compatibility", test_X10_mouse }, |
| 1067 |
{ "Normal Mouse Tracking", test_mouse_normal }, |
| 1068 |
{ "Mouse Highlight Tracking", test_mouse_hilite }, |
| 1069 |
{ "Mouse Any-Event Tracking (XFree86 xterm)", test_mouse_any_event }, |
| 1070 |
{ "Mouse Button-Event Tracking (XFree86 xterm)", test_mouse_button_event }, |
| 1071 |
{ "DEC Locator Events (DECterm)", tst_dec_locator_events }, |
| 1072 |
{ "", NULL } |
| 1073 |
}; |
| 1074 |
/* *INDENT-ON* */ |
| 1075 |
|
| 1076 |
do { |
| 1077 |
vt_clear(2); |
| 1078 |
sprintf(txt_Utf8Mouse, "Mode: %s coordinates", nameOfExtCoords(do_ExtCoords)); |
| 1079 |
sprintf(txt_FocusEvent, "Mode: %sFocus-event", |
| 1080 |
do_FocusEvent ? "" : "no"); |
| 1081 |
__(title(0), println("XTERM mouse features")); |
| 1082 |
__(title(2), println("Choose test type:")); |
| 1083 |
} while (menu(my_menu)); |
| 1084 |
return MENU_NOHOLD; |
| 1085 |
} |