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.

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