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.

266 lines
6.3 KiB

  1. /* See LICENSE for license details. */
  2. /* Arbitrary sizes */
  3. #define UTF_SIZ 4
  4. /* macros */
  5. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  6. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  7. #define LEN(a) (sizeof(a) / sizeof(a)[0])
  8. #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
  9. #define DIVCEIL(n, d) (((n) + ((d) - 1)) / (d))
  10. #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
  11. #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
  12. #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || \
  13. (a).bg != (b).bg)
  14. #define IS_SET(flag) ((term.mode & (flag)) != 0)
  15. #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + \
  16. (t1.tv_nsec-t2.tv_nsec)/1E6)
  17. #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
  18. #define TRUECOLOR(r,g,b) (1 << 24 | (r) << 16 | (g) << 8 | (b))
  19. #define IS_TRUECOL(x) (1 << 24 & (x))
  20. enum glyph_attribute {
  21. ATTR_NULL = 0,
  22. ATTR_BOLD = 1 << 0,
  23. ATTR_FAINT = 1 << 1,
  24. ATTR_ITALIC = 1 << 2,
  25. ATTR_UNDERLINE = 1 << 3,
  26. ATTR_BLINK = 1 << 4,
  27. ATTR_REVERSE = 1 << 5,
  28. ATTR_INVISIBLE = 1 << 6,
  29. ATTR_STRUCK = 1 << 7,
  30. ATTR_WRAP = 1 << 8,
  31. ATTR_WIDE = 1 << 9,
  32. ATTR_WDUMMY = 1 << 10,
  33. ATTR_BOLD_FAINT = ATTR_BOLD | ATTR_FAINT,
  34. };
  35. enum term_mode {
  36. MODE_WRAP = 1 << 0,
  37. MODE_INSERT = 1 << 1,
  38. MODE_APPKEYPAD = 1 << 2,
  39. MODE_ALTSCREEN = 1 << 3,
  40. MODE_CRLF = 1 << 4,
  41. MODE_MOUSEBTN = 1 << 5,
  42. MODE_MOUSEMOTION = 1 << 6,
  43. MODE_REVERSE = 1 << 7,
  44. MODE_KBDLOCK = 1 << 8,
  45. MODE_HIDE = 1 << 9,
  46. MODE_ECHO = 1 << 10,
  47. MODE_APPCURSOR = 1 << 11,
  48. MODE_MOUSESGR = 1 << 12,
  49. MODE_8BIT = 1 << 13,
  50. MODE_BLINK = 1 << 14,
  51. MODE_FBLINK = 1 << 15,
  52. MODE_FOCUS = 1 << 16,
  53. MODE_MOUSEX10 = 1 << 17,
  54. MODE_MOUSEMANY = 1 << 18,
  55. MODE_BRCKTPASTE = 1 << 19,
  56. MODE_PRINT = 1 << 20,
  57. MODE_UTF8 = 1 << 21,
  58. MODE_SIXEL = 1 << 22,
  59. MODE_MOUSE = MODE_MOUSEBTN|MODE_MOUSEMOTION|MODE_MOUSEX10\
  60. |MODE_MOUSEMANY,
  61. };
  62. enum selection_mode {
  63. SEL_IDLE = 0,
  64. SEL_EMPTY = 1,
  65. SEL_READY = 2
  66. };
  67. enum selection_type {
  68. SEL_REGULAR = 1,
  69. SEL_RECTANGULAR = 2
  70. };
  71. enum selection_snap {
  72. SNAP_WORD = 1,
  73. SNAP_LINE = 2
  74. };
  75. typedef unsigned char uchar;
  76. typedef unsigned int uint;
  77. typedef unsigned long ulong;
  78. typedef unsigned short ushort;
  79. typedef uint_least32_t Rune;
  80. #define Glyph Glyph_
  81. typedef struct {
  82. Rune u; /* character code */
  83. ushort mode; /* attribute flags */
  84. uint32_t fg; /* foreground */
  85. uint32_t bg; /* background */
  86. } Glyph;
  87. typedef Glyph *Line;
  88. typedef struct {
  89. Glyph attr; /* current char attributes */
  90. int x;
  91. int y;
  92. char state;
  93. } TCursor;
  94. /* Internal representation of the screen */
  95. typedef struct {
  96. int row; /* nb row */
  97. int col; /* nb col */
  98. Line *line; /* screen */
  99. Line *alt; /* alternate screen */
  100. int *dirty; /* dirtyness of lines */
  101. TCursor c; /* cursor */
  102. int top; /* top scroll limit */
  103. int bot; /* bottom scroll limit */
  104. int mode; /* terminal mode flags */
  105. int esc; /* escape state flags */
  106. char trantbl[4]; /* charset table translation */
  107. int charset; /* current charset */
  108. int icharset; /* selected charset for sequence */
  109. int numlock; /* lock numbers in keyboard */
  110. int *tabs;
  111. } Term;
  112. /* Purely graphic info */
  113. typedef struct {
  114. int tw, th; /* tty width and height */
  115. int w, h; /* window width and height */
  116. int ch; /* char height */
  117. int cw; /* char width */
  118. char state; /* focus, redraw, visible */
  119. int cursor; /* cursor style */
  120. } TermWindow;
  121. typedef struct {
  122. uint b;
  123. uint mask;
  124. char *s;
  125. } MouseShortcut;
  126. typedef struct {
  127. int mode;
  128. int type;
  129. int snap;
  130. /*
  131. * Selection variables:
  132. * nb normalized coordinates of the beginning of the selection
  133. * ne normalized coordinates of the end of the selection
  134. * ob original coordinates of the beginning of the selection
  135. * oe original coordinates of the end of the selection
  136. */
  137. struct {
  138. int x, y;
  139. } nb, ne, ob, oe;
  140. char *primary, *clipboard;
  141. int alt;
  142. struct timespec tclick1;
  143. struct timespec tclick2;
  144. //Atom xtarget;
  145. } Selection;
  146. typedef union {
  147. int i;
  148. uint ui;
  149. float f;
  150. const void *v;
  151. } Arg;
  152. typedef struct {
  153. uint mod;
  154. KeySym keysym;
  155. void (*func)(const Arg *);
  156. const Arg arg;
  157. } Shortcut;
  158. typedef struct {
  159. KeySym k;
  160. uint mask;
  161. char *s;
  162. /* three valued logic variables: 0 indifferent, 1 on, -1 off */
  163. signed char appkey; /* application keypad */
  164. signed char appcursor; /* application cursor */
  165. signed char crlf; /* crlf mode */
  166. } Key;
  167. void die(const char *, ...);
  168. void redraw(void);
  169. int tattrset(int);
  170. void tnew(int, int);
  171. void tresize(int, int);
  172. void tsetdirt(int, int);
  173. void tsetdirtattr(int);
  174. void ttynew(char *, char *, char **);
  175. size_t ttyread(void);
  176. void ttyresize(int, int);
  177. void ttysend(char *, size_t);
  178. void ttywrite(const char *, size_t);
  179. void resettitle(void);
  180. void selclear(void);
  181. void selinit(void);
  182. void selnormalize(void);
  183. int selected(int, int);
  184. char *getsel(void);
  185. size_t utf8decode(const char *, Rune *, size_t);
  186. size_t utf8encode(Rune, char *);
  187. void *xmalloc(size_t);
  188. void *xrealloc(void *, size_t);
  189. char *xstrdup(char *);
  190. /* Globals */
  191. extern TermWindow win;
  192. extern Term term;
  193. extern Selection sel;
  194. extern int cmdfd;
  195. extern pid_t pid;
  196. extern int oldbutton;
  197. /* config.h globals */
  198. extern char font[];
  199. extern int borderpx;
  200. extern float cwscale;
  201. extern float chscale;
  202. extern unsigned int doubleclicktimeout;
  203. extern unsigned int tripleclicktimeout;
  204. extern int allowaltscreen;
  205. extern unsigned int xfps;
  206. extern unsigned int actionfps;
  207. extern unsigned int cursorthickness;
  208. extern int bellvolume;
  209. extern unsigned int blinktimeout;
  210. extern char termname[];
  211. extern const char *colorname[];
  212. extern size_t colornamelen;
  213. extern unsigned int defaultfg;
  214. extern unsigned int defaultbg;
  215. extern unsigned int defaultcs;
  216. extern unsigned int defaultrcs;
  217. extern unsigned int cursorshape;
  218. extern unsigned int cols;
  219. extern unsigned int rows;
  220. extern unsigned int mouseshape;
  221. extern unsigned int mousefg;
  222. extern unsigned int mousebg;
  223. extern unsigned int defaultattr;
  224. extern MouseShortcut mshortcuts[];
  225. extern size_t mshortcutslen;
  226. extern Shortcut shortcuts[];
  227. extern size_t shortcutslen;
  228. extern KeySym mappedkeys[];
  229. extern size_t mappedkeyslen;
  230. extern uint ignoremod;
  231. extern uint forceselmod;
  232. extern Key key[];
  233. extern size_t keyslen;
  234. extern uint selmasks[];
  235. extern size_t selmaskslen;
  236. extern char ascii_printable[];