st.h (6133B)
1 /* See LICENSE for license details. */ 2 3 #include <stdint.h> 4 #include <sys/types.h> 5 6 /* macros */ 7 #define MIN(a, b) ((a) < (b) ? (a) : (b)) 8 #define MAX(a, b) ((a) < (b) ? (b) : (a)) 9 #define LEN(a) (sizeof(a) / sizeof(a)[0]) 10 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b)) 11 #define DIVCEIL(n, d) (((n) + ((d) - 1)) / (d)) 12 #define DEFAULT(a, b) (a) = (a) ? (a) : (b) 13 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x) 14 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || \ 15 (a).bg != (b).bg || (a).decor != (b).decor) 16 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + \ 17 (t1.tv_nsec-t2.tv_nsec)/1E6) 18 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit))) 19 20 #define TRUECOLOR(r,g,b) (1 << 24 | (r) << 16 | (g) << 8 | (b)) 21 #define IS_TRUECOL(x) (1 << 24 & (x)) 22 23 // This decor color indicates that the fg color should be used. Note that it's 24 // not a 24-bit color because the 25-th bit is not set. 25 #define DECOR_DEFAULT_COLOR 0x0ffffff 26 27 enum glyph_attribute { 28 ATTR_NULL = 0, 29 ATTR_BOLD = 1 << 0, 30 ATTR_FAINT = 1 << 1, 31 ATTR_ITALIC = 1 << 2, 32 ATTR_UNDERLINE = 1 << 3, 33 ATTR_BLINK = 1 << 4, 34 ATTR_REVERSE = 1 << 5, 35 ATTR_INVISIBLE = 1 << 6, 36 ATTR_STRUCK = 1 << 7, 37 ATTR_WRAP = 1 << 8, 38 ATTR_WIDE = 1 << 9, 39 ATTR_WDUMMY = 1 << 10, 40 ATTR_BOLD_FAINT = ATTR_BOLD | ATTR_FAINT, 41 ATTR_IMAGE = 1 << 14, 42 }; 43 44 enum selection_mode { 45 SEL_IDLE = 0, 46 SEL_EMPTY = 1, 47 SEL_READY = 2 48 }; 49 50 enum selection_type { 51 SEL_REGULAR = 1, 52 SEL_RECTANGULAR = 2 53 }; 54 55 enum selection_snap { 56 SNAP_WORD = 1, 57 SNAP_LINE = 2 58 }; 59 60 enum underline_style { 61 UNDERLINE_STRAIGHT = 1, 62 UNDERLINE_DOUBLE = 2, 63 UNDERLINE_CURLY = 3, 64 UNDERLINE_DOTTED = 4, 65 UNDERLINE_DASHED = 5, 66 }; 67 68 typedef unsigned char uchar; 69 typedef unsigned int uint; 70 typedef unsigned long ulong; 71 typedef unsigned short ushort; 72 73 typedef uint_least32_t Rune; 74 75 #define Glyph Glyph_ 76 typedef struct { 77 Rune u; /* character code */ 78 ushort mode; /* attribute flags */ 79 uint32_t fg; /* foreground */ 80 uint32_t bg; /* background */ 81 uint32_t decor; /* decoration (like underline) */ 82 } Glyph; 83 84 typedef Glyph *Line; 85 86 typedef union { 87 int i; 88 uint ui; 89 float f; 90 const void *v; 91 const char *s; 92 } Arg; 93 94 void die(const char *, ...); 95 void redraw(void); 96 void draw(void); 97 98 void kscrolldown(const Arg *); 99 void kscrollup(const Arg *); 100 void printscreen(const Arg *); 101 void printsel(const Arg *); 102 void sendbreak(const Arg *); 103 void toggleprinter(const Arg *); 104 105 int tattrset(int); 106 void tnew(int, int); 107 void tresize(int, int); 108 void tsetdirtattr(int); 109 void ttyhangup(void); 110 int ttynew(const char *, char *, const char *, char **); 111 size_t ttyread(void); 112 void ttyresize(int, int); 113 void ttywrite(const char *, size_t, int); 114 115 void resettitle(void); 116 117 void selclear(void); 118 void selinit(void); 119 void selstart(int, int, int); 120 void selextend(int, int, int, int); 121 int selected(int, int); 122 char *getsel(void); 123 124 Glyph getglyphat(int, int); 125 126 size_t utf8encode(Rune, char *); 127 128 void *xmalloc(size_t); 129 void *xrealloc(void *, size_t); 130 char *xstrdup(const char *); 131 132 /* config.h globals */ 133 extern char *utmp; 134 extern char *scroll; 135 extern char *stty_args; 136 extern char *vtiden; 137 extern wchar_t *worddelimiters; 138 extern int allowaltscreen; 139 extern int allowwindowops; 140 extern char *termname; 141 extern unsigned int tabspaces; 142 extern unsigned int defaultfg; 143 extern unsigned int defaultbg; 144 extern unsigned int defaultcs; 145 146 // Accessors to decoration properties stored in `decor`. 147 // The 25-th bit is used to indicate if it's a 24-bit color. 148 static inline uint32_t tgetdecorcolor(Glyph *g) { return g->decor & 0x1ffffff; } 149 static inline uint32_t tgetdecorstyle(Glyph *g) { return (g->decor >> 25) & 0x7; } 150 static inline void tsetdecorcolor(Glyph *g, uint32_t color) { 151 g->decor = (g->decor & ~0x1ffffff) | (color & 0x1ffffff); 152 } 153 static inline void tsetdecorstyle(Glyph *g, uint32_t style) { 154 g->decor = (g->decor & ~(0x7 << 25)) | ((style & 0x7) << 25); 155 } 156 157 158 // Some accessors to image placeholder properties stored in `u`: 159 // - row (1-base) - 9 bits 160 // - column (1-base) - 9 bits 161 // - most significant byte of the image id plus 1 - 9 bits (0 means unspecified, 162 // don't forget to subtract 1). 163 // - the original number of diacritics (0, 1, 2, or 3) - 2 bits 164 // - whether this is a classic (1) or Unicode (0) placeholder - 1 bit 165 static inline uint32_t tgetimgrow(Glyph *g) { return g->u & 0x1ff; } 166 static inline uint32_t tgetimgcol(Glyph *g) { return (g->u >> 9) & 0x1ff; } 167 static inline uint32_t tgetimgid4thbyteplus1(Glyph *g) { return (g->u >> 18) & 0x1ff; } 168 static inline uint32_t tgetimgdiacriticcount(Glyph *g) { return (g->u >> 27) & 0x3; } 169 static inline uint32_t tgetisclassicplaceholder(Glyph *g) { return (g->u >> 29) & 0x1; } 170 static inline void tsetimgrow(Glyph *g, uint32_t row) { 171 g->u = (g->u & ~0x1ff) | (row & 0x1ff); 172 } 173 static inline void tsetimgcol(Glyph *g, uint32_t col) { 174 g->u = (g->u & ~(0x1ff << 9)) | ((col & 0x1ff) << 9); 175 } 176 static inline void tsetimg4thbyteplus1(Glyph *g, uint32_t byteplus1) { 177 g->u = (g->u & ~(0x1ff << 18)) | ((byteplus1 & 0x1ff) << 18); 178 } 179 static inline void tsetimgdiacriticcount(Glyph *g, uint32_t count) { 180 g->u = (g->u & ~(0x3 << 27)) | ((count & 0x3) << 27); 181 } 182 static inline void tsetisclassicplaceholder(Glyph *g, uint32_t isclassic) { 183 g->u = (g->u & ~(0x1 << 29)) | ((isclassic & 0x1) << 29); 184 } 185 186 /// Returns the full image id. This is a naive implementation, if the most 187 /// significant byte is not specified, it's assumed to be 0 instead of inferring 188 /// it from the cells to the left. 189 static inline uint32_t tgetimgid(Glyph *g) { 190 uint32_t msb = tgetimgid4thbyteplus1(g); 191 if (msb != 0) 192 --msb; 193 return (msb << 24) | (g->fg & 0xFFFFFF); 194 } 195 196 /// Sets the full image id. 197 static inline void tsetimgid(Glyph *g, uint32_t id) { 198 g->fg = (id & 0xFFFFFF) | (1 << 24); 199 tsetimg4thbyteplus1(g, ((id >> 24) & 0xFF) + 1); 200 } 201 202 static inline uint32_t tgetimgplacementid(Glyph *g) { 203 if (tgetdecorcolor(g) == DECOR_DEFAULT_COLOR) 204 return 0; 205 return g->decor & 0xFFFFFF; 206 } 207 208 static inline void tsetimgplacementid(Glyph *g, uint32_t id) { 209 g->decor = (id & 0xFFFFFF) | (1 << 24); 210 }