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