Subversion Repositories HelenOS-historic

Rev

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

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