Subversion Repositories HelenOS-historic

Rev

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

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