File: nhdefkey.c

package info (click to toggle)
glhack 1.2-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 24,604 kB
  • ctags: 18,992
  • sloc: ansic: 208,570; cpp: 13,139; yacc: 2,005; makefile: 1,161; lex: 377; sh: 321; awk: 89; sed: 11
file content (329 lines) | stat: -rw-r--r-- 9,115 bytes parent folder | download | duplicates (12)
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*	SCCS Id: @(#)nhdefkey.c	3.4	$Date: 2003/11/01 23:57:00 $   */
/* Copyright (c) NetHack PC Development Team 2003                      */
/* NetHack may be freely redistributed.  See license for details.      */

/*
 * This is the default NetHack keystroke processing.
 * It can be built as a run-time loadable dll (nhdefkey.dll).
 * Alternative keystroke handlers can be built using the
 * entry points in this file as a template.
 *
 * Use the defaults.nh "altkeyhandler" option to set a
 * different dll name (without the ".DLL" extension) to
 * get different processing. Ensure that the dll referenced
 * in defaults.nh exists in the same directory as NetHack in
 * order for it to load successfully.
 *
 */

static char where_to_get_source[] = "http://www.nethack.org/";
static char author[] = "The NetHack Development Team";

#include "hack.h"
#include "wintty.h"
#include "win32api.h"

extern HANDLE hConIn;
extern INPUT_RECORD ir;
char dllname[512];
char *shortdllname;

int FDECL(__declspec(dllexport) __stdcall
ProcessKeystroke, (HANDLE hConIn, INPUT_RECORD *ir, 
    boolean *valid, BOOLEAN_P numberpad, int portdebug));

int WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
{
	char dlltmpname[512];
	char *tmp = dlltmpname, *tmp2;
	*(tmp + GetModuleFileName(hInstance, tmp, 511)) = '\0';
	(void)strcpy(dllname, tmp);
	tmp2 = strrchr(dllname, '\\');
	if (tmp2) {
		tmp2++;
		shortdllname = tmp2;
	}
	return TRUE;
}

/*
 *  Keyboard translation tables.
 *  (Adopted from the MSDOS port)
 */

#define KEYPADLO	0x47
#define KEYPADHI	0x53

#define PADKEYS 	(KEYPADHI - KEYPADLO + 1)
#define iskeypad(x)	(KEYPADLO <= (x) && (x) <= KEYPADHI)

/*
 * Keypad keys are translated to the normal values below.
 * Shifted keypad keys are translated to the
 *    shift values below.
 */

static const struct pad {
	uchar normal, shift, cntrl;
} keypad[PADKEYS] = {
			{'y', 'Y', C('y')},		/* 7 */
			{'k', 'K', C('k')},		/* 8 */
			{'u', 'U', C('u')},		/* 9 */
			{'m', C('p'), C('p')},		/* - */
			{'h', 'H', C('h')},		/* 4 */
			{'g', 'G', 'g'},		/* 5 */
			{'l', 'L', C('l')},		/* 6 */
			{'+', 'P', C('p')},		/* + */
			{'b', 'B', C('b')},		/* 1 */
			{'j', 'J', C('j')},		/* 2 */
			{'n', 'N', C('n')},		/* 3 */
			{'i', 'I', C('i')},		/* Ins */
			{'.', ':', ':'}			/* Del */
}, numpad[PADKEYS] = {
			{'7', M('7'), '7'},		/* 7 */
			{'8', M('8'), '8'},		/* 8 */
			{'9', M('9'), '9'},		/* 9 */
			{'m', C('p'), C('p')},		/* - */
			{'4', M('4'), '4'},		/* 4 */
			{'5', M('5'), '5'},		/* 5 */
			{'6', M('6'), '6'},		/* 6 */
			{'+', 'P', C('p')},		/* + */
			{'1', M('1'), '1'},		/* 1 */
			{'2', M('2'), '2'},		/* 2 */
			{'3', M('3'), '3'},		/* 3 */
			{'0', M('0'), '0'},		/* Ins */
			{'.', ':', ':'}			/* Del */
};

#define inmap(x,vk)	(((x) > 'A' && (x) < 'Z') || (vk) == 0xBF || (x) == '2')

static BYTE KeyState[256];

int __declspec(dllexport) __stdcall
ProcessKeystroke(hConIn,ir, valid, numberpad, portdebug)
HANDLE hConIn;
INPUT_RECORD *ir;
boolean *valid;
boolean numberpad;
int portdebug;
{
	int metaflags = 0, k = 0;
	int keycode, vk;
	unsigned char ch, pre_ch, mk = 0;
	unsigned short int scan;
	unsigned long shiftstate;
	int altseq = 0;
	const struct pad *kpad;

	shiftstate = 0L;
	ch = pre_ch = ir->Event.KeyEvent.uChar.AsciiChar;
	scan  = ir->Event.KeyEvent.wVirtualScanCode;
	vk    = ir->Event.KeyEvent.wVirtualKeyCode;
	keycode = MapVirtualKey(vk, 2);
	shiftstate = ir->Event.KeyEvent.dwControlKeyState;
	KeyState[VK_SHIFT]   = (shiftstate & SHIFT_PRESSED) ? 0x81 : 0;
	KeyState[VK_CONTROL] = (shiftstate & (LEFT_CTRL_PRESSED|RIGHT_CTRL_PRESSED)) ?
				0x81 : 0;
	KeyState[VK_CAPITAL] = (shiftstate & CAPSLOCK_ON) ? 0x81 : 0;

	if (shiftstate & (LEFT_ALT_PRESSED|RIGHT_ALT_PRESSED)) {
		if (ch || inmap(keycode,vk)) altseq = 1;
		else altseq = -1;	/* invalid altseq */
	}
	if (ch || (iskeypad(scan)) || (altseq > 0))
		*valid = TRUE;
	/* if (!valid) return 0; */
    	/*
	 * shiftstate can be checked to see if various special
         * keys were pressed at the same time as the key.
         * Currently we are using the ALT & SHIFT & CONTROLS.
         *
         *           RIGHT_ALT_PRESSED, LEFT_ALT_PRESSED,
         *           RIGHT_CTRL_PRESSED, LEFT_CTRL_PRESSED,
         *           SHIFT_PRESSED,NUMLOCK_ON, SCROLLLOCK_ON,
         *           CAPSLOCK_ON, ENHANCED_KEY
         *
         * are all valid bit masks to use on shiftstate.
         * eg. (shiftstate & LEFT_CTRL_PRESSED) is true if the
         *      left control key was pressed with the keystroke.
         */
        if (iskeypad(scan)) {
            kpad = numberpad ? numpad : keypad;
            if (shiftstate & SHIFT_PRESSED) {
                ch = kpad[scan - KEYPADLO].shift;
            }
            else if (shiftstate & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                ch = kpad[scan - KEYPADLO].cntrl;
            }
            else {
                ch = kpad[scan - KEYPADLO].normal;
            }
        }
        else if (altseq > 0) { /* ALT sequence */
		if (vk == 0xBF) ch = M('?');
		else ch = M(tolower(keycode));
        }
	/* Attempt to work better with international keyboards. */
	else {
		WORD chr[2];
		k = ToAscii(vk, scan, KeyState, chr, 0);
		if (k <= 2)
		    switch(k) {
			case 2:  /* two characters */
				ch = (unsigned char)chr[1];
				*valid = TRUE;
				break;
			case 1:  /* one character */
				ch = (unsigned char)chr[0];
				*valid = TRUE;
				break;
			case 0:  /* no translation */
			default: /* negative */
				*valid = FALSE;
		    }
	}
	if (ch == '\r') ch = '\n';
#ifdef PORT_DEBUG
	if (portdebug) {
		char buf[BUFSZ];
		Sprintf(buf,
	"PORTDEBUG (%s): ch=%u, sc=%u, vk=%d, pre=%d, sh=0x%X, ta=%d (ESC to end)",
			shortdllname, ch, scan, vk, pre_ch, shiftstate, k);
		fprintf(stdout, "\n%s", buf);
	}
#endif
	return ch;
}


int __declspec(dllexport) __stdcall
NHkbhit(hConIn, ir)
HANDLE hConIn;
INPUT_RECORD *ir;
{
	int done = 0;	/* true =  "stop searching"        */
	int retval;	/* true =  "we had a match"        */
	DWORD count;
	unsigned short int scan;
	unsigned char ch;
	unsigned long shiftstate;
	int altseq = 0, keycode, vk;
	done = 0;
	retval = 0;
	while (!done)
	{
	    count = 0;
	    PeekConsoleInput(hConIn,ir,1,&count);
	    if (count > 0) {
		if (ir->EventType == KEY_EVENT && ir->Event.KeyEvent.bKeyDown) {
			ch    = ir->Event.KeyEvent.uChar.AsciiChar;
			scan  = ir->Event.KeyEvent.wVirtualScanCode;
			shiftstate = ir->Event.KeyEvent.dwControlKeyState;
			vk = ir->Event.KeyEvent.wVirtualKeyCode;
			keycode = MapVirtualKey(vk, 2);
			if (shiftstate & (LEFT_ALT_PRESSED|RIGHT_ALT_PRESSED)) {
				if  (ch || inmap(keycode,vk)) altseq = 1;
				else altseq = -1;	/* invalid altseq */
			}
			if (ch || iskeypad(scan) || altseq) {
				done = 1;	    /* Stop looking         */
				retval = 1;         /* Found what we sought */
			} else {
				/* Strange Key event; let's purge it to avoid trouble */
				ReadConsoleInput(hConIn,ir,1,&count);
			}

		}
		else if ((ir->EventType == MOUSE_EVENT &&
		  (ir->Event.MouseEvent.dwButtonState & MOUSEMASK))) {
			done = 1;
			retval = 1;
		}

		else /* Discard it, it's an insignificant event */
			ReadConsoleInput(hConIn,ir,1,&count);
		} else  /* There are no events in console event queue */ {
		done = 1;	  /* Stop looking               */
		retval = 0;
	    }
	}
	return retval;
}

int __declspec(dllexport) __stdcall
CheckInput(hConIn, ir, count, numpad, mode, mod, cc)
HANDLE hConIn;
INPUT_RECORD *ir;
DWORD *count; 
boolean numpad;
int mode;
int *mod;
coord *cc;
{
	int ch;
	boolean valid = 0, done = 0;
	while (!done) {
		ReadConsoleInput(hConIn,ir,1,count);
		if (mode == 0) {
		    if ((ir->EventType == KEY_EVENT) && ir->Event.KeyEvent.bKeyDown) {
			ch = ProcessKeystroke(hConIn, ir, &valid, numpad, 0);
			done = valid;
		    }
		} else {
		    if (count > 0) {
			if (ir->EventType == KEY_EVENT && ir->Event.KeyEvent.bKeyDown) {
			    ch = ProcessKeystroke(hConIn, ir, &valid, numpad, 0);
			    if (valid) return ch;
			} else if (ir->EventType == MOUSE_EVENT) {
			    if ((ir->Event.MouseEvent.dwEventFlags == 0) &&
		   	        (ir->Event.MouseEvent.dwButtonState & MOUSEMASK)) {
			  	    cc->x = ir->Event.MouseEvent.dwMousePosition.X + 1;
			  	    cc->y = ir->Event.MouseEvent.dwMousePosition.Y - 1;

				    if (ir->Event.MouseEvent.dwButtonState & LEFTBUTTON)
		  	       		*mod = CLICK_1;
				    else if (ir->Event.MouseEvent.dwButtonState & RIGHTBUTTON)
					*mod = CLICK_2;
#if 0	/* middle button */
				    else if (ir->Event.MouseEvent.dwButtonState & MIDBUTTON)
			      		*mod = CLICK_3;
#endif 
			           return 0;
			    }
	        	}
		    } else 
			done = 1;
		}
	}
	return mode ? 0 : ch;
}

int __declspec(dllexport) __stdcall
SourceWhere(buf)
char **buf;
{
	if (!buf) return 0;
	*buf = where_to_get_source;
	return 1;
}

int __declspec(dllexport) __stdcall
SourceAuthor(buf)
char **buf;
{
	if (!buf) return 0;
	*buf = author;
	return 1;
}

int __declspec(dllexport) __stdcall
KeyHandlerName(buf, full)
char **buf;
int full;
{
	if (!buf) return 0;
	if (full) *buf = dllname;
	else *buf = shortdllname;
	return 1;
}