Subversion Repositories HelenOS-historic

Rev

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

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