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.

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