1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
/*
* Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Hyra nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/errno.h>
#include <sys/cdefs.h>
#include <sys/param.h>
#include <kfg/window.h>
#include <kfg/font.h>
#include <stddef.h>
#include <string.h>
__always_inline static inline size_t
pixel_index(struct kfg_window *wp, kfgpos_t x, kfgpos_t y)
{
return x + y * (wp->fb_pitch / 4);
}
static int
kfg_win_putc(struct kfg_window *wp, uint32_t x, uint32_t y, char ch)
{
size_t idx;
const uint8_t *glyph;
uint32_t fg, bg;
glyph = &g_KFG_FONT[(int)ch*16];
fg = KFG_WHITE;
bg = wp->bg;
for (uint32_t cy = 0; cy < FONT_HEIGHT; ++cy) {
idx = pixel_index(wp, x + (FONT_WIDTH - 1), y + cy);
for (uint32_t cx = 0; cx < FONT_WIDTH; ++cx) {
wp->framebuf[idx--] = ISSET(glyph[cy], BIT(cx)) ? fg : bg;
}
}
}
static void
draw_win(struct kfg_window *parent, struct kfg_window *wp)
{
kfgpixel_t *framep;
kfgpos_t x_i, y_i; /* Start */
kfgpos_t x_end, y_end; /* End */
kfgpixel_t brush = wp->bg;
kfgpos_t rx, ry; /* Starts at 0 */
kfgpos_t rx_end, ry_end; /* Starts at 0 */
size_t i;
framep = parent->framebuf;
x_i = wp->x;
y_i = wp->y;
x_end = x_i + wp->width;
y_end = y_i + wp->height;
for (kfgpos_t x = x_i; x < x_end; ++x) {
for (kfgpos_t y = y_i; y < y_i+KFG_TITLE_HEIGHT; ++y) {
rx = (x - x_i);
ry = (y - y_i);
if (rx <= KFG_BORDER_WIDTH && (rx % 2) == 0)
brush = KFG_WHITE;
else
brush = KFG_AQUA;
i = pixel_index(parent, x, y);
framep[i] = brush;
}
}
y_i = wp->y + KFG_TITLE_HEIGHT;
for (kfgpos_t x = x_i; x < x_end; ++x) {
for (kfgpos_t y = y_i; y < y_end; ++y) {
rx = (x - x_i);
ry = (y - y_i);
if (rx <= KFG_BORDER_WIDTH)
brush = wp->border_bg;
else if (ry <= KFG_BORDER_HEIGHT)
brush = wp->border_bg;
else if (rx >= (wp->width - KFG_BORDER_WIDTH))
brush = wp->border_bg;
else if (ry >= (wp->height - KFG_BORDER_HEIGHT))
brush = wp->border_bg;
else
brush = wp->bg;
i = pixel_index(parent, x, y);
framep[i] = brush;
}
}
}
/*
* Draw a window on the screen
*
* @parent: Parent window
* @wp: New window to draw
*
* TODO: Double buffering and multiple windows.
*/
int
kfg_win_draw(struct kfg_window *parent, struct kfg_window *wp)
{
kfgpos_t start_x, start_y;
kfgpos_t end_x, end_y;
kfgpos_t max_x, max_y;
kfgdim_t width, height;
if (parent == NULL) {
return -EINVAL;
}
if (parent->framebuf == NULL) {
return -EINVAL;
}
max_x = wp->x + parent->width;
max_y = wp->y + parent->height;
/* Don't overflow the framebuffer! */
if ((wp->x + wp->width) > max_x) {
wp->x = max_x;
}
if ((wp->y + wp->height) > max_y) {
wp->y = max_y;
}
draw_win(parent, wp);
return 0;
}
int
kfg_win_putstr(struct kfg_window *wp, struct kfg_text *tp)
{
size_t slen;
const char *p;
kfgpos_t x, y;
if (tp == NULL)
return -EINVAL;
if (tp->text == NULL)
return -EINVAL;
slen = strlen(tp->text);
x = (wp->x + tp->x) + (KFG_BORDER_WIDTH + 1);
y = (KFG_TITLE_HEIGHT + wp->y) + tp->y;
p = tp->text;
while (slen--) {
if (y >= wp->height) {
break;
}
kfg_win_putc(wp, x, y, *(p++));
x += FONT_WIDTH;
if (x >= wp->width) {
y += FONT_HEIGHT;
x = wp->x + (KFG_BORDER_WIDTH + 1);
}
}
return 0;
}
|