Subversion Repositories HelenOS-historic

Rev

Rev 1466 | Rev 1532 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1466 Rev 1469
Line 48... Line 48...
48
 
48
 
49
#include "input.h"
49
#include "input.h"
50
#include "tetris.h"
50
#include "tetris.h"
51
 
51
 
52
#include <async.h>
52
#include <async.h>
-
 
53
#include "../console/console.h"
53
 
54
 
54
/* return true iff the given timeval is positive */
55
/* return true iff the given timeval is positive */
55
#define TV_POS(tv) \
56
#define TV_POS(tv) \
56
    ((tv)->tv_sec > 0 || ((tv)->tv_sec == 0 && (tv)->tv_usec > 0))
57
    ((tv)->tv_sec > 0 || ((tv)->tv_sec == 0 && (tv)->tv_usec > 0))
57
 
58
 
Line 62... Line 63...
62
    if ((res)->tv_usec < 0) { \
63
    if ((res)->tv_usec < 0) { \
63
        (res)->tv_usec += 1000000; \
64
        (res)->tv_usec += 1000000; \
64
        (res)->tv_sec--; \
65
        (res)->tv_sec--; \
65
    }
66
    }
66
 
67
 
-
 
68
/* We will use a hack here - if lastchar is non-zero, it is
-
 
69
 * the last character read. We will somehow simulate the select
-
 
70
 * semantics.
-
 
71
 */
67
static aid_t getchar_inprog = 0;
72
static aid_t getchar_inprog = 0;
-
 
73
static char lastchar = '\0';
68
 
74
 
69
/*
75
/*
70
 * Do a `read wait': select for reading from stdin, with timeout *tvp.
76
 * Do a `read wait': select for reading from stdin, with timeout *tvp.
71
 * On return, modify *tvp to reflect the amount of time spent waiting.
77
 * On return, modify *tvp to reflect the amount of time spent waiting.
72
 * It will be positive only if input appeared before the time ran out;
78
 * It will be positive only if input appeared before the time ran out;
73
 * otherwise it will be zero or perhaps negative.
79
 * otherwise it will be zero or perhaps negative.
74
 *
80
 *
75
 * If tvp is nil, wait forever, but return if select is interrupted.
81
 * If tvp is nil, wait forever, but return if select is interrupted.
76
 *
82
 *
77
 * Return 0 => no input, 1 => can read() from stdin
83
 * Return 0 => no input, 1 => can read() from stdin
-
 
84
 *
78
 */
85
 */
79
int
86
int
80
rwait(struct timeval *tvp)
87
rwait(struct timeval *tvp)
81
{
88
{
82
    struct timeval starttv, endtv, *s;
89
    struct timeval starttv, endtv, *s;
-
 
90
    static ipc_call_t charcall;
83
    fd_set fds;
91
    int rc;
84
 
92
 
85
#define NILTZ ((struct timezone *)0)
93
#define NILTZ ((struct timezone *)0)
86
 
94
 
87
    /*
95
    /*
88
     * Someday, select() will do this for us.
96
     * Someday, select() will do this for us.
Line 94... Line 102...
94
        endtv = *tvp;
102
        endtv = *tvp;
95
        s = &endtv;
103
        s = &endtv;
96
    } else
104
    } else
97
        s = NULL;
105
        s = NULL;
98
again:
106
again:
99
    FD_ZERO(&fds);
107
    if (!lastchar) {
100
    FD_SET(STDIN_FILENO, &fds);
108
        if (!getchar_inprog)
-
 
109
            getchar_inprog = async_send_2(1,CONSOLE_GETCHAR,0,0,&charcall);
101
    switch (select(STDIN_FILENO + 1, &fds, (fd_set *)0, (fd_set *)0, s)) {
110
        if (async_wait_timeout(getchar_inprog, &rc, s->tv_usec) == ETIMEOUT) {
102
 
-
 
103
    case -1:
111
            tvp->tv_sec = 0;
104
        if (tvp == 0)
112
            tvp->tv_usec = 0;
105
            return (-1);
113
            return (0);
-
 
114
        }
106
        if (errno == EINTR)
115
        getchar_inprog = 0;
107
            goto again;
116
        if (rc) {
108
        stop("select failed, help");
117
            stop("end of file, help");
109
        /* NOTREACHED */
-
 
110
 
118
        }
111
    case 0: /* timed out */
119
        lastchar = IPC_GET_ARG1(charcall);
112
        tvp->tv_sec = 0;
-
 
113
        tvp->tv_usec = 0;
-
 
114
        return (0);
-
 
115
    }
120
    }
116
    if (tvp) {
121
    if (tvp) {
117
        /* since there is input, we may not have timed out */
122
        /* since there is input, we may not have timed out */
118
        (void) gettimeofday(&endtv, NILTZ);
123
        (void) gettimeofday(&endtv, NILTZ);
119
        TV_SUB(&endtv, &starttv);
124
        TV_SUB(&endtv, &starttv);
Line 133... Line 138...
133
    char c;
138
    char c;
134
 
139
 
135
    tv.tv_sec = 0;
140
    tv.tv_sec = 0;
136
    tv.tv_usec = fallrate;
141
    tv.tv_usec = fallrate;
137
    while (TV_POS(&tv))
142
    while (TV_POS(&tv))
138
        if (rwait(&tv) && read(STDIN_FILENO, &c, 1) != 1)
143
        if (rwait(&tv)) {
-
 
144
            lastchar = '\0';
-
 
145
        } else
139
            break;
146
            break;
140
}
147
}
141
 
148
 
142
/*
149
/*
143
 * getchar with timeout.
150
 * getchar with timeout.
Line 162... Line 169...
162
        timeleft.tv_sec = 0;
169
        timeleft.tv_sec = 0;
163
        timeleft.tv_usec = fallrate;
170
        timeleft.tv_usec = fallrate;
164
    }
171
    }
165
    if (!rwait(&timeleft))
172
    if (!rwait(&timeleft))
166
        return (-1);
173
        return (-1);
167
    if (read(STDIN_FILENO, &c, 1) != 1)
174
    c = lastchar;
168
        stop("end of file, help");
175
    lastchar = '\0';
169
    return ((int)(unsigned char)c);
176
    return ((int)(unsigned char)c);
170
}
177
}