Subversion Repositories HelenOS-historic

Rev

Rev 1533 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1533 Rev 1653
1
/*  $OpenBSD: input.c,v 1.12 2005/04/13 02:33:08 deraadt Exp $  */
1
/*  $OpenBSD: input.c,v 1.12 2005/04/13 02:33:08 deraadt Exp $  */
2
/*    $NetBSD: input.c,v 1.3 1996/02/06 22:47:33 jtc Exp $    */
2
/*    $NetBSD: input.c,v 1.3 1996/02/06 22:47:33 jtc Exp $    */
3
 
3
 
4
/*-
4
/*-
5
 * Copyright (c) 1992, 1993
5
 * Copyright (c) 1992, 1993
6
 *  The Regents of the University of California.  All rights reserved.
6
 *  The Regents of the University of California.  All rights reserved.
7
 *
7
 *
8
 * This code is derived from software contributed to Berkeley by
8
 * This code is derived from software contributed to Berkeley by
9
 * Chris Torek and Darren F. Provine.
9
 * Chris Torek and Darren F. Provine.
10
 *
10
 *
11
 * Redistribution and use in source and binary forms, with or without
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
12
 * modification, are permitted provided that the following conditions
13
 * are met:
13
 * are met:
14
 * 1. Redistributions of source code must retain the above copyright
14
 * 1. Redistributions of source code must retain the above copyright
15
 *    notice, this list of conditions and the following disclaimer.
15
 *    notice, this list of conditions and the following disclaimer.
16
 * 2. Redistributions in binary form must reproduce the above copyright
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in the
17
 *    notice, this list of conditions and the following disclaimer in the
18
 *    documentation and/or other materials provided with the distribution.
18
 *    documentation and/or other materials provided with the distribution.
19
 * 3. Neither the name of the University nor the names of its contributors
19
 * 3. Neither the name of the University nor the names of its contributors
20
 *    may be used to endorse or promote products derived from this software
20
 *    may be used to endorse or promote products derived from this software
21
 *    without specific prior written permission.
21
 *    without specific prior written permission.
22
 *
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33
 * SUCH DAMAGE.
33
 * SUCH DAMAGE.
34
 *
34
 *
35
 *  @(#)input.c 8.1 (Berkeley) 5/31/93
35
 *  @(#)input.c 8.1 (Berkeley) 5/31/93
36
 */
36
 */
37
 
37
 
-
 
38
/** @addtogroup tetris
-
 
39
 * @{
-
 
40
 */
-
 
41
/** @file
-
 
42
 */
-
 
43
 
38
/*
44
/*
39
 * Tetris input.
45
 * Tetris input.
40
 */
46
 */
41
 
47
 
42
#include <sys/types.h>
48
#include <sys/types.h>
43
#include <sys/time.h>
49
#include <sys/time.h>
44
#include <stdio.h>
50
#include <stdio.h>
45
 
51
 
46
#include <errno.h>
52
#include <errno.h>
47
#include <unistd.h>
53
#include <unistd.h>
48
#include <string.h>
54
#include <string.h>
49
 
55
 
50
#include "input.h"
56
#include "input.h"
51
#include "tetris.h"
57
#include "tetris.h"
52
 
58
 
53
#include <async.h>
59
#include <async.h>
54
#include "../console/console.h"
60
#include "../console/console.h"
55
 
61
 
56
/* return true iff the given timeval is positive */
62
/* return true iff the given timeval is positive */
57
#define TV_POS(tv) \
63
#define TV_POS(tv) \
58
    ((tv)->tv_sec > 0 || ((tv)->tv_sec == 0 && (tv)->tv_usec > 0))
64
    ((tv)->tv_sec > 0 || ((tv)->tv_sec == 0 && (tv)->tv_usec > 0))
59
 
65
 
60
/* subtract timeval `sub' from `res' */
66
/* subtract timeval `sub' from `res' */
61
#define TV_SUB(res, sub) \
67
#define TV_SUB(res, sub) \
62
    (res)->tv_sec -= (sub)->tv_sec; \
68
    (res)->tv_sec -= (sub)->tv_sec; \
63
    (res)->tv_usec -= (sub)->tv_usec; \
69
    (res)->tv_usec -= (sub)->tv_usec; \
64
    if ((res)->tv_usec < 0) { \
70
    if ((res)->tv_usec < 0) { \
65
        (res)->tv_usec += 1000000; \
71
        (res)->tv_usec += 1000000; \
66
        (res)->tv_sec--; \
72
        (res)->tv_sec--; \
67
    }
73
    }
68
 
74
 
69
/* We will use a hack here - if lastchar is non-zero, it is
75
/* We will use a hack here - if lastchar is non-zero, it is
70
 * the last character read. We will somehow simulate the select
76
 * the last character read. We will somehow simulate the select
71
 * semantics.
77
 * semantics.
72
 */
78
 */
73
static aid_t getchar_inprog = 0;
79
static aid_t getchar_inprog = 0;
74
static char lastchar = '\0';
80
static char lastchar = '\0';
75
 
81
 
76
/*
82
/*
77
 * Do a `read wait': select for reading from stdin, with timeout *tvp.
83
 * Do a `read wait': select for reading from stdin, with timeout *tvp.
78
 * On return, modify *tvp to reflect the amount of time spent waiting.
84
 * On return, modify *tvp to reflect the amount of time spent waiting.
79
 * It will be positive only if input appeared before the time ran out;
85
 * It will be positive only if input appeared before the time ran out;
80
 * otherwise it will be zero or perhaps negative.
86
 * otherwise it will be zero or perhaps negative.
81
 *
87
 *
82
 * If tvp is nil, wait forever, but return if select is interrupted.
88
 * If tvp is nil, wait forever, but return if select is interrupted.
83
 *
89
 *
84
 * Return 0 => no input, 1 => can read() from stdin
90
 * Return 0 => no input, 1 => can read() from stdin
85
 *
91
 *
86
 */
92
 */
87
int
93
int
88
rwait(struct timeval *tvp)
94
rwait(struct timeval *tvp)
89
{
95
{
90
    struct timeval starttv, endtv, *s;
96
    struct timeval starttv, endtv, *s;
91
    static ipc_call_t charcall;
97
    static ipc_call_t charcall;
92
    ipcarg_t rc;
98
    ipcarg_t rc;
93
 
99
 
94
    /*
100
    /*
95
     * Someday, select() will do this for us.
101
     * Someday, select() will do this for us.
96
     * Just in case that day is now, and no one has
102
     * Just in case that day is now, and no one has
97
     * changed this, we use a temporary.
103
     * changed this, we use a temporary.
98
     */
104
     */
99
    if (tvp) {
105
    if (tvp) {
100
        (void) gettimeofday(&starttv, NULL);
106
        (void) gettimeofday(&starttv, NULL);
101
        endtv = *tvp;
107
        endtv = *tvp;
102
        s = &endtv;
108
        s = &endtv;
103
    } else
109
    } else
104
        s = NULL;
110
        s = NULL;
105
again:
111
again:
106
    if (!lastchar) {
112
    if (!lastchar) {
107
        if (!getchar_inprog)
113
        if (!getchar_inprog)
108
            getchar_inprog = async_send_2(1,CONSOLE_GETCHAR,0,0,&charcall);
114
            getchar_inprog = async_send_2(1,CONSOLE_GETCHAR,0,0,&charcall);
109
        if (!s)
115
        if (!s)
110
            async_wait_for(getchar_inprog, &rc);
116
            async_wait_for(getchar_inprog, &rc);
111
        else if (async_wait_timeout(getchar_inprog, &rc, s->tv_usec) == ETIMEOUT) {
117
        else if (async_wait_timeout(getchar_inprog, &rc, s->tv_usec) == ETIMEOUT) {
112
            tvp->tv_sec = 0;
118
            tvp->tv_sec = 0;
113
            tvp->tv_usec = 0;
119
            tvp->tv_usec = 0;
114
            return (0);
120
            return (0);
115
        }
121
        }
116
        getchar_inprog = 0;
122
        getchar_inprog = 0;
117
        if (rc) {
123
        if (rc) {
118
            stop("end of file, help");
124
            stop("end of file, help");
119
        }
125
        }
120
        lastchar = IPC_GET_ARG1(charcall);
126
        lastchar = IPC_GET_ARG1(charcall);
121
    }
127
    }
122
    if (tvp) {
128
    if (tvp) {
123
        /* since there is input, we may not have timed out */
129
        /* since there is input, we may not have timed out */
124
        (void) gettimeofday(&endtv, NULL);
130
        (void) gettimeofday(&endtv, NULL);
125
        TV_SUB(&endtv, &starttv);
131
        TV_SUB(&endtv, &starttv);
126
        TV_SUB(tvp, &endtv);    /* adjust *tvp by elapsed time */
132
        TV_SUB(tvp, &endtv);    /* adjust *tvp by elapsed time */
127
    }
133
    }
128
    return (1);
134
    return (1);
129
}
135
}
130
 
136
 
131
/*
137
/*
132
 * `sleep' for the current turn time (using select).
138
 * `sleep' for the current turn time (using select).
133
 * Eat any input that might be available.
139
 * Eat any input that might be available.
134
 */
140
 */
135
void
141
void
136
tsleep(void)
142
tsleep(void)
137
{
143
{
138
    struct timeval tv;
144
    struct timeval tv;
139
    char c;
145
    char c;
140
 
146
 
141
    tv.tv_sec = 0;
147
    tv.tv_sec = 0;
142
    tv.tv_usec = fallrate;
148
    tv.tv_usec = fallrate;
143
    while (TV_POS(&tv))
149
    while (TV_POS(&tv))
144
        if (rwait(&tv)) {
150
        if (rwait(&tv)) {
145
            lastchar = '\0';
151
            lastchar = '\0';
146
        } else
152
        } else
147
            break;
153
            break;
148
}
154
}
149
 
155
 
150
/*
156
/*
151
 * getchar with timeout.
157
 * getchar with timeout.
152
 */
158
 */
153
int
159
int
154
tgetchar(void)
160
tgetchar(void)
155
{
161
{
156
    static struct timeval timeleft;
162
    static struct timeval timeleft;
157
    char c;
163
    char c;
158
 
164
 
159
    /*
165
    /*
160
     * Reset timeleft to fallrate whenever it is not positive.
166
     * Reset timeleft to fallrate whenever it is not positive.
161
     * In any case, wait to see if there is any input.  If so,
167
     * In any case, wait to see if there is any input.  If so,
162
     * take it, and update timeleft so that the next call to
168
     * take it, and update timeleft so that the next call to
163
     * tgetchar() will not wait as long.  If there is no input,
169
     * tgetchar() will not wait as long.  If there is no input,
164
     * make timeleft zero or negative, and return -1.
170
     * make timeleft zero or negative, and return -1.
165
     *
171
     *
166
     * Most of the hard work is done by rwait().
172
     * Most of the hard work is done by rwait().
167
     */
173
     */
168
    if (!TV_POS(&timeleft)) {
174
    if (!TV_POS(&timeleft)) {
169
        faster();   /* go faster */
175
        faster();   /* go faster */
170
        timeleft.tv_sec = 0;
176
        timeleft.tv_sec = 0;
171
        timeleft.tv_usec = fallrate;
177
        timeleft.tv_usec = fallrate;
172
    }
178
    }
173
    if (!rwait(&timeleft))
179
    if (!rwait(&timeleft))
174
        return (-1);
180
        return (-1);
175
    c = lastchar;
181
    c = lastchar;
176
    lastchar = '\0';
182
    lastchar = '\0';
177
    return ((int)(unsigned char)c);
183
    return ((int)(unsigned char)c);
178
}
184
}
-
 
185
 
-
 
186
/** @}
-
 
187
 */
-
 
188
 
179
 
189