1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
|
/*
* Copyright (c) 1998 - 1999, 2001 Karel Zak "Zakkr" <zakkr@zf.jcu.cz>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* $Id: key.c,v 1.2 2001/01/02 14:16:15 zakkr Exp $
*/
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h> /* struct timeval */
#include <sys/select.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <time.h>
#include <stdlib.h>
#include "aca.h"
#ifdef HAVE_MOUSE
#include "mouse.h"
#endif
#define GET_TIME(tv) (gettimeofday(&tv, (struct timezone *)NULL))
ACA_BASIC_KEYS basic_keys[] = {
#include "key_define.h"
};
/* learn key buffer */
typedef struct {
int key;
char seq[33];
} learn_buff;
char *GL_term_name;
char GL_seq_buff[256];
int GL_last_key;
int GL_current_key;
int GL_inlearn_mode = FALSE;
static int input_fd;
static int learn_std_char = FALSE;
static learn_buff lbuff[21];
INI_SectionData ini_keys_data[MAX_BASIC_KEYS+2];
void aca_keypad(int x)
{
#if DEBUG==1
if (x) _D("KEYPAD: true");
else _D("KEYPAD: flase");
#endif
keypad(stdscr, (aca.keypad = x));
}
void aca_dump_keys(FILE *f, ACA_BASIC_KEYS *k)
{
_D( " aca_dump_keys() ");
if (k->seq)
fprintf(f, "%s=%s\n", keyname(k->curses), k->seq);
}
int learn_keydump_check()
{
ACA_BASIC_KEYS *k;
int re=FALSE;
_D( " learn_keydump_check()");
for(k=basic_keys; k->curses!=0; k++) {
if (k->learn && k->seq)
re = TRUE;
}
return re;
}
int isfunckey( int key )
{
ACA_BASIC_KEYS *k;
_D( " isfunckey()");
for(k=basic_keys; k->curses!=0; k++) {
if (k->curses == key)
return TRUE;
}
return FALSE;
}
int set_learn_key(char *name, char *seq)
{
int re=FALSE;
ACA_BASIC_KEYS *k;
_D( " set_learn_key()");
for(k=basic_keys; k->curses!=0; k++) {
if (!strcmp(keyname(k->curses), name)) {
k->seq = seq;
k->learn = TRUE;
re = TRUE;
if (isprint(seq[0]))
learn_std_char = TRUE;
}
}
return re;
}
static int load_keys(INI_Section *sect)
{
INI_SectionData *sect_d;
int re=FALSE;
_D( " load_keys()");
for(sect_d=sect->data; sect_d->value != NULL; sect_d++) {
if (!sect_d->content)
continue;
if (set_learn_key(sect_d->value, sect_d->content))
re = TRUE;
}
return re;
}
void load_seqences()
{
ACA_BASIC_KEYS *k;
_D( " load_sequences() ");
for(k=basic_keys; k->curses!=0; k++) {
if (k->learn)
continue;
if ((k->seq = tigetstr(k->capname)) == (char *)-1 )
k->seq = SeqN;
k->learn = FALSE;
}
}
void init_keys_ini(INI_Section *sect)
{
ACA_BASIC_KEYS *k;
INI_SectionData *sect_d;
_D( " init_keys_ini()");
for(sect_d=ini_keys_data, k=basic_keys; k->curses!=0; k++, sect_d++) {
sect_d->value = (char *) keyname(k->curses);
sect_d->content = chN;
sect_d->value_call_fn = NULL;
}
++sect_d;
sect_d->value = chN;
sect_d->content = chN;
sect->data = ini_keys_data;
sect->name = GL_term_name;
sect->flag |= SECT_LOAD;
}
void init_keys(aca_INI *ini)
{
INI_Section *sect;
_D( " init_keys() ");
input_fd = fileno(stdin);
if (!aca.keypad)
load_seqences();
if (!ini) return;
if ((sect = INI_section(ini, GL_term_name))) {
if (aca.keypad)
load_seqences();
if (load_keys(sect))
aca_keypad(FALSE);
}
}
int sig_check()
{
_D( " sig_check()");
/* GL_inter.. is set to FALSE in get_k */
if (GL_interrupt_flag) {
_D( " sig_check(): SIG. INTERRUPT WAS CAUGHT");
GL_interrupt_flag = FALSE;
return KEY_F(10);
}
if (GL_winch_flag) {
_D( " sig_check(): SIG. WINCH WAS CAUGHT");
GL_winch_flag = FALSE;
screen_resizer();
return K_SCREEN_RESIZED;
}
return FALSE;
}
int timeout_calls(int current)
{
int re;
_D( " timeout_calls()");
if ((re = sig_check()))
return re;
if (aca.extra_timeout_call)
aca.extra_timeout_call_fn(current);
return RE_OK;
}
static int getch_timer()
{
register int c, current=0, count=0;
_D( " getch_timer()");
if (aca.getch_timeout) {
current = aca.getch_timeout;
wtimeout(stdscr, current);
}
#ifdef HAVE_MOUSE
refresh();
while((c=aca_Gpm_Wgetch(stdscr)) == ERR || c == K_MOUSE_NOT) {
#else
while((c=getch()) == ERR) {
#endif
/* if mouse move (but not press) mouse_handler return
K_MOUSE_NOT and ACA go get up */
if (c==K_MOUSE_NOT) {
current = aca.getch_timeout;
count = 0;
}
if ((c=timeout_calls(current)) != RE_OK)
return c;
if (count < aca.fallasleep)
count += current;
else if (current < 3600000) /* max. is one hour */
current += aca.fallasleep_incr;
wtimeout(stdscr, current);
}
refresh();
return c;
}
/* Convert ctrl keys to curses key
(ctrl alternative keys are in acakey.h)
*/
static int convert_ctrl_keys(int c)
{
ACA_BASIC_KEYS *k;
_D( " convert_ctrl_keys()");
for(k=basic_keys; k->curses!=0; k++)
if (c == XCTRL(k->ctrl))
return k->curses;
return c;
}
static int convert_esc_keys(int c) {
_D( " convert_esc_keys()");
switch(c) {
case '1':
return KEY_F(1);
case '2':
return KEY_F(2);
case '3':
return KEY_F(3);
case '4':
return KEY_F(4);
case '5':
return KEY_F(5);
case '6':
return KEY_F(6);
case '7':
return KEY_F(7);
case '8':
return KEY_F(8);
case '9':
return KEY_F(9);
case '0':
return KEY_F(10);
case ESC_CHAR:
return KEY_F(10);
}
return c;
}
/* set *key - independent on keypad TRUE/FALSE
- if keypad is FALSE set first char of escape seq.
- return TRUE if *key is standard (independent on keypad) key
or FALSE if not
*/
static int getkey_independent(int *key)
{
_D( " getkey_independent()");
*key = getch_timer ();
if (*key=='\t' || *key==K_MOUSE_L || *key==K_MOUSE_M
|| *key==K_MOUSE_R || *key=='\n')
return TRUE;
if (*key != ESC_CHAR && T_XCTRL(*key)) {
*key = convert_ctrl_keys(*key);
return TRUE;
}
return FALSE;
}
/******
void clean_input ()
{
int c;
fd_set Read_FD_Set;
struct timeval endtime;
struct timeval timeout;
GET_TIME (endtime);
endtime.tv_usec += MAX_SEQTIME;
if (endtime.tv_usec > 1000000) {
endtime.tv_usec -= 1000000;
endtime.tv_sec++;
}
nodelay(stdscr, TRUE);
for (;;) {
while ((c = getch()) == ERR) {
GET_TIME (timeout);
timeout.tv_usec = endtime.tv_usec - timeout.tv_usec;
if (timeout.tv_usec < 0)
timeout.tv_sec++;
timeout.tv_sec = endtime.tv_sec - timeout.tv_sec;
if (timeout.tv_sec >= 0 && timeout.tv_usec > 0) {
FD_ZERO (&Read_FD_Set);
FD_SET (input_fd, &Read_FD_Set);
select (FD_SETSIZE, &Read_FD_Set, NULL, NULL, &timeout);
}
else
break;
}
if (c == ERR)
break;
}
nodelay(stdscr, FALSE);
}
*****/
static int getkey_after_esc(int *key)
{
int x;
_D( " getkey_after_esc()");
x = aca.keypad;
aca_keypad(FALSE);
notimeout(stdscr, TRUE);
*key = wgetch(stdscr);
notimeout(stdscr, FALSE);
aca_keypad(x);
if (*key != ERR)
*key = convert_esc_keys(*key);
else
return K_STAY;
return *key;
}
static int set_lastkey(int k)
{
_D( " set_lastkey()");
GL_last_key = GL_current_key;
GL_current_key = k;
return k;
}
int find_in_lbuff(char *seq)
{
int i;
_D( " find_in_lbuff()");
for(i=0; i<=MAX_BASIC_KEYS; i++) {
if (lbuff[i].key) {
if (!strcmp(lbuff[i].seq, seq))
return lbuff[i].key;
}
}
return FALSE;
}
int check_seq(char *buffer)
{
ACA_BASIC_KEYS *k;
int x;
_D(" check_seq()");
for(k=basic_keys; k->curses!=0; k++) {
if (GL_inlearn_mode) {
if ((x=find_in_lbuff(buffer)))
return x;
}
if (k->seq) {
if (!strcmp(k->seq, buffer))
return k->curses;
}
if (!GL_inlearn_mode) {
if (!k->learn && !strcmp(k->default_seq, buffer))
return k->curses;
}
if (aca.forcekey) {
if (!strcmp(k->default_seq, buffer))
return k->curses;
}
}
return RE_ERROR;
}
static void store_key (char *buffer, char **p, int c)
{
_D( " store_key()");
if (*p - buffer > 253)
return;
if (c == ESC_CHAR) {
*(*p)++ = ESC_CHAR;
}
else if (c < ' ') {
*(*p)++ = '^';
*(*p)++ = c + 'a' - 1;
}
else if (c == '^') {
*(*p)++ = '^';
*(*p)++ = '^';
}
else
*(*p)++ = (char) c;
}
/*
Read escape sequences of input_fd and write it to 'buffer',
and write to 'iskey' curses key (of sequencion) if this exist
in ACA basic_keys
*/
static char *makeseq (char *buffer, int *iskey)
{
#define MAX_SEQTIME 200000
fd_set Read_FD_Set;
struct timeval endtime;
struct timeval timeout;
char *p = buffer;
_D( " makeseq()");
store_key (buffer, &p, *iskey);
if (buffer[0] != ESC_CHAR) {
buffer[1] = '\0';
if ((*iskey = check_seq(buffer)) != RE_ERROR)
return buffer;
}
GET_TIME (endtime);
endtime.tv_usec += MAX_SEQTIME;
if (endtime.tv_usec > 1000000) {
endtime.tv_usec -= 1000000;
endtime.tv_sec++;
}
nodelay(stdscr, TRUE);
for (;;) {
while ((*iskey = getch ()) == ERR) {
GET_TIME (timeout);
timeout.tv_usec = endtime.tv_usec - timeout.tv_usec;
if (timeout.tv_usec < 0)
timeout.tv_sec++;
timeout.tv_sec = endtime.tv_sec - timeout.tv_sec;
if (timeout.tv_sec >= 0 && timeout.tv_usec > 0) {
int re=FALSE;
FD_ZERO (&Read_FD_Set);
FD_SET (input_fd, &Read_FD_Set);
_D( "\tmakeseq: CALL select()");
select (FD_SETSIZE, &Read_FD_Set, NULL, NULL, &timeout);
if ((re = sig_check())) {
*iskey = re;
nodelay(stdscr, FALSE);
return (char *) NULL;
}
}
else
break;
}
if (*iskey == ESC_CHAR || *iskey == ERR) /* == '\e' is next seq */
break;
store_key (buffer, &p, *iskey);
*p = '\0';
if ((*iskey = check_seq(buffer)) != RE_ERROR)
break;
}
*p = 0;
nodelay(stdscr, FALSE);
/* user press ESC - not seqencion */
if (buffer[1] == '\0' && buffer[0] == ESC_CHAR) {
*iskey = ESC_CHAR;
return (char *) NULL;
}
else
return buffer;
}
static int getkey_seq(int *key)
{
int x=*key;
_D(" getkey_seq");
GL_seq_buff[0] = '\0';
if (!isprint(*key))
makeseq(GL_seq_buff, key);
else if (learn_std_char) {
GL_seq_buff[0] = *key;
GL_seq_buff[1] = '\0';
*key = check_seq(GL_seq_buff);
}
_D(GL_seq_buff);
if (*key == ESC_CHAR || *key == RE_ERROR) {
*key = x;
return FALSE;
}
else
return TRUE;
}
int get_k()
{
int key;
_D( " get_k()");
if (getkey_independent(&key))
return set_lastkey(key);
if (!aca.keypad) {
if (getkey_seq(&key))
return set_lastkey(key);
}
if (key == ESC_CHAR) {
set_lastkey(ESC_CHAR);
return set_lastkey(getkey_after_esc(&key));
}
return set_lastkey(key);
}
void init_lbuff()
{
int i;
_D( " init_lbuff()");
for(i=0; i<=MAX_BASIC_KEYS; i++) {
lbuff[i].key = 0;
lbuff[i].seq[0] = '\0';
}
}
void set_lbuff(int key, char *seq)
{
int i;
_D( " set_lbuff()");
for(i=0; i<=21; i++) {
if (!lbuff[i].key) {
lbuff[i].key = key;
strncpy(lbuff[i].seq, seq, 32);
return;
}
}
}
void cpy_lbuff()
{
int i;
_D( " cpy_lbuff()");
for(i=0; i<=MAX_BASIC_KEYS; i++) {
if (lbuff[i].key)
set_learn_key((char *)keyname(lbuff[i].key), lbuff[i].seq);
}
}
void cpy_learn2ini(aca_INI *ini)
{
INI_Section *sect;
ACA_BASIC_KEYS *k;
_D( " cpy_learn2ini()");
sect = INI_section (ini, GL_term_name);
for(k=basic_keys; k->curses!=0; k++) {
if (k->learn)
INI_set_data(sect->data, (char *) keyname(k->curses), k->seq);
}
}
int learn_key(int key)
{
_D( " learn_key()");
get_k();
if (GL_seq_buff[0] != '\0') {
set_lbuff(key, GL_seq_buff);
return TRUE;
}
return FALSE;
}
|