1#include "vterm_internal.h"
2
3#include <stdio.h>
4
5static const VTermColor ansi_colors[] = {
6  /* R    G    B */
7  {   0,   0,   0 }, // black
8  { 224,   0,   0 }, // red
9  {   0, 224,   0 }, // green
10  { 224, 224,   0 }, // yellow
11  {   0,   0, 224 }, // blue
12  { 224,   0, 224 }, // magenta
13  {   0, 224, 224 }, // cyan
14  { 224, 224, 224 }, // white == light grey
15
16  // high intensity
17  { 128, 128, 128 }, // black
18  { 255,  64,  64 }, // red
19  {  64, 255,  64 }, // green
20  { 255, 255,  64 }, // yellow
21  {  64,  64, 255 }, // blue
22  { 255,  64, 255 }, // magenta
23  {  64, 255, 255 }, // cyan
24  { 255, 255, 255 }, // white for real
25};
26
27static int ramp6[] = {
28  0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF,
29};
30
31static int ramp24[] = {
32  0x00, 0x0B, 0x16, 0x21, 0x2C, 0x37, 0x42, 0x4D, 0x58, 0x63, 0x6E, 0x79,
33  0x85, 0x90, 0x9B, 0xA6, 0xB1, 0xBC, 0xC7, 0xD2, 0xDD, 0xE8, 0xF3, 0xFF,
34};
35
36static void lookup_colour_ansi(const VTermState *state, long index, VTermColor *col)
37{
38  if(index >= 0 && index < 16) {
39    *col = state->colors[index];
40  }
41}
42
43static void lookup_colour_palette(const VTermState *state, long index, VTermColor *col)
44{
45  if(index >= 0 && index < 16) {
46    // Normal 8 colours or high intensity - parse as palette 0
47    lookup_colour_ansi(state, index, col);
48  }
49  else if(index >= 16 && index < 232) {
50    // 216-colour cube
51    index -= 16;
52
53    col->blue  = ramp6[index     % 6];
54    col->green = ramp6[index/6   % 6];
55    col->red   = ramp6[index/6/6 % 6];
56  }
57  else if(index >= 232 && index < 256) {
58    // 24 greyscales
59    index -= 232;
60
61    col->red   = ramp24[index];
62    col->green = ramp24[index];
63    col->blue  = ramp24[index];
64  }
65}
66
67static int lookup_colour(const VTermState *state, int palette, const long args[], int argcount, VTermColor *col, int *index)
68{
69  switch(palette) {
70  case 2: // RGB mode - 3 args contain colour values directly
71    if(argcount < 3)
72      return argcount;
73
74    col->red   = CSI_ARG(args[0]);
75    col->green = CSI_ARG(args[1]);
76    col->blue  = CSI_ARG(args[2]);
77
78    return 3;
79
80  case 5: // XTerm 256-colour mode
81    if(index)
82      *index = CSI_ARG_OR(args[0], -1);
83
84    lookup_colour_palette(state, argcount ? CSI_ARG_OR(args[0], -1) : -1, col);
85
86    return argcount ? 1 : 0;
87
88  default:
89    fprintf(stderr, "Unrecognised colour palette %d\n", palette);
90    return 0;
91  }
92}
93
94// Some conveniences
95
96static void setpenattr(VTermState *state, VTermAttr attr, VTermValueType type, VTermValue *val)
97{
98#ifdef DEBUG
99  if(type != vterm_get_attr_type(attr)) {
100    fprintf(stderr, "Cannot set attr %d as it has type %d, not type %d\n",
101        attr, vterm_get_attr_type(attr), type);
102    return;
103  }
104#endif
105  if(state->callbacks && state->callbacks->setpenattr)
106    (*state->callbacks->setpenattr)(attr, val, state->cbdata);
107}
108
109static void setpenattr_bool(VTermState *state, VTermAttr attr, int boolean)
110{
111  VTermValue val = { .boolean = boolean };
112  setpenattr(state, attr, VTERM_VALUETYPE_BOOL, &val);
113}
114
115static void setpenattr_int(VTermState *state, VTermAttr attr, int number)
116{
117  VTermValue val = { .number = number };
118  setpenattr(state, attr, VTERM_VALUETYPE_INT, &val);
119}
120
121static void setpenattr_col(VTermState *state, VTermAttr attr, VTermColor color)
122{
123  VTermValue val = { .color = color };
124  setpenattr(state, attr, VTERM_VALUETYPE_COLOR, &val);
125}
126
127static void set_pen_col_ansi(VTermState *state, VTermAttr attr, long col)
128{
129  VTermColor *colp = (attr == VTERM_ATTR_BACKGROUND) ? &state->pen.bg : &state->pen.fg;
130
131  lookup_colour_ansi(state, col, colp);
132
133  setpenattr_col(state, attr, *colp);
134}
135
136INTERNAL void vterm_state_newpen(VTermState *state)
137{
138  // 90% grey so that pure white is brighter
139  state->default_fg.red = state->default_fg.green = state->default_fg.blue = 240;
140  state->default_bg.red = state->default_bg.green = state->default_bg.blue = 0;
141
142  for(int col = 0; col < 16; col++)
143    state->colors[col] = ansi_colors[col];
144}
145
146INTERNAL void vterm_state_resetpen(VTermState *state)
147{
148  state->pen.bold = 0;      setpenattr_bool(state, VTERM_ATTR_BOLD, 0);
149  state->pen.underline = 0; setpenattr_int( state, VTERM_ATTR_UNDERLINE, 0);
150  state->pen.italic = 0;    setpenattr_bool(state, VTERM_ATTR_ITALIC, 0);
151  state->pen.blink = 0;     setpenattr_bool(state, VTERM_ATTR_BLINK, 0);
152  state->pen.reverse = 0;   setpenattr_bool(state, VTERM_ATTR_REVERSE, 0);
153  state->pen.strike = 0;    setpenattr_bool(state, VTERM_ATTR_STRIKE, 0);
154  state->pen.font = 0;      setpenattr_int( state, VTERM_ATTR_FONT, 0);
155
156  state->fg_index = -1;
157  state->bg_index = -1;
158  state->pen.fg = state->default_fg;  setpenattr_col(state, VTERM_ATTR_FOREGROUND, state->default_fg);
159  state->pen.bg = state->default_bg;  setpenattr_col(state, VTERM_ATTR_BACKGROUND, state->default_bg);
160}
161
162INTERNAL void vterm_state_savepen(VTermState *state, int save)
163{
164  if(save) {
165    state->saved.pen = state->pen;
166  }
167  else {
168    state->pen = state->saved.pen;
169
170    setpenattr_bool(state, VTERM_ATTR_BOLD,       state->pen.bold);
171    setpenattr_int( state, VTERM_ATTR_UNDERLINE,  state->pen.underline);
172    setpenattr_bool(state, VTERM_ATTR_ITALIC,     state->pen.italic);
173    setpenattr_bool(state, VTERM_ATTR_BLINK,      state->pen.blink);
174    setpenattr_bool(state, VTERM_ATTR_REVERSE,    state->pen.reverse);
175    setpenattr_bool(state, VTERM_ATTR_STRIKE,     state->pen.strike);
176    setpenattr_int( state, VTERM_ATTR_FONT,       state->pen.font);
177    setpenattr_col( state, VTERM_ATTR_FOREGROUND, state->pen.fg);
178    setpenattr_col( state, VTERM_ATTR_BACKGROUND, state->pen.bg);
179  }
180}
181
182void vterm_state_get_default_colors(const VTermState *state, VTermColor *default_fg, VTermColor *default_bg)
183{
184  *default_fg = state->default_fg;
185  *default_bg = state->default_bg;
186}
187
188void vterm_state_get_palette_color(const VTermState *state, int index, VTermColor *col)
189{
190  lookup_colour_palette(state, index, col);
191}
192
193void vterm_state_set_default_colors(VTermState *state, const VTermColor *default_fg, const VTermColor *default_bg)
194{
195  state->default_fg = *default_fg;
196  state->default_bg = *default_bg;
197}
198
199void vterm_state_set_palette_color(VTermState *state, int index, const VTermColor *col)
200{
201  if(index >= 0 && index < 16)
202    state->colors[index] = *col;
203}
204
205void vterm_state_set_bold_highbright(VTermState *state, int bold_is_highbright)
206{
207  state->bold_is_highbright = bold_is_highbright;
208}
209
210INTERNAL void vterm_state_setpen(VTermState *state, const long args[], int argcount)
211{
212  // SGR - ECMA-48 8.3.117
213
214  int argi = 0;
215  int value;
216
217  while(argi < argcount) {
218    // This logic is easier to do 'done' backwards; set it true, and make it
219    // false again in the 'default' case
220    int done = 1;
221
222    long arg;
223    switch(arg = CSI_ARG(args[argi])) {
224    case CSI_ARG_MISSING:
225    case 0: // Reset
226      vterm_state_resetpen(state);
227      break;
228
229    case 1: // Bold on
230      state->pen.bold = 1;
231      setpenattr_bool(state, VTERM_ATTR_BOLD, 1);
232      if(state->fg_index > -1 && state->fg_index < 8 && state->bold_is_highbright)
233        set_pen_col_ansi(state, VTERM_ATTR_FOREGROUND, state->fg_index + (state->pen.bold ? 8 : 0));
234      break;
235
236    case 3: // Italic on
237      state->pen.italic = 1;
238      setpenattr_bool(state, VTERM_ATTR_ITALIC, 1);
239      break;
240
241    case 4: // Underline single
242      state->pen.underline = 1;
243      setpenattr_int(state, VTERM_ATTR_UNDERLINE, 1);
244      break;
245
246    case 5: // Blink
247      state->pen.blink = 1;
248      setpenattr_bool(state, VTERM_ATTR_BLINK, 1);
249      break;
250
251    case 7: // Reverse on
252      state->pen.reverse = 1;
253      setpenattr_bool(state, VTERM_ATTR_REVERSE, 1);
254      break;
255
256    case 9: // Strikethrough on
257      state->pen.strike = 1;
258      setpenattr_bool(state, VTERM_ATTR_STRIKE, 1);
259      break;
260
261    case 10: case 11: case 12: case 13: case 14:
262    case 15: case 16: case 17: case 18: case 19: // Select font
263      state->pen.font = CSI_ARG(args[argi]) - 10;
264      setpenattr_int(state, VTERM_ATTR_FONT, state->pen.font);
265      break;
266
267    case 21: // Underline double
268      state->pen.underline = 2;
269      setpenattr_int(state, VTERM_ATTR_UNDERLINE, 2);
270      break;
271
272    case 22: // Bold off
273      state->pen.bold = 0;
274      setpenattr_bool(state, VTERM_ATTR_BOLD, 0);
275      break;
276
277    case 23: // Italic and Gothic (currently unsupported) off
278      state->pen.italic = 0;
279      setpenattr_bool(state, VTERM_ATTR_ITALIC, 0);
280      break;
281
282    case 24: // Underline off
283      state->pen.underline = 0;
284      setpenattr_int(state, VTERM_ATTR_UNDERLINE, 0);
285      break;
286
287    case 25: // Blink off
288      state->pen.blink = 0;
289      setpenattr_bool(state, VTERM_ATTR_BLINK, 0);
290      break;
291
292    case 27: // Reverse off
293      state->pen.reverse = 0;
294      setpenattr_bool(state, VTERM_ATTR_REVERSE, 0);
295      break;
296
297    case 29: // Strikethrough off
298      state->pen.strike = 0;
299      setpenattr_bool(state, VTERM_ATTR_STRIKE, 0);
300      break;
301
302    case 30: case 31: case 32: case 33:
303    case 34: case 35: case 36: case 37: // Foreground colour palette
304      value = CSI_ARG(args[argi]) - 30;
305      state->fg_index = value;
306      if(state->pen.bold && state->bold_is_highbright)
307        value += 8;
308      set_pen_col_ansi(state, VTERM_ATTR_FOREGROUND, value);
309      break;
310
311    case 38: // Foreground colour alternative palette
312      state->fg_index = -1;
313      if(argcount - argi < 1)
314        return;
315      argi += 1 + lookup_colour(state, CSI_ARG(args[argi+1]), args+argi+2, argcount-argi-2, &state->pen.fg, &state->fg_index);
316      setpenattr_col(state, VTERM_ATTR_FOREGROUND, state->pen.fg);
317      break;
318
319    case 39: // Foreground colour default
320      state->fg_index = -1;
321      state->pen.fg = state->default_fg;
322      setpenattr_col(state, VTERM_ATTR_FOREGROUND, state->pen.fg);
323      break;
324
325    case 40: case 41: case 42: case 43:
326    case 44: case 45: case 46: case 47: // Background colour palette
327      value = CSI_ARG(args[argi]) - 40;
328      state->bg_index = value;
329      set_pen_col_ansi(state, VTERM_ATTR_BACKGROUND, value);
330      break;
331
332    case 48: // Background colour alternative palette
333      state->bg_index = -1;
334      if(argcount - argi < 1)
335        return;
336      argi += 1 + lookup_colour(state, CSI_ARG(args[argi+1]), args+argi+2, argcount-argi-2, &state->pen.bg, &state->bg_index);
337      setpenattr_col(state, VTERM_ATTR_BACKGROUND, state->pen.bg);
338      break;
339
340    case 49: // Default background
341      state->bg_index = -1;
342      state->pen.bg = state->default_bg;
343      setpenattr_col(state, VTERM_ATTR_BACKGROUND, state->pen.bg);
344      break;
345
346    case 90: case 91: case 92: case 93:
347    case 94: case 95: case 96: case 97: // Foreground colour high-intensity palette
348      value = CSI_ARG(args[argi]) - 90 + 8;
349      state->fg_index = value;
350      set_pen_col_ansi(state, VTERM_ATTR_FOREGROUND, value);
351      break;
352
353    case 100: case 101: case 102: case 103:
354    case 104: case 105: case 106: case 107: // Background colour high-intensity palette
355      value = CSI_ARG(args[argi]) - 100 + 8;
356      state->bg_index = value;
357      set_pen_col_ansi(state, VTERM_ATTR_BACKGROUND, value);
358      break;
359
360    default:
361      done = 0;
362      break;
363    }
364
365    if(!done)
366      fprintf(stderr, "libvterm: Unhandled CSI SGR %lu\n", arg);
367
368    while(CSI_ARG_HAS_MORE(args[argi++]));
369  }
370}
371
372INTERNAL int vterm_state_getpen(VTermState *state, long args[], int argcount)
373{
374  int argi = 0;
375
376  if(state->pen.bold)
377    args[argi++] = 1;
378
379  if(state->pen.italic)
380    args[argi++] = 3;
381
382  if(state->pen.underline == 1)
383    args[argi++] = 4;
384
385  if(state->pen.blink)
386    args[argi++] = 5;
387
388  if(state->pen.reverse)
389    args[argi++] = 7;
390
391  if(state->pen.strike)
392    args[argi++] = 9;
393
394  if(state->pen.font)
395    args[argi++] = 10 + state->pen.font;
396
397  if(state->pen.underline == 2)
398    args[argi++] = 21;
399
400  if(state->fg_index >= 0 && state->fg_index < 8)
401    args[argi++] = 30 + state->fg_index;
402  else if(state->fg_index >= 8 && state->fg_index < 16)
403    args[argi++] = 90 + state->fg_index - 8;
404  else if(state->fg_index >= 16 && state->fg_index < 256) {
405    args[argi++] = CSI_ARG_FLAG_MORE|38;
406    args[argi++] = CSI_ARG_FLAG_MORE|5;
407    args[argi++] = state->fg_index;
408  }
409
410  if(state->bg_index >= 0 && state->bg_index < 8)
411    args[argi++] = 40 + state->bg_index;
412  else if(state->bg_index >= 8 && state->bg_index < 16)
413    args[argi++] = 100 + state->bg_index - 8;
414  else if(state->bg_index >= 16 && state->bg_index < 256) {
415    args[argi++] = CSI_ARG_FLAG_MORE|48;
416    args[argi++] = CSI_ARG_FLAG_MORE|5;
417    args[argi++] = state->bg_index;
418  }
419
420  return argi;
421}
422
423int vterm_state_get_penattr(const VTermState *state, VTermAttr attr, VTermValue *val)
424{
425  switch(attr) {
426  case VTERM_ATTR_BOLD:
427    val->boolean = state->pen.bold;
428    return 1;
429
430  case VTERM_ATTR_UNDERLINE:
431    val->number = state->pen.underline;
432    return 1;
433
434  case VTERM_ATTR_ITALIC:
435    val->boolean = state->pen.italic;
436    return 1;
437
438  case VTERM_ATTR_BLINK:
439    val->boolean = state->pen.blink;
440    return 1;
441
442  case VTERM_ATTR_REVERSE:
443    val->boolean = state->pen.reverse;
444    return 1;
445
446  case VTERM_ATTR_STRIKE:
447    val->boolean = state->pen.strike;
448    return 1;
449
450  case VTERM_ATTR_FONT:
451    val->number = state->pen.font;
452    return 1;
453
454  case VTERM_ATTR_FOREGROUND:
455    val->color = state->pen.fg;
456    return 1;
457
458  case VTERM_ATTR_BACKGROUND:
459    val->color = state->pen.bg;
460    return 1;
461  }
462
463  return 0;
464}
465