1 /*
2  * UAE - The Un*x Amiga Emulator
3  *
4  * Keyboard buffer. Not really needed for X, but for SVGAlib and possibly
5  * Mac and DOS ports.
6  *
7  * Note: it's possible to have two threads in UAE, one reading keystrokes
8  * and the other one writing them. Despite this, no synchronization effort
9  * is needed. This code should be perfectly thread safe. At least if you
10  * assume that integer store instructions are atomic.
11  *
12  * Copyright 1995, 1997 Bernd Schmidt
13  */
14 
15 #include "sysconfig.h"
16 #include "sysdeps.h"
17 #include <assert.h>
18 
19 #include "options.h"
20 #include "keybuf.h"
21 #include "keyboard.h"
22 #include "inputdevice.h"
23 #include "custom.h"
24 #include "savestate.h"
25 
26 static int kpb_first, kpb_last;
27 
28 #define KEYBUF_SIZE 256
29 static int keybuf[KEYBUF_SIZE];
30 
keys_available(void)31 int keys_available (void)
32 {
33 	int val;
34 	val = kpb_first != kpb_last;
35 	return val;
36 }
37 
get_next_key(void)38 int get_next_key (void)
39 {
40 	int key;
41 	assert (kpb_first != kpb_last);
42 
43 	key = keybuf[kpb_last];
44 	if (++kpb_last == KEYBUF_SIZE)
45 		kpb_last = 0;
46 	//write_log (_T("%02x:%d\n"), key >> 1, key & 1);
47 	return key;
48 }
49 
record_key(int kc)50 int record_key (int kc)
51 {
52 	if (pause_emulation)
53 		return 0;
54 	return record_key_direct (kc);
55 }
56 
record_key_direct(int kc)57 int record_key_direct (int kc)
58 {
59 	int fs = 0;
60 	int kpb_next = kpb_first + 1;
61 	int k = kc >> 1;
62 	int b = !(kc & 1);
63 
64 	//write_log (_T("got kc %02X\n"), ((kc << 7) | (kc >> 1)) & 0xff);
65 	if (kpb_next == KEYBUF_SIZE)
66 		kpb_next = 0;
67 	if (kpb_next == kpb_last) {
68 		write_log (_T("Keyboard buffer overrun. Congratulations.\n"));
69 		return 0;
70 	}
71 #if 0
72 	if ((kc >> 1) == AK_RCTRL) {
73 		kc ^= AK_RCTRL << 1;
74 		kc ^= AK_CTRL << 1;
75 	}
76 #endif
77 	keybuf[kpb_first] = kc;
78 	kpb_first = kpb_next;
79 	return 1;
80 }
81 
keybuf_init(void)82 void keybuf_init (void)
83 {
84 	kpb_first = kpb_last = 0;
85 	inputdevice_updateconfig (&changed_prefs, &currprefs);
86 }
87