Subversion Repositories HelenOS-historic

Rev

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

Rev 1524 Rev 1528
1
/*  $OpenBSD: screen.c,v 1.13 2006/04/20 03:25:36 ray Exp $ */
1
/*  $OpenBSD: screen.c,v 1.13 2006/04/20 03:25:36 ray Exp $ */
2
/*  $NetBSD: screen.c,v 1.4 1995/04/29 01:11:36 mycroft Exp $   */
2
/*  $NetBSD: screen.c,v 1.4 1995/04/29 01:11:36 mycroft 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
 *  @(#)screen.c    8.1 (Berkeley) 5/31/93
35
 *  @(#)screen.c    8.1 (Berkeley) 5/31/93
36
 */
36
 */
37
 
37
 
38
/*
38
/*
39
 * Tetris screen control.
39
 * Tetris screen control.
40
 */
40
 */
41
 
41
 
42
#include <err.h>
42
#include <err.h>
43
#include <stdio.h>
43
#include <stdio.h>
44
#include <stdlib.h>
44
#include <stdlib.h>
45
#include <string.h>
45
#include <string.h>
46
#include <unistd.h>
46
#include <unistd.h>
47
#include <io/stream.h>
47
#include <io/stream.h>
48
 
48
 
49
 
49
 
50
#include <async.h>
50
#include <async.h>
51
#include "screen.h"
51
#include "screen.h"
52
#include "tetris.h"
52
#include "tetris.h"
53
#include "../console/console.h"
53
#include "../console/console.h"
54
 
54
 
55
static cell curscreen[B_SIZE];  /* 1 => standout (or otherwise marked) */
55
static cell curscreen[B_SIZE];  /* 1 => standout (or otherwise marked) */
56
static int curscore;
56
static int curscore;
57
static int isset;       /* true => terminal is in game mode */
57
static int isset;       /* true => terminal is in game mode */
58
static struct termios oldtt;
58
static struct termios oldtt;
59
static void (*tstp)(int);
59
static void (*tstp)(int);
60
 
60
 
61
static void scr_stop(int);
61
static void scr_stop(int);
62
static void stopset(int);
62
static void stopset(int);
63
 
63
 
64
static char
64
static char
65
        *CEstr;         /* clear to end of line */
65
        *CEstr;         /* clear to end of line */
66
 
66
 
67
 
67
 
68
/*
68
/*
69
 * putstr() is for unpadded strings (either as in termcap(5) or
69
 * putstr() is for unpadded strings (either as in termcap(5) or
70
 * simply literal strings);
70
 * simply literal strings);
71
 */
71
 */
72
#define putstr(s)   puts(s)
72
#define putstr(s)   puts(s)
73
 
73
 
74
static int con_phone;
74
static int con_phone;
75
 
75
 
76
 
76
 
77
 
77
 
78
static void set_style(int fgcolor, int bgcolor)
78
static void set_style(int fgcolor, int bgcolor)
79
{
79
{
80
    send_call_2(con_phone, CONSOLE_SET_STYLE, fgcolor, bgcolor);
80
    send_call_2(con_phone, CONSOLE_SET_STYLE, fgcolor, bgcolor);
81
}
81
}
82
 
82
 
83
static void start_standout(void)
83
static void start_standout(void)
84
{
84
{
85
    set_style(0, 0xe0e0e0);
85
    set_style(0, 0xe0e0e0);
86
}
86
}
87
 
87
 
88
static void resume_normal(void)
88
static void resume_normal(void)
89
{
89
{
90
    set_style(0xe0e0e0, 0);
90
    set_style(0xe0e0e0, 0);
91
}
91
}
92
 
92
 
93
/*
93
/*
94
 * Clear the screen, forgetting the current contents in the process.
94
 * Clear the screen, forgetting the current contents in the process.
95
 */
95
 */
96
void
96
void
97
scr_clear(void)
97
scr_clear(void)
98
{
98
{
99
 
99
 
-
 
100
    resume_normal();
100
    send_call(con_phone, CONSOLE_CLEAR, 0);
101
    send_call(con_phone, CONSOLE_CLEAR, 0);
101
    curscore = -1;
102
    curscore = -1;
102
    memset((char *)curscreen, 0, sizeof(curscreen));
103
    memset((char *)curscreen, 0, sizeof(curscreen));
103
}
104
}
104
 
105
 
105
/*
106
/*
106
 * Set up screen
107
 * Set up screen
107
 */
108
 */
108
void
109
void
109
scr_init(void)
110
scr_init(void)
110
{
111
{
111
    con_phone = get_fd_phone(1);
112
    con_phone = get_fd_phone(1);
112
    resume_normal();
113
    resume_normal();
113
    scr_clear();
114
    scr_clear();
114
}
115
}
115
 
116
 
116
static void moveto(int r, int c)
117
static void moveto(int r, int c)
117
{
118
{
118
    send_call_2(con_phone, CONSOLE_GOTO, r, c);
119
    send_call_2(con_phone, CONSOLE_GOTO, r, c);
119
}
120
}
120
 
121
 
121
static void fflush(void)
122
static void fflush(void)
122
{
123
{
123
    send_call(con_phone, CONSOLE_FLUSH, 0);
124
    send_call(con_phone, CONSOLE_FLUSH, 0);
124
}
125
}
125
 
126
 
126
struct winsize {
127
struct winsize {
127
    ipcarg_t ws_row;
128
    ipcarg_t ws_row;
128
    ipcarg_t ws_col;
129
    ipcarg_t ws_col;
129
};
130
};
130
 
131
 
131
static int get_display_size(struct winsize *ws)
132
static int get_display_size(struct winsize *ws)
132
{
133
{
133
    return sync_send_2(con_phone, CONSOLE_GETSIZE, 0, 0, &ws->ws_row, &ws->ws_col);
134
    return sync_send_2(con_phone, CONSOLE_GETSIZE, 0, 0, &ws->ws_row, &ws->ws_col);
134
}
135
}
135
 
136
 
136
static void
137
static void
137
scr_stop(int sig)
138
scr_stop(int sig)
138
{
139
{
139
 
140
 
140
    scr_end();
141
    scr_end();
141
    scr_set();
142
    scr_set();
142
    scr_msg(key_msg, 1);
143
    scr_msg(key_msg, 1);
143
}
144
}
144
 
145
 
145
/*
146
/*
146
 * Set up screen mode.
147
 * Set up screen mode.
147
 */
148
 */
148
void
149
void
149
scr_set(void)
150
scr_set(void)
150
{
151
{
151
    struct winsize ws;
152
    struct winsize ws;
152
 
153
 
153
    Rows = 0, Cols = 0;
154
    Rows = 0, Cols = 0;
154
    if (get_display_size(&ws) == 0) {
155
    if (get_display_size(&ws) == 0) {
155
        Rows = ws.ws_row;
156
        Rows = ws.ws_row;
156
        Cols = ws.ws_col;
157
        Cols = ws.ws_col;
157
    }
158
    }
158
    if (Rows < MINROWS || Cols < MINCOLS) {
159
    if (Rows < MINROWS || Cols < MINCOLS) {
159
        char smallscr[55];
160
        char smallscr[55];
160
 
161
 
161
        (void)snprintf(smallscr, sizeof(smallscr),
162
        (void)snprintf(smallscr, sizeof(smallscr),
162
            "the screen is too small (must be at least %dx%d)",
163
            "the screen is too small (must be at least %dx%d)",
163
            MINROWS, MINCOLS);
164
            MINROWS, MINCOLS);
164
        stop(smallscr);
165
        stop(smallscr);
165
    }
166
    }
166
    isset = 1;
167
    isset = 1;
167
 
168
 
168
    scr_clear();
169
    scr_clear();
169
}
170
}
170
 
171
 
171
/*
172
/*
172
 * End screen mode.
173
 * End screen mode.
173
 */
174
 */
174
void
175
void
175
scr_end(void)
176
scr_end(void)
176
{
177
{
177
}
178
}
178
 
179
 
179
void
180
void
180
stop(char *why)
181
stop(char *why)
181
{
182
{
182
 
183
 
183
    if (isset)
184
    if (isset)
184
        scr_end();
185
        scr_end();
185
    errx(1, "aborting: %s", why);
186
    errx(1, "aborting: %s", why);
186
}
187
}
187
 
188
 
188
 
189
 
189
#if vax && !__GNUC__
-
 
190
typedef int regcell;    /* pcc is bad at `register char', etc */
-
 
191
#else
-
 
192
typedef cell regcell;
-
 
193
#endif
-
 
194
 
-
 
195
/*
190
/*
196
 * Update the screen.
191
 * Update the screen.
197
 */
192
 */
198
void
193
void
199
scr_update(void)
194
scr_update(void)
200
{
195
{
201
    cell *bp, *sp;
196
    cell *bp, *sp;
202
    regcell so, cur_so = 0;
197
    cell so, cur_so = 0;
203
    int i, ccol, j;
198
    int i, ccol, j;
204
    static const struct shape *lastshape;
199
    static const struct shape *lastshape;
205
 
200
 
206
    /* always leave cursor after last displayed point */
201
    /* always leave cursor after last displayed point */
207
    curscreen[D_LAST * B_COLS - 1] = -1;
202
    curscreen[D_LAST * B_COLS - 1] = -1;
208
 
203
 
209
    if (score != curscore) {
204
    if (score != curscore) {
210
        moveto(0, 0);
205
        moveto(0, 0);
211
        (void) printf("Score: %d", score);
206
        (void) printf("Score: %d", score);
212
        curscore = score;
207
        curscore = score;
213
    }
208
    }
214
 
209
 
215
    /* draw preview of next pattern */
210
    /* draw preview of next pattern */
216
    if (showpreview && (nextshape != lastshape)) {
211
    if (showpreview && (nextshape != lastshape)) {
217
        int i;
212
        int i;
218
        static int r=5, c=2;
213
        static int r=5, c=2;
219
        int tr, tc, t;
214
        int tr, tc, t;
220
 
215
 
221
        lastshape = nextshape;
216
        lastshape = nextshape;
222
 
217
 
223
        /* clean */
218
        /* clean */
224
        resume_normal();
219
        resume_normal();
225
        moveto(r-1, c-1); putstr("          ");
220
        moveto(r-1, c-1); putstr("          ");
226
        moveto(r,   c-1); putstr("          ");
221
        moveto(r,   c-1); putstr("          ");
227
        moveto(r+1, c-1); putstr("          ");
222
        moveto(r+1, c-1); putstr("          ");
228
        moveto(r+2, c-1); putstr("          ");
223
        moveto(r+2, c-1); putstr("          ");
229
 
224
 
230
        moveto(r-3, c-2);
225
        moveto(r-3, c-2);
231
        putstr("Next shape:");
226
        putstr("Next shape:");
232
 
227
 
233
        /* draw */
228
        /* draw */
234
        start_standout();
229
        start_standout();
235
        moveto(r, 2 * c);
230
        moveto(r, 2 * c);
236
        putstr("  ");
231
        putstr("  ");
237
        for (i = 0; i < 3; i++) {
232
        for (i = 0; i < 3; i++) {
238
            t = c + r * B_COLS;
233
            t = c + r * B_COLS;
239
            t += nextshape->off[i];
234
            t += nextshape->off[i];
240
 
235
 
241
            tr = t / B_COLS;
236
            tr = t / B_COLS;
242
            tc = t % B_COLS;
237
            tc = t % B_COLS;
243
 
238
 
244
            moveto(tr, 2*tc);
239
            moveto(tr, 2*tc);
245
            putstr("  ");
240
            putstr("  ");
246
        }
241
        }
247
        resume_normal();
242
        resume_normal();
248
    }
243
    }
249
 
244
 
250
    bp = &board[D_FIRST * B_COLS];
245
    bp = &board[D_FIRST * B_COLS];
251
    sp = &curscreen[D_FIRST * B_COLS];
246
    sp = &curscreen[D_FIRST * B_COLS];
252
    for (j = D_FIRST; j < D_LAST; j++) {
247
    for (j = D_FIRST; j < D_LAST; j++) {
253
        ccol = -1;
248
        ccol = -1;
254
        for (i = 0; i < B_COLS; bp++, sp++, i++) {
249
        for (i = 0; i < B_COLS; bp++, sp++, i++) {
255
            if (*sp == (so = *bp))
250
            if (*sp == (so = *bp))
256
                continue;
251
                continue;
257
            *sp = so;
252
            *sp = so;
258
            if (i != ccol) {
253
            if (i != ccol) {
259
                moveto(RTOD(j), CTOD(i));
254
                moveto(RTOD(j), CTOD(i));
260
            }
255
            }
261
            if (so != cur_so) {
256
            if (so != cur_so) {
262
                if (so)
257
                if (so)
263
                    start_standout();
258
                    start_standout();
264
                else
259
                else
265
                    resume_normal();
260
                    resume_normal();
266
                cur_so = so;
261
                cur_so = so;
267
            }
262
            }
268
            putstr("  ");
263
            putstr("  ");
269
            ccol = i + 1;
264
            ccol = i + 1;
270
            /*
265
            /*
271
             * Look ahead a bit, to avoid extra motion if
266
             * Look ahead a bit, to avoid extra motion if
272
             * we will be redrawing the cell after the next.
267
             * we will be redrawing the cell after the next.
273
             * Motion probably takes four or more characters,
268
             * Motion probably takes four or more characters,
274
             * so we save even if we rewrite two cells
269
             * so we save even if we rewrite two cells
275
             * `unnecessarily'.  Skip it all, though, if
270
             * `unnecessarily'.  Skip it all, though, if
276
             * the next cell is a different color.
271
             * the next cell is a different color.
277
             */
272
             */
278
#define STOP (B_COLS - 3)
273
#define STOP (B_COLS - 3)
279
            if (i > STOP || sp[1] != bp[1] || so != bp[1])
274
            if (i > STOP || sp[1] != bp[1] || so != bp[1])
280
                continue;
275
                continue;
281
            if (sp[2] != bp[2])
276
            if (sp[2] != bp[2])
282
                sp[1] = -1;
277
                sp[1] = -1;
283
            else if (i < STOP && so == bp[2] && sp[3] != bp[3]) {
278
            else if (i < STOP && so == bp[2] && sp[3] != bp[3]) {
284
                sp[2] = -1;
279
                sp[2] = -1;
285
                sp[1] = -1;
280
                sp[1] = -1;
286
            }
281
            }
287
        }
282
        }
288
    }
283
    }
289
    resume_normal();
284
    resume_normal();
290
 
285
 
291
    fflush();
286
    fflush();
292
}
287
}
293
 
288
 
294
/*
289
/*
295
 * Write a message (set!=0), or clear the same message (set==0).
290
 * Write a message (set!=0), or clear the same message (set==0).
296
 * (We need its length in case we have to overwrite with blanks.)
291
 * (We need its length in case we have to overwrite with blanks.)
297
 */
292
 */
298
void
293
void
299
scr_msg(char *s, int set)
294
scr_msg(char *s, int set)
300
{
295
{
301
   
296
   
302
    if (set || CEstr == NULL) {
-
 
303
        int l = strlen(s);
297
    int l = strlen(s);
304
 
298
   
305
        moveto(Rows - 2, ((Cols - l) >> 1) - 1);
299
    moveto(Rows - 2, ((Cols - l) >> 1) - 1);
306
        if (set)
300
    if (set)
307
            putstr(s);
301
        putstr(s);
308
        else
302
    else
309
            while (--l >= 0)
303
        while (--l >= 0)
310
                (void) putchar(' ');
304
            (void) putchar(' ');
311
    } else {
-
 
312
        moveto(Rows - 2, 0);
-
 
313
        putpad(CEstr);
-
 
314
    }
-
 
315
}
305
}
316
 
306