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