-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreen.c
More file actions
325 lines (285 loc) · 9.03 KB
/
screen.c
File metadata and controls
325 lines (285 loc) · 9.03 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "screen.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include "fonts/8x16.h"
int screen_open(screen_t *screen, const char *dev_fb, const char *sys_backlight) {
screen->fd = open(dev_fb, O_RDWR);
if (screen->fd < 0) {
perror("open fb");
return -1;
}
if (ioctl(screen->fd, FBIOBLANK, 0) < 0) {
perror("unblank screen");
return -1;
}
if (ioctl(screen->fd, FBIOGET_FSCREENINFO, &screen->finfo) < 0 || ioctl(screen->fd, FBIOGET_VSCREENINFO, &screen->vinfo) < 0) {
close(screen->fd);
perror("get screen info");
return -1;
}
if (screen->vinfo.yres_virtual > screen->vinfo.yres) {
screen->vinfo.yres_virtual = screen->vinfo.yres;
screen->vinfo.yoffset = 0;
screen->vinfo.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
if (ioctl(screen->fd, FBIOPUT_VSCREENINFO, &screen->vinfo) < 0) {
perror("set screen info");
fprintf(stderr, "Failed to force single buffer\n");
}
else {
printf("Switch to single buffer\n");
}
}
screen->mem = mmap(NULL, screen->finfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, screen->fd, 0);
if (screen->mem == MAP_FAILED) {
close(screen->fd);
perror("screen mmap");
return -1;
}
snprintf(screen->sys_backlight, sizeof(screen->sys_backlight), "%s/max_brightness", sys_backlight);
FILE *f = fopen(screen->sys_backlight, "r");
if (!f) {
perror("open backlight max");
return -1;
}
if (fscanf(f, "%d", &screen->max_bright) < 0 || screen->max_bright <= 0) {
fclose(f);
perror("read backlight max");
return -1;
}
fclose(f);
snprintf(screen->sys_backlight, sizeof(screen->sys_backlight), "%s/brightness", sys_backlight);
screen->columns = (int)screen->vinfo.xres / font_w;
screen->rows = (int)screen->vinfo.yres / font_h;
screen->fg_color = DEFAULT_FG;
screen->bg_color = DEFAULT_BG;
screen->cursor = (pt_t){0, 0};
screen->cursor_color = COLOR_BRIGHT_WHITE;
printf("Screen: %dx%d, %d bpp, %d columns x %d rows\n", screen->vinfo.xres, screen->vinfo.yres, screen->vinfo.bits_per_pixel, screen->columns, screen->rows);
return 0;
}
int screen_open_virtual(screen_t *screen, const int width, const int height) {
screen->fd = -1; // no file descriptor for virtual screen
screen->vinfo.bits_per_pixel = 32;
screen->vinfo.xres = screen->vinfo.xres_virtual = width;
screen->vinfo.yres = screen->vinfo.yres_virtual = height;
const size_t mem_size = width * height * (screen->vinfo.bits_per_pixel / 8);
screen->mem = malloc(mem_size);
if (!screen->mem) {
perror("virtual screen mmap");
return -1;
}
screen->columns = width / font_w;
screen->rows = height / font_h;
screen->fg_color = DEFAULT_FG;
screen->bg_color = DEFAULT_BG;
screen->cursor = (pt_t){0, 0};
screen->cursor_color = COLOR_BRIGHT_WHITE;
screen->max_bright = 100;
printf("Virtual Screen: %dx%d, %d bpp, %d columns x %d rows\n", width, height, screen->vinfo.bits_per_pixel, screen->columns, screen->rows);
return 0;
}
void screen_close(screen_t *screen) {
munmap(screen->mem, screen->finfo.smem_len);
screen->mem = nullptr;
close(screen->fd);
screen->fd = -1;
}
void screen_backlight(const screen_t *screen, int percent) {
if (percent < 0) percent = 0;
if (percent > 100) percent = 100;
percent = percent * screen->max_bright / 100;
const int fd = open(screen->sys_backlight, O_WRONLY);
if (fd >= 0) {
char buf[16];
snprintf(buf, sizeof(buf), "%d", percent);
printf("Backlight: %s\n", buf);
const ssize_t n = (ssize_t)strlen(buf);
if (write(fd, buf, n) < n) {
perror("set brightness");
}
fsync(fd);
close(fd);
}
}
void screen_redraw(screen_t *screen) {
uint32_t saved_cursor[font_h][font_w];
screen_put_cursor(screen, saved_cursor, 1);
if (screen->fd >= 0) {
screen->vinfo.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
ioctl(screen->fd, FBIOPUT_VSCREENINFO, &screen->vinfo);
}
else {
screen_save_bmp(screen, "/tmp/screen.bmp");
}
screen_put_cursor(screen, saved_cursor, 0);
}
uint32_t screen_pixel_set(const screen_t *screen, const int x, const int y, const uint32_t c) {
if (x < 0 || y < 0 || (uint32_t)x >= screen->vinfo.xres || (uint32_t)y >= screen->vinfo.yres) {
return 0;
}
uint32_t *pixel = (uint32_t *)screen->mem + y * screen->vinfo.xres + x;
const uint32_t old_pixel = *pixel;
*pixel = c;
return old_pixel;
}
uint32_t screen_pixel_get(const screen_t *screen, const int x, const int y) {
if (x < 0 || y < 0 || (uint32_t)x >= screen->vinfo.xres || (uint32_t)y >= screen->vinfo.yres) {
return 0;
}
const uint32_t *pixel = (uint32_t *)screen->mem + y * screen->vinfo.xres + x;
return *pixel;
}
void screen_fill(const screen_t *screen, const rect_t *rect, const uint32_t c) {
const rect_t full = {.x = 0, .y = 0, .w = screen->columns + 1, .h = screen->rows + 1};
if (!rect) rect = &full;
for (int y = 0; y < rect->h * font_h; ++y) {
for (int x = 0; x < rect->w * font_w; ++x) {
screen_pixel_set(screen, rect->x * font_w + x, rect->y * font_h + y, c);
}
}
}
void screen_put_ch(const screen_t *screen, const unsigned char c) {
const int32_t x = screen->cursor.x;
const int32_t y = screen->cursor.y;
const uint8_t *glyph = &font_data[c][0];
for (int row = 0; row < font_h; row++) {
const uint8_t line = glyph[row];
for (int col = 0; col < font_w; col++) {
screen_pixel_set(screen, x * font_w + col, y * font_h + row, line & 0x80 >> col ? screen->fg_color : screen->bg_color);
}
}
}
void screen_put_cursor(const screen_t *screen, uint32_t saved_cursor[font_h][font_w], const int put) {
const int32_t x = screen->cursor.x;
const int32_t y = screen->cursor.y;
const uint8_t *glyph = &font_data[255][0];
for (int row = 0; row < font_h; row++) {
const uint8_t line = glyph[row];
for (int col = 0; col < font_w; col++) {
if (line & 0x80 >> col) {
if (put) {
saved_cursor[row][col] = screen_pixel_set(screen, x * font_w + col, y * font_h + row, screen->cursor_color);
}
else {
screen_pixel_set(screen, x * font_w + col, y * font_h + row, saved_cursor[row][col]);
}
}
}
}
}
void screen_move_clr(const screen_t *screen, const pt_t *dst, const rect_t *src) {
const pt_t d = {
.x = dst->x * font_w,
.y = dst->y * font_h
};
const rect_t s = {
.x = src->x * font_w,
.y = src->y * font_h,
.w = src->w * font_w,
.h = src->h * font_h
};
for (int y = 0; y < s.h; y++) {
for (int x = 0; x < s.w; x++) {
screen_pixel_set(screen, d.x + x, d.y + y, screen_pixel_get(screen, s.x + x, s.y + y));
screen_pixel_set(screen, s.x + x, s.y + y, screen->bg_color);
}
}
}
void screen_scroll_v(const screen_t *screen, const int offset) {
if (offset) {
const rect_t src = {.x = 0, .y = 0, .w = screen->columns, .h = screen->rows};
const pt_t dst = {.x = 0, .y = -offset};
screen_move_clr(screen, &dst, &src);
}
}
void screen_scroll_h(const screen_t *screen, const int offset) {
if (offset == 0) {
return;
}
rect_t src;
pt_t dst;
if (offset < 0) {
// scroll left: move content right
src = (rect_t){.x = 0, .y = 0, .w = screen->columns - offset, .h = screen->rows};
dst = (pt_t){.x = offset, .y = 0};
}
else {
// scroll right: move content left
src = (rect_t){.x = -offset, .y = 0, .w = screen->columns + offset, .h = screen->rows};
dst = (pt_t){.x = 0, .y = 0};
}
screen_move_clr(screen, &dst, &src);
}
void screen_clear(screen_t screen, const int backlight_percent) {
screen_fill(&screen, nullptr, COLOR_BLACK);
screen_redraw(&screen);
screen_backlight(&screen, backlight_percent);
}
int screen_save_bmp(const screen_t *screen, const char *filename) {
const uint32_t width = screen->vinfo.xres;
const uint32_t height = screen->vinfo.yres;
const uint32_t bpp = screen->vinfo.bits_per_pixel;
const uint32_t row_size = width * (bpp / 8);
const uint32_t padding = (4 - (row_size % 4)) % 4;
const uint32_t file_size = 54 + (row_size + padding) * height;
FILE *f = fopen(filename, "wb");
if (!f) {
perror("fopen");
return -1;
}
// BMP header (14 bytes)
const uint8_t header[14] = {
'B', 'M', // Signature
file_size, // File size (little-endian)
file_size >> 8,
file_size >> 16,
file_size >> 24,
0, 0, 0, 0, // Reserved
54, 0, 0, 0 // Pixel data offset
};
fwrite(header, 1, 14, f);
// DIB header (40 bytes)
const uint8_t dib[40] = {
40, 0, 0, 0, // Header size
width, // Width (little-endian)
width >> 8,
width >> 16,
width >> 24,
height, // Height (little-endian)
height >> 8,
height >> 16,
height >> 24,
1, 0, // Color planes
bpp, 0, // Bits per pixel
0, 0, 0, 0, // Compression (none)
0, 0, 0, 0, // Image size (can be 0)
0, 0, 0, 0, // X pixels per meter
0, 0, 0, 0, // Y pixels per meter
0, 0, 0, 0, // Colors in palette
0, 0, 0, 0 // Important colors
};
fwrite(dib, 1, 40, f);
// pixel data (bottom-up, with padding)
uint8_t *row_buffer = malloc(row_size);
if (!row_buffer) {
fclose(f);
return -1;
}
for (uint32_t y = height; y-- > 0;) {
// copy row from screen memory (top-down to bottom-up)
memcpy(row_buffer, (uint8_t *)screen->mem + y * row_size, row_size);
fwrite(row_buffer, 1, row_size, f);
// write padding
for (uint32_t p = 0; p < padding; p++) {
fputc(0, f);
}
}
free(row_buffer);
fclose(f);
return 0;
}