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 XftGlyphFontSpec GlyphFontSpec;
  91. typedef struct {
  92. Glyph attr; /* current char attributes */
  93. int x;
  94. int y;
  95. char state;
  96. } TCursor;
  97. /* Internal representation of the screen */
  98. typedef struct {
  99. int row; /* nb row */
  100. int col; /* nb col */
  101. Line *line; /* screen */
  102. Line *alt; /* alternate screen */
  103. int *dirty; /* dirtyness of lines */
  104. GlyphFontSpec *specbuf; /* font spec buffer used for rendering */
  105. TCursor c; /* cursor */
  106. int top; /* top scroll limit */
  107. int bot; /* bottom scroll limit */
  108. int mode; /* terminal mode flags */
  109. int esc; /* escape state flags */
  110. char trantbl[4]; /* charset table translation */
  111. int charset; /* current charset */
  112. int icharset; /* selected charset for sequence */
  113. int numlock; /* lock numbers in keyboard */
  114. int *tabs;
  115. } Term;
  116. /* Purely graphic info */
  117. typedef struct {
  118. int tw, th; /* tty width and height */
  119. int w, h; /* window width and height */
  120. int ch; /* char height */
  121. int cw; /* char width */
  122. char state; /* focus, redraw, visible */
  123. int cursor; /* cursor style */
  124. } TermWindow;
  125. typedef struct {
  126. uint b;
  127. uint mask;
  128. char *s;
  129. } MouseShortcut;
  130. typedef struct {
  131. int mode;
  132. int type;
  133. int snap;
  134. /*
  135. * Selection variables:
  136. * nb normalized coordinates of the beginning of the selection
  137. * ne normalized coordinates of the end of the selection
  138. * ob original coordinates of the beginning of the selection
  139. * oe original coordinates of the end of the selection
  140. */
  141. struct {
  142. int x, y;
  143. } nb, ne, ob, oe;
  144. char *primary, *clipboard;
  145. int alt;
  146. struct timespec tclick1;
  147. struct timespec tclick2;
  148. //Atom xtarget;
  149. } Selection;
  150. typedef union {
  151. int i;
  152. uint ui;
  153. float f;
  154. const void *v;
  155. } Arg;
  156. typedef struct {
  157. uint mod;
  158. KeySym keysym;
  159. void (*func)(const Arg *);
  160. const Arg arg;
  161. } Shortcut;
  162. void die(const char *, ...);
  163. void redraw(void);
  164. int tattrset(int);
  165. void tnew(int, int);
  166. void tsetdirt(int, int);
  167. void tsetdirtattr(int);
  168. int match(uint, uint);
  169. void ttynew(void);
  170. size_t ttyread(void);
  171. void ttyresize(void);
  172. void ttysend(char *, size_t);
  173. void ttywrite(const char *, size_t);
  174. void resettitle(void);
  175. char *kmap(KeySym, uint);
  176. void cresize(int, int);
  177. void selclear(void);
  178. void selinit(void);
  179. void selnormalize(void);
  180. int selected(int, int);
  181. char *getsel(void);
  182. int x2col(int);
  183. int y2row(int);
  184. size_t utf8decode(char *, Rune *, size_t);
  185. size_t utf8encode(Rune, char *);
  186. void *xmalloc(size_t);
  187. char *xstrdup(char *);
  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 int bellvolume;
  218. extern unsigned int blinktimeout;
  219. extern char termname[];
  220. extern const char *colorname[];
  221. extern size_t colornamelen;
  222. extern unsigned int defaultfg;
  223. extern unsigned int defaultbg;
  224. extern unsigned int defaultcs;
  225. extern unsigned int defaultrcs;
  226. extern unsigned int cursorshape;
  227. extern unsigned int cols;
  228. extern unsigned int rows;
  229. extern unsigned int mouseshape;
  230. extern unsigned int mousefg;
  231. extern unsigned int mousebg;
  232. extern unsigned int defaultattr;
  233. extern MouseShortcut mshortcuts[];
  234. extern size_t mshortcutslen;
  235. extern Shortcut shortcuts[];
  236. extern size_t shortcutslen;
  237. extern uint forceselmod;
  238. extern uint selmasks[];
  239. extern size_t selmaskslen;
  240. extern char ascii_printable[];