Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 264 lines (245 sloc) 6.01 kb
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
/*
** Copyright (C) 1991, 1997 Free Software Foundation, Inc.
**
** This file is part of TACK.
**
** TACK 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, or (at your option)
** any later version.
**
** TACK 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 TACK; see the file COPYING. If not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
** Boston, MA 02110-1301, USA
*/
/* scan mode keyboard support */

#include <tack.h>

MODULE_ID("$Id: scan.c,v 1.8 2009/12/26 21:29:01 tom Exp $")

unsigned scan_max; /* length of longest scan code */
char **scan_up, **scan_down, **scan_name;
unsigned *scan_tested, *scan_length;
static unsigned *scan_value;

static unsigned shift_state;
static char *str;
static int debug_char_count;

#define SHIFT_KEY 0x100
#define CONTROL_KEY 0x200
#define META_KEY 0x400
#define CAPS_LOCK 0x800

static const struct {
const char *name;
unsigned type;
} scan_special[] = {
{"<shift>", SHIFT_KEY},
{"<left shift>", SHIFT_KEY},
{"<right shift>", SHIFT_KEY},
{"<control>", CONTROL_KEY},
{"<left control>", CONTROL_KEY},
{"<right control>", CONTROL_KEY},
{"<meta>", META_KEY},
{"<left meta>", META_KEY},
{"<right meta>", META_KEY},
{"<caps lock>", CAPS_LOCK},
{"<tab>", '\t'},
{"<space>", ' '},
{"<return>", '\r'},
{"<linefeed>", '\n'},
{"<formfeed>", '\f'},
{"<backspace>", '\b'},
{0, 0}
};

static void
scan_blanks(void)
{ /* scan past the white space */
while (*str == ' ' || *str == '\t')
str++;
}

static char *
smash(void)
{ /* convert a string to hex */
char *s, *t;
int ch, i, j;

t = s = str;
for (i = 0; (ch = *str); str++) {
if (ch >= '0' && ch <= '9')
j = ch - '0';
else if (ch >= 'a' && ch <= 'f')
j = 10 - 'a' + ch;
else if (ch >= 'A' && ch <= 'F')
j = 10 - 'A' + ch;
else if (ch == ' ' || ch == '\t')
break;
else
continue;
if (i) {
*s = (char) (*s | j);
s++;
} else
*s = (char) (j << 4);
i ^= 1;
}
*s = '\0';
return t;
}

void
scan_init(char *fn)
{ /* read the scan mode key definitions */
char *s, *sl;
FILE *fp;
int ch, i, j;
unsigned len;
char home[512];

if ((str = getenv("HOME")))
strcpy(home, str);
else
home[0] = '\0';
fp = NULL;
if ((str = getenv("KEYBOARD"))) {
if (!(fp = fopen(str, "r")) && home[0]) {
sprintf(temp, "%s/.scan.%s", home, str);
fp = fopen(temp, "r");
}
}
if (!fp) {
sprintf(temp, ".scan.%s", fn);
fp = fopen(temp, "r");
}
if (!fp && home[0]) {
sprintf(temp, "%s/.scan.%s", home, fn);
fp = fopen(temp, "r");
}
if (!fp) {
ptext("Unable to open scanfile: ");
ptextln(temp);
bye_kids(EXIT_FAILURE);
return;
}
/*
scan file format:

<down value> <up value> <name>

values are in hex. <name> may be any string of characters

*/
scan_up = (char **) malloc(sizeof(char *) * MAX_SCAN);
scan_down = (char **) malloc(sizeof(char *) * MAX_SCAN);
scan_name = (char **) malloc(sizeof(char *) * MAX_SCAN);
scan_tested = (unsigned *) malloc(sizeof(unsigned *) * MAX_SCAN);
scan_length = (unsigned *) malloc(sizeof(unsigned *) * MAX_SCAN);
scan_value = (unsigned *) malloc(sizeof(unsigned *) * MAX_SCAN);
scan_up[0] = scan_down[0] = scan_name[0] = (char *) 0;
str = (char *) malloc(4096); /* buffer space */
sl = str + 4000; /* an upper limit */
scan_max = 1;
for (i = 0;;) {
for (s = str; (ch = getc(fp)) != EOF;) {
if (ch == '\n' || ch == '\r')
break;
*s++ = (char) ch;
}
*s++ = '\0';
if (ch == EOF)
break;
if (*str == '#' || *str == '\0')
continue;
scan_down[i] = smash();
scan_blanks();
scan_up[i] = smash();
scan_blanks();
scan_name[i] = str;

scan_length[i] = strlen(scan_down[i]);
len = strlen(scan_up[i]) + scan_length[i];
if (len > scan_max)
scan_max = len;

scan_value[i] = UChar(scan_name[i][0]);
if (scan_name[i][1]) /* multi-character name */
for (j = 0; scan_special[j].name; j++) {
if (!strcmp(scan_name[i], scan_special[j].name)) {
scan_value[i] = scan_special[j].type;
break;
}
}

i++;
if (str > sl) {
str = (char *) malloc(4096);
sl = str + 4000;
} else
str = s;
}
fclose(fp);
#ifdef notdef
for (i = 0; scan_down[i]; i++) {
put_str(hex_expand_to(scan_down[i], 3));
put_str(hex_expand_to(scan_up[i], 3));
put_str(" ");
put_str(scan_name[i]);
put_crlf();
}
(void) wait_here();
#endif
}

int
scan_key(void)
{ /* read a key and translate scan mode to
ASCII */
unsigned i;
int j, ch;
char buf[64];

for (i = 1;; i++) {
ch = getchar();
if (ch == EOF)
return EOF;
if (debug_fp) {
fprintf(debug_fp, "%02X ", ch);
debug_char_count += 3;
if (debug_char_count > 72) {
fprintf(debug_fp, "\n");
debug_char_count = 0;
}
}
buf[i - 1] = (char) ch;
buf[i] = '\0';
if (buf[0] & 0x80) { /* scan up */
for (j = 0; scan_up[j]; j++) {
if (i == scan_length[j] &&
!strcmp(buf, scan_up[j])) {
i = 0;
shift_state &= ~scan_value[j];
break;
}
}
continue;
}
for (j = 0; scan_down[j]; j++) {
if (i == scan_length[j] && !strcmp(buf, scan_down[j])) {
i = 0;
shift_state |= scan_value[j];
ch = (int) scan_value[j];
if (ch == CAPS_LOCK)
shift_state ^= SHIFT_KEY;
if (ch >= 256)
break;
if (shift_state & SHIFT_KEY) {
if (ch >= 0x60)
ch -= 0x20;
else if (ch >= 0x30 && ch <= 0x3f)
ch -= 0x10;
}
if (shift_state & CONTROL_KEY) {
if ((ch | 0x20) >= 0x60 &&
(ch | 0x20) <= 0x7f)
ch = (ch | 0x20) - 0x60;
}
if (shift_state & META_KEY)
ch |= 0x80;
return ch;
}
}
if (i > scan_max)
i = 1;
}
}
Something went wrong with that request. Please try again.