File: mhinput.c

package info (click to toggle)
nethack 3.6.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,468 kB
  • sloc: ansic: 266,495; cpp: 13,652; yacc: 2,903; perl: 1,426; lex: 581; sh: 535; xml: 372; awk: 98; makefile: 68; fortran: 51; sed: 11
file content (118 lines) | stat: -rw-r--r-- 2,744 bytes parent folder | download | duplicates (6)
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
/* NetHack 3.6	mhinput.c	$NHDT-Date: 1432512798 2015/05/25 00:13:18 $  $NHDT-Branch: master $:$NHDT-Revision: 1.11 $ */
/* Copyright (C) 2001 by Alex Kompel 	 */
/* NetHack may be freely redistributed.  See license for details. */

#include <assert.h>
#include "winMS.h"
#include "mhinput.h"

/* nethack input queue functions */

#define NH_INPUT_BUFFER_SIZE 64

/* as it stands right now we need only one slot
   since events are processed almost the same time as
   they occur but I like large round numbers */

static MSNHEvent nhi_input_buffer[NH_INPUT_BUFFER_SIZE];
static int nhi_init_input = 0;
static int nhi_read_pos = 0;
static int nhi_write_pos = 0;

/* initialize input queue */
void
mswin_nh_input_init()
{
    if (!nhi_init_input) {
        nhi_init_input = 1;

        ZeroMemory(nhi_input_buffer, sizeof(nhi_input_buffer));
        nhi_read_pos = 0;
        nhi_write_pos = 0;
    }
}

/* check for input */
int
mswin_have_input()
{
    return
#ifdef SAFERHANGUP
        /* we always have input (ESC) if hangup was requested */
        program_state.done_hup ||
#endif
        (nhi_read_pos != nhi_write_pos);
}

/* add event to the queue */
void
mswin_input_push(PMSNHEvent event)
{
    int new_write_pos;

    if (!nhi_init_input)
        mswin_nh_input_init();

    new_write_pos = (nhi_write_pos + 1) % NH_INPUT_BUFFER_SIZE;

    if (new_write_pos != nhi_read_pos) {
        memcpy(nhi_input_buffer + nhi_write_pos, event, sizeof(*event));
        nhi_write_pos = new_write_pos;
    }
}

/* get event from the queue and delete it */
PMSNHEvent
mswin_input_pop()
{
    PMSNHEvent retval;

#ifdef SAFERHANGUP
    /* always return ESC when hangup was requested */
    if (program_state.done_hup) {
        static MSNHEvent hangup_event;
        hangup_event.type = NHEVENT_CHAR;
        hangup_event.kbd.ch = '\033';
        return &hangup_event;
    }
#endif

    if (!nhi_init_input)
        mswin_nh_input_init();

    if (nhi_read_pos != nhi_write_pos) {
        retval = &nhi_input_buffer[nhi_read_pos];
        nhi_read_pos = (nhi_read_pos + 1) % NH_INPUT_BUFFER_SIZE;
    } else {
        retval = NULL;
    }

    return retval;
}

/* get event from the queue but leave it there */
PMSNHEvent
mswin_input_peek()
{
    PMSNHEvent retval;

#ifdef SAFERHANGUP
    /* always return ESC when hangup was requested */
    if (program_state.done_hup) {
        static MSNHEvent hangup_event;
        hangup_event.type = NHEVENT_CHAR;
        hangup_event.kbd.ch = '\033';
        return &hangup_event;
    }
#endif

    if (!nhi_init_input)
        mswin_nh_input_init();

    if (nhi_read_pos != nhi_write_pos) {
        retval = &nhi_input_buffer[nhi_read_pos];
    } else {
        retval = NULL;
    }
    return retval;
}