Subversion Repositories HelenOS

Rev

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

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