commit 557e939
uint
·
2026-07-14 22:15:05 +0000 UTC
parent 9fc03e2
reformat comments
9 files changed,
+116,
-285
+6,
-47
1@@ -5,62 +5,21 @@
2
3 #include "font.h"
4
5-/**
6- * @brief clear a buffer size wxh
7- *
8- * @param dst buffer to clear
9- * @param w width of buffer
10- * @param h height of buffer
11- * @param col colour to clear buffer with
12- */
13+/* clear a buffer size wxh */
14 void draw_clear(uint32_t* dst, int w, int h, uint32_t col);
15
16-/**
17- * @brief fill the background of a rune
18- *
19- * @param dst buffer to draw to
20- * @param w width of buffer
21- * @param h height of buffer
22- * @param cl rune column
23- * @param rw rune row
24- * @param cw rune width
25- * @param ch rune height
26- * @param bg rune background colour
27- */
28+/* draw a run of properties (look: vvvv) to screen */
29 void draw_rune(uint32_t* dst, int w, int h, int cl, int rw, int cw, int ch,
30 uint32_t bg);
31
32-/**
33- * @brief fill the background of a rune
34- *
35- * @param dst buffer to draw to
36- * @param w width of buffer
37- * @param h height of buffer
38- * @param cl rune column
39- * @param rw rune row
40- * @param cw rune width
41- * @param ch rune height
42- * @param fg rune foreground colour
43- *
44- * (TODO: runes)
45- */
46+/* fill the background of a rune
47+ (TODO: runes) */
48 void draw_caret(uint32_t* dst, int w, int h, int cl, int rw, int cw, int ch,
49 uint32_t fg);
50
51-/**
52- * @brief draw one codepoint to buffer
53- *
54- * @param face fontface to draw from
55- * @param dst destination buffer
56- * @param w buffer width
57- * @param h buffer height
58- * @param x x-position to draw at
59- * @param baseline text baseline y-position
60- * @param cp codepoint to draw
61- * @param fg foreground colour
62- */
63+/* draw one codepoint to buffer */
64 void draw_codepoint(Fontface* face, uint32_t* dst, int w, int h, int x,
65- int baseline, uint32_t cp, uint32_t fg);
66+ int baseline, uint32_t cp, uint32_t fg);
67
68 #endif /* DRAW_H */
69
+14,
-24
1@@ -7,39 +7,29 @@
2 #define BDF_GLYPHS_MAX (65536)
3
4 typedef struct {
5- bool valid;
6- uint8_t* bmp;
7- int adv; /* advance */
8- int w;
9- int h;
10- int xoff;
11- int yoff;
12+ bool valid;
13+ uint8_t* bmp;
14+ int adv; /* advance */
15+ int w;
16+ int h;
17+ int xoff;
18+ int yoff;
19 } BdfFont;
20 typedef BdfFont BdfGlyph;
21
22 typedef struct {
23- BdfFont* bdf;
24+ BdfFont* bdf;
25
26- double asc; /* ascent */
27- double dsc; /* descent */
28- int cellw;
29- int cellh;
30+ double asc; /* ascent */
31+ double dsc; /* descent */
32+ int cellw;
33+ int cellh;
34 } Fontface;
35
36-/**
37- * @brief load font from path
38- *
39- * @param f font struct to write info to
40- * @param path font path
41- * @return 0=success, -1=failed
42- */
43+/* load font from path into `f` */
44 int font_load(Fontface* f, const char* path);
45
46-/**
47- * @brief free a loaded font
48- *
49- * @param f font struct to free
50- */
51+/* free font `f` */
52 void font_free(Fontface* f);
53
54 #endif /* FONT_H */
+46,
-71
1@@ -12,7 +12,7 @@
2 #define TERM_DEFAULT_BG 0xff222222u
3 #define TERM_ATTR_REVERSE (1 << 0)
4
5-enum TState {
6+typedef enum {
7 TSTATE_NORMAL,
8 TSTATE_ESC,
9 TSTATE_CSI,
10@@ -20,98 +20,73 @@ enum TState {
11 TSTATE_OSC_ESC,
12 TSTATE_CHARSET,
13 TSTATE_CHARSET_SKIP,
14-};
15+} TState;
16
17 typedef struct {
18- uint32_t cp; /* codepoint */
19- uint32_t fg;
20- uint32_t bg;
21- uint8_t attr;
22- uint8_t width;
23- bool dmg; /* rune damaged */
24+ uint32_t cp; /* codepoint */
25+ uint32_t fg;
26+ uint32_t bg;
27+ uint8_t attr;
28+ uint8_t width;
29+ bool dmg; /* rune damaged */
30 } Rune;
31
32 typedef struct {
33- int x;
34- int y;
35+ int x;
36+ int y;
37 } Caret;
38
39 typedef struct {
40- Rune* runes;
41- Rune* alt;
42- Caret saved;
43- uint32_t fg;
44- uint32_t bg;
45- uint8_t attr;
46-
47- int cols;
48- int rows;
49- int scroll_top;
50- int scroll_bot;
51-
52- int ptyfd;
53- pid_t ptypid;
54-
55- enum TState state;
56- bool acs[2];
57- int charset;
58- int charset_target;
59- bool wrapnext;
60- bool cursor_visible;
61- int csi_params[CSI_PARAMS_MAX];
62- int csi_idx;
63- char utf8[4];
64- size_t utf8_len;
65+ Rune* runes;
66+ Rune* alt;
67+ Caret saved;
68+ uint32_t fg;
69+ uint32_t bg;
70+ uint8_t attr;
71+
72+ int cols;
73+ int rows;
74+ int scroll_top;
75+ int scroll_bot;
76+
77+ int ptyfd;
78+ pid_t ptypid;
79+
80+ TState state;
81+ bool acs[2];
82+ int charset;
83+ int charset_target;
84+ bool wrapnext;
85+ bool cursor_visible;
86+ int csi_params[CSI_PARAMS_MAX];
87+ int csi_idx;
88+ char utf8[4];
89+ size_t utf8_len;
90 } Term;
91
92-/**
93- * @brief clear screen by filling runes with ' ' and reseting
94- * to default terminal foreground and background
95- *
96- * @param t terminal instance to scroll
97- * @param c cursor instance to position
98- */
99+/* clear screen by filling runes with ' ' and reseting
100+ to default terminal foreground and background */
101 void term_clear(Term* t, Caret* c);
102
103-/**
104- * @brief damage all the runes on terminal
105- */
106+/* damage all the runes on terminal */
107 void term_damage_all(Term* t);
108
109-/** @brief damage a single cell on terminal
110- */
111+/* damage a single cell on terminal */
112 void term_damage_rune(Term* t, int x, int y);
113
114-/**
115- * @brief handle placing special and normal characters
116- *
117- * @param t terminal instance
118- * @param c cursor instance
119- * @param cp codepoint to place
120- */
121+/* put a character on terminal, handle placing special
122+ and normal characters */
123 void term_putc(Term* t, Caret* c, uint32_t cp);
124
125-/** TODO
126- */
127+/* destroy old terminal and create create new terminal
128+ with specified dimensions */
129 int term_resize(Term* t, Caret* c, int cols, int rows);
130
131-/**
132- * @brief scroll viewport up by one
133- *
134- * @param t terminal instance to scroll
135- */
136+/* scroll viewport up by one */
137 void term_scroll(Term* t);
138
139-/**
140- * @brief handle placing special and normal characters
141- *
142- * @param t terminal instance
143- * @param c cursor instance
144- * @param s string buffer to write
145- * @param n length of string buffer
146- *
147- * @return whether it has been damaged
148- */
149+/* processes terminal input, parse utf8, escape sequences, update
150+ caret, screen state, and damage status. */
151 bool term_write(Term* t, Caret* c, const char* s, size_t n);
152
153 #endif /* TERM_H */
+4,
-21
1@@ -4,31 +4,14 @@
2 #include <stddef.h>
3 #include <stdint.h>
4
5-/**
6- * @brief exit program after printing a message
7- *
8- * @param ec error code
9- * @param msg message to print
10- */
11+/* perror `msg` and exit program with `ec`
12+ TODO? stdarg */
13 void die(int ec, const char* msg);
14
15-/**
16- * @brief convert red, green, blue, alpha values to a AARRGGBB value
17- *
18- * @param r red value
19- * @param g green value
20- * @param b blue value
21- * @param a alpha value
22- *
23- * @return packed color value
24- */
25+/* pack r, g, b and a into u32 (AARRGGBB) */
26 uint32_t rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
27
28-/**
29- * @brief allocate or die
30- *
31- * @param len bytes to allocate
32- */
33+/* malloc, die on failure */
34 void* xmalloc(size_t len);
35
36 #endif /* UTILS_H */
+10,
-35
1@@ -52,9 +52,7 @@ static Term term = {
2 .cursor_visible = true,
3 };
4
5-/**
6- * @brief release resources, close window (TODO)
7- */
8+/* clean up resources */
9 static void cleanup(void)
10 {
11 free(term.runes);
12@@ -67,11 +65,7 @@ static void cleanup(void)
13 }
14 }
15
16-/**
17- * @brief dispatch correct key to pty
18- *
19- * @param c character check dispatch
20- */
21+/* dispatch correct keycode handling non ascii characters */
22 static bool handle_key(const MausEvent* ev)
23 {
24 char text;
25@@ -110,9 +104,7 @@ static bool handle_key(const MausEvent* ev)
26 return false;
27 }
28
29-/**
30- * @brief initialise: window (TODO)
31- */
32+/* initialise font, window, pty */
33 static void init(void)
34 {
35 setlocale(LC_CTYPE, "");
36@@ -145,7 +137,6 @@ static void init(void)
37 if (term.ptypid < 0)
38 die(EXIT_FAILURE, "failed to fork pty");
39 if (term.ptypid == 0) {
40- /* TODO: change later */
41 setenv("TERM", term_name, 1);
42
43 /* shell */
44@@ -165,9 +156,7 @@ static void init(void)
45 term_clear(&term, &car);
46 }
47
48-/**
49- * @brief read data from master pty
50- */
51+/* read from pty master */
52 static bool ptyread(void)
53 {
54 char buf[4096];
55@@ -192,12 +181,7 @@ static bool ptyread(void)
56 return damaged;
57 }
58
59-/**
60- * @brief read data to master pty
61- *
62- * @param s string buffer to write to pty
63- * @param n number of characters in buffer
64- */
65+/* write to pty master */
66 static void ptywrite(const char* s, size_t n)
67 {
68 while (n > 0) {
69@@ -230,9 +214,7 @@ static bool reload_font(void)
70 return true;
71 }
72
73-/**
74- * @brief resize pty to reflect window size
75- */
76+/* resize pty to reflect terminal values */
77 static void resize_pty(void)
78 {
79 struct winsize ws = {
80@@ -246,9 +228,7 @@ static void resize_pty(void)
81 ioctl(term.ptyfd, TIOCSWINSZ, &ws);
82 }
83
84-/**
85- * @brief resize window framebuffer to reflect resized size
86- */
87+/* resize window framebuffer to reflect resized size */
88 static int resize_window(int w, int h)
89 {
90 if (w < 1)
91@@ -267,8 +247,7 @@ static int resize_window(int w, int h)
92 return 0;
93 }
94
95-/** TODO
96- */
97+/* TODO */
98 static void resize_terminal(int w, int h)
99 {
100 int cols = w / font.cellw;
101@@ -288,9 +267,7 @@ static void resize_terminal(int w, int h)
102 resize_pty();
103 }
104
105-/**
106- * @brief program event loop (TODO)
107- */
108+/* event loop (TODO) */
109 static void run(void)
110 {
111 MausEvent ev;
112@@ -393,9 +370,7 @@ static void run(void)
113 }
114 }
115
116-/**
117- * @brief wait for activity while allowing window events
118- */
119+/* wait for activity while allowing window events */
120 static void wait_events(void)
121 {
122 struct pollfd pfd = {
+6,
-18
1@@ -1,34 +1,23 @@
2 #include "draw.h"
3
4 static void draw_bitmap(uint32_t* dst, int w, int h, uint8_t* bmp,
5- int bw, int bh, int x, int y, int aa, uint32_t fg);
6+ int bw, int bh, int x, int y, uint32_t fg);
7 static void blend(uint32_t* dst, int w, int h, int x, int y, uint8_t alpha,
8 uint32_t fg);
9
10+/* draw bitmap of properties (look: vvvv) to screen */
11 static void draw_bitmap(uint32_t* dst, int w, int h, uint8_t* bmp,
12- int bw, int bh, int x, int y, int aa, uint32_t fg)
13+ int bw, int bh, int x, int y, uint32_t fg)
14 {
15 for (int by = 0; by < bh; by++) {
16 for (int bx = 0; bx < bw; bx++) {
17 uint8_t a = bmp[by * bw + bx];
18-
19- if (!aa)
20- a = (a > 128) ? 255 : 0;
21-
22 blend(dst, w, h, x + bx, y + by, a, fg);
23 }
24 }
25 }
26
27-/**
28- * @brief blend a pixel using alpha compositong
29- *
30- * @param dst buffer to write to
31- * @param x pixel x-position
32- * @param y piyel y-position
33- * @param alpha coverage from glyph
34- * @param fg foreground colour
35- */
36+/* blend a pixel using alpha compositong */
37 static void blend(uint32_t* dst, int w, int h, int x, int y, uint8_t alpha,
38 uint32_t fg)
39 {
40@@ -38,8 +27,7 @@ static void blend(uint32_t* dst, int w, int h, int x, int y, uint8_t alpha,
41 uint8_t fg_b;
42
43 if (x < 0 || x >= w || y < 0 || y >= h || alpha == 0)
44- /* transparent/oob */
45- return;
46+ return; /* transparent/oob */
47
48 /* ptr to px at (x, y) */
49 px = (uint8_t*)&dst[y * w + x];
50@@ -101,7 +89,7 @@ void draw_codepoint(Fontface* f, uint32_t* dst, int w, int h, int x,
51
52 draw_bitmap(
53 dst, w, h, g->bmp, g->w, g->h, x + g->xoff,
54- baseline - g->yoff - g->h, 1, fg
55+ baseline - g->yoff - g->h, fg
56 );
57 }
58
+5,
-25
1@@ -10,14 +10,7 @@ static int load_bdf(Fontface* f, const char* path);
2 static int strcicmp(char const* a, char const* b);
3 static bool is_bdf(const char* path);
4
5-/*
6- * @brief load a bdf font
7- *
8- * @param f fontface to load font into
9- * @param path path to load font from
10- *
11- * @return -1=fail, 0=success
12- */
13+/* load a bdf font into `f`, -1 on fail, 0 on success */
14 static int load_bdf(Fontface* f, const char* path)
15 {
16 FILE* fp;
17@@ -96,8 +89,7 @@ static int load_bdf(Fontface* f, const char* path)
18
19 /* each bmp row in BDF is padded as a multiple
20 of 8 bits. this rounds width up to the next
21- multiple of 8
22- */
23+ multiple of 8 */
24 rowbits = (bw + 7) & ~7;
25
26 for (int y = 0; y < bh; y++) {
27@@ -171,14 +163,8 @@ static int load_bdf(Fontface* f, const char* path)
28 return 0;
29 }
30
31-/**
32- * @brief case insensitive string compare
33- *
34- * @param a string1 to compare
35- * @param b string2 to compare
36- *
37- * @return -1=(a<b), 0=(a==b), 1=(a>b)
38- */
39+/* case insensitive string compare
40+ -1=(a<b), 0=(a==b), 1=(a>b) */
41 static int strcicmp(char const* a, char const* b)
42 {
43 int la;
44@@ -195,13 +181,7 @@ static int strcicmp(char const* a, char const* b)
45 return tolower((unsigned char)*a) - tolower((unsigned char)*b);
46 }
47
48-/**
49- * @brief determine if the font path points to a BDF font
50- *
51- * @param path path to font
52- *
53- * @return true if path ends in .bdf
54- */
55+/* determine if font is BDF (by judging extension) */
56 static bool is_bdf(const char* path)
57 {
58 size_t pl = strlen(path);
+21,
-43
1@@ -27,8 +27,7 @@ static void sgr(Term* t);
2 static void utf8_flush(Term* t, Caret* c);
3 static void utf8_putc(Term* t, Caret* c, unsigned char ch);
4
5-/** TODO
6- */
7+/* TODO */
8 static uint32_t acs_map(unsigned char ch)
9 {
10 switch (ch) {
11@@ -46,8 +45,7 @@ static uint32_t acs_map(unsigned char ch)
12 }
13 }
14
15-/** TODO
16- */
17+/* TODO */
18 static int csi_arg(Term* t, int i, int fallback)
19 {
20 if (i > t->csi_idx)
21@@ -58,8 +56,7 @@ static int csi_arg(Term* t, int i, int fallback)
22 return t->csi_params[i];
23 }
24
25-/** TODO
26- */
27+/* TODO */
28 static void alt_screen(Term* t, Caret* c, bool on)
29 {
30 Rune* tmp;
31@@ -84,8 +81,7 @@ static void alt_screen(Term* t, Caret* c, bool on)
32 term_damage_all(t);
33 }
34
35-/** TODO
36- */
37+/* TODO */
38 static void chars_insert(Term* t, Caret* c, int n)
39 {
40 int cols = t->cols - c->x;
41@@ -100,8 +96,7 @@ static void chars_insert(Term* t, Caret* c, int n)
42 RUNE(t, x, c->y).dmg = true;
43 }
44
45-/** TODO
46- */
47+/* TODO */
48 static void chars_delete(Term* t, Caret* c, int n)
49 {
50 int cols = t->cols - c->x;
51@@ -116,8 +111,7 @@ static void chars_delete(Term* t, Caret* c, int n)
52 RUNE(t, x, c->y).dmg = true;
53 }
54
55-/** TODO
56- */
57+/* TODO */
58 static void csi_dispatch(Term* t, Caret* c, unsigned char ch)
59 {
60 switch (ch) {
61@@ -301,11 +295,7 @@ static void csi_dispatch(Term* t, Caret* c, unsigned char ch)
62 }
63 }
64
65-/**
66- * @brief erase CSI parameters
67- *
68- * @param t term instance to reset
69- */
70+/* erase CSI parameters */
71 static void csi_reset(Term* t)
72 {
73 for (int i = 0; i < CSI_PARAMS_MAX; i++)
74@@ -313,8 +303,7 @@ static void csi_reset(Term* t)
75 t->csi_idx = 0;
76 }
77
78-/** TODO
79- */
80+/* TODO */
81 static int cp_width(uint32_t cp)
82 {
83 int w = wcwidth((wchar_t)cp);
84@@ -326,8 +315,7 @@ static int cp_width(uint32_t cp)
85 return w;
86 }
87
88-/** TODO
89- */
90+/* TODO */
91 static void erase_display(Term* t, Caret* c)
92 {
93 int mode = csi_arg(t, 0, 0);
94@@ -361,8 +349,7 @@ static void erase_display(Term* t, Caret* c)
95 }
96 }
97
98-/** TODO
99- */
100+/* TODO */
101 static void erase_line(Term* t, Caret* c)
102 {
103 int mode = csi_arg(t, 0, 0);
104@@ -373,8 +360,7 @@ static void erase_line(Term* t, Caret* c)
105 reset_rune(t, x, c->y);
106 }
107
108-/** TODO
109- */
110+/* TODO */
111 static void reset_rune(Term* t, int x, int y)
112 {
113 Rune* r = &(RUNE(t, x, y));
114@@ -386,8 +372,7 @@ static void reset_rune(Term* t, int x, int y)
115 r->dmg = true;
116 }
117
118-/** TODO
119- */
120+/* TODO */
121 static void rune_prepare(Term* t, int x, int y)
122 {
123 if (x > 0 && RUNE(t, x, y).width == 0)
124@@ -396,8 +381,7 @@ static void rune_prepare(Term* t, int x, int y)
125 reset_rune(t, x + 1, y);
126 }
127
128-/** TODO
129- */
130+/* TODO */
131 static void scroll_down(Term* t, int top, int bot, int n)
132 {
133 int cols = t->cols;
134@@ -416,8 +400,7 @@ static void scroll_down(Term* t, int top, int bot, int n)
135 term_damage_all(t);
136 }
137
138-/** TODO
139- */
140+/* TODO */
141 static void scroll_up(Term* t, int top, int bot, int n)
142 {
143 int cols = t->cols;
144@@ -436,8 +419,7 @@ static void scroll_up(Term* t, int top, int bot, int n)
145 term_damage_all(t);
146 }
147
148-/** TODO
149- */
150+/* TODO */
151 static void sgr(Term* t)
152 {
153 for (int i = 0; i <= t->csi_idx; i++) {
154@@ -480,8 +462,7 @@ static void sgr(Term* t)
155 }
156 }
157
158-/** TODO
159- */
160+/* TODO */
161 static void utf8_flush(Term* t, Caret* c)
162 {
163 if (t->utf8_len == 0)
164@@ -490,8 +471,8 @@ static void utf8_flush(Term* t, Caret* c)
165 t->utf8_len = 0;
166 }
167
168-/** TODO
169- */
170+/* buffer and decode utf8 to code points, then write
171+ using current character set */
172 static void utf8_putc(Term* t, Caret* c, unsigned char ch)
173 {
174 uint_least32_t cp;
175@@ -675,12 +656,8 @@ int term_resize(Term* t, Caret* c, int cols, int rows)
176 int copyrows = orows < rows ? orows : rows;
177 int copycols = ocols < cols ? ocols : cols;
178
179- for (int y = 0; y < copyrows; y++) {
180- memcpy(
181- &nr[y * cols], &or[y * ocols],
182- sizeof(Rune) * copycols
183- );
184- }
185+ for (int y = 0; y < copyrows; y++)
186+ memcpy( &nr[y * cols], &or[y * ocols], sizeof(Rune) * copycols);
187 }
188
189 for (int y = 0; y < rows; y++) {
190@@ -845,3 +822,4 @@ bool term_write(Term* t, Caret* c, const char* s, size_t n)
191
192 return damaged;
193 }
194+
+4,
-1
1@@ -3,13 +3,15 @@
2
3 #include "utils.h"
4
5-/* TODO: stdarg */
6+/* perror `msg` and exit program with `ec`
7+ TODO? stdarg */
8 void die(int ec, const char* msg)
9 {
10 perror(msg);
11 exit(ec);
12 }
13
14+/* pack r, g, b and a into u32 */
15 uint32_t rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
16 {
17 return
18@@ -19,6 +21,7 @@ uint32_t rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
19 ((uint32_t)b);
20 }
21
22+/* malloc, die on failure */
23 void* xmalloc(size_t len)
24 {
25 void* p = malloc(len);