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