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