about summary refs log tree commit diff stats
path: root/apps/hex
Commit message (Expand)AuthorAgeFilesLines
* 6597Kartik Agaram2020-06-291-0/+0
* 6596Kartik Agaram2020-06-291-0/+0
* 6595Kartik Agaram2020-06-291-0/+0
* 6594 - start standardizing the meaning of 'print'Kartik Agaram2020-06-291-0/+0
* 6528Kartik Agaram2020-06-151-0/+0
* 6520 - new app: parse-intKartik Agaram2020-06-141-0/+0
* 6508 - support null exit-descriptorKartik Agaram2020-06-101-0/+0
* 6507 - use syscall names everywhereKartik Agaram2020-06-101-0/+0
* 6409 - primitives for text-mode UIsKartik Agaram2020-05-271-0/+0
* 6406 - primitive 'copy-handle'Kartik Agaram2020-05-251-0/+0
* 6382 - re-enable mu.subx in CIKartik Agaram2020-05-221-0/+0
* update binariesKartik Agaram2020-05-221-0/+0
* handle nulls in lookupKartik Agaram2020-05-181-0/+0
* support 'fake' handles allocated staticallyKartik Agaram2020-05-181-0/+0
* support 'fake' handles allocated staticallyKartik Agaram2020-05-181-0/+0
* Rebuild phases of self-hosted SubX translatorKartik Agaram2020-05-181-0/+0
* 6208Kartik Agaram2020-04-221-0/+0
* 6182 - start of support for safe handlesKartik Agaram2020-04-031-0/+0
* 6181Kartik Agaram2020-04-031-0/+0
* 6153 - switch 'main' to use Mu stringsKartik Agaram2020-03-151-0/+0
* 6094 - new 'compute-offset' instructionKartik Agaram2020-03-071-0/+0
* 6085Kartik Agaram2020-03-061-0/+0
* 6083Kartik Agaram2020-03-061-0/+0
* 6070Kartik Agaram2020-02-291-0/+0
* 6064Kartik Agaram2020-02-271-0/+0
* 6000 - clean up after no-local branchesKartik Agaram2020-02-091-0/+0
* 5999Kartik Agaram2020-02-091-0/+0
* 5948 - branching to named blocksKartik Agaram2020-01-291-0/+0
* 5933Kartik Agaram2020-01-271-0/+0
* 5898 - strengthen slice-empty? checkKartik Agaram2020-01-191-0/+0
* 5887 - reorganize libraryKartik Agaram2020-01-141-0/+0
* 5847 - literal inputsKartik Agaram2019-12-311-0/+0
* 5804Kartik Agaram2019-12-081-0/+0
* 5803Kartik Agaram2019-12-071-0/+0
* 5792Kartik Agaram2019-12-051-0/+0
* 5782 - fix a widespread bug with Heap-sizeKartik Agaram2019-11-301-0/+0
* 5778Kartik Agaram2019-11-291-0/+0
* 5769 - support uppercase hex in SubXKartik Agaram2019-11-281-0/+0
* 5765Kartik Agaram2019-11-261-0/+0
* 5752Kartik Agaram2019-11-181-0/+0
* 5714Kartik Agaram2019-10-251-0/+0
* 5687Kartik Agaram2019-09-231-0/+0
* 5676Kartik Agaram2019-09-191-0/+0
* 5675 - move helpers from subx-common into layersKartik Agaram2019-09-191-0/+0
* 5673 - standardize a few knobsKartik Agaram2019-09-191-0/+0
* 5672 - move hex out of appsKartik Agaram2019-09-191-0/+0
* 5669Kartik Agaram2019-09-191-0/+0
* 5668 - start reorg to permit syntax sugar in layersKartik Agaram2019-09-191-0/+0
* 5647 - experimental support for swapping OSKartik Agaram2019-09-111-0/+0
* 5630Kartik Agaram2019-09-061-0/+0
ref='#n17'>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;
}