aboutsummaryrefslogtreecommitdiff
path: root/termkey2-internal.h
blob: 5da33ffae52e67c68997bd43668fa425e6c6a118 (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
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
#ifndef TERMKEY2_INTERNAL_H
#define TERMKEY2_INTERNAL_H

#include "termkey2.h"

#include <stdint.h>
#include <termios.h>
#include <stdbool.h>
#include <iconv.h>

typedef struct termkey_driver termkey_driver_t;
struct termkey_driver
{
	const char *name;
	void *(*new_driver) (termkey_t *tk, const char *term);
	void (*free_driver) (void *info);
	int (*start_driver) (termkey_t *tk, void *info);
	int (*stop_driver) (termkey_t *tk, void *info);
	termkey_result_t (*peekkey) (termkey_t *tk,
		void *info, termkey_key_t *key, int force, size_t *nbytes);
};

typedef struct keyinfo keyinfo_t;
struct keyinfo
{
	termkey_type_t type;
	termkey_sym_t sym;
	int modifier_mask;
	int modifier_set;
};

typedef struct termkey_driver_node termkey_driver_node_t;
struct termkey_driver_node
{
	termkey_driver_t *driver;
	void *info;
	termkey_driver_node_t *next;
};

struct termkey
{
	int fd;
	int flags;
	int canonflags;

	unsigned char *buffer;
	size_t buffstart; // First offset in buffer
	size_t buffcount; // Number of entires valid in buffer
	size_t buffsize; // Total malloc'ed size

	// Position beyond buffstart at which peekkey() should next start.
	// Normally 0, but see also termkey_interpret_csi().
	size_t hightide;

	struct termios restore_termios;
	bool restore_termios_valid;

	int waittime; // In milliseconds

	bool is_closed; // We've received EOF
	bool is_started;

	int nkeynames;
	const char **keynames;

	keyinfo_t c0[32]; // There are 32 C0 codes
	iconv_t to_utf32_conv;
	iconv_t from_utf32_conv;
	termkey_driver_node_t *drivers;

	// Now some "protected" methods for the driver to call but which we don't
	// want exported as real symbols in the library
	struct
	{
		void (*emit_codepoint) (termkey_t *tk,
			uint32_t codepoint, termkey_key_t *key);
		termkey_result_t (*peekkey_simple) (termkey_t *tk,
			termkey_key_t *key, int force, size_t *nbytes);
		termkey_result_t (*peekkey_mouse) (termkey_t *tk,
			termkey_key_t *key, size_t *nbytes);
	}
	method;
};

static inline void
termkey_key_get_linecol (const termkey_key_t *key, int *line, int *col)
{
	if (col)
		*col = key->code.mouse.x;

	if (line)
		*line = key->code.mouse.y;
}

static inline void
termkey_key_set_linecol (termkey_key_t *key, int line, int col)
{
	if (line > UINT16_MAX)
		line = UINT16_MAX;

	if (col > UINT16_MAX)
		col = UINT16_MAX;

	key->code.mouse.x = col;
	key->code.mouse.y = line;
}

extern termkey_driver_t termkey_driver_csi;
extern termkey_driver_t termkey_driver_ti;

#endif  // ! TERMKEY2_INTERNAL_H