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/cdefs.h>
30
31 #include <sys/types.h>
32
33 #include "bhyvegc.h"
34 #include "console.h"
35
36 static struct {
37 struct bhyvegc *gc;
38
39 fb_render_func_t fb_render_cb;
40 void *fb_arg;
41
42 kbd_event_func_t kbd_event_cb;
43 void *kbd_arg;
44 int kbd_priority;
45
46 ptr_event_func_t ptr_event_cb;
47 void *ptr_arg;
48 int ptr_priority;
49 } console;
50
51 void
console_init(int w,int h,void * fbaddr)52 console_init(int w, int h, void *fbaddr)
53 {
54 console.gc = bhyvegc_init(w, h, fbaddr);
55 }
56
57 void
console_set_fbaddr(void * fbaddr)58 console_set_fbaddr(void *fbaddr)
59 {
60 bhyvegc_set_fbaddr(console.gc, fbaddr);
61 }
62
63 struct bhyvegc_image *
console_get_image(void)64 console_get_image(void)
65 {
66 struct bhyvegc_image *bhyvegc_image;
67
68 bhyvegc_image = bhyvegc_get_image(console.gc);
69
70 return (bhyvegc_image);
71 }
72
73 void
console_fb_register(fb_render_func_t render_cb,void * arg)74 console_fb_register(fb_render_func_t render_cb, void *arg)
75 {
76 console.fb_render_cb = render_cb;
77 console.fb_arg = arg;
78 }
79
80 void
console_refresh(void)81 console_refresh(void)
82 {
83 if (console.fb_render_cb)
84 (*console.fb_render_cb)(console.gc, console.fb_arg);
85 }
86
87 void
console_kbd_register(kbd_event_func_t event_cb,void * arg,int pri)88 console_kbd_register(kbd_event_func_t event_cb, void *arg, int pri)
89 {
90 if (pri > console.kbd_priority) {
91 console.kbd_event_cb = event_cb;
92 console.kbd_arg = arg;
93 console.kbd_priority = pri;
94 }
95 }
96
97 void
console_ptr_register(ptr_event_func_t event_cb,void * arg,int pri)98 console_ptr_register(ptr_event_func_t event_cb, void *arg, int pri)
99 {
100 if (pri > console.ptr_priority) {
101 console.ptr_event_cb = event_cb;
102 console.ptr_arg = arg;
103 console.ptr_priority = pri;
104 }
105 }
106
107 void
console_key_event(int down,uint32_t keysym,uint32_t keycode)108 console_key_event(int down, uint32_t keysym, uint32_t keycode)
109 {
110 if (console.kbd_event_cb)
111 (*console.kbd_event_cb)(down, keysym, keycode, console.kbd_arg);
112 }
113
114 void
console_ptr_event(uint8_t button,int x,int y)115 console_ptr_event(uint8_t button, int x, int y)
116 {
117 if (console.ptr_event_cb)
118 (*console.ptr_event_cb)(button, x, y, console.ptr_arg);
119 }
120