ite.c revision 1.34 1 /* $NetBSD: ite.c,v 1.34 1995/11/30 00:57:06 jtc Exp $ */
2
3 /*
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1990 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * from: Utah Hdr: ite.c 1.1 90/07/09
41 * @(#)ite.c 7.6 (Berkeley) 5/16/91
42 */
43
44 /*
45 * ite - bitmaped terminal.
46 * Supports VT200, a few terminal features will be unavailable until
47 * the system actually probes the device (i.e. not after consinit())
48 */
49
50 #include <sys/param.h>
51 #include <sys/kernel.h>
52 #include <sys/conf.h>
53 #include <sys/device.h>
54 #include <sys/malloc.h>
55 #include <sys/ioctl.h>
56 #include <sys/tty.h>
57 #include <sys/termios.h>
58 #include <sys/systm.h>
59 #include <sys/proc.h>
60 #include <dev/cons.h>
61 #include <amiga/amiga/cc.h>
62 #include <amiga/amiga/kdassert.h>
63 #include <amiga/amiga/color.h> /* DEBUG */
64 #include <amiga/amiga/device.h>
65 #include <amiga/amiga/isr.h>
66 #include <amiga/dev/iteioctl.h>
67 #include <amiga/dev/itevar.h>
68 #include <amiga/dev/kbdmap.h>
69 #include <amiga/dev/grfioctl.h>
70 #include <amiga/dev/grfvar.h>
71
72 #include "grfcc.h"
73
74 /*
75 * XXX go ask sys/kern/tty.c:ttselect()
76 */
77
78 #define ITEUNIT(dev) (minor(dev))
79
80 #define SUBR_INIT(ip) (ip)->grf->g_iteinit(ip)
81 #define SUBR_DEINIT(ip) (ip)->grf->g_itedeinit(ip)
82 #define SUBR_PUTC(ip,c,dy,dx,m) (ip)->grf->g_iteputc(ip,c,dy,dx,m)
83 #define SUBR_CURSOR(ip,flg) (ip)->grf->g_itecursor(ip,flg)
84 #define SUBR_CLEAR(ip,sy,sx,h,w) (ip)->grf->g_iteclear(ip,sy,sx,h,w)
85 #define SUBR_SCROLL(ip,sy,sx,count,dir) \
86 (ip)->grf->g_itescroll(ip,sy,sx,count,dir)
87
88 u_int ite_confunits; /* configured units */
89
90 int start_repeat_timeo = 30; /* first repeat after x s/100 */
91 int next_repeat_timeo = 10; /* next repeat after x s/100 */
92
93 int ite_default_wrap = 1; /* you want vtxxx-nam, binpatch */
94
95 struct ite_softc con_itesoftc;
96 u_char cons_tabs[MAX_TABS];
97
98 struct ite_softc *kbd_ite;
99 int kbd_init;
100
101 /* audio bell stuff */
102 u_int bvolume = 10;
103 u_int bpitch = 660;
104 u_int bmsec = 75;
105
106 static char *bsamplep;
107 static char sample[20] = {
108 0,39,75,103,121,127,121,103,75,39,0,
109 -39,-75,-103,-121,-127,-121,-103,-75,-39
110 };
111
112 static char *index __P((const char *, char));
113 static void ite_sifilter __P((void *, void *));
114 void iteputchar __P((int c, struct ite_softc *ip));
115 void ite_putstr __P((const char * s, int len, dev_t dev));
116 void iteattach __P((struct device *, struct device *, void *));
117 int itematch __P((struct device *, struct cfdata *, void *));
118 static void iteprecheckwrap __P((struct ite_softc *));
119 static void itecheckwrap __P((struct ite_softc *));
120
121
122 struct cfdriver itecd = {
123 NULL, "ite", (cfmatch_t)itematch, iteattach, DV_DULL,
124 sizeof(struct ite_softc), NULL, 0 };
125
126 int
127 itematch(pdp, cdp, auxp)
128 struct device *pdp;
129 struct cfdata *cdp;
130 void *auxp;
131 {
132 struct grf_softc *gp;
133 int maj;
134
135 gp = auxp;
136 /*
137 * all that our mask allows (more than enough no one
138 * has > 32 monitors for text consoles on one machine)
139 */
140 if (cdp->cf_unit >= sizeof(ite_confunits) * NBBY)
141 return(0);
142 /*
143 * XXX
144 * normally this would be done in attach, however
145 * during early init we do not have a device pointer
146 * and thus no unit number.
147 */
148 for(maj = 0; maj < nchrdev; maj++)
149 if (cdevsw[maj].d_open == iteopen)
150 break;
151 gp->g_itedev = makedev(maj, cdp->cf_unit);
152 return(1);
153 }
154
155 void
156 iteattach(pdp, dp, auxp)
157 struct device *pdp, *dp;
158 void *auxp;
159 {
160 struct grf_softc *gp;
161 struct ite_softc *ip;
162 int s;
163
164 gp = (struct grf_softc *)auxp;
165
166 /*
167 * mark unit as attached (XXX see itematch)
168 */
169 ite_confunits |= 1 << ITEUNIT(gp->g_itedev);
170
171 if (dp) {
172 ip = (struct ite_softc *)dp;
173
174 s = spltty();
175 if (con_itesoftc.grf != NULL &&
176 con_itesoftc.grf->g_unit == gp->g_unit) {
177 /*
178 * console reinit copy params over.
179 * and console always gets keyboard
180 */
181 bcopy(&con_itesoftc.grf, &ip->grf,
182 (char *)&ip[1] - (char *)&ip->grf);
183 con_itesoftc.grf = NULL;
184 kbd_ite = ip;
185 }
186 ip->grf = gp;
187 splx(s);
188
189 alloc_sicallback();
190 iteinit(gp->g_itedev);
191 printf(": rows %d cols %d", ip->rows, ip->cols);
192 printf(" repeat at (%d/100)s next at (%d/100)s",
193 start_repeat_timeo, next_repeat_timeo);
194
195 if (kbd_ite == NULL)
196 kbd_ite = ip;
197 if (kbd_ite == ip)
198 printf(" has keyboard");
199 printf("\n");
200 } else {
201 if (con_itesoftc.grf != NULL &&
202 con_itesoftc.grf->g_conpri > gp->g_conpri)
203 return;
204 con_itesoftc.grf = gp;
205 con_itesoftc.tabs = cons_tabs;
206 }
207 }
208
209 struct ite_softc *
210 getitesp(dev)
211 dev_t dev;
212 {
213 if (amiga_realconfig && con_itesoftc.grf == NULL)
214 return(itecd.cd_devs[ITEUNIT(dev)]);
215
216 if (con_itesoftc.grf == NULL)
217 panic("no ite_softc for console");
218 return(&con_itesoftc);
219 }
220
221 /*
222 * cons.c entry points into ite device.
223 */
224
225 /*
226 * Return a priority in consdev->cn_pri field highest wins. This function
227 * is called before any devices have been probed.
228 */
229 void
230 itecnprobe(cd)
231 struct consdev *cd;
232 {
233 /*
234 * bring graphics layer up.
235 */
236 config_console();
237
238 /*
239 * return priority of the best ite (already picked from attach)
240 * or CN_DEAD.
241 */
242 if (con_itesoftc.grf == NULL)
243 cd->cn_pri = CN_DEAD;
244 else {
245 cd->cn_pri = con_itesoftc.grf->g_conpri;
246 cd->cn_dev = con_itesoftc.grf->g_itedev;
247 }
248 }
249
250 /* audio bell stuff */
251 void
252 init_bell()
253 {
254 if (bsamplep != NULL)
255 return;
256 bsamplep = alloc_chipmem(20);
257 if (bsamplep == NULL)
258 panic("no chipmem for ite_bell");
259
260 bcopy(sample, bsamplep, 20);
261 }
262
263 void
264 ite_bell()
265 {
266 u_int clock;
267 u_int period;
268 u_int count;
269
270 clock = 3579545; /* PAL 3546895 */
271
272 /*
273 * the number of clock ticks per sample byte must be > 124
274 * ergo bpitch must be < clock / 124*20
275 * i.e. ~1443, 1300 to be safe (PAL etc.). also not zero obviously
276 */
277 period = clock / (bpitch * 20);
278 count = bmsec * bpitch / 1000;
279
280 play_sample(10, PREP_DMA_MEM(bsamplep), period, bvolume, 0x3, count);
281 }
282
283 void
284 itecninit(cd)
285 struct consdev *cd;
286 {
287 struct ite_softc *ip;
288
289 ip = getitesp(cd->cn_dev);
290 iteinit(cd->cn_dev);
291 ip->flags |= ITE_ACTIVE | ITE_ISCONS;
292
293 init_bell();
294 }
295
296 /*
297 * ite_cnfinish() is called in ite_init() when the device is
298 * being probed in the normal fasion, thus we can finish setting
299 * up this ite now that the system is more functional.
300 */
301 void
302 ite_cnfinish(ip)
303 struct ite_softc *ip;
304 {
305 static int done;
306
307 if (done)
308 return;
309 done = 1;
310 }
311
312 int
313 itecngetc(dev)
314 dev_t dev;
315 {
316 int c;
317
318 /* XXX this should be moved */
319 if (!kbd_init) {
320 kbd_init = 1;
321 kbdenable();
322 }
323 do {
324 c = kbdgetcn();
325 c = ite_cnfilter(c, ITEFILT_CONSOLE);
326 } while (c == -1);
327 return (c);
328 }
329
330 void
331 itecnputc(dev, c)
332 dev_t dev;
333 int c;
334 {
335 static int paniced;
336 struct ite_softc *ip;
337 char ch;
338
339 ip = getitesp(dev);
340 ch = c;
341
342 if (panicstr && !paniced &&
343 (ip->flags & (ITE_ACTIVE | ITE_INGRF)) != ITE_ACTIVE) {
344 (void)ite_on(dev, 3);
345 paniced = 1;
346 }
347 iteputchar(ch, ip);
348 }
349
350 void
351 itecnpollc(dev, on)
352 dev_t dev;
353 int on;
354 {
355
356 }
357
358 /*
359 * standard entry points to the device.
360 */
361
362 /*
363 * iteinit() is the standard entry point for initialization of
364 * an ite device, it is also called from ite_cninit().
365 *
366 */
367 void
368 iteinit(dev)
369 dev_t dev;
370 {
371 struct ite_softc *ip;
372
373 ip = getitesp(dev);
374 if (ip->flags & ITE_INITED)
375 return;
376 bcopy(&ascii_kbdmap, &kbdmap, sizeof(struct kbdmap));
377
378 ip->cursorx = 0;
379 ip->cursory = 0;
380 SUBR_INIT(ip);
381 SUBR_CURSOR(ip, DRAW_CURSOR);
382 if (ip->tabs == NULL)
383 ip->tabs = malloc(MAX_TABS * sizeof(u_char),M_DEVBUF,M_WAITOK);
384 ite_reset(ip);
385 ip->flags |= ITE_INITED;
386 }
387
388 int
389 iteopen(dev, mode, devtype, p)
390 dev_t dev;
391 int mode, devtype;
392 struct proc *p;
393 {
394 struct ite_softc *ip;
395 struct tty *tp;
396 int error, first, unit;
397
398 unit = ITEUNIT(dev);
399 first = 0;
400
401 if (((1 << unit) & ite_confunits) == 0)
402 return (ENXIO);
403
404 ip = getitesp(dev);
405
406 if (ip->tp == NULL)
407 tp = ip->tp = ttymalloc();
408 else
409 tp = ip->tp;
410 if ((tp->t_state & (TS_ISOPEN | TS_XCLUDE)) == (TS_ISOPEN | TS_XCLUDE)
411 && p->p_ucred->cr_uid != 0)
412 return (EBUSY);
413 if ((ip->flags & ITE_ACTIVE) == 0) {
414 error = ite_on(dev, 0);
415 if (error)
416 return (error);
417 first = 1;
418 }
419 tp->t_oproc = itestart;
420 tp->t_param = ite_param;
421 tp->t_dev = dev;
422 if ((tp->t_state & TS_ISOPEN) == 0) {
423 ttychars(tp);
424 tp->t_iflag = TTYDEF_IFLAG;
425 tp->t_oflag = TTYDEF_OFLAG;
426 tp->t_cflag = TTYDEF_CFLAG;
427 tp->t_lflag = TTYDEF_LFLAG;
428 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
429 tp->t_state = TS_WOPEN | TS_CARR_ON;
430 ttsetwater(tp);
431 }
432 error = (*linesw[tp->t_line].l_open) (dev, tp);
433 if (error == 0) {
434 tp->t_winsize.ws_row = ip->rows;
435 tp->t_winsize.ws_col = ip->cols;
436 if (!kbd_init) {
437 kbd_init = 1;
438 kbdenable();
439 }
440 } else if (first)
441 ite_off(dev, 0);
442 return (error);
443 }
444
445 int
446 iteclose(dev, flag, mode, p)
447 dev_t dev;
448 int flag, mode;
449 struct proc *p;
450 {
451 struct tty *tp;
452
453 tp = getitesp(dev)->tp;
454
455 KDASSERT(tp);
456 (*linesw[tp->t_line].l_close) (tp, flag);
457 ttyclose(tp);
458 ite_off(dev, 0);
459 return (0);
460 }
461
462 int
463 iteread(dev, uio, flag)
464 dev_t dev;
465 struct uio *uio;
466 int flag;
467 {
468 struct tty *tp;
469
470 tp = getitesp(dev)->tp;
471
472 KDASSERT(tp);
473 return ((*linesw[tp->t_line].l_read) (tp, uio, flag));
474 }
475
476 int
477 itewrite(dev, uio, flag)
478 dev_t dev;
479 struct uio *uio;
480 int flag;
481 {
482 struct tty *tp;
483
484 tp = getitesp(dev)->tp;
485
486 KDASSERT(tp);
487 return ((*linesw[tp->t_line].l_write) (tp, uio, flag));
488 }
489
490 struct tty *
491 itetty(dev)
492 dev_t dev;
493 {
494 return (getitesp(dev)->tp);
495 }
496
497 int
498 itestop(tp, flag)
499 struct tty *tp;
500 int flag;
501 {
502 return (0);
503 }
504
505 int
506 iteioctl(dev, cmd, addr, flag, p)
507 dev_t dev;
508 u_long cmd;
509 caddr_t addr;
510 int flag;
511 struct proc *p;
512 {
513 struct iterepeat *irp;
514 struct ite_softc *ip;
515 struct itebell *ib;
516 struct tty *tp;
517 int error;
518
519 ip = getitesp(dev);
520 tp = ip->tp;
521
522 KDASSERT(tp);
523
524 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, addr, flag, p);
525 if (error >= 0)
526 return (error);
527 error = ttioctl(tp, cmd, addr, flag, p);
528 if (error >= 0)
529 return (error);
530
531 switch (cmd) {
532 case ITEIOCGBELL:
533 ib = (struct itebell *)addr;
534 ib->volume = bvolume;
535 ib->pitch = bpitch;
536 ib->msec = bmsec;
537 return (0);
538 case ITEIOCSBELL:
539 ib = (struct itebell *)addr;
540
541 if (ib->pitch > MAXBPITCH || ib->pitch < MINBPITCH ||
542 ib->volume > MAXBVOLUME || ib->msec > MAXBTIME)
543 return (EINVAL);
544 bvolume = ib->volume;
545 bpitch = ib->pitch;
546 bmsec = ib->msec;
547 return (0);
548 case ITEIOCSKMAP:
549 if (addr == 0)
550 return(EFAULT);
551 bcopy(addr, &kbdmap, sizeof(struct kbdmap));
552 return(0);
553 case ITEIOCGKMAP:
554 if (addr == NULL)
555 return(EFAULT);
556 bcopy(&kbdmap, addr, sizeof(struct kbdmap));
557 return(0);
558 case ITEIOCGREPT:
559 irp = (struct iterepeat *)addr;
560 irp->start = start_repeat_timeo;
561 irp->next = next_repeat_timeo;
562 return (0);
563 case ITEIOCSREPT:
564 irp = (struct iterepeat *)addr;
565 if (irp->start < ITEMINREPEAT && irp->next < ITEMINREPEAT)
566 return(EINVAL);
567 start_repeat_timeo = irp->start;
568 next_repeat_timeo = irp->next;
569 return(0);
570 }
571 #if NGRFCC > 0
572 /* XXX */
573 if (minor(dev) == 0) {
574 error = ite_grf_ioctl(ip, cmd, addr, flag, p);
575 if (error >= 0)
576 return (error);
577 }
578 #endif
579 return (ENOTTY);
580 }
581
582 void
583 itestart(tp)
584 struct tty *tp;
585 {
586 struct clist *rbp;
587 struct ite_softc *ip;
588 u_char buf[ITEBURST];
589 int s, len;
590
591 ip = getitesp(tp->t_dev);
592
593 KDASSERT(tp);
594
595 s = spltty(); {
596 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
597 goto out;
598
599 tp->t_state |= TS_BUSY;
600 rbp = &tp->t_outq;
601
602 len = q_to_b(rbp, buf, ITEBURST);
603 } splx(s);
604
605 /* Here is a really good place to implement pre/jumpscroll() */
606 ite_putstr(buf, len, tp->t_dev);
607
608 s = spltty(); {
609 tp->t_state &= ~TS_BUSY;
610 /* we have characters remaining. */
611 if (rbp->c_cc) {
612 tp->t_state |= TS_TIMEOUT;
613 timeout(ttrstrt, tp, 1);
614 }
615 /* wakeup we are below */
616 if (rbp->c_cc <= tp->t_lowat) {
617 if (tp->t_state & TS_ASLEEP) {
618 tp->t_state &= ~TS_ASLEEP;
619 wakeup((caddr_t) rbp);
620 }
621 selwakeup(&tp->t_wsel);
622 }
623 out:
624 } splx(s);
625 }
626
627 int
628 ite_on(dev, flag)
629 dev_t dev;
630 int flag;
631 {
632 struct ite_softc *ip;
633 int unit;
634
635 unit = ITEUNIT(dev);
636 if (((1 << unit) & ite_confunits) == 0)
637 return (ENXIO);
638
639 ip = getitesp(dev);
640
641 /* force ite active, overriding graphics mode */
642 if (flag & 1) {
643 ip->flags |= ITE_ACTIVE;
644 ip->flags &= ~(ITE_INGRF | ITE_INITED);
645 }
646 /* leave graphics mode */
647 if (flag & 2) {
648 ip->flags &= ~ITE_INGRF;
649 if ((ip->flags & ITE_ACTIVE) == 0)
650 return (0);
651 }
652 ip->flags |= ITE_ACTIVE;
653 if (ip->flags & ITE_INGRF)
654 return (0);
655 iteinit(dev);
656 return (0);
657 }
658
659 void
660 ite_off(dev, flag)
661 dev_t dev;
662 int flag;
663 {
664 struct ite_softc *ip;
665
666 ip = getitesp(dev);
667 if (flag & 2)
668 ip->flags |= ITE_INGRF;
669 if ((ip->flags & ITE_ACTIVE) == 0)
670 return;
671 if ((flag & 1) ||
672 (ip->flags & (ITE_INGRF | ITE_ISCONS | ITE_INITED)) == ITE_INITED)
673 SUBR_DEINIT(ip);
674 /* XXX hmm grfon() I think wants this to go inactive. */
675 if ((flag & 2) == 0)
676 ip->flags &= ~ITE_ACTIVE;
677 }
678
679 /* XXX called after changes made in underlying grf layer. */
680 /* I want to nuke this */
681 void
682 ite_reinit(dev)
683 dev_t dev;
684 {
685 struct ite_softc *ip;
686
687 ip = getitesp(dev);
688 ip->flags &= ~ITE_INITED;
689 iteinit(dev);
690 }
691
692 int
693 ite_param(tp, t)
694 struct tty *tp;
695 struct termios *t;
696 {
697 tp->t_ispeed = t->c_ispeed;
698 tp->t_ospeed = t->c_ospeed;
699 tp->t_cflag = t->c_cflag;
700 return (0);
701 }
702
703 void
704 ite_reset(ip)
705 struct ite_softc *ip;
706 {
707 int i;
708
709 ip->curx = 0;
710 ip->cury = 0;
711 ip->attribute = ATTR_NOR;
712 ip->save_curx = 0;
713 ip->save_cury = 0;
714 ip->save_attribute = ATTR_NOR;
715 ip->ap = ip->argbuf;
716 ip->emul_level = 0;
717 ip->eightbit_C1 = 0;
718 ip->top_margin = 0;
719 ip->bottom_margin = ip->rows - 1;
720 ip->inside_margins = 0;
721 ip->linefeed_newline = 0;
722 ip->auto_wrap = ite_default_wrap;
723 ip->cursor_appmode = 0;
724 ip->keypad_appmode = 0;
725 ip->imode = 0;
726 ip->key_repeat = 1;
727 bzero(ip->tabs, ip->cols);
728 for (i = 0; i < ip->cols; i++)
729 ip->tabs[i] = ((i & 7) == 0);
730 }
731
732 /*
733 * has to be global becuase of the shared filters.
734 */
735 static u_char key_mod;
736 static u_char last_dead;
737
738 /* Used in console at startup only */
739 int
740 ite_cnfilter(c, caller)
741 u_char c;
742 enum caller caller;
743 {
744 struct key key;
745 u_char code, up, mask;
746 int s, i;
747
748 up = c & 0x80 ? 1 : 0;
749 c &= 0x7f;
750 code = 0;
751
752 s = spltty();
753
754 i = (int)c - KBD_LEFT_SHIFT;
755 if (i >= 0 && i <= (KBD_RIGHT_META - KBD_LEFT_SHIFT)) {
756 mask = 1 << i;
757 if (up)
758 key_mod &= ~mask;
759 else
760 key_mod |= mask;
761 splx(s);
762 return -1;
763 }
764
765 if (up) {
766 splx(s);
767 return -1;
768 }
769
770 /* translate modifiers */
771 if (key_mod & KBD_MOD_SHIFT) {
772 if (key_mod & KBD_MOD_ALT)
773 key = kbdmap.alt_shift_keys[c];
774 else
775 key = kbdmap.shift_keys[c];
776 } else if (key_mod & KBD_MOD_ALT)
777 key = kbdmap.alt_keys[c];
778 else {
779 key = kbdmap.keys[c];
780 /* if CAPS and key is CAPable (no pun intended) */
781 if ((key_mod & KBD_MOD_CAPS) && (key.mode & KBD_MODE_CAPS))
782 key = kbdmap.shift_keys[c];
783 }
784 code = key.code;
785
786 /* if string return */
787 if (key.mode & (KBD_MODE_STRING | KBD_MODE_KPAD)) {
788 splx(s);
789 return -1;
790 }
791 /* handle dead keys */
792 if (key.mode & KBD_MODE_DEAD) {
793 /* if entered twice, send accent itself */
794 if (last_dead == key.mode & KBD_MODE_ACCMASK)
795 last_dead = 0;
796 else {
797 last_dead = key.mode & KBD_MODE_ACCMASK;
798 splx(s);
799 return -1;
800 }
801 }
802 if (last_dead) {
803 /* can't apply dead flag to string-keys */
804 if (code >= '@' && code < 0x7f)
805 code =
806 acctable[KBD_MODE_ACCENT(last_dead)][code - '@'];
807 last_dead = 0;
808 }
809 if (key_mod & KBD_MOD_CTRL)
810 code &= 0x1f;
811 if (key_mod & KBD_MOD_META)
812 code |= 0x80;
813
814 /* do console mapping. */
815 code = code == '\r' ? '\n' : code;
816
817 splx(s);
818 return (code);
819 }
820
821 /* And now the old stuff. */
822
823 /* these are used to implement repeating keys.. */
824 static u_char last_char;
825 static u_char tout_pending;
826
827
828 static void
829 ite_sifilter(void *arg1, void *arg2)
830 {
831 ite_filter((u_char)(size_t)arg1, (enum caller)(size_t)arg2);
832 }
833
834
835 /*ARGSUSED*/
836 static void
837 repeat_handler(arg)
838 void *arg;
839 {
840 tout_pending = 0;
841 if (last_char)
842 add_sicallback(ite_sifilter, (void *)(size_t)last_char,
843 (void *)(size_t)ITEFILT_REPEATER);
844 }
845
846 void
847 ite_filter(c, caller)
848 u_char c;
849 enum caller caller;
850 {
851 struct tty *kbd_tty;
852 u_char code, *str, up, mask;
853 struct key key;
854 int s, i;
855
856 if (kbd_ite == NULL || kbd_ite->tp == NULL)
857 return;
858
859 kbd_tty = kbd_ite->tp;
860
861 /* have to make sure we're at spltty in here */
862 s = spltty();
863
864 /*
865 * keyboard interrupts come at priority 2, while softint
866 * generated keyboard-repeat interrupts come at level 1. So,
867 * to not allow a key-up event to get thru before a repeat for
868 * the key-down, we remove any outstanding callout requests..
869 */
870 rem_sicallback(ite_sifilter);
871
872 up = c & 0x80 ? 1 : 0;
873 c &= 0x7f;
874 code = 0;
875
876 i = (int)c - KBD_LEFT_SHIFT;
877 if (i >= 0 && i <= (KBD_RIGHT_META - KBD_LEFT_SHIFT)) {
878 mask = 1 << i;
879 if (up)
880 key_mod &= ~mask;
881 else
882 key_mod |= mask;
883 splx(s);
884 return;
885 }
886 /* stop repeating on up event */
887 if (up) {
888 if (tout_pending) {
889 untimeout(repeat_handler, 0);
890 tout_pending = 0;
891 last_char = 0;
892 }
893 splx(s);
894 return;
895 } else if (tout_pending && last_char != c) {
896 /* different character, stop also */
897 untimeout(repeat_handler, 0);
898 tout_pending = 0;
899 last_char = 0;
900 }
901 /* Safety button, switch back to ascii keymap. */
902 if (key_mod == (KBD_MOD_LALT | KBD_MOD_LMETA) && c == 0x50) {
903 bcopy(&ascii_kbdmap, &kbdmap, sizeof(struct kbdmap));
904
905 splx(s);
906 return;
907 #ifdef DDB
908 } else if (key_mod == (KBD_MOD_LALT | KBD_MOD_LMETA) && c == 0x59) {
909 extern int Debugger();
910 Debugger();
911 splx(s);
912 return;
913 #endif
914 }
915 /* translate modifiers */
916 if (key_mod & KBD_MOD_SHIFT) {
917 if (key_mod & KBD_MOD_ALT)
918 key = kbdmap.alt_shift_keys[c];
919 else
920 key = kbdmap.shift_keys[c];
921 } else if (key_mod & KBD_MOD_ALT)
922 key = kbdmap.alt_keys[c];
923 else {
924 key = kbdmap.keys[c];
925 /* if CAPS and key is CAPable (no pun intended) */
926 if ((key_mod & KBD_MOD_CAPS) && (key.mode & KBD_MODE_CAPS))
927 key = kbdmap.shift_keys[c];
928 }
929 code = key.code;
930
931 /*
932 * arrange to repeat the keystroke. By doing this at the level
933 * of scan-codes, we can have function keys, and keys that
934 * send strings, repeat too. This also entitles an additional
935 * overhead, since we have to do the conversion each time, but
936 * I guess that's ok.
937 */
938 if (!tout_pending && caller == ITEFILT_TTY && kbd_ite->key_repeat) {
939 tout_pending = 1;
940 last_char = c;
941 timeout(repeat_handler, 0, start_repeat_timeo * hz / 100);
942 } else if (!tout_pending && caller == ITEFILT_REPEATER &&
943 kbd_ite->key_repeat) {
944 tout_pending = 1;
945 last_char = c;
946 timeout(repeat_handler, 0, next_repeat_timeo * hz / 100);
947 }
948 /* handle dead keys */
949 if (key.mode & KBD_MODE_DEAD) {
950 /* if entered twice, send accent itself */
951 if (last_dead == key.mode & KBD_MODE_ACCMASK)
952 last_dead = 0;
953 else {
954 last_dead = key.mode & KBD_MODE_ACCMASK;
955 splx(s);
956 return;
957 }
958 }
959 if (last_dead) {
960 /* can't apply dead flag to string-keys */
961 if (!(key.mode & KBD_MODE_STRING) && code >= '@' &&
962 code < 0x7f)
963 code = acctable[KBD_MODE_ACCENT(last_dead)][code - '@'];
964 last_dead = 0;
965 }
966 /* if not string, apply META and CTRL modifiers */
967 if (!(key.mode & KBD_MODE_STRING)
968 && (!(key.mode & KBD_MODE_KPAD) ||
969 (kbd_ite && !kbd_ite->keypad_appmode))) {
970 if (key_mod & KBD_MOD_CTRL)
971 code &= 0x1f;
972 if (key_mod & KBD_MOD_META)
973 code |= 0x80;
974 } else if ((key.mode & KBD_MODE_KPAD) &&
975 (kbd_ite && kbd_ite->keypad_appmode)) {
976 static char in[] = {
977 0x0f /* 0 */, 0x1d /* 1 */, 0x1e /* 2 */, 0x1f /* 3 */,
978 0x2d /* 4 */, 0x2e /* 5 */, 0x2f /* 6 */, 0x3d /* 7 */,
979 0x3e /* 8 */, 0x3f /* 9 */, 0x4a /* - */, 0x5e /* + */,
980 0x3c /* . */, 0x43 /* e */, 0x5a /* ( */, 0x5b /* ) */,
981 0x5c /* / */, 0x5d /* * */
982 };
983 static char *out = "pqrstuvwxymlnMPQRS";
984 char *cp = index (in, c);
985
986 /*
987 * keypad-appmode sends SS3 followed by the above
988 * translated character
989 */
990 (*linesw[kbd_tty->t_line].l_rint) (27, kbd_tty);
991 (*linesw[kbd_tty->t_line].l_rint) ('O', kbd_tty);
992 (*linesw[kbd_tty->t_line].l_rint) (out[cp - in], kbd_tty);
993 splx(s);
994 return;
995 } else {
996 /* *NO* I don't like this.... */
997 static u_char app_cursor[] =
998 {
999 3, 27, 'O', 'A',
1000 3, 27, 'O', 'B',
1001 3, 27, 'O', 'C',
1002 3, 27, 'O', 'D'};
1003
1004 str = kbdmap.strings + code;
1005 /*
1006 * if this is a cursor key, AND it has the default
1007 * keymap setting, AND we're in app-cursor mode, switch
1008 * to the above table. This is *nasty* !
1009 */
1010 if (c >= 0x4c && c <= 0x4f && kbd_ite->cursor_appmode
1011 && !bcmp(str, "\x03\x1b[", 3) &&
1012 index("ABCD", str[3]))
1013 str = app_cursor + 4 * (str[3] - 'A');
1014
1015 /*
1016 * using a length-byte instead of 0-termination allows
1017 * to embed \0 into strings, although this is not used
1018 * in the default keymap
1019 */
1020 for (i = *str++; i; i--)
1021 (*linesw[kbd_tty->t_line].l_rint) (*str++, kbd_tty);
1022 splx(s);
1023 return;
1024 }
1025 (*linesw[kbd_tty->t_line].l_rint) (code, kbd_tty);
1026
1027 splx(s);
1028 return;
1029 }
1030
1031 /* helper functions, makes the code below more readable */
1032 inline static void
1033 ite_sendstr(str)
1034 char *str;
1035 {
1036 struct tty *kbd_tty;
1037
1038 kbd_tty = kbd_ite->tp;
1039 KDASSERT(kbd_tty);
1040 while (*str)
1041 (*linesw[kbd_tty->t_line].l_rint) (*str++, kbd_tty);
1042 }
1043
1044 static void
1045 alignment_display(ip)
1046 struct ite_softc *ip;
1047 {
1048 int i, j;
1049
1050 for (j = 0; j < ip->rows; j++)
1051 for (i = 0; i < ip->cols; i++)
1052 SUBR_PUTC(ip, 'E', j, i, ATTR_NOR);
1053 attrclr(ip, 0, 0, ip->rows, ip->cols);
1054 SUBR_CURSOR(ip, DRAW_CURSOR);
1055 }
1056
1057 inline static void
1058 snap_cury(ip)
1059 struct ite_softc *ip;
1060 {
1061 if (ip->inside_margins)
1062 {
1063 if (ip->cury < ip->top_margin)
1064 ip->cury = ip->top_margin;
1065 if (ip->cury > ip->bottom_margin)
1066 ip->cury = ip->bottom_margin;
1067 }
1068 }
1069
1070 inline static void
1071 ite_dnchar(ip, n)
1072 struct ite_softc *ip;
1073 int n;
1074 {
1075 n = min(n, ip->cols - ip->curx);
1076 if (n < ip->cols - ip->curx)
1077 {
1078 SUBR_SCROLL(ip, ip->cury, ip->curx + n, n, SCROLL_LEFT);
1079 attrmov(ip, ip->cury, ip->curx + n, ip->cury, ip->curx,
1080 1, ip->cols - ip->curx - n);
1081 attrclr(ip, ip->cury, ip->cols - n, 1, n);
1082 }
1083 while (n-- > 0)
1084 SUBR_PUTC(ip, ' ', ip->cury, ip->cols - n - 1, ATTR_NOR);
1085 SUBR_CURSOR(ip, DRAW_CURSOR);
1086 }
1087
1088 inline static void
1089 ite_inchar(ip, n)
1090 struct ite_softc *ip;
1091 int n;
1092 {
1093 n = min(n, ip->cols - ip->curx);
1094 if (n < ip->cols - ip->curx)
1095 {
1096 SUBR_SCROLL(ip, ip->cury, ip->curx, n, SCROLL_RIGHT);
1097 attrmov(ip, ip->cury, ip->curx, ip->cury, ip->curx + n,
1098 1, ip->cols - ip->curx - n);
1099 attrclr(ip, ip->cury, ip->curx, 1, n);
1100 }
1101 while (n--)
1102 SUBR_PUTC(ip, ' ', ip->cury, ip->curx + n, ATTR_NOR);
1103 SUBR_CURSOR(ip, DRAW_CURSOR);
1104 }
1105
1106 inline static void
1107 ite_clrtoeol(ip)
1108 struct ite_softc *ip;
1109 {
1110 int y = ip->cury, x = ip->curx;
1111 if (ip->cols - x > 0)
1112 {
1113 SUBR_CLEAR(ip, y, x, 1, ip->cols - x);
1114 attrclr(ip, y, x, 1, ip->cols - x);
1115 SUBR_CURSOR(ip, DRAW_CURSOR);
1116 }
1117 }
1118
1119 inline static void
1120 ite_clrtobol(ip)
1121 struct ite_softc *ip;
1122 {
1123 int y = ip->cury, x = min(ip->curx + 1, ip->cols);
1124 SUBR_CLEAR(ip, y, 0, 1, x);
1125 attrclr(ip, y, 0, 1, x);
1126 SUBR_CURSOR(ip, DRAW_CURSOR);
1127 }
1128
1129 inline static void
1130 ite_clrline(ip)
1131 struct ite_softc *ip;
1132 {
1133 int y = ip->cury;
1134 SUBR_CLEAR(ip, y, 0, 1, ip->cols);
1135 attrclr(ip, y, 0, 1, ip->cols);
1136 SUBR_CURSOR(ip, DRAW_CURSOR);
1137 }
1138
1139
1140
1141 inline static void
1142 ite_clrtoeos(ip)
1143 struct ite_softc *ip;
1144 {
1145 ite_clrtoeol(ip);
1146 if (ip->cury < ip->rows - 1)
1147 {
1148 SUBR_CLEAR(ip, ip->cury + 1, 0, ip->rows - 1 - ip->cury, ip->cols);
1149 attrclr(ip, ip->cury, 0, ip->rows - ip->cury, ip->cols);
1150 SUBR_CURSOR(ip, DRAW_CURSOR);
1151 }
1152 }
1153
1154 inline static void
1155 ite_clrtobos(ip)
1156 struct ite_softc *ip;
1157 {
1158 ite_clrtobol(ip);
1159 if (ip->cury > 0)
1160 {
1161 SUBR_CLEAR(ip, 0, 0, ip->cury, ip->cols);
1162 attrclr(ip, 0, 0, ip->cury, ip->cols);
1163 SUBR_CURSOR(ip, DRAW_CURSOR);
1164 }
1165 }
1166
1167 inline static void
1168 ite_clrscreen(ip)
1169 struct ite_softc *ip;
1170 {
1171 SUBR_CLEAR(ip, 0, 0, ip->rows, ip->cols);
1172 attrclr(ip, 0, 0, ip->rows, ip->cols);
1173 SUBR_CURSOR(ip, DRAW_CURSOR);
1174 }
1175
1176
1177
1178 inline static void
1179 ite_dnline(ip, n)
1180 struct ite_softc *ip;
1181 int n;
1182 {
1183 /* interesting.. if the cursor is outside the scrolling
1184 region, this command is simply ignored.. */
1185 if (ip->cury < ip->top_margin || ip->cury > ip->bottom_margin)
1186 return;
1187
1188 n = min(n, ip->bottom_margin + 1 - ip->cury);
1189 if (n <= ip->bottom_margin - ip->cury)
1190 {
1191 SUBR_SCROLL(ip, ip->cury + n, 0, n, SCROLL_UP);
1192 attrmov(ip, ip->cury + n, 0, ip->cury, 0,
1193 ip->bottom_margin + 1 - ip->cury - n, ip->cols);
1194 }
1195 SUBR_CLEAR(ip, ip->bottom_margin - n + 1, 0, n, ip->cols);
1196 attrclr(ip, ip->bottom_margin - n + 1, 0, n, ip->cols);
1197 SUBR_CURSOR(ip, DRAW_CURSOR);
1198 }
1199
1200 inline static void
1201 ite_inline(ip, n)
1202 struct ite_softc *ip;
1203 int n;
1204 {
1205 /* interesting.. if the cursor is outside the scrolling
1206 region, this command is simply ignored.. */
1207 if (ip->cury < ip->top_margin || ip->cury > ip->bottom_margin)
1208 return;
1209
1210 n = min(n, ip->bottom_margin + 1 - ip->cury);
1211 if (n <= ip->bottom_margin - ip->cury)
1212 {
1213 SUBR_SCROLL(ip, ip->cury, 0, n, SCROLL_DOWN);
1214 attrmov(ip, ip->cury, 0, ip->cury + n, 0,
1215 ip->bottom_margin + 1 - ip->cury - n, ip->cols);
1216 }
1217 SUBR_CLEAR(ip, ip->cury, 0, n, ip->cols);
1218 attrclr(ip, ip->cury, 0, n, ip->cols);
1219 SUBR_CURSOR(ip, DRAW_CURSOR);
1220 }
1221
1222 inline static void
1223 ite_lf (ip)
1224 struct ite_softc *ip;
1225 {
1226 ++ip->cury;
1227 if ((ip->cury == ip->bottom_margin+1) || (ip->cury == ip->rows))
1228 {
1229 ip->cury--;
1230 SUBR_SCROLL(ip, ip->top_margin + 1, 0, 1, SCROLL_UP);
1231 ite_clrline(ip);
1232 }
1233 SUBR_CURSOR(ip, MOVE_CURSOR);
1234 clr_attr(ip, ATTR_INV);
1235 }
1236
1237 inline static void
1238 ite_crlf (ip)
1239 struct ite_softc *ip;
1240 {
1241 ip->curx = 0;
1242 ite_lf (ip);
1243 }
1244
1245 inline static void
1246 ite_cr (ip)
1247 struct ite_softc *ip;
1248 {
1249 if (ip->curx)
1250 {
1251 ip->curx = 0;
1252 SUBR_CURSOR(ip, MOVE_CURSOR);
1253 }
1254 }
1255
1256 inline static void
1257 ite_rlf (ip)
1258 struct ite_softc *ip;
1259 {
1260 ip->cury--;
1261 if ((ip->cury < 0) || (ip->cury == ip->top_margin - 1))
1262 {
1263 ip->cury++;
1264 SUBR_SCROLL(ip, ip->top_margin, 0, 1, SCROLL_DOWN);
1265 ite_clrline(ip);
1266 }
1267 SUBR_CURSOR(ip, MOVE_CURSOR);
1268 clr_attr(ip, ATTR_INV);
1269 }
1270
1271 inline static int
1272 atoi (cp)
1273 const char *cp;
1274 {
1275 int n;
1276
1277 for (n = 0; *cp && *cp >= '0' && *cp <= '9'; cp++)
1278 n = n * 10 + *cp - '0';
1279
1280 return n;
1281 }
1282
1283 static char *
1284 index (cp, ch)
1285 const char *cp;
1286 char ch;
1287 {
1288 while (*cp && *cp != ch) cp++;
1289 return *cp ? (char *) cp : 0;
1290 }
1291
1292
1293
1294 inline static int
1295 ite_argnum (ip)
1296 struct ite_softc *ip;
1297 {
1298 char ch;
1299 int n;
1300
1301 /* convert argument string into number */
1302 if (ip->ap == ip->argbuf)
1303 return 1;
1304 ch = *ip->ap;
1305 *ip->ap = 0;
1306 n = atoi (ip->argbuf);
1307 *ip->ap = ch;
1308
1309 return n;
1310 }
1311
1312 inline static int
1313 ite_zargnum (ip)
1314 struct ite_softc *ip;
1315 {
1316 char ch;
1317 int n;
1318
1319 /* convert argument string into number */
1320 if (ip->ap == ip->argbuf)
1321 return 0;
1322 ch = *ip->ap;
1323 *ip->ap = 0;
1324 n = atoi (ip->argbuf);
1325 *ip->ap = ch;
1326
1327 return n; /* don't "n ? n : 1" here, <CSI>0m != <CSI>1m ! */
1328 }
1329
1330 inline static int
1331 strncmp (a, b, l)
1332 const char *a, *b;
1333 int l;
1334 {
1335 for (;l--; a++, b++)
1336 if (*a != *b)
1337 return *a - *b;
1338 return 0;
1339 }
1340
1341 void
1342 ite_putstr(s, len, dev)
1343 const char *s;
1344 int len;
1345 dev_t dev;
1346 {
1347 struct ite_softc *ip;
1348 int i;
1349
1350 ip = getitesp(dev);
1351
1352 /* XXX avoid problems */
1353 if ((ip->flags & (ITE_ACTIVE|ITE_INGRF)) != ITE_ACTIVE)
1354 return;
1355
1356 SUBR_CURSOR(ip, START_CURSOROPT);
1357 for (i = 0; i < len; i++)
1358 if (s[i])
1359 iteputchar(s[i], ip);
1360 SUBR_CURSOR(ip, END_CURSOROPT);
1361 }
1362
1363 static void
1364 iteprecheckwrap(ip)
1365 struct ite_softc *ip;
1366 {
1367 if (ip->auto_wrap && ip->curx == ip->cols) {
1368 ip->curx = 0;
1369 clr_attr(ip, ATTR_INV);
1370 if (++ip->cury >= ip->bottom_margin + 1) {
1371 ip->cury = ip->bottom_margin;
1372 SUBR_CURSOR(ip, MOVE_CURSOR);
1373 SUBR_SCROLL(ip, ip->top_margin + 1, 0, 1, SCROLL_UP);
1374 ite_clrtoeol(ip);
1375 } else
1376 SUBR_CURSOR(ip, MOVE_CURSOR);
1377 }
1378 }
1379
1380 static void
1381 itecheckwrap(ip)
1382 struct ite_softc *ip;
1383 {
1384 #if 0
1385 if (++ip->curx == ip->cols) {
1386 if (ip->auto_wrap) {
1387 ip->curx = 0;
1388 clr_attr(ip, ATTR_INV);
1389 if (++ip->cury >= ip->bottom_margin + 1) {
1390 ip->cury = ip->bottom_margin;
1391 SUBR_CURSOR(ip, MOVE_CURSOR);
1392 SUBR_SCROLL(ip, ip->top_margin + 1, 0, 1, SCROLL_UP);
1393 ite_clrtoeol(ip);
1394 return;
1395 }
1396 } else
1397 /* stay there if no autowrap.. */
1398 ip->curx--;
1399 }
1400 #else
1401 if (ip->curx < ip->cols) {
1402 ip->curx++;
1403 SUBR_CURSOR(ip, MOVE_CURSOR);
1404 }
1405 #endif
1406 }
1407
1408 void
1409 iteputchar(c, ip)
1410 register int c;
1411 struct ite_softc *ip;
1412 {
1413 struct tty *kbd_tty;
1414 int n, x, y;
1415 char *cp;
1416
1417 if (kbd_ite == NULL)
1418 kbd_tty = NULL;
1419 else
1420 kbd_tty = kbd_ite->tp;
1421
1422 if (ip->escape) {
1423 switch (ip->escape) {
1424 case ESC:
1425 switch (c) {
1426 /*
1427 * first 7bit equivalents for the
1428 * 8bit control characters
1429 */
1430 case 'D':
1431 c = IND;
1432 ip->escape = 0;
1433 break;
1434 /*
1435 * and fall into the next
1436 * switch below (same for all `break')
1437 */
1438 case 'E':
1439 c = NEL;
1440 ip->escape = 0;
1441 break;
1442 case 'H':
1443 c = HTS;
1444 ip->escape = 0;
1445 break;
1446 case 'M':
1447 c = RI;
1448 ip->escape = 0;
1449 break;
1450 case 'N':
1451 c = SS2;
1452 ip->escape = 0;
1453 break;
1454 case 'O':
1455 c = SS3;
1456 ip->escape = 0;
1457 break;
1458 case 'P':
1459 c = DCS;
1460 ip->escape = 0;
1461 break;
1462 case '[':
1463 c = CSI;
1464 ip->escape = 0;
1465 break;
1466 case '\\':
1467 c = ST;
1468 ip->escape = 0;
1469 break;
1470 case ']':
1471 c = OSC;
1472 ip->escape = 0;
1473 break;
1474 case '^':
1475 c = PM;
1476 ip->escape = 0;
1477 break;
1478 case '_':
1479 c = APC;
1480 ip->escape = 0;
1481 break;
1482 /* introduces 7/8bit control */
1483 case ' ':
1484 /* can be followed by either F or G */
1485 ip->escape = ' ';
1486 break;
1487 /*
1488 * a lot of character set selections, not yet
1489 * used... 94-character sets:
1490 */
1491 case '(': /* G0 */
1492 case ')': /* G1 */
1493 ip->escape = c;
1494 return;
1495 case '*': /* G2 */
1496 case '+': /* G3 */
1497 case 'B': /* ASCII */
1498 case 'A': /* ISO latin 1 */
1499 case '<': /* user preferred suplemental */
1500 case '0': /* dec special graphics */
1501 /*
1502 * 96-character sets:
1503 */
1504 case '-': /* G1 */
1505 case '.': /* G2 */
1506 case '/': /* G3 */
1507 /*
1508 * national character sets:
1509 */
1510 case '4': /* dutch */
1511 case '5':
1512 case 'C': /* finnish */
1513 case 'R': /* french */
1514 case 'Q': /* french canadian */
1515 case 'K': /* german */
1516 case 'Y': /* italian */
1517 case '6': /* norwegian/danish */
1518 /*
1519 * note: %5 and %6 are not supported (two
1520 * chars..)
1521 */
1522 ip->escape = 0;
1523 /* just ignore for now */
1524 return;
1525 /*
1526 * locking shift modes (as you might guess, not
1527 * yet supported..)
1528 */
1529 case '`':
1530 ip->GR = ip->G1;
1531 ip->escape = 0;
1532 return;
1533 case 'n':
1534 ip->GL = ip->G2;
1535 ip->escape = 0;
1536 return;
1537 case '}':
1538 ip->GR = ip->G2;
1539 ip->escape = 0;
1540 return;
1541 case 'o':
1542 ip->GL = ip->G3;
1543 ip->escape = 0;
1544 return;
1545 case '|':
1546 ip->GR = ip->G3;
1547 ip->escape = 0;
1548 return;
1549 case '#':
1550 /* font width/height control */
1551 ip->escape = '#';
1552 return;
1553 case 'c':
1554 /* hard terminal reset .. */
1555 ite_reset(ip);
1556 SUBR_CURSOR(ip, MOVE_CURSOR);
1557 ip->escape = 0;
1558 return;
1559 case '7':
1560 ip->save_curx = ip->curx;
1561 ip->save_cury = ip->cury;
1562 ip->save_attribute = ip->attribute;
1563 ip->escape = 0;
1564 return;
1565 case '8':
1566 ip->curx = ip->save_curx;
1567 ip->cury = ip->save_cury;
1568 ip->attribute = ip->save_attribute;
1569 SUBR_CURSOR(ip, MOVE_CURSOR);
1570 ip->escape = 0;
1571 return;
1572 case '=':
1573 ip->keypad_appmode = 1;
1574 ip->escape = 0;
1575 return;
1576 case '>':
1577 ip->keypad_appmode = 0;
1578 ip->escape = 0;
1579 return;
1580 case 'Z': /* request ID */
1581 /* XXX not clean */
1582 if (ip->emul_level == EMUL_VT100)
1583 ite_sendstr("\033[?61;0c");
1584 else
1585 ite_sendstr("\033[?63;0c");
1586 ip->escape = 0;
1587 return;
1588 default:
1589 /*
1590 * default catch all for not recognized ESC
1591 * sequences
1592 */
1593 ip->escape = 0;
1594 return;
1595 }
1596 break;
1597 case '(':
1598 case ')':
1599 ip->escape = 0;
1600 return;
1601 case ' ':
1602 switch (c) {
1603 case 'F':
1604 ip->eightbit_C1 = 0;
1605 ip->escape = 0;
1606 return;
1607 case 'G':
1608 ip->eightbit_C1 = 1;
1609 ip->escape = 0;
1610 return;
1611 default:
1612 /* not supported */
1613 ip->escape = 0;
1614 return;
1615 }
1616 break;
1617 case '#':
1618 switch (c) {
1619 case '5':
1620 /* single height, single width */
1621 ip->escape = 0;
1622 return;
1623 case '6':
1624 /* double width, single height */
1625 ip->escape = 0;
1626 return;
1627 case '3':
1628 /* top half */
1629 ip->escape = 0;
1630 return;
1631 case '4':
1632 /* bottom half */
1633 ip->escape = 0;
1634 return;
1635 case '8':
1636 /* screen alignment pattern... */
1637 alignment_display(ip);
1638 ip->escape = 0;
1639 return;
1640 default:
1641 ip->escape = 0;
1642 return;
1643 }
1644 break;
1645 case CSI:
1646 /* the biggie... */
1647 switch (c) {
1648 case '0':
1649 case '1':
1650 case '2':
1651 case '3':
1652 case '4':
1653 case '5':
1654 case '6':
1655 case '7':
1656 case '8':
1657 case '9':
1658 case ';':
1659 case '\"':
1660 case '$':
1661 case '>':
1662 if (ip->ap < ip->argbuf + MAX_ARGSIZE)
1663 *ip->ap++ = c;
1664 return;
1665 case BS:
1666 /*
1667 * you wouldn't believe such perversion is
1668 * possible? it is.. BS is allowed in between
1669 * cursor sequences (at least), according to
1670 * vttest..
1671 */
1672 if (--ip->curx < 0)
1673 ip->curx = 0;
1674 else
1675 SUBR_CURSOR(ip, MOVE_CURSOR);
1676 break;
1677 case 'p':
1678 *ip->ap = 0;
1679 if (!strncmp(ip->argbuf, "61\"", 3))
1680 ip->emul_level = EMUL_VT100;
1681 else if (!strncmp(ip->argbuf, "63;1\"", 5)
1682 || !strncmp(ip->argbuf, "62;1\"", 5))
1683 ip->emul_level = EMUL_VT300_7;
1684 else
1685 ip->emul_level = EMUL_VT300_8;
1686 ip->escape = 0;
1687 return;
1688 case '?':
1689 *ip->ap = 0;
1690 ip->escape = '?';
1691 ip->ap = ip->argbuf;
1692 return;
1693 case 'c':
1694 *ip->ap = 0;
1695 if (ip->argbuf[0] == '>') {
1696 ite_sendstr("\033[>24;0;0;0c");
1697 } else
1698 switch (ite_zargnum(ip)) {
1699 case 0:
1700 /*
1701 * primary DA request, send
1702 * primary DA response
1703 */
1704 if (ip->emul_level
1705 == EMUL_VT100)
1706 ite_sendstr(
1707 "\033[?1;1c");
1708 else
1709 ite_sendstr(
1710 "\033[?63;1c");
1711 break;
1712 }
1713 ip->escape = 0;
1714 return;
1715 case 'n':
1716 switch (ite_zargnum(ip)) {
1717 case 5:
1718 /* no malfunction */
1719 ite_sendstr("\033[0n");
1720 break;
1721 case 6:
1722 /* cursor position report */
1723 sprintf(ip->argbuf, "\033[%d;%dR",
1724 ip->cury + 1, ip->curx + 1);
1725 ite_sendstr(ip->argbuf);
1726 break;
1727 }
1728 ip->escape = 0;
1729 return;
1730 case 'x':
1731 switch (ite_zargnum(ip)) {
1732 case 0:
1733 /* Fake some terminal parameters. */
1734 ite_sendstr("\033[2;1;1;112;112;1;0x");
1735 break;
1736 case 1:
1737 ite_sendstr("\033[3;1;1;112;112;1;0x");
1738 break;
1739 }
1740 ip->escape = 0;
1741 return;
1742 case 'g':
1743 switch (ite_zargnum(ip)) {
1744 case 0:
1745 if (ip->curx < ip->cols)
1746 ip->tabs[ip->curx] = 0;
1747 break;
1748 case 3:
1749 for (n = 0; n < ip->cols; n++)
1750 ip->tabs[n] = 0;
1751 break;
1752 }
1753 ip->escape = 0;
1754 return;
1755 case 'h':
1756 case 'l':
1757 n = ite_zargnum(ip);
1758 switch (n) {
1759 case 4:
1760 /* insert/replace mode */
1761 ip->imode = (c == 'h');
1762 break;
1763 case 20:
1764 ip->linefeed_newline = (c == 'h');
1765 break;
1766 }
1767 ip->escape = 0;
1768 return;
1769 case 'M':
1770 ite_dnline(ip, ite_argnum(ip));
1771 ip->escape = 0;
1772 return;
1773 case 'L':
1774 ite_inline(ip, ite_argnum(ip));
1775 ip->escape = 0;
1776 return;
1777 case 'P':
1778 ite_dnchar(ip, ite_argnum(ip));
1779 ip->escape = 0;
1780 return;
1781 case '@':
1782 ite_inchar(ip, ite_argnum(ip));
1783 ip->escape = 0;
1784 return;
1785 case 'G':
1786 /*
1787 * this one was *not* in my vt320 manual but in
1788 * a vt320 termcap entry.. who is right? It's
1789 * supposed to set the horizontal cursor
1790 * position.
1791 */
1792 *ip->ap = 0;
1793 x = atoi(ip->argbuf);
1794 if (x)
1795 x--;
1796 ip->curx = min(x, ip->cols - 1);
1797 ip->escape = 0;
1798 SUBR_CURSOR(ip, MOVE_CURSOR);
1799 clr_attr(ip, ATTR_INV);
1800 return;
1801 case 'd':
1802 /*
1803 * same thing here, this one's for setting the
1804 * absolute vertical cursor position. Not
1805 * documented...
1806 */
1807 *ip->ap = 0;
1808 y = atoi(ip->argbuf);
1809 if (y)
1810 y--;
1811 if (ip->inside_margins)
1812 y += ip->top_margin;
1813 ip->cury = min(y, ip->rows - 1);
1814 ip->escape = 0;
1815 snap_cury(ip);
1816 SUBR_CURSOR(ip, MOVE_CURSOR);
1817 clr_attr(ip, ATTR_INV);
1818 return;
1819 case 'H':
1820 case 'f':
1821 *ip->ap = 0;
1822 y = atoi(ip->argbuf);
1823 x = 0;
1824 cp = index(ip->argbuf, ';');
1825 if (cp)
1826 x = atoi(cp + 1);
1827 if (x)
1828 x--;
1829 if (y)
1830 y--;
1831 if (ip->inside_margins)
1832 y += ip->top_margin;
1833 ip->cury = min(y, ip->rows - 1);
1834 ip->curx = min(x, ip->cols - 1);
1835 ip->escape = 0;
1836 snap_cury(ip);
1837 SUBR_CURSOR(ip, MOVE_CURSOR);
1838 clr_attr(ip, ATTR_INV);
1839 return;
1840 case 'A':
1841 n = ite_argnum(ip);
1842 n = ip->cury - (n ? n : 1);
1843 if (n < 0)
1844 n = 0;
1845 if (ip->inside_margins)
1846 n = max(ip->top_margin, n);
1847 else if (n == ip->top_margin - 1)
1848 /*
1849 * allow scrolling outside region, but
1850 * don't scroll out of active region
1851 * without explicit CUP
1852 */
1853 n = ip->top_margin;
1854 ip->cury = n;
1855 ip->escape = 0;
1856 SUBR_CURSOR(ip, MOVE_CURSOR);
1857 clr_attr(ip, ATTR_INV);
1858 return;
1859 case 'B':
1860 n = ite_argnum(ip);
1861 n = ip->cury + (n ? n : 1);
1862 n = min(ip->rows - 1, n);
1863 if (ip->inside_margins)
1864 n = min(ip->bottom_margin, n);
1865 else if (n == ip->bottom_margin + 1)
1866 /*
1867 * allow scrolling outside region, but
1868 * don't scroll out of active region
1869 * without explicit CUP
1870 */
1871 n = ip->bottom_margin;
1872 ip->cury = n;
1873 ip->escape = 0;
1874 SUBR_CURSOR(ip, MOVE_CURSOR);
1875 clr_attr(ip, ATTR_INV);
1876 return;
1877 case 'C':
1878 n = ite_argnum(ip);
1879 n = n ? n : 1;
1880 ip->curx = min(ip->curx + n, ip->cols - 1);
1881 ip->escape = 0;
1882 SUBR_CURSOR(ip, MOVE_CURSOR);
1883 clr_attr(ip, ATTR_INV);
1884 return;
1885 case 'D':
1886 n = ite_argnum(ip);
1887 n = n ? n : 1;
1888 n = ip->curx - n;
1889 ip->curx = n >= 0 ? n : 0;
1890 ip->escape = 0;
1891 SUBR_CURSOR(ip, MOVE_CURSOR);
1892 clr_attr(ip, ATTR_INV);
1893 return;
1894 case 'J':
1895 *ip->ap = 0;
1896 n = ite_zargnum(ip);
1897 if (n == 0)
1898 ite_clrtoeos(ip);
1899 else if (n == 1)
1900 ite_clrtobos(ip);
1901 else if (n == 2)
1902 ite_clrscreen(ip);
1903 ip->escape = 0;
1904 return;
1905 case 'K':
1906 n = ite_zargnum(ip);
1907 if (n == 0)
1908 ite_clrtoeol(ip);
1909 else if (n == 1)
1910 ite_clrtobol(ip);
1911 else if (n == 2)
1912 ite_clrline(ip);
1913 ip->escape = 0;
1914 return;
1915 case 'X':
1916 n = ite_argnum(ip) - 1;
1917 n = min(n, ip->cols - 1 - ip->curx);
1918 for (; n >= 0; n--) {
1919 attrclr(ip, ip->cury, ip->curx + n, 1, 1);
1920 SUBR_PUTC(ip, ' ', ip->cury, ip->curx + n, ATTR_NOR);
1921 }
1922 ip->escape = 0;
1923 return;
1924 case '}':
1925 case '`':
1926 /* status line control */
1927 ip->escape = 0;
1928 return;
1929 case 'r':
1930 *ip->ap = 0;
1931 x = atoi(ip->argbuf);
1932 x = x ? x : 1;
1933 y = ip->rows;
1934 cp = index(ip->argbuf, ';');
1935 if (cp) {
1936 y = atoi(cp + 1);
1937 y = y ? y : ip->rows;
1938 }
1939 if (y - x < 2) {
1940 /*
1941 * if illegal scrolling region, reset
1942 * to defaults
1943 */
1944 x = 1;
1945 y = ip->rows;
1946 }
1947 x--;
1948 y--;
1949 ip->top_margin = min(x, ip->rows - 1);
1950 ip->bottom_margin = min(y, ip->rows - 1);
1951 if (ip->inside_margins) {
1952 ip->cury = ip->top_margin;
1953 ip->curx = 0;
1954 SUBR_CURSOR(ip, MOVE_CURSOR);
1955 }
1956 ip->escape = 0;
1957 return;
1958 case 'm':
1959 /* big attribute setter/resetter */
1960 { char *cp;
1961 *ip->ap = 0;
1962 /* kludge to make CSIm work (== CSI0m) */
1963 if (ip->ap == ip->argbuf)
1964 ip->ap++;
1965 for (cp = ip->argbuf; cp < ip->ap;) {
1966 switch (*cp) {
1967 case 0:
1968 case '0':
1969 clr_attr(ip, ATTR_ALL);
1970 cp++;
1971 break;
1972
1973 case '1':
1974 set_attr(ip, ATTR_BOLD);
1975 cp++;
1976 break;
1977
1978 case '2':
1979 switch (cp[1]) {
1980 case '2':
1981 clr_attr(ip, ATTR_BOLD);
1982 cp += 2;
1983 break;
1984
1985 case '4':
1986 clr_attr(ip, ATTR_UL);
1987 cp += 2;
1988 break;
1989
1990 case '5':
1991 clr_attr(ip, ATTR_BLINK);
1992 cp += 2;
1993 break;
1994
1995 case '7':
1996 clr_attr(ip, ATTR_INV);
1997 cp += 2;
1998 break;
1999
2000 default:
2001 cp++;
2002 break;
2003 }
2004 break;
2005
2006 case '4':
2007 set_attr(ip, ATTR_UL);
2008 cp++;
2009 break;
2010
2011 case '5':
2012 set_attr(ip, ATTR_BLINK);
2013 cp++;
2014 break;
2015
2016 case '7':
2017 set_attr(ip, ATTR_INV);
2018 cp++;
2019 break;
2020
2021 default:
2022 cp++;
2023 break;
2024 }
2025 }
2026 ip->escape = 0;
2027 return; }
2028 case 'u':
2029 /* DECRQTSR */
2030 ite_sendstr("\033P\033\\");
2031 ip->escape = 0;
2032 return;
2033 default:
2034 ip->escape = 0;
2035 return;
2036 }
2037 break;
2038 case '?': /* CSI ? */
2039 switch (c) {
2040 case '0':
2041 case '1':
2042 case '2':
2043 case '3':
2044 case '4':
2045 case '5':
2046 case '6':
2047 case '7':
2048 case '8':
2049 case '9':
2050 case ';':
2051 case '\"':
2052 case '$':
2053 /*
2054 * Don't fill the last character;
2055 * it's needed.
2056 * XXX yeah, where ??
2057 */
2058 if (ip->ap < ip->argbuf + MAX_ARGSIZE - 1)
2059 *ip->ap++ = c;
2060 return;
2061 case 'n':
2062 *ip->ap = 0;
2063 if (ip->ap == &ip->argbuf[2]) {
2064 if (!strncmp(ip->argbuf, "15", 2))
2065 /* printer status: no printer */
2066 ite_sendstr("\033[13n");
2067
2068 else if (!strncmp(ip->argbuf, "25", 2))
2069 /* udk status */
2070 ite_sendstr("\033[20n");
2071
2072 else if (!strncmp(ip->argbuf, "26", 2))
2073 /* keyboard dialect: US */
2074 ite_sendstr("\033[27;1n");
2075 }
2076 ip->escape = 0;
2077 return;
2078 case 'h':
2079 case 'l':
2080 n = ite_zargnum(ip);
2081 switch (n) {
2082 case 1:
2083 ip->cursor_appmode = (c == 'h');
2084 break;
2085 case 3:
2086 /* 132/80 columns (132 == 'h') */
2087 break;
2088 case 4: /* smooth scroll */
2089 break;
2090 case 5:
2091 /*
2092 * light background (=='h') /dark
2093 * background(=='l')
2094 */
2095 break;
2096 case 6: /* origin mode */
2097 ip->inside_margins = (c == 'h');
2098 ip->curx = 0;
2099 ip->cury = ip->inside_margins ?
2100 ip->top_margin : 0;
2101 SUBR_CURSOR(ip, MOVE_CURSOR);
2102 break;
2103 case 7: /* auto wraparound */
2104 ip->auto_wrap = (c == 'h');
2105 break;
2106 case 8: /* keyboard repeat */
2107 ip->key_repeat = (c == 'h');
2108 break;
2109 case 20: /* newline mode */
2110 ip->linefeed_newline = (c == 'h');
2111 break;
2112 case 25: /* cursor on/off */
2113 SUBR_CURSOR(ip, (c == 'h') ?
2114 DRAW_CURSOR : ERASE_CURSOR);
2115 break;
2116 }
2117 ip->escape = 0;
2118 return;
2119 default:
2120 ip->escape = 0;
2121 return;
2122 }
2123 break;
2124 default:
2125 ip->escape = 0;
2126 return;
2127 }
2128 }
2129 switch (c) {
2130 case VT: /* VT is treated like LF */
2131 case FF: /* so is FF */
2132 case LF:
2133 /*
2134 * cr->crlf distinction is done here, on output, not on input!
2135 */
2136 if (ip->linefeed_newline)
2137 ite_crlf(ip);
2138 else
2139 ite_lf(ip);
2140 break;
2141 case CR:
2142 ite_cr(ip);
2143 break;
2144 case BS:
2145 if (--ip->curx < 0)
2146 ip->curx = 0;
2147 else
2148 SUBR_CURSOR(ip, MOVE_CURSOR);
2149 break;
2150 case HT:
2151 for (n = ip->curx + 1; n < ip->cols; n++) {
2152 if (ip->tabs[n]) {
2153 ip->curx = n;
2154 SUBR_CURSOR(ip, MOVE_CURSOR);
2155 break;
2156 }
2157 }
2158 break;
2159 case BEL:
2160 if (kbd_tty && kbd_ite && kbd_ite->tp == kbd_tty)
2161 ite_bell();
2162 break;
2163 case SO:
2164 ip->GL = ip->G1;
2165 break;
2166 case SI:
2167 ip->GL = ip->G0;
2168 break;
2169 case ENQ:
2170 /* send answer-back message !! */
2171 break;
2172 case CAN:
2173 ip->escape = 0; /* cancel any escape sequence in progress */
2174 break;
2175 case SUB:
2176 ip->escape = 0; /* dito, but see below */
2177 /* should also display a reverse question mark!! */
2178 break;
2179 case ESC:
2180 ip->escape = ESC;
2181 break;
2182 /*
2183 * now it gets weird.. 8bit control sequences..
2184 */
2185 case IND:
2186 /* index: move cursor down, scroll */
2187 ite_lf(ip);
2188 break;
2189 case NEL:
2190 /* next line. next line, first pos. */
2191 ite_crlf(ip);
2192 break;
2193 case HTS:
2194 /* set horizontal tab */
2195 if (ip->curx < ip->cols)
2196 ip->tabs[ip->curx] = 1;
2197 break;
2198 case RI:
2199 /* reverse index */
2200 ite_rlf(ip);
2201 break;
2202 case SS2:
2203 /* go into G2 for one character */
2204 /* not yet supported */
2205 break;
2206 case SS3:
2207 /* go into G3 for one character */
2208 break;
2209 case DCS:
2210 /* device control string introducer */
2211 ip->escape = DCS;
2212 ip->ap = ip->argbuf;
2213 break;
2214 case CSI:
2215 /* control sequence introducer */
2216 ip->escape = CSI;
2217 ip->ap = ip->argbuf;
2218 break;
2219 case ST:
2220 /* string terminator */
2221 /* ignore, if not used as terminator */
2222 break;
2223 case OSC:
2224 /*
2225 * introduces OS command. Ignore everything
2226 * upto ST
2227 */
2228 ip->escape = OSC;
2229 break;
2230 case PM:
2231 /* privacy message, ignore everything upto ST */
2232 ip->escape = PM;
2233 break;
2234 case APC:
2235 /*
2236 * application program command, ignore * everything upto ST
2237 */
2238 ip->escape = APC;
2239 break;
2240 default:
2241 if ((c & 0x7f) < ' ' || c == DEL)
2242 break;
2243 if (ip->imode)
2244 ite_inchar(ip, 1);
2245 iteprecheckwrap(ip);
2246 #ifdef DO_WEIRD_ATTRIBUTES
2247 if ((ip->attribute & ATTR_INV) || attrtest(ip, ATTR_INV)) {
2248 attrset(ip, ATTR_INV);
2249 SUBR_PUTC(ip, c, ip->cury, ip->curx, ATTR_INV);
2250 } else
2251 SUBR_PUTC(ip, c, ip->cury, ip->curx, ATTR_NOR);
2252 #else
2253 SUBR_PUTC(ip, c, ip->cury, ip->curx, ip->attribute);
2254 #endif
2255 SUBR_CURSOR(ip, DRAW_CURSOR);
2256 itecheckwrap(ip);
2257 break;
2258 }
2259 }
2260
2261