Simple Terminal from SuckLess
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.

178 lines
3.9 KiB

  1. /* See LICENSE for licence details. */
  2. #define _XOPEN_SOURCE
  3. #include <ctype.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <locale.h>
  7. #include <stdarg.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <signal.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/select.h>
  14. #include <sys/stat.h>
  15. #include <sys/types.h>
  16. #include <sys/wait.h>
  17. #include <unistd.h>
  18. #include <X11/Xlib.h>
  19. #include <X11/keysym.h>
  20. #include <X11/Xutil.h>
  21. /* special keys */
  22. #define KEYDELETE "\033[3~"
  23. #define KEYHOME "\033[1~"
  24. #define KEYEND "\033[4~"
  25. #define KEYPREV "\033[5~"
  26. #define KEYNEXT "\033[6~"
  27. #define TNAME "st"
  28. #define SHELL "/bin/bash"
  29. #define TAB 8
  30. #define FONT "fixed"
  31. #define BORDER 3
  32. #define LINESPACE 1 /* additional pixel between each line */
  33. /* Default colors */
  34. #define DefaultFG 7
  35. #define DefaultBG 0
  36. #define DefaultCS 1
  37. #define BellCol DefaultFG /* visual bell color */
  38. static char* colorname[] = {
  39. "black",
  40. "red",
  41. "green",
  42. "yellow",
  43. "blue",
  44. "magenta",
  45. "cyan",
  46. "white",
  47. };
  48. /* Arbitrary sizes */
  49. #define ESCSIZ 256
  50. #define ESCARG 16
  51. #define SERRNO strerror(errno)
  52. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  53. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  54. #define LEN(a) (sizeof(a) / sizeof(a[0]))
  55. #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
  56. #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
  57. #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
  58. enum { ATnone=0 , ATreverse=1 , ATunderline=2, ATbold=4 }; /* Attribute */
  59. enum { CSup, CSdown, CSright, CSleft, CShide, CSdraw, CSwrap, CSsave, CSload }; /* Cursor */
  60. enum { CRset=1 , CRupdate=2 }; /* Character state */
  61. enum { TMwrap=1 , TMinsert=2, TMaltcharset }; /* Terminal mode */
  62. enum { SCupdate, SCredraw }; /* screen draw mode */
  63. typedef int Color;
  64. typedef struct {
  65. char c; /* character code */
  66. char mode; /* attribute flags */
  67. Color fg; /* foreground */
  68. Color bg; /* background */
  69. char state; /* state flag */
  70. } Glyph;
  71. typedef Glyph* Line;
  72. typedef struct {
  73. Glyph attr; /* current char attributes */
  74. char hidden;
  75. int x;
  76. int y;
  77. } TCursor;
  78. /* Escape sequence structs */
  79. typedef struct {
  80. char buf[ESCSIZ+1]; /* raw string */
  81. int len; /* raw string length */
  82. /* ESC <pre> [[ [<priv>] <arg> [;]] <mode>] */
  83. char pre;
  84. char priv;
  85. int arg[ESCARG+1];
  86. int narg; /* nb of args */
  87. char mode;
  88. } Escseq;
  89. /* Internal representation of the screen */
  90. typedef struct {
  91. int row; /* nb row */
  92. int col; /* nb col */
  93. Line* line; /* screen */
  94. TCursor c; /* cursor */
  95. int top; /* top scroll limit */
  96. int bot; /* bottom scroll limit */
  97. int mode; /* terminal mode */
  98. } Term;
  99. /* Purely graphic info */
  100. typedef struct {
  101. Display* dis;
  102. Window win;
  103. int scr;
  104. int w; /* window width */
  105. int h; /* window height */
  106. int ch; /* char height */
  107. int cw; /* char width */
  108. } XWindow;
  109. /* Drawing Context */
  110. typedef struct {
  111. unsigned long col[LEN(colorname)];
  112. XFontStruct* font;
  113. GC gc;
  114. } DC;
  115. void die(const char *errstr, ...);
  116. void draw(int);
  117. void execsh(void);
  118. void sigchld(int);
  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);