2 * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/ioctl.h>
40 #if defined(__FreeBSD__)
42 #elif defined(__linux__)
50 static tf_bell_t test_bell;
51 static tf_cursor_t test_cursor;
52 static tf_putchar_t test_putchar;
53 static tf_fill_t test_fill;
54 static tf_copy_t test_copy;
55 static tf_param_t test_param;
56 static tf_respond_t test_respond;
58 static teken_funcs_t tf = {
60 .tf_cursor = test_cursor,
61 .tf_putchar = test_putchar,
64 .tf_param = test_param,
65 .tf_respond = test_respond,
76 #else /* !TEKEN_XTERM */
78 #endif /* TEKEN_XTERM */
79 struct pixel buffer[NCOLS][NROWS];
84 printchar(const teken_pos_t *p)
90 assert(p->tp_row < NROWS);
91 assert(p->tp_col < NCOLS);
95 px = &buffer[p->tp_col][p->tp_row];
97 /* Convert Unicode to UTF-8. */
101 } else if (px->c < 0x800) {
102 str[0] = 0xc0 | (px->c >> 6);
103 str[1] = 0x80 | (px->c & 0x3f);
104 } else if (px->c < 0x10000) {
105 str[0] = 0xe0 | (px->c >> 12);
106 str[1] = 0x80 | ((px->c >> 6) & 0x3f);
107 str[2] = 0x80 | (px->c & 0x3f);
109 str[0] = 0xf0 | (px->c >> 18);
110 str[1] = 0x80 | ((px->c >> 12) & 0x3f);
111 str[2] = 0x80 | ((px->c >> 6) & 0x3f);
112 str[3] = 0x80 | (px->c & 0x3f);
114 #else /* !TEKEN_UTF8 */
116 #endif /* TEKEN_UTF8 */
118 if (px->a.ta_format & TF_BOLD)
120 if (px->a.ta_format & TF_UNDERLINE)
122 if (px->a.ta_format & TF_BLINK)
125 bkgdset(attr | COLOR_PAIR(px->a.ta_fgcolor + 8 * px->a.ta_bgcolor));
126 mvaddstr(p->tp_row, p->tp_col, str);
132 test_bell(void *s __unused)
139 test_cursor(void *s __unused, const teken_pos_t *p)
142 move(p->tp_row, p->tp_col);
146 test_putchar(void *s __unused, const teken_pos_t *p, teken_char_t c,
147 const teken_attr_t *a)
150 buffer[p->tp_col][p->tp_row].c = c;
151 buffer[p->tp_col][p->tp_row].a = *a;
156 test_fill(void *s, const teken_rect_t *r, teken_char_t c,
157 const teken_attr_t *a)
161 /* Braindead implementation of fill() - just call putchar(). */
162 for (p.tp_row = r->tr_begin.tp_row; p.tp_row < r->tr_end.tp_row; p.tp_row++)
163 for (p.tp_col = r->tr_begin.tp_col; p.tp_col < r->tr_end.tp_col; p.tp_col++)
164 test_putchar(s, &p, c, a);
168 test_copy(void *s __unused, const teken_rect_t *r, const teken_pos_t *p)
170 int nrow, ncol, x, y; /* Has to be signed - >= 0 comparison */
174 * Copying is a little tricky. We must make sure we do it in
175 * correct order, to make sure we don't overwrite our own data.
178 nrow = r->tr_end.tp_row - r->tr_begin.tp_row;
179 ncol = r->tr_end.tp_col - r->tr_begin.tp_col;
181 if (p->tp_row < r->tr_begin.tp_row) {
182 /* Copy from top to bottom. */
183 if (p->tp_col < r->tr_begin.tp_col) {
184 /* Copy from left to right. */
185 for (y = 0; y < nrow; y++) {
186 d.tp_row = p->tp_row + y;
187 for (x = 0; x < ncol; x++) {
188 d.tp_col = p->tp_col + x;
189 buffer[d.tp_col][d.tp_row] =
190 buffer[r->tr_begin.tp_col + x][r->tr_begin.tp_row + y];
195 /* Copy from right to left. */
196 for (y = 0; y < nrow; y++) {
197 d.tp_row = p->tp_row + y;
198 for (x = ncol - 1; x >= 0; x--) {
199 d.tp_col = p->tp_col + x;
200 buffer[d.tp_col][d.tp_row] =
201 buffer[r->tr_begin.tp_col + x][r->tr_begin.tp_row + y];
207 /* Copy from bottom to top. */
208 if (p->tp_col < r->tr_begin.tp_col) {
209 /* Copy from left to right. */
210 for (y = nrow - 1; y >= 0; y--) {
211 d.tp_row = p->tp_row + y;
212 for (x = 0; x < ncol; x++) {
213 d.tp_col = p->tp_col + x;
214 buffer[d.tp_col][d.tp_row] =
215 buffer[r->tr_begin.tp_col + x][r->tr_begin.tp_row + y];
220 /* Copy from right to left. */
221 for (y = nrow - 1; y >= 0; y--) {
222 d.tp_row = p->tp_row + y;
223 for (x = ncol - 1; x >= 0; x--) {
224 d.tp_col = p->tp_col + x;
225 buffer[d.tp_col][d.tp_row] =
226 buffer[r->tr_begin.tp_col + x][r->tr_begin.tp_row + y];
235 test_param(void *s __unused, int cmd, unsigned int value)
243 keypad(stdscr, value ? TRUE : FALSE);
249 test_respond(void *s __unused, const void *buf, size_t len)
252 write(ptfd, buf, len);
260 for (i = 0; i < NROWS; i++)
261 mvaddch(i, NCOLS, '|');
262 for (i = 0; i < NCOLS; i++)
263 mvaddch(NROWS, i, '-');
265 mvaddch(NROWS, NCOLS, '+');
273 for (tp.tp_row = 0; tp.tp_row < NROWS; tp.tp_row++)
274 for (tp.tp_col = 0; tp.tp_col < NCOLS; tp.tp_col++)
281 main(int argc __unused, char *argv[] __unused)
289 const int ccolors[8] = {
290 COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW,
291 COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE
296 setlocale(LC_CTYPE, "UTF-8");
297 #endif /* TEKEN_UTF8 */
299 tp.tp_row = ws.ws_row = NROWS;
300 tp.tp_col = ws.ws_col = NCOLS;
302 switch (forkpty(&ptfd, NULL, NULL, &ws)) {
308 setenv("TERM", "xterm", 1);
309 #else /* !TEKEN_XTERM */
310 setenv("TERM", "cons25", 1);
311 #endif /* TEKEN_XTERM */
313 setenv("LC_CTYPE", "UTF-8", 0);
314 #endif /* TEKEN_UTF8 */
315 execlp("zsh", "-zsh", NULL);
316 execlp("bash", "-bash", NULL);
317 execlp("sh", "-sh", NULL);
321 teken_init(&t, &tf, NULL);
322 teken_set_winsize(&t, &tp);
327 for (i = 0; i < 8; i++)
328 for (j = 0; j < 8; j++)
329 init_pair(i + 8 * j, ccolors[i], ccolors[j]);
336 FD_SET(STDIN_FILENO, &rfds);
339 if (select(ptfd + 1, &rfds, NULL, NULL, NULL) < 0) {
340 if (errno == EINTR) {
348 if (FD_ISSET(STDIN_FILENO, &rfds)) {
349 bl = read(STDIN_FILENO, b, sizeof b);
355 if (FD_ISSET(ptfd, &rfds)) {
356 bl = read(ptfd, b, sizeof b);
359 teken_input(&t, b, bl);