Subversion Repositories HelenOS

Rev

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

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