aboutsummaryrefslogtreecommitdiff
path: root/sys/libterm/input.c
blob: 038e8df5b5994e290edfb2ce5fe7ec4e4b37dbaf (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
#include <u.h>
#include <libn.h>
#include <unibilium.h>

#define iota(x)     1 << (x)

// -----------------------------------------------------------------------
// enums

enum 
{
    KeyRune, KeyFunc, KeySym, 
    KeyMouse, KeyPosition, KeyModeReport, 
    KeyDCS, KeyOSC, /* add other recognised types here */
    KeyUnknownCSI = -1
};

enum KeyEvent 
{
    EvNil, EvKey, EvEOF, EvAgain, EvErr,
};

enum 
{
    SymUnknown = -1,
    SymNone    =  0,

    /* special names in c0 */
    SymBackspace, SymTab, SymEnter, SymEscape,

    /* special names in g0 */
    SymSpace, SymDel,

    /* special keys */
    SymUp, SymDown, SymLeft, SymRight, SymBegin, SymFind, SymInsert,
    SymDelete, SymSelect, SymPageUp, SymPageDown, SymHome, SymEnd,

    /* special keys from terminfo */
    SymCancel,  SymClear,  SymClose,     SymCommand, SymCopy, SymExit,
    SymHelp,    SymMark,   SymMessage,   SymMove,    SymOpen, SymOptions,
    SymPrint,   SymRedo,   SymReference, SymRefresh, SymReplace,
    SymRestart, SymResume, SymSave,      SymSuspend, SymUndo,

    /* numeric keypad special keys */
    SymKp0, SymKp1, SymKp2, SymKp3, SymKp4, SymKp5, SymKp6, SymKp7, SymKp8,
    SymKp9, SymKpEnter, SymKpPlus, SymKpMinus, SymKpMul, SymKpDiv, SymKpComma,
    SymKpDot, SymKpEq,

    /* et cetera ad nauseum */
    NumSyms 
};

enum MouseEvent
{
    MouseNil, MousePress, MouseDrag, MouseRelease,
};

enum 
{
    ModShift = iota(0),
    ModAlt   = iota(1),
    ModCtrl  = iota(2),
};

// -----------------------------------------------------------------------
// types

typedef struct Input Input;

struct Input
{
    int fd;
    int wait; /* in msec */
    struct {
        char *b, *c, bytes[1024];
    } rbuf;   /* read buffer */
    struct {
        char *b, *c, bytes[1024];
    } ebuf;   /* escape buffer */
};

// -----------------------------------------------------------------------
// globals

// -----------------------------------------------------------------------
// utility functions

// -----------------------------------------------------------------------
// implementation

Input *
makeinput(int fd, int flags, unibi_term *info)
{
    int    i;
    Input *in;
    char  *e;

    if (!(in = calloc(1, sizeof(in))))
        panicf("out of memory");

    in->fd      = fd;
    in->wait    = 50; /* in msec */

    /* initialize buffers */
    in->rbuf.c = in->rbuf.b = in->rbuf.bytes;

    return in;
}