-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathframebuffer.c
233 lines (195 loc) · 5.5 KB
/
framebuffer.c
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* GreenPois0n Cynanide - framebuffer.c
* Copyright (C) 2010 Chronic-Dev Team
* Copyright (C) 2010 Joshua Hill
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
#include <string.h>
#include "font.h"
#include "device.h"
#include "common.h"
#include "commands.h"
#include "framebuffer.h"
static Font* gFbFont;
static unsigned int gFbX;
static unsigned int gFbY;
static unsigned int gFbTWidth;
static unsigned int gFbTHeight;
static unsigned int gFbWidth;
static unsigned int gFbHeight;
Bool gFbHasInit = FALSE;
Bool gFbDisplayText = FALSE;
static unsigned int gFbBackgroundColor;
static unsigned int gFbForegroundColor;
inline int font_get_pixel(Font* font, int ch, int x, int y) {
register int index = ((font->width * font->height) * ch) + (font->width * y) + x;
return (font->data[index / 8] >> (index % 8)) & 0x1;
}
volatile unsigned int* fb_get_pixel(register unsigned int x, register unsigned int y) {
return (((unsigned int*)FRAMEBUFFER) + (y * gFbWidth) + x);
}
static void fb_scrollup() {
register volatile unsigned int* newFirstLine = fb_get_pixel(0, gFbFont->height);
register volatile unsigned int* oldFirstLine = fb_get_pixel(0, 0);
register volatile unsigned int* end = oldFirstLine + (gFbWidth * gFbHeight);
while(newFirstLine < end) {
*(oldFirstLine++) = *(newFirstLine++);
}
while(oldFirstLine < end) {
*(oldFirstLine++) = gFbBackgroundColor;
}
gFbY--;
}
void fb_setup() {
gFbFont = (Font*) font_data;
gFbBackgroundColor = COLOR_BLACK;
gFbForegroundColor = COLOR_WHITE;
gFbWidth = FRAMEBUFFER_WIDTH;
gFbHeight = FRAMEBUFFER_HEIGHT;
gFbTWidth = gFbWidth / gFbFont->width;
gFbTHeight = gFbHeight / gFbFont->height;
}
int fb_init() {
if(gFbHasInit) return 0;
fb_setup();
fb_clear();
fb_set_loc(0,0);
fb_display_text(TRUE);
fb_print("=====================================================");
#ifdef S5L8930X
fb_print("=====================================================");
fb_print(" ");
#endif
fb_print(" greenpois0n ");
#ifdef S5L8930X
fb_print(" ");
#endif
fb_print(" http://www.greenpois0n.com ");
#ifdef S5L8930X
fb_print(" ");
fb_print("=====================================================");
#endif
fb_print("=====================================================");
cmd_add("fbecho", &fb_cmd, "write characters back to framebuffer");
gFbHasInit = TRUE;
return 0;
}
int fb_cmd(int argc, CmdArg* argv) {
cmd_start();
int i = 0;
if (argc < 2) {
puts("usage: fbecho <message>\n");
return 0;
}
//enter_critical_section();
for (i = 1; i < argc; i++) {
fb_print(argv[i].string);
fb_print(" ");
}
//exit_critical_section();
fb_print("\n");
return 0;
}
void fb_clear() {
unsigned int *p = 0;
for(p = (unsigned int*)FRAMEBUFFER; p < (unsigned int*)(FRAMEBUFFER + (gFbWidth * gFbHeight * 4)); p++) {
*p = gFbBackgroundColor;
}
}
unsigned int fb_get_x() {
return gFbX;
}
unsigned int fb_get_y() {
return gFbY;
}
unsigned int fb_get_width() {
return gFbTWidth;
}
unsigned int fb_get_height() {
return gFbTHeight;
}
void fb_set_loc(unsigned int x, unsigned int y) {
gFbX = x;
gFbY = y;
}
void fb_set_colors(unsigned int fore, unsigned int back) {
gFbForegroundColor = fore;
gFbBackgroundColor = back;
}
void fb_display_text(Bool option) {
gFbDisplayText = option;
}
void fb_putc(int c) {
if(c == '\r') {
gFbX = 0;
} else if(c == '\n') {
gFbX = 0;
gFbY++;
} else {
register unsigned int sx;
register unsigned int sy;
for(sy = 0; sy < gFbFont->height; sy++) {
for(sx = 0; sx < gFbFont->width; sx++) {
if(font_get_pixel(gFbFont, c, sx, sy)) {
*(fb_get_pixel(sx + (gFbFont->width * gFbX), sy + (gFbFont->height * gFbY))) = gFbForegroundColor;
} else {
*(fb_get_pixel(sx + (gFbFont->width * gFbX), sy + (gFbFont->height * gFbY))) = gFbBackgroundColor;
}
}
}
gFbX++;
}
if(gFbX == gFbTWidth) {
gFbX = 0;
gFbY++;
}
if(gFbY == gFbTHeight) {
fb_scrollup();
}
}
void fb_print(const char* str) {
if(!gFbDisplayText)
return;
unsigned int len = strlen(str);
int i;
for(i = 0; i < len; i++) {
fb_putc(str[i]);
}
}
void fb_print_force(const char* str) {
size_t len = strlen(str);
int i;
for(i = 0; i < len; i++) {
fb_putc(str[i]);
}
}
void fb_draw_image(unsigned int* image, unsigned int x, unsigned int y, unsigned int width, unsigned int height) {
register unsigned int sx;
register unsigned int sy;
for(sy = 0; sy < height; sy++) {
for(sx = 0; sx < width; sx++) {
*(fb_get_pixel(sx + x, sy + y)) = image[(sy * width) + sx];
}
}
}
void fb_capture_image(unsigned int* image, unsigned int x, unsigned int y, unsigned int width, unsigned int height) {
register unsigned int sx;
register unsigned int sy;
for(sy = 0; sy < height; sy++) {
for(sx = 0; sx < width; sx++) {
image[(sy * width) + sx] = *(fb_get_pixel(sx + x, sy + y));
}
}
}