]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/vt/vt_core.c
MFC 262785 263183 264182 264999 265391 265392 265395 265397 265398 265402 265403
[FreeBSD/stable/10.git] / sys / dev / vt / vt_core.c
1 /*-
2  * Copyright (c) 2009, 2013 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Ed Schouten under sponsorship from the
6  * FreeBSD Foundation.
7  *
8  * Portions of this software were developed by Oleksandr Rybalko
9  * under sponsorship from the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include "opt_compat.h"
37
38 #include <sys/param.h>
39 #include <sys/consio.h>
40 #include <sys/eventhandler.h>
41 #include <sys/fbio.h>
42 #include <sys/kbio.h>
43 #include <sys/kdb.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/priv.h>
49 #include <sys/proc.h>
50 #include <sys/reboot.h>
51 #include <sys/systm.h>
52 #include <sys/terminal.h>
53
54 #include <dev/kbd/kbdreg.h>
55 #include <dev/vt/vt.h>
56
57 #if defined(__i386__) || defined(__amd64__)
58 #include <machine/psl.h>
59 #include <machine/frame.h>
60 #endif
61
62 static tc_bell_t        vtterm_bell;
63 static tc_cursor_t      vtterm_cursor;
64 static tc_putchar_t     vtterm_putchar;
65 static tc_fill_t        vtterm_fill;
66 static tc_copy_t        vtterm_copy;
67 static tc_param_t       vtterm_param;
68 static tc_done_t        vtterm_done;
69
70 static tc_cnprobe_t     vtterm_cnprobe;
71 static tc_cngetc_t      vtterm_cngetc;
72
73 static tc_opened_t      vtterm_opened;
74 static tc_ioctl_t       vtterm_ioctl;
75 static tc_mmap_t        vtterm_mmap;
76
77 const struct terminal_class vt_termclass = {
78         .tc_bell        = vtterm_bell,
79         .tc_cursor      = vtterm_cursor,
80         .tc_putchar     = vtterm_putchar,
81         .tc_fill        = vtterm_fill,
82         .tc_copy        = vtterm_copy,
83         .tc_param       = vtterm_param,
84         .tc_done        = vtterm_done,
85
86         .tc_cnprobe     = vtterm_cnprobe,
87         .tc_cngetc      = vtterm_cngetc,
88
89         .tc_opened      = vtterm_opened,
90         .tc_ioctl       = vtterm_ioctl,
91         .tc_mmap        = vtterm_mmap,
92 };
93
94 /*
95  * Use a constant timer of 25 Hz to redraw the screen.
96  *
97  * XXX: In theory we should only fire up the timer when there is really
98  * activity. Unfortunately we cannot always start timers. We really
99  * don't want to process kernel messages synchronously, because it
100  * really slows down the system.
101  */
102 #define VT_TIMERFREQ    25
103
104 /* Bell pitch/duration. */
105 #define VT_BELLDURATION ((5 * hz + 99) / 100)
106 #define VT_BELLPITCH    800
107
108 #define VT_LOCK(vd)     mtx_lock(&(vd)->vd_lock)
109 #define VT_UNLOCK(vd)   mtx_unlock(&(vd)->vd_lock)
110
111 #define VT_UNIT(vw)     ((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \
112                         (vw)->vw_number)
113
114 /* XXX while syscons is here. */
115 int sc_txtmouse_no_retrace_wait;
116
117 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD, 0, "vt(9) parameters");
118 VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)");
119 VT_SYSCTL_INT(debug, 0, "vt(9) debug level");
120 VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode");
121 VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend");
122
123 static struct vt_device vt_consdev;
124 static unsigned int vt_unit = 0;
125 static MALLOC_DEFINE(M_VT, "vt", "vt device");
126 struct vt_device *main_vd = &vt_consdev;
127
128 /* Boot logo. */
129 extern unsigned int vt_logo_width;
130 extern unsigned int vt_logo_height;
131 extern unsigned int vt_logo_depth;
132 extern unsigned char vt_logo_image[];
133
134 /* Font. */
135 extern struct vt_font vt_font_default;
136 #ifndef SC_NO_CUTPASTE
137 extern struct mouse_cursor vt_default_mouse_pointer;
138 #endif
139
140 static int signal_vt_rel(struct vt_window *);
141 static int signal_vt_acq(struct vt_window *);
142 static int finish_vt_rel(struct vt_window *, int, int *);
143 static int finish_vt_acq(struct vt_window *);
144 static int vt_window_switch(struct vt_window *);
145 static int vt_late_window_switch(struct vt_window *);
146 static int vt_proc_alive(struct vt_window *);
147 static void vt_resize(struct vt_device *);
148 static void vt_update_static(void *);
149
150 SET_DECLARE(vt_drv_set, struct vt_driver);
151
152 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_DEFAULT_HEIGHT))
153 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_DEFAULT_WIDTH))
154
155 static struct terminal  vt_consterm;
156 static struct vt_window vt_conswindow;
157 static struct vt_device vt_consdev = {
158         .vd_driver = NULL,
159         .vd_softc = NULL,
160         .vd_flags = VDF_INVALID,
161         .vd_windows = { [VT_CONSWINDOW] =  &vt_conswindow, },
162         .vd_curwindow = &vt_conswindow,
163         .vd_markedwin = NULL,
164         .vd_kbstate = 0,
165 };
166 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)];
167 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE];
168 static struct vt_window vt_conswindow = {
169         .vw_number = VT_CONSWINDOW,
170         .vw_flags = VWF_CONSOLE,
171         .vw_buf = {
172                 .vb_buffer = vt_constextbuf,
173                 .vb_rows = vt_constextbufrows,
174                 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE,
175                 .vb_curroffset = 0,
176                 .vb_roffset = 0,
177                 .vb_flags = VBF_STATIC,
178                 .vb_mark_start = {.tp_row = 0, .tp_col = 0,},
179                 .vb_mark_end = {.tp_row = 0, .tp_col = 0,},
180                 .vb_scr_size = {
181                         .tp_row = _VTDEFH,
182                         .tp_col = _VTDEFW,
183                 },
184         },
185         .vw_device = &vt_consdev,
186         .vw_terminal = &vt_consterm,
187         .vw_kbdmode = K_XLATE,
188 };
189 static struct terminal vt_consterm = {
190         .tm_class = &vt_termclass,
191         .tm_softc = &vt_conswindow,
192         .tm_flags = TF_CONS,
193 };
194 static struct consdev vt_consterm_consdev = {
195         .cn_ops = &termcn_cnops,
196         .cn_arg = &vt_consterm,
197         .cn_name = "ttyv0",
198 };
199
200 /* Add to set of consoles. */
201 DATA_SET(cons_set, vt_consterm_consdev);
202
203 /*
204  * Right after kmem is done to allow early drivers to use locking and allocate
205  * memory.
206  */
207 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static,
208     &vt_consdev);
209 /* Delay until all devices attached, to not waste time. */
210 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade,
211     &vt_consdev);
212
213 /* Initialize locks/mem depended members. */
214 static void
215 vt_update_static(void *dummy)
216 {
217
218         if (main_vd->vd_driver != NULL)
219                 printf("VT: running with driver \"%s\".\n",
220                     main_vd->vd_driver->vd_name);
221         else
222                 printf("VT: init without driver.\n");
223
224         mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
225         cv_init(&main_vd->vd_winswitch, "vtwswt");
226 }
227
228 static void
229 vt_switch_timer(void *arg)
230 {
231
232         vt_late_window_switch((struct vt_window *)arg);
233 }
234
235 static int
236 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
237 {
238
239         DPRINTF(40, "%s\n", __func__);
240         curvw->vw_switch_to = vw;
241         /* Set timer to allow switch in case when process hang. */
242         callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
243             vt_switch_timer, (void *)vw);
244         /* Notify process about vt switch attempt. */
245         DPRINTF(30, "%s: Notify process.\n", __func__);
246         signal_vt_rel(curvw);
247
248         return (0);
249 }
250
251 static int
252 vt_window_postswitch(struct vt_window *vw)
253 {
254
255         signal_vt_acq(vw);
256         return (0);
257 }
258
259 /* vt_late_window_switch will done VT switching for regular case. */
260 static int
261 vt_late_window_switch(struct vt_window *vw)
262 {
263         int ret;
264
265         callout_stop(&vw->vw_proc_dead_timer);
266
267         ret = vt_window_switch(vw);
268         if (ret)
269                 return (ret);
270
271         /* Notify owner process about terminal availability. */
272         if (vw->vw_smode.mode == VT_PROCESS) {
273                 ret = vt_window_postswitch(vw);
274         }
275         return (ret);
276 }
277
278 /* Switch window. */
279 static int
280 vt_proc_window_switch(struct vt_window *vw)
281 {
282         struct vt_window *curvw;
283         struct vt_device *vd;
284         int ret;
285
286         vd = vw->vw_device;
287         curvw = vd->vd_curwindow;
288
289         if (curvw->vw_flags & VWF_VTYLOCK)
290                 return (EBUSY);
291
292         /* Ask current process permitions to switch away. */
293         if (curvw->vw_smode.mode == VT_PROCESS) {
294                 DPRINTF(30, "%s: VT_PROCESS ", __func__);
295                 if (vt_proc_alive(curvw) == FALSE) {
296                         DPRINTF(30, "Dead. Cleaning.");
297                         /* Dead */
298                 } else {
299                         DPRINTF(30, "%s: Signaling process.\n", __func__);
300                         /* Alive, try to ask him. */
301                         ret = vt_window_preswitch(vw, curvw);
302                         /* Wait for process answer or timeout. */
303                         return (ret);
304                 }
305                 DPRINTF(30, "\n");
306         }
307
308         ret = vt_late_window_switch(vw);
309         return (ret);
310 }
311
312 /* Switch window ignoring process locking. */
313 static int
314 vt_window_switch(struct vt_window *vw)
315 {
316         struct vt_device *vd = vw->vw_device;
317         struct vt_window *curvw = vd->vd_curwindow;
318         keyboard_t *kbd;
319
320         VT_LOCK(vd);
321         if (curvw == vw) {
322                 /* Nothing to do. */
323                 VT_UNLOCK(vd);
324                 return (0);
325         }
326         if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
327                 VT_UNLOCK(vd);
328                 return (EINVAL);
329         }
330
331         vd->vd_curwindow = vw;
332         vd->vd_flags |= VDF_INVALID;
333         cv_broadcast(&vd->vd_winswitch);
334         VT_UNLOCK(vd);
335
336         if (vd->vd_driver->vd_postswitch)
337                 vd->vd_driver->vd_postswitch(vd);
338
339         /* Restore per-window keyboard mode. */
340         mtx_lock(&Giant);
341         kbd = kbd_get_keyboard(vd->vd_keyboard);
342         if (kbd != NULL) {
343                 kbdd_ioctl(kbd, KDSKBMODE, (void *)&vw->vw_kbdmode);
344         }
345         mtx_unlock(&Giant);
346         DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
347
348         return (0);
349 }
350
351 static inline void
352 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
353 {
354
355         size->tp_row = vd->vd_height;
356         size->tp_col = vd->vd_width;
357         if (vf != NULL) {
358                 size->tp_row /= vf->vf_height;
359                 size->tp_col /= vf->vf_width;
360         }
361 }
362
363 static inline void
364 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
365 {
366
367         size->ws_row = size->ws_ypixel = vd->vd_height;
368         size->ws_col = size->ws_xpixel = vd->vd_width;
369         if (vf != NULL) {
370                 size->ws_row /= vf->vf_height;
371                 size->ws_col /= vf->vf_width;
372         }
373 }
374
375 static void
376 vt_scroll(struct vt_window *vw, int offset, int whence)
377 {
378         int diff;
379         term_pos_t size;
380
381         if ((vw->vw_flags & VWF_SCROLL) == 0)
382                 return;
383
384         vt_termsize(vw->vw_device, vw->vw_font, &size);
385
386         diff = vthistory_seek(&vw->vw_buf, offset, whence);
387         /*
388          * Offset changed, please update Nth lines on sceen.
389          * +N - Nth lines at top;
390          * -N - Nth lines at bottom.
391          */
392
393         if (diff < -size.tp_row || diff > size.tp_row) {
394                 vw->vw_device->vd_flags |= VDF_INVALID;
395                 return;
396         }
397         vw->vw_device->vd_flags |= VDF_INVALID; /*XXX*/
398 }
399
400 static int
401 vt_machine_kbdevent(int c)
402 {
403
404         switch (c) {
405         case SPCLKEY | DBG:
406                 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
407                 return (1);
408         case SPCLKEY | RBT:
409                 /* XXX: Make this configurable! */
410                 shutdown_nice(0);
411                 return (1);
412         case SPCLKEY | HALT:
413                 shutdown_nice(RB_HALT);
414                 return (1);
415         case SPCLKEY | PDWN:
416                 shutdown_nice(RB_HALT|RB_POWEROFF);
417                 return (1);
418         };
419
420         return (0);
421 }
422
423 static void
424 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
425 {
426         struct vt_device *vd;
427         term_pos_t size;
428
429         vd = vw->vw_device;
430         /* Only special keys handled in ScrollLock mode */
431         if ((c & SPCLKEY) == 0)
432                 return;
433
434         c &= ~SPCLKEY;
435
436         if (console == 0) {
437                 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
438                         vw = vd->vd_windows[c - F_SCR];
439                         if (vw != NULL)
440                                 vt_proc_window_switch(vw);
441                         return;
442                 }
443                 VT_LOCK(vd);
444         }
445
446         switch (c) {
447         case SLK: {
448                 /* Turn scrolling off. */
449                 vt_scroll(vw, 0, VHS_END);
450                 VTBUF_SLCK_DISABLE(&vw->vw_buf);
451                 vw->vw_flags &= ~VWF_SCROLL;
452                 break;
453         }
454         case FKEY | F(49): /* Home key. */
455                 vt_scroll(vw, 0, VHS_SET);
456                 break;
457         case FKEY | F(50): /* Arrow up. */
458                 vt_scroll(vw, -1, VHS_CUR);
459                 break;
460         case FKEY | F(51): /* Page up. */
461                 vt_termsize(vd, vw->vw_font, &size);
462                 vt_scroll(vw, -size.tp_row, VHS_CUR);
463                 break;
464         case FKEY | F(57): /* End key. */
465                 vt_scroll(vw, 0, VHS_END);
466                 break;
467         case FKEY | F(58): /* Arrow down. */
468                 vt_scroll(vw, 1, VHS_CUR);
469                 break;
470         case FKEY | F(59): /* Page down. */
471                 vt_termsize(vd, vw->vw_font, &size);
472                 vt_scroll(vw, size.tp_row, VHS_CUR);
473                 break;
474         }
475
476         if (console == 0)
477                 VT_UNLOCK(vd);
478 }
479
480 static int
481 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
482 {
483         struct vt_window *vw = vd->vd_curwindow;
484         int state = 0;
485
486 #if VT_ALT_TO_ESC_HACK
487         if (c & RELKEY) {
488                 switch (c & ~RELKEY) {
489                 case (SPCLKEY | RALT):
490                         if (vt_enable_altgr != 0)
491                                 break;
492                 case (SPCLKEY | LALT):
493                         vd->vd_kbstate &= ~ALKED;
494                 }
495                 /* Other keys ignored for RELKEY event. */
496                 return (0);
497         } else {
498                 switch (c & ~RELKEY) {
499                 case (SPCLKEY | RALT):
500                         if (vt_enable_altgr != 0)
501                                 break;
502                 case (SPCLKEY | LALT):
503                         vd->vd_kbstate |= ALKED;
504                 }
505         }
506 #else
507         if (c & RELKEY)
508                 /* Other keys ignored for RELKEY event. */
509                 return (0);
510 #endif
511
512         if (vt_machine_kbdevent(c))
513                 return (0);
514
515         if (vw->vw_flags & VWF_SCROLL) {
516                 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
517                 /* Scroll mode keys handled, nothing to do more. */
518                 return (0);
519         }
520
521         if (c & SPCLKEY) {
522                 c &= ~SPCLKEY;
523
524                 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
525                         vw = vd->vd_windows[c - F_SCR];
526                         if (vw != NULL)
527                                 vt_proc_window_switch(vw);
528                         return (0);
529                 }
530
531                 switch (c) {
532                 case SLK: {
533
534                         kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
535                         VT_LOCK(vd);
536                         if (state & SLKED) {
537                                 /* Turn scrolling on. */
538                                 vw->vw_flags |= VWF_SCROLL;
539                                 VTBUF_SLCK_ENABLE(&vw->vw_buf);
540                         } else {
541                                 /* Turn scrolling off. */
542                                 vw->vw_flags &= ~VWF_SCROLL;
543                                 VTBUF_SLCK_DISABLE(&vw->vw_buf);
544                                 vt_scroll(vw, 0, VHS_END);
545                         }
546                         VT_UNLOCK(vd);
547                         break;
548                 }
549                 case FKEY | F(1):  case FKEY | F(2):  case FKEY | F(3):
550                 case FKEY | F(4):  case FKEY | F(5):  case FKEY | F(6):
551                 case FKEY | F(7):  case FKEY | F(8):  case FKEY | F(9):
552                 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
553                         /* F1 through F12 keys. */
554                         terminal_input_special(vw->vw_terminal,
555                             TKEY_F1 + c - (FKEY | F(1)));
556                         break;
557                 case FKEY | F(49): /* Home key. */
558                         terminal_input_special(vw->vw_terminal, TKEY_HOME);
559                         break;
560                 case FKEY | F(50): /* Arrow up. */
561                         terminal_input_special(vw->vw_terminal, TKEY_UP);
562                         break;
563                 case FKEY | F(51): /* Page up. */
564                         terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
565                         break;
566                 case FKEY | F(53): /* Arrow left. */
567                         terminal_input_special(vw->vw_terminal, TKEY_LEFT);
568                         break;
569                 case FKEY | F(55): /* Arrow right. */
570                         terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
571                         break;
572                 case FKEY | F(57): /* End key. */
573                         terminal_input_special(vw->vw_terminal, TKEY_END);
574                         break;
575                 case FKEY | F(58): /* Arrow down. */
576                         terminal_input_special(vw->vw_terminal, TKEY_DOWN);
577                         break;
578                 case FKEY | F(59): /* Page down. */
579                         terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
580                         break;
581                 case FKEY | F(60): /* Insert key. */
582                         terminal_input_special(vw->vw_terminal, TKEY_INSERT);
583                         break;
584                 case FKEY | F(61): /* Delete key. */
585                         terminal_input_special(vw->vw_terminal, TKEY_DELETE);
586                         break;
587                 }
588         } else if (KEYFLAGS(c) == 0) {
589                 /* Don't do UTF-8 conversion when doing raw mode. */
590                 if (vw->vw_kbdmode == K_XLATE) {
591 #if VT_ALT_TO_ESC_HACK
592                         if (vd->vd_kbstate & ALKED) {
593                                 /*
594                                  * Prepend ESC sequence if one of ALT keys down.
595                                  */
596                                 terminal_input_char(vw->vw_terminal, 0x1b);
597                         }
598 #endif
599
600                         terminal_input_char(vw->vw_terminal, KEYCHAR(c));
601                 } else
602                         terminal_input_raw(vw->vw_terminal, c);
603         }
604         return (0);
605 }
606
607 static int
608 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
609 {
610         struct vt_device *vd = arg;
611         int c;
612
613         switch (event) {
614         case KBDIO_KEYINPUT:
615                 break;
616         case KBDIO_UNLOADING:
617                 mtx_lock(&Giant);
618                 vd->vd_keyboard = -1;
619                 kbd_release(kbd, (void *)&vd->vd_keyboard);
620                 mtx_unlock(&Giant);
621                 return (0);
622         default:
623                 return (EINVAL);
624         }
625
626         while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
627                 vt_processkey(kbd, vd, c);
628
629         return (0);
630 }
631
632 static int
633 vt_allocate_keyboard(struct vt_device *vd)
634 {
635         int              idx0, idx;
636         keyboard_t      *k0, *k;
637         keyboard_info_t  ki;
638
639         idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
640         vd->vd_keyboard = idx0;
641         if (idx0 >= 0) {
642                 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
643                 k0 = kbd_get_keyboard(idx0);
644
645                 for (idx = kbd_find_keyboard2("*", -1, 0);
646                      idx != -1;
647                      idx = kbd_find_keyboard2("*", -1, idx + 1)) {
648                         k = kbd_get_keyboard(idx);
649
650                         if (idx == idx0 || KBD_IS_BUSY(k))
651                                 continue;
652
653                         bzero(&ki, sizeof(ki));
654                         strcpy(ki.kb_name, k->kb_name);
655                         ki.kb_unit = k->kb_unit;
656
657                         kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
658                 }
659         } else {
660                 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
661                 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
662                 if (idx0 < 0) {
663                         DPRINTF(10, "%s: No keyboard found.\n", __func__);
664                         return (-1);
665                 }
666         }
667         DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
668
669         return (idx0);
670 }
671
672 static void
673 vtterm_bell(struct terminal *tm)
674 {
675         struct vt_window *vw = tm->tm_softc;
676         struct vt_device *vd = vw->vw_device;
677
678         if (vd->vd_flags & VDF_QUIET_BELL)
679                 return;
680
681         sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
682 }
683
684 static void
685 vtterm_beep(struct terminal *tm, u_int param)
686 {
687         u_int freq, period;
688
689         if ((param == 0) || ((param & 0xffff) == 0)) {
690                 vtterm_bell(tm);
691                 return;
692         }
693
694         period = ((param >> 16) & 0xffff) * hz / 1000;
695         freq = 1193182 / (param & 0xffff);
696
697         sysbeep(freq, period);
698 }
699
700 static void
701 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
702 {
703         struct vt_window *vw = tm->tm_softc;
704
705         vtbuf_cursor_position(&vw->vw_buf, p);
706 }
707
708 static void
709 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
710 {
711         struct vt_window *vw = tm->tm_softc;
712
713         vtbuf_putchar(&vw->vw_buf, p, c);
714 }
715
716 static void
717 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
718 {
719         struct vt_window *vw = tm->tm_softc;
720
721         vtbuf_fill_locked(&vw->vw_buf, r, c);
722 }
723
724 static void
725 vtterm_copy(struct terminal *tm, const term_rect_t *r,
726     const term_pos_t *p)
727 {
728         struct vt_window *vw = tm->tm_softc;
729
730         vtbuf_copy(&vw->vw_buf, r, p);
731 }
732
733 static void
734 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
735 {
736         struct vt_window *vw = tm->tm_softc;
737
738         switch (cmd) {
739         case TP_SHOWCURSOR:
740                 vtbuf_cursor_visibility(&vw->vw_buf, arg);
741                 break;
742         case TP_MOUSE:
743                 vw->vw_mouse_level = arg;
744                 break;
745         }
746 }
747
748 static inline void
749 vt_determine_colors(term_char_t c, int cursor,
750     term_color_t *fg, term_color_t *bg)
751 {
752         term_color_t tmp;
753         int invert;
754
755         invert = 0;
756
757         *fg = TCHAR_FGCOLOR(c);
758         if (TCHAR_FORMAT(c) & TF_BOLD)
759                 *fg = TCOLOR_LIGHT(*fg);
760         *bg = TCHAR_BGCOLOR(c);
761
762         if (TCHAR_FORMAT(c) & TF_REVERSE)
763                 invert ^= 1;
764         if (cursor)
765                 invert ^= 1;
766
767         if (invert) {
768                 tmp = *fg;
769                 *fg = *bg;
770                 *bg = tmp;
771         }
772 }
773
774 static void
775 vt_bitblt_char(struct vt_device *vd, struct vt_font *vf, term_char_t c,
776     int iscursor, unsigned int row, unsigned int col)
777 {
778         term_color_t fg, bg;
779
780         vt_determine_colors(c, iscursor, &fg, &bg);
781
782         if (vf != NULL) {
783                 const uint8_t *src;
784                 vt_axis_t top, left;
785
786                 src = vtfont_lookup(vf, c);
787
788                 /*
789                  * Align the terminal to the centre of the screen.
790                  * Fonts may not always be able to fill the entire
791                  * screen.
792                  */
793                 top = row * vf->vf_height + vd->vd_offset.tp_row;
794                 left = col * vf->vf_width + vd->vd_offset.tp_col;
795
796                 vd->vd_driver->vd_bitbltchr(vd, src, NULL, 0, top, left,
797                     vf->vf_width, vf->vf_height, fg, bg);
798         } else {
799                 vd->vd_driver->vd_putchar(vd, TCHAR_CHARACTER(c),
800                     row, col, fg, bg);
801         }
802 }
803
804 static void
805 vt_flush(struct vt_device *vd)
806 {
807         struct vt_window *vw;
808         struct vt_font *vf;
809         struct vt_bufmask tmask;
810         unsigned int row, col;
811         term_rect_t tarea;
812         term_pos_t size;
813         term_char_t *r;
814 #ifndef SC_NO_CUTPASTE
815         struct mouse_cursor *m;
816         int bpl, h, w;
817 #endif
818
819         vw = vd->vd_curwindow;
820         if (vw == NULL)
821                 return;
822         vf = vw->vw_font;
823         if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
824                 return;
825
826         if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
827                 return;
828
829         vtbuf_undirty(&vw->vw_buf, &tarea, &tmask);
830         vt_termsize(vd, vf, &size);
831
832         /* Force a full redraw when the screen contents are invalid. */
833         if (vd->vd_flags & VDF_INVALID) {
834                 tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0;
835                 tarea.tr_end = size;
836                 tmask.vbm_row = tmask.vbm_col = VBM_DIRTY;
837
838                 vd->vd_flags &= ~VDF_INVALID;
839         }
840
841 #ifndef SC_NO_CUTPASTE
842         if ((vw->vw_flags & VWF_MOUSE_HIDE) == 0) {
843                 /* Mark last mouse position as dirty to erase. */
844                 vtbuf_mouse_cursor_position(&vw->vw_buf, vd->vd_mdirtyx,
845                     vd->vd_mdirtyy);
846         }
847 #endif
848
849         for (row = tarea.tr_begin.tp_row; row < tarea.tr_end.tp_row; row++) {
850                 if (!VTBUF_DIRTYROW(&tmask, row))
851                         continue;
852                 r = VTBUF_GET_ROW(&vw->vw_buf, row);
853                 for (col = tarea.tr_begin.tp_col;
854                     col < tarea.tr_end.tp_col; col++) {
855                         if (!VTBUF_DIRTYCOL(&tmask, col))
856                                 continue;
857
858                         vt_bitblt_char(vd, vf, r[col],
859                             VTBUF_ISCURSOR(&vw->vw_buf, row, col), row, col);
860                 }
861         }
862
863 #ifndef SC_NO_CUTPASTE
864         /* Mouse disabled. */
865         if (vw->vw_flags & VWF_MOUSE_HIDE)
866                 return;
867
868         /* No mouse for DDB. */
869         if (kdb_active || panicstr != NULL)
870                 return;
871
872         if ((vd->vd_flags & (VDF_MOUSECURSOR|VDF_TEXTMODE)) ==
873             VDF_MOUSECURSOR) {
874                 m = &vt_default_mouse_pointer;
875                 bpl = (m->w + 7) >> 3; /* Bytes per source line. */
876                 w = m->w;
877                 h = m->h;
878
879                 if ((vd->vd_mx + m->w) > (size.tp_col * vf->vf_width))
880                         w = (size.tp_col * vf->vf_width) - vd->vd_mx - 1;
881                 if ((vd->vd_my + m->h) > (size.tp_row * vf->vf_height))
882                         h = (size.tp_row * vf->vf_height) - vd->vd_my - 1;
883
884                 vd->vd_driver->vd_maskbitbltchr(vd, m->map, m->mask, bpl,
885                     vd->vd_offset.tp_row + vd->vd_my,
886                     vd->vd_offset.tp_col + vd->vd_mx,
887                     w, h, TC_WHITE, TC_BLACK);
888                 /* Save point of last mouse cursor to erase it later. */
889                 vd->vd_mdirtyx = vd->vd_mx / vf->vf_width;
890                 vd->vd_mdirtyy = vd->vd_my / vf->vf_height;
891         }
892 #endif
893 }
894
895 static void
896 vt_timer(void *arg)
897 {
898         struct vt_device *vd;
899
900         vd = arg;
901         /* Update screen if required. */
902         vt_flush(vd);
903
904         /* Schedule for next update. */
905         callout_schedule(&vd->vd_timer, hz / VT_TIMERFREQ);
906 }
907
908 static void
909 vtterm_done(struct terminal *tm)
910 {
911         struct vt_window *vw = tm->tm_softc;
912         struct vt_device *vd = vw->vw_device;
913
914         if (kdb_active || panicstr != NULL) {
915                 /* Switch to the debugger. */
916                 if (vd->vd_curwindow != vw) {
917                         vd->vd_curwindow = vw;
918                         vd->vd_flags |= VDF_INVALID;
919                         if (vd->vd_driver->vd_postswitch)
920                                 vd->vd_driver->vd_postswitch(vd);
921                 }
922                 vd->vd_flags &= ~VDF_SPLASH;
923                 vt_flush(vd);
924         } else if (!(vd->vd_flags & VDF_ASYNC)) {
925                 vt_flush(vd);
926         }
927 }
928
929 #ifdef DEV_SPLASH
930 static void
931 vtterm_splash(struct vt_device *vd)
932 {
933         vt_axis_t top, left;
934
935         /* Display a nice boot splash. */
936         if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
937
938                 top = (vd->vd_height - vt_logo_height) / 2;
939                 left = (vd->vd_width - vt_logo_width) / 2;
940                 switch (vt_logo_depth) {
941                 case 1:
942                         /* XXX: Unhardcode colors! */
943                         vd->vd_driver->vd_bitbltchr(vd, vt_logo_image, NULL, 0,
944                             top, left, vt_logo_width, vt_logo_height, 0xf, 0x0);
945                 }
946                 vd->vd_flags |= VDF_SPLASH;
947         }
948 }
949 #endif
950
951
952 static void
953 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
954 {
955         struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
956         struct vt_window *vw = tm->tm_softc;
957         struct vt_device *vd = vw->vw_device;
958         struct winsize wsz;
959
960         if (vd->vd_flags & VDF_INITIALIZED)
961                 /* Initialization already done. */
962                 return;
963
964         SET_FOREACH(vtdlist, vt_drv_set) {
965                 vtd = *vtdlist;
966                 if (vtd->vd_probe == NULL)
967                         continue;
968                 if (vtd->vd_probe(vd) == CN_DEAD)
969                         continue;
970                 if ((vtdbest == NULL) ||
971                     (vtd->vd_priority > vtdbest->vd_priority))
972                         vtdbest = vtd;
973         }
974         if (vtdbest == NULL) {
975                 cp->cn_pri = CN_DEAD;
976                 vd->vd_flags |= VDF_DEAD;
977         } else {
978                 vd->vd_driver = vtdbest;
979                 cp->cn_pri = vd->vd_driver->vd_init(vd);
980         }
981
982         /* Check if driver's vt_init return CN_DEAD. */
983         if (cp->cn_pri == CN_DEAD) {
984                 vd->vd_flags |= VDF_DEAD;
985         }
986
987         /* Initialize any early-boot keyboard drivers */
988         kbd_configure(KB_CONF_PROBE_ONLY);
989
990         vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
991         vd->vd_windows[VT_CONSWINDOW] = vw;
992         sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
993
994         /* Attach default font if not in TEXTMODE. */
995         if (!(vd->vd_flags & VDF_TEXTMODE))
996                 vw->vw_font = vtfont_ref(&vt_font_default);
997
998         vtbuf_init_early(&vw->vw_buf);
999         vt_winsize(vd, vw->vw_font, &wsz);
1000         terminal_set_winsize(tm, &wsz);
1001
1002         if (vtdbest != NULL) {
1003 #ifdef DEV_SPLASH
1004                 vtterm_splash(vd);
1005 #endif
1006                 vd->vd_flags |= VDF_INITIALIZED;
1007         }
1008 }
1009
1010 static int
1011 vtterm_cngetc(struct terminal *tm)
1012 {
1013         struct vt_window *vw = tm->tm_softc;
1014         struct vt_device *vd = vw->vw_device;
1015         keyboard_t *kbd;
1016         int state;
1017         u_int c;
1018
1019         if (vw->vw_kbdsq && *vw->vw_kbdsq)
1020                 return (*vw->vw_kbdsq++);
1021
1022         state = 0;
1023         /* Make sure the splash screen is not there. */
1024         if (vd->vd_flags & VDF_SPLASH) {
1025                 /* Remove splash */
1026                 vd->vd_flags &= ~VDF_SPLASH;
1027                 /* Mark screen as invalid to force update */
1028                 vd->vd_flags |= VDF_INVALID;
1029                 vt_flush(vd);
1030         }
1031
1032         /* Stripped down keyboard handler. */
1033         kbd = kbd_get_keyboard(vd->vd_keyboard);
1034         if (kbd == NULL)
1035                 return (-1);
1036
1037         /* Force keyboard input mode to K_XLATE */
1038         c = K_XLATE;
1039         kbdd_ioctl(kbd, KDSKBMODE, (void *)&c);
1040
1041         /* Switch the keyboard to polling to make it work here. */
1042         kbdd_poll(kbd, TRUE);
1043         c = kbdd_read_char(kbd, 0);
1044         kbdd_poll(kbd, FALSE);
1045         if (c & RELKEY)
1046                 return (-1);
1047
1048         if (vw->vw_flags & VWF_SCROLL) {
1049                 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1050                 vt_flush(vd);
1051                 return (-1);
1052         }
1053
1054         /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1055         if (c & SPCLKEY) {
1056                 switch (c) {
1057                 case SPCLKEY | SLK:
1058                         kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
1059                         if (state & SLKED) {
1060                                 /* Turn scrolling on. */
1061                                 vw->vw_flags |= VWF_SCROLL;
1062                                 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1063                         } else {
1064                                 /* Turn scrolling off. */
1065                                 vt_scroll(vw, 0, VHS_END);
1066                                 vw->vw_flags &= ~VWF_SCROLL;
1067                                 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1068                         }
1069                         break;
1070                 /* XXX: KDB can handle history. */
1071                 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1072                         vw->vw_kbdsq = "\x1b[A";
1073                         break;
1074                 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1075                         vw->vw_kbdsq = "\x1b[B";
1076                         break;
1077                 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1078                         vw->vw_kbdsq = "\x1b[C";
1079                         break;
1080                 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1081                         vw->vw_kbdsq = "\x1b[D";
1082                         break;
1083                 }
1084
1085                 /* Force refresh to make scrollback work. */
1086                 vt_flush(vd);
1087         } else if (KEYFLAGS(c) == 0) {
1088                 return (KEYCHAR(c));
1089         }
1090
1091         if (vw->vw_kbdsq && *vw->vw_kbdsq)
1092                 return (*vw->vw_kbdsq++);
1093
1094         return (-1);
1095 }
1096
1097 static void
1098 vtterm_opened(struct terminal *tm, int opened)
1099 {
1100         struct vt_window *vw = tm->tm_softc;
1101         struct vt_device *vd = vw->vw_device;
1102
1103         VT_LOCK(vd);
1104         vd->vd_flags &= ~VDF_SPLASH;
1105         if (opened)
1106                 vw->vw_flags |= VWF_OPENED;
1107         else {
1108                 vw->vw_flags &= ~VWF_OPENED;
1109                 /* TODO: finish ACQ/REL */
1110         }
1111         VT_UNLOCK(vd);
1112 }
1113
1114 static int
1115 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1116 {
1117         struct vt_device *vd = vw->vw_device;
1118         struct terminal *tm = vw->vw_terminal;
1119         term_pos_t size;
1120         struct winsize wsz;
1121
1122         /*
1123          * Changing fonts.
1124          *
1125          * Changing fonts is a little tricky.  We must prevent
1126          * simultaneous access to the device, so we must stop
1127          * the display timer and the terminal from accessing.
1128          * We need to switch fonts and grow our screen buffer.
1129          *
1130          * XXX: Right now the code uses terminal_mute() to
1131          * prevent data from reaching the console driver while
1132          * resizing the screen buffer.  This isn't elegant...
1133          */
1134
1135         VT_LOCK(vd);
1136         if (vw->vw_flags & VWF_BUSY) {
1137                 /* Another process is changing the font. */
1138                 VT_UNLOCK(vd);
1139                 return (EBUSY);
1140         }
1141         if (vw->vw_font == NULL) {
1142                 /* Our device doesn't need fonts. */
1143                 VT_UNLOCK(vd);
1144                 return (ENOTTY);
1145         }
1146         vw->vw_flags |= VWF_BUSY;
1147         VT_UNLOCK(vd);
1148
1149         vt_termsize(vd, vf, &size);
1150         vt_winsize(vd, vf, &wsz);
1151         /* Save offset to font aligned area. */
1152         vd->vd_offset.tp_col = (vd->vd_width % vf->vf_width) / 2;
1153         vd->vd_offset.tp_row = (vd->vd_height % vf->vf_height) / 2;
1154
1155         /* Grow the screen buffer and terminal. */
1156         terminal_mute(tm, 1);
1157         vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1158         terminal_set_winsize_blank(tm, &wsz, 0);
1159         terminal_mute(tm, 0);
1160
1161         /* Actually apply the font to the current window. */
1162         VT_LOCK(vd);
1163         vtfont_unref(vw->vw_font);
1164         vw->vw_font = vtfont_ref(vf);
1165
1166         /* Force a full redraw the next timer tick. */
1167         if (vd->vd_curwindow == vw)
1168                 vd->vd_flags |= VDF_INVALID;
1169         vw->vw_flags &= ~VWF_BUSY;
1170         VT_UNLOCK(vd);
1171         return (0);
1172 }
1173
1174 static int
1175 vt_set_border(struct vt_window *vw, struct vt_font *vf, term_color_t c)
1176 {
1177         struct vt_device *vd = vw->vw_device;
1178         int x, y, off_x, off_y;
1179
1180         if (vd->vd_driver->vd_drawrect == NULL)
1181                 return (ENOTSUP);
1182
1183         x = vd->vd_width - 1;
1184         y = vd->vd_height - 1;
1185         off_x = vd->vd_offset.tp_col;
1186         off_y = vd->vd_offset.tp_row;
1187
1188         /* Top bar. */
1189         if (off_y > 0)
1190                 vd->vd_driver->vd_drawrect(vd, 0, 0, x, off_y - 1, 1, c);
1191         /* Left bar. */
1192         if (off_x > 0)
1193                 vd->vd_driver->vd_drawrect(vd, 0, off_y, off_x - 1, y - off_y,
1194                     1, c);
1195         /* Right bar.  May be 1 pixel wider than necessary due to rounding. */
1196         vd->vd_driver->vd_drawrect(vd, x - off_x, off_y, x, y - off_y, 1, c);
1197         /* Bottom bar.  May be 1 mixel taller than necessary due to rounding. */
1198         vd->vd_driver->vd_drawrect(vd, 0, y - off_y, x, y, 1, c);
1199
1200         return (0);
1201 }
1202
1203 static int
1204 vt_proc_alive(struct vt_window *vw)
1205 {
1206         struct proc *p;
1207
1208         if (vw->vw_smode.mode != VT_PROCESS)
1209                 return (FALSE);
1210
1211         if (vw->vw_proc) {
1212                 if ((p = pfind(vw->vw_pid)) != NULL)
1213                         PROC_UNLOCK(p);
1214                 if (vw->vw_proc == p)
1215                         return (TRUE);
1216                 vw->vw_proc = NULL;
1217                 vw->vw_smode.mode = VT_AUTO;
1218                 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1219                 vw->vw_pid = 0;
1220         }
1221         return (FALSE);
1222 }
1223
1224 static int
1225 signal_vt_rel(struct vt_window *vw)
1226 {
1227
1228         if (vw->vw_smode.mode != VT_PROCESS)
1229                 return (FALSE);
1230         if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1231                 vw->vw_proc = NULL;
1232                 vw->vw_pid = 0;
1233                 return (TRUE);
1234         }
1235         vw->vw_flags |= VWF_SWWAIT_REL;
1236         PROC_LOCK(vw->vw_proc);
1237         kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1238         PROC_UNLOCK(vw->vw_proc);
1239         DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1240         return (TRUE);
1241 }
1242
1243 static int
1244 signal_vt_acq(struct vt_window *vw)
1245 {
1246
1247         if (vw->vw_smode.mode != VT_PROCESS)
1248                 return (FALSE);
1249         if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1250                 cnavailable(vw->vw_terminal->consdev, FALSE);
1251         if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1252                 vw->vw_proc = NULL;
1253                 vw->vw_pid = 0;
1254                 return (TRUE);
1255         }
1256         vw->vw_flags |= VWF_SWWAIT_ACQ;
1257         PROC_LOCK(vw->vw_proc);
1258         kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1259         PROC_UNLOCK(vw->vw_proc);
1260         DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1261         return (TRUE);
1262 }
1263
1264 static int
1265 finish_vt_rel(struct vt_window *vw, int release, int *s)
1266 {
1267
1268         if (vw->vw_flags & VWF_SWWAIT_REL) {
1269                 vw->vw_flags &= ~VWF_SWWAIT_REL;
1270                 if (release) {
1271                         callout_drain(&vw->vw_proc_dead_timer);
1272                         vt_late_window_switch(vw->vw_switch_to);
1273                 }
1274                 return (0);
1275         }
1276         return (EINVAL);
1277 }
1278
1279 static int
1280 finish_vt_acq(struct vt_window *vw)
1281 {
1282
1283         if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1284                 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1285                 return (0);
1286         }
1287         return (EINVAL);
1288 }
1289
1290 #ifndef SC_NO_CUTPASTE
1291 static void
1292 vt_mouse_terminput_button(struct vt_device *vd, int button)
1293 {
1294         struct vt_window *vw;
1295         struct vt_font *vf;
1296         char mouseb[6] = "\x1B[M";
1297         int i, x, y;
1298
1299         vw = vd->vd_curwindow;
1300         vf = vw->vw_font;
1301
1302         /* Translate to char position. */
1303         x = vd->vd_mx / vf->vf_width;
1304         y = vd->vd_my / vf->vf_height;
1305         /* Avoid overflow. */
1306         x = MIN(x, 255 - '!');
1307         y = MIN(y, 255 - '!');
1308
1309         mouseb[3] = ' ' + button;
1310         mouseb[4] = '!' + x;
1311         mouseb[5] = '!' + y;
1312
1313         for (i = 0; i < sizeof(mouseb); i++ )
1314                 terminal_input_char(vw->vw_terminal, mouseb[i]);
1315 }
1316
1317 static void
1318 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1319     int cnt)
1320 {
1321
1322         switch (type) {
1323         case MOUSE_BUTTON_EVENT:
1324                 if (cnt > 0) {
1325                         /* Mouse button pressed. */
1326                         if (event & MOUSE_BUTTON1DOWN)
1327                                 vt_mouse_terminput_button(vd, 0);
1328                         if (event & MOUSE_BUTTON2DOWN)
1329                                 vt_mouse_terminput_button(vd, 1);
1330                         if (event & MOUSE_BUTTON3DOWN)
1331                                 vt_mouse_terminput_button(vd, 2);
1332                 } else {
1333                         /* Mouse button released. */
1334                         vt_mouse_terminput_button(vd, 3);
1335                 }
1336                 break;
1337 #ifdef notyet
1338         case MOUSE_MOTION_EVENT:
1339                 if (mouse->u.data.z < 0) {
1340                         /* Scroll up. */
1341                         sc_mouse_input_button(vd, 64);
1342                 } else if (mouse->u.data.z > 0) {
1343                         /* Scroll down. */
1344                         sc_mouse_input_button(vd, 65);
1345                 }
1346                 break;
1347 #endif
1348         }
1349 }
1350
1351 void
1352 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1353 {
1354         struct vt_device *vd;
1355         struct vt_window *vw;
1356         struct vt_font *vf;
1357         term_pos_t size;
1358         term_char_t *buf;
1359         int i, len, mark;
1360
1361         vd = main_vd;
1362         vw = vd->vd_curwindow;
1363         vf = vw->vw_font;
1364         mark = 0;
1365
1366         if (vw->vw_flags & VWF_MOUSE_HIDE)
1367                 return; /* Mouse disabled. */
1368
1369         if (vf == NULL) /* Text mode. */
1370                 return;
1371
1372         /*
1373          * TODO: add flag about pointer position changed, to not redraw chars
1374          * under mouse pointer when nothing changed.
1375          */
1376
1377         if (vw->vw_mouse_level > 0)
1378                 vt_mouse_terminput(vd, type, x, y, event, cnt);
1379
1380         switch (type) {
1381         case MOUSE_ACTION:
1382         case MOUSE_MOTION_EVENT:
1383                 /* Movement */
1384                 x += vd->vd_mx;
1385                 y += vd->vd_my;
1386
1387                 vt_termsize(vd, vf, &size);
1388
1389                 /* Apply limits. */
1390                 x = MAX(x, 0);
1391                 y = MAX(y, 0);
1392                 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1393                 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1394
1395                 vd->vd_mx = x;
1396                 vd->vd_my = y;
1397                 if ((vd->vd_mstate & MOUSE_BUTTON1DOWN) &&
1398                     (vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1399                         vd->vd_mx / vf->vf_width, 
1400                         vd->vd_my / vf->vf_height) == 1)) {
1401
1402                         /*
1403                          * We have something marked to copy, so update pointer
1404                          * to window with selection.
1405                          */
1406                         vd->vd_markedwin = vw;
1407                 }
1408                 return; /* Done */
1409         case MOUSE_BUTTON_EVENT:
1410                 /* Buttons */
1411                 break;
1412         default:
1413                 return; /* Done */
1414         }
1415
1416         switch (event) {
1417         case MOUSE_BUTTON1DOWN:
1418                 switch (cnt % 4) {
1419                 case 0: /* up */
1420                         mark = VTB_MARK_END;
1421                         break;
1422                 case 1: /* single click: start cut operation */
1423                         mark = VTB_MARK_START;
1424                         break;
1425                 case 2: /* double click: cut a word */
1426                         mark = VTB_MARK_WORD;
1427                         break;
1428                 case 3: /* triple click: cut a line */
1429                         mark = VTB_MARK_ROW;
1430                         break;
1431                 }
1432                 break;
1433         case VT_MOUSE_PASTEBUTTON:
1434                 switch (cnt) {
1435                 case 0: /* up */
1436                         break;
1437                 default:
1438                         if (vd->vd_markedwin == NULL)
1439                                 return;
1440                         /* Get current selecton size in bytes. */
1441                         len = vtbuf_get_marked_len(&vd->vd_markedwin->vw_buf);
1442                         if (len <= 0)
1443                                 return;
1444
1445                         buf = malloc(len, M_VT, M_WAITOK | M_ZERO);
1446                         /* Request cupy/paste buffer data, no more than `len' */
1447                         vtbuf_extract_marked(&vd->vd_markedwin->vw_buf, buf,
1448                             len);
1449
1450                         len /= sizeof(term_char_t);
1451                         for (i = 0; i < len; i++ ) {
1452                                 if (buf[i] == '\0')
1453                                         continue;
1454                                 terminal_input_char(vw->vw_terminal, buf[i]);
1455                         }
1456
1457                         /* Done, so cleanup. */
1458                         free(buf, M_VT);
1459                         break;
1460                 }
1461                 return; /* Done */
1462         case VT_MOUSE_EXTENDBUTTON:
1463                 switch (cnt) {
1464                 case 0: /* up */
1465                         if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1466                                 mark = VTB_MARK_EXTEND;
1467                         else
1468                                 mark = 0;
1469                         break;
1470                 default:
1471                         mark = VTB_MARK_EXTEND;
1472                         break;
1473                 }
1474                 break;
1475         default:
1476                 return; /* Done */
1477         }
1478
1479         /* Save buttons state. */
1480         if (cnt > 0)
1481                 vd->vd_mstate |= event;
1482         else
1483                 vd->vd_mstate &= ~event;
1484
1485         if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1486             vd->vd_my / vf->vf_height) == 1) {
1487                 /*
1488                  * We have something marked to copy, so update pointer to
1489                  * window with selection.
1490                  */
1491                 vd->vd_markedwin = vw;
1492         }
1493 }
1494
1495 void
1496 vt_mouse_state(int show)
1497 {
1498         struct vt_device *vd;
1499         struct vt_window *vw;
1500
1501         vd = main_vd;
1502         vw = vd->vd_curwindow;
1503
1504         switch (show) {
1505         case VT_MOUSE_HIDE:
1506                 vw->vw_flags |= VWF_MOUSE_HIDE;
1507                 break;
1508         case VT_MOUSE_SHOW:
1509                 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1510                 break;
1511         }
1512 }
1513 #endif
1514
1515 static int
1516 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1517     int nprot, vm_memattr_t *memattr)
1518 {
1519         struct vt_window *vw = tm->tm_softc;
1520         struct vt_device *vd = vw->vw_device;
1521
1522         if (vd->vd_driver->vd_fb_mmap)
1523                 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1524                     memattr));
1525
1526         return (ENXIO);
1527 }
1528
1529 static int
1530 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1531     struct thread *td)
1532 {
1533         struct vt_window *vw = tm->tm_softc;
1534         struct vt_device *vd = vw->vw_device;
1535         keyboard_t *kbd;
1536         int error, i, s;
1537 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1538     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1539         int ival;
1540
1541         switch (cmd) {
1542         case _IO('v', 4):
1543                 cmd = VT_RELDISP;
1544                 break;
1545         case _IO('v', 5):
1546                 cmd = VT_ACTIVATE;
1547                 break;
1548         case _IO('v', 6):
1549                 cmd = VT_WAITACTIVE;
1550                 break;
1551         case _IO('K', 20):
1552                 cmd = KDSKBSTATE;
1553                 break;
1554         case _IO('K', 67):
1555                 cmd = KDSETRAD;
1556                 break;
1557         case _IO('K', 7):
1558                 cmd = KDSKBMODE;
1559                 break;
1560         case _IO('K', 8):
1561                 cmd = KDMKTONE;
1562                 break;
1563         case _IO('K', 63):
1564                 cmd = KIOCSOUND;
1565                 break;
1566         case _IO('K', 66):
1567                 cmd = KDSETLED;
1568                 break;
1569         case _IO('c', 110):
1570                 cmd = CONS_SETKBD;
1571                 break;
1572         default:
1573                 goto skip_thunk;
1574         }
1575         ival = IOCPARM_IVAL(data);
1576         data = (caddr_t)&ival;
1577 skip_thunk:
1578 #endif
1579
1580         switch (cmd) {
1581         case KDSETRAD:          /* set keyboard repeat & delay rates (old) */
1582                 if (*(int *)data & ~0x7f)
1583                         return (EINVAL);
1584         case GIO_KEYMAP:
1585         case PIO_KEYMAP:
1586         case GIO_DEADKEYMAP:
1587         case PIO_DEADKEYMAP:
1588         case GETFKEY:
1589         case SETFKEY:
1590         case KDGKBINFO:
1591         case KDGKBTYPE:
1592         case KDSKBSTATE:        /* set keyboard state (locks) */
1593         case KDGKBSTATE:        /* get keyboard state (locks) */
1594         case KDGETREPEAT:       /* get keyboard repeat & delay rates */
1595         case KDSETREPEAT:       /* set keyboard repeat & delay rates (new) */
1596         case KDSETLED:          /* set keyboard LED status */
1597         case KDGETLED:          /* get keyboard LED status */
1598         case KBADDKBD:          /* add/remove keyboard to/from mux */
1599         case KBRELKBD: {
1600                 error = 0;
1601
1602                 mtx_lock(&Giant);
1603                 kbd = kbd_get_keyboard(vd->vd_keyboard);
1604                 if (kbd != NULL)
1605                         error = kbdd_ioctl(kbd, cmd, data);
1606                 mtx_unlock(&Giant);
1607                 if (error == ENOIOCTL) {
1608                         if (cmd == KDGKBTYPE) {
1609                                 /* always return something? XXX */
1610                                 *(int *)data = 0;
1611                         } else {
1612                                 return (ENODEV);
1613                         }
1614                 }
1615                 return (error);
1616         }
1617         case KDGKBMODE: {
1618                 int mode = -1;
1619
1620                 mtx_lock(&Giant);
1621                 kbd = kbd_get_keyboard(vd->vd_keyboard);
1622                 if (kbd != NULL) {
1623                         kbdd_ioctl(kbd, KDGKBMODE, (void *)&mode);
1624                 }
1625                 mtx_unlock(&Giant);
1626                 DPRINTF(20, "mode %d, vw_kbdmode %d\n", mode, vw->vw_kbdmode);
1627                 *(int *)data = mode;
1628                 return (0);
1629         }
1630         case KDSKBMODE: {
1631                 int mode;
1632
1633                 mode = *(int *)data;
1634                 switch (mode) {
1635                 case K_XLATE:
1636                 case K_RAW:
1637                 case K_CODE:
1638                         vw->vw_kbdmode = mode;
1639                         if (vw == vd->vd_curwindow) {
1640                                 keyboard_t *kbd;
1641                                 error = 0;
1642
1643                                 mtx_lock(&Giant);
1644                                 kbd = kbd_get_keyboard(vd->vd_keyboard);
1645                                 if (kbd != NULL) {
1646                                         error = kbdd_ioctl(kbd, KDSKBMODE,
1647                                             (void *)&mode);
1648                                 }
1649                                 mtx_unlock(&Giant);
1650                         }
1651                         return (0);
1652                 default:
1653                         return (EINVAL);
1654                 }
1655         }
1656         case FBIOGTYPE:
1657         case FBIO_GETWINORG:    /* get frame buffer window origin */
1658         case FBIO_GETDISPSTART: /* get display start address */
1659         case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
1660         case FBIO_BLANK:        /* blank display */
1661                 if (vd->vd_driver->vd_fb_ioctl)
1662                         return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
1663                 break;
1664         case CONS_BLANKTIME:
1665                 /* XXX */
1666                 return (0);
1667         case CONS_GET:
1668                 /* XXX */
1669                 *(int *)data = M_CG640x480;
1670                 return (0);
1671         case CONS_BELLTYPE:     /* set bell type sound */
1672                 if ((*(int *)data) & CONS_QUIET_BELL)
1673                         vd->vd_flags |= VDF_QUIET_BELL;
1674                 else
1675                         vd->vd_flags &= ~VDF_QUIET_BELL;
1676                 return (0);
1677         case CONS_GETINFO: {
1678                 vid_info_t *vi = (vid_info_t *)data;
1679
1680                 vi->m_num = vd->vd_curwindow->vw_number + 1;
1681                 /* XXX: other fields! */
1682                 return (0);
1683         }
1684         case CONS_GETVERS: 
1685                 *(int *)data = 0x200;
1686                 return (0);
1687         case CONS_MODEINFO:
1688                 /* XXX */
1689                 return (0);
1690         case CONS_MOUSECTL: {
1691                 mouse_info_t *mouse = (mouse_info_t*)data;
1692
1693                 /*
1694                  * This has no effect on vt(4).  We don't draw any mouse
1695                  * cursor.  Just ignore MOUSE_HIDE and MOUSE_SHOW to
1696                  * prevent excessive errors.  All the other commands
1697                  * should not be applied to individual TTYs, but only to
1698                  * consolectl.
1699                  */
1700                 switch (mouse->operation) {
1701                 case MOUSE_HIDE:
1702                         vd->vd_flags &= ~VDF_MOUSECURSOR;
1703                         return (0);
1704                 case MOUSE_SHOW:
1705                         vd->vd_mx = vd->vd_width / 2;
1706                         vd->vd_my = vd->vd_height / 2;
1707                         vd->vd_flags |= VDF_MOUSECURSOR;
1708                         return (0);
1709                 default:
1710                         return (EINVAL);
1711                 }
1712         }
1713         case PIO_VFONT: {
1714                 struct vt_font *vf;
1715
1716                 error = vtfont_load((void *)data, &vf);
1717                 if (error != 0)
1718                         return (error);
1719
1720                 error = vt_change_font(vw, vf);
1721                 if (error == 0) {
1722                         /* XXX: replace 0 with current bg color. */
1723                         vt_set_border(vw, vf, 0);
1724                 }
1725                 vtfont_unref(vf);
1726                 return (error);
1727         }
1728         case GIO_SCRNMAP: {
1729                 scrmap_t *sm = (scrmap_t *)data;
1730                 int i;
1731
1732                 /* We don't have screen maps, so return a handcrafted one. */
1733                 for (i = 0; i < 256; i++)
1734                         sm->scrmap[i] = i;
1735                 return (0);
1736         }
1737         case KDSETMODE:
1738                 /* XXX */
1739                 return (0);
1740         case KDENABIO:          /* allow io operations */
1741                 error = priv_check(td, PRIV_IO);
1742                 if (error != 0)
1743                         return (error);
1744                 error = securelevel_gt(td->td_ucred, 0);
1745                 if (error != 0)
1746                         return (error);
1747 #if defined(__i386__)
1748                 td->td_frame->tf_eflags |= PSL_IOPL;
1749 #elif defined(__amd64__)
1750                 td->td_frame->tf_rflags |= PSL_IOPL;
1751 #endif
1752                 return (0);
1753         case KDDISABIO:         /* disallow io operations (default) */
1754 #if defined(__i386__)
1755                 td->td_frame->tf_eflags &= ~PSL_IOPL;
1756 #elif defined(__amd64__)
1757                 td->td_frame->tf_rflags &= ~PSL_IOPL;
1758 #endif
1759                 return (0);
1760         case KDMKTONE:          /* sound the bell */
1761                 vtterm_beep(tm, *(u_int *)data);
1762                 return (0);
1763         case KIOCSOUND:         /* make tone (*data) hz */
1764                 /* TODO */
1765                 return (0);
1766         case CONS_SETKBD:               /* set the new keyboard */
1767                 mtx_lock(&Giant);
1768                 error = 0;
1769                 if (vd->vd_keyboard != *(int *)data) {
1770                         kbd = kbd_get_keyboard(*(int *)data);
1771                         if (kbd == NULL) {
1772                                 mtx_unlock(&Giant);
1773                                 return (EINVAL);
1774                         }
1775                         i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
1776                             (void *)&vd->vd_keyboard, vt_kbdevent, vd);
1777                         if (i >= 0) {
1778                                 if (vd->vd_keyboard != -1) {
1779                                         kbd_release(kbd,
1780                                             (void *)&vd->vd_keyboard);
1781                                 }
1782                                 kbd = kbd_get_keyboard(i);
1783                                 vd->vd_keyboard = i;
1784
1785                                 (void)kbdd_ioctl(kbd, KDSKBMODE,
1786                                     (caddr_t)&vd->vd_curwindow->vw_kbdmode);
1787                         } else {
1788                                 error = EPERM;  /* XXX */
1789                         }
1790                 }
1791                 mtx_unlock(&Giant);
1792                 return (error);
1793         case CONS_RELKBD:               /* release the current keyboard */
1794                 mtx_lock(&Giant);
1795                 error = 0;
1796                 if (vd->vd_keyboard != -1) {
1797                         kbd = kbd_get_keyboard(vd->vd_keyboard);
1798                         if (kbd == NULL) {
1799                                 mtx_unlock(&Giant);
1800                                 return (EINVAL);
1801                         }
1802                         error = kbd_release(kbd, (void *)&vd->vd_keyboard);
1803                         if (error == 0) {
1804                                 vd->vd_keyboard = -1;
1805                         }
1806                 }
1807                 mtx_unlock(&Giant);
1808                 return (error);
1809         case VT_ACTIVATE: {
1810                 int win;
1811                 win = *(int *)data - 1;
1812                 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
1813                     VT_UNIT(vw), win);
1814                 if ((win > VT_MAXWINDOWS) || (win < 0))
1815                         return (EINVAL);
1816                 return (vt_proc_window_switch(vd->vd_windows[win]));
1817         }
1818         case VT_GETACTIVE:
1819                 *(int *)data = vd->vd_curwindow->vw_number + 1;
1820                 return (0);
1821         case VT_GETINDEX:
1822                 *(int *)data = vw->vw_number + 1;
1823                 return (0);
1824         case VT_LOCKSWITCH:
1825                 /* TODO: Check current state, switching can be in progress. */
1826                 if ((*(int *)data) == 0x01)
1827                         vw->vw_flags |= VWF_VTYLOCK;
1828                 else if ((*(int *)data) == 0x02)
1829                         vw->vw_flags &= ~VWF_VTYLOCK;
1830                 else
1831                         return (EINVAL);
1832                 return (0);
1833         case VT_OPENQRY:
1834                 VT_LOCK(vd);
1835                 for (i = 0; i < VT_MAXWINDOWS; i++) {
1836                         vw = vd->vd_windows[i];
1837                         if (vw == NULL)
1838                                 continue;
1839                         if (!(vw->vw_flags & VWF_OPENED)) {
1840                                 *(int *)data = vw->vw_number + 1;
1841                                 VT_UNLOCK(vd);
1842                                 return (0);
1843                         }
1844                 }
1845                 VT_UNLOCK(vd);
1846                 return (EINVAL);
1847         case VT_WAITACTIVE:
1848                 error = 0;
1849
1850                 i = *(unsigned int *)data;
1851                 if (i > VT_MAXWINDOWS)
1852                         return (EINVAL);
1853                 if (i != 0)
1854                         vw = vd->vd_windows[i - 1];
1855
1856                 VT_LOCK(vd);
1857                 while (vd->vd_curwindow != vw && error == 0)
1858                         error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
1859                 VT_UNLOCK(vd);
1860                 return (error);
1861         case VT_SETMODE: {      /* set screen switcher mode */
1862                 struct vt_mode *mode;
1863                 struct proc *p1;
1864
1865                 mode = (struct vt_mode *)data;
1866                 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
1867                 if (vw->vw_smode.mode == VT_PROCESS) {
1868                         p1 = pfind(vw->vw_pid);
1869                         if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
1870                                 if (p1)
1871                                         PROC_UNLOCK(p1);
1872                                 DPRINTF(5, "error EPERM\n");
1873                                 return (EPERM);
1874                         }
1875                         if (p1)
1876                                 PROC_UNLOCK(p1);
1877                 }
1878                 if (mode->mode == VT_AUTO) {
1879                         vw->vw_smode.mode = VT_AUTO;
1880                         vw->vw_proc = NULL;
1881                         vw->vw_pid = 0;
1882                         DPRINTF(5, "VT_AUTO, ");
1883                         if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1884                                 cnavailable(vw->vw_terminal->consdev, TRUE);
1885                         /* were we in the middle of the vty switching process? */
1886                         if (finish_vt_rel(vw, TRUE, &s) == 0)
1887                                 DPRINTF(5, "reset WAIT_REL, ");
1888                         if (finish_vt_acq(vw) == 0)
1889                                 DPRINTF(5, "reset WAIT_ACQ, ");
1890                         return (0);
1891                 } else if (mode->mode == VT_PROCESS) {
1892                         if (!ISSIGVALID(mode->relsig) ||
1893                             !ISSIGVALID(mode->acqsig) ||
1894                             !ISSIGVALID(mode->frsig)) {
1895                                 DPRINTF(5, "error EINVAL\n");
1896                                 return (EINVAL);
1897                         }
1898                         DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
1899                         bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
1900                         vw->vw_proc = td->td_proc;
1901                         vw->vw_pid = vw->vw_proc->p_pid;
1902                         if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1903                                 cnavailable(vw->vw_terminal->consdev, FALSE);
1904                 } else {
1905                         DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
1906                             mode->mode);
1907                         return (EINVAL);
1908                 }
1909                 DPRINTF(5, "\n");
1910                 return (0);
1911         }
1912         case VT_GETMODE:        /* get screen switcher mode */
1913                 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
1914                 return (0);
1915
1916         case VT_RELDISP:        /* screen switcher ioctl */
1917                 /*
1918                  * This must be the current vty which is in the VT_PROCESS
1919                  * switching mode...
1920                  */
1921                 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
1922                     VT_PROCESS)) {
1923                         return (EINVAL);
1924                 }
1925                 /* ...and this process is controlling it. */
1926                 if (vw->vw_proc != td->td_proc) {
1927                         return (EPERM);
1928                 }
1929                 error = EINVAL;
1930                 switch(*(int *)data) {
1931                 case VT_FALSE:  /* user refuses to release screen, abort */
1932                         if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
1933                                 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
1934                                     SC_DRIVER_NAME, VT_UNIT(vw));
1935                         break;
1936                 case VT_TRUE:   /* user has released screen, go on */
1937                         /* finish_vt_rel(..., TRUE, ...) should not be locked */
1938                         if (vw->vw_flags & VWF_SWWAIT_REL) {
1939                                 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
1940                                         DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
1941                                             SC_DRIVER_NAME, VT_UNIT(vw));
1942                         } else {
1943                                 error = EINVAL;
1944                         }
1945                         return (error);
1946                 case VT_ACKACQ: /* acquire acknowledged, switch completed */
1947                         if ((error = finish_vt_acq(vw)) == 0)
1948                                 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
1949                                     SC_DRIVER_NAME, VT_UNIT(vw));
1950                         break;
1951                 default:
1952                         break;
1953                 }
1954                 return (error);
1955         }
1956
1957         return (ENOIOCTL);
1958 }
1959
1960 static struct vt_window *
1961 vt_allocate_window(struct vt_device *vd, unsigned int window)
1962 {
1963         struct vt_window *vw;
1964         struct terminal *tm;
1965         term_pos_t size;
1966         struct winsize wsz;
1967
1968         vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
1969         vw->vw_device = vd;
1970         vw->vw_number = window;
1971         vw->vw_kbdmode = K_XLATE;
1972
1973         if (!(vd->vd_flags & VDF_TEXTMODE))
1974                 vw->vw_font = vtfont_ref(&vt_font_default);
1975
1976         vt_termsize(vd, vw->vw_font, &size);
1977         vt_winsize(vd, vw->vw_font, &wsz);
1978         vtbuf_init(&vw->vw_buf, &size);
1979
1980         tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
1981         terminal_set_winsize(tm, &wsz);
1982         vd->vd_windows[window] = vw;
1983         callout_init(&vw->vw_proc_dead_timer, 0);
1984
1985         return (vw);
1986 }
1987
1988 void
1989 vt_upgrade(struct vt_device *vd)
1990 {
1991         struct vt_window *vw;
1992         unsigned int i;
1993
1994         for (i = 0; i < VT_MAXWINDOWS; i++) {
1995                 vw = vd->vd_windows[i];
1996                 if (vw == NULL) {
1997                         /* New window. */
1998                         vw = vt_allocate_window(vd, i);
1999                 }
2000                 if (!(vw->vw_flags & VWF_READY)) {
2001                         callout_init(&vw->vw_proc_dead_timer, 0);
2002                         terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2003                         vw->vw_flags |= VWF_READY;
2004                         if (vw->vw_flags & VWF_CONSOLE) {
2005                                 /* For existing console window. */
2006                                 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2007                                     vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2008                         }
2009                 }
2010
2011         }
2012         VT_LOCK(vd);
2013         if (vd->vd_curwindow == NULL)
2014                 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2015
2016         if (!(vd->vd_flags & VDF_ASYNC)) {
2017         /* Attach keyboard. */
2018         vt_allocate_keyboard(vd);
2019         DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
2020
2021                 /* Init 25 Hz timer. */
2022                 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2023
2024                 /* Start timer when everything ready. */
2025                 vd->vd_flags |= VDF_ASYNC;
2026                 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2027         }
2028
2029         VT_UNLOCK(vd);
2030
2031         /* Refill settings with new sizes. */
2032         vt_resize(vd);
2033 }
2034
2035 static void
2036 vt_resize(struct vt_device *vd)
2037 {
2038         struct vt_window *vw;
2039         int i;
2040
2041         for (i = 0; i < VT_MAXWINDOWS; i++) {
2042                 vw = vd->vd_windows[i];
2043                 VT_LOCK(vd);
2044                 /* Assign default font to window, if not textmode. */
2045                 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2046                         vw->vw_font = vtfont_ref(&vt_font_default);
2047                 VT_UNLOCK(vd);
2048                 /* Resize terminal windows */
2049                 vt_change_font(vw, vw->vw_font);
2050         }
2051 }
2052
2053 void
2054 vt_allocate(struct vt_driver *drv, void *softc)
2055 {
2056         struct vt_device *vd;
2057         struct winsize wsz;
2058
2059         if (main_vd->vd_driver == NULL) {
2060                 main_vd->vd_driver = drv;
2061                 printf("VT: initialize with new VT driver \"%s\".\n",
2062                     drv->vd_name);
2063         } else {
2064                 /*
2065                  * Check if have rights to replace current driver. For example:
2066                  * it is bad idea to replace KMS driver with generic VGA one.
2067                  */
2068                 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2069                         printf("VT: Driver priority %d too low. Current %d\n ",
2070                             drv->vd_priority, main_vd->vd_driver->vd_priority);
2071                         return;
2072                 }
2073                 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2074                     main_vd->vd_driver->vd_name, drv->vd_name);
2075         }
2076         vd = main_vd;
2077         VT_LOCK(vd);
2078         if (drv->vd_maskbitbltchr == NULL)
2079                 drv->vd_maskbitbltchr = drv->vd_bitbltchr;
2080
2081         if (vd->vd_flags & VDF_ASYNC) {
2082                 /* Stop vt_flush periodic task. */
2083                 callout_drain(&vd->vd_timer);
2084                 /*
2085                  * Mute current terminal until we done. vt_change_font (called
2086                  * from vt_resize) will unmute it.
2087                  */
2088                 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2089         }
2090
2091         /*
2092          * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2093          * set it.
2094          */
2095         vd->vd_flags &= ~VDF_TEXTMODE;
2096
2097         vd->vd_driver = drv;
2098         vd->vd_softc = softc;
2099         vd->vd_driver->vd_init(vd);
2100         VT_UNLOCK(vd);
2101
2102         vt_upgrade(vd);
2103
2104 #ifdef DEV_SPLASH
2105         if (vd->vd_flags & VDF_SPLASH)
2106                 vtterm_splash(vd);
2107 #endif
2108
2109         if (vd->vd_flags & VDF_ASYNC) {
2110                 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2111                 callout_schedule(&vd->vd_timer, hz / VT_TIMERFREQ);
2112         }
2113
2114         termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2115
2116         /* Update console window sizes to actual. */
2117         vt_winsize(vd, vd->vd_windows[VT_CONSWINDOW]->vw_font, &wsz);
2118         terminal_set_winsize_blank(vd->vd_windows[VT_CONSWINDOW]->vw_terminal,
2119             &wsz, 0);
2120 }
2121
2122 void
2123 vt_suspend()
2124 {
2125
2126         if (vt_suspendswitch == 0)
2127                 return;
2128         /* Save current window. */
2129         main_vd->vd_savedwindow = main_vd->vd_curwindow;
2130         /* Ask holding process to free window and switch to console window */
2131         vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]);
2132 }
2133
2134 void
2135 vt_resume()
2136 {
2137
2138         if (vt_suspendswitch == 0)
2139                 return;
2140         /* Switch back to saved window */
2141         if (main_vd->vd_savedwindow != NULL)
2142                 vt_proc_window_switch(main_vd->vd_savedwindow);
2143         main_vd->vd_savedwindow = NULL;
2144 }