My build of suckless st terminal
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
3.7 KiB

  1. /* See LICENSE for licence details. */
  2. #define _XOPEN_SOURCE
  3. #include <ctype.h>
  4. #include <fcntl.h>
  5. #include <locale.h>
  6. #include <stdarg.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/ioctl.h>
  11. #include <sys/select.h>
  12. #include <sys/stat.h>
  13. #include <sys/types.h>
  14. #include <unistd.h>
  15. #include <X11/Xlib.h>
  16. #include <X11/keysym.h>
  17. #include <X11/Xutil.h>
  18. /* special keys */
  19. #define KEYDELETE "\033[3~"
  20. #define KEYHOME "\033[1~"
  21. #define KEYEND "\033[4~"
  22. #define KEYPREV "\033[5~"
  23. #define KEYNEXT "\033[6~"
  24. #define TNAME "st"
  25. #define SHELL "/bin/bash"
  26. #define TAB 8
  27. #define FONT "fixed"
  28. #define BORDER 3
  29. #define LINESPACE 1 /* additional pixel between each line */
  30. /* Default colors */
  31. #define DefaultFG 7
  32. #define DefaultBG 0
  33. #define DefaultCS 1
  34. #define BellCol DefaultFG /* visual bell color */
  35. static char* colorname[] = {
  36. "black",
  37. "red",
  38. "green",
  39. "yellow",
  40. "blue",
  41. "magenta",
  42. "cyan",
  43. "white",
  44. };
  45. /* Arbitrary sizes */
  46. #define ESCSIZ 256
  47. #define ESCARG 16
  48. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  49. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  50. #define LEN(a) (sizeof(a) / sizeof(a[0]))
  51. #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
  52. #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
  53. #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
  54. enum { ATnone=0 , ATreverse=1 , ATunderline=2, ATbold=4 }; /* Attribute */
  55. enum { CSup, CSdown, CSright, CSleft, CShide, CSdraw, CSwrap, CSsave, CSload }; /* Cursor */
  56. enum { CRset=1 , CRupdate=2 }; /* Character state */
  57. enum { TMwrap=1 , TMinsert=2 }; /* Terminal mode */
  58. enum { SCupdate, SCredraw }; /* screen draw mode */
  59. typedef int Color;
  60. typedef struct {
  61. char c; /* character code */
  62. char mode; /* attribute flags */
  63. Color fg; /* foreground */
  64. Color bg; /* background */
  65. char state; /* state flag */
  66. } Glyph;
  67. typedef Glyph* Line;
  68. typedef struct {
  69. Glyph attr; /* current char attributes */
  70. char hidden;
  71. int x;
  72. int y;
  73. } TCursor;
  74. /* Escape sequence structs */
  75. typedef struct {
  76. char buf[ESCSIZ+1]; /* raw string */
  77. int len; /* raw string length */
  78. /* ESC <pre> [[ [<priv>] <arg> [;]] <mode>] */
  79. char pre;
  80. char priv;
  81. int arg[ESCARG+1];
  82. int narg; /* nb of args */
  83. char mode;
  84. } Escseq;
  85. /* Internal representation of the screen */
  86. typedef struct {
  87. int row; /* nb row */
  88. int col; /* nb col */
  89. Line* line; /* screen */
  90. TCursor c; /* cursor */
  91. int top; /* top scroll limit */
  92. int bot; /* bottom scroll limit */
  93. int mode; /* terminal mode */
  94. } Term;
  95. /* Purely graphic info */
  96. typedef struct {
  97. Display* dis;
  98. Window win;
  99. int scr;
  100. int w; /* window width */
  101. int h; /* window height */
  102. int ch; /* char height */
  103. int cw; /* char width */
  104. } XWindow;
  105. /* Drawing Context */
  106. typedef struct {
  107. unsigned long col[LEN(colorname)];
  108. XFontStruct* font;
  109. GC gc;
  110. } DC;
  111. void die(const char *errstr, ...);
  112. void draw(int);
  113. void execsh(void);
  114. void kpress(XKeyEvent *);
  115. void resize(XEvent *);
  116. void run(void);
  117. int escaddc(char);
  118. int escfinal(char);
  119. void escdump(void);
  120. void eschandle(void);
  121. void escparse(void);
  122. void escreset(void);
  123. void tclearregion(int, int, int, int);
  124. void tcpos(int);
  125. void tcursor(int);
  126. void tdeletechar(int);
  127. void tdeleteline(int);
  128. void tdump(void);
  129. void tinsertblank(int);
  130. void tinsertblankline(int);
  131. void tmoveto(int, int);
  132. void tnew(int, int);
  133. void tnewline(void);
  134. void tputc(char);
  135. void tputs(char*, int);
  136. void tresize(int, int);
  137. void tscroll(void);
  138. void tsetattr(int*, int);
  139. void tsetchar(char);
  140. void tsetscroll(int, int);
  141. void ttynew(void);
  142. void ttyread(void);
  143. void ttyresize(int, int);
  144. void ttywrite(char *, size_t);
  145. unsigned long xgetcol(const char *);
  146. void xclear(int, int, int, int);
  147. void xcursor(int);
  148. void xdrawc(int, int, Glyph);
  149. void xinit(void);
  150. void xscroll(void);