1b4b1c516SEd Schouten /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
4b4b1c516SEd Schouten * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5b4b1c516SEd Schouten * All rights reserved.
6b4b1c516SEd Schouten *
7b4b1c516SEd Schouten * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
8b4b1c516SEd Schouten * All rights reserved.
9b4b1c516SEd Schouten *
10b4b1c516SEd Schouten * Redistribution and use in source and binary forms, with or without
11b4b1c516SEd Schouten * modification, are permitted provided that the following conditions
12b4b1c516SEd Schouten * are met:
13b4b1c516SEd Schouten * 1. Redistributions of source code must retain the above copyright
14b4b1c516SEd Schouten * notice, this list of conditions and the following disclaimer as
15b4b1c516SEd Schouten * the first lines of this file unmodified.
16b4b1c516SEd Schouten * 2. Redistributions in binary form must reproduce the above copyright
17b4b1c516SEd Schouten * notice, this list of conditions and the following disclaimer in the
18b4b1c516SEd Schouten * documentation and/or other materials provided with the distribution.
19b4b1c516SEd Schouten *
20b4b1c516SEd Schouten * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
21b4b1c516SEd Schouten * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22b4b1c516SEd Schouten * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23b4b1c516SEd Schouten * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24b4b1c516SEd Schouten * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25b4b1c516SEd Schouten * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26b4b1c516SEd Schouten * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27b4b1c516SEd Schouten * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28b4b1c516SEd Schouten * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29b4b1c516SEd Schouten * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30b4b1c516SEd Schouten */
31b4b1c516SEd Schouten
32b4b1c516SEd Schouten #include <sys/cdefs.h>
33b4b1c516SEd Schouten #include "opt_syscons.h"
3487da28e9SEd Schouten #include "opt_teken.h"
35b4b1c516SEd Schouten
36b4b1c516SEd Schouten #include <sys/param.h>
37b4b1c516SEd Schouten #include <sys/systm.h>
38b4b1c516SEd Schouten #include <sys/kernel.h>
39b4b1c516SEd Schouten #include <sys/module.h>
40b4b1c516SEd Schouten #include <sys/consio.h>
413a8a07eaSEd Schouten #include <sys/kbio.h>
42b4b1c516SEd Schouten
4376a0183eSMateusz Guzik #if defined(__arm__) || defined(__powerpc__)
44b4b1c516SEd Schouten #include <machine/sc_machdep.h>
45b4b1c516SEd Schouten #else
46b4b1c516SEd Schouten #include <machine/pc/display.h>
47b4b1c516SEd Schouten #endif
48b4b1c516SEd Schouten
49b4b1c516SEd Schouten #include <dev/syscons/syscons.h>
50b4b1c516SEd Schouten
519b934d09SEd Schouten #include <teken/teken.h>
52b4b1c516SEd Schouten
53eeb7d30eSBruce Evans static void scteken_sc_to_te_attr(unsigned char, teken_attr_t *);
54eeb7d30eSBruce Evans static int scteken_te_to_sc_attr(const teken_attr_t *);
55b4b1c516SEd Schouten
56b4b1c516SEd Schouten static sc_term_init_t scteken_init;
57b4b1c516SEd Schouten static sc_term_term_t scteken_term;
58b4b1c516SEd Schouten static sc_term_puts_t scteken_puts;
59b4b1c516SEd Schouten static sc_term_ioctl_t scteken_ioctl;
60b4b1c516SEd Schouten static sc_term_default_attr_t scteken_default_attr;
61b4b1c516SEd Schouten static sc_term_clear_t scteken_clear;
62b4b1c516SEd Schouten static sc_term_input_t scteken_input;
633a8a07eaSEd Schouten static sc_term_fkeystr_t scteken_fkeystr;
64912da699SBruce Evans static sc_term_sync_t scteken_sync;
65b4b1c516SEd Schouten static void scteken_nop(void);
66b4b1c516SEd Schouten
67b4b1c516SEd Schouten typedef struct {
68b4b1c516SEd Schouten teken_t ts_teken;
69b4b1c516SEd Schouten int ts_busy;
70b4b1c516SEd Schouten } teken_stat;
71b4b1c516SEd Schouten
72b4b1c516SEd Schouten static teken_stat reserved_teken_stat;
73b4b1c516SEd Schouten
74af032a9dSBruce Evans static void scteken_sync_internal(scr_stat *, teken_stat *);
75af032a9dSBruce Evans
76b4b1c516SEd Schouten static sc_term_sw_t sc_term_scteken = {
77b4b1c516SEd Schouten { NULL, NULL },
78b4b1c516SEd Schouten "scteken", /* emulator name */
79b4b1c516SEd Schouten "teken terminal", /* description */
80b4b1c516SEd Schouten "*", /* matching renderer */
81b4b1c516SEd Schouten sizeof(teken_stat), /* softc size */
82b4b1c516SEd Schouten 0,
83b4b1c516SEd Schouten scteken_init,
84b4b1c516SEd Schouten scteken_term,
85b4b1c516SEd Schouten scteken_puts,
86b4b1c516SEd Schouten scteken_ioctl,
87b4b1c516SEd Schouten (sc_term_reset_t *)scteken_nop,
88b4b1c516SEd Schouten scteken_default_attr,
89b4b1c516SEd Schouten scteken_clear,
90b4b1c516SEd Schouten (sc_term_notify_t *)scteken_nop,
91b4b1c516SEd Schouten scteken_input,
923a8a07eaSEd Schouten scteken_fkeystr,
93912da699SBruce Evans scteken_sync,
94b4b1c516SEd Schouten };
95b4b1c516SEd Schouten
96b4b1c516SEd Schouten SCTERM_MODULE(scteken, sc_term_scteken);
97b4b1c516SEd Schouten
98b4b1c516SEd Schouten static tf_bell_t scteken_bell;
99b4b1c516SEd Schouten static tf_cursor_t scteken_cursor;
100b4b1c516SEd Schouten static tf_putchar_t scteken_putchar;
101b4b1c516SEd Schouten static tf_fill_t scteken_fill;
102b4b1c516SEd Schouten static tf_copy_t scteken_copy;
103b4b1c516SEd Schouten static tf_param_t scteken_param;
104b4b1c516SEd Schouten static tf_respond_t scteken_respond;
105b4b1c516SEd Schouten
106b4b1c516SEd Schouten static const teken_funcs_t scteken_funcs = {
107b4b1c516SEd Schouten .tf_bell = scteken_bell,
108b4b1c516SEd Schouten .tf_cursor = scteken_cursor,
109b4b1c516SEd Schouten .tf_putchar = scteken_putchar,
110b4b1c516SEd Schouten .tf_fill = scteken_fill,
111b4b1c516SEd Schouten .tf_copy = scteken_copy,
112b4b1c516SEd Schouten .tf_param = scteken_param,
113b4b1c516SEd Schouten .tf_respond = scteken_respond,
114b4b1c516SEd Schouten };
115b4b1c516SEd Schouten
116b4b1c516SEd Schouten static int
scteken_init(scr_stat * scp,void ** softc,int code)117b4b1c516SEd Schouten scteken_init(scr_stat *scp, void **softc, int code)
118b4b1c516SEd Schouten {
1191356a080SEd Schouten teken_stat *ts;
120af032a9dSBruce Evans teken_attr_t ta;
121b4b1c516SEd Schouten
122b4b1c516SEd Schouten if (*softc == NULL) {
123b4b1c516SEd Schouten if (reserved_teken_stat.ts_busy)
124b4b1c516SEd Schouten return (EINVAL);
125b4b1c516SEd Schouten *softc = &reserved_teken_stat;
126b4b1c516SEd Schouten }
127b4b1c516SEd Schouten ts = *softc;
128b4b1c516SEd Schouten
129b4b1c516SEd Schouten switch (code) {
130b4b1c516SEd Schouten case SC_TE_COLD_INIT:
131b4b1c516SEd Schouten ++sc_term_scteken.te_refcount;
132b4b1c516SEd Schouten ts->ts_busy = 1;
133b4b1c516SEd Schouten /* FALLTHROUGH */
134b4b1c516SEd Schouten case SC_TE_WARM_INIT:
135af032a9dSBruce Evans ta = *teken_get_defattr(&ts->ts_teken);
136b4b1c516SEd Schouten teken_init(&ts->ts_teken, &scteken_funcs, scp);
137af032a9dSBruce Evans teken_set_defattr(&ts->ts_teken, &ta);
138e06d84fcSEd Schouten #ifndef TEKEN_UTF8
139e06d84fcSEd Schouten teken_set_8bit(&ts->ts_teken);
140e06d84fcSEd Schouten #endif /* !TEKEN_UTF8 */
141e42fc368SEd Schouten #ifdef TEKEN_CONS25
14287da28e9SEd Schouten teken_set_cons25(&ts->ts_teken);
143e42fc368SEd Schouten #endif /* TEKEN_CONS25 */
1443a199184SBruce Evans teken_set_cons25keys(&ts->ts_teken);
145af032a9dSBruce Evans scteken_sync_internal(scp, ts);
146b4b1c516SEd Schouten break;
147b4b1c516SEd Schouten }
148b4b1c516SEd Schouten
149b4b1c516SEd Schouten return (0);
150b4b1c516SEd Schouten }
151b4b1c516SEd Schouten
152b4b1c516SEd Schouten static int
scteken_term(scr_stat * scp,void ** softc)153b4b1c516SEd Schouten scteken_term(scr_stat *scp, void **softc)
154b4b1c516SEd Schouten {
155b4b1c516SEd Schouten
156b4b1c516SEd Schouten if (*softc == &reserved_teken_stat) {
157b4b1c516SEd Schouten *softc = NULL;
158b4b1c516SEd Schouten reserved_teken_stat.ts_busy = 0;
159b4b1c516SEd Schouten }
160b4b1c516SEd Schouten --sc_term_scteken.te_refcount;
161b4b1c516SEd Schouten
162b4b1c516SEd Schouten return (0);
163b4b1c516SEd Schouten }
164b4b1c516SEd Schouten
165b4b1c516SEd Schouten static void
scteken_puts(scr_stat * scp,u_char * buf,int len)166d91400bfSBruce Evans scteken_puts(scr_stat *scp, u_char *buf, int len)
167b4b1c516SEd Schouten {
168b4b1c516SEd Schouten teken_stat *ts = scp->ts;
169b4b1c516SEd Schouten
170b4b1c516SEd Schouten scp->sc->write_in_progress++;
171b4b1c516SEd Schouten teken_input(&ts->ts_teken, buf, len);
172b4b1c516SEd Schouten scp->sc->write_in_progress--;
173b4b1c516SEd Schouten }
174b4b1c516SEd Schouten
175b4b1c516SEd Schouten static int
scteken_ioctl(scr_stat * scp,struct tty * tp,u_long cmd,caddr_t data,struct thread * td)176b4b1c516SEd Schouten scteken_ioctl(scr_stat *scp, struct tty *tp, u_long cmd, caddr_t data,
177b4b1c516SEd Schouten struct thread *td)
178b4b1c516SEd Schouten {
1793b31c196SEd Schouten teken_stat *ts = scp->ts;
180b4b1c516SEd Schouten vid_info_t *vi;
181eeb7d30eSBruce Evans int attr;
182b4b1c516SEd Schouten
183b4b1c516SEd Schouten switch (cmd) {
184b4b1c516SEd Schouten case GIO_ATTR: /* get current attributes */
1853b31c196SEd Schouten *(int*)data =
186eeb7d30eSBruce Evans scteken_te_to_sc_attr(teken_get_curattr(&ts->ts_teken));
187b4b1c516SEd Schouten return (0);
188b4b1c516SEd Schouten case CONS_GETINFO: /* get current (virtual) console info */
189b4b1c516SEd Schouten vi = (vid_info_t *)data;
190b4b1c516SEd Schouten if (vi->size != sizeof(struct vid_info))
191b4b1c516SEd Schouten return EINVAL;
1923b31c196SEd Schouten
193eeb7d30eSBruce Evans attr = scteken_te_to_sc_attr(teken_get_defattr(&ts->ts_teken));
1943b31c196SEd Schouten vi->mv_norm.fore = attr & 0x0f;
1953b31c196SEd Schouten vi->mv_norm.back = (attr >> 4) & 0x0f;
1963b31c196SEd Schouten vi->mv_rev.fore = vi->mv_norm.back;
1973b31c196SEd Schouten vi->mv_rev.back = vi->mv_norm.fore;
198b4b1c516SEd Schouten /*
199b4b1c516SEd Schouten * The other fields are filled by the upper routine. XXX
200b4b1c516SEd Schouten */
201b4b1c516SEd Schouten return (ENOIOCTL);
202b4b1c516SEd Schouten }
203b4b1c516SEd Schouten
204b4b1c516SEd Schouten return (ENOIOCTL);
205b4b1c516SEd Schouten }
206b4b1c516SEd Schouten
207b4b1c516SEd Schouten static void
scteken_default_attr(scr_stat * scp,int color,int rev_color)208b4b1c516SEd Schouten scteken_default_attr(scr_stat *scp, int color, int rev_color)
209b4b1c516SEd Schouten {
210b4b1c516SEd Schouten teken_stat *ts = scp->ts;
211b4b1c516SEd Schouten teken_attr_t ta;
212b4b1c516SEd Schouten
213eeb7d30eSBruce Evans scteken_sc_to_te_attr(color, &ta);
214b4b1c516SEd Schouten teken_set_defattr(&ts->ts_teken, &ta);
215b4b1c516SEd Schouten }
216b4b1c516SEd Schouten
217b4b1c516SEd Schouten static void
scteken_clear(scr_stat * scp)218b4b1c516SEd Schouten scteken_clear(scr_stat *scp)
219b4b1c516SEd Schouten {
220b8188f52SBruce Evans teken_stat *ts = scp->ts;
221b4b1c516SEd Schouten
222b4b1c516SEd Schouten sc_move_cursor(scp, 0, 0);
223af032a9dSBruce Evans scteken_sync_internal(scp, ts);
224b8188f52SBruce Evans sc_vtb_clear(&scp->vtb, scp->sc->scr_map[0x20],
225b8188f52SBruce Evans scteken_te_to_sc_attr(teken_get_curattr(&ts->ts_teken))
226b8188f52SBruce Evans << 8);
227b4b1c516SEd Schouten mark_all(scp);
228b4b1c516SEd Schouten }
229b4b1c516SEd Schouten
230b4b1c516SEd Schouten static int
scteken_input(scr_stat * scp,int c,struct tty * tp)231b4b1c516SEd Schouten scteken_input(scr_stat *scp, int c, struct tty *tp)
232b4b1c516SEd Schouten {
233b4b1c516SEd Schouten
234b4b1c516SEd Schouten return FALSE;
235b4b1c516SEd Schouten }
236b4b1c516SEd Schouten
2373a8a07eaSEd Schouten static const char *
scteken_fkeystr(scr_stat * scp,int c)2383a8a07eaSEd Schouten scteken_fkeystr(scr_stat *scp, int c)
2393a8a07eaSEd Schouten {
2403a8a07eaSEd Schouten teken_stat *ts = scp->ts;
2413a8a07eaSEd Schouten unsigned int k;
2423a8a07eaSEd Schouten
2433a8a07eaSEd Schouten switch (c) {
2443a8a07eaSEd Schouten case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
2453a8a07eaSEd Schouten case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
2463a8a07eaSEd Schouten case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
2473a8a07eaSEd Schouten case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
2483a8a07eaSEd Schouten k = TKEY_F1 + c - (FKEY | F(1));
2493a8a07eaSEd Schouten break;
2503a8a07eaSEd Schouten case FKEY | F(49):
2513a8a07eaSEd Schouten k = TKEY_HOME;
2523a8a07eaSEd Schouten break;
2533a8a07eaSEd Schouten case FKEY | F(50):
2543a8a07eaSEd Schouten k = TKEY_UP;
2553a8a07eaSEd Schouten break;
2563a8a07eaSEd Schouten case FKEY | F(51):
2573a8a07eaSEd Schouten k = TKEY_PAGE_UP;
2583a8a07eaSEd Schouten break;
2593a8a07eaSEd Schouten case FKEY | F(53):
2603a8a07eaSEd Schouten k = TKEY_LEFT;
2613a8a07eaSEd Schouten break;
2623a8a07eaSEd Schouten case FKEY | F(55):
2633a8a07eaSEd Schouten k = TKEY_RIGHT;
2643a8a07eaSEd Schouten break;
2653a8a07eaSEd Schouten case FKEY | F(57):
2663a8a07eaSEd Schouten k = TKEY_END;
2673a8a07eaSEd Schouten break;
2683a8a07eaSEd Schouten case FKEY | F(58):
2693a8a07eaSEd Schouten k = TKEY_DOWN;
2703a8a07eaSEd Schouten break;
2713a8a07eaSEd Schouten case FKEY | F(59):
2723a8a07eaSEd Schouten k = TKEY_PAGE_DOWN;
2733a8a07eaSEd Schouten break;
2743a8a07eaSEd Schouten case FKEY | F(60):
2753a8a07eaSEd Schouten k = TKEY_INSERT;
2763a8a07eaSEd Schouten break;
2773a8a07eaSEd Schouten case FKEY | F(61):
2783a8a07eaSEd Schouten k = TKEY_DELETE;
2793a8a07eaSEd Schouten break;
2803a8a07eaSEd Schouten default:
2813a8a07eaSEd Schouten return (NULL);
2823a8a07eaSEd Schouten }
2833a8a07eaSEd Schouten
2843a8a07eaSEd Schouten return (teken_get_sequence(&ts->ts_teken, k));
2853a8a07eaSEd Schouten }
2863a8a07eaSEd Schouten
287b4b1c516SEd Schouten static void
scteken_sync_internal(scr_stat * scp,teken_stat * ts)288af032a9dSBruce Evans scteken_sync_internal(scr_stat *scp, teken_stat *ts)
289ad530aa9SBruce Evans {
290ad530aa9SBruce Evans teken_pos_t tp;
291ad530aa9SBruce Evans
292912da699SBruce Evans tp.tp_col = scp->xsize;
293912da699SBruce Evans tp.tp_row = scp->ysize;
294912da699SBruce Evans teken_set_winsize_noreset(&ts->ts_teken, &tp);
295912da699SBruce Evans tp.tp_col = scp->xpos;
296912da699SBruce Evans tp.tp_row = scp->ypos;
297ad530aa9SBruce Evans teken_set_cursor(&ts->ts_teken, &tp);
298ad530aa9SBruce Evans }
299ad530aa9SBruce Evans
300ad530aa9SBruce Evans static void
scteken_sync(scr_stat * scp)301af032a9dSBruce Evans scteken_sync(scr_stat *scp)
302af032a9dSBruce Evans {
303af032a9dSBruce Evans scteken_sync_internal(scp, scp->ts);
304af032a9dSBruce Evans }
305af032a9dSBruce Evans
306af032a9dSBruce Evans static void
scteken_nop(void)307b4b1c516SEd Schouten scteken_nop(void)
308b4b1c516SEd Schouten {
309b4b1c516SEd Schouten
310b4b1c516SEd Schouten }
311b4b1c516SEd Schouten
312b4b1c516SEd Schouten /*
313b4b1c516SEd Schouten * libteken routines.
314b4b1c516SEd Schouten */
315b4b1c516SEd Schouten
3164eb235fbSBruce Evans static const teken_color_t sc_to_te_color[] = {
3174eb235fbSBruce Evans TC_BLACK, TC_BLUE, TC_GREEN, TC_CYAN,
318cf8880d5SEd Maste TC_RED, TC_MAGENTA, TC_YELLOW, TC_WHITE,
3194eb235fbSBruce Evans };
3204eb235fbSBruce Evans
3214eb235fbSBruce Evans static const unsigned char te_to_sc_color[] = {
322b4b1c516SEd Schouten FG_BLACK, FG_RED, FG_GREEN, FG_BROWN,
323b4b1c516SEd Schouten FG_BLUE, FG_MAGENTA, FG_CYAN, FG_LIGHTGREY,
324b4b1c516SEd Schouten };
325b4b1c516SEd Schouten
326b4b1c516SEd Schouten static void
scteken_sc_to_te_attr(unsigned char color,teken_attr_t * a)327eeb7d30eSBruce Evans scteken_sc_to_te_attr(unsigned char color, teken_attr_t *a)
328b4b1c516SEd Schouten {
329b4b1c516SEd Schouten
330b4b1c516SEd Schouten /*
3314eb235fbSBruce Evans * Conversions of attrs are not reversible. Since sc attrs are
3324eb235fbSBruce Evans * pure colors in the simplest mode (16-color graphics) and the
3334eb235fbSBruce Evans * API is too deficient to tell us the mode, always convert to
3344eb235fbSBruce Evans * pure colors. The conversion is essentially the identity except
3354eb235fbSBruce Evans * for reordering the non-brightness bits in the 2 color numbers.
336b4b1c516SEd Schouten */
337b4b1c516SEd Schouten a->ta_format = 0;
3384eb235fbSBruce Evans a->ta_fgcolor = sc_to_te_color[color & 7] | (color & 8);
3394eb235fbSBruce Evans a->ta_bgcolor = sc_to_te_color[(color >> 4) & 7] | ((color >> 4) & 8);
340b4b1c516SEd Schouten }
341b4b1c516SEd Schouten
342eeb7d30eSBruce Evans static int
scteken_te_to_sc_attr(const teken_attr_t * a)343eeb7d30eSBruce Evans scteken_te_to_sc_attr(const teken_attr_t *a)
344b4b1c516SEd Schouten {
3454eb235fbSBruce Evans int attr;
3464a6ecf07SEd Schouten teken_color_t fg, bg;
347b4b1c516SEd Schouten
3484a6ecf07SEd Schouten if (a->ta_format & TF_REVERSE) {
3494eb235fbSBruce Evans fg = a->ta_bgcolor;
3504eb235fbSBruce Evans bg = a->ta_fgcolor;
3514a6ecf07SEd Schouten } else {
3524eb235fbSBruce Evans fg = a->ta_fgcolor;
3534eb235fbSBruce Evans bg = a->ta_bgcolor;
3544a6ecf07SEd Schouten }
3554eb235fbSBruce Evans if (fg >= 16)
3564eb235fbSBruce Evans fg = teken_256to16(fg);
3574eb235fbSBruce Evans if (bg >= 16)
3584eb235fbSBruce Evans bg = teken_256to16(bg);
3594eb235fbSBruce Evans attr = te_to_sc_color[fg & 7] | (fg & 8) |
3604eb235fbSBruce Evans ((te_to_sc_color[bg & 7] | (bg & 8)) << 4);
361b4b1c516SEd Schouten
3624eb235fbSBruce Evans /* XXX: underline mapping for Hercules adapter can be better. */
3634eb235fbSBruce Evans if (a->ta_format & (TF_BOLD | TF_UNDERLINE))
3644eb235fbSBruce Evans attr ^= 8;
365b4b1c516SEd Schouten if (a->ta_format & TF_BLINK)
3664eb235fbSBruce Evans attr ^= 0x80;
367b4b1c516SEd Schouten
3683b31c196SEd Schouten return (attr);
369b4b1c516SEd Schouten }
370b4b1c516SEd Schouten
371b4b1c516SEd Schouten static void
scteken_bell(void * arg)372b4b1c516SEd Schouten scteken_bell(void *arg)
373b4b1c516SEd Schouten {
374b4b1c516SEd Schouten scr_stat *scp = arg;
375b4b1c516SEd Schouten
376b4b1c516SEd Schouten sc_bell(scp, scp->bell_pitch, scp->bell_duration);
377b4b1c516SEd Schouten }
378b4b1c516SEd Schouten
379b4b1c516SEd Schouten static void
scteken_cursor(void * arg,const teken_pos_t * p)380b4b1c516SEd Schouten scteken_cursor(void *arg, const teken_pos_t *p)
381b4b1c516SEd Schouten {
382b4b1c516SEd Schouten scr_stat *scp = arg;
383b4b1c516SEd Schouten
384b4b1c516SEd Schouten sc_move_cursor(scp, p->tp_col, p->tp_row);
385b4b1c516SEd Schouten }
386b4b1c516SEd Schouten
387b32dcb66SEd Schouten #ifdef TEKEN_UTF8
388b32dcb66SEd Schouten struct unicp437 {
389b32dcb66SEd Schouten uint16_t unicode_base;
390b32dcb66SEd Schouten uint8_t cp437_base;
391b32dcb66SEd Schouten uint8_t length;
392b32dcb66SEd Schouten };
393b32dcb66SEd Schouten
394b32dcb66SEd Schouten static const struct unicp437 cp437table[] = {
395706b8a10SEd Schouten { 0x0020, 0x20, 0x5e }, { 0x00a0, 0x20, 0x00 },
396706b8a10SEd Schouten { 0x00a1, 0xad, 0x00 }, { 0x00a2, 0x9b, 0x00 },
397706b8a10SEd Schouten { 0x00a3, 0x9c, 0x00 }, { 0x00a5, 0x9d, 0x00 },
398706b8a10SEd Schouten { 0x00a7, 0x15, 0x00 }, { 0x00aa, 0xa6, 0x00 },
399706b8a10SEd Schouten { 0x00ab, 0xae, 0x00 }, { 0x00ac, 0xaa, 0x00 },
400706b8a10SEd Schouten { 0x00b0, 0xf8, 0x00 }, { 0x00b1, 0xf1, 0x00 },
401706b8a10SEd Schouten { 0x00b2, 0xfd, 0x00 }, { 0x00b5, 0xe6, 0x00 },
402706b8a10SEd Schouten { 0x00b6, 0x14, 0x00 }, { 0x00b7, 0xfa, 0x00 },
403706b8a10SEd Schouten { 0x00ba, 0xa7, 0x00 }, { 0x00bb, 0xaf, 0x00 },
404706b8a10SEd Schouten { 0x00bc, 0xac, 0x00 }, { 0x00bd, 0xab, 0x00 },
405a2c22d41SRobert Millan { 0x00bf, 0xa8, 0x00 }, { 0x00c0, 0x41, 0x00 },
406a2c22d41SRobert Millan { 0x00c1, 0x41, 0x00 }, { 0x00c2, 0x41, 0x00 },
407a2c22d41SRobert Millan { 0x00c4, 0x8e, 0x01 }, { 0x00c6, 0x92, 0x00 },
408a2c22d41SRobert Millan { 0x00c7, 0x80, 0x00 }, { 0x00c8, 0x45, 0x00 },
409a2c22d41SRobert Millan { 0x00c9, 0x90, 0x00 }, { 0x00ca, 0x45, 0x00 },
410a2c22d41SRobert Millan { 0x00cb, 0x45, 0x00 }, { 0x00cc, 0x49, 0x00 },
411a2c22d41SRobert Millan { 0x00cd, 0x49, 0x00 }, { 0x00ce, 0x49, 0x00 },
412a2c22d41SRobert Millan { 0x00cf, 0x49, 0x00 }, { 0x00d1, 0xa5, 0x00 },
413a2c22d41SRobert Millan { 0x00d2, 0x4f, 0x00 }, { 0x00d3, 0x4f, 0x00 },
414a2c22d41SRobert Millan { 0x00d4, 0x4f, 0x00 }, { 0x00d6, 0x99, 0x00 },
415a2c22d41SRobert Millan { 0x00d9, 0x55, 0x00 }, { 0x00da, 0x55, 0x00 },
416a2c22d41SRobert Millan { 0x00db, 0x55, 0x00 }, { 0x00dc, 0x9a, 0x00 },
417706b8a10SEd Schouten { 0x00df, 0xe1, 0x00 }, { 0x00e0, 0x85, 0x00 },
418706b8a10SEd Schouten { 0x00e1, 0xa0, 0x00 }, { 0x00e2, 0x83, 0x00 },
419706b8a10SEd Schouten { 0x00e4, 0x84, 0x00 }, { 0x00e5, 0x86, 0x00 },
420706b8a10SEd Schouten { 0x00e6, 0x91, 0x00 }, { 0x00e7, 0x87, 0x00 },
421706b8a10SEd Schouten { 0x00e8, 0x8a, 0x00 }, { 0x00e9, 0x82, 0x00 },
422706b8a10SEd Schouten { 0x00ea, 0x88, 0x01 }, { 0x00ec, 0x8d, 0x00 },
423706b8a10SEd Schouten { 0x00ed, 0xa1, 0x00 }, { 0x00ee, 0x8c, 0x00 },
424706b8a10SEd Schouten { 0x00ef, 0x8b, 0x00 }, { 0x00f0, 0xeb, 0x00 },
425b32dcb66SEd Schouten { 0x00f1, 0xa4, 0x00 }, { 0x00f2, 0x95, 0x00 },
426b32dcb66SEd Schouten { 0x00f3, 0xa2, 0x00 }, { 0x00f4, 0x93, 0x00 },
427b32dcb66SEd Schouten { 0x00f6, 0x94, 0x00 }, { 0x00f7, 0xf6, 0x00 },
428706b8a10SEd Schouten { 0x00f8, 0xed, 0x00 }, { 0x00f9, 0x97, 0x00 },
429706b8a10SEd Schouten { 0x00fa, 0xa3, 0x00 }, { 0x00fb, 0x96, 0x00 },
430706b8a10SEd Schouten { 0x00fc, 0x81, 0x00 }, { 0x00ff, 0x98, 0x00 },
431a2c22d41SRobert Millan { 0x013f, 0x4c, 0x00 }, { 0x0140, 0x6c, 0x00 },
432706b8a10SEd Schouten { 0x0192, 0x9f, 0x00 }, { 0x0393, 0xe2, 0x00 },
433706b8a10SEd Schouten { 0x0398, 0xe9, 0x00 }, { 0x03a3, 0xe4, 0x00 },
434706b8a10SEd Schouten { 0x03a6, 0xe8, 0x00 }, { 0x03a9, 0xea, 0x00 },
435706b8a10SEd Schouten { 0x03b1, 0xe0, 0x01 }, { 0x03b4, 0xeb, 0x00 },
436706b8a10SEd Schouten { 0x03b5, 0xee, 0x00 }, { 0x03bc, 0xe6, 0x00 },
437b32dcb66SEd Schouten { 0x03c0, 0xe3, 0x00 }, { 0x03c3, 0xe5, 0x00 },
438b32dcb66SEd Schouten { 0x03c4, 0xe7, 0x00 }, { 0x03c6, 0xed, 0x00 },
439706b8a10SEd Schouten { 0x03d5, 0xed, 0x00 }, { 0x2010, 0x2d, 0x00 },
440706b8a10SEd Schouten { 0x2014, 0x2d, 0x00 }, { 0x2018, 0x60, 0x00 },
441706b8a10SEd Schouten { 0x2019, 0x27, 0x00 }, { 0x201c, 0x22, 0x00 },
442706b8a10SEd Schouten { 0x201d, 0x22, 0x00 }, { 0x2022, 0x07, 0x00 },
443706b8a10SEd Schouten { 0x203c, 0x13, 0x00 }, { 0x207f, 0xfc, 0x00 },
444706b8a10SEd Schouten { 0x20a7, 0x9e, 0x00 }, { 0x20ac, 0xee, 0x00 },
445706b8a10SEd Schouten { 0x2126, 0xea, 0x00 }, { 0x2190, 0x1b, 0x00 },
446706b8a10SEd Schouten { 0x2191, 0x18, 0x00 }, { 0x2192, 0x1a, 0x00 },
447706b8a10SEd Schouten { 0x2193, 0x19, 0x00 }, { 0x2194, 0x1d, 0x00 },
448706b8a10SEd Schouten { 0x2195, 0x12, 0x00 }, { 0x21a8, 0x17, 0x00 },
449706b8a10SEd Schouten { 0x2202, 0xeb, 0x00 }, { 0x2208, 0xee, 0x00 },
450706b8a10SEd Schouten { 0x2211, 0xe4, 0x00 }, { 0x2212, 0x2d, 0x00 },
451b32dcb66SEd Schouten { 0x2219, 0xf9, 0x00 }, { 0x221a, 0xfb, 0x00 },
452b32dcb66SEd Schouten { 0x221e, 0xec, 0x00 }, { 0x221f, 0x1c, 0x00 },
453b32dcb66SEd Schouten { 0x2229, 0xef, 0x00 }, { 0x2248, 0xf7, 0x00 },
454b32dcb66SEd Schouten { 0x2261, 0xf0, 0x00 }, { 0x2264, 0xf3, 0x00 },
455b32dcb66SEd Schouten { 0x2265, 0xf2, 0x00 }, { 0x2302, 0x7f, 0x00 },
456b32dcb66SEd Schouten { 0x2310, 0xa9, 0x00 }, { 0x2320, 0xf4, 0x00 },
457b32dcb66SEd Schouten { 0x2321, 0xf5, 0x00 }, { 0x2500, 0xc4, 0x00 },
458b32dcb66SEd Schouten { 0x2502, 0xb3, 0x00 }, { 0x250c, 0xda, 0x00 },
459b32dcb66SEd Schouten { 0x2510, 0xbf, 0x00 }, { 0x2514, 0xc0, 0x00 },
460b32dcb66SEd Schouten { 0x2518, 0xd9, 0x00 }, { 0x251c, 0xc3, 0x00 },
461b32dcb66SEd Schouten { 0x2524, 0xb4, 0x00 }, { 0x252c, 0xc2, 0x00 },
462b32dcb66SEd Schouten { 0x2534, 0xc1, 0x00 }, { 0x253c, 0xc5, 0x00 },
463b32dcb66SEd Schouten { 0x2550, 0xcd, 0x00 }, { 0x2551, 0xba, 0x00 },
464b32dcb66SEd Schouten { 0x2552, 0xd5, 0x00 }, { 0x2553, 0xd6, 0x00 },
465b32dcb66SEd Schouten { 0x2554, 0xc9, 0x00 }, { 0x2555, 0xb8, 0x00 },
466b32dcb66SEd Schouten { 0x2556, 0xb7, 0x00 }, { 0x2557, 0xbb, 0x00 },
467b32dcb66SEd Schouten { 0x2558, 0xd4, 0x00 }, { 0x2559, 0xd3, 0x00 },
468b32dcb66SEd Schouten { 0x255a, 0xc8, 0x00 }, { 0x255b, 0xbe, 0x00 },
469b32dcb66SEd Schouten { 0x255c, 0xbd, 0x00 }, { 0x255d, 0xbc, 0x00 },
470b32dcb66SEd Schouten { 0x255e, 0xc6, 0x01 }, { 0x2560, 0xcc, 0x00 },
471b32dcb66SEd Schouten { 0x2561, 0xb5, 0x00 }, { 0x2562, 0xb6, 0x00 },
472b32dcb66SEd Schouten { 0x2563, 0xb9, 0x00 }, { 0x2564, 0xd1, 0x01 },
473b32dcb66SEd Schouten { 0x2566, 0xcb, 0x00 }, { 0x2567, 0xcf, 0x00 },
474b32dcb66SEd Schouten { 0x2568, 0xd0, 0x00 }, { 0x2569, 0xca, 0x00 },
475b32dcb66SEd Schouten { 0x256a, 0xd8, 0x00 }, { 0x256b, 0xd7, 0x00 },
476b32dcb66SEd Schouten { 0x256c, 0xce, 0x00 }, { 0x2580, 0xdf, 0x00 },
477b32dcb66SEd Schouten { 0x2584, 0xdc, 0x00 }, { 0x2588, 0xdb, 0x00 },
478b32dcb66SEd Schouten { 0x258c, 0xdd, 0x00 }, { 0x2590, 0xde, 0x00 },
479b32dcb66SEd Schouten { 0x2591, 0xb0, 0x02 }, { 0x25a0, 0xfe, 0x00 },
480a2c22d41SRobert Millan { 0x25ac, 0x16, 0x00 },
481a2c22d41SRobert Millan { 0x25ae, 0xdb, 0x00 }, { 0x25b2, 0x1e, 0x00 },
482b32dcb66SEd Schouten { 0x25ba, 0x10, 0x00 }, { 0x25bc, 0x1f, 0x00 },
483324f7abbSEd Schouten { 0x25c4, 0x11, 0x00 }, { 0x25cb, 0x09, 0x00 },
484324f7abbSEd Schouten { 0x25d8, 0x08, 0x00 }, { 0x25d9, 0x0a, 0x00 },
485324f7abbSEd Schouten { 0x263a, 0x01, 0x01 }, { 0x263c, 0x0f, 0x00 },
486324f7abbSEd Schouten { 0x2640, 0x0c, 0x00 }, { 0x2642, 0x0b, 0x00 },
487324f7abbSEd Schouten { 0x2660, 0x06, 0x00 }, { 0x2663, 0x05, 0x00 },
488324f7abbSEd Schouten { 0x2665, 0x03, 0x01 }, { 0x266a, 0x0d, 0x01 },
489b32dcb66SEd Schouten };
490b32dcb66SEd Schouten
491b32dcb66SEd Schouten static void
scteken_get_cp437(teken_char_t * c,int * attr)492b32dcb66SEd Schouten scteken_get_cp437(teken_char_t *c, int *attr)
493b32dcb66SEd Schouten {
494b32dcb66SEd Schouten int min, mid, max;
495b32dcb66SEd Schouten
496b32dcb66SEd Schouten min = 0;
497b32dcb66SEd Schouten max = (sizeof(cp437table) / sizeof(struct unicp437)) - 1;
498b32dcb66SEd Schouten
499b32dcb66SEd Schouten if (*c < cp437table[0].unicode_base ||
500b32dcb66SEd Schouten *c > cp437table[max].unicode_base + cp437table[max].length)
501b32dcb66SEd Schouten goto bad;
502b32dcb66SEd Schouten
503b32dcb66SEd Schouten while (max >= min) {
504b32dcb66SEd Schouten mid = (min + max) / 2;
505b32dcb66SEd Schouten if (*c < cp437table[mid].unicode_base) {
506b32dcb66SEd Schouten max = mid - 1;
507b32dcb66SEd Schouten } else if (*c > cp437table[mid].unicode_base +
508b32dcb66SEd Schouten cp437table[mid].length) {
509b32dcb66SEd Schouten min = mid + 1;
510b32dcb66SEd Schouten } else {
511b32dcb66SEd Schouten *c -= cp437table[mid].unicode_base;
512b32dcb66SEd Schouten *c += cp437table[mid].cp437_base;
513b32dcb66SEd Schouten return;
514b32dcb66SEd Schouten }
515b32dcb66SEd Schouten }
516b32dcb66SEd Schouten bad:
517b32dcb66SEd Schouten /* Character not present in CP437. */
518b32dcb66SEd Schouten *attr = (FG_RED|BG_BLACK) << 8;
519b32dcb66SEd Schouten *c = '?';
520b32dcb66SEd Schouten }
521b32dcb66SEd Schouten #endif /* TEKEN_UTF8 */
522b32dcb66SEd Schouten
523b4b1c516SEd Schouten static void
scteken_putchar(void * arg,const teken_pos_t * tp,teken_char_t c,const teken_attr_t * a)524b4b1c516SEd Schouten scteken_putchar(void *arg, const teken_pos_t *tp, teken_char_t c,
525b4b1c516SEd Schouten const teken_attr_t *a)
526b4b1c516SEd Schouten {
527b4b1c516SEd Schouten scr_stat *scp = arg;
528b4b1c516SEd Schouten u_char *map;
529b4b1c516SEd Schouten u_char ch;
530b4b1c516SEd Schouten vm_offset_t p;
531b4b1c516SEd Schouten int cursor, attr;
532b4b1c516SEd Schouten
533a6c26592SEd Schouten /*
534a6c26592SEd Schouten * No support for printing right hand sides for CJK fullwidth
535a6c26592SEd Schouten * characters. Simply print a space and assume that the left
536a6c26592SEd Schouten * hand side describes the entire character.
537a6c26592SEd Schouten */
538eeb7d30eSBruce Evans attr = scteken_te_to_sc_attr(a) << 8;
539a6c26592SEd Schouten if (a->ta_format & TF_CJK_RIGHT)
540a6c26592SEd Schouten c = ' ';
541b32dcb66SEd Schouten #ifdef TEKEN_UTF8
542b32dcb66SEd Schouten scteken_get_cp437(&c, &attr);
543b32dcb66SEd Schouten #endif /* TEKEN_UTF8 */
544b4b1c516SEd Schouten ch = c;
545b4b1c516SEd Schouten
546b4b1c516SEd Schouten map = scp->sc->scr_map;
547b4b1c516SEd Schouten
548b4b1c516SEd Schouten cursor = tp->tp_row * scp->xsize + tp->tp_col;
549b4b1c516SEd Schouten p = sc_vtb_pointer(&scp->vtb, cursor);
550b4b1c516SEd Schouten sc_vtb_putchar(&scp->vtb, p, map[ch], attr);
551b4b1c516SEd Schouten
552b4b1c516SEd Schouten mark_for_update(scp, cursor);
553b4b1c516SEd Schouten /*
554b4b1c516SEd Schouten * XXX: Why do we need this? Only marking `cursor' should be
555b4b1c516SEd Schouten * enough. Without this line, we get artifacts.
556b4b1c516SEd Schouten */
557b4b1c516SEd Schouten mark_for_update(scp, imin(cursor + 1, scp->xsize * scp->ysize - 1));
558b4b1c516SEd Schouten }
559b4b1c516SEd Schouten
560b4b1c516SEd Schouten static void
scteken_fill(void * arg,const teken_rect_t * r,teken_char_t c,const teken_attr_t * a)561b4b1c516SEd Schouten scteken_fill(void *arg, const teken_rect_t *r, teken_char_t c,
562b4b1c516SEd Schouten const teken_attr_t *a)
563b4b1c516SEd Schouten {
564b4b1c516SEd Schouten scr_stat *scp = arg;
565b4b1c516SEd Schouten u_char *map;
566b4b1c516SEd Schouten u_char ch;
567b4b1c516SEd Schouten unsigned int width;
568b4b1c516SEd Schouten int attr, row;
569b4b1c516SEd Schouten
570eeb7d30eSBruce Evans attr = scteken_te_to_sc_attr(a) << 8;
571b32dcb66SEd Schouten #ifdef TEKEN_UTF8
572b32dcb66SEd Schouten scteken_get_cp437(&c, &attr);
573b32dcb66SEd Schouten #endif /* TEKEN_UTF8 */
574b4b1c516SEd Schouten ch = c;
575b4b1c516SEd Schouten
576b4b1c516SEd Schouten map = scp->sc->scr_map;
577b4b1c516SEd Schouten
578b4b1c516SEd Schouten if (r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize) {
579b4b1c516SEd Schouten /* Single contiguous region to fill. */
580b4b1c516SEd Schouten sc_vtb_erase(&scp->vtb, r->tr_begin.tp_row * scp->xsize,
581b4b1c516SEd Schouten (r->tr_end.tp_row - r->tr_begin.tp_row) * scp->xsize,
582b4b1c516SEd Schouten map[ch], attr);
583b4b1c516SEd Schouten } else {
584b4b1c516SEd Schouten /* Fill display line by line. */
585b4b1c516SEd Schouten width = r->tr_end.tp_col - r->tr_begin.tp_col;
586b4b1c516SEd Schouten
587b4b1c516SEd Schouten for (row = r->tr_begin.tp_row; row < r->tr_end.tp_row; row++) {
588b4b1c516SEd Schouten sc_vtb_erase(&scp->vtb, r->tr_begin.tp_row *
589b4b1c516SEd Schouten scp->xsize + r->tr_begin.tp_col,
590b4b1c516SEd Schouten width, map[ch], attr);
591b4b1c516SEd Schouten }
592b4b1c516SEd Schouten }
593b4b1c516SEd Schouten
594b4b1c516SEd Schouten /* Mark begin and end positions to be refreshed. */
595b4b1c516SEd Schouten mark_for_update(scp,
596b4b1c516SEd Schouten r->tr_begin.tp_row * scp->xsize + r->tr_begin.tp_col);
597b4b1c516SEd Schouten mark_for_update(scp,
598b4b1c516SEd Schouten (r->tr_end.tp_row - 1) * scp->xsize + (r->tr_end.tp_col - 1));
599b4b1c516SEd Schouten sc_remove_cutmarking(scp);
600b4b1c516SEd Schouten }
601b4b1c516SEd Schouten
602b4b1c516SEd Schouten static void
scteken_copy(void * arg,const teken_rect_t * r,const teken_pos_t * p)603b4b1c516SEd Schouten scteken_copy(void *arg, const teken_rect_t *r, const teken_pos_t *p)
604b4b1c516SEd Schouten {
605b4b1c516SEd Schouten scr_stat *scp = arg;
606b4b1c516SEd Schouten unsigned int width;
607b4b1c516SEd Schouten int src, dst, end;
608b4b1c516SEd Schouten
609b4b1c516SEd Schouten #ifndef SC_NO_HISTORY
610b4b1c516SEd Schouten /*
611b4b1c516SEd Schouten * We count a line of input as history if we perform a copy of
612b4b1c516SEd Schouten * one whole line upward. In other words: if a line of text gets
613b4b1c516SEd Schouten * overwritten by a rectangle that's right below it.
614b4b1c516SEd Schouten */
615b4b1c516SEd Schouten if (scp->history != NULL &&
616b4b1c516SEd Schouten r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize &&
617b4b1c516SEd Schouten r->tr_begin.tp_row == p->tp_row + 1) {
618b4b1c516SEd Schouten sc_hist_save_one_line(scp, p->tp_row);
619b4b1c516SEd Schouten }
620b4b1c516SEd Schouten #endif
621b4b1c516SEd Schouten
622b4b1c516SEd Schouten if (r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize) {
623b4b1c516SEd Schouten /* Single contiguous region to copy. */
624b4b1c516SEd Schouten sc_vtb_move(&scp->vtb, r->tr_begin.tp_row * scp->xsize,
625b4b1c516SEd Schouten p->tp_row * scp->xsize,
626b4b1c516SEd Schouten (r->tr_end.tp_row - r->tr_begin.tp_row) * scp->xsize);
627b4b1c516SEd Schouten } else {
628b4b1c516SEd Schouten /* Copy line by line. */
629b4b1c516SEd Schouten width = r->tr_end.tp_col - r->tr_begin.tp_col;
630b4b1c516SEd Schouten
631b4b1c516SEd Schouten if (p->tp_row < r->tr_begin.tp_row) {
632b4b1c516SEd Schouten /* Copy from top to bottom. */
633b4b1c516SEd Schouten src = r->tr_begin.tp_row * scp->xsize +
634b4b1c516SEd Schouten r->tr_begin.tp_col;
635b4b1c516SEd Schouten end = r->tr_end.tp_row * scp->xsize +
636b4b1c516SEd Schouten r->tr_end.tp_col;
637b4b1c516SEd Schouten dst = p->tp_row * scp->xsize + p->tp_col;
638b4b1c516SEd Schouten
639b4b1c516SEd Schouten while (src < end) {
640b4b1c516SEd Schouten sc_vtb_move(&scp->vtb, src, dst, width);
641b4b1c516SEd Schouten
642b4b1c516SEd Schouten src += scp->xsize;
643b4b1c516SEd Schouten dst += scp->xsize;
644b4b1c516SEd Schouten }
645b4b1c516SEd Schouten } else {
646b4b1c516SEd Schouten /* Copy from bottom to top. */
647b4b1c516SEd Schouten src = (r->tr_end.tp_row - 1) * scp->xsize +
648b4b1c516SEd Schouten r->tr_begin.tp_col;
649b4b1c516SEd Schouten end = r->tr_begin.tp_row * scp->xsize +
650b4b1c516SEd Schouten r->tr_begin.tp_col;
651b4b1c516SEd Schouten dst = (p->tp_row + r->tr_end.tp_row -
652b4b1c516SEd Schouten r->tr_begin.tp_row - 1) * scp->xsize + p->tp_col;
653b4b1c516SEd Schouten
654b4b1c516SEd Schouten while (src >= end) {
655b4b1c516SEd Schouten sc_vtb_move(&scp->vtb, src, dst, width);
656b4b1c516SEd Schouten
657b4b1c516SEd Schouten src -= scp->xsize;
658b4b1c516SEd Schouten dst -= scp->xsize;
659b4b1c516SEd Schouten }
660b4b1c516SEd Schouten }
661b4b1c516SEd Schouten }
662b4b1c516SEd Schouten
663b4b1c516SEd Schouten /* Mark begin and end positions to be refreshed. */
664b4b1c516SEd Schouten mark_for_update(scp,
665b4b1c516SEd Schouten p->tp_row * scp->xsize + p->tp_col);
666b4b1c516SEd Schouten mark_for_update(scp,
667b4b1c516SEd Schouten (p->tp_row + r->tr_end.tp_row - r->tr_begin.tp_row - 1) *
668b4b1c516SEd Schouten scp->xsize +
669b4b1c516SEd Schouten (p->tp_col + r->tr_end.tp_col - r->tr_begin.tp_col - 1));
670b4b1c516SEd Schouten sc_remove_cutmarking(scp);
671b4b1c516SEd Schouten }
672b4b1c516SEd Schouten
673b4b1c516SEd Schouten static void
scteken_param(void * arg,int cmd,unsigned int value)674ec034df1SEd Schouten scteken_param(void *arg, int cmd, unsigned int value)
675b4b1c516SEd Schouten {
67615e0c651SBruce Evans static int cattrs[] = {
67715e0c651SBruce Evans 0, /* block */
67815e0c651SBruce Evans CONS_BLINK_CURSOR, /* blinking block */
67915e0c651SBruce Evans CONS_CHAR_CURSOR, /* underline */
68015e0c651SBruce Evans CONS_CHAR_CURSOR | CONS_BLINK_CURSOR, /* blinking underline */
68115e0c651SBruce Evans CONS_RESET_CURSOR, /* reset to default */
68215e0c651SBruce Evans CONS_HIDDEN_CURSOR, /* hide cursor */
68315e0c651SBruce Evans };
68415e0c651SBruce Evans static int tcattrs[] = {
68515e0c651SBruce Evans CONS_RESET_CURSOR | CONS_LOCAL_CURSOR, /* normal */
68615e0c651SBruce Evans CONS_HIDDEN_CURSOR | CONS_LOCAL_CURSOR, /* invisible */
68715e0c651SBruce Evans };
688b4b1c516SEd Schouten scr_stat *scp = arg;
68915e0c651SBruce Evans int flags, n, v0, v1, v2;
690b4b1c516SEd Schouten
691b4b1c516SEd Schouten switch (cmd) {
692dd833891SBruce Evans case TP_SETBORDER:
693dd833891SBruce Evans scp->border = value & 0xff;
694dd833891SBruce Evans if (scp == scp->sc->cur_scp)
695dd833891SBruce Evans sc_set_border(scp, scp->border);
696dd833891SBruce Evans break;
69715e0c651SBruce Evans case TP_SETGLOBALCURSOR:
69815e0c651SBruce Evans n = value & 0xff;
69915e0c651SBruce Evans v0 = (value >> 8) & 0xff;
70015e0c651SBruce Evans v1 = (value >> 16) & 0xff;
70115e0c651SBruce Evans v2 = (value >> 24) & 0xff;
70215e0c651SBruce Evans switch (n) {
70315e0c651SBruce Evans case 1: /* flags only */
70415e0c651SBruce Evans if (v0 < sizeof(cattrs) / sizeof(cattrs[0]))
70515e0c651SBruce Evans v0 = cattrs[v0];
70615e0c651SBruce Evans else /* backward compatibility */
70715e0c651SBruce Evans v0 = cattrs[v0 & 0x3];
70815e0c651SBruce Evans sc_change_cursor_shape(scp, v0, -1, -1);
70915e0c651SBruce Evans break;
71015e0c651SBruce Evans case 2:
71115e0c651SBruce Evans v2 = 0;
71215e0c651SBruce Evans v0 &= 0x1f; /* backward compatibility */
71315e0c651SBruce Evans v1 &= 0x1f;
71415e0c651SBruce Evans /* FALL THROUGH */
71515e0c651SBruce Evans case 3: /* base and height */
71615e0c651SBruce Evans if (v2 == 0) /* count from top */
71715e0c651SBruce Evans sc_change_cursor_shape(scp, -1,
71815e0c651SBruce Evans scp->font_size - v1 - 1,
71915e0c651SBruce Evans v1 - v0 + 1);
72015e0c651SBruce Evans else if (v2 == 1) /* count from bottom */
72115e0c651SBruce Evans sc_change_cursor_shape(scp, -1,
72215e0c651SBruce Evans v0, v1 - v0 + 1);
72315e0c651SBruce Evans break;
72415e0c651SBruce Evans }
72515e0c651SBruce Evans break;
72615e0c651SBruce Evans case TP_SETLOCALCURSOR:
72715e0c651SBruce Evans if (value < sizeof(tcattrs) / sizeof(tcattrs[0]))
72815e0c651SBruce Evans sc_change_cursor_shape(scp, tcattrs[value], -1, -1);
72936e19a0fSBruce Evans else if (value == 2) {
73036e19a0fSBruce Evans sc_change_cursor_shape(scp,
73136e19a0fSBruce Evans CONS_RESET_CURSOR | CONS_LOCAL_CURSOR, -1, -1);
73236e19a0fSBruce Evans flags = scp->base_curs_attr.flags ^ CONS_BLINK_CURSOR;
73336e19a0fSBruce Evans sc_change_cursor_shape(scp,
73436e19a0fSBruce Evans flags | CONS_LOCAL_CURSOR, -1, -1);
73536e19a0fSBruce Evans }
73615e0c651SBruce Evans break;
737b4b1c516SEd Schouten case TP_SHOWCURSOR:
738e4501d81SBruce Evans if (value != 0)
7394ea1f4f5SBruce Evans flags = scp->base_curs_attr.flags & ~CONS_HIDDEN_CURSOR;
740e4501d81SBruce Evans else
7414ea1f4f5SBruce Evans flags = scp->base_curs_attr.flags | CONS_HIDDEN_CURSOR;
742e4501d81SBruce Evans sc_change_cursor_shape(scp, flags | CONS_LOCAL_CURSOR, -1, -1);
743b4b1c516SEd Schouten break;
744b4b1c516SEd Schouten case TP_SWITCHVT:
745b4b1c516SEd Schouten sc_switch_scr(scp->sc, value);
746b4b1c516SEd Schouten break;
747ec034df1SEd Schouten case TP_SETBELLPD:
748ec034df1SEd Schouten scp->bell_pitch = TP_SETBELLPD_PITCH(value);
749ec034df1SEd Schouten scp->bell_duration = TP_SETBELLPD_DURATION(value);
750ec034df1SEd Schouten break;
75153e69c0cSEd Schouten case TP_MOUSE:
75253e69c0cSEd Schouten scp->mouse_level = value;
75353e69c0cSEd Schouten break;
754b4b1c516SEd Schouten }
755b4b1c516SEd Schouten }
756b4b1c516SEd Schouten
757b4b1c516SEd Schouten static void
scteken_respond(void * arg,const void * buf,size_t len)758b4b1c516SEd Schouten scteken_respond(void *arg, const void *buf, size_t len)
759b4b1c516SEd Schouten {
760b4b1c516SEd Schouten scr_stat *scp = arg;
761b4b1c516SEd Schouten
76253e69c0cSEd Schouten sc_respond(scp, buf, len, 0);
763b4b1c516SEd Schouten }
764