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/power.h>
49 #include <sys/priv.h>
50 #include <sys/proc.h>
51 #include <sys/random.h>
52 #include <sys/reboot.h>
53 #include <sys/systm.h>
54 #include <sys/terminal.h>
55
56 #include <dev/kbd/kbdreg.h>
57 #include <dev/vt/vt.h>
58
59 #if defined(__i386__) || defined(__amd64__)
60 #include <machine/psl.h>
61 #include <machine/frame.h>
62 #endif
63
64 static tc_bell_t vtterm_bell;
65 static tc_cursor_t vtterm_cursor;
66 static tc_putchar_t vtterm_putchar;
67 static tc_fill_t vtterm_fill;
68 static tc_copy_t vtterm_copy;
69 static tc_param_t vtterm_param;
70 static tc_done_t vtterm_done;
71
72 static tc_cnprobe_t vtterm_cnprobe;
73 static tc_cngetc_t vtterm_cngetc;
74
75 static tc_cngrab_t vtterm_cngrab;
76 static tc_cnungrab_t vtterm_cnungrab;
77
78 static tc_opened_t vtterm_opened;
79 static tc_ioctl_t vtterm_ioctl;
80 static tc_mmap_t vtterm_mmap;
81
82 const struct terminal_class vt_termclass = {
83 .tc_bell = vtterm_bell,
84 .tc_cursor = vtterm_cursor,
85 .tc_putchar = vtterm_putchar,
86 .tc_fill = vtterm_fill,
87 .tc_copy = vtterm_copy,
88 .tc_param = vtterm_param,
89 .tc_done = vtterm_done,
90
91 .tc_cnprobe = vtterm_cnprobe,
92 .tc_cngetc = vtterm_cngetc,
93
94 .tc_cngrab = vtterm_cngrab,
95 .tc_cnungrab = vtterm_cnungrab,
96
97 .tc_opened = vtterm_opened,
98 .tc_ioctl = vtterm_ioctl,
99 .tc_mmap = vtterm_mmap,
100 };
101
102 /*
103 * Use a constant timer of 25 Hz to redraw the screen.
104 *
105 * XXX: In theory we should only fire up the timer when there is really
106 * activity. Unfortunately we cannot always start timers. We really
107 * don't want to process kernel messages synchronously, because it
108 * really slows down the system.
109 */
110 #define VT_TIMERFREQ 25
111
112 /* Bell pitch/duration. */
113 #define VT_BELLDURATION ((5 * hz + 99) / 100)
114 #define VT_BELLPITCH 800
115
116 #define VT_UNIT(vw) ((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \
117 (vw)->vw_number)
118
119 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD, 0, "vt(9) parameters");
120 static VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)");
121 static VT_SYSCTL_INT(enable_bell, 1, "Enable bell");
122 static VT_SYSCTL_INT(debug, 0, "vt(9) debug level");
123 static VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode");
124 static VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend");
125
126 /* Allow to disable some keyboard combinations. */
127 static VT_SYSCTL_INT(kbd_halt, 1, "Enable halt keyboard combination. "
128 "See kbdmap(5) to configure.");
129 static VT_SYSCTL_INT(kbd_poweroff, 1, "Enable Power Off keyboard combination. "
130 "See kbdmap(5) to configure.");
131 static VT_SYSCTL_INT(kbd_reboot, 1, "Enable reboot keyboard combination. "
132 "See kbdmap(5) to configure (typically Ctrl-Alt-Delete).");
133 static VT_SYSCTL_INT(kbd_debug, 1, "Enable key combination to enter debugger. "
134 "See kbdmap(5) to configure (typically Ctrl-Alt-Esc).");
135 static VT_SYSCTL_INT(kbd_panic, 0, "Enable request to panic. "
136 "See kbdmap(5) to configure.");
137
138 /* Used internally, not a tunable. */
139 int vt_draw_logo_cpus;
140 VT_SYSCTL_INT(splash_cpu, 0, "Show logo CPUs during boot");
141 VT_SYSCTL_INT(splash_ncpu, 0, "Override number of logos displayed "
142 "(0 = do not override)");
143 VT_SYSCTL_INT(splash_cpu_style, 2, "Draw logo style "
144 "(0 = Alternate beastie, 1 = Beastie, 2 = Orb)");
145 VT_SYSCTL_INT(splash_cpu_duration, 10, "Hide logos after (seconds)");
146
147 static unsigned int vt_unit = 0;
148 static MALLOC_DEFINE(M_VT, "vt", "vt device");
149 struct vt_device *main_vd = &vt_consdev;
150
151 /* Boot logo. */
152 extern unsigned int vt_logo_width;
153 extern unsigned int vt_logo_height;
154 extern unsigned int vt_logo_depth;
155 extern unsigned char vt_logo_image[];
156 #ifndef DEV_SPLASH
157 #define vtterm_draw_cpu_logos(...) do {} while (0)
158 const unsigned int vt_logo_sprite_height;
159 #endif
160
161 /* Font. */
162 extern struct vt_font vt_font_default;
163 #ifndef SC_NO_CUTPASTE
164 extern struct vt_mouse_cursor vt_default_mouse_pointer;
165 #endif
166
167 static int signal_vt_rel(struct vt_window *);
168 static int signal_vt_acq(struct vt_window *);
169 static int finish_vt_rel(struct vt_window *, int, int *);
170 static int finish_vt_acq(struct vt_window *);
171 static int vt_window_switch(struct vt_window *);
172 static int vt_late_window_switch(struct vt_window *);
173 static int vt_proc_alive(struct vt_window *);
174 static void vt_resize(struct vt_device *);
175 static void vt_update_static(void *);
176 #ifndef SC_NO_CUTPASTE
177 static void vt_mouse_paste(void);
178 #endif
179 static void vt_suspend_handler(void *priv);
180 static void vt_resume_handler(void *priv);
181
182 SET_DECLARE(vt_drv_set, struct vt_driver);
183
184 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_DEFAULT_HEIGHT))
185 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_DEFAULT_WIDTH))
186
187 struct terminal vt_consterm;
188 static struct vt_window vt_conswindow;
189 struct vt_device vt_consdev = {
190 .vd_driver = NULL,
191 .vd_softc = NULL,
192 .vd_prev_driver = NULL,
193 .vd_prev_softc = NULL,
194 .vd_flags = VDF_INVALID,
195 .vd_windows = { [VT_CONSWINDOW] = &vt_conswindow, },
196 .vd_curwindow = &vt_conswindow,
197 .vd_kbstate = 0,
198
199 #ifndef SC_NO_CUTPASTE
200 .vd_pastebuf = {
201 .vpb_buf = NULL,
202 .vpb_bufsz = 0,
203 .vpb_len = 0
204 },
205 .vd_mcursor = &vt_default_mouse_pointer,
206 .vd_mcursor_fg = TC_WHITE,
207 .vd_mcursor_bg = TC_BLACK,
208 #endif
209 };
210 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)];
211 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE];
212 static struct vt_window vt_conswindow = {
213 .vw_number = VT_CONSWINDOW,
214 .vw_flags = VWF_CONSOLE,
215 .vw_buf = {
216 .vb_buffer = &vt_constextbuf[0],
217 .vb_rows = &vt_constextbufrows[0],
218 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE,
219 .vb_curroffset = 0,
220 .vb_roffset = 0,
221 .vb_flags = VBF_STATIC,
222 .vb_mark_start = {.tp_row = 0, .tp_col = 0,},
223 .vb_mark_end = {.tp_row = 0, .tp_col = 0,},
224 .vb_scr_size = {
225 .tp_row = _VTDEFH,
226 .tp_col = _VTDEFW,
227 },
228 },
229 .vw_device = &vt_consdev,
230 .vw_terminal = &vt_consterm,
231 .vw_kbdmode = K_XLATE,
232 .vw_grabbed = 0,
233 };
234 struct terminal vt_consterm = {
235 .tm_class = &vt_termclass,
236 .tm_softc = &vt_conswindow,
237 .tm_flags = TF_CONS,
238 };
239 static struct consdev vt_consterm_consdev = {
240 .cn_ops = &termcn_cnops,
241 .cn_arg = &vt_consterm,
242 .cn_name = "ttyv0",
243 };
244
245 /* Add to set of consoles. */
246 DATA_SET(cons_set, vt_consterm_consdev);
247
248 /*
249 * Right after kmem is done to allow early drivers to use locking and allocate
250 * memory.
251 */
252 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static,
253 &vt_consdev);
254 /* Delay until all devices attached, to not waste time. */
255 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade,
256 &vt_consdev);
257
258 /* Initialize locks/mem depended members. */
259 static void
vt_update_static(void * dummy)260 vt_update_static(void *dummy)
261 {
262
263 if (!vty_enabled(VTY_VT))
264 return;
265 if (main_vd->vd_driver != NULL)
266 printf("VT(%s): %s %ux%u\n", main_vd->vd_driver->vd_name,
267 (main_vd->vd_flags & VDF_TEXTMODE) ? "text" : "resolution",
268 main_vd->vd_width, main_vd->vd_height);
269 else
270 printf("VT: init without driver.\n");
271
272 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
273 cv_init(&main_vd->vd_winswitch, "vtwswt");
274 }
275
276 static void
vt_schedule_flush(struct vt_device * vd,int ms)277 vt_schedule_flush(struct vt_device *vd, int ms)
278 {
279
280 if (ms <= 0)
281 /* Default to initial value. */
282 ms = 1000 / VT_TIMERFREQ;
283
284 callout_schedule(&vd->vd_timer, hz / (1000 / ms));
285 }
286
287 void
vt_resume_flush_timer(struct vt_device * vd,int ms)288 vt_resume_flush_timer(struct vt_device *vd, int ms)
289 {
290
291 if (!(vd->vd_flags & VDF_ASYNC) ||
292 !atomic_cmpset_int(&vd->vd_timer_armed, 0, 1))
293 return;
294
295 vt_schedule_flush(vd, ms);
296 }
297
298 static void
vt_suspend_flush_timer(struct vt_device * vd)299 vt_suspend_flush_timer(struct vt_device *vd)
300 {
301 /*
302 * As long as this function is called locked, callout_stop()
303 * has the same effect like callout_drain() with regard to
304 * preventing the callback function from executing.
305 */
306 VT_LOCK_ASSERT(vd, MA_OWNED);
307
308 if (!(vd->vd_flags & VDF_ASYNC) ||
309 !atomic_cmpset_int(&vd->vd_timer_armed, 1, 0))
310 return;
311
312 callout_stop(&vd->vd_timer);
313 }
314
315 static void
vt_switch_timer(void * arg)316 vt_switch_timer(void *arg)
317 {
318
319 vt_late_window_switch((struct vt_window *)arg);
320 }
321
322 static int
vt_save_kbd_mode(struct vt_window * vw,keyboard_t * kbd)323 vt_save_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
324 {
325 int mode, ret;
326
327 mode = 0;
328 ret = kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode);
329 if (ret == ENOIOCTL)
330 ret = ENODEV;
331 if (ret != 0)
332 return (ret);
333
334 vw->vw_kbdmode = mode;
335
336 return (0);
337 }
338
339 static int
vt_update_kbd_mode(struct vt_window * vw,keyboard_t * kbd)340 vt_update_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
341 {
342 int ret;
343
344 ret = kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode);
345 if (ret == ENOIOCTL)
346 ret = ENODEV;
347
348 return (ret);
349 }
350
351 static int
vt_save_kbd_state(struct vt_window * vw,keyboard_t * kbd)352 vt_save_kbd_state(struct vt_window *vw, keyboard_t *kbd)
353 {
354 int state, ret;
355
356 state = 0;
357 ret = kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
358 if (ret == ENOIOCTL)
359 ret = ENODEV;
360 if (ret != 0)
361 return (ret);
362
363 vw->vw_kbdstate &= ~LOCK_MASK;
364 vw->vw_kbdstate |= state & LOCK_MASK;
365
366 return (0);
367 }
368
369 static int
vt_update_kbd_state(struct vt_window * vw,keyboard_t * kbd)370 vt_update_kbd_state(struct vt_window *vw, keyboard_t *kbd)
371 {
372 int state, ret;
373
374 state = vw->vw_kbdstate & LOCK_MASK;
375 ret = kbdd_ioctl(kbd, KDSKBSTATE, (caddr_t)&state);
376 if (ret == ENOIOCTL)
377 ret = ENODEV;
378
379 return (ret);
380 }
381
382 static int
vt_save_kbd_leds(struct vt_window * vw,keyboard_t * kbd)383 vt_save_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
384 {
385 int leds, ret;
386
387 leds = 0;
388 ret = kbdd_ioctl(kbd, KDGETLED, (caddr_t)&leds);
389 if (ret == ENOIOCTL)
390 ret = ENODEV;
391 if (ret != 0)
392 return (ret);
393
394 vw->vw_kbdstate &= ~LED_MASK;
395 vw->vw_kbdstate |= leds & LED_MASK;
396
397 return (0);
398 }
399
400 static int
vt_update_kbd_leds(struct vt_window * vw,keyboard_t * kbd)401 vt_update_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
402 {
403 int leds, ret;
404
405 leds = vw->vw_kbdstate & LED_MASK;
406 ret = kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds);
407 if (ret == ENOIOCTL)
408 ret = ENODEV;
409
410 return (ret);
411 }
412
413 static int
vt_window_preswitch(struct vt_window * vw,struct vt_window * curvw)414 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
415 {
416
417 DPRINTF(40, "%s\n", __func__);
418 curvw->vw_switch_to = vw;
419 /* Set timer to allow switch in case when process hang. */
420 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
421 vt_switch_timer, (void *)vw);
422 /* Notify process about vt switch attempt. */
423 DPRINTF(30, "%s: Notify process.\n", __func__);
424 signal_vt_rel(curvw);
425
426 return (0);
427 }
428
429 static int
vt_window_postswitch(struct vt_window * vw)430 vt_window_postswitch(struct vt_window *vw)
431 {
432
433 signal_vt_acq(vw);
434 return (0);
435 }
436
437 /* vt_late_window_switch will done VT switching for regular case. */
438 static int
vt_late_window_switch(struct vt_window * vw)439 vt_late_window_switch(struct vt_window *vw)
440 {
441 int ret;
442
443 callout_stop(&vw->vw_proc_dead_timer);
444
445 ret = vt_window_switch(vw);
446 if (ret)
447 return (ret);
448
449 /* Notify owner process about terminal availability. */
450 if (vw->vw_smode.mode == VT_PROCESS) {
451 ret = vt_window_postswitch(vw);
452 }
453 return (ret);
454 }
455
456 /* Switch window. */
457 static int
vt_proc_window_switch(struct vt_window * vw)458 vt_proc_window_switch(struct vt_window *vw)
459 {
460 struct vt_window *curvw;
461 struct vt_device *vd;
462 int ret;
463
464 /* Prevent switching to NULL */
465 if (vw == NULL) {
466 DPRINTF(30, "%s: Cannot switch: vw is NULL.", __func__);
467 return (EINVAL);
468 }
469 vd = vw->vw_device;
470 curvw = vd->vd_curwindow;
471
472 /* Check if virtual terminal is locked */
473 if (curvw->vw_flags & VWF_VTYLOCK)
474 return (EBUSY);
475
476 /* Check if switch already in progress */
477 if (curvw->vw_flags & VWF_SWWAIT_REL) {
478 /* Check if switching to same window */
479 if (curvw->vw_switch_to == vw) {
480 DPRINTF(30, "%s: Switch in progress to same vw.", __func__);
481 return (0); /* success */
482 }
483 DPRINTF(30, "%s: Switch in progress to different vw.", __func__);
484 return (EBUSY);
485 }
486
487 /* Avoid switching to already selected window */
488 if (vw == curvw) {
489 DPRINTF(30, "%s: Cannot switch: vw == curvw.", __func__);
490 return (0); /* success */
491 }
492
493 /* Ask current process permission to switch away. */
494 if (curvw->vw_smode.mode == VT_PROCESS) {
495 DPRINTF(30, "%s: VT_PROCESS ", __func__);
496 if (vt_proc_alive(curvw) == FALSE) {
497 DPRINTF(30, "Dead. Cleaning.");
498 /* Dead */
499 } else {
500 DPRINTF(30, "%s: Signaling process.\n", __func__);
501 /* Alive, try to ask him. */
502 ret = vt_window_preswitch(vw, curvw);
503 /* Wait for process answer or timeout. */
504 return (ret);
505 }
506 DPRINTF(30, "\n");
507 }
508
509 ret = vt_late_window_switch(vw);
510 return (ret);
511 }
512
513 /* Switch window ignoring process locking. */
514 static int
vt_window_switch(struct vt_window * vw)515 vt_window_switch(struct vt_window *vw)
516 {
517 struct vt_device *vd = vw->vw_device;
518 struct vt_window *curvw = vd->vd_curwindow;
519 keyboard_t *kbd;
520
521 VT_LOCK(vd);
522 if (curvw == vw) {
523 /* Nothing to do. */
524 VT_UNLOCK(vd);
525 return (0);
526 }
527 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
528 VT_UNLOCK(vd);
529 return (EINVAL);
530 }
531
532 vt_suspend_flush_timer(vd);
533
534 vd->vd_curwindow = vw;
535 vd->vd_flags |= VDF_INVALID;
536 cv_broadcast(&vd->vd_winswitch);
537 VT_UNLOCK(vd);
538
539 if (vd->vd_driver->vd_postswitch)
540 vd->vd_driver->vd_postswitch(vd);
541
542 vt_resume_flush_timer(vd, 0);
543
544 /* Restore per-window keyboard mode. */
545 mtx_lock(&Giant);
546 kbd = kbd_get_keyboard(vd->vd_keyboard);
547 if (kbd != NULL) {
548 if (curvw->vw_kbdmode == K_XLATE)
549 vt_save_kbd_state(curvw, kbd);
550
551 vt_update_kbd_mode(vw, kbd);
552 vt_update_kbd_state(vw, kbd);
553 }
554 mtx_unlock(&Giant);
555 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
556
557 return (0);
558 }
559
560 void
vt_termsize(struct vt_device * vd,struct vt_font * vf,term_pos_t * size)561 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
562 {
563
564 size->tp_row = vd->vd_height;
565 if (vt_draw_logo_cpus)
566 size->tp_row -= vt_logo_sprite_height;
567 size->tp_col = vd->vd_width;
568 if (vf != NULL) {
569 size->tp_row /= vf->vf_height;
570 size->tp_col /= vf->vf_width;
571 }
572 }
573
574 static inline void
vt_termrect(struct vt_device * vd,struct vt_font * vf,term_rect_t * rect)575 vt_termrect(struct vt_device *vd, struct vt_font *vf, term_rect_t *rect)
576 {
577
578 rect->tr_begin.tp_row = rect->tr_begin.tp_col = 0;
579 if (vt_draw_logo_cpus)
580 rect->tr_begin.tp_row = vt_logo_sprite_height;
581
582 rect->tr_end.tp_row = vd->vd_height;
583 rect->tr_end.tp_col = vd->vd_width;
584
585 if (vf != NULL) {
586 rect->tr_begin.tp_row =
587 howmany(rect->tr_begin.tp_row, vf->vf_height);
588
589 rect->tr_end.tp_row /= vf->vf_height;
590 rect->tr_end.tp_col /= vf->vf_width;
591 }
592 }
593
594 void
vt_winsize(struct vt_device * vd,struct vt_font * vf,struct winsize * size)595 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
596 {
597
598 size->ws_ypixel = vd->vd_height;
599 if (vt_draw_logo_cpus)
600 size->ws_ypixel -= vt_logo_sprite_height;
601 size->ws_row = size->ws_ypixel;
602 size->ws_col = size->ws_xpixel = vd->vd_width;
603 if (vf != NULL) {
604 size->ws_row /= vf->vf_height;
605 size->ws_col /= vf->vf_width;
606 }
607 }
608
609 void
vt_compute_drawable_area(struct vt_window * vw)610 vt_compute_drawable_area(struct vt_window *vw)
611 {
612 struct vt_device *vd;
613 struct vt_font *vf;
614 vt_axis_t height;
615
616 vd = vw->vw_device;
617
618 if (vw->vw_font == NULL) {
619 vw->vw_draw_area.tr_begin.tp_col = 0;
620 vw->vw_draw_area.tr_begin.tp_row = 0;
621 if (vt_draw_logo_cpus)
622 vw->vw_draw_area.tr_begin.tp_row = vt_logo_sprite_height;
623 vw->vw_draw_area.tr_end.tp_col = vd->vd_width;
624 vw->vw_draw_area.tr_end.tp_row = vd->vd_height;
625 return;
626 }
627
628 vf = vw->vw_font;
629
630 /*
631 * Compute the drawable area, so that the text is centered on
632 * the screen.
633 */
634
635 height = vd->vd_height;
636 if (vt_draw_logo_cpus)
637 height -= vt_logo_sprite_height;
638 vw->vw_draw_area.tr_begin.tp_col = (vd->vd_width % vf->vf_width) / 2;
639 vw->vw_draw_area.tr_begin.tp_row = (height % vf->vf_height) / 2;
640 if (vt_draw_logo_cpus)
641 vw->vw_draw_area.tr_begin.tp_row += vt_logo_sprite_height;
642 vw->vw_draw_area.tr_end.tp_col = vw->vw_draw_area.tr_begin.tp_col +
643 vd->vd_width / vf->vf_width * vf->vf_width;
644 vw->vw_draw_area.tr_end.tp_row = vw->vw_draw_area.tr_begin.tp_row +
645 height / vf->vf_height * vf->vf_height;
646 }
647
648 static void
vt_scroll(struct vt_window * vw,int offset,int whence)649 vt_scroll(struct vt_window *vw, int offset, int whence)
650 {
651 int diff;
652 term_pos_t size;
653
654 if ((vw->vw_flags & VWF_SCROLL) == 0)
655 return;
656
657 vt_termsize(vw->vw_device, vw->vw_font, &size);
658
659 diff = vthistory_seek(&vw->vw_buf, offset, whence);
660 if (diff)
661 vw->vw_device->vd_flags |= VDF_INVALID;
662 vt_resume_flush_timer(vw->vw_device, 0);
663 }
664
665 static int
vt_machine_kbdevent(int c)666 vt_machine_kbdevent(int c)
667 {
668
669 switch (c) {
670 case SPCLKEY | DBG: /* kbdmap(5) keyword `debug`. */
671 if (vt_kbd_debug)
672 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
673 return (1);
674 case SPCLKEY | HALT: /* kbdmap(5) keyword `halt`. */
675 if (vt_kbd_halt)
676 shutdown_nice(RB_HALT);
677 return (1);
678 case SPCLKEY | PASTE: /* kbdmap(5) keyword `paste`. */
679 #ifndef SC_NO_CUTPASTE
680 /* Insert text from cut-paste buffer. */
681 vt_mouse_paste();
682 #endif
683 break;
684 case SPCLKEY | PDWN: /* kbdmap(5) keyword `pdwn`. */
685 if (vt_kbd_poweroff)
686 shutdown_nice(RB_HALT|RB_POWEROFF);
687 return (1);
688 case SPCLKEY | PNC: /* kbdmap(5) keyword `panic`. */
689 /*
690 * Request to immediate panic if sysctl
691 * kern.vt.enable_panic_key allow it.
692 */
693 if (vt_kbd_panic)
694 panic("Forced by the panic key");
695 return (1);
696 case SPCLKEY | RBT: /* kbdmap(5) keyword `boot`. */
697 if (vt_kbd_reboot)
698 shutdown_nice(RB_AUTOBOOT);
699 return (1);
700 case SPCLKEY | SPSC: /* kbdmap(5) keyword `spsc`. */
701 /* Force activatation/deactivation of the screen saver. */
702 /* TODO */
703 return (1);
704 case SPCLKEY | STBY: /* XXX Not present in kbdcontrol parser. */
705 /* Put machine into Stand-By mode. */
706 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
707 return (1);
708 case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */
709 /* Suspend machine. */
710 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
711 return (1);
712 };
713
714 return (0);
715 }
716
717 static void
vt_scrollmode_kbdevent(struct vt_window * vw,int c,int console)718 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
719 {
720 struct vt_device *vd;
721 term_pos_t size;
722
723 vd = vw->vw_device;
724 /* Only special keys handled in ScrollLock mode */
725 if ((c & SPCLKEY) == 0)
726 return;
727
728 c &= ~SPCLKEY;
729
730 if (console == 0) {
731 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
732 vw = vd->vd_windows[c - F_SCR];
733 vt_proc_window_switch(vw);
734 return;
735 }
736 VT_LOCK(vd);
737 }
738
739 switch (c) {
740 case SLK: {
741 /* Turn scrolling off. */
742 vt_scroll(vw, 0, VHS_END);
743 VTBUF_SLCK_DISABLE(&vw->vw_buf);
744 vw->vw_flags &= ~VWF_SCROLL;
745 break;
746 }
747 case FKEY | F(49): /* Home key. */
748 vt_scroll(vw, 0, VHS_SET);
749 break;
750 case FKEY | F(50): /* Arrow up. */
751 vt_scroll(vw, -1, VHS_CUR);
752 break;
753 case FKEY | F(51): /* Page up. */
754 vt_termsize(vd, vw->vw_font, &size);
755 vt_scroll(vw, -size.tp_row, VHS_CUR);
756 break;
757 case FKEY | F(57): /* End key. */
758 vt_scroll(vw, 0, VHS_END);
759 break;
760 case FKEY | F(58): /* Arrow down. */
761 vt_scroll(vw, 1, VHS_CUR);
762 break;
763 case FKEY | F(59): /* Page down. */
764 vt_termsize(vd, vw->vw_font, &size);
765 vt_scroll(vw, size.tp_row, VHS_CUR);
766 break;
767 }
768
769 if (console == 0)
770 VT_UNLOCK(vd);
771 }
772
773 static int
vt_processkey(keyboard_t * kbd,struct vt_device * vd,int c)774 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
775 {
776 struct vt_window *vw = vd->vd_curwindow;
777
778 random_harvest_queue(&c, sizeof(c), 1, RANDOM_KEYBOARD);
779 #if VT_ALT_TO_ESC_HACK
780 if (c & RELKEY) {
781 switch (c & ~RELKEY) {
782 case (SPCLKEY | RALT):
783 if (vt_enable_altgr != 0)
784 break;
785 case (SPCLKEY | LALT):
786 vd->vd_kbstate &= ~ALKED;
787 }
788 /* Other keys ignored for RELKEY event. */
789 return (0);
790 } else {
791 switch (c & ~RELKEY) {
792 case (SPCLKEY | RALT):
793 if (vt_enable_altgr != 0)
794 break;
795 case (SPCLKEY | LALT):
796 vd->vd_kbstate |= ALKED;
797 }
798 }
799 #else
800 if (c & RELKEY)
801 /* Other keys ignored for RELKEY event. */
802 return (0);
803 #endif
804
805 if (vt_machine_kbdevent(c))
806 return (0);
807
808 if (vw->vw_flags & VWF_SCROLL) {
809 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
810 /* Scroll mode keys handled, nothing to do more. */
811 return (0);
812 }
813
814 if (c & SPCLKEY) {
815 c &= ~SPCLKEY;
816
817 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
818 vw = vd->vd_windows[c - F_SCR];
819 vt_proc_window_switch(vw);
820 return (0);
821 }
822
823 switch (c) {
824 case NEXT:
825 /* Switch to next VT. */
826 c = (vw->vw_number + 1) % VT_MAXWINDOWS;
827 vw = vd->vd_windows[c];
828 vt_proc_window_switch(vw);
829 return (0);
830 case PREV:
831 /* Switch to previous VT. */
832 c = (vw->vw_number + VT_MAXWINDOWS - 1) % VT_MAXWINDOWS;
833 vw = vd->vd_windows[c];
834 vt_proc_window_switch(vw);
835 return (0);
836 case SLK: {
837 vt_save_kbd_state(vw, kbd);
838 VT_LOCK(vd);
839 if (vw->vw_kbdstate & SLKED) {
840 /* Turn scrolling on. */
841 vw->vw_flags |= VWF_SCROLL;
842 VTBUF_SLCK_ENABLE(&vw->vw_buf);
843 } else {
844 /* Turn scrolling off. */
845 vw->vw_flags &= ~VWF_SCROLL;
846 VTBUF_SLCK_DISABLE(&vw->vw_buf);
847 vt_scroll(vw, 0, VHS_END);
848 }
849 VT_UNLOCK(vd);
850 break;
851 }
852 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
853 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
854 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
855 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
856 /* F1 through F12 keys. */
857 terminal_input_special(vw->vw_terminal,
858 TKEY_F1 + c - (FKEY | F(1)));
859 break;
860 case FKEY | F(49): /* Home key. */
861 terminal_input_special(vw->vw_terminal, TKEY_HOME);
862 break;
863 case FKEY | F(50): /* Arrow up. */
864 terminal_input_special(vw->vw_terminal, TKEY_UP);
865 break;
866 case FKEY | F(51): /* Page up. */
867 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
868 break;
869 case FKEY | F(53): /* Arrow left. */
870 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
871 break;
872 case FKEY | F(55): /* Arrow right. */
873 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
874 break;
875 case FKEY | F(57): /* End key. */
876 terminal_input_special(vw->vw_terminal, TKEY_END);
877 break;
878 case FKEY | F(58): /* Arrow down. */
879 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
880 break;
881 case FKEY | F(59): /* Page down. */
882 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
883 break;
884 case FKEY | F(60): /* Insert key. */
885 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
886 break;
887 case FKEY | F(61): /* Delete key. */
888 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
889 break;
890 }
891 } else if (KEYFLAGS(c) == 0) {
892 /* Don't do UTF-8 conversion when doing raw mode. */
893 if (vw->vw_kbdmode == K_XLATE) {
894 #if VT_ALT_TO_ESC_HACK
895 if (vd->vd_kbstate & ALKED) {
896 /*
897 * Prepend ESC sequence if one of ALT keys down.
898 */
899 terminal_input_char(vw->vw_terminal, 0x1b);
900 }
901 #endif
902
903 #if defined(KDB)
904 kdb_alt_break(c, &vd->vd_altbrk);
905 #endif
906 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
907 } else
908 terminal_input_raw(vw->vw_terminal, c);
909 }
910 return (0);
911 }
912
913 static int
vt_kbdevent(keyboard_t * kbd,int event,void * arg)914 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
915 {
916 struct vt_device *vd = arg;
917 int c;
918
919 switch (event) {
920 case KBDIO_KEYINPUT:
921 break;
922 case KBDIO_UNLOADING:
923 mtx_lock(&Giant);
924 vd->vd_keyboard = -1;
925 kbd_release(kbd, (void *)vd);
926 mtx_unlock(&Giant);
927 return (0);
928 default:
929 return (EINVAL);
930 }
931
932 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
933 vt_processkey(kbd, vd, c);
934
935 return (0);
936 }
937
938 static int
vt_allocate_keyboard(struct vt_device * vd)939 vt_allocate_keyboard(struct vt_device *vd)
940 {
941 int idx0, idx;
942 keyboard_t *k0, *k;
943 keyboard_info_t ki;
944
945 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
946 if (idx0 >= 0) {
947 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
948 k0 = kbd_get_keyboard(idx0);
949
950 for (idx = kbd_find_keyboard2("*", -1, 0);
951 idx != -1;
952 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
953 k = kbd_get_keyboard(idx);
954
955 if (idx == idx0 || KBD_IS_BUSY(k))
956 continue;
957
958 bzero(&ki, sizeof(ki));
959 strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name));
960 ki.kb_name[sizeof(ki.kb_name) - 1] = '\0';
961 ki.kb_unit = k->kb_unit;
962
963 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
964 }
965 } else {
966 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
967 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
968 if (idx0 < 0) {
969 DPRINTF(10, "%s: No keyboard found.\n", __func__);
970 return (-1);
971 }
972 }
973 vd->vd_keyboard = idx0;
974 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
975
976 return (idx0);
977 }
978
979 static void
vtterm_bell(struct terminal * tm)980 vtterm_bell(struct terminal *tm)
981 {
982 struct vt_window *vw = tm->tm_softc;
983 struct vt_device *vd = vw->vw_device;
984
985 if (!vt_enable_bell)
986 return;
987
988 if (vd->vd_flags & VDF_QUIET_BELL)
989 return;
990
991 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
992 }
993
994 static void
vtterm_beep(struct terminal * tm,u_int param)995 vtterm_beep(struct terminal *tm, u_int param)
996 {
997 u_int freq, period;
998
999 if (!vt_enable_bell)
1000 return;
1001
1002 if ((param == 0) || ((param & 0xffff) == 0)) {
1003 vtterm_bell(tm);
1004 return;
1005 }
1006
1007 period = ((param >> 16) & 0xffff) * hz / 1000;
1008 freq = 1193182 / (param & 0xffff);
1009
1010 sysbeep(freq, period);
1011 }
1012
1013 static void
vtterm_cursor(struct terminal * tm,const term_pos_t * p)1014 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
1015 {
1016 struct vt_window *vw = tm->tm_softc;
1017
1018 vtbuf_cursor_position(&vw->vw_buf, p);
1019 vt_resume_flush_timer(vw->vw_device, 0);
1020 }
1021
1022 static void
vtterm_putchar(struct terminal * tm,const term_pos_t * p,term_char_t c)1023 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
1024 {
1025 struct vt_window *vw = tm->tm_softc;
1026
1027 vtbuf_putchar(&vw->vw_buf, p, c);
1028 vt_resume_flush_timer(vw->vw_device, 0);
1029 }
1030
1031 static void
vtterm_fill(struct terminal * tm,const term_rect_t * r,term_char_t c)1032 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
1033 {
1034 struct vt_window *vw = tm->tm_softc;
1035
1036 vtbuf_fill_locked(&vw->vw_buf, r, c);
1037 vt_resume_flush_timer(vw->vw_device, 0);
1038 }
1039
1040 static void
vtterm_copy(struct terminal * tm,const term_rect_t * r,const term_pos_t * p)1041 vtterm_copy(struct terminal *tm, const term_rect_t *r,
1042 const term_pos_t *p)
1043 {
1044 struct vt_window *vw = tm->tm_softc;
1045
1046 vtbuf_copy(&vw->vw_buf, r, p);
1047 vt_resume_flush_timer(vw->vw_device, 0);
1048 }
1049
1050 static void
vtterm_param(struct terminal * tm,int cmd,unsigned int arg)1051 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
1052 {
1053 struct vt_window *vw = tm->tm_softc;
1054
1055 switch (cmd) {
1056 case TP_SHOWCURSOR:
1057 vtbuf_cursor_visibility(&vw->vw_buf, arg);
1058 vt_resume_flush_timer(vw->vw_device, 0);
1059 break;
1060 case TP_MOUSE:
1061 vw->vw_mouse_level = arg;
1062 break;
1063 }
1064 }
1065
1066 void
vt_determine_colors(term_char_t c,int cursor,term_color_t * fg,term_color_t * bg)1067 vt_determine_colors(term_char_t c, int cursor,
1068 term_color_t *fg, term_color_t *bg)
1069 {
1070 term_color_t tmp;
1071 int invert;
1072
1073 invert = 0;
1074
1075 *fg = TCHAR_FGCOLOR(c);
1076 if (TCHAR_FORMAT(c) & TF_BOLD)
1077 *fg = TCOLOR_LIGHT(*fg);
1078 *bg = TCHAR_BGCOLOR(c);
1079
1080 if (TCHAR_FORMAT(c) & TF_REVERSE)
1081 invert ^= 1;
1082 if (cursor)
1083 invert ^= 1;
1084
1085 if (invert) {
1086 tmp = *fg;
1087 *fg = *bg;
1088 *bg = tmp;
1089 }
1090 }
1091
1092 #ifndef SC_NO_CUTPASTE
1093 int
vt_is_cursor_in_area(const struct vt_device * vd,const term_rect_t * area)1094 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area)
1095 {
1096 unsigned int mx, my;
1097
1098 /*
1099 * We use the cursor position saved during the current refresh,
1100 * in case the cursor moved since.
1101 */
1102 mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col;
1103 my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row;
1104
1105 if (mx >= area->tr_end.tp_col ||
1106 mx + vd->vd_mcursor->width <= area->tr_begin.tp_col ||
1107 my >= area->tr_end.tp_row ||
1108 my + vd->vd_mcursor->height <= area->tr_begin.tp_row)
1109 return (0);
1110 return (1);
1111 }
1112
1113 static void
vt_mark_mouse_position_as_dirty(struct vt_device * vd)1114 vt_mark_mouse_position_as_dirty(struct vt_device *vd)
1115 {
1116 term_rect_t area;
1117 struct vt_window *vw;
1118 struct vt_font *vf;
1119 int x, y;
1120
1121 vw = vd->vd_curwindow;
1122 vf = vw->vw_font;
1123
1124 x = vd->vd_mx_drawn;
1125 y = vd->vd_my_drawn;
1126
1127 if (vf != NULL) {
1128 area.tr_begin.tp_col = x / vf->vf_width;
1129 area.tr_begin.tp_row = y / vf->vf_height;
1130 area.tr_end.tp_col =
1131 ((x + vd->vd_mcursor->width) / vf->vf_width) + 1;
1132 area.tr_end.tp_row =
1133 ((y + vd->vd_mcursor->height) / vf->vf_height) + 1;
1134 } else {
1135 /*
1136 * No font loaded (ie. vt_vga operating in textmode).
1137 *
1138 * FIXME: This fake area needs to be revisited once the
1139 * mouse cursor is supported in vt_vga's textmode.
1140 */
1141 area.tr_begin.tp_col = x;
1142 area.tr_begin.tp_row = y;
1143 area.tr_end.tp_col = x + 2;
1144 area.tr_end.tp_row = y + 2;
1145 }
1146
1147 vtbuf_dirty(&vw->vw_buf, &area);
1148 }
1149 #endif
1150
1151 static int
vt_flush(struct vt_device * vd)1152 vt_flush(struct vt_device *vd)
1153 {
1154 struct vt_window *vw;
1155 struct vt_font *vf;
1156 term_rect_t tarea;
1157 #ifndef SC_NO_CUTPASTE
1158 int cursor_was_shown, cursor_moved;
1159 #endif
1160
1161 vw = vd->vd_curwindow;
1162 if (vw == NULL)
1163 return (0);
1164
1165 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
1166 return (0);
1167
1168 vf = vw->vw_font;
1169 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
1170 return (0);
1171
1172 #ifndef SC_NO_CUTPASTE
1173 cursor_was_shown = vd->vd_mshown;
1174 cursor_moved = (vd->vd_mx != vd->vd_mx_drawn ||
1175 vd->vd_my != vd->vd_my_drawn);
1176
1177 /* Check if the cursor should be displayed or not. */
1178 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
1179 !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed. */
1180 !kdb_active && panicstr == NULL) { /* DDB inactive. */
1181 vd->vd_mshown = 1;
1182 } else {
1183 vd->vd_mshown = 0;
1184 }
1185
1186 /*
1187 * If the cursor changed display state or moved, we must mark
1188 * the old position as dirty, so that it's erased.
1189 */
1190 if (cursor_was_shown != vd->vd_mshown ||
1191 (vd->vd_mshown && cursor_moved))
1192 vt_mark_mouse_position_as_dirty(vd);
1193
1194 /*
1195 * Save position of the mouse cursor. It's used by backends to
1196 * know where to draw the cursor and during the next refresh to
1197 * erase the previous position.
1198 */
1199 vd->vd_mx_drawn = vd->vd_mx;
1200 vd->vd_my_drawn = vd->vd_my;
1201
1202 /*
1203 * If the cursor is displayed and has moved since last refresh,
1204 * mark the new position as dirty.
1205 */
1206 if (vd->vd_mshown && cursor_moved)
1207 vt_mark_mouse_position_as_dirty(vd);
1208 #endif
1209
1210 vtbuf_undirty(&vw->vw_buf, &tarea);
1211
1212 /* Force a full redraw when the screen contents are invalid. */
1213 if (vd->vd_flags & VDF_INVALID) {
1214 vd->vd_flags &= ~VDF_INVALID;
1215
1216 vt_termrect(vd, vf, &tarea);
1217 if (vt_draw_logo_cpus)
1218 vtterm_draw_cpu_logos(vd);
1219 }
1220
1221 if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
1222 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea);
1223 return (1);
1224 }
1225
1226 return (0);
1227 }
1228
1229 static void
vt_timer(void * arg)1230 vt_timer(void *arg)
1231 {
1232 struct vt_device *vd;
1233 int changed;
1234
1235 vd = arg;
1236 /* Update screen if required. */
1237 changed = vt_flush(vd);
1238
1239 /* Schedule for next update. */
1240 if (changed)
1241 vt_schedule_flush(vd, 0);
1242 else
1243 vd->vd_timer_armed = 0;
1244 }
1245
1246 static void
vtterm_done(struct terminal * tm)1247 vtterm_done(struct terminal *tm)
1248 {
1249 struct vt_window *vw = tm->tm_softc;
1250 struct vt_device *vd = vw->vw_device;
1251
1252 if (kdb_active || panicstr != NULL) {
1253 /* Switch to the debugger. */
1254 if (vd->vd_curwindow != vw) {
1255 vd->vd_curwindow = vw;
1256 vd->vd_flags |= VDF_INVALID;
1257 if (vd->vd_driver->vd_postswitch)
1258 vd->vd_driver->vd_postswitch(vd);
1259 }
1260 vd->vd_flags &= ~VDF_SPLASH;
1261 vt_flush(vd);
1262 } else if (!(vd->vd_flags & VDF_ASYNC)) {
1263 vt_flush(vd);
1264 }
1265 }
1266
1267 #ifdef DEV_SPLASH
1268 static void
vtterm_splash(struct vt_device * vd)1269 vtterm_splash(struct vt_device *vd)
1270 {
1271 vt_axis_t top, left;
1272
1273 /* Display a nice boot splash. */
1274 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1275
1276 top = (vd->vd_height - vt_logo_height) / 2;
1277 left = (vd->vd_width - vt_logo_width) / 2;
1278 switch (vt_logo_depth) {
1279 case 1:
1280 /* XXX: Unhardcode colors! */
1281 vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow,
1282 vt_logo_image, NULL, vt_logo_width, vt_logo_height,
1283 left, top, TC_WHITE, TC_BLACK);
1284 }
1285 vd->vd_flags |= VDF_SPLASH;
1286 }
1287 }
1288 #endif
1289
1290
1291 static void
vtterm_cnprobe(struct terminal * tm,struct consdev * cp)1292 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1293 {
1294 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1295 struct vt_window *vw = tm->tm_softc;
1296 struct vt_device *vd = vw->vw_device;
1297 struct winsize wsz;
1298 term_attr_t attr;
1299 term_char_t c;
1300
1301 if (!vty_enabled(VTY_VT))
1302 return;
1303
1304 if (vd->vd_flags & VDF_INITIALIZED)
1305 /* Initialization already done. */
1306 return;
1307
1308 SET_FOREACH(vtdlist, vt_drv_set) {
1309 vtd = *vtdlist;
1310 if (vtd->vd_probe == NULL)
1311 continue;
1312 if (vtd->vd_probe(vd) == CN_DEAD)
1313 continue;
1314 if ((vtdbest == NULL) ||
1315 (vtd->vd_priority > vtdbest->vd_priority))
1316 vtdbest = vtd;
1317 }
1318 if (vtdbest == NULL) {
1319 cp->cn_pri = CN_DEAD;
1320 vd->vd_flags |= VDF_DEAD;
1321 } else {
1322 vd->vd_driver = vtdbest;
1323 cp->cn_pri = vd->vd_driver->vd_init(vd);
1324 }
1325
1326 /* Check if driver's vt_init return CN_DEAD. */
1327 if (cp->cn_pri == CN_DEAD) {
1328 vd->vd_flags |= VDF_DEAD;
1329 }
1330
1331 /* Initialize any early-boot keyboard drivers */
1332 kbd_configure(KB_CONF_PROBE_ONLY);
1333
1334 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1335 vd->vd_windows[VT_CONSWINDOW] = vw;
1336 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1337
1338 /* Attach default font if not in TEXTMODE. */
1339 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
1340 vw->vw_font = vtfont_ref(&vt_font_default);
1341 vt_compute_drawable_area(vw);
1342 }
1343
1344 /*
1345 * The original screen size was faked (_VTDEFW x _VTDEFH). Now
1346 * that we have the real viewable size, fix it in the static
1347 * buffer.
1348 */
1349 if (vd->vd_width != 0 && vd->vd_height != 0)
1350 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size);
1351
1352 vtbuf_init_early(&vw->vw_buf);
1353 vt_winsize(vd, vw->vw_font, &wsz);
1354 c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1355 TERMINAL_NORM_ATTR;
1356 attr.ta_format = TCHAR_FORMAT(c);
1357 attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1358 attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1359 terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1360
1361 if (vtdbest != NULL) {
1362 #ifdef DEV_SPLASH
1363 if (!vt_splash_cpu)
1364 vtterm_splash(vd);
1365 #endif
1366 vd->vd_flags |= VDF_INITIALIZED;
1367 }
1368 }
1369
1370 static int
vtterm_cngetc(struct terminal * tm)1371 vtterm_cngetc(struct terminal *tm)
1372 {
1373 struct vt_window *vw = tm->tm_softc;
1374 struct vt_device *vd = vw->vw_device;
1375 keyboard_t *kbd;
1376 u_int c;
1377
1378 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1379 return (*vw->vw_kbdsq++);
1380
1381 /* Make sure the splash screen is not there. */
1382 if (vd->vd_flags & VDF_SPLASH) {
1383 /* Remove splash */
1384 vd->vd_flags &= ~VDF_SPLASH;
1385 /* Mark screen as invalid to force update */
1386 vd->vd_flags |= VDF_INVALID;
1387 vt_flush(vd);
1388 }
1389
1390 /* Stripped down keyboard handler. */
1391 kbd = kbd_get_keyboard(vd->vd_keyboard);
1392 if (kbd == NULL)
1393 return (-1);
1394
1395 /* Force keyboard input mode to K_XLATE */
1396 vw->vw_kbdmode = K_XLATE;
1397 vt_update_kbd_mode(vw, kbd);
1398
1399 /* Switch the keyboard to polling to make it work here. */
1400 kbdd_poll(kbd, TRUE);
1401 c = kbdd_read_char(kbd, 0);
1402 kbdd_poll(kbd, FALSE);
1403 if (c & RELKEY)
1404 return (-1);
1405
1406 if (vw->vw_flags & VWF_SCROLL) {
1407 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1408 vt_flush(vd);
1409 return (-1);
1410 }
1411
1412 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1413 if (c & SPCLKEY) {
1414 switch (c) {
1415 case SPCLKEY | SLK:
1416 vt_save_kbd_state(vw, kbd);
1417 if (vw->vw_kbdstate & SLKED) {
1418 /* Turn scrolling on. */
1419 vw->vw_flags |= VWF_SCROLL;
1420 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1421 } else {
1422 /* Turn scrolling off. */
1423 vt_scroll(vw, 0, VHS_END);
1424 vw->vw_flags &= ~VWF_SCROLL;
1425 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1426 }
1427 break;
1428 /* XXX: KDB can handle history. */
1429 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1430 vw->vw_kbdsq = "\x1b[A";
1431 break;
1432 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1433 vw->vw_kbdsq = "\x1b[B";
1434 break;
1435 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1436 vw->vw_kbdsq = "\x1b[C";
1437 break;
1438 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1439 vw->vw_kbdsq = "\x1b[D";
1440 break;
1441 }
1442
1443 /* Force refresh to make scrollback work. */
1444 vt_flush(vd);
1445 } else if (KEYFLAGS(c) == 0) {
1446 return (KEYCHAR(c));
1447 }
1448
1449 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1450 return (*vw->vw_kbdsq++);
1451
1452 return (-1);
1453 }
1454
1455 static void
vtterm_cngrab(struct terminal * tm)1456 vtterm_cngrab(struct terminal *tm)
1457 {
1458 struct vt_device *vd;
1459 struct vt_window *vw;
1460 keyboard_t *kbd;
1461
1462 vw = tm->tm_softc;
1463 vd = vw->vw_device;
1464
1465 if (!cold)
1466 vt_window_switch(vw);
1467
1468 kbd = kbd_get_keyboard(vd->vd_keyboard);
1469 if (kbd == NULL)
1470 return;
1471
1472 if (vw->vw_grabbed++ > 0)
1473 return;
1474
1475 /*
1476 * Make sure the keyboard is accessible even when the kbd device
1477 * driver is disabled.
1478 */
1479 kbdd_enable(kbd);
1480
1481 /* We shall always use the keyboard in the XLATE mode here. */
1482 vw->vw_prev_kbdmode = vw->vw_kbdmode;
1483 vw->vw_kbdmode = K_XLATE;
1484 vt_update_kbd_mode(vw, kbd);
1485
1486 kbdd_poll(kbd, TRUE);
1487 }
1488
1489 static void
vtterm_cnungrab(struct terminal * tm)1490 vtterm_cnungrab(struct terminal *tm)
1491 {
1492 struct vt_device *vd;
1493 struct vt_window *vw;
1494 keyboard_t *kbd;
1495
1496 vw = tm->tm_softc;
1497 vd = vw->vw_device;
1498
1499 kbd = kbd_get_keyboard(vd->vd_keyboard);
1500 if (kbd == NULL)
1501 return;
1502
1503 if (--vw->vw_grabbed > 0)
1504 return;
1505
1506 kbdd_poll(kbd, FALSE);
1507
1508 vw->vw_kbdmode = vw->vw_prev_kbdmode;
1509 vt_update_kbd_mode(vw, kbd);
1510 kbdd_disable(kbd);
1511 }
1512
1513 static void
vtterm_opened(struct terminal * tm,int opened)1514 vtterm_opened(struct terminal *tm, int opened)
1515 {
1516 struct vt_window *vw = tm->tm_softc;
1517 struct vt_device *vd = vw->vw_device;
1518
1519 VT_LOCK(vd);
1520 vd->vd_flags &= ~VDF_SPLASH;
1521 if (opened)
1522 vw->vw_flags |= VWF_OPENED;
1523 else {
1524 vw->vw_flags &= ~VWF_OPENED;
1525 /* TODO: finish ACQ/REL */
1526 }
1527 VT_UNLOCK(vd);
1528 }
1529
1530 static int
vt_set_border(struct vt_window * vw,term_color_t c)1531 vt_set_border(struct vt_window *vw, term_color_t c)
1532 {
1533 struct vt_device *vd = vw->vw_device;
1534
1535 if (vd->vd_driver->vd_drawrect == NULL)
1536 return (ENOTSUP);
1537
1538 /* Top bar. */
1539 if (vw->vw_draw_area.tr_begin.tp_row > 0)
1540 vd->vd_driver->vd_drawrect(vd,
1541 0, 0,
1542 vd->vd_width - 1, vw->vw_draw_area.tr_begin.tp_row - 1,
1543 1, c);
1544
1545 /* Left bar. */
1546 if (vw->vw_draw_area.tr_begin.tp_col > 0)
1547 vd->vd_driver->vd_drawrect(vd,
1548 0, 0,
1549 vw->vw_draw_area.tr_begin.tp_col - 1, vd->vd_height - 1,
1550 1, c);
1551
1552 /* Right bar. */
1553 if (vw->vw_draw_area.tr_end.tp_col < vd->vd_width)
1554 vd->vd_driver->vd_drawrect(vd,
1555 vw->vw_draw_area.tr_end.tp_col - 1, 0,
1556 vd->vd_width - 1, vd->vd_height - 1,
1557 1, c);
1558
1559 /* Bottom bar. */
1560 if (vw->vw_draw_area.tr_end.tp_row < vd->vd_height)
1561 vd->vd_driver->vd_drawrect(vd,
1562 0, vw->vw_draw_area.tr_end.tp_row - 1,
1563 vd->vd_width - 1, vd->vd_height - 1,
1564 1, c);
1565
1566 return (0);
1567 }
1568
1569 static int
vt_change_font(struct vt_window * vw,struct vt_font * vf)1570 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1571 {
1572 struct vt_device *vd = vw->vw_device;
1573 struct terminal *tm = vw->vw_terminal;
1574 term_pos_t size;
1575 struct winsize wsz;
1576
1577 /*
1578 * Changing fonts.
1579 *
1580 * Changing fonts is a little tricky. We must prevent
1581 * simultaneous access to the device, so we must stop
1582 * the display timer and the terminal from accessing.
1583 * We need to switch fonts and grow our screen buffer.
1584 *
1585 * XXX: Right now the code uses terminal_mute() to
1586 * prevent data from reaching the console driver while
1587 * resizing the screen buffer. This isn't elegant...
1588 */
1589
1590 VT_LOCK(vd);
1591 if (vw->vw_flags & VWF_BUSY) {
1592 /* Another process is changing the font. */
1593 VT_UNLOCK(vd);
1594 return (EBUSY);
1595 }
1596 vw->vw_flags |= VWF_BUSY;
1597 VT_UNLOCK(vd);
1598
1599 vt_termsize(vd, vf, &size);
1600 vt_winsize(vd, vf, &wsz);
1601
1602 /* Grow the screen buffer and terminal. */
1603 terminal_mute(tm, 1);
1604 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1605 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1606 terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
1607 terminal_mute(tm, 0);
1608
1609 /* Actually apply the font to the current window. */
1610 VT_LOCK(vd);
1611 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
1612 /*
1613 * In case vt_change_font called to update size we don't need
1614 * to update font link.
1615 */
1616 vtfont_unref(vw->vw_font);
1617 vw->vw_font = vtfont_ref(vf);
1618 }
1619
1620 /*
1621 * Compute the drawable area and move the mouse cursor inside
1622 * it, in case the new area is smaller than the previous one.
1623 */
1624 vt_compute_drawable_area(vw);
1625 vd->vd_mx = min(vd->vd_mx,
1626 vw->vw_draw_area.tr_end.tp_col -
1627 vw->vw_draw_area.tr_begin.tp_col - 1);
1628 vd->vd_my = min(vd->vd_my,
1629 vw->vw_draw_area.tr_end.tp_row -
1630 vw->vw_draw_area.tr_begin.tp_row - 1);
1631
1632 /* Force a full redraw the next timer tick. */
1633 if (vd->vd_curwindow == vw) {
1634 vt_set_border(vw, TC_BLACK);
1635 vd->vd_flags |= VDF_INVALID;
1636 vt_resume_flush_timer(vw->vw_device, 0);
1637 }
1638 vw->vw_flags &= ~VWF_BUSY;
1639 VT_UNLOCK(vd);
1640 return (0);
1641 }
1642
1643 static int
vt_proc_alive(struct vt_window * vw)1644 vt_proc_alive(struct vt_window *vw)
1645 {
1646 struct proc *p;
1647
1648 if (vw->vw_smode.mode != VT_PROCESS)
1649 return (FALSE);
1650
1651 if (vw->vw_proc) {
1652 if ((p = pfind(vw->vw_pid)) != NULL)
1653 PROC_UNLOCK(p);
1654 if (vw->vw_proc == p)
1655 return (TRUE);
1656 vw->vw_proc = NULL;
1657 vw->vw_smode.mode = VT_AUTO;
1658 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1659 vw->vw_pid = 0;
1660 }
1661 return (FALSE);
1662 }
1663
1664 static int
signal_vt_rel(struct vt_window * vw)1665 signal_vt_rel(struct vt_window *vw)
1666 {
1667
1668 if (vw->vw_smode.mode != VT_PROCESS)
1669 return (FALSE);
1670 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1671 vw->vw_proc = NULL;
1672 vw->vw_pid = 0;
1673 return (TRUE);
1674 }
1675 vw->vw_flags |= VWF_SWWAIT_REL;
1676 PROC_LOCK(vw->vw_proc);
1677 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1678 PROC_UNLOCK(vw->vw_proc);
1679 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1680 return (TRUE);
1681 }
1682
1683 static int
signal_vt_acq(struct vt_window * vw)1684 signal_vt_acq(struct vt_window *vw)
1685 {
1686
1687 if (vw->vw_smode.mode != VT_PROCESS)
1688 return (FALSE);
1689 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1690 cnavailable(vw->vw_terminal->consdev, FALSE);
1691 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1692 vw->vw_proc = NULL;
1693 vw->vw_pid = 0;
1694 return (TRUE);
1695 }
1696 vw->vw_flags |= VWF_SWWAIT_ACQ;
1697 PROC_LOCK(vw->vw_proc);
1698 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1699 PROC_UNLOCK(vw->vw_proc);
1700 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1701 return (TRUE);
1702 }
1703
1704 static int
finish_vt_rel(struct vt_window * vw,int release,int * s)1705 finish_vt_rel(struct vt_window *vw, int release, int *s)
1706 {
1707
1708 if (vw->vw_flags & VWF_SWWAIT_REL) {
1709 vw->vw_flags &= ~VWF_SWWAIT_REL;
1710 if (release) {
1711 callout_drain(&vw->vw_proc_dead_timer);
1712 vt_late_window_switch(vw->vw_switch_to);
1713 }
1714 return (0);
1715 }
1716 return (EINVAL);
1717 }
1718
1719 static int
finish_vt_acq(struct vt_window * vw)1720 finish_vt_acq(struct vt_window *vw)
1721 {
1722
1723 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1724 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1725 return (0);
1726 }
1727 return (EINVAL);
1728 }
1729
1730 #ifndef SC_NO_CUTPASTE
1731 static void
vt_mouse_terminput_button(struct vt_device * vd,int button)1732 vt_mouse_terminput_button(struct vt_device *vd, int button)
1733 {
1734 struct vt_window *vw;
1735 struct vt_font *vf;
1736 char mouseb[6] = "\x1B[M";
1737 int i, x, y;
1738
1739 vw = vd->vd_curwindow;
1740 vf = vw->vw_font;
1741
1742 /* Translate to char position. */
1743 x = vd->vd_mx / vf->vf_width;
1744 y = vd->vd_my / vf->vf_height;
1745 /* Avoid overflow. */
1746 x = MIN(x, 255 - '!');
1747 y = MIN(y, 255 - '!');
1748
1749 mouseb[3] = ' ' + button;
1750 mouseb[4] = '!' + x;
1751 mouseb[5] = '!' + y;
1752
1753 for (i = 0; i < sizeof(mouseb); i++)
1754 terminal_input_char(vw->vw_terminal, mouseb[i]);
1755 }
1756
1757 static void
vt_mouse_terminput(struct vt_device * vd,int type,int x,int y,int event,int cnt)1758 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1759 int cnt)
1760 {
1761
1762 switch (type) {
1763 case MOUSE_BUTTON_EVENT:
1764 if (cnt > 0) {
1765 /* Mouse button pressed. */
1766 if (event & MOUSE_BUTTON1DOWN)
1767 vt_mouse_terminput_button(vd, 0);
1768 if (event & MOUSE_BUTTON2DOWN)
1769 vt_mouse_terminput_button(vd, 1);
1770 if (event & MOUSE_BUTTON3DOWN)
1771 vt_mouse_terminput_button(vd, 2);
1772 } else {
1773 /* Mouse button released. */
1774 vt_mouse_terminput_button(vd, 3);
1775 }
1776 break;
1777 #ifdef notyet
1778 case MOUSE_MOTION_EVENT:
1779 if (mouse->u.data.z < 0) {
1780 /* Scroll up. */
1781 sc_mouse_input_button(vd, 64);
1782 } else if (mouse->u.data.z > 0) {
1783 /* Scroll down. */
1784 sc_mouse_input_button(vd, 65);
1785 }
1786 break;
1787 #endif
1788 }
1789 }
1790
1791 static void
vt_mouse_paste()1792 vt_mouse_paste()
1793 {
1794 term_char_t *buf;
1795 int i, len;
1796
1797 len = VD_PASTEBUFLEN(main_vd);
1798 buf = VD_PASTEBUF(main_vd);
1799 len /= sizeof(term_char_t);
1800 for (i = 0; i < len; i++) {
1801 if (buf[i] == '\0')
1802 continue;
1803 terminal_input_char(main_vd->vd_curwindow->vw_terminal,
1804 buf[i]);
1805 }
1806 }
1807
1808 void
vt_mouse_event(int type,int x,int y,int event,int cnt,int mlevel)1809 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1810 {
1811 struct vt_device *vd;
1812 struct vt_window *vw;
1813 struct vt_font *vf;
1814 term_pos_t size;
1815 int len, mark;
1816
1817 vd = main_vd;
1818 vw = vd->vd_curwindow;
1819 vf = vw->vw_font;
1820 mark = 0;
1821
1822 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
1823 /*
1824 * Either the mouse is disabled, or the window is in
1825 * "graphics mode". The graphics mode is usually set by
1826 * an X server, using the KDSETMODE ioctl.
1827 */
1828 return;
1829
1830 if (vf == NULL) /* Text mode. */
1831 return;
1832
1833 /*
1834 * TODO: add flag about pointer position changed, to not redraw chars
1835 * under mouse pointer when nothing changed.
1836 */
1837
1838 if (vw->vw_mouse_level > 0)
1839 vt_mouse_terminput(vd, type, x, y, event, cnt);
1840
1841 switch (type) {
1842 case MOUSE_ACTION:
1843 case MOUSE_MOTION_EVENT:
1844 /* Movement */
1845 x += vd->vd_mx;
1846 y += vd->vd_my;
1847
1848 vt_termsize(vd, vf, &size);
1849
1850 /* Apply limits. */
1851 x = MAX(x, 0);
1852 y = MAX(y, 0);
1853 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1854 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1855
1856 vd->vd_mx = x;
1857 vd->vd_my = y;
1858 if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
1859 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1860 vd->vd_mx / vf->vf_width,
1861 vd->vd_my / vf->vf_height);
1862
1863 vt_resume_flush_timer(vw->vw_device, 0);
1864 return; /* Done */
1865 case MOUSE_BUTTON_EVENT:
1866 /* Buttons */
1867 break;
1868 default:
1869 return; /* Done */
1870 }
1871
1872 switch (event) {
1873 case MOUSE_BUTTON1DOWN:
1874 switch (cnt % 4) {
1875 case 0: /* up */
1876 mark = VTB_MARK_END;
1877 break;
1878 case 1: /* single click: start cut operation */
1879 mark = VTB_MARK_START;
1880 break;
1881 case 2: /* double click: cut a word */
1882 mark = VTB_MARK_WORD;
1883 break;
1884 case 3: /* triple click: cut a line */
1885 mark = VTB_MARK_ROW;
1886 break;
1887 }
1888 break;
1889 case VT_MOUSE_PASTEBUTTON:
1890 switch (cnt) {
1891 case 0: /* up */
1892 break;
1893 default:
1894 vt_mouse_paste();
1895 break;
1896 }
1897 return; /* Done */
1898 case VT_MOUSE_EXTENDBUTTON:
1899 switch (cnt) {
1900 case 0: /* up */
1901 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1902 mark = VTB_MARK_EXTEND;
1903 else
1904 mark = 0;
1905 break;
1906 default:
1907 mark = VTB_MARK_EXTEND;
1908 break;
1909 }
1910 break;
1911 default:
1912 return; /* Done */
1913 }
1914
1915 /* Save buttons state. */
1916 if (cnt > 0)
1917 vd->vd_mstate |= event;
1918 else
1919 vd->vd_mstate &= ~event;
1920
1921 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1922 vd->vd_my / vf->vf_height) == 1) {
1923 /*
1924 * We have something marked to copy, so update pointer to
1925 * window with selection.
1926 */
1927 vt_resume_flush_timer(vw->vw_device, 0);
1928
1929 switch (mark) {
1930 case VTB_MARK_END:
1931 case VTB_MARK_WORD:
1932 case VTB_MARK_ROW:
1933 case VTB_MARK_EXTEND:
1934 break;
1935 default:
1936 /* Other types of mark do not require to copy data. */
1937 return;
1938 }
1939
1940 /* Get current selection size in bytes. */
1941 len = vtbuf_get_marked_len(&vw->vw_buf);
1942 if (len <= 0)
1943 return;
1944
1945 /* Reallocate buffer only if old one is too small. */
1946 if (len > VD_PASTEBUFSZ(vd)) {
1947 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
1948 M_WAITOK | M_ZERO);
1949 /* Update buffer size. */
1950 VD_PASTEBUFSZ(vd) = len;
1951 }
1952 /* Request copy/paste buffer data, no more than `len' */
1953 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
1954 VD_PASTEBUFSZ(vd));
1955
1956 VD_PASTEBUFLEN(vd) = len;
1957
1958 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
1959 }
1960 }
1961
1962 void
vt_mouse_state(int show)1963 vt_mouse_state(int show)
1964 {
1965 struct vt_device *vd;
1966 struct vt_window *vw;
1967
1968 vd = main_vd;
1969 vw = vd->vd_curwindow;
1970
1971 switch (show) {
1972 case VT_MOUSE_HIDE:
1973 vw->vw_flags |= VWF_MOUSE_HIDE;
1974 break;
1975 case VT_MOUSE_SHOW:
1976 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1977 break;
1978 }
1979
1980 /* Mark mouse position as dirty. */
1981 vt_mark_mouse_position_as_dirty(vd);
1982 vt_resume_flush_timer(vw->vw_device, 0);
1983 }
1984 #endif
1985
1986 static int
vtterm_mmap(struct terminal * tm,vm_ooffset_t offset,vm_paddr_t * paddr,int nprot,vm_memattr_t * memattr)1987 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1988 int nprot, vm_memattr_t *memattr)
1989 {
1990 struct vt_window *vw = tm->tm_softc;
1991 struct vt_device *vd = vw->vw_device;
1992
1993 if (vd->vd_driver->vd_fb_mmap)
1994 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1995 memattr));
1996
1997 return (ENXIO);
1998 }
1999
2000 static int
vtterm_ioctl(struct terminal * tm,u_long cmd,caddr_t data,struct thread * td)2001 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
2002 struct thread *td)
2003 {
2004 struct vt_window *vw = tm->tm_softc;
2005 struct vt_device *vd = vw->vw_device;
2006 keyboard_t *kbd;
2007 int error, i, s;
2008 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
2009 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
2010 int ival;
2011
2012 switch (cmd) {
2013 case _IO('v', 4):
2014 cmd = VT_RELDISP;
2015 break;
2016 case _IO('v', 5):
2017 cmd = VT_ACTIVATE;
2018 break;
2019 case _IO('v', 6):
2020 cmd = VT_WAITACTIVE;
2021 break;
2022 case _IO('K', 20):
2023 cmd = KDSKBSTATE;
2024 break;
2025 case _IO('K', 67):
2026 cmd = KDSETRAD;
2027 break;
2028 case _IO('K', 7):
2029 cmd = KDSKBMODE;
2030 break;
2031 case _IO('K', 8):
2032 cmd = KDMKTONE;
2033 break;
2034 case _IO('K', 63):
2035 cmd = KIOCSOUND;
2036 break;
2037 case _IO('K', 66):
2038 cmd = KDSETLED;
2039 break;
2040 case _IO('c', 110):
2041 cmd = CONS_SETKBD;
2042 break;
2043 default:
2044 goto skip_thunk;
2045 }
2046 ival = IOCPARM_IVAL(data);
2047 data = (caddr_t)&ival;
2048 skip_thunk:
2049 #endif
2050
2051 switch (cmd) {
2052 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
2053 if (*(int *)data & ~0x7f)
2054 return (EINVAL);
2055 /* FALLTHROUGH */
2056 case GIO_KEYMAP:
2057 case PIO_KEYMAP:
2058 case GIO_DEADKEYMAP:
2059 case PIO_DEADKEYMAP:
2060 case GETFKEY:
2061 case SETFKEY:
2062 case KDGKBINFO:
2063 case KDGKBTYPE:
2064 case KDGETREPEAT: /* get keyboard repeat & delay rates */
2065 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
2066 case KBADDKBD: /* add/remove keyboard to/from mux */
2067 case KBRELKBD: {
2068 error = 0;
2069
2070 mtx_lock(&Giant);
2071 kbd = kbd_get_keyboard(vd->vd_keyboard);
2072 if (kbd != NULL)
2073 error = kbdd_ioctl(kbd, cmd, data);
2074 mtx_unlock(&Giant);
2075 if (error == ENOIOCTL) {
2076 if (cmd == KDGKBTYPE) {
2077 /* always return something? XXX */
2078 *(int *)data = 0;
2079 } else {
2080 return (ENODEV);
2081 }
2082 }
2083 return (error);
2084 }
2085 case KDGKBSTATE: { /* get keyboard state (locks) */
2086 error = 0;
2087
2088 if (vw == vd->vd_curwindow) {
2089 mtx_lock(&Giant);
2090 kbd = kbd_get_keyboard(vd->vd_keyboard);
2091 if (kbd != NULL)
2092 error = vt_save_kbd_state(vw, kbd);
2093 mtx_unlock(&Giant);
2094
2095 if (error != 0)
2096 return (error);
2097 }
2098
2099 *(int *)data = vw->vw_kbdstate & LOCK_MASK;
2100
2101 return (error);
2102 }
2103 case KDSKBSTATE: { /* set keyboard state (locks) */
2104 int state;
2105
2106 state = *(int *)data;
2107 if (state & ~LOCK_MASK)
2108 return (EINVAL);
2109
2110 vw->vw_kbdstate &= ~LOCK_MASK;
2111 vw->vw_kbdstate |= state;
2112
2113 error = 0;
2114 if (vw == vd->vd_curwindow) {
2115 mtx_lock(&Giant);
2116 kbd = kbd_get_keyboard(vd->vd_keyboard);
2117 if (kbd != NULL)
2118 error = vt_update_kbd_state(vw, kbd);
2119 mtx_unlock(&Giant);
2120 }
2121
2122 return (error);
2123 }
2124 case KDGETLED: { /* get keyboard LED status */
2125 error = 0;
2126
2127 if (vw == vd->vd_curwindow) {
2128 mtx_lock(&Giant);
2129 kbd = kbd_get_keyboard(vd->vd_keyboard);
2130 if (kbd != NULL)
2131 error = vt_save_kbd_leds(vw, kbd);
2132 mtx_unlock(&Giant);
2133
2134 if (error != 0)
2135 return (error);
2136 }
2137
2138 *(int *)data = vw->vw_kbdstate & LED_MASK;
2139
2140 return (error);
2141 }
2142 case KDSETLED: { /* set keyboard LED status */
2143 int leds;
2144
2145 leds = *(int *)data;
2146 if (leds & ~LED_MASK)
2147 return (EINVAL);
2148
2149 vw->vw_kbdstate &= ~LED_MASK;
2150 vw->vw_kbdstate |= leds;
2151
2152 error = 0;
2153 if (vw == vd->vd_curwindow) {
2154 mtx_lock(&Giant);
2155 kbd = kbd_get_keyboard(vd->vd_keyboard);
2156 if (kbd != NULL)
2157 error = vt_update_kbd_leds(vw, kbd);
2158 mtx_unlock(&Giant);
2159 }
2160
2161 return (error);
2162 }
2163 case KDGKBMODE: {
2164 error = 0;
2165
2166 if (vw == vd->vd_curwindow) {
2167 mtx_lock(&Giant);
2168 kbd = kbd_get_keyboard(vd->vd_keyboard);
2169 if (kbd != NULL)
2170 error = vt_save_kbd_mode(vw, kbd);
2171 mtx_unlock(&Giant);
2172
2173 if (error != 0)
2174 return (error);
2175 }
2176
2177 *(int *)data = vw->vw_kbdmode;
2178
2179 return (error);
2180 }
2181 case KDSKBMODE: {
2182 int mode;
2183
2184 mode = *(int *)data;
2185 switch (mode) {
2186 case K_XLATE:
2187 case K_RAW:
2188 case K_CODE:
2189 vw->vw_kbdmode = mode;
2190
2191 error = 0;
2192 if (vw == vd->vd_curwindow) {
2193 mtx_lock(&Giant);
2194 kbd = kbd_get_keyboard(vd->vd_keyboard);
2195 if (kbd != NULL)
2196 error = vt_update_kbd_mode(vw, kbd);
2197 mtx_unlock(&Giant);
2198 }
2199
2200 return (error);
2201 default:
2202 return (EINVAL);
2203 }
2204 }
2205 case FBIOGTYPE:
2206 case FBIO_GETWINORG: /* get frame buffer window origin */
2207 case FBIO_GETDISPSTART: /* get display start address */
2208 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
2209 case FBIO_BLANK: /* blank display */
2210 if (vd->vd_driver->vd_fb_ioctl)
2211 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
2212 break;
2213 case CONS_BLANKTIME:
2214 /* XXX */
2215 return (0);
2216 case CONS_GET:
2217 /* XXX */
2218 *(int *)data = M_CG640x480;
2219 return (0);
2220 case CONS_BELLTYPE: /* set bell type sound */
2221 if ((*(int *)data) & CONS_QUIET_BELL)
2222 vd->vd_flags |= VDF_QUIET_BELL;
2223 else
2224 vd->vd_flags &= ~VDF_QUIET_BELL;
2225 return (0);
2226 case CONS_GETINFO: {
2227 vid_info_t *vi = (vid_info_t *)data;
2228 if (vi->size != sizeof(struct vid_info))
2229 return (EINVAL);
2230
2231 if (vw == vd->vd_curwindow) {
2232 kbd = kbd_get_keyboard(vd->vd_keyboard);
2233 if (kbd != NULL)
2234 vt_save_kbd_state(vw, kbd);
2235 }
2236
2237 vi->m_num = vd->vd_curwindow->vw_number + 1;
2238 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK;
2239 /* XXX: other fields! */
2240 return (0);
2241 }
2242 case CONS_GETVERS:
2243 *(int *)data = 0x200;
2244 return (0);
2245 case CONS_MODEINFO:
2246 /* XXX */
2247 return (0);
2248 case CONS_MOUSECTL: {
2249 mouse_info_t *mouse = (mouse_info_t*)data;
2250
2251 /*
2252 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
2253 * should not be applied to individual TTYs, but only to
2254 * consolectl.
2255 */
2256 switch (mouse->operation) {
2257 case MOUSE_HIDE:
2258 if (vd->vd_flags & VDF_MOUSECURSOR) {
2259 vd->vd_flags &= ~VDF_MOUSECURSOR;
2260 #ifndef SC_NO_CUTPASTE
2261 vt_mouse_state(VT_MOUSE_HIDE);
2262 #endif
2263 }
2264 return (0);
2265 case MOUSE_SHOW:
2266 if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2267 vd->vd_flags |= VDF_MOUSECURSOR;
2268 vd->vd_mx = vd->vd_width / 2;
2269 vd->vd_my = vd->vd_height / 2;
2270 #ifndef SC_NO_CUTPASTE
2271 vt_mouse_state(VT_MOUSE_SHOW);
2272 #endif
2273 }
2274 return (0);
2275 default:
2276 return (EINVAL);
2277 }
2278 }
2279 case PIO_VFONT: {
2280 struct vt_font *vf;
2281
2282 if (vd->vd_flags & VDF_TEXTMODE)
2283 return (ENOTSUP);
2284
2285 error = vtfont_load((void *)data, &vf);
2286 if (error != 0)
2287 return (error);
2288
2289 error = vt_change_font(vw, vf);
2290 vtfont_unref(vf);
2291 return (error);
2292 }
2293 case PIO_VFONT_DEFAULT: {
2294 /* Reset to default font. */
2295 error = vt_change_font(vw, &vt_font_default);
2296 return (error);
2297 }
2298 case GIO_SCRNMAP: {
2299 scrmap_t *sm = (scrmap_t *)data;
2300
2301 /* We don't have screen maps, so return a handcrafted one. */
2302 for (i = 0; i < 256; i++)
2303 sm->scrmap[i] = i;
2304 return (0);
2305 }
2306 case KDSETMODE:
2307 /*
2308 * FIXME: This implementation is incomplete compared to
2309 * syscons.
2310 */
2311 switch (*(int *)data) {
2312 case KD_TEXT:
2313 case KD_TEXT1:
2314 case KD_PIXEL:
2315 vw->vw_flags &= ~VWF_GRAPHICS;
2316 break;
2317 case KD_GRAPHICS:
2318 vw->vw_flags |= VWF_GRAPHICS;
2319 break;
2320 }
2321 return (0);
2322 case KDENABIO: /* allow io operations */
2323 error = priv_check(td, PRIV_IO);
2324 if (error != 0)
2325 return (error);
2326 error = securelevel_gt(td->td_ucred, 0);
2327 if (error != 0)
2328 return (error);
2329 #if defined(__i386__)
2330 td->td_frame->tf_eflags |= PSL_IOPL;
2331 #elif defined(__amd64__)
2332 td->td_frame->tf_rflags |= PSL_IOPL;
2333 #endif
2334 return (0);
2335 case KDDISABIO: /* disallow io operations (default) */
2336 #if defined(__i386__)
2337 td->td_frame->tf_eflags &= ~PSL_IOPL;
2338 #elif defined(__amd64__)
2339 td->td_frame->tf_rflags &= ~PSL_IOPL;
2340 #endif
2341 return (0);
2342 case KDMKTONE: /* sound the bell */
2343 vtterm_beep(tm, *(u_int *)data);
2344 return (0);
2345 case KIOCSOUND: /* make tone (*data) hz */
2346 /* TODO */
2347 return (0);
2348 case CONS_SETKBD: /* set the new keyboard */
2349 mtx_lock(&Giant);
2350 error = 0;
2351 if (vd->vd_keyboard != *(int *)data) {
2352 kbd = kbd_get_keyboard(*(int *)data);
2353 if (kbd == NULL) {
2354 mtx_unlock(&Giant);
2355 return (EINVAL);
2356 }
2357 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2358 (void *)vd, vt_kbdevent, vd);
2359 if (i >= 0) {
2360 if (vd->vd_keyboard != -1) {
2361 vt_save_kbd_state(vd->vd_curwindow, kbd);
2362 kbd_release(kbd, (void *)vd);
2363 }
2364 kbd = kbd_get_keyboard(i);
2365 vd->vd_keyboard = i;
2366
2367 vt_update_kbd_mode(vd->vd_curwindow, kbd);
2368 vt_update_kbd_state(vd->vd_curwindow, kbd);
2369 } else {
2370 error = EPERM; /* XXX */
2371 }
2372 }
2373 mtx_unlock(&Giant);
2374 return (error);
2375 case CONS_RELKBD: /* release the current keyboard */
2376 mtx_lock(&Giant);
2377 error = 0;
2378 if (vd->vd_keyboard != -1) {
2379 kbd = kbd_get_keyboard(vd->vd_keyboard);
2380 if (kbd == NULL) {
2381 mtx_unlock(&Giant);
2382 return (EINVAL);
2383 }
2384 vt_save_kbd_state(vd->vd_curwindow, kbd);
2385 error = kbd_release(kbd, (void *)vd);
2386 if (error == 0) {
2387 vd->vd_keyboard = -1;
2388 }
2389 }
2390 mtx_unlock(&Giant);
2391 return (error);
2392 case VT_ACTIVATE: {
2393 int win;
2394 win = *(int *)data - 1;
2395 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2396 VT_UNIT(vw), win);
2397 if ((win >= VT_MAXWINDOWS) || (win < 0))
2398 return (EINVAL);
2399 return (vt_proc_window_switch(vd->vd_windows[win]));
2400 }
2401 case VT_GETACTIVE:
2402 *(int *)data = vd->vd_curwindow->vw_number + 1;
2403 return (0);
2404 case VT_GETINDEX:
2405 *(int *)data = vw->vw_number + 1;
2406 return (0);
2407 case VT_LOCKSWITCH:
2408 /* TODO: Check current state, switching can be in progress. */
2409 if ((*(int *)data) == 0x01)
2410 vw->vw_flags |= VWF_VTYLOCK;
2411 else if ((*(int *)data) == 0x02)
2412 vw->vw_flags &= ~VWF_VTYLOCK;
2413 else
2414 return (EINVAL);
2415 return (0);
2416 case VT_OPENQRY:
2417 VT_LOCK(vd);
2418 for (i = 0; i < VT_MAXWINDOWS; i++) {
2419 vw = vd->vd_windows[i];
2420 if (vw == NULL)
2421 continue;
2422 if (!(vw->vw_flags & VWF_OPENED)) {
2423 *(int *)data = vw->vw_number + 1;
2424 VT_UNLOCK(vd);
2425 return (0);
2426 }
2427 }
2428 VT_UNLOCK(vd);
2429 return (EINVAL);
2430 case VT_WAITACTIVE: {
2431 unsigned int idx;
2432
2433 error = 0;
2434
2435 idx = *(unsigned int *)data;
2436 if (idx > VT_MAXWINDOWS)
2437 return (EINVAL);
2438 if (idx > 0)
2439 vw = vd->vd_windows[idx - 1];
2440
2441 VT_LOCK(vd);
2442 while (vd->vd_curwindow != vw && error == 0)
2443 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2444 VT_UNLOCK(vd);
2445 return (error);
2446 }
2447 case VT_SETMODE: { /* set screen switcher mode */
2448 struct vt_mode *mode;
2449 struct proc *p1;
2450
2451 mode = (struct vt_mode *)data;
2452 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
2453 if (vw->vw_smode.mode == VT_PROCESS) {
2454 p1 = pfind(vw->vw_pid);
2455 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
2456 if (p1)
2457 PROC_UNLOCK(p1);
2458 DPRINTF(5, "error EPERM\n");
2459 return (EPERM);
2460 }
2461 if (p1)
2462 PROC_UNLOCK(p1);
2463 }
2464 if (mode->mode == VT_AUTO) {
2465 vw->vw_smode.mode = VT_AUTO;
2466 vw->vw_proc = NULL;
2467 vw->vw_pid = 0;
2468 DPRINTF(5, "VT_AUTO, ");
2469 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2470 cnavailable(vw->vw_terminal->consdev, TRUE);
2471 /* were we in the middle of the vty switching process? */
2472 if (finish_vt_rel(vw, TRUE, &s) == 0)
2473 DPRINTF(5, "reset WAIT_REL, ");
2474 if (finish_vt_acq(vw) == 0)
2475 DPRINTF(5, "reset WAIT_ACQ, ");
2476 return (0);
2477 } else if (mode->mode == VT_PROCESS) {
2478 if (!ISSIGVALID(mode->relsig) ||
2479 !ISSIGVALID(mode->acqsig) ||
2480 !ISSIGVALID(mode->frsig)) {
2481 DPRINTF(5, "error EINVAL\n");
2482 return (EINVAL);
2483 }
2484 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2485 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2486 vw->vw_proc = td->td_proc;
2487 vw->vw_pid = vw->vw_proc->p_pid;
2488 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2489 cnavailable(vw->vw_terminal->consdev, FALSE);
2490 } else {
2491 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2492 mode->mode);
2493 return (EINVAL);
2494 }
2495 DPRINTF(5, "\n");
2496 return (0);
2497 }
2498 case VT_GETMODE: /* get screen switcher mode */
2499 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2500 return (0);
2501
2502 case VT_RELDISP: /* screen switcher ioctl */
2503 /*
2504 * This must be the current vty which is in the VT_PROCESS
2505 * switching mode...
2506 */
2507 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2508 VT_PROCESS)) {
2509 return (EINVAL);
2510 }
2511 /* ...and this process is controlling it. */
2512 if (vw->vw_proc != td->td_proc) {
2513 return (EPERM);
2514 }
2515 error = EINVAL;
2516 switch(*(int *)data) {
2517 case VT_FALSE: /* user refuses to release screen, abort */
2518 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2519 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2520 SC_DRIVER_NAME, VT_UNIT(vw));
2521 break;
2522 case VT_TRUE: /* user has released screen, go on */
2523 /* finish_vt_rel(..., TRUE, ...) should not be locked */
2524 if (vw->vw_flags & VWF_SWWAIT_REL) {
2525 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2526 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2527 SC_DRIVER_NAME, VT_UNIT(vw));
2528 } else {
2529 error = EINVAL;
2530 }
2531 return (error);
2532 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2533 if ((error = finish_vt_acq(vw)) == 0)
2534 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2535 SC_DRIVER_NAME, VT_UNIT(vw));
2536 break;
2537 default:
2538 break;
2539 }
2540 return (error);
2541 }
2542
2543 return (ENOIOCTL);
2544 }
2545
2546 static struct vt_window *
vt_allocate_window(struct vt_device * vd,unsigned int window)2547 vt_allocate_window(struct vt_device *vd, unsigned int window)
2548 {
2549 struct vt_window *vw;
2550 struct terminal *tm;
2551 term_pos_t size;
2552 struct winsize wsz;
2553
2554 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2555 vw->vw_device = vd;
2556 vw->vw_number = window;
2557 vw->vw_kbdmode = K_XLATE;
2558
2559 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
2560 vw->vw_font = vtfont_ref(&vt_font_default);
2561 vt_compute_drawable_area(vw);
2562 }
2563
2564 vt_termsize(vd, vw->vw_font, &size);
2565 vt_winsize(vd, vw->vw_font, &wsz);
2566 vtbuf_init(&vw->vw_buf, &size);
2567
2568 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2569 terminal_set_winsize(tm, &wsz);
2570 vd->vd_windows[window] = vw;
2571 callout_init(&vw->vw_proc_dead_timer, 0);
2572
2573 return (vw);
2574 }
2575
2576 void
vt_upgrade(struct vt_device * vd)2577 vt_upgrade(struct vt_device *vd)
2578 {
2579 struct vt_window *vw;
2580 unsigned int i;
2581 int register_handlers;
2582
2583 if (!vty_enabled(VTY_VT))
2584 return;
2585 if (main_vd->vd_driver == NULL)
2586 return;
2587
2588 for (i = 0; i < VT_MAXWINDOWS; i++) {
2589 vw = vd->vd_windows[i];
2590 if (vw == NULL) {
2591 /* New window. */
2592 vw = vt_allocate_window(vd, i);
2593 }
2594 if (!(vw->vw_flags & VWF_READY)) {
2595 callout_init(&vw->vw_proc_dead_timer, 0);
2596 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2597 vw->vw_flags |= VWF_READY;
2598 if (vw->vw_flags & VWF_CONSOLE) {
2599 /* For existing console window. */
2600 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2601 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2602 }
2603 }
2604
2605 }
2606 VT_LOCK(vd);
2607 if (vd->vd_curwindow == NULL)
2608 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2609
2610 register_handlers = 0;
2611 if (!(vd->vd_flags & VDF_ASYNC)) {
2612 /* Attach keyboard. */
2613 vt_allocate_keyboard(vd);
2614
2615 /* Init 25 Hz timer. */
2616 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2617
2618 /* Start timer when everything ready. */
2619 vd->vd_flags |= VDF_ASYNC;
2620 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2621 vd->vd_timer_armed = 1;
2622 register_handlers = 1;
2623 }
2624
2625 VT_UNLOCK(vd);
2626
2627 /* Refill settings with new sizes. */
2628 vt_resize(vd);
2629
2630 if (register_handlers) {
2631 /* Register suspend/resume handlers. */
2632 EVENTHANDLER_REGISTER(power_suspend_early, vt_suspend_handler,
2633 vd, EVENTHANDLER_PRI_ANY);
2634 EVENTHANDLER_REGISTER(power_resume, vt_resume_handler, vd,
2635 EVENTHANDLER_PRI_ANY);
2636 }
2637 }
2638
2639 static void
vt_resize(struct vt_device * vd)2640 vt_resize(struct vt_device *vd)
2641 {
2642 struct vt_window *vw;
2643 int i;
2644
2645 for (i = 0; i < VT_MAXWINDOWS; i++) {
2646 vw = vd->vd_windows[i];
2647 VT_LOCK(vd);
2648 /* Assign default font to window, if not textmode. */
2649 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2650 vw->vw_font = vtfont_ref(&vt_font_default);
2651 VT_UNLOCK(vd);
2652
2653 /* Resize terminal windows */
2654 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2655 DPRINTF(100, "%s: vt_change_font() is busy, "
2656 "window %d\n", __func__, i);
2657 }
2658 }
2659 }
2660
2661 static void
vt_replace_backend(const struct vt_driver * drv,void * softc)2662 vt_replace_backend(const struct vt_driver *drv, void *softc)
2663 {
2664 struct vt_device *vd;
2665
2666 vd = main_vd;
2667
2668 if (vd->vd_flags & VDF_ASYNC) {
2669 /* Stop vt_flush periodic task. */
2670 VT_LOCK(vd);
2671 vt_suspend_flush_timer(vd);
2672 VT_UNLOCK(vd);
2673 /*
2674 * Mute current terminal until we done. vt_change_font (called
2675 * from vt_resize) will unmute it.
2676 */
2677 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2678 }
2679
2680 /*
2681 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2682 * set it.
2683 */
2684 VT_LOCK(vd);
2685 vd->vd_flags &= ~VDF_TEXTMODE;
2686
2687 if (drv != NULL) {
2688 /*
2689 * We want to upgrade from the current driver to the
2690 * given driver.
2691 */
2692
2693 vd->vd_prev_driver = vd->vd_driver;
2694 vd->vd_prev_softc = vd->vd_softc;
2695 vd->vd_driver = drv;
2696 vd->vd_softc = softc;
2697
2698 vd->vd_driver->vd_init(vd);
2699 } else if (vd->vd_prev_driver != NULL && vd->vd_prev_softc != NULL) {
2700 /*
2701 * No driver given: we want to downgrade to the previous
2702 * driver.
2703 */
2704 const struct vt_driver *old_drv;
2705 void *old_softc;
2706
2707 old_drv = vd->vd_driver;
2708 old_softc = vd->vd_softc;
2709
2710 vd->vd_driver = vd->vd_prev_driver;
2711 vd->vd_softc = vd->vd_prev_softc;
2712 vd->vd_prev_driver = NULL;
2713 vd->vd_prev_softc = NULL;
2714
2715 vd->vd_flags |= VDF_DOWNGRADE;
2716
2717 vd->vd_driver->vd_init(vd);
2718
2719 if (old_drv->vd_fini)
2720 old_drv->vd_fini(vd, old_softc);
2721
2722 vd->vd_flags &= ~VDF_DOWNGRADE;
2723 }
2724
2725 VT_UNLOCK(vd);
2726
2727 /* Update windows sizes and initialize last items. */
2728 vt_upgrade(vd);
2729
2730 #ifdef DEV_SPLASH
2731 if (vd->vd_flags & VDF_SPLASH)
2732 vtterm_splash(vd);
2733 #endif
2734
2735 if (vd->vd_flags & VDF_ASYNC) {
2736 /* Allow to put chars now. */
2737 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2738 /* Rerun timer for screen updates. */
2739 vt_resume_flush_timer(vd, 0);
2740 }
2741
2742 /*
2743 * Register as console. If it already registered, cnadd() will ignore
2744 * it.
2745 */
2746 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2747 }
2748
2749 static void
vt_suspend_handler(void * priv)2750 vt_suspend_handler(void *priv)
2751 {
2752 struct vt_device *vd;
2753
2754 vd = priv;
2755 if (vd->vd_driver != NULL && vd->vd_driver->vd_suspend != NULL)
2756 vd->vd_driver->vd_suspend(vd);
2757 }
2758
2759 static void
vt_resume_handler(void * priv)2760 vt_resume_handler(void *priv)
2761 {
2762 struct vt_device *vd;
2763
2764 vd = priv;
2765 if (vd->vd_driver != NULL && vd->vd_driver->vd_resume != NULL)
2766 vd->vd_driver->vd_resume(vd);
2767 }
2768
2769 void
vt_allocate(const struct vt_driver * drv,void * softc)2770 vt_allocate(const struct vt_driver *drv, void *softc)
2771 {
2772
2773 if (!vty_enabled(VTY_VT))
2774 return;
2775
2776 if (main_vd->vd_driver == NULL) {
2777 main_vd->vd_driver = drv;
2778 printf("VT: initialize with new VT driver \"%s\".\n",
2779 drv->vd_name);
2780 } else {
2781 /*
2782 * Check if have rights to replace current driver. For example:
2783 * it is bad idea to replace KMS driver with generic VGA one.
2784 */
2785 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2786 printf("VT: Driver priority %d too low. Current %d\n ",
2787 drv->vd_priority, main_vd->vd_driver->vd_priority);
2788 return;
2789 }
2790 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2791 main_vd->vd_driver->vd_name, drv->vd_name);
2792 }
2793
2794 vt_replace_backend(drv, softc);
2795 }
2796
2797 void
vt_deallocate(const struct vt_driver * drv,void * softc)2798 vt_deallocate(const struct vt_driver *drv, void *softc)
2799 {
2800
2801 if (!vty_enabled(VTY_VT))
2802 return;
2803
2804 if (main_vd->vd_prev_driver == NULL ||
2805 main_vd->vd_driver != drv ||
2806 main_vd->vd_softc != softc)
2807 return;
2808
2809 printf("VT: Switching back from \"%s\" to \"%s\".\n",
2810 main_vd->vd_driver->vd_name, main_vd->vd_prev_driver->vd_name);
2811
2812 vt_replace_backend(NULL, NULL);
2813 }
2814
2815 void
vt_suspend(struct vt_device * vd)2816 vt_suspend(struct vt_device *vd)
2817 {
2818 int error;
2819
2820 if (vt_suspendswitch == 0)
2821 return;
2822 /* Save current window. */
2823 vd->vd_savedwindow = vd->vd_curwindow;
2824 /* Ask holding process to free window and switch to console window */
2825 vt_proc_window_switch(vd->vd_windows[VT_CONSWINDOW]);
2826
2827 /* Wait for the window switch to complete. */
2828 error = 0;
2829 VT_LOCK(vd);
2830 while (vd->vd_curwindow != vd->vd_windows[VT_CONSWINDOW] && error == 0)
2831 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2832 VT_UNLOCK(vd);
2833 }
2834
2835 void
vt_resume(struct vt_device * vd)2836 vt_resume(struct vt_device *vd)
2837 {
2838
2839 if (vt_suspendswitch == 0)
2840 return;
2841 /* Switch back to saved window, if any */
2842 vt_proc_window_switch(vd->vd_savedwindow);
2843 vd->vd_savedwindow = NULL;
2844 }
2845