st.h (10671B)
1 /* See LICENSE for license details. */ 2 3 #include <stdint.h> 4 #include <time.h> 5 #include <sys/types.h> 6 #include <X11/Xatom.h> 7 #include <X11/Xlib.h> 8 #include <X11/cursorfont.h> 9 #include <X11/keysym.h> 10 #include <X11/Xft/Xft.h> 11 #include <X11/XKBlib.h> 12 #include "patches.h" 13 14 /* macros */ 15 #define MIN(a, b) ((a) < (b) ? (a) : (b)) 16 #define MAX(a, b) ((a) < (b) ? (b) : (a)) 17 #define LEN(a) (sizeof(a) / sizeof(a)[0]) 18 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b)) 19 #define DIVCEIL(n, d) (((n) + ((d) - 1)) / (d)) 20 #define DEFAULT(a, b) (a) = (a) ? (a) : (b) 21 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x) 22 #if LIGATURES_PATCH 23 #define ATTRCMP(a, b) (((a).mode & (~ATTR_WRAP) & (~ATTR_LIGA)) != ((b).mode & (~ATTR_WRAP) & (~ATTR_LIGA)) || \ 24 (a).fg != (b).fg || \ 25 (a).bg != (b).bg) 26 #else 27 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || \ 28 (a).bg != (b).bg) 29 #endif // LIGATURES_PATCH 30 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + \ 31 (t1.tv_nsec-t2.tv_nsec)/1E6) 32 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit))) 33 34 #define TRUECOLOR(r,g,b) (1 << 24 | (r) << 16 | (g) << 8 | (b)) 35 #define IS_TRUECOL(x) (1 << 24 & (x)) 36 #if SCROLLBACK_PATCH || REFLOW_PATCH 37 #define HISTSIZE 2000 38 #endif // SCROLLBACK_PATCH | REFLOW_PATCH 39 40 enum glyph_attribute { 41 ATTR_NULL = 0, 42 ATTR_SET = 1 << 0, 43 ATTR_BOLD = 1 << 1, 44 ATTR_FAINT = 1 << 2, 45 ATTR_ITALIC = 1 << 3, 46 ATTR_UNDERLINE = 1 << 4, 47 ATTR_BLINK = 1 << 5, 48 ATTR_REVERSE = 1 << 6, 49 ATTR_INVISIBLE = 1 << 7, 50 ATTR_STRUCK = 1 << 8, 51 ATTR_WRAP = 1 << 9, 52 ATTR_WIDE = 1 << 10, 53 ATTR_WDUMMY = 1 << 11, 54 #if SELECTION_COLORS_PATCH 55 ATTR_SELECTED = 1 << 12, 56 #endif // SELECTION_COLORS_PATCH | REFLOW_PATCH 57 #if BOXDRAW_PATCH 58 ATTR_BOXDRAW = 1 << 13, 59 #endif // BOXDRAW_PATCH 60 #if UNDERCURL_PATCH 61 ATTR_DIRTYUNDERLINE = 1 << 14, 62 #endif // UNDERCURL_PATCH 63 #if LIGATURES_PATCH 64 ATTR_LIGA = 1 << 15, 65 #endif // LIGATURES_PATCH 66 #if SIXEL_PATCH 67 ATTR_SIXEL = 1 << 16, 68 #endif // SIXEL_PATCH 69 #if KEYBOARDSELECT_PATCH && REFLOW_PATCH 70 ATTR_HIGHLIGHT = 1 << 17, 71 #endif // KEYBOARDSELECT_PATCH 72 ATTR_BOLD_FAINT = ATTR_BOLD | ATTR_FAINT, 73 #if OSC133_PATCH 74 ATTR_FTCS_PROMPT = 1 << 18, /* OSC 133 ; A ST */ 75 #endif // OSC133_PATCH 76 }; 77 78 #if SIXEL_PATCH 79 typedef struct _ImageList { 80 struct _ImageList *next, *prev; 81 unsigned char *pixels; 82 void *pixmap; 83 void *clipmask; 84 int width; 85 int height; 86 int x; 87 int y; 88 #if REFLOW_PATCH 89 int reflow_y; 90 #endif // REFLOW_PATCH 91 int cols; 92 int cw; 93 int ch; 94 int transparent; 95 } ImageList; 96 #endif // SIXEL_PATCH 97 98 #if WIDE_GLYPHS_PATCH 99 enum drawing_mode { 100 DRAW_NONE = 0, 101 DRAW_BG = 1 << 0, 102 DRAW_FG = 1 << 1, 103 }; 104 #endif // WIDE_GLYPHS_PATCH 105 106 /* Used to control which screen(s) keybindings and mouse shortcuts apply to. */ 107 enum screen { 108 S_PRI = -1, /* primary screen */ 109 S_ALL = 0, /* both primary and alt screen */ 110 S_ALT = 1 /* alternate screen */ 111 }; 112 113 enum selection_mode { 114 SEL_IDLE = 0, 115 SEL_EMPTY = 1, 116 SEL_READY = 2 117 }; 118 119 enum selection_type { 120 SEL_REGULAR = 1, 121 SEL_RECTANGULAR = 2 122 }; 123 124 enum selection_snap { 125 SNAP_WORD = 1, 126 SNAP_LINE = 2 127 }; 128 129 typedef unsigned char uchar; 130 typedef unsigned int uint; 131 typedef unsigned long ulong; 132 typedef unsigned short ushort; 133 134 typedef uint_least32_t Rune; 135 136 typedef XftDraw *Draw; 137 typedef XftColor Color; 138 typedef XftGlyphFontSpec GlyphFontSpec; 139 140 #define Glyph Glyph_ 141 typedef struct { 142 Rune u; /* character code */ 143 uint32_t mode; /* attribute flags */ 144 uint32_t fg; /* foreground */ 145 uint32_t bg; /* background */ 146 #if UNDERCURL_PATCH 147 int ustyle; /* underline style */ 148 int ucolor[3]; /* underline color */ 149 #endif // UNDERCURL_PATCH 150 } Glyph; 151 152 typedef Glyph *Line; 153 154 #if LIGATURES_PATCH 155 typedef struct { 156 int ox; 157 int charlen; 158 int numspecs; 159 Glyph base; 160 } GlyphFontSeq; 161 #endif // LIGATURES_PATCH 162 163 typedef struct { 164 Glyph attr; /* current char attributes */ 165 int x; 166 int y; 167 char state; 168 } TCursor; 169 170 /* Internal representation of the screen */ 171 typedef struct { 172 int row; /* nb row */ 173 int col; /* nb col */ 174 #if COLUMNS_PATCH 175 int maxcol; 176 #endif // COLUMNS_PATCH 177 Line *line; /* screen */ 178 Line *alt; /* alternate screen */ 179 #if REFLOW_PATCH 180 Line hist[HISTSIZE]; /* history buffer */ 181 int histi; /* history index */ 182 int histf; /* nb history available */ 183 int scr; /* scroll back */ 184 int wrapcwidth[2]; /* used in updating WRAPNEXT when resizing */ 185 #elif SCROLLBACK_PATCH 186 Line hist[HISTSIZE]; /* history buffer */ 187 int histi; /* history index */ 188 int histn; /* number of history entries */ 189 int scr; /* scroll back */ 190 #endif // SCROLLBACK_PATCH | REFLOW_PATCH 191 int *dirty; /* dirtyness of lines */ 192 TCursor c; /* cursor */ 193 int ocx; /* old cursor col */ 194 int ocy; /* old cursor row */ 195 int top; /* top scroll limit */ 196 int bot; /* bottom scroll limit */ 197 int mode; /* terminal mode flags */ 198 int esc; /* escape state flags */ 199 char trantbl[4]; /* charset table translation */ 200 int charset; /* current charset */ 201 int icharset; /* selected charset for sequence */ 202 int *tabs; 203 #if SIXEL_PATCH 204 ImageList *images; /* sixel images */ 205 ImageList *images_alt; /* sixel images for alternate screen */ 206 #endif // SIXEL_PATCH 207 Rune lastc; /* last printed char outside of sequence, 0 if control */ 208 #if OSC7_PATCH 209 char* cwd; /* current working directory */ 210 #endif // OSC7_PATCH 211 } Term; 212 213 typedef union { 214 int i; 215 uint ui; 216 float f; 217 const void *v; 218 const char *s; 219 } Arg; 220 221 /* Purely graphic info */ 222 typedef struct { 223 int tw, th; /* tty width and height */ 224 int w, h; /* window width and height */ 225 #if BACKGROUND_IMAGE_PATCH 226 int x, y; /* window location */ 227 #endif // BACKGROUND_IMAGE_PATCH 228 #if ANYSIZE_PATCH 229 int hborderpx, vborderpx; 230 #endif // ANYSIZE_PATCH 231 int ch; /* char height */ 232 int cw; /* char width */ 233 #if VERTCENTER_PATCH 234 int cyo; /* char y offset */ 235 #endif // VERTCENTER_PATCH 236 int mode; /* window state/mode flags */ 237 int cursor; /* cursor style */ 238 } TermWindow; 239 240 typedef struct { 241 Display *dpy; 242 Colormap cmap; 243 Window win; 244 Drawable buf; 245 GlyphFontSpec *specbuf; /* font spec buffer used for rendering */ 246 #if LIGATURES_PATCH 247 GlyphFontSeq *specseq; 248 #endif // LIGATURES_PATCH 249 Atom xembed, wmdeletewin, netwmname, netwmiconname, netwmpid; 250 #if FULLSCREEN_PATCH 251 Atom netwmstate, netwmfullscreen; 252 #endif // FULLSCREEN_PATCH 253 #if NETWMICON_PATCH || NETWMICON_LEGACY_PATCH || NETWMICON_FF_PATCH 254 Atom netwmicon; 255 #endif // NETWMICON_PATCH 256 struct { 257 XIM xim; 258 XIC xic; 259 XPoint spot; 260 XVaNestedList spotlist; 261 } ime; 262 Draw draw; 263 #if BACKGROUND_IMAGE_PATCH 264 GC bggc; /* Graphics Context for background */ 265 #endif // BACKGROUND_IMAGE_PATCH 266 Visual *vis; 267 XSetWindowAttributes attrs; 268 #if HIDECURSOR_PATCH || OPENURLONCLICK_PATCH 269 /* Here, we use the term *pointer* to differentiate the cursor 270 * one sees when hovering the mouse over the terminal from, e.g., 271 * a green rectangle where text would be entered. */ 272 Cursor vpointer, bpointer; /* visible and hidden pointers */ 273 int pointerisvisible; 274 #endif // HIDECURSOR_PATCH 275 #if OPENURLONCLICK_PATCH 276 Cursor upointer; 277 #endif // OPENURLONCLICK_PATCH 278 int scr; 279 int isfixed; /* is fixed geometry? */ 280 #if ALPHA_PATCH 281 int depth; /* bit depth */ 282 #endif // ALPHA_PATCH 283 int l, t; /* left and top offset */ 284 int gm; /* geometry mask */ 285 } XWindow; 286 287 typedef struct { 288 Atom xtarget; 289 char *primary, *clipboard; 290 struct timespec tclick1; 291 struct timespec tclick2; 292 } XSelection; 293 294 /* types used in config.h */ 295 typedef struct { 296 uint mod; 297 KeySym keysym; 298 void (*func)(const Arg *); 299 const Arg arg; 300 int screen; 301 } Shortcut; 302 303 typedef struct { 304 uint mod; 305 uint button; 306 void (*func)(const Arg *); 307 const Arg arg; 308 uint release; 309 int screen; 310 } MouseShortcut; 311 312 typedef struct { 313 KeySym k; 314 uint mask; 315 char *s; 316 /* three-valued logic variables: 0 indifferent, 1 on, -1 off */ 317 signed char appkey; /* application keypad */ 318 signed char appcursor; /* application cursor */ 319 } Key; 320 321 /* Font structure */ 322 #define Font Font_ 323 typedef struct { 324 int height; 325 int width; 326 int ascent; 327 int descent; 328 int badslant; 329 int badweight; 330 short lbearing; 331 short rbearing; 332 XftFont *match; 333 FcFontSet *set; 334 FcPattern *pattern; 335 } Font; 336 337 /* Drawing Context */ 338 typedef struct { 339 Color *col; 340 size_t collen; 341 Font font, bfont, ifont, ibfont; 342 GC gc; 343 } DC; 344 345 void die(const char *, ...); 346 void redraw(void); 347 void draw(void); 348 void drawregion(int, int, int, int); 349 void tfulldirt(void); 350 351 void printscreen(const Arg *); 352 void printsel(const Arg *); 353 void sendbreak(const Arg *); 354 void toggleprinter(const Arg *); 355 356 int tattrset(int); 357 int tisaltscr(void); 358 void tnew(int, int); 359 void tresize(int, int); 360 void tsetdirtattr(int); 361 void ttyhangup(void); 362 int ttynew(const char *, char *, const char *, char **); 363 size_t ttyread(void); 364 void ttyresize(int, int); 365 void ttywrite(const char *, size_t, int); 366 367 void resettitle(void); 368 369 void selclear(void); 370 void selinit(void); 371 void selremove(void); 372 void selstart(int, int, int); 373 void selextend(int, int, int, int); 374 int selected(int, int); 375 char *getsel(void); 376 377 size_t utf8encode(Rune, char *); 378 379 void *xmalloc(size_t); 380 void *xrealloc(void *, size_t); 381 char *xstrdup(const char *); 382 383 int xgetcolor(int x, unsigned char *r, unsigned char *g, unsigned char *b); 384 385 #if BOXDRAW_PATCH 386 int isboxdraw(Rune); 387 ushort boxdrawindex(const Glyph *); 388 #ifdef XFT_VERSION 389 /* only exposed to x.c, otherwise we'll need Xft.h for the types */ 390 void boxdraw_xinit(Display *, Colormap, XftDraw *, Visual *); 391 void drawboxes(int, int, int, int, XftColor *, XftColor *, const XftGlyphFontSpec *, int); 392 #endif // XFT_VERSION 393 #endif // BOXDRAW_PATCH 394 395 /* config.h globals */ 396 extern char *utmp; 397 extern char *scroll; 398 extern char *stty_args; 399 extern char *vtiden; 400 extern wchar_t *worddelimiters; 401 #if KEYBOARDSELECT_PATCH && REFLOW_PATCH 402 extern wchar_t *kbds_sdelim; 403 extern wchar_t *kbds_ldelim; 404 #endif // KEYBOARDSELECT_PATCH 405 extern int allowaltscreen; 406 extern int allowwindowops; 407 extern char *termname; 408 extern unsigned int tabspaces; 409 extern unsigned int defaultfg; 410 extern unsigned int defaultbg; 411 extern unsigned int defaultcs; 412 #if EXTERNALPIPE_PATCH 413 extern int extpipeactive; 414 #endif // EXTERNALPIPE_PATCH 415 416 #if BOXDRAW_PATCH 417 extern const int boxdraw, boxdraw_bold, boxdraw_braille; 418 #endif // BOXDRAW_PATCH 419 #if ALPHA_PATCH 420 extern float alpha; 421 #if ALPHA_FOCUS_HIGHLIGHT_PATCH 422 extern float alphaUnfocused; 423 #endif // ALPHA_FOCUS_HIGHLIGHT_PATCH 424 #endif // ALPHA_PATCH 425 426 extern DC dc; 427 extern XWindow xw; 428 extern XSelection xsel; 429 extern TermWindow win; 430 extern Term term;