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.

181 lines
3.9 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 <unistd.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <sys/select.h>
  14. #include <sys/ioctl.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 "-*-terminus-medium-r-normal-*-14-*-*-*-*-*-*-*"
  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. #ifdef TRUECOLOR
  60. #error Truecolor not implemented yet
  61. typedef int Color;
  62. #else
  63. typedef char Color;
  64. #endif
  65. typedef struct {
  66. char c; /* character code */
  67. char mode; /* attribute flags */
  68. Color fg; /* foreground */
  69. Color bg; /* background */
  70. char state; /* state flag */
  71. } Glyph;
  72. typedef Glyph* Line;
  73. typedef struct {
  74. Glyph attr; /* current char attributes */
  75. char hidden;
  76. int x;
  77. int y;
  78. } TCursor;
  79. /* Escape sequence structs */
  80. typedef struct {
  81. char buf[ESCSIZ+1]; /* raw string */
  82. int len; /* raw string length */
  83. /* ESC <pre> [[ [<priv>] <arg> [;]] <mode>] */
  84. char pre;
  85. char priv;
  86. int arg[ESCARG+1];
  87. int narg; /* nb of args */
  88. char mode;
  89. } Escseq;
  90. /* Internal representation of the screen */
  91. typedef struct {
  92. int row; /* nb row */
  93. int col; /* nb col */
  94. Line* line; /* screen */
  95. TCursor c; /* cursor */
  96. int top; /* top scroll limit */
  97. int bot; /* bottom scroll limit */
  98. int mode; /* terminal mode */
  99. } Term;
  100. /* Purely graphic info */
  101. typedef struct {
  102. Display* dis;
  103. Window win;
  104. int scr;
  105. int w; /* window width */
  106. int h; /* window height */
  107. int ch; /* char height */
  108. int cw; /* char width */
  109. } XWindow;
  110. /* Drawing Context */
  111. typedef struct {
  112. unsigned long col[LEN(colorname)];
  113. XFontStruct* font;
  114. GC gc;
  115. } DC;
  116. void die(const char *errstr, ...);
  117. void draw(int);
  118. void execsh(void);
  119. void kpress(XKeyEvent *);
  120. void resize(XEvent *);
  121. void run(void);
  122. int escaddc(char);
  123. int escfinal(char);
  124. void escdump(void);
  125. void eschandle(void);
  126. void escparse(void);
  127. void escreset(void);
  128. void tclearregion(int, int, int, int);
  129. void tcpos(int);
  130. void tcursor(int);
  131. void tdeletechar(int);
  132. void tdeleteline(int);
  133. void tdump(void);
  134. void tinsertblank(int);
  135. void tinsertblankline(int);
  136. void tmoveto(int, int);
  137. void tnew(int, int);
  138. void tnewline(void);
  139. void tputc(char);
  140. void tputs(char*, int);
  141. void tresize(int, int);
  142. void tscroll(void);
  143. void tsetattr(int*, int);
  144. void tsetchar(char);
  145. void tsetscroll(int, int);
  146. void ttynew(void);
  147. void ttyread(void);
  148. void ttyresize(int, int);
  149. void ttywrite(char *, size_t);
  150. unsigned long xgetcol(const char *);
  151. void xclear(int, int, int, int);
  152. void xcursor(int);
  153. void xdrawc(int, int, Glyph);
  154. void xinit(void);
  155. void xscroll(void);