Subversion Repositories HelenOS

Rev

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

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