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  int col;
139  // 90% grey so that pure white is brighter
140  state->default_fg.red = state->default_fg.green = state->default_fg.blue = 240;
141  state->default_bg.red = state->default_bg.green = state->default_bg.blue = 0;
142
143  for(col = 0; col < 16; col++)
144    state->colors[col] = ansi_colors[col];
145}
146
147INTERNAL void vterm_state_resetpen(VTermState *state)
148{
149  state->pen.bold = 0;      setpenattr_bool(state, VTERM_ATTR_BOLD, 0);
150  state->pen.underline = 0; setpenattr_int( state, VTERM_ATTR_UNDERLINE, 0);
151  state->pen.italic = 0;    setpenattr_bool(state, VTERM_ATTR_ITALIC, 0);
152  state->pen.blink = 0;     setpenattr_bool(state, VTERM_ATTR_BLINK, 0);
153  state->pen.reverse = 0;   setpenattr_bool(state, VTERM_ATTR_REVERSE, 0);
154  state->pen.strike = 0;    setpenattr_bool(state, VTERM_ATTR_STRIKE, 0);
155  state->pen.font = 0;      setpenattr_int( state, VTERM_ATTR_FONT, 0);
156
157  state->fg_index = -1;
158  state->bg_index = -1;
159  state->pen.fg = state->default_fg;  setpenattr_col(state, VTERM_ATTR_FOREGROUND, state->default_fg);
160  state->pen.bg = state->default_bg;  setpenattr_col(state, VTERM_ATTR_BACKGROUND, state->default_bg);
161}
162
163INTERNAL void vterm_state_savepen(VTermState *state, int save)
164{
165  if(save) {
166    state->saved.pen = state->pen;
167  }
168  else {
169    state->pen = state->saved.pen;
170
171    setpenattr_bool(state, VTERM_ATTR_BOLD,       state->pen.bold);
172    setpenattr_int( state, VTERM_ATTR_UNDERLINE,  state->pen.underline);
173    setpenattr_bool(state, VTERM_ATTR_ITALIC,     state->pen.italic);
174    setpenattr_bool(state, VTERM_ATTR_BLINK,      state->pen.blink);
175    setpenattr_bool(state, VTERM_ATTR_REVERSE,    state->pen.reverse);
176    setpenattr_bool(state, VTERM_ATTR_STRIKE,     state->pen.strike);
177    setpenattr_int( state, VTERM_ATTR_FONT,       state->pen.font);
178    setpenattr_col( state, VTERM_ATTR_FOREGROUND, state->pen.fg);
179    setpenattr_col( state, VTERM_ATTR_BACKGROUND, state->pen.bg);
180  }
181}
182
183void vterm_state_get_default_colors(const VTermState *state, VTermColor *default_fg, VTermColor *default_bg)
184{
185  *default_fg = state->default_fg;
186  *default_bg = state->default_bg;
187}
188
189void vterm_state_get_palette_color(const VTermState *state, int index, VTermColor *col)
190{
191  lookup_colour_palette(state, index, col);
192}
193
194void vterm_state_set_default_colors(VTermState *state, const VTermColor *default_fg, const VTermColor *default_bg)
195{
196  state->default_fg = *default_fg;
197  state->default_bg = *default_bg;
198}
199
200void vterm_state_set_palette_color(VTermState *state, int index, const VTermColor *col)
201{
202  if(index >= 0 && index < 16)
203    state->colors[index] = *col;
204}
205
206void vterm_state_set_bold_highbright(VTermState *state, int bold_is_highbright)
207{
208  state->bold_is_highbright = bold_is_highbright;
209}
210
211INTERNAL void vterm_state_setpen(VTermState *state, const long args[], int argcount)
212{
213  // SGR - ECMA-48 8.3.117
214
215  int argi = 0;
216  int value;
217
218  while(argi < argcount) {
219    // This logic is easier to do 'done' backwards; set it true, and make it
220    // false again in the 'default' case
221    int done = 1;
222
223    long arg;
224    switch(arg = CSI_ARG(args[argi])) {
225    case CSI_ARG_MISSING:
226    case 0: // Reset
227      vterm_state_resetpen(state);
228      break;
229
230    case 1: // Bold on
231      state->pen.bold = 1;
232      setpenattr_bool(state, VTERM_ATTR_BOLD, 1);
233      if(state->fg_index > -1 && state->fg_index < 8 && state->bold_is_highbright)
234        set_pen_col_ansi(state, VTERM_ATTR_FOREGROUND, state->fg_index + (state->pen.bold ? 8 : 0));
235      break;
236
237    case 3: // Italic on
238      state->pen.italic = 1;
239      setpenattr_bool(state, VTERM_ATTR_ITALIC, 1);
240      break;
241
242    case 4: // Underline single
243      state->pen.underline = 1;
244      setpenattr_int(state, VTERM_ATTR_UNDERLINE, 1);
245      break;
246
247    case 5: // Blink
248      state->pen.blink = 1;
249      setpenattr_bool(state, VTERM_ATTR_BLINK, 1);
250      break;
251
252    case 7: // Reverse on
253      state->pen.reverse = 1;
254      setpenattr_bool(state, VTERM_ATTR_REVERSE, 1);
255      break;
256
257    case 9: // Strikethrough on
258      state->pen.strike = 1;
259      setpenattr_bool(state, VTERM_ATTR_STRIKE, 1);
260      break;
261
262    case 10: case 11: case 12: case 13: case 14:
263    case 15: case 16: case 17: case 18: case 19: // Select font
264      state->pen.font = CSI_ARG(args[argi]) - 10;
265      setpenattr_int(state, VTERM_ATTR_FONT, state->pen.font);
266      break;
267
268    case 21: // Underline double
269      state->pen.underline = 2;
270      setpenattr_int(state, VTERM_ATTR_UNDERLINE, 2);
271      break;
272
273    case 22: // Bold off
274      state->pen.bold = 0;
275      setpenattr_bool(state, VTERM_ATTR_BOLD, 0);
276      break;
277
278    case 23: // Italic and Gothic (currently unsupported) off
279      state->pen.italic = 0;
280      setpenattr_bool(state, VTERM_ATTR_ITALIC, 0);
281      break;
282
283    case 24: // Underline off
284      state->pen.underline = 0;
285      setpenattr_int(state, VTERM_ATTR_UNDERLINE, 0);
286      break;
287
288    case 25: // Blink off
289      state->pen.blink = 0;
290      setpenattr_bool(state, VTERM_ATTR_BLINK, 0);
291      break;
292
293    case 27: // Reverse off
294      state->pen.reverse = 0;
295      setpenattr_bool(state, VTERM_ATTR_REVERSE, 0);
296      break;
297
298    case 29: // Strikethrough off
299      state->pen.strike = 0;
300      setpenattr_bool(state, VTERM_ATTR_STRIKE, 0);
301      break;
302
303    case 30: case 31: case 32: case 33:
304    case 34: case 35: case 36: case 37: // Foreground colour palette
305      value = CSI_ARG(args[argi]) - 30;
306      state->fg_index = value;
307      if(state->pen.bold && state->bold_is_highbright)
308        value += 8;
309      set_pen_col_ansi(state, VTERM_ATTR_FOREGROUND, value);
310      break;
311
312    case 38: // Foreground colour alternative palette
313      state->fg_index = -1;
314      if(argcount - argi < 1)
315        return;
316      argi += 1 + lookup_colour(state, CSI_ARG(args[argi+1]), args+argi+2, argcount-argi-2, &state->pen.fg, &state->fg_index);
317      setpenattr_col(state, VTERM_ATTR_FOREGROUND, state->pen.fg);
318      break;
319
320    case 39: // Foreground colour default
321      state->fg_index = -1;
322      state->pen.fg = state->default_fg;
323      setpenattr_col(state, VTERM_ATTR_FOREGROUND, state->pen.fg);
324      break;
325
326    case 40: case 41: case 42: case 43:
327    case 44: case 45: case 46: case 47: // Background colour palette
328      value = CSI_ARG(args[argi]) - 40;
329      state->bg_index = value;
330      set_pen_col_ansi(state, VTERM_ATTR_BACKGROUND, value);
331      break;
332
333    case 48: // Background colour alternative palette
334      state->bg_index = -1;
335      if(argcount - argi < 1)
336        return;
337      argi += 1 + lookup_colour(state, CSI_ARG(args[argi+1]), args+argi+2, argcount-argi-2, &state->pen.bg, &state->bg_index);
338      setpenattr_col(state, VTERM_ATTR_BACKGROUND, state->pen.bg);
339      break;
340
341    case 49: // Default background
342      state->bg_index = -1;
343      state->pen.bg = state->default_bg;
344      setpenattr_col(state, VTERM_ATTR_BACKGROUND, state->pen.bg);
345      break;
346
347    case 90: case 91: case 92: case 93:
348    case 94: case 95: case 96: case 97: // Foreground colour high-intensity palette
349      value = CSI_ARG(args[argi]) - 90 + 8;
350      state->fg_index = value;
351      set_pen_col_ansi(state, VTERM_ATTR_FOREGROUND, value);
352      break;
353
354    case 100: case 101: case 102: case 103:
355    case 104: case 105: case 106: case 107: // Background colour high-intensity palette
356      value = CSI_ARG(args[argi]) - 100 + 8;
357      state->bg_index = value;
358      set_pen_col_ansi(state, VTERM_ATTR_BACKGROUND, value);
359      break;
360
361    default:
362      done = 0;
363      break;
364    }
365
366    if(!done)
367      fprintf(stderr, "libvterm: Unhandled CSI SGR %lu\n", arg);
368
369    while(CSI_ARG_HAS_MORE(args[argi++]));
370  }
371}
372
373INTERNAL int vterm_state_getpen(VTermState *state, long args[], int argcount)
374{
375  int argi = 0;
376
377  if(state->pen.bold)
378    args[argi++] = 1;
379
380  if(state->pen.italic)
381    args[argi++] = 3;
382
383  if(state->pen.underline == 1)
384    args[argi++] = 4;
385
386  if(state->pen.blink)
387    args[argi++] = 5;
388
389  if(state->pen.reverse)
390    args[argi++] = 7;
391
392  if(state->pen.strike)
393    args[argi++] = 9;
394
395  if(state->pen.font)
396    args[argi++] = 10 + state->pen.font;
397
398  if(state->pen.underline == 2)
399    args[argi++] = 21;
400
401  if(state->fg_index >= 0 && state->fg_index < 8)
402    args[argi++] = 30 + state->fg_index;
403  else if(state->fg_index >= 8 && state->fg_index < 16)
404    args[argi++] = 90 + state->fg_index - 8;
405  else if(state->fg_index >= 16 && state->fg_index < 256) {
406    args[argi++] = CSI_ARG_FLAG_MORE|38;
407    args[argi++] = CSI_ARG_FLAG_MORE|5;
408    args[argi++] = state->fg_index;
409  }
410
411  if(state->bg_index >= 0 && state->bg_index < 8)
412    args[argi++] = 40 + state->bg_index;
413  else if(state->bg_index >= 8 && state->bg_index < 16)
414    args[argi++] = 100 + state->bg_index - 8;
415  else if(state->bg_index >= 16 && state->bg_index < 256) {
416    args[argi++] = CSI_ARG_FLAG_MORE|48;
417    args[argi++] = CSI_ARG_FLAG_MORE|5;
418    args[argi++] = state->bg_index;
419  }
420
421  return argi;
422}
423
424int vterm_state_get_penattr(const VTermState *state, VTermAttr attr, VTermValue *val)
425{
426  switch(attr) {
427  case VTERM_ATTR_BOLD:
428    val->boolean = state->pen.bold;
429    return 1;
430
431  case VTERM_ATTR_UNDERLINE:
432    val->number = state->pen.underline;
433    return 1;
434
435  case VTERM_ATTR_ITALIC:
436    val->boolean = state->pen.italic;
437    return 1;
438
439  case VTERM_ATTR_BLINK:
440    val->boolean = state->pen.blink;
441    return 1;
442
443  case VTERM_ATTR_REVERSE:
444    val->boolean = state->pen.reverse;
445    return 1;
446
447  case VTERM_ATTR_STRIKE:
448    val->boolean = state->pen.strike;
449    return 1;
450
451  case VTERM_ATTR_FONT:
452    val->number = state->pen.font;
453    return 1;
454
455  case VTERM_ATTR_FOREGROUND:
456    val->color = state->pen.fg;
457    return 1;
458
459  case VTERM_ATTR_BACKGROUND:
460    val->color = state->pen.bg;
461    return 1;
462  }
463
464  return 0;
465}
466