Subversion Repositories HelenOS

Rev

Rev 1965 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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