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