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