xref: /freebsd/sys/teken/demo/teken_demo.c (revision 2f02600abfddfc4e9f20dd384a2e729b451e16bd)
1 /*-
2  * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/ioctl.h>
30 
31 #include <assert.h>
32 #include <errno.h>
33 #include <inttypes.h>
34 #include <locale.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 
39 #include <ncurses.h>
40 #if defined(__FreeBSD__)
41 #include <libutil.h>
42 #elif defined(__linux__)
43 #include <pty.h>
44 #else
45 #include <util.h>
46 #endif
47 
48 #include <teken.h>
49 
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;
57 
58 static teken_funcs_t tf = {
59 	.tf_bell	= test_bell,
60 	.tf_cursor	= test_cursor,
61 	.tf_putchar	= test_putchar,
62 	.tf_fill	= test_fill,
63 	.tf_copy	= test_copy,
64 	.tf_param	= test_param,
65 	.tf_respond	= test_respond,
66 };
67 
68 struct pixel {
69 	teken_char_t	c;
70 	teken_attr_t	a;
71 };
72 
73 #define NCOLS	80
74 #define NROWS	24
75 struct pixel buffer[NCOLS][NROWS];
76 
77 static int ptfd;
78 
79 static void
80 printchar(const teken_pos_t *p)
81 {
82 	int y, x, attr = 0;
83 	struct pixel *px;
84 	char str[5] = { 0 };
85 
86 	assert(p->tp_row < NROWS);
87 	assert(p->tp_col < NCOLS);
88 
89 	getyx(stdscr, y, x);
90 
91 	px = &buffer[p->tp_col][p->tp_row];
92 
93 	/* Convert Unicode to UTF-8. */
94 	if (px->c < 0x80) {
95 		str[0] = px->c;
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);
103 	} else {
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);
108 	}
109 
110 	if (px->a.ta_format & TF_BOLD)
111 		attr |= A_BOLD;
112 	if (px->a.ta_format & TF_UNDERLINE)
113 		attr |= A_UNDERLINE;
114 	if (px->a.ta_format & TF_BLINK)
115 		attr |= A_BLINK;
116 	if (px->a.ta_format & TF_REVERSE)
117 		attr |= A_REVERSE;
118 
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);
122 
123 	move(y, x);
124 }
125 
126 static void
127 test_bell(void *s __unused)
128 {
129 
130 	beep();
131 }
132 
133 static void
134 test_cursor(void *s __unused, const teken_pos_t *p)
135 {
136 
137 	move(p->tp_row, p->tp_col);
138 }
139 
140 static void
141 test_putchar(void *s __unused, const teken_pos_t *p, teken_char_t c,
142     const teken_attr_t *a)
143 {
144 
145 	buffer[p->tp_col][p->tp_row].c = c;
146 	buffer[p->tp_col][p->tp_row].a = *a;
147 	printchar(p);
148 }
149 
150 static void
151 test_fill(void *s, const teken_rect_t *r, teken_char_t c,
152     const teken_attr_t *a)
153 {
154 	teken_pos_t p;
155 
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);
160 }
161 
162 static void
163 test_copy(void *s __unused, const teken_rect_t *r, const teken_pos_t *p)
164 {
165 	int nrow, ncol, x, y; /* Has to be signed - >= 0 comparison */
166 	teken_pos_t d;
167 
168 	/*
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.
171 	 */
172 
173 	nrow = r->tr_end.tp_row - r->tr_begin.tp_row;
174 	ncol = r->tr_end.tp_col - r->tr_begin.tp_col;
175 
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];
186 					printchar(&d);
187 				}
188 			}
189 		} else {
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];
197 					printchar(&d);
198 				}
199 			}
200 		}
201 	} else {
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];
211 					printchar(&d);
212 				}
213 			}
214 		} else {
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];
222 					printchar(&d);
223 				}
224 			}
225 		}
226 	}
227 }
228 
229 static void
230 test_param(void *s __unused, int cmd, unsigned int value)
231 {
232 
233 	switch (cmd) {
234 	case TP_SHOWCURSOR:
235 		curs_set(value);
236 		break;
237 	case TP_KEYPADAPP:
238 		keypad(stdscr, value ? TRUE : FALSE);
239 		break;
240 	}
241 }
242 
243 static void
244 test_respond(void *s __unused, const void *buf, size_t len)
245 {
246 
247 	write(ptfd, buf, len);
248 }
249 
250 static void
251 redraw_border(void)
252 {
253 	unsigned int i;
254 
255 	for (i = 0; i < NROWS; i++)
256 		mvaddch(i, NCOLS, '|');
257 	for (i = 0; i < NCOLS; i++)
258 		mvaddch(NROWS, i, '-');
259 
260 	mvaddch(NROWS, NCOLS, '+');
261 }
262 
263 static void
264 redraw_all(void)
265 {
266 	teken_pos_t tp;
267 
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++)
270 			printchar(&tp);
271 
272 	redraw_border();
273 }
274 
275 int
276 main(int argc __unused, char *argv[] __unused)
277 {
278 	struct winsize ws;
279 	teken_t t;
280 	teken_pos_t tp;
281 	fd_set rfds;
282 	char b[256];
283 	ssize_t bl;
284 	const int ccolors[8] = {
285 	    COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW,
286 	    COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE
287 	};
288 	int i, j;
289 
290 	setlocale(LC_CTYPE, "UTF-8");
291 
292 	tp.tp_row = ws.ws_row = NROWS;
293 	tp.tp_col = ws.ws_col = NCOLS;
294 
295 	switch (forkpty(&ptfd, NULL, NULL, &ws)) {
296 	case -1:
297 		perror("forkpty");
298 		exit(1);
299 	case 0:
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);
305 		_exit(1);
306 	}
307 
308 	teken_init(&t, &tf, NULL);
309 	teken_set_winsize(&t, &tp);
310 
311 	initscr();
312 	raw();
313 	start_color();
314 	for (i = 0; i < 8; i++)
315 		for (j = 0; j < 8; j++)
316 			init_pair(i + 8 * j, ccolors[i], ccolors[j]);
317 
318 	redraw_border();
319 
320 	FD_ZERO(&rfds);
321 
322 	for (;;) {
323 		FD_SET(STDIN_FILENO, &rfds);
324 		FD_SET(ptfd, &rfds);
325 
326 		if (select(ptfd + 1, &rfds, NULL, NULL, NULL) < 0) {
327 			if (errno == EINTR) {
328 				redraw_all();
329 				refresh();
330 				continue;
331 			}
332 			break;
333 		}
334 
335 		if (FD_ISSET(STDIN_FILENO, &rfds)) {
336 			bl = read(STDIN_FILENO, b, sizeof b);
337 			if (bl <= 0)
338 				break;
339 			write(ptfd, b, bl);
340 		}
341 
342 		if (FD_ISSET(ptfd, &rfds)) {
343 			bl = read(ptfd, b, sizeof b);
344 			if (bl <= 0)
345 				break;
346 			teken_input(&t, b, bl);
347 			refresh();
348 		}
349 	}
350 
351 	endwin();
352 
353 	return (0);
354 }
355