2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/ioctl.h>
42 #if defined(__FreeBSD__)
44 #elif defined(__linux__)
52 static tf_bell_t test_bell;
53 static tf_cursor_t test_cursor;
54 static tf_putchar_t test_putchar;
55 static tf_fill_t test_fill;
56 static tf_copy_t test_copy;
57 static tf_param_t test_param;
58 static tf_respond_t test_respond;
60 static teken_funcs_t tf = {
62 .tf_cursor = test_cursor,
63 .tf_putchar = test_putchar,
66 .tf_param = test_param,
67 .tf_respond = test_respond,
77 static struct pixel buffer[NCOLS][NROWS];
82 printchar(const teken_pos_t *p)
88 assert(p->tp_row < NROWS);
89 assert(p->tp_col < NCOLS);
91 px = &buffer[p->tp_col][p->tp_row];
92 /* No need to print right hand side of CJK character manually. */
93 if (px->a.ta_format & TF_CJK_RIGHT)
96 /* Convert Unicode to UTF-8. */
99 } else if (px->c < 0x800) {
100 str[0] = 0xc0 | (px->c >> 6);
101 str[1] = 0x80 | (px->c & 0x3f);
102 } else if (px->c < 0x10000) {
103 str[0] = 0xe0 | (px->c >> 12);
104 str[1] = 0x80 | ((px->c >> 6) & 0x3f);
105 str[2] = 0x80 | (px->c & 0x3f);
107 str[0] = 0xf0 | (px->c >> 18);
108 str[1] = 0x80 | ((px->c >> 12) & 0x3f);
109 str[2] = 0x80 | ((px->c >> 6) & 0x3f);
110 str[3] = 0x80 | (px->c & 0x3f);
113 if (px->a.ta_format & TF_BOLD)
115 if (px->a.ta_format & TF_UNDERLINE)
117 if (px->a.ta_format & TF_BLINK)
119 if (px->a.ta_format & TF_REVERSE)
122 bkgdset(attr | COLOR_PAIR(teken_256to8(px->a.ta_fgcolor) +
123 8 * teken_256to8(px->a.ta_bgcolor)));
125 mvaddstr(p->tp_row, p->tp_col, str);
130 test_bell(void *s __unused)
137 test_cursor(void *s __unused, const teken_pos_t *p)
140 move(p->tp_row, p->tp_col);
144 test_putchar(void *s __unused, const teken_pos_t *p, teken_char_t c,
145 const teken_attr_t *a)
148 buffer[p->tp_col][p->tp_row].c = c;
149 buffer[p->tp_col][p->tp_row].a = *a;
154 test_fill(void *s, const teken_rect_t *r, teken_char_t c,
155 const teken_attr_t *a)
159 /* Braindead implementation of fill() - just call putchar(). */
160 for (p.tp_row = r->tr_begin.tp_row; p.tp_row < r->tr_end.tp_row; p.tp_row++)
161 for (p.tp_col = r->tr_begin.tp_col; p.tp_col < r->tr_end.tp_col; p.tp_col++)
162 test_putchar(s, &p, c, a);
166 test_copy(void *s __unused, const teken_rect_t *r, const teken_pos_t *p)
168 int nrow, ncol, x, y; /* Has to be signed - >= 0 comparison */
172 * Copying is a little tricky. We must make sure we do it in
173 * correct order, to make sure we don't overwrite our own data.
176 nrow = r->tr_end.tp_row - r->tr_begin.tp_row;
177 ncol = r->tr_end.tp_col - r->tr_begin.tp_col;
179 if (p->tp_row < r->tr_begin.tp_row) {
180 /* Copy from top to bottom. */
181 if (p->tp_col < r->tr_begin.tp_col) {
182 /* Copy from left to right. */
183 for (y = 0; y < nrow; y++) {
184 d.tp_row = p->tp_row + y;
185 for (x = 0; x < ncol; x++) {
186 d.tp_col = p->tp_col + x;
187 buffer[d.tp_col][d.tp_row] =
188 buffer[r->tr_begin.tp_col + x][r->tr_begin.tp_row + y];
193 /* Copy from right to left. */
194 for (y = 0; y < nrow; y++) {
195 d.tp_row = p->tp_row + y;
196 for (x = ncol - 1; x >= 0; x--) {
197 d.tp_col = p->tp_col + x;
198 buffer[d.tp_col][d.tp_row] =
199 buffer[r->tr_begin.tp_col + x][r->tr_begin.tp_row + y];
205 /* Copy from bottom to top. */
206 if (p->tp_col < r->tr_begin.tp_col) {
207 /* Copy from left to right. */
208 for (y = nrow - 1; y >= 0; y--) {
209 d.tp_row = p->tp_row + y;
210 for (x = 0; x < ncol; x++) {
211 d.tp_col = p->tp_col + x;
212 buffer[d.tp_col][d.tp_row] =
213 buffer[r->tr_begin.tp_col + x][r->tr_begin.tp_row + y];
218 /* Copy from right to left. */
219 for (y = nrow - 1; y >= 0; y--) {
220 d.tp_row = p->tp_row + y;
221 for (x = ncol - 1; x >= 0; x--) {
222 d.tp_col = p->tp_col + x;
223 buffer[d.tp_col][d.tp_row] =
224 buffer[r->tr_begin.tp_col + x][r->tr_begin.tp_row + y];
233 test_param(void *s __unused, int cmd, unsigned int value)
241 keypad(stdscr, value ? TRUE : FALSE);
247 test_respond(void *s __unused, const void *buf, size_t len)
250 write(ptfd, buf, len);
258 for (i = 0; i < NROWS; i++)
259 mvaddch(i, NCOLS, '|');
260 for (i = 0; i < NCOLS; i++)
261 mvaddch(NROWS, i, '-');
263 mvaddch(NROWS, NCOLS, '+');
271 for (tp.tp_row = 0; tp.tp_row < NROWS; tp.tp_row++)
272 for (tp.tp_col = 0; tp.tp_col < NCOLS; tp.tp_col++)
279 main(int argc __unused, char *argv[] __unused)
287 const int ccolors[8] = {
288 COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW,
289 COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE
293 setlocale(LC_CTYPE, "UTF-8");
295 tp.tp_row = ws.ws_row = NROWS;
296 tp.tp_col = ws.ws_col = NCOLS;
298 switch (forkpty(&ptfd, NULL, NULL, &ws)) {
303 setenv("TERM", "xterm", 1);
304 setenv("LC_CTYPE", "UTF-8", 0);
305 execlp("zsh", "-zsh", NULL);
306 execlp("bash", "-bash", NULL);
307 execlp("sh", "-sh", NULL);
311 teken_init(&t, &tf, NULL);
312 teken_set_winsize(&t, &tp);
317 for (i = 0; i < 8; i++)
318 for (j = 0; j < 8; j++)
319 init_pair(i + 8 * j, ccolors[i], ccolors[j]);
326 FD_SET(STDIN_FILENO, &rfds);
329 if (select(ptfd + 1, &rfds, NULL, NULL, NULL) < 0) {
330 if (errno == EINTR) {
338 if (FD_ISSET(STDIN_FILENO, &rfds)) {
339 bl = read(STDIN_FILENO, b, sizeof b);
345 if (FD_ISSET(ptfd, &rfds)) {
346 bl = read(ptfd, b, sizeof b);
349 teken_input(&t, b, bl);