xref: /openbsd-src/sys/dev/wscons/wsdisplay.c (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1 /* $OpenBSD: wsdisplay.c,v 1.124 2015/09/08 11:13:20 deraadt Exp $ */
2 /* $NetBSD: wsdisplay.c,v 1.82 2005/02/27 00:27:52 perry Exp $ */
3 
4 /*
5  * Copyright (c) 1996, 1997 Christopher G. Demetriou.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Christopher G. Demetriou
18  *	for the NetBSD Project.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/conf.h>
36 #include <sys/device.h>
37 #include <sys/ioctl.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/syslog.h>
41 #include <sys/systm.h>
42 #include <sys/tty.h>
43 #include <sys/signalvar.h>
44 #include <sys/errno.h>
45 #include <sys/fcntl.h>
46 #include <sys/vnode.h>
47 #include <sys/timeout.h>
48 #include <sys/poll.h>
49 
50 #include <dev/wscons/wscons_features.h>
51 #include <dev/wscons/wsconsio.h>
52 #include <dev/wscons/wsdisplayvar.h>
53 #include <dev/wscons/wsksymvar.h>
54 #include <dev/wscons/wsksymdef.h>
55 #include <dev/wscons/wsemulvar.h>
56 #include <dev/wscons/wscons_callbacks.h>
57 #include <dev/cons.h>
58 
59 #include "wsdisplay.h"
60 #include "wskbd.h"
61 #include "wsmux.h"
62 
63 #if NWSKBD > 0
64 #include <dev/wscons/wseventvar.h>
65 #include <dev/wscons/wsmuxvar.h>
66 #endif
67 
68 #include "wsmoused.h"
69 
70 struct wsscreen_internal {
71 	const struct wsdisplay_emulops *emulops;
72 	void	*emulcookie;
73 
74 	const struct wsscreen_descr *scrdata;
75 
76 	const struct wsemul_ops *wsemul;
77 	void	*wsemulcookie;
78 };
79 
80 struct wsscreen {
81 	struct wsscreen_internal *scr_dconf;
82 
83 	struct tty *scr_tty;
84 	int	scr_hold_screen;		/* hold tty output */
85 
86 	int scr_flags;
87 #define SCR_OPEN 1		/* is it open? */
88 #define SCR_WAITACTIVE 2	/* someone waiting on activation */
89 #define SCR_GRAPHICS 4		/* graphics mode, no text (emulation) output */
90 #define	SCR_DUMBFB 8		/* in use as dumb fb (iff SCR_GRAPHICS) */
91 
92 #ifdef WSDISPLAY_COMPAT_USL
93 	const struct wscons_syncops *scr_syncops;
94 	void *scr_synccookie;
95 #endif
96 
97 #ifdef WSDISPLAY_COMPAT_RAWKBD
98 	int scr_rawkbd;
99 #endif
100 
101 	struct wsdisplay_softc *sc;
102 
103 #ifdef HAVE_WSMOUSED_SUPPORT
104 	/* mouse console support via wsmoused(8) */
105 	u_int mouse;		/* mouse cursor position */
106 	u_int cursor;		/* selection cursor position (if
107 				   different from mouse cursor pos) */
108 	u_int cpy_start;	/* position of the copy start mark*/
109 	u_int cpy_end;		/* position of the copy end mark */
110 	u_int orig_start;	/* position of the original sel. start*/
111 	u_int orig_end;		/* position of the original sel. end */
112 
113 	u_int mouse_flags;	/* flags, status of the mouse */
114 #define MOUSE_VISIBLE	0x01	/* flag, the mouse cursor is visible */
115 #define SEL_EXISTS	0x02	/* flag, a selection exists */
116 #define SEL_IN_PROGRESS 0x04	/* flag, a selection is in progress */
117 #define SEL_EXT_AFTER	0x08	/* flag, selection is extended after */
118 #define BLANK_TO_EOL	0x10	/* flag, there are only blanks
119 				   characters to eol */
120 #define SEL_BY_CHAR	0x20	/* flag, select character by character*/
121 #define SEL_BY_WORD	0x40	/* flag, select word by word */
122 #define SEL_BY_LINE	0x80	/* flag, select line by line */
123 
124 #define IS_MOUSE_VISIBLE(scr)	((scr)->mouse_flags & MOUSE_VISIBLE)
125 #define IS_SEL_EXISTS(scr)	((scr)->mouse_flags & SEL_EXISTS)
126 #define IS_SEL_IN_PROGRESS(scr)	((scr)->mouse_flags & SEL_IN_PROGRESS)
127 #define IS_SEL_EXT_AFTER(scr)	((scr)->mouse_flags & SEL_EXT_AFTER)
128 #define IS_BLANK_TO_EOL(scr)	((scr)->mouse_flags & BLANK_TO_EOL)
129 #define IS_SEL_BY_CHAR(scr)	((scr)->mouse_flags & SEL_BY_CHAR)
130 #define IS_SEL_BY_WORD(scr)	((scr)->mouse_flags & SEL_BY_WORD)
131 #define IS_SEL_BY_LINE(scr)	((scr)->mouse_flags & SEL_BY_LINE)
132 #endif	/* HAVE_WSMOUSED_SUPPORT */
133 };
134 
135 struct wsscreen *wsscreen_attach(struct wsdisplay_softc *, int, const char *,
136 	    const struct wsscreen_descr *, void *, int, int, long);
137 void	wsscreen_detach(struct wsscreen *);
138 int	wsdisplay_addscreen(struct wsdisplay_softc *, int, const char *,
139 	    const char *);
140 int	wsdisplay_getscreen(struct wsdisplay_softc *,
141 	    struct wsdisplay_addscreendata *);
142 void	wsdisplay_resume_device(struct device *);
143 void	wsdisplay_suspend_device(struct device *);
144 void	wsdisplay_addscreen_print(struct wsdisplay_softc *, int, int);
145 void	wsdisplay_closescreen(struct wsdisplay_softc *, struct wsscreen *);
146 int	wsdisplay_delscreen(struct wsdisplay_softc *, int, int);
147 
148 void	wsdisplay_burner_setup(struct wsdisplay_softc *, struct wsscreen *);
149 void	wsdisplay_burner(void *v);
150 
151 struct wsdisplay_softc {
152 	struct device sc_dv;
153 
154 	const struct wsdisplay_accessops *sc_accessops;
155 	void	*sc_accesscookie;
156 
157 	const struct wsscreen_list *sc_scrdata;
158 
159 	struct wsscreen *sc_scr[WSDISPLAY_MAXSCREEN];
160 	int sc_focusidx;	/* available only if sc_focus isn't null */
161 	struct wsscreen *sc_focus;
162 
163 #ifdef HAVE_BURNER_SUPPORT
164 	struct timeout sc_burner;
165 	int	sc_burnoutintvl;	/* delay before blanking */
166 	int	sc_burninintvl;		/* delay before unblanking */
167 	int	sc_burnout;		/* current sc_burner delay */
168 	int	sc_burnman;		/* nonzero if screen blanked */
169 	int	sc_burnflags;
170 #endif
171 
172 	int	sc_isconsole;
173 
174 	int sc_flags;
175 #define SC_SWITCHPENDING	0x01
176 #define	SC_PASTE_AVAIL		0x02
177 	int sc_screenwanted, sc_oldscreen; /* valid with SC_SWITCHPENDING */
178 	int sc_resumescreen; /* if set, can't switch until resume. */
179 
180 #if NWSKBD > 0
181 	struct wsevsrc *sc_input;
182 #ifdef WSDISPLAY_COMPAT_RAWKBD
183 	int sc_rawkbd;
184 #endif
185 #endif /* NWSKBD > 0 */
186 
187 #ifdef HAVE_WSMOUSED_SUPPORT
188 	char *sc_copybuffer;
189 	u_int sc_copybuffer_size;
190 #endif
191 };
192 
193 extern struct cfdriver wsdisplay_cd;
194 
195 /* Autoconfiguration definitions. */
196 int	wsdisplay_emul_match(struct device *, void *, void *);
197 void	wsdisplay_emul_attach(struct device *, struct device *, void *);
198 int	wsdisplay_emul_detach(struct device *, int);
199 
200 int	wsdisplay_activate(struct device *, int);
201 
202 struct cfdriver wsdisplay_cd = {
203 	NULL, "wsdisplay", DV_TTY
204 };
205 
206 struct cfattach wsdisplay_emul_ca = {
207 	sizeof(struct wsdisplay_softc), wsdisplay_emul_match,
208 	wsdisplay_emul_attach, wsdisplay_emul_detach, wsdisplay_activate
209 };
210 
211 void	wsdisplaystart(struct tty *);
212 int	wsdisplayparam(struct tty *, struct termios *);
213 
214 /* Internal macros, functions, and variables. */
215 #define	WSDISPLAYUNIT(dev)		(minor(dev) >> 8)
216 #define	WSDISPLAYSCREEN(dev)		(minor(dev) & 0xff)
217 #define ISWSDISPLAYCTL(dev)		(WSDISPLAYSCREEN(dev) == 255)
218 #define WSDISPLAYMINOR(unit, screen)	(((unit) << 8) | (screen))
219 
220 #define	WSSCREEN_HAS_TTY(scr)		((scr)->scr_tty != NULL)
221 
222 void	wsdisplay_common_attach(struct wsdisplay_softc *sc,
223 	    int console, int mux, const struct wsscreen_list *,
224 	    const struct wsdisplay_accessops *accessops,
225 	    void *accesscookie, u_int defaultscreens);
226 int	wsdisplay_common_detach(struct wsdisplay_softc *, int);
227 void	wsdisplay_kbdholdscr(struct wsscreen *, int);
228 
229 #ifdef WSDISPLAY_COMPAT_RAWKBD
230 int	wsdisplay_update_rawkbd(struct wsdisplay_softc *, struct wsscreen *);
231 #endif
232 
233 int	wsdisplay_console_initted;
234 struct wsdisplay_softc *wsdisplay_console_device;
235 struct wsscreen_internal wsdisplay_console_conf;
236 
237 int	wsdisplay_getc_dummy(dev_t);
238 void	wsdisplay_pollc(dev_t, int);
239 
240 int	wsdisplay_cons_pollmode;
241 void	(*wsdisplay_cons_kbd_pollc)(dev_t, int);
242 
243 struct consdev wsdisplay_cons = {
244 	NULL, NULL, wsdisplay_getc_dummy, wsdisplay_cnputc,
245 	    wsdisplay_pollc, NULL, NODEV, CN_LOWPRI
246 };
247 
248 #ifndef WSDISPLAY_DEFAULTSCREENS
249 #define WSDISPLAY_DEFAULTSCREENS	1
250 #endif
251 int	wsdisplay_defaultscreens = WSDISPLAY_DEFAULTSCREENS;
252 
253 int	wsdisplay_switch1(void *, int, int);
254 int	wsdisplay_switch2(void *, int, int);
255 int	wsdisplay_switch3(void *, int, int);
256 
257 int	wsdisplay_clearonclose;
258 
259 struct wsscreen *
260 wsscreen_attach(struct wsdisplay_softc *sc, int console, const char *emul,
261     const struct wsscreen_descr *type, void *cookie, int ccol, int crow,
262     long defattr)
263 {
264 	struct wsscreen_internal *dconf;
265 	struct wsscreen *scr;
266 
267 	scr = malloc(sizeof(*scr), M_DEVBUF, M_ZERO | M_NOWAIT);
268 	if (!scr)
269 		return (NULL);
270 
271 	if (console) {
272 		dconf = &wsdisplay_console_conf;
273 		/*
274 		 * Tell the emulation about the callback argument.
275 		 * The other stuff is already there.
276 		 */
277 		(void)(*dconf->wsemul->attach)(1, 0, 0, 0, 0, scr, 0);
278 	} else { /* not console */
279 		dconf = malloc(sizeof(*dconf), M_DEVBUF, M_NOWAIT);
280 		if (dconf == NULL)
281 			goto fail;
282 		dconf->emulops = type->textops;
283 		dconf->emulcookie = cookie;
284 		if (dconf->emulops == NULL ||
285 		    (dconf->wsemul = wsemul_pick(emul)) == NULL)
286 			goto fail;
287 		dconf->wsemulcookie = (*dconf->wsemul->attach)(0, type, cookie,
288 		    ccol, crow, scr, defattr);
289 		if (dconf->wsemulcookie == NULL)
290 			goto fail;
291 		dconf->scrdata = type;
292 	}
293 
294 	scr->scr_dconf = dconf;
295 	scr->scr_tty = ttymalloc(0);
296 	scr->sc = sc;
297 	return (scr);
298 
299 fail:
300 	if (dconf != NULL)
301 		free(dconf, M_DEVBUF, sizeof(*dconf));
302 	free(scr, M_DEVBUF, sizeof(*scr));
303 	return (NULL);
304 }
305 
306 void
307 wsscreen_detach(struct wsscreen *scr)
308 {
309 	int ccol, crow; /* XXX */
310 
311 	if (WSSCREEN_HAS_TTY(scr)) {
312 		timeout_del(&scr->scr_tty->t_rstrt_to);
313 		ttyfree(scr->scr_tty);
314 	}
315 	(*scr->scr_dconf->wsemul->detach)(scr->scr_dconf->wsemulcookie,
316 	    &ccol, &crow);
317 	free(scr->scr_dconf, M_DEVBUF, sizeof(*scr->scr_dconf));
318 	free(scr, M_DEVBUF, sizeof(*scr));
319 }
320 
321 const struct wsscreen_descr *
322 wsdisplay_screentype_pick(const struct wsscreen_list *scrdata, const char *name)
323 {
324 	int i;
325 	const struct wsscreen_descr *scr;
326 
327 	KASSERT(scrdata->nscreens > 0);
328 
329 	if (name == NULL || *name == '\0')
330 		return (scrdata->screens[0]);
331 
332 	for (i = 0; i < scrdata->nscreens; i++) {
333 		scr = scrdata->screens[i];
334 		if (!strncmp(name, scr->name, WSSCREEN_NAME_SIZE))
335 			return (scr);
336 	}
337 
338 	return (0);
339 }
340 
341 /*
342  * print info about attached screen
343  */
344 void
345 wsdisplay_addscreen_print(struct wsdisplay_softc *sc, int idx, int count)
346 {
347 	printf("%s: screen %d", sc->sc_dv.dv_xname, idx);
348 	if (count > 1)
349 		printf("-%d", idx + (count-1));
350 	printf(" added (%s, %s emulation)\n",
351 	    sc->sc_scr[idx]->scr_dconf->scrdata->name,
352 	    sc->sc_scr[idx]->scr_dconf->wsemul->name);
353 }
354 
355 int
356 wsdisplay_addscreen(struct wsdisplay_softc *sc, int idx,
357     const char *screentype, const char *emul)
358 {
359 	const struct wsscreen_descr *scrdesc;
360 	int error;
361 	void *cookie;
362 	int ccol, crow;
363 	long defattr;
364 	struct wsscreen *scr;
365 	int s;
366 
367 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
368 		return (EINVAL);
369 	if (sc->sc_scr[idx] != NULL)
370 		return (EBUSY);
371 
372 	scrdesc = wsdisplay_screentype_pick(sc->sc_scrdata, screentype);
373 	if (!scrdesc)
374 		return (ENXIO);
375 	error = (*sc->sc_accessops->alloc_screen)(sc->sc_accesscookie,
376 	    scrdesc, &cookie, &ccol, &crow, &defattr);
377 	if (error)
378 		return (error);
379 
380 	scr = wsscreen_attach(sc, 0, emul, scrdesc,
381 	    cookie, ccol, crow, defattr);
382 	if (scr == NULL) {
383 		(*sc->sc_accessops->free_screen)(sc->sc_accesscookie, cookie);
384 		return (ENXIO);
385 	}
386 
387 	sc->sc_scr[idx] = scr;
388 
389 	/* if no screen has focus yet, activate the first we get */
390 	s = spltty();
391 	if (!sc->sc_focus) {
392 		(*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
393 		    scr->scr_dconf->emulcookie, 0, 0, 0);
394 		sc->sc_focusidx = idx;
395 		sc->sc_focus = scr;
396 	}
397 	splx(s);
398 
399 #ifdef HAVE_WSMOUSED_SUPPORT
400 	allocate_copybuffer(sc); /* enlarge the copy buffer if necessary */
401 #endif
402 	return (0);
403 }
404 
405 int
406 wsdisplay_getscreen(struct wsdisplay_softc *sc,
407     struct wsdisplay_addscreendata *sd)
408 {
409 	struct wsscreen *scr;
410 
411 	if (sd->idx < 0 && sc->sc_focus)
412 		sd->idx = sc->sc_focusidx;
413 
414 	if (sd->idx < 0 || sd->idx >= WSDISPLAY_MAXSCREEN)
415 		return (EINVAL);
416 
417 	scr = sc->sc_scr[sd->idx];
418 	if (scr == NULL)
419 		return (ENXIO);
420 
421 	strncpy(sd->screentype, scr->scr_dconf->scrdata->name,
422 	    WSSCREEN_NAME_SIZE);
423 	strncpy(sd->emul, scr->scr_dconf->wsemul->name, WSEMUL_NAME_SIZE);
424 
425 	return (0);
426 }
427 
428 void
429 wsdisplay_closescreen(struct wsdisplay_softc *sc, struct wsscreen *scr)
430 {
431 	int maj, mn, idx;
432 
433 	/* hangup */
434 	if (WSSCREEN_HAS_TTY(scr)) {
435 		struct tty *tp = scr->scr_tty;
436 		(*linesw[tp->t_line].l_modem)(tp, 0);
437 	}
438 
439 	/* locate the major number */
440 	for (maj = 0; maj < nchrdev; maj++)
441 		if (cdevsw[maj].d_open == wsdisplayopen)
442 			break;
443 	/* locate the screen index */
444 	for (idx = 0; idx < WSDISPLAY_MAXSCREEN; idx++)
445 		if (scr == sc->sc_scr[idx])
446 			break;
447 #ifdef DIAGNOSTIC
448 	if (idx == WSDISPLAY_MAXSCREEN)
449 		panic("wsdisplay_forceclose: bad screen");
450 #endif
451 
452 	/* nuke the vnodes */
453 	mn = WSDISPLAYMINOR(sc->sc_dv.dv_unit, idx);
454 	vdevgone(maj, mn, mn, VCHR);
455 }
456 
457 int
458 wsdisplay_delscreen(struct wsdisplay_softc *sc, int idx, int flags)
459 {
460 	struct wsscreen *scr;
461 	int s;
462 	void *cookie;
463 
464 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
465 		return (EINVAL);
466 	if ((scr = sc->sc_scr[idx]) == NULL)
467 		return (ENXIO);
468 
469 	if (scr->scr_dconf == &wsdisplay_console_conf ||
470 #ifdef WSDISPLAY_COMPAT_USL
471 	    scr->scr_syncops ||
472 #endif
473 	    ((scr->scr_flags & SCR_OPEN) && !(flags & WSDISPLAY_DELSCR_FORCE)))
474 		return (EBUSY);
475 
476 	wsdisplay_closescreen(sc, scr);
477 
478 	/*
479 	 * delete pointers, so neither device entries
480 	 * nor keyboard input can reference it anymore
481 	 */
482 	s = spltty();
483 	if (sc->sc_focus == scr) {
484 		sc->sc_focus = NULL;
485 #ifdef WSDISPLAY_COMPAT_RAWKBD
486 		wsdisplay_update_rawkbd(sc, 0);
487 #endif
488 	}
489 	sc->sc_scr[idx] = NULL;
490 	splx(s);
491 
492 	/*
493 	 * Wake up processes waiting for the screen to
494 	 * be activated. Sleepers must check whether
495 	 * the screen still exists.
496 	 */
497 	if (scr->scr_flags & SCR_WAITACTIVE)
498 		wakeup(scr);
499 
500 	/* save a reference to the graphics screen */
501 	cookie = scr->scr_dconf->emulcookie;
502 
503 	wsscreen_detach(scr);
504 
505 	(*sc->sc_accessops->free_screen)(sc->sc_accesscookie, cookie);
506 
507 	if ((flags & WSDISPLAY_DELSCR_QUIET) == 0)
508 		printf("%s: screen %d deleted\n", sc->sc_dv.dv_xname, idx);
509 	return (0);
510 }
511 
512 /*
513  * Autoconfiguration functions.
514  */
515 int
516 wsdisplay_emul_match(struct device *parent, void *match, void *aux)
517 {
518 	struct cfdata *cf = match;
519 	struct wsemuldisplaydev_attach_args *ap = aux;
520 
521 	if (cf->wsemuldisplaydevcf_console != WSEMULDISPLAYDEVCF_CONSOLE_UNK) {
522 		/*
523 		 * If console-ness of device specified, either match
524 		 * exactly (at high priority), or fail.
525 		 */
526 		if (cf->wsemuldisplaydevcf_console != 0 && ap->console != 0)
527 			return (10);
528 		else
529 			return (0);
530 	}
531 
532 	/* If console-ness unspecified, it wins. */
533 	return (1);
534 }
535 
536 void
537 wsdisplay_emul_attach(struct device *parent, struct device *self, void *aux)
538 {
539 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)self;
540 	struct wsemuldisplaydev_attach_args *ap = aux;
541 
542 	wsdisplay_common_attach(sc, ap->console,
543 	    sc->sc_dv.dv_cfdata->wsemuldisplaydevcf_mux, ap->scrdata,
544 	    ap->accessops, ap->accesscookie, ap->defaultscreens);
545 
546 	if (ap->console && cn_tab == &wsdisplay_cons) {
547 		int maj;
548 
549 		/* locate the major number */
550 		for (maj = 0; maj < nchrdev; maj++)
551 			if (cdevsw[maj].d_open == wsdisplayopen)
552 				break;
553 
554 		cn_tab->cn_dev = makedev(maj, WSDISPLAYMINOR(self->dv_unit, 0));
555 	}
556 }
557 
558 /*
559  * Detach a display.
560  */
561 int
562 wsdisplay_emul_detach(struct device *self, int flags)
563 {
564 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)self;
565 
566 	return (wsdisplay_common_detach(sc, flags));
567 }
568 
569 int
570 wsdisplay_activate(struct device *self, int act)
571 {
572 	int ret = 0;
573 
574 	switch (act) {
575 	case DVACT_POWERDOWN:
576 		wsdisplay_switchtoconsole();
577 		break;
578 	}
579 
580 	return (ret);
581 }
582 
583 int
584 wsdisplay_common_detach(struct wsdisplay_softc *sc, int flags)
585 {
586 	int i;
587 	int rc;
588 
589 	/* We don't support detaching the console display yet. */
590 	if (sc->sc_isconsole)
591 		return (EBUSY);
592 
593 	/* Delete all screens managed by this display */
594 	for (i = 0; i < WSDISPLAY_MAXSCREEN; i++)
595 		if (sc->sc_scr[i] != NULL) {
596 			if ((rc = wsdisplay_delscreen(sc, i,
597 			    WSDISPLAY_DELSCR_QUIET | (flags & DETACH_FORCE ?
598 			     WSDISPLAY_DELSCR_FORCE : 0))) != 0)
599 				return (rc);
600 		}
601 
602 #ifdef HAVE_BURNER_SUPPORT
603 	timeout_del(&sc->sc_burner);
604 #endif
605 
606 #if NWSKBD > 0
607 	if (sc->sc_input != NULL) {
608 #if NWSMUX > 0
609 		/*
610 		 * If we are the display of the mux we are attached to,
611 		 * disconnect all input devices from us.
612 		 */
613 		if (sc->sc_input->me_dispdv == &sc->sc_dv) {
614 			if ((rc = wsmux_set_display((struct wsmux_softc *)
615 						    sc->sc_input, NULL)) != 0)
616 				return (rc);
617 		}
618 
619 		/*
620 		 * XXX
621 		 * If we created a standalone mux (dmux), we should destroy it
622 		 * there, but there is currently no support for this in wsmux.
623 		 */
624 #else
625 		if ((rc = wskbd_set_display((struct device *)sc->sc_input,
626 		    NULL)) != 0)
627 			return (rc);
628 #endif
629 	}
630 #endif
631 
632 	return (0);
633 }
634 
635 /* Print function (for parent devices). */
636 int
637 wsemuldisplaydevprint(void *aux, const char *pnp)
638 {
639 #if 0 /* -Wunused */
640 	struct wsemuldisplaydev_attach_args *ap = aux;
641 #endif
642 
643 	if (pnp)
644 		printf("wsdisplay at %s", pnp);
645 #if 0 /* don't bother; it's ugly */
646 	printf(" console %d", ap->console);
647 #endif
648 
649 	return (UNCONF);
650 }
651 
652 /* Submatch function (for parent devices). */
653 int
654 wsemuldisplaydevsubmatch(struct device *parent, void *match, void *aux)
655 {
656 	extern struct cfdriver wsdisplay_cd;
657 	struct cfdata *cf = match;
658 
659 	/* only allow wsdisplay to attach */
660 	if (cf->cf_driver == &wsdisplay_cd)
661 		return ((*cf->cf_attach->ca_match)(parent, match, aux));
662 
663 	return (0);
664 }
665 
666 void
667 wsdisplay_common_attach(struct wsdisplay_softc *sc, int console, int kbdmux,
668     const struct wsscreen_list *scrdata,
669     const struct wsdisplay_accessops *accessops, void *accesscookie,
670     u_int defaultscreens)
671 {
672 	int i, start = 0;
673 #if NWSKBD > 0
674 	struct wsevsrc *kme;
675 #if NWSMUX > 0
676 	struct wsmux_softc *mux;
677 
678 	if (kbdmux >= 0)
679 		mux = wsmux_getmux(kbdmux);
680 	else
681 		mux = wsmux_create("dmux", sc->sc_dv.dv_unit);
682 	/* XXX panic()ing isn't nice, but attach cannot fail */
683 	if (mux == NULL)
684 		panic("wsdisplay_common_attach: no memory");
685 	sc->sc_input = &mux->sc_base;
686 
687 	if (kbdmux >= 0)
688 		printf(" mux %d", kbdmux);
689 #else
690 #if 0	/* not worth keeping, especially since the default value is not -1... */
691 	if (kbdmux >= 0)
692 		printf(" (mux ignored)");
693 #endif
694 #endif	/* NWSMUX > 0 */
695 #endif	/* NWSKBD > 0 */
696 
697 	sc->sc_isconsole = console;
698 	sc->sc_resumescreen = WSDISPLAY_NULLSCREEN;
699 
700 	if (console) {
701 		KASSERT(wsdisplay_console_initted);
702 		KASSERT(wsdisplay_console_device == NULL);
703 
704 		sc->sc_scr[0] = wsscreen_attach(sc, 1, 0, 0, 0, 0, 0, 0);
705 		if (sc->sc_scr[0] == NULL)
706 			return;
707 		wsdisplay_console_device = sc;
708 
709 		printf(": console (%s, %s emulation)",
710 		       wsdisplay_console_conf.scrdata->name,
711 		       wsdisplay_console_conf.wsemul->name);
712 
713 #if NWSKBD > 0
714 		kme = wskbd_set_console_display(&sc->sc_dv, sc->sc_input);
715 		if (kme != NULL)
716 			printf(", using %s", kme->me_dv.dv_xname);
717 #if NWSMUX == 0
718 		sc->sc_input = kme;
719 #endif
720 #endif
721 
722 		sc->sc_focusidx = 0;
723 		sc->sc_focus = sc->sc_scr[0];
724 		start = 1;
725 	}
726 	printf("\n");
727 
728 #if NWSKBD > 0 && NWSMUX > 0
729 	/*
730 	 * If this mux did not have a display device yet, volunteer for
731 	 * the job.
732 	 */
733 	if (mux->sc_displaydv == NULL)
734 		wsmux_set_display(mux, &sc->sc_dv);
735 #endif
736 
737 	sc->sc_accessops = accessops;
738 	sc->sc_accesscookie = accesscookie;
739 	sc->sc_scrdata = scrdata;
740 
741 	/*
742 	 * Set up a number of virtual screens if wanted. The
743 	 * WSDISPLAYIO_ADDSCREEN ioctl is more flexible, so this code
744 	 * is for special cases like installation kernels, as well as
745 	 * sane multihead defaults.
746 	 */
747 	if (defaultscreens == 0)
748 		defaultscreens = wsdisplay_defaultscreens;
749 	for (i = start; i < defaultscreens; i++) {
750 		if (wsdisplay_addscreen(sc, i, 0, 0))
751 			break;
752 	}
753 
754 	if (i > start)
755 		wsdisplay_addscreen_print(sc, start, i-start);
756 
757 #ifdef HAVE_BURNER_SUPPORT
758 	sc->sc_burnoutintvl = (hz * WSDISPLAY_DEFBURNOUT) / 1000;
759 	sc->sc_burninintvl = (hz * WSDISPLAY_DEFBURNIN) / 1000;
760 	sc->sc_burnflags = WSDISPLAY_BURN_OUTPUT | WSDISPLAY_BURN_KBD |
761 	    WSDISPLAY_BURN_MOUSE;
762 	timeout_set(&sc->sc_burner, wsdisplay_burner, sc);
763 	sc->sc_burnout = sc->sc_burnoutintvl;
764 	wsdisplay_burn(sc, sc->sc_burnflags);
765 #endif
766 
767 #if NWSKBD > 0 && NWSMUX == 0
768 	if (console == 0) {
769 		/*
770 		 * In the non-wsmux world, always connect wskbd0 and wsdisplay0
771 		 * together.
772 		 */
773 		extern struct cfdriver wskbd_cd;
774 
775 		if (wskbd_cd.cd_ndevs != 0 && sc->sc_dv.dv_unit == 0) {
776 			if (wsdisplay_set_kbd(&sc->sc_dv,
777 			    (struct wsevsrc *)wskbd_cd.cd_devs[0]) == 0)
778 				wskbd_set_display(wskbd_cd.cd_devs[0],
779 				    &sc->sc_dv);
780 		}
781 	}
782 #endif
783 }
784 
785 void
786 wsdisplay_cnattach(const struct wsscreen_descr *type, void *cookie, int ccol,
787     int crow, long defattr)
788 {
789 	const struct wsemul_ops *wsemul;
790 	const struct wsdisplay_emulops *emulops;
791 
792 	KASSERT(type->nrows > 0);
793 	KASSERT(type->ncols > 0);
794 	KASSERT(crow < type->nrows);
795 	KASSERT(ccol < type->ncols);
796 
797 	wsdisplay_console_conf.emulops = emulops = type->textops;
798 	wsdisplay_console_conf.emulcookie = cookie;
799 	wsdisplay_console_conf.scrdata = type;
800 
801 #ifdef WSEMUL_DUMB
802 	/*
803 	 * If the emulops structure is crippled, force a dumb emulation.
804 	 */
805 	if (emulops->cursor == NULL ||
806 	    emulops->copycols == NULL || emulops->copyrows == NULL ||
807 	    emulops->erasecols == NULL || emulops->eraserows == NULL)
808 		wsemul = wsemul_pick("dumb");
809 	else
810 #endif
811 		wsemul = wsemul_pick("");
812 	wsdisplay_console_conf.wsemul = wsemul;
813 	wsdisplay_console_conf.wsemulcookie =
814 	    (*wsemul->cnattach)(type, cookie, ccol, crow, defattr);
815 
816 	if (!wsdisplay_console_initted)
817 		cn_tab = &wsdisplay_cons;
818 
819 	wsdisplay_console_initted = 1;
820 }
821 
822 /*
823  * Tty and cdevsw functions.
824  */
825 int
826 wsdisplayopen(dev_t dev, int flag, int mode, struct proc *p)
827 {
828 	struct wsdisplay_softc *sc;
829 	struct tty *tp;
830 	int unit, newopen, error;
831 	struct wsscreen *scr;
832 
833 	unit = WSDISPLAYUNIT(dev);
834 	if (unit >= wsdisplay_cd.cd_ndevs ||	/* make sure it was attached */
835 	    (sc = wsdisplay_cd.cd_devs[unit]) == NULL)
836 		return (ENXIO);
837 
838 	if (ISWSDISPLAYCTL(dev))
839 		return (0);
840 
841 	if (WSDISPLAYSCREEN(dev) >= WSDISPLAY_MAXSCREEN)
842 		return (ENXIO);
843 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
844 		return (ENXIO);
845 
846 	if (WSSCREEN_HAS_TTY(scr)) {
847 		tp = scr->scr_tty;
848 		tp->t_oproc = wsdisplaystart;
849 		tp->t_param = wsdisplayparam;
850 		tp->t_dev = dev;
851 		newopen = (tp->t_state & TS_ISOPEN) == 0;
852 		if (newopen) {
853 			ttychars(tp);
854 			tp->t_iflag = TTYDEF_IFLAG;
855 			tp->t_oflag = TTYDEF_OFLAG;
856 			tp->t_cflag = TTYDEF_CFLAG;
857 			tp->t_lflag = TTYDEF_LFLAG;
858 			tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
859 			wsdisplayparam(tp, &tp->t_termios);
860 			ttsetwater(tp);
861 		} else if ((tp->t_state & TS_XCLUDE) != 0 &&
862 			   suser(p, 0) != 0)
863 			return (EBUSY);
864 		tp->t_state |= TS_CARR_ON;
865 
866 		error = ((*linesw[tp->t_line].l_open)(dev, tp, p));
867 		if (error)
868 			return (error);
869 
870 		if (newopen) {
871 			/* set window sizes as appropriate, and reset
872 			   the emulation */
873 			tp->t_winsize.ws_row = scr->scr_dconf->scrdata->nrows;
874 			tp->t_winsize.ws_col = scr->scr_dconf->scrdata->ncols;
875 		}
876 	}
877 
878 	scr->scr_flags |= SCR_OPEN;
879 	return (0);
880 }
881 
882 int
883 wsdisplayclose(dev_t dev, int flag, int mode, struct proc *p)
884 {
885 	struct wsdisplay_softc *sc;
886 	struct tty *tp;
887 	int unit;
888 	struct wsscreen *scr;
889 
890 	unit = WSDISPLAYUNIT(dev);
891 	sc = wsdisplay_cd.cd_devs[unit];
892 
893 	if (ISWSDISPLAYCTL(dev))
894 		return (0);
895 
896 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
897 		return (ENXIO);
898 
899 	if (WSSCREEN_HAS_TTY(scr)) {
900 		if (scr->scr_hold_screen) {
901 			int s;
902 
903 			/* XXX RESET KEYBOARD LEDS, etc. */
904 			s = spltty();	/* avoid conflict with keyboard */
905 			wsdisplay_kbdholdscr(scr, 0);
906 			splx(s);
907 		}
908 		tp = scr->scr_tty;
909 		(*linesw[tp->t_line].l_close)(tp, flag, p);
910 		ttyclose(tp);
911 	}
912 
913 #ifdef WSDISPLAY_COMPAT_USL
914 	if (scr->scr_syncops)
915 		(*scr->scr_syncops->destroy)(scr->scr_synccookie);
916 #endif
917 
918 	scr->scr_flags &= ~SCR_GRAPHICS;
919 	(*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
920 					 WSEMUL_RESET);
921 	if (wsdisplay_clearonclose)
922 		(*scr->scr_dconf->wsemul->reset)
923 			(scr->scr_dconf->wsemulcookie, WSEMUL_CLEARSCREEN);
924 
925 #ifdef WSDISPLAY_COMPAT_RAWKBD
926 	if (scr->scr_rawkbd) {
927 		int kbmode = WSKBD_TRANSLATED;
928 		(void) wsdisplay_internal_ioctl(sc, scr, WSKBDIO_SETMODE,
929 		    (caddr_t)&kbmode, FWRITE, p);
930 	}
931 #endif
932 
933 	scr->scr_flags &= ~SCR_OPEN;
934 
935 #ifdef HAVE_WSMOUSED_SUPPORT
936 	/* remove the selection at logout */
937 	if (sc->sc_copybuffer != NULL)
938 		bzero(sc->sc_copybuffer, sc->sc_copybuffer_size);
939 	CLR(sc->sc_flags, SC_PASTE_AVAIL);
940 #endif
941 
942 	return (0);
943 }
944 
945 int
946 wsdisplayread(dev_t dev, struct uio *uio, int flag)
947 {
948 	struct wsdisplay_softc *sc;
949 	struct tty *tp;
950 	int unit;
951 	struct wsscreen *scr;
952 
953 	unit = WSDISPLAYUNIT(dev);
954 	sc = wsdisplay_cd.cd_devs[unit];
955 
956 	if (ISWSDISPLAYCTL(dev))
957 		return (0);
958 
959 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
960 		return (ENXIO);
961 
962 	if (!WSSCREEN_HAS_TTY(scr))
963 		return (ENODEV);
964 
965 	tp = scr->scr_tty;
966 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
967 }
968 
969 int
970 wsdisplaywrite(dev_t dev, struct uio *uio, int flag)
971 {
972 	struct wsdisplay_softc *sc;
973 	struct tty *tp;
974 	int unit;
975 	struct wsscreen *scr;
976 
977 	unit = WSDISPLAYUNIT(dev);
978 	sc = wsdisplay_cd.cd_devs[unit];
979 
980 	if (ISWSDISPLAYCTL(dev))
981 		return (0);
982 
983 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
984 		return (ENXIO);
985 
986 	if (!WSSCREEN_HAS_TTY(scr))
987 		return (ENODEV);
988 
989 	tp = scr->scr_tty;
990 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
991 }
992 
993 struct tty *
994 wsdisplaytty(dev_t dev)
995 {
996 	struct wsdisplay_softc *sc;
997 	int unit;
998 	struct wsscreen *scr;
999 
1000 	unit = WSDISPLAYUNIT(dev);
1001 	sc = wsdisplay_cd.cd_devs[unit];
1002 
1003 	if (ISWSDISPLAYCTL(dev))
1004 		panic("wsdisplaytty() on ctl device");
1005 
1006 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
1007 		return (NULL);
1008 
1009 	return (scr->scr_tty);
1010 }
1011 
1012 int
1013 wsdisplayioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
1014 {
1015 	struct wsdisplay_softc *sc;
1016 	struct tty *tp;
1017 	int unit, error;
1018 	struct wsscreen *scr;
1019 
1020 	unit = WSDISPLAYUNIT(dev);
1021 	sc = wsdisplay_cd.cd_devs[unit];
1022 
1023 #ifdef WSDISPLAY_COMPAT_USL
1024 	error = wsdisplay_usl_ioctl1(sc, cmd, data, flag, p);
1025 	if (error >= 0)
1026 		return (error);
1027 #endif
1028 
1029 	if (ISWSDISPLAYCTL(dev)) {
1030 	       	if (cmd != WSDISPLAYIO_GTYPE)
1031 			return (wsdisplay_cfg_ioctl(sc, cmd, data, flag, p));
1032 		/* pass WSDISPLAYIO_GTYPE to the first screen */
1033 		dev = makedev(major(dev), WSDISPLAYMINOR(unit, 0));
1034 	}
1035 
1036 	if (WSDISPLAYSCREEN(dev) >= WSDISPLAY_MAXSCREEN)
1037 		return (ENODEV);
1038 
1039 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
1040 		return (ENXIO);
1041 
1042 	if (WSSCREEN_HAS_TTY(scr)) {
1043 		tp = scr->scr_tty;
1044 
1045 /* printf("disc\n"); */
1046 		/* do the line discipline ioctls first */
1047 		error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
1048 		if (error >= 0)
1049 			return (error);
1050 
1051 /* printf("tty\n"); */
1052 		/* then the tty ioctls */
1053 		error = ttioctl(tp, cmd, data, flag, p);
1054 		if (error >= 0)
1055 			return (error);
1056 	}
1057 
1058 #ifdef WSDISPLAY_COMPAT_USL
1059 	error = wsdisplay_usl_ioctl2(sc, scr, cmd, data, flag, p);
1060 	if (error >= 0)
1061 		return (error);
1062 #endif
1063 
1064 	error = wsdisplay_internal_ioctl(sc, scr, cmd, data, flag, p);
1065 	return (error != -1 ? error : ENOTTY);
1066 }
1067 
1068 int
1069 wsdisplay_param(struct device *dev, u_long cmd, struct wsdisplay_param *dp)
1070 {
1071 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
1072 
1073 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd,
1074 	    (caddr_t)dp, 0, NULL));
1075 }
1076 
1077 int
1078 wsdisplay_internal_ioctl(struct wsdisplay_softc *sc, struct wsscreen *scr,
1079     u_long cmd, caddr_t data, int flag, struct proc *p)
1080 {
1081 	int error;
1082 
1083 #if NWSKBD > 0
1084 	struct wsevsrc *inp;
1085 
1086 #ifdef WSDISPLAY_COMPAT_RAWKBD
1087 	switch (cmd) {
1088 	case WSKBDIO_SETMODE:
1089 		if ((flag & FWRITE) == 0)
1090 			return (EACCES);
1091 		scr->scr_rawkbd = (*(int *)data == WSKBD_RAW);
1092 		return (wsdisplay_update_rawkbd(sc, scr));
1093 	case WSKBDIO_GETMODE:
1094 		*(int *)data = (scr->scr_rawkbd ?
1095 				WSKBD_RAW : WSKBD_TRANSLATED);
1096 		return (0);
1097 	}
1098 #endif
1099 	inp = sc->sc_input;
1100 	if (inp != NULL) {
1101 		error = wsevsrc_display_ioctl(inp, cmd, data, flag, p);
1102 		if (error >= 0)
1103 			return (error);
1104 	}
1105 #endif /* NWSKBD > 0 */
1106 
1107 	switch (cmd) {
1108 	case WSDISPLAYIO_SMODE:
1109 	case WSDISPLAYIO_USEFONT:
1110 #ifdef HAVE_BURNER_SUPPORT
1111 	case WSDISPLAYIO_SVIDEO:
1112 	case WSDISPLAYIO_SBURNER:
1113 #endif
1114 	case WSDISPLAYIO_SETSCREEN:
1115 		if ((flag & FWRITE) == 0)
1116 			return (EACCES);
1117 	}
1118 
1119 	switch (cmd) {
1120 	case WSDISPLAYIO_GMODE:
1121 		if (scr->scr_flags & SCR_GRAPHICS) {
1122 			if (scr->scr_flags & SCR_DUMBFB)
1123 				*(u_int *)data = WSDISPLAYIO_MODE_DUMBFB;
1124 			else
1125 				*(u_int *)data = WSDISPLAYIO_MODE_MAPPED;
1126 		} else
1127 			*(u_int *)data = WSDISPLAYIO_MODE_EMUL;
1128 		return (0);
1129 
1130 	case WSDISPLAYIO_SMODE:
1131 #define d (*(int *)data)
1132 		if (d != WSDISPLAYIO_MODE_EMUL &&
1133 		    d != WSDISPLAYIO_MODE_MAPPED &&
1134 		    d != WSDISPLAYIO_MODE_DUMBFB)
1135 			return (EINVAL);
1136 
1137 		scr->scr_flags &= ~SCR_GRAPHICS;
1138 		if (d == WSDISPLAYIO_MODE_MAPPED ||
1139 		    d == WSDISPLAYIO_MODE_DUMBFB) {
1140 			scr->scr_flags |= SCR_GRAPHICS |
1141 			    ((d == WSDISPLAYIO_MODE_DUMBFB) ?  SCR_DUMBFB : 0);
1142 
1143 			/* clear cursor */
1144 			(*scr->scr_dconf->wsemul->reset)
1145 			    (scr->scr_dconf->wsemulcookie, WSEMUL_CLEARCURSOR);
1146 		}
1147 
1148 #ifdef HAVE_BURNER_SUPPORT
1149 		wsdisplay_burner_setup(sc, scr);
1150 #endif
1151 
1152 		(void)(*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
1153 		    flag, p);
1154 
1155 		return (0);
1156 #undef d
1157 
1158 	case WSDISPLAYIO_USEFONT:
1159 #define d ((struct wsdisplay_font *)data)
1160 		if (!sc->sc_accessops->load_font)
1161 			return (EINVAL);
1162 		d->data = NULL;
1163 		error = (*sc->sc_accessops->load_font)(sc->sc_accesscookie,
1164 		    scr->scr_dconf->emulcookie, d);
1165 		if (!error)
1166 			(*scr->scr_dconf->wsemul->reset)
1167 			    (scr->scr_dconf->wsemulcookie, WSEMUL_SYNCFONT);
1168 		return (error);
1169 #undef d
1170 #ifdef HAVE_BURNER_SUPPORT
1171 	case WSDISPLAYIO_GVIDEO:
1172 		*(u_int *)data = !sc->sc_burnman;
1173 		break;
1174 
1175 	case WSDISPLAYIO_SVIDEO:
1176 		if (*(u_int *)data != WSDISPLAYIO_VIDEO_OFF &&
1177 		    *(u_int *)data != WSDISPLAYIO_VIDEO_ON)
1178 			return (EINVAL);
1179 		if (sc->sc_accessops->burn_screen == NULL)
1180 			return (EOPNOTSUPP);
1181 		(*sc->sc_accessops->burn_screen)(sc->sc_accesscookie,
1182 		     *(u_int *)data, sc->sc_burnflags);
1183 		sc->sc_burnman = *(u_int *)data == WSDISPLAYIO_VIDEO_OFF;
1184 		break;
1185 
1186 	case WSDISPLAYIO_GBURNER:
1187 #define d ((struct wsdisplay_burner *)data)
1188 		d->on  = sc->sc_burninintvl  * 1000 / hz;
1189 		d->off = sc->sc_burnoutintvl * 1000 / hz;
1190 		d->flags = sc->sc_burnflags;
1191 		return (0);
1192 
1193 	case WSDISPLAYIO_SBURNER:
1194 	    {
1195 		struct wsscreen *active;
1196 
1197 		if (d->flags & ~(WSDISPLAY_BURN_VBLANK | WSDISPLAY_BURN_KBD |
1198 		    WSDISPLAY_BURN_MOUSE | WSDISPLAY_BURN_OUTPUT))
1199 			return EINVAL;
1200 
1201 		error = 0;
1202 		sc->sc_burnflags = d->flags;
1203 		/* disable timeout if necessary */
1204 		if ((sc->sc_burnflags & (WSDISPLAY_BURN_OUTPUT |
1205 		    WSDISPLAY_BURN_KBD | WSDISPLAY_BURN_MOUSE)) == 0) {
1206 			if (sc->sc_burnout)
1207 				timeout_del(&sc->sc_burner);
1208 		}
1209 
1210 		active = sc->sc_focus;
1211 		if (active == NULL)
1212 			active = scr;
1213 
1214 		if (d->on) {
1215 			sc->sc_burninintvl = hz * d->on / 1000;
1216 			if (sc->sc_burnman) {
1217 				sc->sc_burnout = sc->sc_burninintvl;
1218 				/* reinit timeout if changed */
1219 				if ((active->scr_flags & SCR_GRAPHICS) == 0)
1220 					wsdisplay_burn(sc, sc->sc_burnflags);
1221 			}
1222 		}
1223 		if (d->off) {
1224 			sc->sc_burnoutintvl = hz * d->off / 1000;
1225 			if (!sc->sc_burnman) {
1226 				sc->sc_burnout = sc->sc_burnoutintvl;
1227 				/* reinit timeout if changed */
1228 				if ((active->scr_flags & SCR_GRAPHICS) == 0)
1229 					wsdisplay_burn(sc, sc->sc_burnflags);
1230 			}
1231 		}
1232 		return (error);
1233 	    }
1234 #undef d
1235 #endif	/* HAVE_BURNER_SUPPORT */
1236 	case WSDISPLAYIO_GETSCREEN:
1237 		return (wsdisplay_getscreen(sc,
1238 		    (struct wsdisplay_addscreendata *)data));
1239 
1240 	case WSDISPLAYIO_SETSCREEN:
1241 		return (wsdisplay_switch((void *)sc, *(int *)data, 1));
1242 
1243 	case WSDISPLAYIO_GETSCREENTYPE:
1244 #define d ((struct wsdisplay_screentype *)data)
1245 		if (d->idx >= sc->sc_scrdata->nscreens)
1246 			return(EINVAL);
1247 
1248 		d->nidx = sc->sc_scrdata->nscreens;
1249 		strncpy(d->name, sc->sc_scrdata->screens[d->idx]->name,
1250 			WSSCREEN_NAME_SIZE);
1251 		d->ncols = sc->sc_scrdata->screens[d->idx]->ncols;
1252 		d->nrows = sc->sc_scrdata->screens[d->idx]->nrows;
1253 		d->fontwidth = sc->sc_scrdata->screens[d->idx]->fontwidth;
1254 		d->fontheight = sc->sc_scrdata->screens[d->idx]->fontheight;
1255 		return (0);
1256 #undef d
1257 	case WSDISPLAYIO_GETEMULTYPE:
1258 #define d ((struct wsdisplay_emultype *)data)
1259 		if (wsemul_getname(d->idx) == NULL)
1260 			return(EINVAL);
1261 		strncpy(d->name, wsemul_getname(d->idx), WSEMUL_NAME_SIZE);
1262 		return (0);
1263 #undef d
1264         }
1265 
1266 	/* check ioctls for display */
1267 	return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
1268 	    flag, p));
1269 }
1270 
1271 int
1272 wsdisplay_cfg_ioctl(struct wsdisplay_softc *sc, u_long cmd, caddr_t data,
1273     int flag, struct proc *p)
1274 {
1275 	int error;
1276 	void *buf;
1277 	size_t fontsz;
1278 #if NWSKBD > 0
1279 	struct wsevsrc *inp;
1280 #endif
1281 
1282 	switch (cmd) {
1283 #ifdef HAVE_WSMOUSED_SUPPORT
1284 	case WSDISPLAYIO_WSMOUSED:
1285 		error = wsmoused(sc, data, flag, p);
1286 		return (error);
1287 #endif
1288 	case WSDISPLAYIO_ADDSCREEN:
1289 #define d ((struct wsdisplay_addscreendata *)data)
1290 		if ((error = wsdisplay_addscreen(sc, d->idx,
1291 		    d->screentype, d->emul)) == 0)
1292 			wsdisplay_addscreen_print(sc, d->idx, 0);
1293 		return (error);
1294 #undef d
1295 	case WSDISPLAYIO_DELSCREEN:
1296 #define d ((struct wsdisplay_delscreendata *)data)
1297 		return (wsdisplay_delscreen(sc, d->idx, d->flags));
1298 #undef d
1299 	case WSDISPLAYIO_GETSCREEN:
1300 		return (wsdisplay_getscreen(sc,
1301 		    (struct wsdisplay_addscreendata *)data));
1302 	case WSDISPLAYIO_SETSCREEN:
1303 		return (wsdisplay_switch((void *)sc, *(int *)data, 1));
1304 	case WSDISPLAYIO_LDFONT:
1305 #define d ((struct wsdisplay_font *)data)
1306 		if (!sc->sc_accessops->load_font)
1307 			return (EINVAL);
1308 		fontsz = d->fontheight * d->stride * d->numchars;
1309 		if (fontsz > WSDISPLAY_MAXFONTSZ)
1310 			return (EINVAL);
1311 
1312 		buf = malloc(fontsz, M_DEVBUF, M_WAITOK);
1313 		error = copyin(d->data, buf, fontsz);
1314 		if (error) {
1315 			free(buf, M_DEVBUF, fontsz);
1316 			return (error);
1317 		}
1318 		d->data = buf;
1319 		error =
1320 		  (*sc->sc_accessops->load_font)(sc->sc_accesscookie, 0, d);
1321 		if (error)
1322 			free(buf, M_DEVBUF, fontsz);
1323 		return (error);
1324 
1325 	case WSDISPLAYIO_LSFONT:
1326 		if (!sc->sc_accessops->list_font)
1327 			return (EINVAL);
1328 		error =
1329 		  (*sc->sc_accessops->list_font)(sc->sc_accesscookie, d);
1330 		return (error);
1331 
1332 	case WSDISPLAYIO_DELFONT:
1333 		return (EINVAL);
1334 #undef d
1335 
1336 #if NWSKBD > 0
1337 	case WSMUXIO_ADD_DEVICE:
1338 #define d ((struct wsmux_device *)data)
1339 		if (d->idx == -1 && d->type == WSMUX_KBD)
1340 			d->idx = wskbd_pickfree();
1341 #undef d
1342 		/* FALLTHROUGH */
1343 	case WSMUXIO_INJECTEVENT:
1344 	case WSMUXIO_REMOVE_DEVICE:
1345 	case WSMUXIO_LIST_DEVICES:
1346 		inp = sc->sc_input;
1347 		if (inp == NULL)
1348 			return (ENXIO);
1349 		return (wsevsrc_ioctl(inp, cmd, data, flag,p));
1350 #endif /* NWSKBD > 0 */
1351 
1352 	}
1353 	return (EINVAL);
1354 }
1355 
1356 paddr_t
1357 wsdisplaymmap(dev_t dev, off_t offset, int prot)
1358 {
1359 	struct wsdisplay_softc *sc = wsdisplay_cd.cd_devs[WSDISPLAYUNIT(dev)];
1360 	struct wsscreen *scr;
1361 
1362 	if (ISWSDISPLAYCTL(dev))
1363 		return (-1);
1364 
1365 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
1366 		return (-1);
1367 
1368 	if (!(scr->scr_flags & SCR_GRAPHICS))
1369 		return (-1);
1370 
1371 	/* pass mmap to display */
1372 	return ((*sc->sc_accessops->mmap)(sc->sc_accesscookie, offset, prot));
1373 }
1374 
1375 int
1376 wsdisplaypoll(dev_t dev, int events, struct proc *p)
1377 {
1378 	struct wsdisplay_softc *sc = wsdisplay_cd.cd_devs[WSDISPLAYUNIT(dev)];
1379 	struct wsscreen *scr;
1380 
1381 	if (ISWSDISPLAYCTL(dev))
1382 		return (0);
1383 
1384 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
1385 		return (POLLERR);
1386 
1387 	if (!WSSCREEN_HAS_TTY(scr))
1388 		return (POLLERR);
1389 
1390 	return (ttpoll(dev, events, p));
1391 }
1392 
1393 int
1394 wsdisplaykqfilter(dev_t dev, struct knote *kn)
1395 {
1396 	struct wsdisplay_softc *sc = wsdisplay_cd.cd_devs[WSDISPLAYUNIT(dev)];
1397 	struct wsscreen *scr;
1398 
1399 	if (ISWSDISPLAYCTL(dev))
1400 		return (ENXIO);
1401 
1402 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(dev)]) == NULL)
1403 		return (ENXIO);
1404 
1405 	if (!WSSCREEN_HAS_TTY(scr))
1406 		return (ENXIO);
1407 
1408 	return (ttkqfilter(dev, kn));
1409 }
1410 
1411 void
1412 wsdisplaystart(struct tty *tp)
1413 {
1414 	struct wsdisplay_softc *sc;
1415 	struct wsscreen *scr;
1416 	int s, n, done, unit;
1417 	u_char *buf;
1418 
1419 	unit = WSDISPLAYUNIT(tp->t_dev);
1420 	if (unit >= wsdisplay_cd.cd_ndevs ||
1421 	    (sc = wsdisplay_cd.cd_devs[unit]) == NULL)
1422 		return;
1423 
1424 	s = spltty();
1425 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
1426 		splx(s);
1427 		return;
1428 	}
1429 	if (tp->t_outq.c_cc == 0 && tp->t_wsel.si_selpid == 0)
1430 		goto low;
1431 
1432 	if ((scr = sc->sc_scr[WSDISPLAYSCREEN(tp->t_dev)]) == NULL) {
1433 		splx(s);
1434 		return;
1435 	}
1436 	if (scr->scr_hold_screen) {
1437 		tp->t_state |= TS_TIMEOUT;
1438 		splx(s);
1439 		return;
1440 	}
1441 	tp->t_state |= TS_BUSY;
1442 	splx(s);
1443 
1444 	/*
1445 	 * Drain output from ring buffer.
1446 	 * The output will normally be in one contiguous chunk, but when the
1447 	 * ring wraps, it will be in two pieces.. one at the end of the ring,
1448 	 * the other at the start.  For performance, rather than loop here,
1449 	 * we output one chunk, see if there's another one, and if so, output
1450 	 * it too.
1451 	 */
1452 
1453 	n = ndqb(&tp->t_outq, 0);
1454 	buf = tp->t_outq.c_cf;
1455 
1456 	if (!(scr->scr_flags & SCR_GRAPHICS)) {
1457 #ifdef HAVE_BURNER_SUPPORT
1458 		wsdisplay_burn(sc, WSDISPLAY_BURN_OUTPUT);
1459 #endif
1460 #ifdef HAVE_WSMOUSED_SUPPORT
1461 		if (scr == sc->sc_focus)
1462 			mouse_remove(scr);
1463 #endif
1464 		done = (*scr->scr_dconf->wsemul->output)
1465 		    (scr->scr_dconf->wsemulcookie, buf, n, 0);
1466 	} else
1467 		done = n;
1468 	ndflush(&tp->t_outq, done);
1469 
1470 	if (done == n) {
1471 		if ((n = ndqb(&tp->t_outq, 0)) > 0) {
1472 			buf = tp->t_outq.c_cf;
1473 
1474 			if (!(scr->scr_flags & SCR_GRAPHICS)) {
1475 				done = (*scr->scr_dconf->wsemul->output)
1476 				    (scr->scr_dconf->wsemulcookie, buf, n, 0);
1477 			} else
1478 				done = n;
1479 			ndflush(&tp->t_outq, done);
1480 		}
1481 	}
1482 
1483 	s = spltty();
1484 	tp->t_state &= ~TS_BUSY;
1485 	/* Come back if there's more to do */
1486 	if (tp->t_outq.c_cc) {
1487 		tp->t_state |= TS_TIMEOUT;
1488 		timeout_add(&tp->t_rstrt_to, (hz > 128) ? (hz / 128) : 1);
1489 	}
1490 low:
1491 	ttwakeupwr(tp);
1492 	splx(s);
1493 }
1494 
1495 int
1496 wsdisplaystop(struct tty *tp, int flag)
1497 {
1498 	int s;
1499 
1500 	s = spltty();
1501 	if (ISSET(tp->t_state, TS_BUSY))
1502 		if (!ISSET(tp->t_state, TS_TTSTOP))
1503 			SET(tp->t_state, TS_FLUSH);
1504 	splx(s);
1505 
1506 	return (0);
1507 }
1508 
1509 /* Set line parameters. */
1510 int
1511 wsdisplayparam(struct tty *tp, struct termios *t)
1512 {
1513 
1514 	tp->t_ispeed = t->c_ispeed;
1515 	tp->t_ospeed = t->c_ospeed;
1516 	tp->t_cflag = t->c_cflag;
1517 	return (0);
1518 }
1519 
1520 /*
1521  * Callbacks for the emulation code.
1522  */
1523 void
1524 wsdisplay_emulbell(void *v)
1525 {
1526 	struct wsscreen *scr = v;
1527 
1528 	if (scr == NULL)		/* console, before real attach */
1529 		return;
1530 
1531 	if (scr->scr_flags & SCR_GRAPHICS) /* can this happen? */
1532 		return;
1533 
1534 	(void) wsdisplay_internal_ioctl(scr->sc, scr, WSKBDIO_BELL, NULL,
1535 	    FWRITE, NULL);
1536 }
1537 
1538 #if !defined(WSEMUL_NO_VT100)
1539 void
1540 wsdisplay_emulinput(void *v, const u_char *data, u_int count)
1541 {
1542 	struct wsscreen *scr = v;
1543 	struct tty *tp;
1544 
1545 	if (v == NULL)			/* console, before real attach */
1546 		return;
1547 
1548 	if (scr->scr_flags & SCR_GRAPHICS) /* XXX can't happen */
1549 		return;
1550 	if (!WSSCREEN_HAS_TTY(scr))
1551 		return;
1552 
1553 	tp = scr->scr_tty;
1554 	while (count-- > 0)
1555 		(*linesw[tp->t_line].l_rint)(*data++, tp);
1556 }
1557 #endif
1558 
1559 /*
1560  * Calls from the keyboard interface.
1561  */
1562 void
1563 wsdisplay_kbdinput(struct device *dev, kbd_t layout, keysym_t *ks, int num)
1564 {
1565 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
1566 	struct wsscreen *scr;
1567 	const u_char *dp;
1568 	int count;
1569 	struct tty *tp;
1570 
1571 	scr = sc->sc_focus;
1572 	if (!scr || !WSSCREEN_HAS_TTY(scr))
1573 		return;
1574 
1575 
1576 	tp = scr->scr_tty;
1577 	for (; num > 0; num--) {
1578 		count = (*scr->scr_dconf->wsemul->translate)
1579 		    (scr->scr_dconf->wsemulcookie, layout, *ks++, &dp);
1580 		while (count-- > 0)
1581 			(*linesw[tp->t_line].l_rint)(*dp++, tp);
1582 	}
1583 }
1584 
1585 #ifdef WSDISPLAY_COMPAT_RAWKBD
1586 void
1587 wsdisplay_rawkbdinput(struct device *dev, u_char *buf, int num)
1588 {
1589 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
1590 	struct wsscreen *scr;
1591 	struct tty *tp;
1592 
1593 	scr = sc->sc_focus;
1594 	if (!scr || !WSSCREEN_HAS_TTY(scr))
1595 		return;
1596 
1597 	tp = scr->scr_tty;
1598 	while (num-- > 0)
1599 		(*linesw[tp->t_line].l_rint)(*buf++, tp);
1600 }
1601 int
1602 wsdisplay_update_rawkbd(struct wsdisplay_softc *sc, struct wsscreen *scr)
1603 {
1604 #if NWSKBD > 0
1605 	int s, raw, data, error;
1606 	struct wsevsrc *inp;
1607 
1608 	s = spltty();
1609 
1610 	raw = (scr ? scr->scr_rawkbd : 0);
1611 
1612 	if (scr != sc->sc_focus || sc->sc_rawkbd == raw) {
1613 		splx(s);
1614 		return (0);
1615 	}
1616 
1617 	data = raw ? WSKBD_RAW : WSKBD_TRANSLATED;
1618 	inp = sc->sc_input;
1619 	if (inp == NULL) {
1620 		splx(s);
1621 		return (ENXIO);
1622 	}
1623 	error = wsevsrc_display_ioctl(inp, WSKBDIO_SETMODE, &data, FWRITE, 0);
1624 	if (!error)
1625 		sc->sc_rawkbd = raw;
1626 	splx(s);
1627 	return (error);
1628 #else
1629 	return (0);
1630 #endif
1631 }
1632 #endif
1633 
1634 int
1635 wsdisplay_switch3(void *arg, int error, int waitok)
1636 {
1637 	struct wsdisplay_softc *sc = arg;
1638 	int no;
1639 	struct wsscreen *scr;
1640 
1641 #ifdef WSDISPLAY_COMPAT_USL
1642 	if (!ISSET(sc->sc_flags, SC_SWITCHPENDING)) {
1643 		printf("wsdisplay_switch3: not switching\n");
1644 		return (EINVAL);
1645 	}
1646 
1647 	no = sc->sc_screenwanted;
1648 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1649 		panic("wsdisplay_switch3: invalid screen %d", no);
1650 	scr = sc->sc_scr[no];
1651 	if (!scr) {
1652 		printf("wsdisplay_switch3: screen %d disappeared\n", no);
1653 		error = ENXIO;
1654 	}
1655 
1656 	if (error) {
1657 		/* try to recover, avoid recursion */
1658 
1659 		if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
1660 			printf("wsdisplay_switch3: giving up\n");
1661 			sc->sc_focus = NULL;
1662 #ifdef WSDISPLAY_COMPAT_RAWKBD
1663 			wsdisplay_update_rawkbd(sc, 0);
1664 #endif
1665 			CLR(sc->sc_flags, SC_SWITCHPENDING);
1666 			return (error);
1667 		}
1668 
1669 		sc->sc_screenwanted = sc->sc_oldscreen;
1670 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
1671 		return (wsdisplay_switch1(arg, 0, waitok));
1672 	}
1673 #else
1674 	/*
1675 	 * If we do not have syncops support, we come straight from
1676 	 * wsdisplay_switch2 which has already validated our arguments
1677 	 * and did not sleep.
1678 	 */
1679 	no = sc->sc_screenwanted;
1680 	scr = sc->sc_scr[no];
1681 #endif
1682 
1683 	CLR(sc->sc_flags, SC_SWITCHPENDING);
1684 
1685 #ifdef HAVE_BURNER_SUPPORT
1686 	if (!error)
1687 		wsdisplay_burner_setup(sc, scr);
1688 #endif
1689 
1690 	if (!error && (scr->scr_flags & SCR_WAITACTIVE))
1691 		wakeup(scr);
1692 	return (error);
1693 }
1694 
1695 int
1696 wsdisplay_switch2(void *arg, int error, int waitok)
1697 {
1698 	struct wsdisplay_softc *sc = arg;
1699 	int no;
1700 	struct wsscreen *scr;
1701 
1702 	if (!ISSET(sc->sc_flags, SC_SWITCHPENDING)) {
1703 		printf("wsdisplay_switch2: not switching\n");
1704 		return (EINVAL);
1705 	}
1706 
1707 	no = sc->sc_screenwanted;
1708 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1709 		panic("wsdisplay_switch2: invalid screen %d", no);
1710 	scr = sc->sc_scr[no];
1711 	if (!scr) {
1712 		printf("wsdisplay_switch2: screen %d disappeared\n", no);
1713 		error = ENXIO;
1714 	}
1715 
1716 	if (error) {
1717 		/* try to recover, avoid recursion */
1718 
1719 		if (sc->sc_oldscreen == WSDISPLAY_NULLSCREEN) {
1720 			printf("wsdisplay_switch2: giving up\n");
1721 			sc->sc_focus = NULL;
1722 			CLR(sc->sc_flags, SC_SWITCHPENDING);
1723 			return (error);
1724 		}
1725 
1726 		sc->sc_screenwanted = sc->sc_oldscreen;
1727 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
1728 		return (wsdisplay_switch1(arg, 0, waitok));
1729 	}
1730 
1731 	sc->sc_focusidx = no;
1732 	sc->sc_focus = scr;
1733 
1734 #ifdef WSDISPLAY_COMPAT_RAWKBD
1735 	(void) wsdisplay_update_rawkbd(sc, scr);
1736 #endif
1737 	/* keyboard map??? */
1738 
1739 #ifdef WSDISPLAY_COMPAT_USL
1740 #define wsswitch_cb3 ((void (*)(void *, int, int))wsdisplay_switch3)
1741 	if (scr->scr_syncops) {
1742 		error = (*scr->scr_syncops->attach)(scr->scr_synccookie, waitok,
1743 		    sc->sc_isconsole && wsdisplay_cons_pollmode ?
1744 		      0 : wsswitch_cb3, sc);
1745 		if (error == EAGAIN) {
1746 			/* switch will be done asynchronously */
1747 			return (0);
1748 		}
1749 	}
1750 #endif
1751 
1752 	return (wsdisplay_switch3(sc, error, waitok));
1753 }
1754 
1755 int
1756 wsdisplay_switch1(void *arg, int error, int waitok)
1757 {
1758 	struct wsdisplay_softc *sc = arg;
1759 	int no;
1760 	struct wsscreen *scr;
1761 
1762 	if (!ISSET(sc->sc_flags, SC_SWITCHPENDING)) {
1763 		printf("wsdisplay_switch1: not switching\n");
1764 		return (EINVAL);
1765 	}
1766 
1767 	no = sc->sc_screenwanted;
1768 	if (no == WSDISPLAY_NULLSCREEN) {
1769 		CLR(sc->sc_flags, SC_SWITCHPENDING);
1770 		if (!error) {
1771 			sc->sc_focus = NULL;
1772 		}
1773 		wakeup(sc);
1774 		return (error);
1775 	}
1776 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1777 		panic("wsdisplay_switch1: invalid screen %d", no);
1778 	scr = sc->sc_scr[no];
1779 	if (!scr) {
1780 		printf("wsdisplay_switch1: screen %d disappeared\n", no);
1781 		error = ENXIO;
1782 	}
1783 
1784 	if (error) {
1785 		CLR(sc->sc_flags, SC_SWITCHPENDING);
1786 		return (error);
1787 	}
1788 
1789 #define wsswitch_cb2 ((void (*)(void *, int, int))wsdisplay_switch2)
1790 	error = (*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
1791 	    scr->scr_dconf->emulcookie, waitok,
1792 	    sc->sc_isconsole && wsdisplay_cons_pollmode ? 0 : wsswitch_cb2, sc);
1793 	if (error == EAGAIN) {
1794 		/* switch will be done asynchronously */
1795 		return (0);
1796 	}
1797 
1798 	return (wsdisplay_switch2(sc, error, waitok));
1799 }
1800 
1801 int
1802 wsdisplay_switch(struct device *dev, int no, int waitok)
1803 {
1804 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
1805 	int s, res = 0;
1806 	struct wsscreen *scr;
1807 
1808 	if (no != WSDISPLAY_NULLSCREEN) {
1809 		if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1810 			return (EINVAL);
1811 		if (sc->sc_scr[no] == NULL)
1812 			return (ENXIO);
1813 	}
1814 
1815 	s = spltty();
1816 
1817 	while (sc->sc_resumescreen != WSDISPLAY_NULLSCREEN && res == 0)
1818 		res = tsleep(&sc->sc_resumescreen, PCATCH, "wsrestore", 0);
1819 	if (res) {
1820 		splx(s);
1821 		return (res);
1822 	}
1823 
1824 	if ((sc->sc_focus && no == sc->sc_focusidx) ||
1825 	    (sc->sc_focus == NULL && no == WSDISPLAY_NULLSCREEN)) {
1826 		splx(s);
1827 		return (0);
1828 	}
1829 
1830 	if (ISSET(sc->sc_flags, SC_SWITCHPENDING)) {
1831 		splx(s);
1832 		return (EBUSY);
1833 	}
1834 
1835 	SET(sc->sc_flags, SC_SWITCHPENDING);
1836 	sc->sc_screenwanted = no;
1837 
1838 	splx(s);
1839 
1840 	scr = sc->sc_focus;
1841 	if (!scr) {
1842 		sc->sc_oldscreen = WSDISPLAY_NULLSCREEN;
1843 		return (wsdisplay_switch1(sc, 0, waitok));
1844 	} else
1845 		sc->sc_oldscreen = sc->sc_focusidx;
1846 
1847 #ifdef WSDISPLAY_COMPAT_USL
1848 #define wsswitch_cb1 ((void (*)(void *, int, int))wsdisplay_switch1)
1849 	if (scr->scr_syncops) {
1850 		res = (*scr->scr_syncops->detach)(scr->scr_synccookie, waitok,
1851 		    sc->sc_isconsole && wsdisplay_cons_pollmode ?
1852 		      0 : wsswitch_cb1, sc);
1853 		if (res == EAGAIN) {
1854 			/* switch will be done asynchronously */
1855 			return (0);
1856 		}
1857 	} else if (scr->scr_flags & SCR_GRAPHICS) {
1858 		/* no way to save state */
1859 		res = EBUSY;
1860 	}
1861 #endif
1862 
1863 #ifdef HAVE_WSMOUSED_SUPPORT
1864 	mouse_remove(scr);
1865 #endif
1866 
1867 	return (wsdisplay_switch1(sc, res, waitok));
1868 }
1869 
1870 void
1871 wsdisplay_reset(struct device *dev, enum wsdisplay_resetops op)
1872 {
1873 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
1874 	struct wsscreen *scr;
1875 
1876 	scr = sc->sc_focus;
1877 
1878 	if (!scr)
1879 		return;
1880 
1881 	switch (op) {
1882 	case WSDISPLAY_RESETEMUL:
1883 		(*scr->scr_dconf->wsemul->reset)(scr->scr_dconf->wsemulcookie,
1884 		    WSEMUL_RESET);
1885 		break;
1886 	case WSDISPLAY_RESETCLOSE:
1887 		wsdisplay_closescreen(sc, scr);
1888 		break;
1889 	}
1890 }
1891 
1892 #ifdef WSDISPLAY_COMPAT_USL
1893 /*
1894  * Interface for (external) VT switch / process synchronization code
1895  */
1896 int
1897 wsscreen_attach_sync(struct wsscreen *scr, const struct wscons_syncops *ops,
1898     void *cookie)
1899 {
1900 	if (scr->scr_syncops) {
1901 		/*
1902 		 * The screen is already claimed.
1903 		 * Check if the owner is still alive.
1904 		 */
1905 		if ((*scr->scr_syncops->check)(scr->scr_synccookie))
1906 			return (EBUSY);
1907 	}
1908 	scr->scr_syncops = ops;
1909 	scr->scr_synccookie = cookie;
1910 	return (0);
1911 }
1912 
1913 int
1914 wsscreen_detach_sync(struct wsscreen *scr)
1915 {
1916 	if (!scr->scr_syncops)
1917 		return (EINVAL);
1918 	scr->scr_syncops = NULL;
1919 	return (0);
1920 }
1921 
1922 int
1923 wsscreen_lookup_sync(struct wsscreen *scr,
1924     const struct wscons_syncops *ops, /* used as ID */
1925     void **cookiep)
1926 {
1927 	if (!scr->scr_syncops || ops != scr->scr_syncops)
1928 		return (EINVAL);
1929 	*cookiep = scr->scr_synccookie;
1930 	return (0);
1931 }
1932 #endif
1933 
1934 /*
1935  * Interface to virtual screen stuff
1936  */
1937 int
1938 wsdisplay_maxscreenidx(struct wsdisplay_softc *sc)
1939 {
1940 	return (WSDISPLAY_MAXSCREEN - 1);
1941 }
1942 
1943 int
1944 wsdisplay_screenstate(struct wsdisplay_softc *sc, int idx)
1945 {
1946 	if (idx < 0 || idx >= WSDISPLAY_MAXSCREEN)
1947 		return (EINVAL);
1948 	if (!sc->sc_scr[idx])
1949 		return (ENXIO);
1950 	return ((sc->sc_scr[idx]->scr_flags & SCR_OPEN) ? EBUSY : 0);
1951 }
1952 
1953 int
1954 wsdisplay_getactivescreen(struct wsdisplay_softc *sc)
1955 {
1956 	return (sc->sc_focus ? sc->sc_focusidx : WSDISPLAY_NULLSCREEN);
1957 }
1958 
1959 int
1960 wsscreen_switchwait(struct wsdisplay_softc *sc, int no)
1961 {
1962 	struct wsscreen *scr;
1963 	int s, res = 0;
1964 
1965 	if (no == WSDISPLAY_NULLSCREEN) {
1966 		s = spltty();
1967 		while (sc->sc_focus && res == 0) {
1968 			res = tsleep(sc, PCATCH, "wswait", 0);
1969 		}
1970 		splx(s);
1971 		return (res);
1972 	}
1973 
1974 	if (no < 0 || no >= WSDISPLAY_MAXSCREEN)
1975 		return (ENXIO);
1976 	scr = sc->sc_scr[no];
1977 	if (!scr)
1978 		return (ENXIO);
1979 
1980 	s = spltty();
1981 	if (scr != sc->sc_focus) {
1982 		scr->scr_flags |= SCR_WAITACTIVE;
1983 		res = tsleep(scr, PCATCH, "wswait2", 0);
1984 		if (scr != sc->sc_scr[no])
1985 			res = ENXIO; /* disappeared in the meantime */
1986 		else
1987 			scr->scr_flags &= ~SCR_WAITACTIVE;
1988 	}
1989 	splx(s);
1990 	return (res);
1991 }
1992 
1993 void
1994 wsdisplay_kbdholdscr(struct wsscreen *scr, int hold)
1995 {
1996 	if (hold)
1997 		scr->scr_hold_screen = 1;
1998 	else {
1999 		scr->scr_hold_screen = 0;
2000 		timeout_add(&scr->scr_tty->t_rstrt_to, 0); /* "immediate" */
2001 	}
2002 }
2003 
2004 void
2005 wsdisplay_kbdholdscreen(struct device *dev, int hold)
2006 {
2007 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)dev;
2008 	struct wsscreen *scr;
2009 
2010 	scr = sc->sc_focus;
2011 	if (scr != NULL && WSSCREEN_HAS_TTY(scr))
2012 		wsdisplay_kbdholdscr(scr, hold);
2013 }
2014 
2015 #if NWSKBD > 0
2016 void
2017 wsdisplay_set_console_kbd(struct wsevsrc *src)
2018 {
2019 	if (wsdisplay_console_device == NULL) {
2020 		src->me_dispdv = NULL;
2021 		return;
2022 	}
2023 #if NWSMUX > 0
2024 	if (wsmux_attach_sc((struct wsmux_softc *)
2025 			    wsdisplay_console_device->sc_input, src)) {
2026 		src->me_dispdv = NULL;
2027 		return;
2028 	}
2029 #else
2030 	wsdisplay_console_device->sc_input = src;
2031 #endif
2032 	src->me_dispdv = &wsdisplay_console_device->sc_dv;
2033 }
2034 
2035 #if NWSMUX == 0
2036 int
2037 wsdisplay_set_kbd(struct device *disp, struct wsevsrc *kbd)
2038 {
2039 	struct wsdisplay_softc *sc = (struct wsdisplay_softc *)disp;
2040 
2041 	if (sc->sc_input != NULL)
2042 		return (EBUSY);
2043 
2044 	sc->sc_input = kbd;
2045 
2046 	return (0);
2047 }
2048 #endif
2049 
2050 #endif /* NWSKBD > 0 */
2051 
2052 /*
2053  * Console interface.
2054  */
2055 void
2056 wsdisplay_cnputc(dev_t dev, int i)
2057 {
2058 	struct wsscreen_internal *dc;
2059 	char c = i;
2060 
2061 	if (!wsdisplay_console_initted)
2062 		return;
2063 
2064 	if (wsdisplay_console_device != NULL &&
2065 	    (wsdisplay_console_device->sc_scr[0] != NULL) &&
2066 	    (wsdisplay_console_device->sc_scr[0]->scr_flags & SCR_GRAPHICS))
2067 		return;
2068 
2069 	dc = &wsdisplay_console_conf;
2070 #ifdef HAVE_BURNER_SUPPORT
2071 	/*wsdisplay_burn(wsdisplay_console_device, WSDISPLAY_BURN_OUTPUT);*/
2072 #endif
2073 	(void)(*dc->wsemul->output)(dc->wsemulcookie, &c, 1, 1);
2074 }
2075 
2076 int
2077 wsdisplay_getc_dummy(dev_t dev)
2078 {
2079 	/* panic? */
2080 	return (0);
2081 }
2082 
2083 void
2084 wsdisplay_pollc(dev_t dev, int on)
2085 {
2086 
2087 	wsdisplay_cons_pollmode = on;
2088 
2089 	/* notify to fb drivers */
2090 	if (wsdisplay_console_device != NULL &&
2091 	    wsdisplay_console_device->sc_accessops->pollc != NULL)
2092 		(*wsdisplay_console_device->sc_accessops->pollc)
2093 		    (wsdisplay_console_device->sc_accesscookie, on);
2094 
2095 	/* notify to kbd drivers */
2096 	if (wsdisplay_cons_kbd_pollc)
2097 		(*wsdisplay_cons_kbd_pollc)(dev, on);
2098 }
2099 
2100 void
2101 wsdisplay_set_cons_kbd(int (*get)(dev_t), void (*poll)(dev_t, int),
2102     void (*bell)(dev_t, u_int, u_int, u_int))
2103 {
2104 	wsdisplay_cons.cn_getc = get;
2105 	wsdisplay_cons.cn_bell = bell;
2106 	wsdisplay_cons_kbd_pollc = poll;
2107 }
2108 
2109 void
2110 wsdisplay_unset_cons_kbd(void)
2111 {
2112 	wsdisplay_cons.cn_getc = wsdisplay_getc_dummy;
2113 	wsdisplay_cons.cn_bell = NULL;
2114 	wsdisplay_cons_kbd_pollc = NULL;
2115 }
2116 
2117 /*
2118  * Switch the console display to its first screen.
2119  */
2120 void
2121 wsdisplay_switchtoconsole(void)
2122 {
2123 	struct wsdisplay_softc *sc;
2124 	struct wsscreen *scr;
2125 
2126 	if (wsdisplay_console_device != NULL && cn_tab == &wsdisplay_cons) {
2127 		sc = wsdisplay_console_device;
2128 		if ((scr = sc->sc_scr[0]) == NULL)
2129 			return;
2130 		(*sc->sc_accessops->show_screen)(sc->sc_accesscookie,
2131 		    scr->scr_dconf->emulcookie, 0, NULL, NULL);
2132 	}
2133 }
2134 
2135 /*
2136  * Deal with the xserver doing driver in userland and thus screwing up suspend
2137  * and resume by switching away from it at suspend/resume time.
2138  *
2139  * these functions must be called from the MD suspend callback, since we may
2140  * need to sleep if we have a user (probably an X server) on a vt. therefore
2141  * this can't be a config_suspend() hook.
2142  */
2143 void
2144 wsdisplay_suspend(void)
2145 {
2146 	int	i;
2147 
2148 	for (i = 0; i < wsdisplay_cd.cd_ndevs; i++)
2149 		if (wsdisplay_cd.cd_devs[i] != NULL)
2150 			wsdisplay_suspend_device(wsdisplay_cd.cd_devs[i]);
2151 }
2152 
2153 void
2154 wsdisplay_suspend_device(struct device *dev)
2155 {
2156 	struct wsdisplay_softc	*sc = (struct wsdisplay_softc *)dev;
2157 	struct wsscreen		*scr;
2158 	int			 active, idx, ret = 0, s;
2159 
2160 	if ((active = wsdisplay_getactivescreen(sc)) == WSDISPLAY_NULLSCREEN)
2161 		return;
2162 
2163 	scr = sc->sc_scr[active];
2164 	/*
2165 	 * We want to switch out of graphics mode for the suspend, but
2166 	 * only if we're in WSDISPLAY_MODE_MAPPED.
2167 	 */
2168 retry:
2169 	idx = WSDISPLAY_MAXSCREEN;
2170 	if (scr->scr_flags & SCR_GRAPHICS &&
2171 	    (scr->scr_flags & SCR_DUMBFB) == 0) {
2172 		for (idx = 0; idx < WSDISPLAY_MAXSCREEN; idx++) {
2173 			if (sc->sc_scr[idx] == NULL || sc->sc_scr[idx] == scr)
2174 				continue;
2175 
2176 			if ((sc->sc_scr[idx]->scr_flags & SCR_GRAPHICS) == 0)
2177 				break;
2178 		}
2179 	}
2180 
2181 	/* if we don't have anything to switch to, we can't do anything */
2182 	if (idx == WSDISPLAY_MAXSCREEN)
2183 		return;
2184 
2185 	/*
2186 	 * we do a lot of magic here because we need to know that the
2187 	 * switch has completed before we return
2188 	 */
2189 	ret = wsdisplay_switch((struct device *)sc, idx, 1);
2190 	if (ret == EBUSY) {
2191 		/* XXX sleep on what's going on */
2192 		goto retry;
2193 	} else if (ret)
2194 		return;
2195 
2196 	s = spltty();
2197 	sc->sc_resumescreen = active; /* block other vt switches until resume */
2198 	splx(s);
2199 	/*
2200 	 * This will either return ENXIO (invalid (shouldn't happen) or
2201 	 * wsdisplay disappeared (problem solved)), or EINTR/ERESTART.
2202 	 * Not much we can do about the latter since we can't return to
2203 	 * userland.
2204 	 */
2205 	(void)wsscreen_switchwait(sc, idx);
2206 }
2207 
2208 void
2209 wsdisplay_resume(void)
2210 {
2211 	int	i;
2212 
2213 	for (i = 0; i < wsdisplay_cd.cd_ndevs; i++)
2214 		if (wsdisplay_cd.cd_devs[i] != NULL)
2215 			wsdisplay_resume_device(wsdisplay_cd.cd_devs[i]);
2216 }
2217 
2218 void
2219 wsdisplay_resume_device(struct device *dev)
2220 {
2221 	struct wsdisplay_softc	*sc = (struct wsdisplay_softc *)dev;
2222 	int			 idx, s;
2223 
2224 	if (sc->sc_resumescreen != WSDISPLAY_NULLSCREEN) {
2225 		s = spltty();
2226 		idx = sc->sc_resumescreen;
2227 		sc->sc_resumescreen = WSDISPLAY_NULLSCREEN;
2228 		wakeup(&sc->sc_resumescreen);
2229 		splx(s);
2230 		(void)wsdisplay_switch((struct device *)sc, idx, 1);
2231 	}
2232 }
2233 
2234 #ifdef HAVE_SCROLLBACK_SUPPORT
2235 void
2236 wsscrollback(void *arg, int op)
2237 {
2238 	struct wsdisplay_softc *sc = arg;
2239 	int lines;
2240 
2241 	if (sc->sc_focus == NULL)
2242 		return;
2243 
2244 	if (op == WSDISPLAY_SCROLL_RESET)
2245 		lines = 0;
2246 	else {
2247 		lines = sc->sc_focus->scr_dconf->scrdata->nrows - 1;
2248 		if (op == WSDISPLAY_SCROLL_BACKWARD)
2249 			lines = -lines;
2250 	}
2251 
2252 	if (sc->sc_accessops->scrollback) {
2253 		(*sc->sc_accessops->scrollback)(sc->sc_accesscookie,
2254 		    sc->sc_focus->scr_dconf->emulcookie, lines);
2255 	}
2256 }
2257 #endif
2258 
2259 #ifdef HAVE_BURNER_SUPPORT
2260 /*
2261  * Update screen burner behaviour after either a screen focus change or
2262  * a screen mode change.
2263  * This is needed to allow X11 to manage screen blanking without any
2264  * interference from the kernel.
2265  */
2266 void
2267 wsdisplay_burner_setup(struct wsdisplay_softc *sc, struct wsscreen *scr)
2268 {
2269 	if (scr->scr_flags & SCR_GRAPHICS) {
2270 		/* enable video _immediately_ if it needs to be... */
2271 		if (sc->sc_burnman)
2272 			wsdisplay_burner(sc);
2273 		/* ...and disable the burner while X is running */
2274 		if (sc->sc_burnout) {
2275 			timeout_del(&sc->sc_burner);
2276 			sc->sc_burnout = 0;
2277 		}
2278 	} else {
2279 		/* reenable the burner after exiting from X */
2280 		if (!sc->sc_burnman) {
2281 			sc->sc_burnout = sc->sc_burnoutintvl;
2282 			wsdisplay_burn(sc, sc->sc_burnflags);
2283 		}
2284 	}
2285 }
2286 
2287 void
2288 wsdisplay_burn(void *v, u_int flags)
2289 {
2290 	struct wsdisplay_softc *sc = v;
2291 
2292 	if ((flags & sc->sc_burnflags & (WSDISPLAY_BURN_OUTPUT |
2293 	    WSDISPLAY_BURN_KBD | WSDISPLAY_BURN_MOUSE)) &&
2294 	    sc->sc_accessops->burn_screen) {
2295 		if (sc->sc_burnout)
2296 			timeout_add(&sc->sc_burner, sc->sc_burnout);
2297 		if (sc->sc_burnman)
2298 			sc->sc_burnout = 0;
2299 	}
2300 }
2301 
2302 void
2303 wsdisplay_burner(void *v)
2304 {
2305 	struct wsdisplay_softc *sc = v;
2306 	int s;
2307 
2308 	if (sc->sc_accessops->burn_screen) {
2309 		(*sc->sc_accessops->burn_screen)(sc->sc_accesscookie,
2310 		    sc->sc_burnman, sc->sc_burnflags);
2311 		s = spltty();
2312 		if (sc->sc_burnman) {
2313 			sc->sc_burnout = sc->sc_burnoutintvl;
2314 			timeout_add(&sc->sc_burner, sc->sc_burnout);
2315 		} else
2316 			sc->sc_burnout = sc->sc_burninintvl;
2317 		sc->sc_burnman = !sc->sc_burnman;
2318 		splx(s);
2319 	}
2320 }
2321 #endif
2322 
2323 #ifdef HAVE_WSMOUSED_SUPPORT
2324 /*
2325  * wsmoused(8) support functions
2326  */
2327 
2328 /*
2329  * Main function, called from wsdisplay_cfg_ioctl.
2330  */
2331 int
2332 wsmoused(struct wsdisplay_softc *sc, caddr_t data, int flag, struct proc *p)
2333 {
2334 	struct wscons_event mouse_event = *(struct wscons_event *)data;
2335 
2336 	if (IS_MOTION_EVENT(mouse_event.type)) {
2337 		if (sc->sc_focus != NULL)
2338 			motion_event(sc->sc_focus, mouse_event.type,
2339 			    mouse_event.value);
2340 		return 0;
2341 	}
2342 	if (IS_BUTTON_EVENT(mouse_event.type)) {
2343 		if (sc->sc_focus != NULL) {
2344 			/* XXX tv_sec contains the number of clicks */
2345 			if (mouse_event.type ==
2346 			    WSCONS_EVENT_MOUSE_DOWN) {
2347 				button_event(sc->sc_focus,
2348 				    mouse_event.value,
2349 				    mouse_event.time.tv_sec);
2350 			} else
2351 				button_event(sc->sc_focus,
2352 				    mouse_event.value, 0);
2353 		}
2354 		return (0);
2355 	}
2356 	if (IS_CTRL_EVENT(mouse_event.type)) {
2357 		return ctrl_event(sc, mouse_event.type,
2358 		    mouse_event.value, p);
2359 	}
2360 	return -1;
2361 }
2362 
2363 /*
2364  * Mouse motion events
2365  */
2366 void
2367 motion_event(struct wsscreen *scr, u_int type, int value)
2368 {
2369 	switch (type) {
2370 	case WSCONS_EVENT_MOUSE_DELTA_X:
2371 		mouse_moverel(scr, value, 0);
2372 		break;
2373 	case WSCONS_EVENT_MOUSE_DELTA_Y:
2374 		mouse_moverel(scr, 0, -value);
2375 		break;
2376 #ifdef HAVE_SCROLLBACK_SUPPORT
2377 	case WSCONS_EVENT_MOUSE_DELTA_Z:
2378 		mouse_zaxis(scr, value);
2379 		break;
2380 #endif
2381 	default:
2382 		break;
2383 	}
2384 }
2385 
2386 /*
2387  * Button clicks events
2388  */
2389 void
2390 button_event(struct wsscreen *scr, int button, int clicks)
2391 {
2392 	switch (button) {
2393 	case MOUSE_COPY_BUTTON:
2394 		switch (clicks % 4) {
2395 		case 0: /* button is up */
2396 			mouse_copy_end(scr);
2397 			mouse_copy_selection(scr);
2398 			break;
2399 		case 1: /* single click */
2400 			mouse_copy_start(scr);
2401 			mouse_copy_selection(scr);
2402 			break;
2403 		case 2: /* double click */
2404 			mouse_copy_word(scr);
2405 			mouse_copy_selection(scr);
2406 			break;
2407 		case 3: /* triple click */
2408 			mouse_copy_line(scr);
2409 			mouse_copy_selection(scr);
2410 			break;
2411 		}
2412 		break;
2413 	case MOUSE_PASTE_BUTTON:
2414 		if (clicks != 0)
2415 			mouse_paste(scr);
2416 		break;
2417 	case MOUSE_EXTEND_BUTTON:
2418 		if (clicks != 0)
2419 			mouse_copy_extend_after(scr);
2420 		break;
2421 	default:
2422 		break;
2423 	}
2424 }
2425 
2426 /*
2427  * Control events
2428  */
2429 int
2430 ctrl_event(struct wsdisplay_softc *sc, u_int type, int value, struct proc *p)
2431 {
2432 	struct wsscreen *scr;
2433 	int i;
2434 
2435 	switch (type) {
2436 	case WSCONS_EVENT_WSMOUSED_OFF:
2437 		CLR(sc->sc_flags, SC_PASTE_AVAIL);
2438 		return (0);
2439 	case WSCONS_EVENT_WSMOUSED_ON:
2440 		if (!sc->sc_accessops->getchar)
2441 			/* no wsmoused(8) support in the display driver */
2442 			return (1);
2443 		allocate_copybuffer(sc);
2444 		CLR(sc->sc_flags, SC_PASTE_AVAIL);
2445 
2446 		for (i = 0 ; i < WSDISPLAY_DEFAULTSCREENS ; i++)
2447 			if ((scr = sc->sc_scr[i]) != NULL) {
2448 				scr->mouse =
2449 				    (WS_NCOLS(scr) * WS_NROWS(scr)) / 2;
2450 				scr->cursor = scr->mouse;
2451 				scr->cpy_start = 0;
2452 				scr->cpy_end = 0;
2453 				scr->orig_start = 0;
2454 				scr->orig_end = 0;
2455 				scr->mouse_flags = 0;
2456 			}
2457 		return (0);
2458 	default:	/* can't happen, really */
2459 		return 0;
2460 	}
2461 }
2462 
2463 void
2464 mouse_moverel(struct wsscreen *scr, int dx, int dy)
2465 {
2466 	struct wsscreen_internal *dconf = scr->scr_dconf;
2467 	u_int old_mouse = scr->mouse;
2468 	int mouse_col = scr->mouse % N_COLS(dconf);
2469 	int mouse_row = scr->mouse / N_COLS(dconf);
2470 
2471 	/* update position */
2472 	if (mouse_col + dx >= MAXCOL(dconf))
2473 		mouse_col = MAXCOL(dconf);
2474 	else {
2475 		if (mouse_col + dx <= 0)
2476 			mouse_col = 0;
2477 		else
2478 			mouse_col += dx;
2479 	}
2480 	if (mouse_row + dy >= MAXROW(dconf))
2481 		mouse_row = MAXROW(dconf);
2482 	else {
2483 		if (mouse_row + dy <= 0)
2484 			mouse_row = 0;
2485 		else
2486 			mouse_row += dy;
2487 	}
2488 	scr->mouse = mouse_row * N_COLS(dconf) + mouse_col;
2489 
2490 	/* if we have moved */
2491 	if (old_mouse != scr->mouse) {
2492 		/* XXX unblank screen if display.ms_act */
2493 		if (ISSET(scr->mouse_flags, SEL_IN_PROGRESS)) {
2494 			/* selection in progress */
2495 			mouse_copy_extend(scr);
2496 		} else {
2497 			inverse_char(scr, scr->mouse);
2498 			if (ISSET(scr->mouse_flags, MOUSE_VISIBLE))
2499 				inverse_char(scr, old_mouse);
2500 			else
2501 				SET(scr->mouse_flags, MOUSE_VISIBLE);
2502 		}
2503 	}
2504 }
2505 
2506 void
2507 inverse_char(struct wsscreen *scr, u_int pos)
2508 {
2509 	struct wsscreen_internal *dconf = scr->scr_dconf;
2510 	struct wsdisplay_charcell cell;
2511 	int fg, bg, ul;
2512 	int flags;
2513 	int tmp;
2514 	long attr;
2515 
2516 	GETCHAR(scr, pos, &cell);
2517 
2518 	(*dconf->emulops->unpack_attr)(dconf->emulcookie, cell.attr, &fg,
2519 	    &bg, &ul);
2520 
2521 	/*
2522 	 * Display the mouse cursor as a color inverted cell whenever
2523 	 * possible. If this is not possible, ask for the video reverse
2524 	 * attribute.
2525 	 */
2526 	flags = 0;
2527 	if (dconf->scrdata->capabilities & WSSCREEN_WSCOLORS) {
2528 		flags |= WSATTR_WSCOLORS;
2529 		tmp = fg;
2530 		fg = bg;
2531 		bg = tmp;
2532 	} else if (dconf->scrdata->capabilities & WSSCREEN_REVERSE) {
2533 		flags |= WSATTR_REVERSE;
2534 	}
2535 	if ((*dconf->emulops->alloc_attr)(dconf->emulcookie, fg, bg, flags |
2536 	    (ul ? WSATTR_UNDERLINE : 0), &attr) == 0) {
2537 		cell.attr = attr;
2538 		PUTCHAR(dconf, pos, cell.uc, cell.attr);
2539 	}
2540 }
2541 
2542 void
2543 inverse_region(struct wsscreen *scr, u_int start, u_int end)
2544 {
2545 	struct wsscreen_internal *dconf = scr->scr_dconf;
2546 	u_int current_pos;
2547 	u_int abs_end;
2548 
2549 	/* sanity check, useful because 'end' can be (u_int)-1 */
2550 	abs_end = N_COLS(dconf) * N_ROWS(dconf);
2551 	if (end > abs_end)
2552 		return;
2553 	current_pos = start;
2554 	while (current_pos <= end)
2555 		inverse_char(scr, current_pos++);
2556 }
2557 
2558 /*
2559  * Return the number of contiguous blank characters between the right margin
2560  * if border == 1 or between the next non-blank character and the current mouse
2561  * cursor if border == 0
2562  */
2563 u_int
2564 skip_spc_right(struct wsscreen *scr, int border)
2565 {
2566 	struct wsscreen_internal *dconf = scr->scr_dconf;
2567 	struct wsdisplay_charcell cell;
2568 	u_int current = scr->cpy_end;
2569 	u_int mouse_col = scr->cpy_end % N_COLS(dconf);
2570 	u_int limit = current + (N_COLS(dconf) - mouse_col - 1);
2571 	u_int res = 0;
2572 
2573 	while (GETCHAR(scr, current, &cell) == 0 && cell.uc == ' ' &&
2574 	    current <= limit) {
2575 		current++;
2576 		res++;
2577 	}
2578 	if (border == BORDER) {
2579 		if (current > limit)
2580 			return (res - 1);
2581 		else
2582 			return (0);
2583 	} else {
2584 		if (res != 0)
2585 			return (res - 1);
2586 		else
2587 			return (res);
2588 	}
2589 }
2590 
2591 /*
2592  * Return the number of contiguous blank characters between the first of the
2593  * contiguous blank characters and the current mouse cursor
2594  */
2595 u_int
2596 skip_spc_left(struct wsscreen *scr)
2597 {
2598 	struct wsscreen_internal *dconf = scr->scr_dconf;
2599 	struct wsdisplay_charcell cell;
2600 	u_int current = scr->cpy_start;
2601 	u_int mouse_col = scr->mouse % N_COLS(dconf);
2602 	u_int limit = current - mouse_col;
2603 	u_int res = 0;
2604 
2605 	while (GETCHAR(scr, current, &cell) == 0 && cell.uc == ' ' &&
2606 	    current >= limit) {
2607 		current--;
2608 		res++;
2609 	}
2610 	if (res != 0)
2611 		res--;
2612 	return (res);
2613 }
2614 
2615 /*
2616  * Class of characters
2617  * Stolen from xterm sources of the Xfree project (see cvs tag below)
2618  * $TOG: button.c /main/76 1997/07/30 16:56:19 kaleb $
2619  */
2620 static const int charClass[256] = {
2621 /* NUL  SOH  STX  ETX  EOT  ENQ  ACK  BEL */
2622     32,   1,   1,   1,   1,   1,   1,   1,
2623 /*  BS   HT   NL   VT   NP   CR   SO   SI */
2624      1,  32,   1,   1,   1,   1,   1,   1,
2625 /* DLE  DC1  DC2  DC3  DC4  NAK  SYN  ETB */
2626      1,   1,   1,   1,   1,   1,   1,   1,
2627 /* CAN   EM  SUB  ESC   FS   GS   RS   US */
2628      1,   1,   1,   1,   1,   1,   1,   1,
2629 /*  SP    !    "    #    $    %    &    ' */
2630     32,  33,  34,  35,  36,  37,  38,  39,
2631 /*   (    )    *    +    ,    -    .    / */
2632     40,  41,  42,  43,  44,  45,  46,  47,
2633 /*   0    1    2    3    4    5    6    7 */
2634     48,  48,  48,  48,  48,  48,  48,  48,
2635 /*   8    9    :    ;    <    =    >    ? */
2636     48,  48,  58,  59,  60,  61,  62,  63,
2637 /*   @    A    B    C    D    E    F    G */
2638     64,  48,  48,  48,  48,  48,  48,  48,
2639 /*   H    I    J    K    L    M    N    O */
2640     48,  48,  48,  48,  48,  48,  48,  48,
2641 /*   P    Q    R    S    T    U    V    W */
2642     48,  48,  48,  48,  48,  48,  48,  48,
2643 /*   X    Y    Z    [    \    ]    ^    _ */
2644     48,  48,  48,  91,  92,  93,  94,  48,
2645 /*   `    a    b    c    d    e    f    g */
2646     96,  48,  48,  48,  48,  48,  48,  48,
2647 /*   h    i    j    k    l    m    n    o */
2648     48,  48,  48,  48,  48,  48,  48,  48,
2649 /*   p    q    r    s    t    u    v    w */
2650     48,  48,  48,  48,  48,  48,  48,  48,
2651 /*   x    y    z    {    |    }    ~  DEL */
2652     48,  48,  48, 123, 124, 125, 126,   1,
2653 /* x80  x81  x82  x83  IND  NEL  SSA  ESA */
2654      1,   1,   1,   1,   1,   1,   1,   1,
2655 /* HTS  HTJ  VTS  PLD  PLU   RI  SS2  SS3 */
2656      1,   1,   1,   1,   1,   1,   1,   1,
2657 /* DCS  PU1  PU2  STS  CCH   MW  SPA  EPA */
2658      1,   1,   1,   1,   1,   1,   1,   1,
2659 /* x98  x99  x9A  CSI   ST  OSC   PM  APC */
2660      1,   1,   1,   1,   1,   1,   1,   1,
2661 /*   -    i   c/    L   ox   Y-    |   So */
2662    160, 161, 162, 163, 164, 165, 166, 167,
2663 /*  ..   c0   ip   <<    _        R0    - */
2664    168, 169, 170, 171, 172, 173, 174, 175,
2665 /*   o   +-    2    3    '    u   q|    . */
2666    176, 177, 178, 179, 180, 181, 182, 183,
2667 /*   ,    1    2   >>  1/4  1/2  3/4    ? */
2668    184, 185, 186, 187, 188, 189, 190, 191,
2669 /*  A`   A'   A^   A~   A:   Ao   AE   C, */
2670     48,  48,  48,  48,  48,  48,  48,  48,
2671 /*  E`   E'   E^   E:   I`   I'   I^   I: */
2672     48,  48,  48,  48,  48,  48,  48,  48,
2673 /*  D-   N~   O`   O'   O^   O~   O:    X */
2674     48,  48,  48,  48,  48,  48,  48, 216,
2675 /*  O/   U`   U'   U^   U:   Y'    P    B */
2676     48,  48,  48,  48,  48,  48,  48,  48,
2677 /*  a`   a'   a^   a~   a:   ao   ae   c, */
2678     48,  48,  48,  48,  48,  48,  48,  48,
2679 /*  e`   e'   e^   e:    i`  i'   i^   i: */
2680     48,  48,  48,  48,  48,  48,  48,  48,
2681 /*   d   n~   o`   o'   o^   o~   o:   -: */
2682     48,  48,  48,  48,  48,  48,  48,  248,
2683 /*  o/   u`   u'   u^   u:   y'    P   y: */
2684     48,  48,  48,  48,  48,  48,  48,  48
2685 };
2686 
2687 /*
2688  * Find the first blank beginning after the current cursor position
2689  */
2690 u_int
2691 skip_char_right(struct wsscreen *scr, u_int offset)
2692 {
2693 	struct wsscreen_internal *dconf = scr->scr_dconf;
2694 	struct wsdisplay_charcell cell;
2695 	u_int current = offset;
2696 	u_int limit = current +
2697 	    (N_COLS(dconf) - (scr->mouse % N_COLS(dconf)) - 1);
2698 	u_int class;
2699 	u_int res = 0;
2700 
2701 	GETCHAR(scr, current, &cell);
2702 	class = charClass[cell.uc & 0xff];
2703 	while (GETCHAR(scr, current, &cell) == 0 &&
2704 	    charClass[cell.uc & 0xff] == class && current <= limit) {
2705 		current++;
2706 		res++;
2707 	}
2708 	if (res != 0)
2709 		res--;
2710 	return (res);
2711 }
2712 
2713 /*
2714  * Find the first non-blank character before the cursor position
2715  */
2716 u_int
2717 skip_char_left(struct wsscreen *scr, u_int offset)
2718 {
2719 	struct wsscreen_internal *dconf = scr->scr_dconf;
2720 	struct wsdisplay_charcell cell;
2721 	u_int current = offset;
2722 	u_int limit = current - (scr->mouse % N_COLS(dconf));
2723 	u_int class;
2724 	u_int res = 0;
2725 
2726 	GETCHAR(scr, current, &cell);
2727 	class = charClass[cell.uc & 0xff];
2728 	while (GETCHAR(scr, current, &cell) == 0 &&
2729 	    charClass[cell.uc & 0xff] == class && current >= limit) {
2730 		current--;
2731 		res++;
2732 	}
2733 	if (res != 0)
2734 		res--;
2735 	return (res);
2736 }
2737 
2738 /*
2739  * Compare character classes
2740  */
2741 u_int
2742 class_cmp(struct wsscreen *scr, u_int first, u_int second)
2743 {
2744 	struct wsdisplay_charcell cell;
2745 	u_int first_class;
2746 	u_int second_class;
2747 
2748 	if (GETCHAR(scr, first, &cell) != 0)
2749 		return (1);
2750 	first_class = charClass[cell.uc & 0xff];
2751 	if (GETCHAR(scr, second, &cell) != 0)
2752 		return (1);
2753 	second_class = charClass[cell.uc & 0xff];
2754 
2755 	if (first_class != second_class)
2756 		return (1);
2757 	else
2758 		return (0);
2759 }
2760 
2761 /*
2762  * Beginning of a copy operation
2763  */
2764 void
2765 mouse_copy_start(struct wsscreen *scr)
2766 {
2767 	u_int right;
2768 
2769 	/* if no selection, then that's the first one */
2770 	SET(scr->sc->sc_flags, SC_PASTE_AVAIL);
2771 
2772 	/* remove the previous selection */
2773 	if (ISSET(scr->mouse_flags, SEL_EXISTS))
2774 		remove_selection(scr);
2775 
2776 	/* initial show of the cursor */
2777 	if (!ISSET(scr->mouse_flags, MOUSE_VISIBLE))
2778 		inverse_char(scr, scr->mouse);
2779 
2780 	scr->cpy_start = scr->cpy_end = scr->mouse;
2781 	scr->orig_start = scr->cpy_start;
2782 	scr->orig_end = scr->cpy_end;
2783 	scr->cursor = scr->cpy_end + 1; /* init value */
2784 
2785 	/* useful later, in mouse_copy_extend */
2786 	right = skip_spc_right(scr, BORDER);
2787 	if (right)
2788 		SET(scr->mouse_flags, BLANK_TO_EOL);
2789 
2790 	SET(scr->mouse_flags, SEL_IN_PROGRESS | SEL_EXISTS | SEL_BY_CHAR);
2791 	CLR(scr->mouse_flags, SEL_BY_WORD | SEL_BY_LINE);
2792 	CLR(scr->mouse_flags, MOUSE_VISIBLE); /* cursor hidden in selection */
2793 }
2794 
2795 /*
2796  * Copy of the word under the cursor
2797  */
2798 void
2799 mouse_copy_word(struct wsscreen *scr)
2800 {
2801 	struct wsdisplay_charcell cell;
2802 	u_int right;
2803 	u_int left;
2804 
2805 	if (ISSET(scr->mouse_flags, SEL_EXISTS))
2806 		remove_selection(scr);
2807 
2808 	if (ISSET(scr->mouse_flags, MOUSE_VISIBLE))
2809 		inverse_char(scr, scr->mouse);
2810 
2811 	scr->cpy_start = scr->cpy_end = scr->mouse;
2812 
2813 	if (GETCHAR(scr, scr->mouse, &cell) == 0 &&
2814 	    IS_ALPHANUM(cell.uc)) {
2815 		right = skip_char_right(scr, scr->cpy_end);
2816 		left = skip_char_left(scr, scr->cpy_start);
2817 	} else {
2818 		right = skip_spc_right(scr, NO_BORDER);
2819 		left = skip_spc_left(scr);
2820 	}
2821 
2822 	scr->cpy_start -= left;
2823 	scr->cpy_end += right;
2824 	scr->orig_start = scr->cpy_start;
2825 	scr->orig_end = scr->cpy_end;
2826 	scr->cursor = scr->cpy_end + 1; /* init value, never happen */
2827 	inverse_region(scr, scr->cpy_start, scr->cpy_end);
2828 
2829 	SET(scr->mouse_flags, SEL_IN_PROGRESS | SEL_EXISTS | SEL_BY_WORD);
2830 	CLR(scr->mouse_flags, SEL_BY_CHAR | SEL_BY_LINE);
2831 	/* mouse cursor hidden in the selection */
2832 	CLR(scr->mouse_flags, BLANK_TO_EOL | MOUSE_VISIBLE);
2833 }
2834 
2835 /*
2836  * Copy of the current line
2837  */
2838 void
2839 mouse_copy_line(struct wsscreen *scr)
2840 {
2841 	struct wsscreen_internal *dconf = scr->scr_dconf;
2842 	u_int row = scr->mouse / N_COLS(dconf);
2843 
2844 	if (ISSET(scr->mouse_flags, SEL_EXISTS))
2845 		remove_selection(scr);
2846 
2847 	if (ISSET(scr->mouse_flags, MOUSE_VISIBLE))
2848 		inverse_char(scr, scr->mouse);
2849 
2850 	scr->cpy_start = row * N_COLS(dconf);
2851 	scr->cpy_end = scr->cpy_start + (N_COLS(dconf) - 1);
2852 	scr->orig_start = scr->cpy_start;
2853 	scr->orig_end = scr->cpy_end;
2854 	scr->cursor = scr->cpy_end + 1;
2855 	inverse_region(scr, scr->cpy_start, scr->cpy_end);
2856 
2857 	SET(scr->mouse_flags, SEL_IN_PROGRESS | SEL_EXISTS | SEL_BY_LINE);
2858 	CLR(scr->mouse_flags, SEL_BY_CHAR | SEL_BY_WORD);
2859 	/* mouse cursor hidden in the selection */
2860 	CLR(scr->mouse_flags, BLANK_TO_EOL | MOUSE_VISIBLE);
2861 }
2862 
2863 /*
2864  * End of a copy operation
2865  */
2866 void
2867 mouse_copy_end(struct wsscreen *scr)
2868 {
2869 	CLR(scr->mouse_flags, SEL_IN_PROGRESS);
2870 	if (ISSET(scr->mouse_flags, SEL_BY_WORD) ||
2871 	    ISSET(scr->mouse_flags, SEL_BY_LINE)) {
2872 		if (scr->cursor != scr->cpy_end + 1)
2873 			inverse_char(scr, scr->cursor);
2874 		scr->cursor = scr->cpy_end + 1;
2875 	}
2876 }
2877 
2878 
2879 /*
2880  * Generic selection extend function
2881  */
2882 void
2883 mouse_copy_extend(struct wsscreen *scr)
2884 {
2885 	if (ISSET(scr->mouse_flags, SEL_BY_CHAR))
2886 		mouse_copy_extend_char(scr);
2887 	if (ISSET(scr->mouse_flags, SEL_BY_WORD))
2888 		mouse_copy_extend_word(scr);
2889 	if (ISSET(scr->mouse_flags, SEL_BY_LINE))
2890 		mouse_copy_extend_line(scr);
2891 }
2892 
2893 /*
2894  * Extend a selected region, character by character
2895  */
2896 void
2897 mouse_copy_extend_char(struct wsscreen *scr)
2898 {
2899 	u_int right;
2900 
2901 	if (!ISSET(scr->mouse_flags, SEL_EXT_AFTER)) {
2902 		if (ISSET(scr->mouse_flags, BLANK_TO_EOL)) {
2903 			/*
2904 			 * First extension of selection. We handle special
2905 			 * cases of blank characters to eol
2906 			 */
2907 
2908 			right = skip_spc_right(scr, BORDER);
2909 			if (scr->mouse > scr->orig_start) {
2910 				/* the selection goes to the lower part of
2911 				   the screen */
2912 
2913 				/* remove the previous cursor, start of
2914 				   selection is now next line */
2915 				inverse_char(scr, scr->cpy_start);
2916 				scr->cpy_start += (right + 1);
2917 				scr->cpy_end = scr->cpy_start;
2918 				scr->orig_start = scr->cpy_start;
2919 				/* simulate the initial mark */
2920 				inverse_char(scr, scr->cpy_start);
2921 			} else {
2922 				/* the selection goes to the upper part
2923 				   of the screen */
2924 				/* remove the previous cursor, start of
2925 				   selection is now at the eol */
2926 				inverse_char(scr, scr->cpy_start);
2927 				scr->orig_start += (right + 1);
2928 				scr->cpy_start = scr->orig_start - 1;
2929 				scr->cpy_end = scr->orig_start - 1;
2930 				/* simulate the initial mark */
2931 				inverse_char(scr, scr->cpy_start);
2932 			}
2933 			CLR(scr->mouse_flags, BLANK_TO_EOL);
2934 		}
2935 
2936 		if (scr->mouse < scr->orig_start &&
2937 		    scr->cpy_end >= scr->orig_start) {
2938 			/* we go to the upper part of the screen */
2939 
2940 			/* reverse the old selection region */
2941 			remove_selection(scr);
2942 			scr->cpy_end = scr->orig_start - 1;
2943 			scr->cpy_start = scr->orig_start;
2944 		}
2945 		if (scr->cpy_start < scr->orig_start &&
2946 		    scr->mouse >= scr->orig_start) {
2947 			/* we go to the lower part of the screen */
2948 
2949 			/* reverse the old selection region */
2950 
2951 			remove_selection(scr);
2952 			scr->cpy_start = scr->orig_start;
2953 			scr->cpy_end = scr->orig_start - 1;
2954 		}
2955 		/* restore flags cleared in remove_selection() */
2956 		SET(scr->mouse_flags, SEL_IN_PROGRESS | SEL_EXISTS);
2957 	}
2958 
2959 	if (scr->mouse >= scr->orig_start) {
2960 		/* lower part of the screen */
2961 		if (scr->mouse > scr->cpy_end) {
2962 			/* extending selection */
2963 			inverse_region(scr, scr->cpy_end + 1, scr->mouse);
2964 		} else {
2965 			/* reducing selection */
2966 			inverse_region(scr, scr->mouse + 1, scr->cpy_end);
2967 		}
2968 		scr->cpy_end = scr->mouse;
2969 	} else {
2970 		/* upper part of the screen */
2971 		if (scr->mouse < scr->cpy_start) {
2972 			/* extending selection */
2973 			inverse_region(scr, scr->mouse, scr->cpy_start - 1);
2974 		} else {
2975 			/* reducing selection */
2976 			inverse_region(scr, scr->cpy_start, scr->mouse - 1);
2977 		}
2978 		scr->cpy_start = scr->mouse;
2979 	}
2980 }
2981 
2982 /*
2983  * Extend a selected region, word by word
2984  */
2985 void
2986 mouse_copy_extend_word(struct wsscreen *scr)
2987 {
2988 	u_int old_cpy_end;
2989 	u_int old_cpy_start;
2990 
2991 	if (!ISSET(scr->mouse_flags, SEL_EXT_AFTER)) {
2992 		/* remove cursor in selection (black one) */
2993 		if (scr->cursor != scr->cpy_end + 1)
2994 			inverse_char(scr, scr->cursor);
2995 
2996 		/* now, switch between lower and upper part of the screen */
2997 		if (scr->mouse < scr->orig_start &&
2998 		    scr->cpy_end >= scr->orig_start) {
2999 			/* going to the upper part of the screen */
3000 			inverse_region(scr, scr->orig_end + 1, scr->cpy_end);
3001 			scr->cpy_end = scr->orig_end;
3002 		}
3003 
3004 		if (scr->mouse > scr->orig_end &&
3005 		    scr->cpy_start <= scr->orig_start) {
3006 			/* going to the lower part of the screen */
3007 			inverse_region(scr, scr->cpy_start,
3008 			    scr->orig_start - 1);
3009 			scr->cpy_start = scr->orig_start;
3010 		}
3011 	}
3012 
3013 	if (scr->mouse >= scr->orig_start) {
3014 		/* lower part of the screen */
3015 		if (scr->mouse > scr->cpy_end) {
3016 			/* extending selection */
3017 			old_cpy_end = scr->cpy_end;
3018 			scr->cpy_end = scr->mouse +
3019 			    skip_char_right(scr, scr->mouse);
3020 			inverse_region(scr, old_cpy_end + 1, scr->cpy_end);
3021 		} else {
3022 			if (class_cmp(scr, scr->mouse, scr->mouse + 1)) {
3023 				/* reducing selection (remove last word) */
3024 				old_cpy_end = scr->cpy_end;
3025 				scr->cpy_end = scr->mouse;
3026 				inverse_region(scr, scr->cpy_end + 1,
3027 				    old_cpy_end);
3028 			} else {
3029 				old_cpy_end = scr->cpy_end;
3030 				scr->cpy_end = scr->mouse +
3031 				    skip_char_right(scr, scr->mouse);
3032 				if (scr->cpy_end != old_cpy_end) {
3033 					/* reducing selection, from the end of
3034 					 * next word */
3035 					inverse_region(scr, scr->cpy_end + 1,
3036 					    old_cpy_end);
3037 				}
3038 			}
3039 		}
3040 	} else {
3041 		/* upper part of the screen */
3042 		if (scr->mouse < scr->cpy_start) {
3043 			/* extending selection */
3044 			old_cpy_start = scr->cpy_start;
3045 			scr->cpy_start = scr->mouse -
3046 			    skip_char_left(scr, scr->mouse);
3047 			inverse_region(scr, scr->cpy_start, old_cpy_start - 1);
3048 		} else {
3049 			if (class_cmp(scr, scr->mouse - 1, scr->mouse)) {
3050 				/* reducing selection (remove last word) */
3051 				old_cpy_start = scr->cpy_start;
3052 				scr->cpy_start = scr->mouse;
3053 				inverse_region(scr, old_cpy_start,
3054 				    scr->cpy_start - 1);
3055 			} else {
3056 				old_cpy_start = scr->cpy_start;
3057 				scr->cpy_start = scr->mouse -
3058 				    skip_char_left(scr, scr->mouse);
3059 				if (scr->cpy_start != old_cpy_start) {
3060 					inverse_region(scr, old_cpy_start,
3061 					    scr->cpy_start - 1);
3062 				}
3063 			}
3064 		}
3065 	}
3066 
3067 	if (!ISSET(scr->mouse_flags, SEL_EXT_AFTER)) {
3068 		/* display new cursor */
3069 		scr->cursor = scr->mouse;
3070 		inverse_char(scr, scr->cursor);
3071 	}
3072 }
3073 
3074 /*
3075  * Extend a selected region, line by line
3076  */
3077 void
3078 mouse_copy_extend_line(struct wsscreen *scr)
3079 {
3080 	struct wsscreen_internal *dconf = scr->scr_dconf;
3081 	u_int old_row;
3082 	u_int new_row;
3083 	u_int old_cpy_start;
3084 	u_int old_cpy_end;
3085 
3086 	if (!ISSET(scr->mouse_flags, SEL_EXT_AFTER)) {
3087 		/* remove cursor in selection (black one) */
3088 		if (scr->cursor != scr->cpy_end + 1)
3089 			inverse_char(scr, scr->cursor);
3090 
3091 		/* now, switch between lower and upper part of the screen */
3092 		if (scr->mouse < scr->orig_start &&
3093 		    scr->cpy_end >= scr->orig_start) {
3094 			/* going to the upper part of the screen */
3095 			inverse_region(scr, scr->orig_end + 1, scr->cpy_end);
3096 			scr->cpy_end = scr->orig_end;
3097 		}
3098 
3099 		if (scr->mouse > scr->orig_end &&
3100 		    scr->cpy_start <= scr->orig_start) {
3101 			/* going to the lower part of the screen */
3102 			inverse_region(scr, scr->cpy_start,
3103 			    scr->orig_start - 1);
3104 			scr->cpy_start = scr->orig_start;
3105 		}
3106 	}
3107 
3108 	if (scr->mouse >= scr->orig_start) {
3109 		/* lower part of the screen */
3110 		if (scr->cursor == scr->cpy_end + 1)
3111 			scr->cursor = scr->cpy_end;
3112 		old_row = scr->cursor / N_COLS(dconf);
3113 		new_row = scr->mouse / N_COLS(dconf);
3114 		old_cpy_end = scr->cpy_end;
3115 		scr->cpy_end = new_row * N_COLS(dconf) + MAXCOL(dconf);
3116 		if (new_row > old_row)
3117 			inverse_region(scr, old_cpy_end + 1, scr->cpy_end);
3118 		else if (new_row < old_row)
3119 			inverse_region(scr, scr->cpy_end + 1, old_cpy_end);
3120 	} else {
3121 		/* upper part of the screen */
3122 		old_row = scr->cursor / N_COLS(dconf);
3123 		new_row = scr->mouse / N_COLS(dconf);
3124 		old_cpy_start = scr->cpy_start;
3125 		scr->cpy_start = new_row * N_COLS(dconf);
3126 		if (new_row < old_row)
3127 			inverse_region(scr, scr->cpy_start, old_cpy_start - 1);
3128 		else if (new_row > old_row)
3129 			inverse_region(scr, old_cpy_start, scr->cpy_start - 1);
3130 	}
3131 
3132 	if (!ISSET(scr->mouse_flags, SEL_EXT_AFTER)) {
3133 		/* display new cursor */
3134 		scr->cursor = scr->mouse;
3135 		inverse_char(scr, scr->cursor);
3136 	}
3137 }
3138 
3139 /*
3140  * Add an extension to a selected region, word by word
3141  */
3142 void
3143 mouse_copy_extend_after(struct wsscreen *scr)
3144 {
3145 	u_int start_dist;
3146 	u_int end_dist;
3147 
3148 	if (ISSET(scr->mouse_flags, SEL_EXISTS)) {
3149 		SET(scr->mouse_flags, SEL_EXT_AFTER);
3150 		mouse_hide(scr); /* hide current cursor */
3151 
3152 		if (scr->cpy_start > scr->mouse)
3153 			start_dist = scr->cpy_start - scr->mouse;
3154 		else
3155 			start_dist = scr->mouse - scr->cpy_start;
3156 		if (scr->mouse > scr->cpy_end)
3157 			end_dist = scr->mouse - scr->cpy_end;
3158 		else
3159 			end_dist = scr->cpy_end - scr->mouse;
3160 		if (start_dist < end_dist) {
3161 			/* upper part of the screen*/
3162 			scr->orig_start = scr->mouse + 1;
3163 			/* only used in mouse_copy_extend_line() */
3164 			scr->cursor = scr->cpy_start;
3165 		} else {
3166 			/* lower part of the screen */
3167 			scr->orig_start = scr->mouse;
3168 			/* only used in mouse_copy_extend_line() */
3169 			scr->cursor = scr->cpy_end;
3170 		}
3171 		if (ISSET(scr->mouse_flags, SEL_BY_CHAR))
3172 			mouse_copy_extend_char(scr);
3173 		if (ISSET(scr->mouse_flags, SEL_BY_WORD))
3174 			mouse_copy_extend_word(scr);
3175 		if (ISSET(scr->mouse_flags, SEL_BY_LINE))
3176 			mouse_copy_extend_line(scr);
3177 		mouse_copy_selection(scr);
3178 	}
3179 }
3180 
3181 void
3182 mouse_hide(struct wsscreen *scr)
3183 {
3184 	if (ISSET(scr->mouse_flags, MOUSE_VISIBLE)) {
3185 		inverse_char(scr, scr->mouse);
3186 		CLR(scr->mouse_flags, MOUSE_VISIBLE);
3187 	}
3188 }
3189 
3190 /*
3191  * Remove a previously selected region
3192  */
3193 void
3194 remove_selection(struct wsscreen *scr)
3195 {
3196 	if (ISSET(scr->mouse_flags, SEL_EXT_AFTER)) {
3197 		/* reset the flag indicating an extension of selection */
3198 		CLR(scr->mouse_flags, SEL_EXT_AFTER);
3199 	}
3200 	inverse_region(scr, scr->cpy_start, scr->cpy_end);
3201 	CLR(scr->mouse_flags, SEL_IN_PROGRESS | SEL_EXISTS);
3202 }
3203 
3204 /*
3205  * Put the current visual selection in the selection buffer
3206  */
3207 void
3208 mouse_copy_selection(struct wsscreen *scr)
3209 {
3210 	struct wsscreen_internal *dconf = scr->scr_dconf;
3211 	struct wsdisplay_charcell cell;
3212 	u_int current = 0;
3213 	u_int blank = current;
3214 	u_int buf_end = (N_COLS(dconf) + 1) * N_ROWS(dconf);
3215 	u_int sel_cur;
3216 	u_int sel_end;
3217 
3218 	sel_cur = scr->cpy_start;
3219 	sel_end = scr->cpy_end;
3220 
3221 	while (sel_cur <= sel_end && current < buf_end - 1) {
3222 		if (GETCHAR(scr, sel_cur, &cell) != 0)
3223 			break;
3224 		scr->sc->sc_copybuffer[current] = cell.uc;
3225 		if (!IS_SPACE(cell.uc))
3226 			blank = current + 1; /* first blank after non-blank */
3227 		current++;
3228 		if (sel_cur % N_COLS(dconf) == MAXCOL(dconf)) {
3229 			/*
3230 			 * If we are on the last column of the screen,
3231 			 * insert a carriage return.
3232 			 */
3233 			scr->sc->sc_copybuffer[blank] = '\r';
3234 			current = ++blank;
3235 		}
3236 		sel_cur++;
3237 	}
3238 
3239 	scr->sc->sc_copybuffer[current] = '\0';
3240 }
3241 
3242 /*
3243  * Paste the current selection
3244  */
3245 void
3246 mouse_paste(struct wsscreen *scr)
3247 {
3248 	char *current = scr->sc->sc_copybuffer;
3249 	struct tty *tp;
3250 	u_int len;
3251 
3252 	if (ISSET(scr->sc->sc_flags, SC_PASTE_AVAIL)) {
3253 		if (!WSSCREEN_HAS_TTY(scr))
3254 			return;
3255 
3256 		tp = scr->scr_tty;
3257 		for (len = strlen(scr->sc->sc_copybuffer); len != 0; len--)
3258 			(*linesw[tp->t_line].l_rint)(*current++, tp);
3259 	}
3260 }
3261 
3262 #ifdef HAVE_SCROLLBACK_SUPPORT
3263 /*
3264  * Handle the z axis.
3265  * The z axis (roller or wheel) is mapped by default to scrollback.
3266  */
3267 void
3268 mouse_zaxis(struct wsscreen *scr, int z)
3269 {
3270 	if (z < 0)
3271 		wsscrollback(scr->sc, WSDISPLAY_SCROLL_BACKWARD);
3272 	else
3273 		wsscrollback(scr->sc, WSDISPLAY_SCROLL_FORWARD);
3274 }
3275 #endif
3276 
3277 /*
3278  * Allocate the copy buffer. The size is:
3279  * (cols + 1) * (rows)
3280  * (+1 for '\n' at the end of lines),
3281  * where cols and rows are the maximum of column and rows of all screens.
3282  */
3283 void
3284 allocate_copybuffer(struct wsdisplay_softc *sc)
3285 {
3286 	int nscreens = sc->sc_scrdata->nscreens;
3287 	int i, s;
3288 	const struct wsscreen_descr **screens_list = sc->sc_scrdata->screens;
3289 	const struct wsscreen_descr *current;
3290 	u_int size = sc->sc_copybuffer_size;
3291 
3292 	s = spltty();
3293 	for (i = 0; i < nscreens; i++) {
3294 		current = *screens_list;
3295 		if ((current->ncols + 1) * current->nrows > size)
3296 			size = (current->ncols + 1) * current->nrows;
3297 		screens_list++;
3298 	}
3299 	if (size != sc->sc_copybuffer_size && sc->sc_copybuffer_size != 0) {
3300 		bzero(sc->sc_copybuffer, sc->sc_copybuffer_size);
3301 		free(sc->sc_copybuffer, M_DEVBUF, 0);
3302 	}
3303 	if ((sc->sc_copybuffer = (char *)malloc(size, M_DEVBUF, M_NOWAIT)) ==
3304 	    NULL) {
3305 		printf("%s: couldn't allocate copy buffer\n",
3306 		    sc->sc_dv.dv_xname);
3307 		size = 0;
3308 	}
3309 	sc->sc_copybuffer_size = size;
3310 	splx(s);
3311 }
3312 
3313 /* Remove selection and cursor on current screen */
3314 void
3315 mouse_remove(struct wsscreen *scr)
3316 {
3317 	if (ISSET(scr->mouse_flags, SEL_EXISTS))
3318 		remove_selection(scr);
3319 
3320 	mouse_hide(scr);
3321 }
3322 
3323 #endif /* HAVE_WSMOUSED_SUPPORT */
3324