1 #ifndef GUARD_TERMKEY_INTERNAL_H_
2 #define GUARD_TERMKEY_INTERNAL_H_
3
4 #include "termkey.h"
5
6 #include <stdint.h>
7 #include <termios.h>
8
9 struct TermKeyDriver
10 {
11 const char *name;
12 void *(*new_driver)(TermKey *tk, const char *term);
13 void (*free_driver)(void *info);
14 int (*start_driver)(TermKey *tk, void *info);
15 int (*stop_driver)(TermKey *tk, void *info);
16 TermKeyResult (*peekkey)(TermKey *tk, void *info, TermKeyKey *key, int force, size_t *nbytes);
17 };
18
19 struct keyinfo {
20 TermKeyType type;
21 TermKeySym sym;
22 int modifier_mask;
23 int modifier_set;
24 };
25
26 struct TermKeyDriverNode;
27 struct TermKeyDriverNode {
28 struct TermKeyDriver *driver;
29 void *info;
30 struct TermKeyDriverNode *next;
31 };
32
33 struct TermKey {
34 int fd;
35 int flags;
36 int canonflags;
37 unsigned char *buffer;
38 size_t buffstart; // First offset in buffer
39 size_t buffcount; // NUMBER of entires valid in buffer
40 size_t buffsize; // Total malloc'ed size
41 size_t hightide; /* Position beyond buffstart at which peekkey() should next start
42 * normally 0, but see also termkey_interpret_csi */
43
44 struct termios restore_termios;
45 char restore_termios_valid;
46
47 TermKey_Terminfo_Getstr_Hook *ti_getstr_hook;
48 void *ti_getstr_hook_data;
49
50 int waittime; // msec
51
52 char is_closed;
53 char is_started;
54
55 int nkeynames;
56 const char **keynames;
57
58 // There are 32 C0 codes
59 struct keyinfo c0[32];
60
61 struct TermKeyDriverNode *drivers;
62
63 // Now some "protected" methods for the driver to call but which we don't
64 // want exported as real symbols in the library
65 struct {
66 void (*emit_codepoint)(TermKey *tk, long codepoint, TermKeyKey *key);
67 TermKeyResult (*peekkey_simple)(TermKey *tk, TermKeyKey *key, int force, size_t *nbytes);
68 TermKeyResult (*peekkey_mouse)(TermKey *tk, TermKeyKey *key, size_t *nbytes);
69 } method;
70 };
71
termkey_key_get_linecol(const TermKeyKey * key,int * line,int * col)72 static inline void termkey_key_get_linecol(const TermKeyKey *key, int *line, int *col)
73 {
74 if(col)
75 *col = (unsigned char)key->code.mouse[1] | ((unsigned char)key->code.mouse[3] & 0x0f) << 8;
76
77 if(line)
78 *line = (unsigned char)key->code.mouse[2] | ((unsigned char)key->code.mouse[3] & 0x70) << 4;
79 }
80
termkey_key_set_linecol(TermKeyKey * key,int line,int col)81 static inline void termkey_key_set_linecol(TermKeyKey *key, int line, int col)
82 {
83 if(line > 0xfff)
84 line = 0xfff;
85
86 if(col > 0x7ff)
87 col = 0x7ff;
88
89 key->code.mouse[1] = (line & 0x0ff);
90 key->code.mouse[2] = (col & 0x0ff);
91 key->code.mouse[3] = (line & 0xf00) >> 8 | (col & 0x300) >> 4;
92 }
93
94 extern struct TermKeyDriver termkey_driver_csi;
95 extern struct TermKeyDriver termkey_driver_ti;
96
97 #endif
98