comparison keymap.c @ 3:5a977ccbc7a9 default tip

Empty changelog
author darius
date Sat, 06 Dec 1997 05:41:29 +0000
parents
children
comparison
equal deleted inserted replaced
2:fba0b6e6cdc7 3:5a977ccbc7a9
1 /* $Id: keymap.c,v 1.1.1.1 1997/12/06 05:41:29 darius Exp $ */
2
3 /*
4 * keymap.c
5 * Bill Dyess, 10/20/93
6 */
7
8 #include "copyright.h"
9 #include <stdio.h>
10 /*#include <sys/types.h>
11 #ifdef RS6K
12 #include <sys/select.h>
13 #endif
14 #include <signal.h>
15 #include <errno.h>
16 #include "packets.h"*/
17 #include "Wlib.h"
18 #include "defs.h"
19 #include "struct.h"
20 #include "data.h"
21 #include "proto.h"
22
23 #define control(x) (x)+128
24
25 int
26 doKeymap(data)
27 W_Event *data;
28 {
29 int key = data->key;
30
31 #ifdef MACROS
32 if (macroState) { /* macro needed a destination or in macro
33 mode */
34 if (key > 256 && data->type != W_EV_BUTTON) { /* an alt-key will exit
35 the macro mode */
36 /*
37 but let mouse buttons do macros! button conversion is done by
38 calling function.
39 */
40 warning(" ");
41 } else {
42 if (key > 256)
43 data->key -= 256;
44 doMacro(data);
45 return -1;
46 }
47 }
48 #endif /* MACROS */
49
50 if (key < 256) { /* not alt key */
51 key = myship->s_keymap[key];
52
53 #ifdef MACROS
54 if (macrotable[key] && (macrotable[key]->flags & MACSINGLE)) {
55 data->key = key;
56 doMacro(data);
57 return -1;
58 }
59 #endif /* MACROS */
60
61 } else
62 key = key - 256;
63 return key;
64 }
65
66 unsigned char def_keymap[256];
67 unsigned char def_buttonmap[12];
68 void
69 buildShipKeymap(shipp)
70 struct ship *shipp;
71 {
72 char keybuf[40], ckeybuf[40], buttonbuf[40], cbuttonbuf[40];
73 struct stringlist *l;
74
75 bcopy(def_keymap, shipp->s_keymap, 256);
76 bcopy(def_buttonmap, shipp->s_buttonmap, 12);
77
78 sprintf(keybuf, "keymap.%c%c", shipp->s_desig[0], shipp->s_desig[1]);
79 sprintf(ckeybuf, "ckeymap.%c%c", shipp->s_desig[0], shipp->s_desig[1]);
80 sprintf(buttonbuf, "buttonmap.%c%c", shipp->s_desig[0], shipp->s_desig[1]);
81 sprintf(cbuttonbuf, "cbuttonmap.%c%c", shipp->s_desig[0], shipp->s_desig[1]);
82
83 for (l = defaults; l; l = l->next) {
84 if (!strcmpi(keybuf, l->string))
85 keymapAdd(l->value, shipp->s_keymap);
86 else if (!strcmpi(ckeybuf, l->string))
87 ckeymapAdd(l->value, shipp->s_keymap);
88 else if (!strcmpi(buttonbuf, l->string))
89 buttonmapAdd(l->value, shipp->s_buttonmap);
90 else if (!strcmpi(cbuttonbuf, l->string))
91 cbuttonmapAdd(l->value, shipp->s_buttonmap);
92 }
93 }
94
95 void
96 initkeymap(type)
97 int type;
98 {
99 char keybuf[40], ckeybuf[40], buttonbuf[40], cbuttonbuf[40];
100 struct ship *sh;
101 int i, j;
102 struct stringlist *l;
103
104 keybuf[0] = ckeybuf[0] = buttonbuf[0] = cbuttonbuf[0] = 0;
105
106 if (type < 0) {
107 for (i = 0; i < 256; i++)
108 def_keymap[i] = i;
109
110 for (l = defaults; l; l = l->next) {
111 if (!strcmpi("keymap", l->string))
112 keymapAdd(l->value, def_keymap);
113 else if (!strcmpi("ckeymap", l->string))
114 ckeymapAdd(l->value, def_keymap);
115 else if (!strcmpi("buttonmap", l->string))
116 buttonmapAdd(l->value, def_buttonmap);
117 else if (!strcmpi("cbuttonmap", l->string))
118 cbuttonmapAdd(l->value, def_buttonmap);
119 }
120
121 for (j = 0; j < nshiptypes; j++) {
122 buildShipKeymap(getship(j));
123 }
124 }
125 }
126
127 void
128 keymapAdd(str, map)
129 char *str, *map;
130 {
131 if (str) {
132 /* parse non-control char keymap */
133 while (*str != '\0' && *(str + 1) != '\0') {
134 if (*str >= 32 && *str < 127) {
135 map[(int) *str] = *(str + 1);
136 }
137 str += 2;
138 }
139 }
140 }
141
142 void
143 ckeymapAdd(cstr, map)
144 char *cstr, *map;
145 {
146 unsigned char key[2];
147 short state = 0;
148
149 if (cstr) {
150 /*
151 control chars are allowed, so use ^char to mean control, and ^^ to
152 mean ^
153 */
154 while (*cstr != '\0') {
155 if (*cstr == '^') {
156 cstr++;
157 if (*cstr == '^' || !*cstr)
158 key[state] = '^';
159 else
160 key[state] = 128 + *cstr;
161 } else {
162 key[state] = *cstr;
163 }
164 if (*cstr)
165 cstr++;
166 if (state)
167 map[key[0]] = key[1];
168 state = 1 - state;
169 }
170 }
171 }
172
173 void
174 buttonmapAdd(str, map)
175 char *str, *map;
176 {
177 unsigned char button, ch;
178
179 if (str) {
180 while (*str != '\0' && *(str + 1) != '\0') {
181 if (*str < 'a')
182 button = *str++ - '1';
183 else
184 button = 9 + *str++ - 'a';
185 if (button > 11)
186 fprintf(stderr, "%c ignored in buttonmap\n", *(str - 1));
187 else {
188 ch = *str++;
189 map[button] = ch;
190 }
191 }
192 }
193 }
194
195 void
196 cbuttonmapAdd(cstr, map)
197 char *cstr, *map;
198 {
199 unsigned char button, ch;
200
201 if (cstr) {
202 while (*cstr != '\0' && *(cstr + 1) != '\0') {
203 /*
204 code for cbuttonmap, which allows buttons to be mapped to
205 control keys. [BDyess]
206 */
207 if (*cstr < 'a')
208 button = *cstr++ - '1';
209 else
210 button = 9 + *cstr++ - 'a';
211 if (button > 11)
212 fprintf(stderr, "%c ignored in cbuttonmap\n", *(cstr - 1));
213 else {
214 ch = *cstr++;
215 if (ch == '^') {
216 ch = *cstr++;
217 if (ch != '^')
218 ch += 128;
219 }
220 map[button] = ch;
221 }
222 }
223 }
224 }
225
226 #ifdef KEYMAP_DEBUG
227 void
228 dumpKeymap()
229 {
230 int i;
231
232 for (i = 0; i < 256; i++) {
233 printf("%3d %c : %3d %c\n",
234 i,
235 isprint(i) ? i : '_',
236 myship->s_keymap[i],
237 isprint(myship->s_keymap[i]) ? myship->s_keymap[i] : '_');
238 }
239 }
240 #endif /* KEYMAP_DEBUG */