about summary refs log tree commit diff stats
path: root/termbox/input.inl
blob: 618e0b280e5d418cdf9c0c946e1abe58ee6f9b7e (plain) (blame)
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
// if s1 starts with s2 returns true, else false
// len is the length of s1
// s2 should be null-terminated
static bool starts_with(const char *s1, int len, const char *s2)
{
  int n = 0;
  while (*s2 && n < len) {
    if (*s1++ != *s2++)
      return false;
    n++;
  }
  return *s2 == 0;
}

// convert escape sequence to event, and return consumed bytes on success (failure == 0)
static int parse_escape_seq(struct tb_event *event, const char *buf, int len)
{
  if (len >= 6 && starts_with(buf, len, "\033[M")) {

    switch (buf[3] & 3) {
    case 0:
      if (buf[3] == 0x60)
        event->key = TB_KEY_MOUSE_WHEEL_UP;
      else
        event->key = TB_KEY_MOUSE_LEFT;
      break;
    case 1:
      if (buf[3] == 0x61)
        event->key = TB_KEY_MOUSE_WHEEL_DOWN;
      else
        event->key = TB_KEY_MOUSE_MIDDLE;
      break;
    case 2:
      event->key = TB_KEY_MOUSE_RIGHT;
      break;
    case 3:
      event->key = TB_KEY_MOUSE_RELEASE;
      break;
    default:
      return -6;
    }
    event->type = TB_EVENT_MOUSE; // TB_EVENT_KEY by default

    // the coord is 1,1 for upper left
    event->x = (uint8_t)buf[4] - 1 - 32;
    event->y = (uint8_t)buf[5] - 1 - 32;

    return 6;
  }

  // it's pretty simple here, find 'starts_with' match and return
  // success, else return failure
  int i;
  for (i = 0; keys[i]; i++) {
    if (starts_with(buf, len, keys[i])) {
      event->ch = 0;
      event->key = 0xFFFF-i;
      return strlen(keys[i]);
    }
  }
  return 0;
}

static bool extract_event(struct tb_event *event, struct bytebuffer *inbuf)
{
  const char *buf = inbuf->buf;
  const int len = inbuf->len;
  if (len == 0)
    return false;

  if (buf[0] == '\033') {
    int n = parse_escape_seq(event, buf, len);
    if (n != 0) {
      bool success = true;
      if (n < 0) {
        success = false;
        n = -n;
      }
      bytebuffer_truncate(inbuf, n);
      return success;
    } else {
      // it's not escape sequence; assume it's esc
      event->ch = 0;
      event->key = TB_KEY_ESC;
      bytebuffer_truncate(inbuf, 1);
      return true;
    }
  }

  // if we're here, this is not an escape sequence and not an alt sequence
  // so, it's a FUNCTIONAL KEY or a UNICODE character

  // first of all check if it's a functional key
  if ((unsigned char)buf[0] <= TB_KEY_SPACE ||
      (unsigned char)buf[0] == TB_KEY_BACKSPACE2)
  {
    // fill event, pop buffer, return success */
    event->ch = 0;
    event->key = (uint16_t)buf[0];
    bytebuffer_truncate(inbuf, 1);
    return true;
  }

  // feh... we got utf8 here

  // check if there is all bytes
  if (len >= tb_utf8_char_length(buf[0])) {
    /* everything ok, fill event, pop buffer, return success */
    tb_utf8_char_to_unicode(&event->ch, buf);
    event->key = 0;
    bytebuffer_truncate(inbuf, tb_utf8_char_length(buf[0]));
    return true;
  }

  // event isn't recognized, perhaps there is not enough bytes in utf8
  // sequence
  return false;
}
pan class="w"> strict; use warnings; our $fileinfo = $ARGV[0]; our $verbose = 0; my $line; my @lines; while(<STDIN>) { $line = $_; push @lines,$line; } my $oline = ""; my $fmtline = "%s"; foreach $line (@lines) { my $newline = ""; foreach my $seg (split(/(.\x08.)/,$line)) { my $newseg = $seg; $newseg =~ m/^(.)\x08(.)$/; if (!defined($1) || !defined($2)) { $newline .= $seg; next; } if ($1 eq $2) { $newline .= "${2}"; next; } if ($1 eq "_") { $newline .= "${2}"; next; } $newline .= $seg; next; } if ($verbose > 0) { printf STDERR "==> text{bf,it}\n line: <%s>\nnewline: <%s>\n",$line,$newline; } $line = $newline; $line =~ m/(.)\x08/; if (defined($1)) { printf STDERR "Removing %s\\x08\n",$1; } $line =~ s/.\x08//g; # combine adjacent entries foreach my $macro (("textbf", "textit")) { $oline = ""; while ($oline ne $line) { #printf STDERR "combine adjacent\n"; $oline = $line; $line =~ s/\xab\\${macro}\{([^\}]*)\}\xbb\xab\\${macro}\{([^\}]*)\}\xbb/\xab\\${macro}\{$1$2\}\xbb/g; } } # combine space separated foreach my $macro (("textbf")) { #printf STDERR "combine space\n"; $oline = ""; while ($oline ne $line) { $oline = $line; $line =~ s/\xab\\${macro}\{([^\}]*)\}\xbb[ ]+\xab\\${macro}\{([^\}]*)\}\xbb/\xab\\${macro}\{$1 $2\}\xbb/g; } } # do the substitution one at a time to be sure to add all man pages, not just the last ones per line. # XXX provide an exceptions list, audio(9) has mono(1) and stereo(2) # XXX references, which are _not_ man pages $oline = ""; while ($oline ne $line) { $oline=$line; $line =~ s/\{(http|ftp|https):\/\/(.*)\}/ $1:\/\/$2 /; if (0) { if ($line =~ m/ ([a-z][a-z0-9\.\-\_]*)\(([1-9])\)([,\.\) ])/) { my $quote = texquote($1); $line =~ s/ ([a-z][a-z0-9\.\-\_]*)\(([1-9])\)([,\.\) ])/ \xab\\man{$quote}{$2}\xbb$3/; } if ($line =~ m/ ([a-z][a-z0-9\.\-\_]*)\(([1-9])\)$/) { my $quote = texquote($1); $line =~ s/ ([a-z][a-z0-9\.\-\_]*)\(([1-9])\)$/ \xab\\man{$quote}{$2}\xbb/; } } } my @macros = ("textbf","textit","man","href"); # quote arguments for tex foreach my $macro (@macros) { my $newline = ""; foreach my $seg (split(/(\xab\\${macro}\{[^\xbb]*\}\xbb)/,$line)) { #printf STDERR "quote args\n"; my $newseg = $seg; # check for nesting first; we only want to escape the # inner most argument, process nested macro if it has a nested macro # since the nested macro won't catch in the other regex cases my $foundnest = 0; foreach my $nest (@macros) { if ($macro eq $nest) { next; } $newseg =~ m/^\xab\\${macro}\{[ ]*\\${nest}\{([^\xbb]*)\}\{([^\xbb]*)\}\}\xbb$/; if (defined($2)) { $foundnest = 1; $newline .= "\xab\\${macro}\{\\${nest}\{".texquote($1)."\}\{".texquote(${2})."\}\}\xbb"; last; } $newseg =~ m/^\xab\\${macro}\{[ ]*\\${nest}\{([^\xbb]*)\}\}\xbb$/; if (defined($1)) { $foundnest = 1; $newline .= "\xab\\${macro}\{\\${nest}\{".texquote($1)."\}\}\xbb"; last; } } if ($foundnest > 0) { next; } # check for 2 args first $newseg =~ m/^\xab\\${macro}\{([^\xbb]*)\}\{([^\xbb]*)\}\xbb$/; if (defined($2)) { $newline .= "\xab\\${macro}\{".texquote($1)."\}\{".texquote(${2})."\}\xbb"; next; } $newseg =~ m/^\xab\\${macro}\{([^\xbb]*)\}\xbb$/; if (defined($1)) { $newline .= "\xab\\${macro}\{".texquote($1)."\}\xbb"; next; } $newline .= $seg; } $line = $newline; } printf $fmtline,$line; } 1; sub texquote { my ($text) = @_; my ($ret) = ""; my ($esctest) = ""; my ($escbase) = "BaCkSlAsH"; my ($esccount) = 0; #$verbose++; if ($verbose > 0) { printf STDERR "\ntexquote: '%s' -> ",$text; } if ($text =~ m/\\/) { $esctest=sprintf "%s%d",$escbase,$esccount++; while ($text =~ m/$esctest/) { $esctest=sprintf "%s%d",$escbase,$esccount++; } $text =~ s/\\/$esctest/g; if ($verbose > 0) { printf STDERR "'%s' -> ",$text; } } $text =~ s/([%\{\}_#\&\$\^])/\\$1/g; $text =~ s/([<>\|\*~])/\{\$$1\$\}/g; if ($esccount > 0) { $text =~ s/$esctest/\$\\backslash\$/g; } if ($verbose > 0) { printf STDERR "'%s'\n",$text; } #$verbose--; return $text; }