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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
/*
* Copyright © 2008-2010 dragchan <zgchan317@gmail.com>
* This file is part of FbTerm.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
#include <linux/keyboard.h>
#include <linux/input.h>
#include "input_key.h"
static char key_down[NR_KEYS];
static unsigned char shift_down[NR_SHIFT];
static short shift_state;
static char lock_state;
static char cr_with_lf, applic_keypad, cursor_esco;
static int npadch;
void init_keycode_state()
{
npadch = -1;
shift_state = 0;
memset(key_down, 0, sizeof(char) * NR_KEYS);
memset(shift_down, 0, sizeof(char) * NR_SHIFT);
ioctl(STDIN_FILENO, KDGKBLED, &lock_state);
}
void update_term_mode(char crlf, char appkey, char curo)
{
cr_with_lf = crlf;
applic_keypad = appkey;
cursor_esco = curo;
}
unsigned short keycode_to_keysym(unsigned short keycode, char down)
{
if (keycode >= NR_KEYS) return K_HOLE;
char rep = (down && key_down[keycode]);
key_down[keycode] = down;
struct kbentry ke;
ke.kb_table = shift_state;
ke.kb_index = keycode;
if (ioctl(STDIN_FILENO, KDGKBENT, &ke) == -1) return K_HOLE;
if (KTYP(ke.kb_value) == KT_LETTER && (lock_state & K_CAPSLOCK)) {
ke.kb_table = shift_state ^ (1 << KG_SHIFT);
if (ioctl(STDIN_FILENO, KDGKBENT, &ke) == -1) return K_HOLE;
}
if (ke.kb_value == K_HOLE || ke.kb_value == K_NOSUCHMAP) return K_HOLE;
unsigned value = KVAL(ke.kb_value);
switch (KTYP(ke.kb_value)) {
case KT_SPEC:
switch (ke.kb_value) {
case K_NUM:
if (applic_keypad) break;
case K_BARENUMLOCK:
case K_CAPS:
case K_CAPSON:
if (down && !rep) {
if (value == KVAL(K_NUM) || value == KVAL(K_BARENUMLOCK)) lock_state ^= K_NUMLOCK;
else if (value == KVAL(K_CAPS)) lock_state ^= K_CAPSLOCK;
else if (value == KVAL(K_CAPSON)) lock_state |= K_CAPSLOCK;
ioctl(STDIN_FILENO, KDSKBLED, lock_state);
}
break;
default:
break;
}
break;
case KT_SHIFT:
if (value >= NR_SHIFT || rep) break;
if (value == KVAL(K_CAPSSHIFT)) {
value = KVAL(K_SHIFT);
if (down && (lock_state & K_CAPSLOCK)) {
lock_state &= ~K_CAPSLOCK;
ioctl(STDIN_FILENO, KDSKBLED, lock_state);
}
}
if (down) shift_down[value]++;
else if (shift_down[value]) shift_down[value]--;
if (shift_down[value]) shift_state |= (1 << value);
else shift_state &= ~(1 << value);
break;
case KT_LATIN:
case KT_LETTER:
case KT_FN:
case KT_PAD:
case KT_CONS:
case KT_CUR:
case KT_META:
case KT_ASCII:
break;
default:
printf("not support!\n");
break;
}
return ke.kb_value;
}
unsigned short keypad_keysym_redirect(unsigned short keysym)
{
if (applic_keypad || KTYP(keysym) != KT_PAD || KVAL(keysym) >= NR_PAD) return keysym;
#define KL(val) K(KT_LATIN, val)
static const unsigned short num_map[] = {
KL('0'), KL('1'), KL('2'), KL('3'), KL('4'),
KL('5'), KL('6'), KL('7'), KL('8'), KL('9'),
KL('+'), KL('-'), KL('*'), KL('/'), K_ENTER,
KL(','), KL('.'), KL('?'), KL('('), KL(')'),
KL('#')
};
static const unsigned short fn_map[] = {
K_INSERT, K_SELECT, K_DOWN, K_PGDN, K_LEFT,
K_P5, K_RIGHT, K_FIND, K_UP, K_PGUP,
KL('+'), KL('-'), KL('*'), KL('/'), K_ENTER,
K_REMOVE, K_REMOVE, KL('?'), KL('('), KL(')'),
KL('#')
};
if (lock_state & K_NUMLOCK) return num_map[keysym - K_P0];
return fn_map[keysym - K_P0];
}
static unsigned to_utf8(unsigned c, char *buf)
{
unsigned index = 0;
if (c < 0x80)
buf[index++] = c;
else if (c < 0x800) {
// 110***** 10******
buf[index++] = 0xc0 | (c >> 6);
buf[index++] = 0x80 | (c & 0x3f);
} else if (c < 0x10000) {
if (c >= 0xD800 && c < 0xE000)
return index;
if (c == 0xFFFF)
return index;
// 1110**** 10****** 10******
buf[index++] = 0xe0 | (c >> 12);
buf[index++] = 0x80 | ((c >> 6) & 0x3f);
buf[index++] = 0x80 | (c & 0x3f);
} else if (c < 0x200000) {
// 11110*** 10****** 10****** 10******
buf[index++] = 0xf0 | (c >> 18);
buf[index++] = 0x80 | ((c >> 12) & 0x3f);
buf[index++] = 0x80 | ((c >> 6) & 0x3f);
buf[index++] = 0x80 | (c & 0x3f);
}
return index;
}
char *keysym_to_term_string(unsigned short keysym, char down)
{
static struct kbsentry kse;
char *buf = (char *)kse.kb_string;
*buf = 0;
if (KTYP(keysym) != KT_SHIFT && !down) return buf;
keysym = keypad_keysym_redirect(keysym);
unsigned index = 0, value = KVAL(keysym);
switch (KTYP(keysym)) {
case KT_LATIN:
case KT_LETTER:
if (value < KVAL(AC_START) || value > KVAL(AC_END)) index = to_utf8(value, buf);
break;
case KT_FN:
kse.kb_func = value;
ioctl(STDIN_FILENO, KDGKBSENT, &kse);
index = strlen(buf);
break;
case KT_SPEC:
if (keysym == K_ENTER) {
buf[index++] = '\r';
if (cr_with_lf) buf[index++] = '\n';
} else if (keysym == K_NUM && applic_keypad) {
buf[index++] = '\e';
buf[index++] = 'O';
buf[index++] = 'P';
}
break;
case KT_PAD:
if (applic_keypad && !shift_down[KG_SHIFT]) {
if (value < NR_PAD) {
static const char app_map[] = "pqrstuvwxylSRQMnnmPQS";
buf[index++] = '\e';
buf[index++] = 'O';
buf[index++] = app_map[value];
}
} else if (keysym == K_P5 && !(lock_state & K_NUMLOCK)) {
buf[index++] = '\e';
buf[index++] = (applic_keypad ? 'O' : '[');
buf[index++] = 'G';
}
break;
case KT_CUR:
if (value < 4) {
static const char cur_chars[] = "BDCA";
buf[index++] = '\e';
buf[index++] = (cursor_esco ? 'O' : '[');
buf[index++] = cur_chars[value];
}
break;
case KT_META: {
long flag;
ioctl(STDIN_FILENO, KDGKBMETA, &flag);
if (flag == K_METABIT) {
buf[index++] = 0x80 | value;
} else {
buf[index++] = '\e';
buf[index++] = value;
}
break;
}
case KT_SHIFT:
if (!down && npadch != -1) {
index = to_utf8(npadch, buf);
npadch = -1;
}
break;
case KT_ASCII:
if (value < NR_ASCII) {
int base = 10;
if (value >= KVAL(K_HEX0)) {
base = 16;
value -= KVAL(K_HEX0);
}
if (npadch == -1) npadch = value;
else npadch = npadch * base + value;
}
break;
default:
break;
}
buf[index] = 0;
return buf;
}
|