File: bind.c

package info (click to toggle)
kbtin 2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,856 kB
  • sloc: ansic: 18,627; perl: 179; sh: 88; makefile: 17
file content (229 lines) | stat: -rw-r--r-- 5,940 bytes parent folder | download | duplicates (2)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "tintin.h"
#include "protos/action.h"
#include "protos/alias.h"
#include "protos/globals.h"
#include "protos/hash.h"
#include "protos/misc.h"
#include "protos/print.h"
#include "protos/parse.h"
#include "protos/user.h"
#include "protos/string.h"

static struct hashtable *keynames;

static const char *KEYNAMES[]=
{
    "ESC[[A",       "F1",
    "ESC[[B",       "F2",
    "ESC[[C",       "F3",
    "ESC[[D",       "F4",
    "ESC[[E",       "F5",
    "ESC[11~",      "F1",
    "ESC[12~",      "F2",
    "ESC[13~",      "F3",
    "ESC[14~",      "F4",
    "ESC[15~",      "F5",
    "ESC[17~",      "F6",
    "ESC[18~",      "F7",
    "ESC[19~",      "F8",
    "ESC[20~",      "F9",
    "ESC[21~",      "F10",
    "ESC[23~",      "F11",
    "ESC[24~",      "F12",
    "ESC[25~",      "F13",
    "ESC[26~",      "F14",
    "ESC[27~",      "F15",
    "ESC[28~",      "F16",
    "ESC[29~",      "F17",
    "ESC[30~",      "F18",
    "ESC[31~",      "F19",
    "ESC[32~",      "F20",
    "ESC[33~",      "F21",
    "ESC[34~",      "F22",
    "ESC[35~",      "F23",
    "ESC[36~",      "F24",
    "ESC[A",        "UpArrow",
    "ESC[B",        "DownArrow",
    "ESC[C",        "RightArrow",
    "ESC[D",        "LeftArrow",
    "ESC[E",        "MidArrow",
    "ESC[F",        "PgDn",
    "ESC[G",        "MidArrow",
    "ESC[H",        "Home",
    "ESC[P",        "Pause",
    "ESC[1~",       "Home",
    "ESC[2~",       "Ins",
    "ESC[3~",       "Del",
    "ESC[4~",       "End",
    "ESC[5~",       "PgUp",
    "ESC[6~",       "PgDn",
    "ESC[7~",       "Home",
    "ESC[8~",       "End",
    "ESC[OA",       "UpArrow",      /* alternate cursor mode */
    "ESC[OB",       "DownArrow",
    "ESC[OC",       "RightArrow",
    "ESC[OD",       "LeftArrow",
    "ESC[OE",       "MidArrow",
    "ESCOE",        "Kpad5",        /* screen on vte */
    "ESCOM",        "KpadEnter",    /* alternate keypad mode */
    "ESCOP",        "KpadNumLock",
    "ESCOQ",        "KpadDivide",
    "ESCOR",        "KpadMultiply",
    "ESCOS",        "KpadMinus",
    "ESCOX",        "KpadEqual",
    "ESCOj",        "KpadMultiply",
    "ESCOk",        "KpadPlus",
    "ESCOl",        "KpadPlus",
    "ESCOm",        "KpadMinus",
    "ESCOn",        "KpadDot",
    "ESCOo",        "KpadMinus",
    "ESCOp",        "Kpad0",
    "ESCOq",        "Kpad1",
    "ESCOr",        "Kpad2",
    "ESCOs",        "Kpad3",
    "ESCOt",        "Kpad4",
    "ESCOu",        "Kpad5",
    "ESCOv",        "Kpad6",
    "ESCOw",        "Kpad7",
    "ESCOx",        "Kpad8",
    "ESCOy",        "Kpad9",
    "",             "",
};

static const char *NORMAL_KEYNAMES[]=
{
    "ESCOP",        "KpadNumLock",
    "ESCOQ",        "KpadDivide",
    "ESCOR",        "KpadMultiply",
    "ESCOS",        "KpadMinus",
    "ESCOo",        "KpadDivide",
    "",             "",
};

static const char *XTERM_KEYNAMES[]=
{
    "ESCOP",        "F1",
    "ESCOQ",        "F2",
    "ESCOR",        "F3",
    "ESCOS",        "F4",
    "ESCOm",        "KpadMinus",
    "",             "",
};

/*********************/
/* the #bind command */
/*********************/
void bind_command(const char *arg, struct session *ses)
{
    char left[BUFFER_SIZE], right[BUFFER_SIZE];

    if (!ui_keyboard)
    {
        tintin_eprintf(ses, "#UI: no access to keyboard => no keybindings");
        return;
    }
    arg = get_arg_in_braces(arg, left, 0);
    arg = get_arg_in_braces(arg, right, 1);

    if (*left && *right)
    {
        set_hash(ses->binds, left, right);
        if (ses->mesvar[MSG_BIND])
            tintin_printf(ses, "#Ok. {%s} is now bound to {%s}.", left, right);
        bindnum++;
        return;
    }
    show_hashlist(ses, ses->binds, left,
        "#Bound keys:",
        "#No match(es) found for {%s}");
}

/***********************/
/* the #unbind command */
/***********************/
void unbind_command(const char *arg, struct session *ses)
{
    char left[BUFFER_SIZE];

    if (!ui_keyboard)
    {
        tintin_eprintf(ses, "#UI: no access to keyboard => no keybindings");
        return;
    }
    arg = get_arg(arg, left, 1, ses);
    delete_hashlist(ses, ses->binds, left,
        ses->mesvar[MSG_BIND]? "#Ok. {%s} is no longer bound." : 0,
        ses->mesvar[MSG_BIND]? "#No match(es) found for {%s}" : 0);
}


static const char *bitted(const char *key, uint8_t bits)
{
    if (!bits)
        return key;

    static char esckey[64];
    snprintf(esckey, sizeof(esckey), "%s%s%s%s",
        bits&4? "Ctrl-" : "",
        bits&2? "Alt-" : "",
        bits&1? "Shift-" : "",
        key);
    return esckey;
}


bool find_bind(const char *key, uint8_t bits, int msg, struct session *ses)
{
    char *val;

#ifdef KEYBOARD_DEBUG
    tintin_printf(ses, "~3~[~11~%s~3~]~7~", bitted(key, bits));
#endif
    if ((val=get_hash(ses->binds, bitted(key, bits))))
    {          /* search twice, both for raw key code and key name */
        parse_input(val, true, ses);
        recursion=0;
        return true;
    }
    if ((val=get_hash(keynames, key)))
    {
        key=val;
#ifdef KEYBOARD_DEBUG
        tintin_printf(ses, "~3~→ [~11~%s~3~]~7~", bitted(key, bits));
#endif
        if ((val=get_hash(ses->binds, bitted(key, bits))))
        {
            parse_input(val, true, ses);
            recursion=0;
            return true;
        }
    }
    if (msg)
        tintin_printf(ses, "#Unbound keycode: %s", bitted(key, bits));
    return false;
}


void init_bind(void)
{
    keynames=init_hash();
    if (!ui_keyboard)
        return;
    for (const char**n=KEYNAMES;**n;n+=2)
        set_hash_nostring(keynames, n[0], n[1]);
}

void cleanup_bind(void)
{
    kill_hash_nostring(keynames);
}

void bind_xterm(bool xterm)
{
    if (xterm)
        for (const char**n=XTERM_KEYNAMES;**n;n+=2)
            set_hash_nostring(keynames, n[0], n[1]);
    else
        for (const char**n=NORMAL_KEYNAMES;**n;n+=2)
            set_hash_nostring(keynames, n[0], n[1]);
}