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