aboutsummaryrefslogtreecommitdiff
path: root/toys/example/demo_scankey.c
blob: cc625092da4470c93066e1c6eecc0e536099ad67 (plain)
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
/* demo_scankey.c - collate incoming ansi escape sequences.
 *
 * Copyright 2015 Rob Landley <rob@landley.net>
 *
 * TODO sigwinch

USE_DEMO_SCANKEY(NEWTOY(demo_scankey, 0, TOYFLAG_BIN))

config DEMO_SCANKEY
  bool "demo_scankey"
  default n
  help
    usage: demo_scankey

    Move a letter around the screen. Hit ESC to exit.
*/

#define FOR_demo_scankey
#include "toys.h"

void demo_scankey_main(void)
{
  time_t t[2];
  unsigned width, height, tick;
  char c = 'X', scratch[16];
  int key, x, y;

  t[0] = t[1] = x = tick = 0;
  memset(scratch, 0, 16);
  y = 1;

  sigatexit(tty_sigreset);  // Make ctrl-c restore tty
  tty_esc("?25l");          // hide cursor
  tty_esc("0m");            // reset color to default
  tty_esc("2J");            // Clear screen
  xset_terminal(1, 1, 0, 0); // Raw mode

  for (;;) {
    tty_jump(x, y);
    xputc(c);
    t[1&++tick] = time(0);
    if (t[0] != t[1]) terminal_probesize(&width, &height);
    // Don't block first time through, to force header print
    key = scan_key_getsize(scratch, -1*!!t[0], &width, &height);
    tty_jump(0, 0);
    printf("ESC to exit: ");
    // Print unknown escape sequence
    if (*scratch) {
      printf("key=[ESC");
      // Fetch rest of sequence after deviation, time gap determines end
      while (0<(key = scan_key_getsize(scratch, 0, &width, &height)))
        printf("%c", key);
      printf("] ");
    } else printf("key=%d ", key);
    printf("x=%d y=%d width=%d height=%d\033[K", x, y, width, height);
    fflush(0);

    if (key == -2) continue;
    if (key <= ' ') break;
    if (key>=256) {
      tty_jump(x, y);
      xputc(' ');

      key -= 256;
      if (key==KEY_UP) y--;
      else if (key==KEY_DOWN) y++;
      else if (key==KEY_RIGHT) x++;
      else if (key==KEY_LEFT) x--;
      else if (key==KEY_PGUP) y = 0;
      else if (key==KEY_PGDN) y = 999;
      else if (key==KEY_HOME) x = 0;
      else if (key==KEY_END) x = 999;
      if (y<1) y = 1;
      if (y>=height) y = height-1;
      if (x<0) x = 0;
      if (x>=width) x = width-1;
    } else c = key;
  }
  tty_reset();
}