summaryrefslogtreecommitdiff
path: root/fbgl.h
blob: c15e0aaded42513b2a9179bcbf2cf201e700e76e (plain)
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#ifndef __FBGL_H__
#define __FBGL_H__

#define VERSION "0.1.0"
#define NAME "FBGL"
#define DEFAULT_FB "/dev/fb0"

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <linux/fb.h>
#include <stddef.h>
#include <stdio.h>
#include <termios.h>
#include <signal.h>

static struct termios orig_termios;

/**
* Structs
*/
typedef struct fbgl {
	int width;
	int height;
	int fd;
	size_t screen_size;
	uint32_t *pixels;
	struct fb_var_screeninfo vinfo; // Variable screen information
	struct fb_fix_screeninfo finfo; // Fixed screen information
} fbgl_t;

typedef struct fbgl_window {
	int x; // Top-left x-coordinate of the window
	int y; // Top-left y-coordinate of the window
	int width; // Width of the window
	int height; // Height of the window
	fbgl_t *fb; // Pointer to the framebuffer context
} fbgl_window_t;

typedef struct fbgl_psf2_header {
	uint8_t magic[2]; // Magic number, should be {0x72, 0xB5}
	uint8_t version; // Version of PSF2 (usually 0)
	uint8_t header_size; // Size of the header (usually 32 bytes)
	uint16_t flags; // Flags (usually 0)
	uint16_t numglyphs; // Number of glyphs (characters)
	uint16_t bytes_per_glyph; // Number of bytes per glyph (depends on font size)
	uint16_t height; // Height of each character in pixels
	uint16_t width; // Width of each character in pixels
	uint8_t *glyphs; // Pointer to the glyph data
} fbgl_psf2_header_t;

#ifdef FBGL_HIDE_CURSOR
#include <linux/kd.h>
int fbgl_hide_cursor(int fd)
{
	int tty_fd = open("/dev/tty0", O_RDWR);
	if (tty_fd == -1) {
		perror("Error opening /dev/tty0");
		return -1;
	}

	if (ioctl(tty_fd, KDSETMODE, KD_GRAPHICS) == -1) {
		perror("Error setting graphics mode");
		close(tty_fd);
		return -1;
	}

	close(tty_fd);
	return 0;
}
#endif //FBGL_HIDE_CURSOR

#ifdef __cplusplus
extern "C" {
#endif

/**
 * General purpose methods
 */
char const *fbgl_name_info(void);
char const *fbgl_version_info(void);
void fbgl_enable_raw_mode();
void fbgl_disable_raw_mode();
void fbgl_cleanup(int sig);
int fbgl_check_esc_key();
void fbgl_set_signal_handlers();

/*Create and destroy methods*/
int fbgl_init(const char *device, fbgl_t *fb);
void fbgl_destroy(fbgl_t *fb);

/**
* Drawing functions
*/
void fbgl_clear(uint32_t color);
void fbgl_put_pixel(int x, int y, uint32_t color, fbgl_t *fb);
void fbgl_draw_line(int x0, int y0, int x1, int y1, uint32_t color);
void fbgl_set_bg();

/**
* Display methods
*/
void fbgl_display();

/**
* Access framebuffer data methods
*/
uint32_t *fb_get_data(void);
int fb_get_width(void);
int fb_get_height(void);

/**
* PSF2 Font Methods
*/
fbgl_psf2_header_t *fbgl_load_psf2_font(const char *font_file);
void fbgl_render_char(fbgl_t *framebuffer, int fb_width, int fb_height, int x,
		      int y, char ch, fbgl_psf2_header_t *font);
void fbgl_render_text(fbgl_t *framebuffer, int fb_width, int fb_height, int x,
		      int y, const char *text, fbgl_psf2_header_t *font);
void fbgl_free_psf2_font(fbgl_psf2_header_t *font);

#ifdef FBGL_IMPLEMENTATION

char const *fbgl_name_info(void)
{
	return NAME;
}

char const *fbgl_version_info(void)
{
	return VERSION;
}

int fbgl_init(const char *device, fbgl_t *fb)
{
	if (!fb) {
		fprintf(stderr, "Error: fbgl_t pointer is NULL.");
		return -1;
	}

	fb->fd = device == NULL ? open(DEFAULT_FB, O_RDWR) :
				  open(device, O_RDWR);
	if (fb->fd == -1) {
		perror("Error openning framebuffer device");
		return -1;
	}

	if (ioctl(fb->fd, FBIOGET_FSCREENINFO, &fb->finfo) == -1) {
		perror("Error: Reading fixed information.");
		close(fb->fd);
		return -1;
	}
	if (ioctl(fb->fd, FBIOGET_VSCREENINFO, &fb->vinfo) == -1) {
		perror("Error reading variable information");
		close(fb->fd);
		return -1;
	}

#ifdef FBGL_HIDE_CURSOR
	fbgl_hide_cursor(fb->fd);
	fbgl_set_signal_handlers();
	fbgl_enable_raw_mode();
#endif //FBGL_HIDE_CURSOR

	fb->width = fb->vinfo.xres;
	fb->height = fb->vinfo.yres;
	fb->screen_size = fb->finfo.smem_len;

	// Map framebuffer to memory
	fb->pixels = (uint32_t *)mmap(NULL, fb->screen_size,
				      PROT_READ | PROT_WRITE, MAP_SHARED,
				      fb->fd, 0);
	if (fb->pixels == MAP_FAILED) {
		perror("Error mapping framebuffer device to memory");
		close(fb->fd);
		return -1;
	}

	return 0;
}

void fbgl_destroy(fbgl_t *fb)
{
	if (!fb || fb->fd == -1) {
		fprintf(stderr,
			"Error: framebuffer not initialized or already destroyed.\n");
		return;
	}

	if (fb->pixels && fb->pixels != MAP_FAILED) {
		munmap(fb->pixels, fb->screen_size);
	}

	close(fb->fd);
	fb->fd = -1;

#ifdef FBGL_HIDE_CURSOR
	fbgl_disable_raw_mode();
#endif // FBGL_HIDE_CURSOR
}

void fbgl_set_bg(fbgl_t *fb, uint32_t color)
{
	if (!fb || fb->fd == -1) {
		fprintf(stderr, "Error: framebuffer not initialized.\n");
		return;
	}

	// Fill the entire framebuffer with the specified color
	for (size_t i = 0; i < (fb->width * fb->height); ++i) {
		fb->pixels[i] = color;
	}
}

void fbgl_put_pixel(int x, int y, uint32_t color, fbgl_t *fb)
{
	if (!fb || !fb->pixels) {
		fprintf(stderr, "Error: framebuffer not initialized.\n");
		return;
	}

	if (x < 0 || x >= fb->width || y < 0 || y >= fb->height) {
		return; // Ignore out-of-bound coordinates
	}

	size_t index = y * fb->width + x;
	fb->pixels[index] = color;
}

void fbgl_enable_raw_mode()
{
	struct termios raw;

	if (tcgetattr(STDIN_FILENO, &orig_termios) == -1) {
		perror("tcgetattr");
		exit(EXIT_FAILURE);
	}
	raw = orig_termios;
	raw.c_lflag &= ~(ECHO | ICANON);
	if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) {
		perror("tcsetattr");
		exit(EXIT_FAILURE);
	}
}

void fbgl_disable_raw_mode()
{
	if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1) {
		perror("tcsetattr");
	}
}

void fbgl_cleanup(int sig)
{
	fbgl_disable_raw_mode();
	printf("\033[2J\033[H"); // Clear the terminal screen and move the cursor to top-left
	exit(sig);
}

int fbgl_check_esc_key()
{
	char c;
	struct timeval tv = { 0, 0 };
	fd_set fds;
	FD_ZERO(&fds);
	FD_SET(STDIN_FILENO, &fds);

	// Check if there's input available
	if (select(STDIN_FILENO + 1, &fds, NULL, NULL, &tv) == -1) {
		perror("select");
		return 0;
	}

	if (FD_ISSET(STDIN_FILENO, &fds)) {
		if (read(STDIN_FILENO, &c, 1) == -1) {
			perror("read");
			return 0;
		}
		return c == 27; // ASCII value of the `Esc` key
	}

	return 0;
}

void fbgl_set_signal_handlers()
{
	struct sigaction sa;
	sa.sa_handler = fbgl_cleanup;
	sa.sa_flags = 0;
	sigemptyset(&sa.sa_mask);

	if (sigaction(SIGINT, &sa, NULL) == -1 ||
	    sigaction(SIGTERM, &sa, NULL) == -1) {
		perror("sigaction");
		exit(EXIT_FAILURE);
	}
}

fbgl_psf2_header_t *fbgl_load_psf2_font(const char *font_file)
{
	fbgl_psf2_header_t *font = NULL;
	int fd = open(font_file, O_RDONLY);
	if (fd == -1) {
		perror("Error opening font file");
		return NULL;
	}

	// Read the font header
	font = (fbgl_psf2_header_t *)malloc(sizeof(fbgl_psf2_header_t));
	if (read(fd, font, sizeof(fbgl_psf2_header_t)) !=
	    sizeof(fbgl_psf2_header_t)) {
		perror("Error reading font header");
		free(font);
		close(fd);
		return NULL;
	}

	// Check magic number
	if (font->magic[0] != 0x72 || font->magic[1] != 0xB5) {
		fprintf(stderr, "Invalid PSF2 font magic number.\n");
		free(font);
		close(fd);
		return NULL;
	}

	// Read the font glyph data
	font->glyphs =
		(uint8_t *)malloc(font->numglyphs * font->bytes_per_glyph);
	if (read(fd, font->glyphs, font->numglyphs * font->bytes_per_glyph) !=
	    font->numglyphs * font->bytes_per_glyph) {
		perror("Error reading glyph data");
		free(font->glyphs);
		free(font);
		close(fd);
		return NULL;
	}

	close(fd);
	return font;
    }

    void fbgl_render_char(fbgl_t *framebuffer, int fb_width, int fb_height, int x, int y, char ch, fbgl_psf2_header_t *font)
{
    if (!framebuffer || !font) return;

    int glyph_idx = (uint8_t)ch; // Get the glyph index for the character
    if (glyph_idx >= font->numglyphs) return;

    uint8_t *glyph_data = font->glyphs + (glyph_idx * font->bytes_per_glyph);
    for (int j = 0; j < font->height; ++j) {
        for (int i = 0; i < font->width; ++i) {
            if (glyph_data[j] & (1 << (7 - i))) {
                fbgl_put_pixel(x + i, y + j, 0xFFFFFF, framebuffer);
            }
        }
    }
}

void fbgl_render_text(fbgl_t *framebuffer, int fb_width, int fb_height, int x, int y, const char *text, fbgl_psf2_header_t *font)
{
    while (*text) {
        fbgl_render_char(framebuffer, fb_width, fb_height, x, y, *text, font);
        x += font->width;
        ++text;
    }
}

void fbgl_free_psf2_font(fbgl_psf2_header_t *font)
{
    if (font) {
        free(font->glyphs);
        free(font);
    }
}

#endif

#ifdef __cplusplus
} // extern "C"
#endif
#endif // __FBGL_H__