ite.c revision 1.35 1 /* $NetBSD: ite.c,v 1.35 1996/02/24 07:44:06 chopps 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 void
1331 ite_putstr(s, len, dev)
1332 const char *s;
1333 int len;
1334 dev_t dev;
1335 {
1336 struct ite_softc *ip;
1337 int i;
1338
1339 ip = getitesp(dev);
1340
1341 /* XXX avoid problems */
1342 if ((ip->flags & (ITE_ACTIVE|ITE_INGRF)) != ITE_ACTIVE)
1343 return;
1344
1345 SUBR_CURSOR(ip, START_CURSOROPT);
1346 for (i = 0; i < len; i++)
1347 if (s[i])
1348 iteputchar(s[i], ip);
1349 SUBR_CURSOR(ip, END_CURSOROPT);
1350 }
1351
1352 static void
1353 iteprecheckwrap(ip)
1354 struct ite_softc *ip;
1355 {
1356 if (ip->auto_wrap && ip->curx == ip->cols) {
1357 ip->curx = 0;
1358 clr_attr(ip, ATTR_INV);
1359 if (++ip->cury >= ip->bottom_margin + 1) {
1360 ip->cury = ip->bottom_margin;
1361 SUBR_CURSOR(ip, MOVE_CURSOR);
1362 SUBR_SCROLL(ip, ip->top_margin + 1, 0, 1, SCROLL_UP);
1363 ite_clrtoeol(ip);
1364 } else
1365 SUBR_CURSOR(ip, MOVE_CURSOR);
1366 }
1367 }
1368
1369 static void
1370 itecheckwrap(ip)
1371 struct ite_softc *ip;
1372 {
1373 #if 0
1374 if (++ip->curx == ip->cols) {
1375 if (ip->auto_wrap) {
1376 ip->curx = 0;
1377 clr_attr(ip, ATTR_INV);
1378 if (++ip->cury >= ip->bottom_margin + 1) {
1379 ip->cury = ip->bottom_margin;
1380 SUBR_CURSOR(ip, MOVE_CURSOR);
1381 SUBR_SCROLL(ip, ip->top_margin + 1, 0, 1, SCROLL_UP);
1382 ite_clrtoeol(ip);
1383 return;
1384 }
1385 } else
1386 /* stay there if no autowrap.. */
1387 ip->curx--;
1388 }
1389 #else
1390 if (ip->curx < ip->cols) {
1391 ip->curx++;
1392 SUBR_CURSOR(ip, MOVE_CURSOR);
1393 }
1394 #endif
1395 }
1396
1397 void
1398 iteputchar(c, ip)
1399 register int c;
1400 struct ite_softc *ip;
1401 {
1402 struct tty *kbd_tty;
1403 int n, x, y;
1404 char *cp;
1405
1406 if (kbd_ite == NULL)
1407 kbd_tty = NULL;
1408 else
1409 kbd_tty = kbd_ite->tp;
1410
1411 if (ip->escape) {
1412 switch (ip->escape) {
1413 case ESC:
1414 switch (c) {
1415 /*
1416 * first 7bit equivalents for the
1417 * 8bit control characters
1418 */
1419 case 'D':
1420 c = IND;
1421 ip->escape = 0;
1422 break;
1423 /*
1424 * and fall into the next
1425 * switch below (same for all `break')
1426 */
1427 case 'E':
1428 c = NEL;
1429 ip->escape = 0;
1430 break;
1431 case 'H':
1432 c = HTS;
1433 ip->escape = 0;
1434 break;
1435 case 'M':
1436 c = RI;
1437 ip->escape = 0;
1438 break;
1439 case 'N':
1440 c = SS2;
1441 ip->escape = 0;
1442 break;
1443 case 'O':
1444 c = SS3;
1445 ip->escape = 0;
1446 break;
1447 case 'P':
1448 c = DCS;
1449 ip->escape = 0;
1450 break;
1451 case '[':
1452 c = CSI;
1453 ip->escape = 0;
1454 break;
1455 case '\\':
1456 c = ST;
1457 ip->escape = 0;
1458 break;
1459 case ']':
1460 c = OSC;
1461 ip->escape = 0;
1462 break;
1463 case '^':
1464 c = PM;
1465 ip->escape = 0;
1466 break;
1467 case '_':
1468 c = APC;
1469 ip->escape = 0;
1470 break;
1471 /* introduces 7/8bit control */
1472 case ' ':
1473 /* can be followed by either F or G */
1474 ip->escape = ' ';
1475 break;
1476 /*
1477 * a lot of character set selections, not yet
1478 * used... 94-character sets:
1479 */
1480 case '(': /* G0 */
1481 case ')': /* G1 */
1482 ip->escape = c;
1483 return;
1484 case '*': /* G2 */
1485 case '+': /* G3 */
1486 case 'B': /* ASCII */
1487 case 'A': /* ISO latin 1 */
1488 case '<': /* user preferred suplemental */
1489 case '0': /* dec special graphics */
1490 /*
1491 * 96-character sets:
1492 */
1493 case '-': /* G1 */
1494 case '.': /* G2 */
1495 case '/': /* G3 */
1496 /*
1497 * national character sets:
1498 */
1499 case '4': /* dutch */
1500 case '5':
1501 case 'C': /* finnish */
1502 case 'R': /* french */
1503 case 'Q': /* french canadian */
1504 case 'K': /* german */
1505 case 'Y': /* italian */
1506 case '6': /* norwegian/danish */
1507 /*
1508 * note: %5 and %6 are not supported (two
1509 * chars..)
1510 */
1511 ip->escape = 0;
1512 /* just ignore for now */
1513 return;
1514 /*
1515 * locking shift modes (as you might guess, not
1516 * yet supported..)
1517 */
1518 case '`':
1519 ip->GR = ip->G1;
1520 ip->escape = 0;
1521 return;
1522 case 'n':
1523 ip->GL = ip->G2;
1524 ip->escape = 0;
1525 return;
1526 case '}':
1527 ip->GR = ip->G2;
1528 ip->escape = 0;
1529 return;
1530 case 'o':
1531 ip->GL = ip->G3;
1532 ip->escape = 0;
1533 return;
1534 case '|':
1535 ip->GR = ip->G3;
1536 ip->escape = 0;
1537 return;
1538 case '#':
1539 /* font width/height control */
1540 ip->escape = '#';
1541 return;
1542 case 'c':
1543 /* hard terminal reset .. */
1544 ite_reset(ip);
1545 SUBR_CURSOR(ip, MOVE_CURSOR);
1546 ip->escape = 0;
1547 return;
1548 case '7':
1549 ip->save_curx = ip->curx;
1550 ip->save_cury = ip->cury;
1551 ip->save_attribute = ip->attribute;
1552 ip->escape = 0;
1553 return;
1554 case '8':
1555 ip->curx = ip->save_curx;
1556 ip->cury = ip->save_cury;
1557 ip->attribute = ip->save_attribute;
1558 SUBR_CURSOR(ip, MOVE_CURSOR);
1559 ip->escape = 0;
1560 return;
1561 case '=':
1562 ip->keypad_appmode = 1;
1563 ip->escape = 0;
1564 return;
1565 case '>':
1566 ip->keypad_appmode = 0;
1567 ip->escape = 0;
1568 return;
1569 case 'Z': /* request ID */
1570 /* XXX not clean */
1571 if (ip->emul_level == EMUL_VT100)
1572 ite_sendstr("\033[?61;0c");
1573 else
1574 ite_sendstr("\033[?63;0c");
1575 ip->escape = 0;
1576 return;
1577 default:
1578 /*
1579 * default catch all for not recognized ESC
1580 * sequences
1581 */
1582 ip->escape = 0;
1583 return;
1584 }
1585 break;
1586 case '(':
1587 case ')':
1588 ip->escape = 0;
1589 return;
1590 case ' ':
1591 switch (c) {
1592 case 'F':
1593 ip->eightbit_C1 = 0;
1594 ip->escape = 0;
1595 return;
1596 case 'G':
1597 ip->eightbit_C1 = 1;
1598 ip->escape = 0;
1599 return;
1600 default:
1601 /* not supported */
1602 ip->escape = 0;
1603 return;
1604 }
1605 break;
1606 case '#':
1607 switch (c) {
1608 case '5':
1609 /* single height, single width */
1610 ip->escape = 0;
1611 return;
1612 case '6':
1613 /* double width, single height */
1614 ip->escape = 0;
1615 return;
1616 case '3':
1617 /* top half */
1618 ip->escape = 0;
1619 return;
1620 case '4':
1621 /* bottom half */
1622 ip->escape = 0;
1623 return;
1624 case '8':
1625 /* screen alignment pattern... */
1626 alignment_display(ip);
1627 ip->escape = 0;
1628 return;
1629 default:
1630 ip->escape = 0;
1631 return;
1632 }
1633 break;
1634 case CSI:
1635 /* the biggie... */
1636 switch (c) {
1637 case '0':
1638 case '1':
1639 case '2':
1640 case '3':
1641 case '4':
1642 case '5':
1643 case '6':
1644 case '7':
1645 case '8':
1646 case '9':
1647 case ';':
1648 case '\"':
1649 case '$':
1650 case '>':
1651 if (ip->ap < ip->argbuf + MAX_ARGSIZE)
1652 *ip->ap++ = c;
1653 return;
1654 case BS:
1655 /*
1656 * you wouldn't believe such perversion is
1657 * possible? it is.. BS is allowed in between
1658 * cursor sequences (at least), according to
1659 * vttest..
1660 */
1661 if (--ip->curx < 0)
1662 ip->curx = 0;
1663 else
1664 SUBR_CURSOR(ip, MOVE_CURSOR);
1665 break;
1666 case 'p':
1667 *ip->ap = 0;
1668 if (!strncmp(ip->argbuf, "61\"", 3))
1669 ip->emul_level = EMUL_VT100;
1670 else if (!strncmp(ip->argbuf, "63;1\"", 5)
1671 || !strncmp(ip->argbuf, "62;1\"", 5))
1672 ip->emul_level = EMUL_VT300_7;
1673 else
1674 ip->emul_level = EMUL_VT300_8;
1675 ip->escape = 0;
1676 return;
1677 case '?':
1678 *ip->ap = 0;
1679 ip->escape = '?';
1680 ip->ap = ip->argbuf;
1681 return;
1682 case 'c':
1683 *ip->ap = 0;
1684 if (ip->argbuf[0] == '>') {
1685 ite_sendstr("\033[>24;0;0;0c");
1686 } else
1687 switch (ite_zargnum(ip)) {
1688 case 0:
1689 /*
1690 * primary DA request, send
1691 * primary DA response
1692 */
1693 if (ip->emul_level
1694 == EMUL_VT100)
1695 ite_sendstr(
1696 "\033[?1;1c");
1697 else
1698 ite_sendstr(
1699 "\033[?63;1c");
1700 break;
1701 }
1702 ip->escape = 0;
1703 return;
1704 case 'n':
1705 switch (ite_zargnum(ip)) {
1706 case 5:
1707 /* no malfunction */
1708 ite_sendstr("\033[0n");
1709 break;
1710 case 6:
1711 /* cursor position report */
1712 sprintf(ip->argbuf, "\033[%d;%dR",
1713 ip->cury + 1, ip->curx + 1);
1714 ite_sendstr(ip->argbuf);
1715 break;
1716 }
1717 ip->escape = 0;
1718 return;
1719 case 'x':
1720 switch (ite_zargnum(ip)) {
1721 case 0:
1722 /* Fake some terminal parameters. */
1723 ite_sendstr("\033[2;1;1;112;112;1;0x");
1724 break;
1725 case 1:
1726 ite_sendstr("\033[3;1;1;112;112;1;0x");
1727 break;
1728 }
1729 ip->escape = 0;
1730 return;
1731 case 'g':
1732 switch (ite_zargnum(ip)) {
1733 case 0:
1734 if (ip->curx < ip->cols)
1735 ip->tabs[ip->curx] = 0;
1736 break;
1737 case 3:
1738 for (n = 0; n < ip->cols; n++)
1739 ip->tabs[n] = 0;
1740 break;
1741 }
1742 ip->escape = 0;
1743 return;
1744 case 'h':
1745 case 'l':
1746 n = ite_zargnum(ip);
1747 switch (n) {
1748 case 4:
1749 /* insert/replace mode */
1750 ip->imode = (c == 'h');
1751 break;
1752 case 20:
1753 ip->linefeed_newline = (c == 'h');
1754 break;
1755 }
1756 ip->escape = 0;
1757 return;
1758 case 'M':
1759 ite_dnline(ip, ite_argnum(ip));
1760 ip->escape = 0;
1761 return;
1762 case 'L':
1763 ite_inline(ip, ite_argnum(ip));
1764 ip->escape = 0;
1765 return;
1766 case 'P':
1767 ite_dnchar(ip, ite_argnum(ip));
1768 ip->escape = 0;
1769 return;
1770 case '@':
1771 ite_inchar(ip, ite_argnum(ip));
1772 ip->escape = 0;
1773 return;
1774 case 'G':
1775 /*
1776 * this one was *not* in my vt320 manual but in
1777 * a vt320 termcap entry.. who is right? It's
1778 * supposed to set the horizontal cursor
1779 * position.
1780 */
1781 *ip->ap = 0;
1782 x = atoi(ip->argbuf);
1783 if (x)
1784 x--;
1785 ip->curx = min(x, ip->cols - 1);
1786 ip->escape = 0;
1787 SUBR_CURSOR(ip, MOVE_CURSOR);
1788 clr_attr(ip, ATTR_INV);
1789 return;
1790 case 'd':
1791 /*
1792 * same thing here, this one's for setting the
1793 * absolute vertical cursor position. Not
1794 * documented...
1795 */
1796 *ip->ap = 0;
1797 y = atoi(ip->argbuf);
1798 if (y)
1799 y--;
1800 if (ip->inside_margins)
1801 y += ip->top_margin;
1802 ip->cury = min(y, ip->rows - 1);
1803 ip->escape = 0;
1804 snap_cury(ip);
1805 SUBR_CURSOR(ip, MOVE_CURSOR);
1806 clr_attr(ip, ATTR_INV);
1807 return;
1808 case 'H':
1809 case 'f':
1810 *ip->ap = 0;
1811 y = atoi(ip->argbuf);
1812 x = 0;
1813 cp = index(ip->argbuf, ';');
1814 if (cp)
1815 x = atoi(cp + 1);
1816 if (x)
1817 x--;
1818 if (y)
1819 y--;
1820 if (ip->inside_margins)
1821 y += ip->top_margin;
1822 ip->cury = min(y, ip->rows - 1);
1823 ip->curx = min(x, ip->cols - 1);
1824 ip->escape = 0;
1825 snap_cury(ip);
1826 SUBR_CURSOR(ip, MOVE_CURSOR);
1827 clr_attr(ip, ATTR_INV);
1828 return;
1829 case 'A':
1830 n = ite_argnum(ip);
1831 n = ip->cury - (n ? n : 1);
1832 if (n < 0)
1833 n = 0;
1834 if (ip->inside_margins)
1835 n = max(ip->top_margin, n);
1836 else if (n == ip->top_margin - 1)
1837 /*
1838 * allow scrolling outside region, but
1839 * don't scroll out of active region
1840 * without explicit CUP
1841 */
1842 n = ip->top_margin;
1843 ip->cury = n;
1844 ip->escape = 0;
1845 SUBR_CURSOR(ip, MOVE_CURSOR);
1846 clr_attr(ip, ATTR_INV);
1847 return;
1848 case 'B':
1849 n = ite_argnum(ip);
1850 n = ip->cury + (n ? n : 1);
1851 n = min(ip->rows - 1, n);
1852 if (ip->inside_margins)
1853 n = min(ip->bottom_margin, n);
1854 else if (n == ip->bottom_margin + 1)
1855 /*
1856 * allow scrolling outside region, but
1857 * don't scroll out of active region
1858 * without explicit CUP
1859 */
1860 n = ip->bottom_margin;
1861 ip->cury = n;
1862 ip->escape = 0;
1863 SUBR_CURSOR(ip, MOVE_CURSOR);
1864 clr_attr(ip, ATTR_INV);
1865 return;
1866 case 'C':
1867 n = ite_argnum(ip);
1868 n = n ? n : 1;
1869 ip->curx = min(ip->curx + n, ip->cols - 1);
1870 ip->escape = 0;
1871 SUBR_CURSOR(ip, MOVE_CURSOR);
1872 clr_attr(ip, ATTR_INV);
1873 return;
1874 case 'D':
1875 n = ite_argnum(ip);
1876 n = n ? n : 1;
1877 n = ip->curx - n;
1878 ip->curx = n >= 0 ? n : 0;
1879 ip->escape = 0;
1880 SUBR_CURSOR(ip, MOVE_CURSOR);
1881 clr_attr(ip, ATTR_INV);
1882 return;
1883 case 'J':
1884 *ip->ap = 0;
1885 n = ite_zargnum(ip);
1886 if (n == 0)
1887 ite_clrtoeos(ip);
1888 else if (n == 1)
1889 ite_clrtobos(ip);
1890 else if (n == 2)
1891 ite_clrscreen(ip);
1892 ip->escape = 0;
1893 return;
1894 case 'K':
1895 n = ite_zargnum(ip);
1896 if (n == 0)
1897 ite_clrtoeol(ip);
1898 else if (n == 1)
1899 ite_clrtobol(ip);
1900 else if (n == 2)
1901 ite_clrline(ip);
1902 ip->escape = 0;
1903 return;
1904 case 'X':
1905 n = ite_argnum(ip) - 1;
1906 n = min(n, ip->cols - 1 - ip->curx);
1907 for (; n >= 0; n--) {
1908 attrclr(ip, ip->cury, ip->curx + n, 1, 1);
1909 SUBR_PUTC(ip, ' ', ip->cury, ip->curx + n, ATTR_NOR);
1910 }
1911 ip->escape = 0;
1912 return;
1913 case '}':
1914 case '`':
1915 /* status line control */
1916 ip->escape = 0;
1917 return;
1918 case 'r':
1919 *ip->ap = 0;
1920 x = atoi(ip->argbuf);
1921 x = x ? x : 1;
1922 y = ip->rows;
1923 cp = index(ip->argbuf, ';');
1924 if (cp) {
1925 y = atoi(cp + 1);
1926 y = y ? y : ip->rows;
1927 }
1928 if (y - x < 2) {
1929 /*
1930 * if illegal scrolling region, reset
1931 * to defaults
1932 */
1933 x = 1;
1934 y = ip->rows;
1935 }
1936 x--;
1937 y--;
1938 ip->top_margin = min(x, ip->rows - 1);
1939 ip->bottom_margin = min(y, ip->rows - 1);
1940 if (ip->inside_margins) {
1941 ip->cury = ip->top_margin;
1942 ip->curx = 0;
1943 SUBR_CURSOR(ip, MOVE_CURSOR);
1944 }
1945 ip->escape = 0;
1946 return;
1947 case 'm':
1948 /* big attribute setter/resetter */
1949 { char *cp;
1950 *ip->ap = 0;
1951 /* kludge to make CSIm work (== CSI0m) */
1952 if (ip->ap == ip->argbuf)
1953 ip->ap++;
1954 for (cp = ip->argbuf; cp < ip->ap;) {
1955 switch (*cp) {
1956 case 0:
1957 case '0':
1958 clr_attr(ip, ATTR_ALL);
1959 cp++;
1960 break;
1961
1962 case '1':
1963 set_attr(ip, ATTR_BOLD);
1964 cp++;
1965 break;
1966
1967 case '2':
1968 switch (cp[1]) {
1969 case '2':
1970 clr_attr(ip, ATTR_BOLD);
1971 cp += 2;
1972 break;
1973
1974 case '4':
1975 clr_attr(ip, ATTR_UL);
1976 cp += 2;
1977 break;
1978
1979 case '5':
1980 clr_attr(ip, ATTR_BLINK);
1981 cp += 2;
1982 break;
1983
1984 case '7':
1985 clr_attr(ip, ATTR_INV);
1986 cp += 2;
1987 break;
1988
1989 default:
1990 cp++;
1991 break;
1992 }
1993 break;
1994
1995 case '4':
1996 set_attr(ip, ATTR_UL);
1997 cp++;
1998 break;
1999
2000 case '5':
2001 set_attr(ip, ATTR_BLINK);
2002 cp++;
2003 break;
2004
2005 case '7':
2006 set_attr(ip, ATTR_INV);
2007 cp++;
2008 break;
2009
2010 default:
2011 cp++;
2012 break;
2013 }
2014 }
2015 ip->escape = 0;
2016 return; }
2017 case 'u':
2018 /* DECRQTSR */
2019 ite_sendstr("\033P\033\\");
2020 ip->escape = 0;
2021 return;
2022 default:
2023 ip->escape = 0;
2024 return;
2025 }
2026 break;
2027 case '?': /* CSI ? */
2028 switch (c) {
2029 case '0':
2030 case '1':
2031 case '2':
2032 case '3':
2033 case '4':
2034 case '5':
2035 case '6':
2036 case '7':
2037 case '8':
2038 case '9':
2039 case ';':
2040 case '\"':
2041 case '$':
2042 /*
2043 * Don't fill the last character;
2044 * it's needed.
2045 * XXX yeah, where ??
2046 */
2047 if (ip->ap < ip->argbuf + MAX_ARGSIZE - 1)
2048 *ip->ap++ = c;
2049 return;
2050 case 'n':
2051 *ip->ap = 0;
2052 if (ip->ap == &ip->argbuf[2]) {
2053 if (!strncmp(ip->argbuf, "15", 2))
2054 /* printer status: no printer */
2055 ite_sendstr("\033[13n");
2056
2057 else if (!strncmp(ip->argbuf, "25", 2))
2058 /* udk status */
2059 ite_sendstr("\033[20n");
2060
2061 else if (!strncmp(ip->argbuf, "26", 2))
2062 /* keyboard dialect: US */
2063 ite_sendstr("\033[27;1n");
2064 }
2065 ip->escape = 0;
2066 return;
2067 case 'h':
2068 case 'l':
2069 n = ite_zargnum(ip);
2070 switch (n) {
2071 case 1:
2072 ip->cursor_appmode = (c == 'h');
2073 break;
2074 case 3:
2075 /* 132/80 columns (132 == 'h') */
2076 break;
2077 case 4: /* smooth scroll */
2078 break;
2079 case 5:
2080 /*
2081 * light background (=='h') /dark
2082 * background(=='l')
2083 */
2084 break;
2085 case 6: /* origin mode */
2086 ip->inside_margins = (c == 'h');
2087 ip->curx = 0;
2088 ip->cury = ip->inside_margins ?
2089 ip->top_margin : 0;
2090 SUBR_CURSOR(ip, MOVE_CURSOR);
2091 break;
2092 case 7: /* auto wraparound */
2093 ip->auto_wrap = (c == 'h');
2094 break;
2095 case 8: /* keyboard repeat */
2096 ip->key_repeat = (c == 'h');
2097 break;
2098 case 20: /* newline mode */
2099 ip->linefeed_newline = (c == 'h');
2100 break;
2101 case 25: /* cursor on/off */
2102 SUBR_CURSOR(ip, (c == 'h') ?
2103 DRAW_CURSOR : ERASE_CURSOR);
2104 break;
2105 }
2106 ip->escape = 0;
2107 return;
2108 default:
2109 ip->escape = 0;
2110 return;
2111 }
2112 break;
2113 default:
2114 ip->escape = 0;
2115 return;
2116 }
2117 }
2118 switch (c) {
2119 case VT: /* VT is treated like LF */
2120 case FF: /* so is FF */
2121 case LF:
2122 /*
2123 * cr->crlf distinction is done here, on output, not on input!
2124 */
2125 if (ip->linefeed_newline)
2126 ite_crlf(ip);
2127 else
2128 ite_lf(ip);
2129 break;
2130 case CR:
2131 ite_cr(ip);
2132 break;
2133 case BS:
2134 if (--ip->curx < 0)
2135 ip->curx = 0;
2136 else
2137 SUBR_CURSOR(ip, MOVE_CURSOR);
2138 break;
2139 case HT:
2140 for (n = ip->curx + 1; n < ip->cols; n++) {
2141 if (ip->tabs[n]) {
2142 ip->curx = n;
2143 SUBR_CURSOR(ip, MOVE_CURSOR);
2144 break;
2145 }
2146 }
2147 break;
2148 case BEL:
2149 if (kbd_tty && kbd_ite && kbd_ite->tp == kbd_tty)
2150 ite_bell();
2151 break;
2152 case SO:
2153 ip->GL = ip->G1;
2154 break;
2155 case SI:
2156 ip->GL = ip->G0;
2157 break;
2158 case ENQ:
2159 /* send answer-back message !! */
2160 break;
2161 case CAN:
2162 ip->escape = 0; /* cancel any escape sequence in progress */
2163 break;
2164 case SUB:
2165 ip->escape = 0; /* dito, but see below */
2166 /* should also display a reverse question mark!! */
2167 break;
2168 case ESC:
2169 ip->escape = ESC;
2170 break;
2171 /*
2172 * now it gets weird.. 8bit control sequences..
2173 */
2174 case IND:
2175 /* index: move cursor down, scroll */
2176 ite_lf(ip);
2177 break;
2178 case NEL:
2179 /* next line. next line, first pos. */
2180 ite_crlf(ip);
2181 break;
2182 case HTS:
2183 /* set horizontal tab */
2184 if (ip->curx < ip->cols)
2185 ip->tabs[ip->curx] = 1;
2186 break;
2187 case RI:
2188 /* reverse index */
2189 ite_rlf(ip);
2190 break;
2191 case SS2:
2192 /* go into G2 for one character */
2193 /* not yet supported */
2194 break;
2195 case SS3:
2196 /* go into G3 for one character */
2197 break;
2198 case DCS:
2199 /* device control string introducer */
2200 ip->escape = DCS;
2201 ip->ap = ip->argbuf;
2202 break;
2203 case CSI:
2204 /* control sequence introducer */
2205 ip->escape = CSI;
2206 ip->ap = ip->argbuf;
2207 break;
2208 case ST:
2209 /* string terminator */
2210 /* ignore, if not used as terminator */
2211 break;
2212 case OSC:
2213 /*
2214 * introduces OS command. Ignore everything
2215 * upto ST
2216 */
2217 ip->escape = OSC;
2218 break;
2219 case PM:
2220 /* privacy message, ignore everything upto ST */
2221 ip->escape = PM;
2222 break;
2223 case APC:
2224 /*
2225 * application program command, ignore * everything upto ST
2226 */
2227 ip->escape = APC;
2228 break;
2229 default:
2230 if ((c & 0x7f) < ' ' || c == DEL)
2231 break;
2232 if (ip->imode)
2233 ite_inchar(ip, 1);
2234 iteprecheckwrap(ip);
2235 #ifdef DO_WEIRD_ATTRIBUTES
2236 if ((ip->attribute & ATTR_INV) || attrtest(ip, ATTR_INV)) {
2237 attrset(ip, ATTR_INV);
2238 SUBR_PUTC(ip, c, ip->cury, ip->curx, ATTR_INV);
2239 } else
2240 SUBR_PUTC(ip, c, ip->cury, ip->curx, ATTR_NOR);
2241 #else
2242 SUBR_PUTC(ip, c, ip->cury, ip->curx, ip->attribute);
2243 #endif
2244 SUBR_CURSOR(ip, DRAW_CURSOR);
2245 itecheckwrap(ip);
2246 break;
2247 }
2248 }
2249
2250