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 *
| 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 *
|
123 move(y, x); 124} 125 126static void 127test_bell(void *s __unused) 128{ 129 130 beep(); 131} 132 133static void 134test_cursor(void *s __unused, const teken_pos_t *p) 135{ 136 137 move(p->tp_row, p->tp_col); 138} 139 140static void 141test_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 150static void 151test_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 162static void 163test_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 229static void 230test_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 243static void 244test_respond(void *s __unused, const void *buf, size_t len) 245{ 246 247 write(ptfd, buf, len); 248} 249 250static void 251redraw_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 263static void 264redraw_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 275int 276main(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}
| 124 move(y, x); 125} 126 127static void 128test_bell(void *s __unused) 129{ 130 131 beep(); 132} 133 134static void 135test_cursor(void *s __unused, const teken_pos_t *p) 136{ 137 138 move(p->tp_row, p->tp_col); 139} 140 141static void 142test_putchar(void *s __unused, const teken_pos_t *p, teken_char_t c, 143 const teken_attr_t *a) 144{ 145 146 buffer[p->tp_col][p->tp_row].c = c; 147 buffer[p->tp_col][p->tp_row].a = *a; 148 printchar(p); 149} 150 151static void 152test_fill(void *s, const teken_rect_t *r, teken_char_t c, 153 const teken_attr_t *a) 154{ 155 teken_pos_t p; 156 157 /* Braindead implementation of fill() - just call putchar(). */ 158 for (p.tp_row = r->tr_begin.tp_row; p.tp_row < r->tr_end.tp_row; p.tp_row++) 159 for (p.tp_col = r->tr_begin.tp_col; p.tp_col < r->tr_end.tp_col; p.tp_col++) 160 test_putchar(s, &p, c, a); 161} 162 163static void 164test_copy(void *s __unused, const teken_rect_t *r, const teken_pos_t *p) 165{ 166 int nrow, ncol, x, y; /* Has to be signed - >= 0 comparison */ 167 teken_pos_t d; 168 169 /* 170 * Copying is a little tricky. We must make sure we do it in 171 * correct order, to make sure we don't overwrite our own data. 172 */ 173 174 nrow = r->tr_end.tp_row - r->tr_begin.tp_row; 175 ncol = r->tr_end.tp_col - r->tr_begin.tp_col; 176 177 if (p->tp_row < r->tr_begin.tp_row) { 178 /* Copy from top to bottom. */ 179 if (p->tp_col < r->tr_begin.tp_col) { 180 /* Copy from left to right. */ 181 for (y = 0; y < nrow; y++) { 182 d.tp_row = p->tp_row + y; 183 for (x = 0; x < ncol; x++) { 184 d.tp_col = p->tp_col + x; 185 buffer[d.tp_col][d.tp_row] = 186 buffer[r->tr_begin.tp_col + x][r->tr_begin.tp_row + y]; 187 printchar(&d); 188 } 189 } 190 } else { 191 /* Copy from right to left. */ 192 for (y = 0; y < nrow; y++) { 193 d.tp_row = p->tp_row + y; 194 for (x = ncol - 1; x >= 0; x--) { 195 d.tp_col = p->tp_col + x; 196 buffer[d.tp_col][d.tp_row] = 197 buffer[r->tr_begin.tp_col + x][r->tr_begin.tp_row + y]; 198 printchar(&d); 199 } 200 } 201 } 202 } else { 203 /* Copy from bottom to top. */ 204 if (p->tp_col < r->tr_begin.tp_col) { 205 /* Copy from left to right. */ 206 for (y = nrow - 1; y >= 0; y--) { 207 d.tp_row = p->tp_row + y; 208 for (x = 0; x < ncol; x++) { 209 d.tp_col = p->tp_col + x; 210 buffer[d.tp_col][d.tp_row] = 211 buffer[r->tr_begin.tp_col + x][r->tr_begin.tp_row + y]; 212 printchar(&d); 213 } 214 } 215 } else { 216 /* Copy from right to left. */ 217 for (y = nrow - 1; y >= 0; y--) { 218 d.tp_row = p->tp_row + y; 219 for (x = ncol - 1; x >= 0; x--) { 220 d.tp_col = p->tp_col + x; 221 buffer[d.tp_col][d.tp_row] = 222 buffer[r->tr_begin.tp_col + x][r->tr_begin.tp_row + y]; 223 printchar(&d); 224 } 225 } 226 } 227 } 228} 229 230static void 231test_param(void *s __unused, int cmd, unsigned int value) 232{ 233 234 switch (cmd) { 235 case TP_SHOWCURSOR: 236 curs_set(value); 237 break; 238 case TP_KEYPADAPP: 239 keypad(stdscr, value ? TRUE : FALSE); 240 break; 241 } 242} 243 244static void 245test_respond(void *s __unused, const void *buf, size_t len) 246{ 247 248 write(ptfd, buf, len); 249} 250 251static void 252redraw_border(void) 253{ 254 unsigned int i; 255 256 for (i = 0; i < NROWS; i++) 257 mvaddch(i, NCOLS, '|'); 258 for (i = 0; i < NCOLS; i++) 259 mvaddch(NROWS, i, '-'); 260 261 mvaddch(NROWS, NCOLS, '+'); 262} 263 264static void 265redraw_all(void) 266{ 267 teken_pos_t tp; 268 269 for (tp.tp_row = 0; tp.tp_row < NROWS; tp.tp_row++) 270 for (tp.tp_col = 0; tp.tp_col < NCOLS; tp.tp_col++) 271 printchar(&tp); 272 273 redraw_border(); 274} 275 276int 277main(int argc __unused, char *argv[] __unused) 278{ 279 struct winsize ws; 280 teken_t t; 281 teken_pos_t tp; 282 fd_set rfds; 283 char b[256]; 284 ssize_t bl; 285 const int ccolors[8] = { 286 COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW, 287 COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE 288 }; 289 int i, j; 290 291 setlocale(LC_CTYPE, "UTF-8"); 292 293 tp.tp_row = ws.ws_row = NROWS; 294 tp.tp_col = ws.ws_col = NCOLS; 295 296 switch (forkpty(&ptfd, NULL, NULL, &ws)) { 297 case -1: 298 perror("forkpty"); 299 exit(1); 300 case 0: 301 setenv("TERM", "xterm", 1); 302 setenv("LC_CTYPE", "UTF-8", 0); 303 execlp("zsh", "-zsh", NULL); 304 execlp("bash", "-bash", NULL); 305 execlp("sh", "-sh", NULL); 306 _exit(1); 307 } 308 309 teken_init(&t, &tf, NULL); 310 teken_set_winsize(&t, &tp); 311 312 initscr(); 313 raw(); 314 start_color(); 315 for (i = 0; i < 8; i++) 316 for (j = 0; j < 8; j++) 317 init_pair(i + 8 * j, ccolors[i], ccolors[j]); 318 319 redraw_border(); 320 321 FD_ZERO(&rfds); 322 323 for (;;) { 324 FD_SET(STDIN_FILENO, &rfds); 325 FD_SET(ptfd, &rfds); 326 327 if (select(ptfd + 1, &rfds, NULL, NULL, NULL) < 0) { 328 if (errno == EINTR) { 329 redraw_all(); 330 refresh(); 331 continue; 332 } 333 break; 334 } 335 336 if (FD_ISSET(STDIN_FILENO, &rfds)) { 337 bl = read(STDIN_FILENO, b, sizeof b); 338 if (bl <= 0) 339 break; 340 write(ptfd, b, bl); 341 } 342 343 if (FD_ISSET(ptfd, &rfds)) { 344 bl = read(ptfd, b, sizeof b); 345 if (bl <= 0) 346 break; 347 teken_input(&t, b, bl); 348 refresh(); 349 } 350 } 351 352 endwin(); 353 354 return (0); 355}
|