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,
75 struct pixel buffer[NCOLS][NROWS];
80 printchar(const teken_pos_t *p)
86 assert(p->tp_row < NROWS);
87 assert(p->tp_col < NCOLS);
91 px = &buffer[p->tp_col][p->tp_row];
93 /* Convert Unicode to UTF-8. */
96 } else if (px->c < 0x800) {
97 str[0] = 0xc0 | (px->c >> 6);
98 str[1] = 0x80 | (px->c & 0x3f);
99 } else if (px->c < 0x10000) {
100 str[0] = 0xe0 | (px->c >> 12);
101 str[1] = 0x80 | ((px->c >> 6) & 0x3f);
102 str[2] = 0x80 | (px->c & 0x3f);
104 str[0] = 0xf0 | (px->c >> 18);
105 str[1] = 0x80 | ((px->c >> 12) & 0x3f);
106 str[2] = 0x80 | ((px->c >> 6) & 0x3f);
107 str[3] = 0x80 | (px->c & 0x3f);
110 if (px->a.ta_format & TF_BOLD)
112 if (px->a.ta_format & TF_UNDERLINE)
114 if (px->a.ta_format & TF_BLINK)
116 if (px->a.ta_format & TF_REVERSE)
119 bkgdset(attr | COLOR_PAIR(teken_256to8(px->a.ta_fgcolor) +
120 8 * teken_256to8(px->a.ta_bgcolor)));
121 mvaddstr(p->tp_row, p->tp_col, str);
127 test_bell(void *s __unused)
134 test_cursor(void *s __unused, const teken_pos_t *p)
137 move(p->tp_row, p->tp_col);
141 test_putchar(void *s __unused, const teken_pos_t *p, teken_char_t c,
142 const teken_attr_t *a)
145 buffer[p->tp_col][p->tp_row].c = c;
146 buffer[p->tp_col][p->tp_row].a = *a;
151 test_fill(void *s, const teken_rect_t *r, teken_char_t c,
152 const teken_attr_t *a)
156 /* Braindead implementation of fill() - just call putchar(). */
157 for (p.tp_row = r->tr_begin.tp_row; p.tp_row < r->tr_end.tp_row; p.tp_row++)
158 for (p.tp_col = r->tr_begin.tp_col; p.tp_col < r->tr_end.tp_col; p.tp_col++)
159 test_putchar(s, &p, c, a);
163 test_copy(void *s __unused, const teken_rect_t *r, const teken_pos_t *p)
165 int nrow, ncol, x, y; /* Has to be signed - >= 0 comparison */
169 * Copying is a little tricky. We must make sure we do it in
170 * correct order, to make sure we don't overwrite our own data.
173 nrow = r->tr_end.tp_row - r->tr_begin.tp_row;
174 ncol = r->tr_end.tp_col - r->tr_begin.tp_col;
176 if (p->tp_row < r->tr_begin.tp_row) {
177 /* Copy from top to bottom. */
178 if (p->tp_col < r->tr_begin.tp_col) {
179 /* Copy from left to right. */
180 for (y = 0; y < nrow; y++) {
181 d.tp_row = p->tp_row + y;
182 for (x = 0; x < ncol; x++) {
183 d.tp_col = p->tp_col + x;
184 buffer[d.tp_col][d.tp_row] =
185 buffer[r->tr_begin.tp_col + x][r->tr_begin.tp_row + y];
190 /* Copy from right to left. */
191 for (y = 0; y < nrow; y++) {
192 d.tp_row = p->tp_row + y;
193 for (x = ncol - 1; x >= 0; x--) {
194 d.tp_col = p->tp_col + x;
195 buffer[d.tp_col][d.tp_row] =
196 buffer[r->tr_begin.tp_col + x][r->tr_begin.tp_row + y];
202 /* Copy from bottom to top. */
203 if (p->tp_col < r->tr_begin.tp_col) {
204 /* Copy from left to right. */
205 for (y = nrow - 1; y >= 0; y--) {
206 d.tp_row = p->tp_row + y;
207 for (x = 0; x < ncol; x++) {
208 d.tp_col = p->tp_col + x;
209 buffer[d.tp_col][d.tp_row] =
210 buffer[r->tr_begin.tp_col + x][r->tr_begin.tp_row + y];
215 /* Copy from right to left. */
216 for (y = nrow - 1; y >= 0; y--) {
217 d.tp_row = p->tp_row + y;
218 for (x = ncol - 1; x >= 0; x--) {
219 d.tp_col = p->tp_col + x;
220 buffer[d.tp_col][d.tp_row] =
221 buffer[r->tr_begin.tp_col + x][r->tr_begin.tp_row + y];
230 test_param(void *s __unused, int cmd, unsigned int value)
238 keypad(stdscr, value ? TRUE : FALSE);
244 test_respond(void *s __unused, const void *buf, size_t len)
247 write(ptfd, buf, len);
255 for (i = 0; i < NROWS; i++)
256 mvaddch(i, NCOLS, '|');
257 for (i = 0; i < NCOLS; i++)
258 mvaddch(NROWS, i, '-');
260 mvaddch(NROWS, NCOLS, '+');
268 for (tp.tp_row = 0; tp.tp_row < NROWS; tp.tp_row++)
269 for (tp.tp_col = 0; tp.tp_col < NCOLS; tp.tp_col++)
276 main(int argc __unused, char *argv[] __unused)
284 const int ccolors[8] = {
285 COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW,
286 COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE
290 setlocale(LC_CTYPE, "UTF-8");
292 tp.tp_row = ws.ws_row = NROWS;
293 tp.tp_col = ws.ws_col = NCOLS;
295 switch (forkpty(&ptfd, NULL, NULL, &ws)) {
300 setenv("TERM", "xterm", 1);
301 setenv("LC_CTYPE", "UTF-8", 0);
302 execlp("zsh", "-zsh", NULL);
303 execlp("bash", "-bash", NULL);
304 execlp("sh", "-sh", NULL);
308 teken_init(&t, &tf, NULL);
309 teken_set_winsize(&t, &tp);
314 for (i = 0; i < 8; i++)
315 for (j = 0; j < 8; j++)
316 init_pair(i + 8 * j, ccolors[i], ccolors[j]);
323 FD_SET(STDIN_FILENO, &rfds);
326 if (select(ptfd + 1, &rfds, NULL, NULL, NULL) < 0) {
327 if (errno == EINTR) {
335 if (FD_ISSET(STDIN_FILENO, &rfds)) {
336 bl = read(STDIN_FILENO, b, sizeof b);
342 if (FD_ISSET(ptfd, &rfds)) {
343 bl = read(ptfd, b, sizeof b);
346 teken_input(&t, b, bl);