xref: /freebsd/usr.sbin/bhyve/console.c (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2015 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 
31 #include "bhyvegc.h"
32 #include "console.h"
33 
34 static struct {
35 	struct bhyvegc		*gc;
36 
37 	fb_render_func_t	fb_render_cb;
38 	void			*fb_arg;
39 
40 	kbd_event_func_t	kbd_event_cb;
41 	void			*kbd_arg;
42 	int			kbd_priority;
43 
44 	ptr_event_func_t	ptr_event_cb;
45 	void			*ptr_arg;
46 	int			ptr_priority;
47 } console;
48 
49 void
50 console_init(int w, int h, void *fbaddr)
51 {
52 	console.gc = bhyvegc_init(w, h, fbaddr);
53 }
54 
55 void
56 console_set_fbaddr(void *fbaddr)
57 {
58 	bhyvegc_set_fbaddr(console.gc, fbaddr);
59 }
60 
61 struct bhyvegc_image *
62 console_get_image(void)
63 {
64 	struct bhyvegc_image *bhyvegc_image;
65 
66 	bhyvegc_image = bhyvegc_get_image(console.gc);
67 
68 	return (bhyvegc_image);
69 }
70 
71 void
72 console_fb_register(fb_render_func_t render_cb, void *arg)
73 {
74 	console.fb_render_cb = render_cb;
75 	console.fb_arg = arg;
76 }
77 
78 void
79 console_refresh(void)
80 {
81 	if (console.fb_render_cb)
82 		(*console.fb_render_cb)(console.gc, console.fb_arg);
83 }
84 
85 void
86 console_kbd_register(kbd_event_func_t event_cb, void *arg, int pri)
87 {
88 	if (pri > console.kbd_priority) {
89 		console.kbd_event_cb = event_cb;
90 		console.kbd_arg = arg;
91 		console.kbd_priority = pri;
92 	}
93 }
94 
95 void
96 console_ptr_register(ptr_event_func_t event_cb, void *arg, int pri)
97 {
98 	if (pri > console.ptr_priority) {
99 		console.ptr_event_cb = event_cb;
100 		console.ptr_arg = arg;
101 		console.ptr_priority = pri;
102 	}
103 }
104 
105 void
106 console_key_event(int down, uint32_t keysym, uint32_t keycode)
107 {
108 	if (console.kbd_event_cb)
109 		(*console.kbd_event_cb)(down, keysym, keycode, console.kbd_arg);
110 }
111 
112 void
113 console_ptr_event(uint8_t button, int x, int y)
114 {
115 	if (console.ptr_event_cb)
116 		(*console.ptr_event_cb)(button, x, y, console.ptr_arg);
117 }
118