NHDT->ANH, nethack->anethack, nhdat->anhdat
[aNetHack.git] / win / win32 / mhinput.c
blob3a8c0f34482231d7987bb1f5bc8cd6f3ff601f8d
1 /* aNetHack 0.0.1 mhinput.c $ANH-Date: 1432512810 2015/05/25 00:13:30 $ $ANH-Branch: master $:$ANH-Revision: 1.11 $ */
2 /* Copyright (C) 2001 by Alex Kompel */
3 /* aNetHack may be freely redistributed. See license for details. */
5 #include <assert.h>
6 #include "winMS.h"
7 #include "mhinput.h"
9 /* anethack input queue functions */
11 #define NH_INPUT_BUFFER_SIZE 64
13 /* as it stands right now we need only one slot
14 since events are processed almost the same time as
15 they occur but I like large round numbers */
17 static MSNHEvent nhi_input_buffer[NH_INPUT_BUFFER_SIZE];
18 static int nhi_init_input = 0;
19 static int nhi_read_pos = 0;
20 static int nhi_write_pos = 0;
22 /* initialize input queue */
23 void
24 mswin_nh_input_init(void)
26 if (!nhi_init_input) {
27 nhi_init_input = 1;
29 ZeroMemory(nhi_input_buffer, sizeof(nhi_input_buffer));
30 nhi_read_pos = 0;
31 nhi_write_pos = 0;
35 /* check for input */
36 int
37 mswin_have_input()
39 return
40 #ifdef SAFERHANGUP
41 /* we always have input (ESC) if hangup was requested */
42 program_state.done_hup ||
43 #endif
44 (nhi_read_pos != nhi_write_pos);
47 /* add event to the queue */
48 void
49 mswin_input_push(PMSNHEvent event)
51 int new_write_pos;
53 if (!nhi_init_input)
54 mswin_nh_input_init();
56 new_write_pos = (nhi_write_pos + 1) % NH_INPUT_BUFFER_SIZE;
58 if (new_write_pos != nhi_read_pos) {
59 memcpy(nhi_input_buffer + nhi_write_pos, event, sizeof(*event));
60 nhi_write_pos = new_write_pos;
64 /* get event from the queue and delete it */
65 PMSNHEvent
66 mswin_input_pop()
68 PMSNHEvent retval;
70 #ifdef SAFERHANGUP
71 /* always return ESC when hangup was requested */
72 if (program_state.done_hup) {
73 static MSNHEvent hangup_event;
74 hangup_event.type = NHEVENT_CHAR;
75 hangup_event.kbd.ch = '\033';
76 return &hangup_event;
78 #endif
80 if (!nhi_init_input)
81 mswin_nh_input_init();
83 if (nhi_read_pos != nhi_write_pos) {
84 retval = &nhi_input_buffer[nhi_read_pos];
85 nhi_read_pos = (nhi_read_pos + 1) % NH_INPUT_BUFFER_SIZE;
86 } else {
87 retval = NULL;
90 return retval;
93 /* get event from the queue but leave it there */
94 PMSNHEvent
95 mswin_input_peek()
97 PMSNHEvent retval;
99 #ifdef SAFERHANGUP
100 /* always return ESC when hangup was requested */
101 if (program_state.done_hup) {
102 static MSNHEvent hangup_event;
103 hangup_event.type = NHEVENT_CHAR;
104 hangup_event.kbd.ch = '\033';
105 return &hangup_event;
107 #endif
109 if (!nhi_init_input)
110 mswin_nh_input_init();
112 if (nhi_read_pos != nhi_write_pos) {
113 retval = &nhi_input_buffer[nhi_read_pos];
114 } else {
115 retval = NULL;
117 return retval;