1dae0a4e0SAdrien Destugues - PulkoMandy#include "vterm_internal.h"
2dae0a4e0SAdrien Destugues - PulkoMandy
3dae0a4e0SAdrien Destugues - PulkoMandy#include <stdio.h>
4dae0a4e0SAdrien Destugues - PulkoMandy
5dae0a4e0SAdrien Destugues - PulkoMandystatic const VTermColor ansi_colors[] = {
6dae0a4e0SAdrien Destugues - PulkoMandy  /* R    G    B */
7dae0a4e0SAdrien Destugues - PulkoMandy  {   0,   0,   0 }, // black
8dae0a4e0SAdrien Destugues - PulkoMandy  { 224,   0,   0 }, // red
9dae0a4e0SAdrien Destugues - PulkoMandy  {   0, 224,   0 }, // green
10dae0a4e0SAdrien Destugues - PulkoMandy  { 224, 224,   0 }, // yellow
11dae0a4e0SAdrien Destugues - PulkoMandy  {   0,   0, 224 }, // blue
12dae0a4e0SAdrien Destugues - PulkoMandy  { 224,   0, 224 }, // magenta
13dae0a4e0SAdrien Destugues - PulkoMandy  {   0, 224, 224 }, // cyan
14dae0a4e0SAdrien Destugues - PulkoMandy  { 224, 224, 224 }, // white == light grey
15dae0a4e0SAdrien Destugues - PulkoMandy
16dae0a4e0SAdrien Destugues - PulkoMandy  // high intensity
17dae0a4e0SAdrien Destugues - PulkoMandy  { 128, 128, 128 }, // black
18dae0a4e0SAdrien Destugues - PulkoMandy  { 255,  64,  64 }, // red
19dae0a4e0SAdrien Destugues - PulkoMandy  {  64, 255,  64 }, // green
20dae0a4e0SAdrien Destugues - PulkoMandy  { 255, 255,  64 }, // yellow
21dae0a4e0SAdrien Destugues - PulkoMandy  {  64,  64, 255 }, // blue
22dae0a4e0SAdrien Destugues - PulkoMandy  { 255,  64, 255 }, // magenta
23dae0a4e0SAdrien Destugues - PulkoMandy  {  64, 255, 255 }, // cyan
24dae0a4e0SAdrien Destugues - PulkoMandy  { 255, 255, 255 }, // white for real
25dae0a4e0SAdrien Destugues - PulkoMandy};
26dae0a4e0SAdrien Destugues - PulkoMandy
27dae0a4e0SAdrien Destugues - PulkoMandystatic int ramp6[] = {
28dae0a4e0SAdrien Destugues - PulkoMandy  0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF,
29dae0a4e0SAdrien Destugues - PulkoMandy};
30dae0a4e0SAdrien Destugues - PulkoMandy
31dae0a4e0SAdrien Destugues - PulkoMandystatic int ramp24[] = {
32dae0a4e0SAdrien Destugues - PulkoMandy  0x00, 0x0B, 0x16, 0x21, 0x2C, 0x37, 0x42, 0x4D, 0x58, 0x63, 0x6E, 0x79,
33dae0a4e0SAdrien Destugues - PulkoMandy  0x85, 0x90, 0x9B, 0xA6, 0xB1, 0xBC, 0xC7, 0xD2, 0xDD, 0xE8, 0xF3, 0xFF,
34dae0a4e0SAdrien Destugues - PulkoMandy};
35dae0a4e0SAdrien Destugues - PulkoMandy
361ec25517SAdrien Destuguesstatic void lookup_colour_ansi(const VTermState *state, long index, VTermColor *col)
37dae0a4e0SAdrien Destugues - PulkoMandy{
38dae0a4e0SAdrien Destugues - PulkoMandy  if(index >= 0 && index < 16) {
391ec25517SAdrien Destugues    *col = state->colors[index];
40dae0a4e0SAdrien Destugues - PulkoMandy  }
41dae0a4e0SAdrien Destugues - PulkoMandy}
42dae0a4e0SAdrien Destugues - PulkoMandy
431ec25517SAdrien Destuguesstatic void lookup_colour_palette(const VTermState *state, long index, VTermColor *col)
44dae0a4e0SAdrien Destugues - PulkoMandy{
451ec25517SAdrien Destugues  if(index >= 0 && index < 16) {
461ec25517SAdrien Destugues    // Normal 8 colours or high intensity - parse as palette 0
471ec25517SAdrien Destugues    lookup_colour_ansi(state, index, col);
481ec25517SAdrien Destugues  }
491ec25517SAdrien Destugues  else if(index >= 16 && index < 232) {
501ec25517SAdrien Destugues    // 216-colour cube
511ec25517SAdrien Destugues    index -= 16;
521ec25517SAdrien Destugues
531ec25517SAdrien Destugues    col->blue  = ramp6[index     % 6];
541ec25517SAdrien Destugues    col->green = ramp6[index/6   % 6];
551ec25517SAdrien Destugues    col->red   = ramp6[index/6/6 % 6];
561ec25517SAdrien Destugues  }
571ec25517SAdrien Destugues  else if(index >= 232 && index < 256) {
581ec25517SAdrien Destugues    // 24 greyscales
591ec25517SAdrien Destugues    index -= 232;
60dae0a4e0SAdrien Destugues - PulkoMandy
611ec25517SAdrien Destugues    col->red   = ramp24[index];
621ec25517SAdrien Destugues    col->green = ramp24[index];
631ec25517SAdrien Destugues    col->blue  = ramp24[index];
641ec25517SAdrien Destugues  }
651ec25517SAdrien Destugues}
661ec25517SAdrien Destugues
671ec25517SAdrien Destuguesstatic int lookup_colour(const VTermState *state, int palette, const long args[], int argcount, VTermColor *col, int *index)
681ec25517SAdrien Destugues{
69dae0a4e0SAdrien Destugues - PulkoMandy  switch(palette) {
70dae0a4e0SAdrien Destugues - PulkoMandy  case 2: // RGB mode - 3 args contain colour values directly
71dae0a4e0SAdrien Destugues - PulkoMandy    if(argcount < 3)
72dae0a4e0SAdrien Destugues - PulkoMandy      return argcount;
73dae0a4e0SAdrien Destugues - PulkoMandy
74dae0a4e0SAdrien Destugues - PulkoMandy    col->red   = CSI_ARG(args[0]);
75dae0a4e0SAdrien Destugues - PulkoMandy    col->green = CSI_ARG(args[1]);
76dae0a4e0SAdrien Destugues - PulkoMandy    col->blue  = CSI_ARG(args[2]);
77dae0a4e0SAdrien Destugues - PulkoMandy
78dae0a4e0SAdrien Destugues - PulkoMandy    return 3;
79dae0a4e0SAdrien Destugues - PulkoMandy
80dae0a4e0SAdrien Destugues - PulkoMandy  case 5: // XTerm 256-colour mode
811ec25517SAdrien Destugues    if(index)
821ec25517SAdrien Destugues      *index = CSI_ARG_OR(args[0], -1);
83dae0a4e0SAdrien Destugues - PulkoMandy
841ec25517SAdrien Destugues    lookup_colour_palette(state, argcount ? CSI_ARG_OR(args[0], -1) : -1, col);
85dae0a4e0SAdrien Destugues - PulkoMandy
86dae0a4e0SAdrien Destugues - PulkoMandy    return argcount ? 1 : 0;
87dae0a4e0SAdrien Destugues - PulkoMandy
88dae0a4e0SAdrien Destugues - PulkoMandy  default:
89dae0a4e0SAdrien Destugues - PulkoMandy    fprintf(stderr, "Unrecognised colour palette %d\n", palette);
90dae0a4e0SAdrien Destugues - PulkoMandy    return 0;
91dae0a4e0SAdrien Destugues - PulkoMandy  }
92dae0a4e0SAdrien Destugues - PulkoMandy}
93dae0a4e0SAdrien Destugues - PulkoMandy
94dae0a4e0SAdrien Destugues - PulkoMandy// Some conveniences
95dae0a4e0SAdrien Destugues - PulkoMandy
96dae0a4e0SAdrien Destugues - PulkoMandystatic void setpenattr(VTermState *state, VTermAttr attr, VTermValueType type, VTermValue *val)
97dae0a4e0SAdrien Destugues - PulkoMandy{
98dae0a4e0SAdrien Destugues - PulkoMandy#ifdef DEBUG
99dae0a4e0SAdrien Destugues - PulkoMandy  if(type != vterm_get_attr_type(attr)) {
100dae0a4e0SAdrien Destugues - PulkoMandy    fprintf(stderr, "Cannot set attr %d as it has type %d, not type %d\n",
101dae0a4e0SAdrien Destugues - PulkoMandy        attr, vterm_get_attr_type(attr), type);
102dae0a4e0SAdrien Destugues - PulkoMandy    return;
103dae0a4e0SAdrien Destugues - PulkoMandy  }
104dae0a4e0SAdrien Destugues - PulkoMandy#endif
105dae0a4e0SAdrien Destugues - PulkoMandy  if(state->callbacks && state->callbacks->setpenattr)
106dae0a4e0SAdrien Destugues - PulkoMandy    (*state->callbacks->setpenattr)(attr, val, state->cbdata);
107dae0a4e0SAdrien Destugues - PulkoMandy}
108dae0a4e0SAdrien Destugues - PulkoMandy
109dae0a4e0SAdrien Destugues - PulkoMandystatic void setpenattr_bool(VTermState *state, VTermAttr attr, int boolean)
110dae0a4e0SAdrien Destugues - PulkoMandy{
111dae0a4e0SAdrien Destugues - PulkoMandy  VTermValue val = { .boolean = boolean };
112dae0a4e0SAdrien Destugues - PulkoMandy  setpenattr(state, attr, VTERM_VALUETYPE_BOOL, &val);
113dae0a4e0SAdrien Destugues - PulkoMandy}
114dae0a4e0SAdrien Destugues - PulkoMandy
115dae0a4e0SAdrien Destugues - PulkoMandystatic void setpenattr_int(VTermState *state, VTermAttr attr, int number)
116dae0a4e0SAdrien Destugues - PulkoMandy{
117dae0a4e0SAdrien Destugues - PulkoMandy  VTermValue val = { .number = number };
118dae0a4e0SAdrien Destugues - PulkoMandy  setpenattr(state, attr, VTERM_VALUETYPE_INT, &val);
119dae0a4e0SAdrien Destugues - PulkoMandy}
120dae0a4e0SAdrien Destugues - PulkoMandy
121dae0a4e0SAdrien Destugues - PulkoMandystatic void setpenattr_col(VTermState *state, VTermAttr attr, VTermColor color)
122dae0a4e0SAdrien Destugues - PulkoMandy{
123dae0a4e0SAdrien Destugues - PulkoMandy  VTermValue val = { .color = color };
124dae0a4e0SAdrien Destugues - PulkoMandy  setpenattr(state, attr, VTERM_VALUETYPE_COLOR, &val);
125dae0a4e0SAdrien Destugues - PulkoMandy}
126dae0a4e0SAdrien Destugues - PulkoMandy
127dae0a4e0SAdrien Destugues - PulkoMandystatic void set_pen_col_ansi(VTermState *state, VTermAttr attr, long col)
128dae0a4e0SAdrien Destugues - PulkoMandy{
129dae0a4e0SAdrien Destugues - PulkoMandy  VTermColor *colp = (attr == VTERM_ATTR_BACKGROUND) ? &state->pen.bg : &state->pen.fg;
130dae0a4e0SAdrien Destugues - PulkoMandy
1311ec25517SAdrien Destugues  lookup_colour_ansi(state, col, colp);
132dae0a4e0SAdrien Destugues - PulkoMandy
133dae0a4e0SAdrien Destugues - PulkoMandy  setpenattr_col(state, attr, *colp);
134dae0a4e0SAdrien Destugues - PulkoMandy}
135dae0a4e0SAdrien Destugues - PulkoMandy
1361ec25517SAdrien DestuguesINTERNAL void vterm_state_newpen(VTermState *state)
1371ec25517SAdrien Destugues{
1386a545a8eSAdrien Destugues  int col;
1391ec25517SAdrien Destugues  // 90% grey so that pure white is brighter
1401ec25517SAdrien Destugues  state->default_fg.red = state->default_fg.green = state->default_fg.blue = 240;
1411ec25517SAdrien Destugues  state->default_bg.red = state->default_bg.green = state->default_bg.blue = 0;
1421ec25517SAdrien Destugues
1436a545a8eSAdrien Destugues  for(col = 0; col < 16; col++)
1441ec25517SAdrien Destugues    state->colors[col] = ansi_colors[col];
1451ec25517SAdrien Destugues}
1461ec25517SAdrien Destugues
1471ec25517SAdrien DestuguesINTERNAL void vterm_state_resetpen(VTermState *state)
148dae0a4e0SAdrien Destugues - PulkoMandy{
149dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.bold = 0;      setpenattr_bool(state, VTERM_ATTR_BOLD, 0);
150dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.underline = 0; setpenattr_int( state, VTERM_ATTR_UNDERLINE, 0);
151dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.italic = 0;    setpenattr_bool(state, VTERM_ATTR_ITALIC, 0);
152dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.blink = 0;     setpenattr_bool(state, VTERM_ATTR_BLINK, 0);
153dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.reverse = 0;   setpenattr_bool(state, VTERM_ATTR_REVERSE, 0);
154dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.strike = 0;    setpenattr_bool(state, VTERM_ATTR_STRIKE, 0);
155dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.font = 0;      setpenattr_int( state, VTERM_ATTR_FONT, 0);
156dae0a4e0SAdrien Destugues - PulkoMandy
1571ec25517SAdrien Destugues  state->fg_index = -1;
1581ec25517SAdrien Destugues  state->bg_index = -1;
159dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.fg = state->default_fg;  setpenattr_col(state, VTERM_ATTR_FOREGROUND, state->default_fg);
160dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.bg = state->default_bg;  setpenattr_col(state, VTERM_ATTR_BACKGROUND, state->default_bg);
161dae0a4e0SAdrien Destugues - PulkoMandy}
162dae0a4e0SAdrien Destugues - PulkoMandy
1631ec25517SAdrien DestuguesINTERNAL void vterm_state_savepen(VTermState *state, int save)
164dae0a4e0SAdrien Destugues - PulkoMandy{
165dae0a4e0SAdrien Destugues - PulkoMandy  if(save) {
166dae0a4e0SAdrien Destugues - PulkoMandy    state->saved.pen = state->pen;
167dae0a4e0SAdrien Destugues - PulkoMandy  }
168dae0a4e0SAdrien Destugues - PulkoMandy  else {
169dae0a4e0SAdrien Destugues - PulkoMandy    state->pen = state->saved.pen;
170dae0a4e0SAdrien Destugues - PulkoMandy
171dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_bool(state, VTERM_ATTR_BOLD,       state->pen.bold);
172dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_int( state, VTERM_ATTR_UNDERLINE,  state->pen.underline);
173dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_bool(state, VTERM_ATTR_ITALIC,     state->pen.italic);
174dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_bool(state, VTERM_ATTR_BLINK,      state->pen.blink);
175dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_bool(state, VTERM_ATTR_REVERSE,    state->pen.reverse);
176dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_bool(state, VTERM_ATTR_STRIKE,     state->pen.strike);
177dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_int( state, VTERM_ATTR_FONT,       state->pen.font);
178dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_col( state, VTERM_ATTR_FOREGROUND, state->pen.fg);
179dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_col( state, VTERM_ATTR_BACKGROUND, state->pen.bg);
180dae0a4e0SAdrien Destugues - PulkoMandy  }
181dae0a4e0SAdrien Destugues - PulkoMandy}
182dae0a4e0SAdrien Destugues - PulkoMandy
1831ec25517SAdrien Destuguesvoid vterm_state_get_default_colors(const VTermState *state, VTermColor *default_fg, VTermColor *default_bg)
1841ec25517SAdrien Destugues{
1851ec25517SAdrien Destugues  *default_fg = state->default_fg;
1861ec25517SAdrien Destugues  *default_bg = state->default_bg;
1871ec25517SAdrien Destugues}
1881ec25517SAdrien Destugues
1891ec25517SAdrien Destuguesvoid vterm_state_get_palette_color(const VTermState *state, int index, VTermColor *col)
1901ec25517SAdrien Destugues{
1911ec25517SAdrien Destugues  lookup_colour_palette(state, index, col);
1921ec25517SAdrien Destugues}
1931ec25517SAdrien Destugues
1941ec25517SAdrien Destuguesvoid vterm_state_set_default_colors(VTermState *state, const VTermColor *default_fg, const VTermColor *default_bg)
195dae0a4e0SAdrien Destugues - PulkoMandy{
196dae0a4e0SAdrien Destugues - PulkoMandy  state->default_fg = *default_fg;
197dae0a4e0SAdrien Destugues - PulkoMandy  state->default_bg = *default_bg;
198dae0a4e0SAdrien Destugues - PulkoMandy}
199dae0a4e0SAdrien Destugues - PulkoMandy
2001ec25517SAdrien Destuguesvoid vterm_state_set_palette_color(VTermState *state, int index, const VTermColor *col)
2011ec25517SAdrien Destugues{
2021ec25517SAdrien Destugues  if(index >= 0 && index < 16)
2031ec25517SAdrien Destugues    state->colors[index] = *col;
2041ec25517SAdrien Destugues}
2051ec25517SAdrien Destugues
206dae0a4e0SAdrien Destugues - PulkoMandyvoid vterm_state_set_bold_highbright(VTermState *state, int bold_is_highbright)
207dae0a4e0SAdrien Destugues - PulkoMandy{
208dae0a4e0SAdrien Destugues - PulkoMandy  state->bold_is_highbright = bold_is_highbright;
209dae0a4e0SAdrien Destugues - PulkoMandy}
210dae0a4e0SAdrien Destugues - PulkoMandy
2111ec25517SAdrien DestuguesINTERNAL void vterm_state_setpen(VTermState *state, const long args[], int argcount)
212dae0a4e0SAdrien Destugues - PulkoMandy{
213dae0a4e0SAdrien Destugues - PulkoMandy  // SGR - ECMA-48 8.3.117
214dae0a4e0SAdrien Destugues - PulkoMandy
215dae0a4e0SAdrien Destugues - PulkoMandy  int argi = 0;
216dae0a4e0SAdrien Destugues - PulkoMandy  int value;
217dae0a4e0SAdrien Destugues - PulkoMandy
218dae0a4e0SAdrien Destugues - PulkoMandy  while(argi < argcount) {
219dae0a4e0SAdrien Destugues - PulkoMandy    // This logic is easier to do 'done' backwards; set it true, and make it
220dae0a4e0SAdrien Destugues - PulkoMandy    // false again in the 'default' case
221dae0a4e0SAdrien Destugues - PulkoMandy    int done = 1;
222dae0a4e0SAdrien Destugues - PulkoMandy
223dae0a4e0SAdrien Destugues - PulkoMandy    long arg;
224dae0a4e0SAdrien Destugues - PulkoMandy    switch(arg = CSI_ARG(args[argi])) {
225dae0a4e0SAdrien Destugues - PulkoMandy    case CSI_ARG_MISSING:
226dae0a4e0SAdrien Destugues - PulkoMandy    case 0: // Reset
227dae0a4e0SAdrien Destugues - PulkoMandy      vterm_state_resetpen(state);
228dae0a4e0SAdrien Destugues - PulkoMandy      break;
229dae0a4e0SAdrien Destugues - PulkoMandy
230dae0a4e0SAdrien Destugues - PulkoMandy    case 1: // Bold on
231dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.bold = 1;
232dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_BOLD, 1);
2331ec25517SAdrien Destugues      if(state->fg_index > -1 && state->fg_index < 8 && state->bold_is_highbright)
2341ec25517SAdrien Destugues        set_pen_col_ansi(state, VTERM_ATTR_FOREGROUND, state->fg_index + (state->pen.bold ? 8 : 0));
235dae0a4e0SAdrien Destugues - PulkoMandy      break;
236dae0a4e0SAdrien Destugues - PulkoMandy
237dae0a4e0SAdrien Destugues - PulkoMandy    case 3: // Italic on
238dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.italic = 1;
239dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_ITALIC, 1);
240dae0a4e0SAdrien Destugues - PulkoMandy      break;
241dae0a4e0SAdrien Destugues - PulkoMandy
242dae0a4e0SAdrien Destugues - PulkoMandy    case 4: // Underline single
243dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.underline = 1;
244dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_int(state, VTERM_ATTR_UNDERLINE, 1);
245dae0a4e0SAdrien Destugues - PulkoMandy      break;
246dae0a4e0SAdrien Destugues - PulkoMandy
247dae0a4e0SAdrien Destugues - PulkoMandy    case 5: // Blink
248dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.blink = 1;
249dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_BLINK, 1);
250dae0a4e0SAdrien Destugues - PulkoMandy      break;
251dae0a4e0SAdrien Destugues - PulkoMandy
252dae0a4e0SAdrien Destugues - PulkoMandy    case 7: // Reverse on
253dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.reverse = 1;
254dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_REVERSE, 1);
255dae0a4e0SAdrien Destugues - PulkoMandy      break;
256dae0a4e0SAdrien Destugues - PulkoMandy
257dae0a4e0SAdrien Destugues - PulkoMandy    case 9: // Strikethrough on
258dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.strike = 1;
259dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_STRIKE, 1);
260dae0a4e0SAdrien Destugues - PulkoMandy      break;
261dae0a4e0SAdrien Destugues - PulkoMandy
262dae0a4e0SAdrien Destugues - PulkoMandy    case 10: case 11: case 12: case 13: case 14:
263dae0a4e0SAdrien Destugues - PulkoMandy    case 15: case 16: case 17: case 18: case 19: // Select font
264dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.font = CSI_ARG(args[argi]) - 10;
265dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_int(state, VTERM_ATTR_FONT, state->pen.font);
266dae0a4e0SAdrien Destugues - PulkoMandy      break;
267dae0a4e0SAdrien Destugues - PulkoMandy
268dae0a4e0SAdrien Destugues - PulkoMandy    case 21: // Underline double
269dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.underline = 2;
270dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_int(state, VTERM_ATTR_UNDERLINE, 2);
271dae0a4e0SAdrien Destugues - PulkoMandy      break;
272dae0a4e0SAdrien Destugues - PulkoMandy
273dae0a4e0SAdrien Destugues - PulkoMandy    case 22: // Bold off
274dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.bold = 0;
275dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_BOLD, 0);
276dae0a4e0SAdrien Destugues - PulkoMandy      break;
277dae0a4e0SAdrien Destugues - PulkoMandy
278dae0a4e0SAdrien Destugues - PulkoMandy    case 23: // Italic and Gothic (currently unsupported) off
279dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.italic = 0;
280dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_ITALIC, 0);
281dae0a4e0SAdrien Destugues - PulkoMandy      break;
282dae0a4e0SAdrien Destugues - PulkoMandy
283dae0a4e0SAdrien Destugues - PulkoMandy    case 24: // Underline off
284dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.underline = 0;
285dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_int(state, VTERM_ATTR_UNDERLINE, 0);
286dae0a4e0SAdrien Destugues - PulkoMandy      break;
287dae0a4e0SAdrien Destugues - PulkoMandy
288dae0a4e0SAdrien Destugues - PulkoMandy    case 25: // Blink off
289dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.blink = 0;
290dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_BLINK, 0);
291dae0a4e0SAdrien Destugues - PulkoMandy      break;
292dae0a4e0SAdrien Destugues - PulkoMandy
293dae0a4e0SAdrien Destugues - PulkoMandy    case 27: // Reverse off
294dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.reverse = 0;
295dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_REVERSE, 0);
296dae0a4e0SAdrien Destugues - PulkoMandy      break;
297dae0a4e0SAdrien Destugues - PulkoMandy
298dae0a4e0SAdrien Destugues - PulkoMandy    case 29: // Strikethrough off
299dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.strike = 0;
300dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_STRIKE, 0);
301dae0a4e0SAdrien Destugues - PulkoMandy      break;
302dae0a4e0SAdrien Destugues - PulkoMandy
303dae0a4e0SAdrien Destugues - PulkoMandy    case 30: case 31: case 32: case 33:
304dae0a4e0SAdrien Destugues - PulkoMandy    case 34: case 35: case 36: case 37: // Foreground colour palette
305dae0a4e0SAdrien Destugues - PulkoMandy      value = CSI_ARG(args[argi]) - 30;
3061ec25517SAdrien Destugues      state->fg_index = value;
307dae0a4e0SAdrien Destugues - PulkoMandy      if(state->pen.bold && state->bold_is_highbright)
308dae0a4e0SAdrien Destugues - PulkoMandy        value += 8;
309dae0a4e0SAdrien Destugues - PulkoMandy      set_pen_col_ansi(state, VTERM_ATTR_FOREGROUND, value);
310dae0a4e0SAdrien Destugues - PulkoMandy      break;
311dae0a4e0SAdrien Destugues - PulkoMandy
312dae0a4e0SAdrien Destugues - PulkoMandy    case 38: // Foreground colour alternative palette
3131ec25517SAdrien Destugues      state->fg_index = -1;
314dae0a4e0SAdrien Destugues - PulkoMandy      if(argcount - argi < 1)
315dae0a4e0SAdrien Destugues - PulkoMandy        return;
3161ec25517SAdrien Destugues      argi += 1 + lookup_colour(state, CSI_ARG(args[argi+1]), args+argi+2, argcount-argi-2, &state->pen.fg, &state->fg_index);
317dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_col(state, VTERM_ATTR_FOREGROUND, state->pen.fg);
318dae0a4e0SAdrien Destugues - PulkoMandy      break;
319dae0a4e0SAdrien Destugues - PulkoMandy
320dae0a4e0SAdrien Destugues - PulkoMandy    case 39: // Foreground colour default
3211ec25517SAdrien Destugues      state->fg_index = -1;
322dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.fg = state->default_fg;
323dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_col(state, VTERM_ATTR_FOREGROUND, state->pen.fg);
324dae0a4e0SAdrien Destugues - PulkoMandy      break;
325dae0a4e0SAdrien Destugues - PulkoMandy
326dae0a4e0SAdrien Destugues - PulkoMandy    case 40: case 41: case 42: case 43:
327dae0a4e0SAdrien Destugues - PulkoMandy    case 44: case 45: case 46: case 47: // Background colour palette
3281ec25517SAdrien Destugues      value = CSI_ARG(args[argi]) - 40;
3291ec25517SAdrien Destugues      state->bg_index = value;
3301ec25517SAdrien Destugues      set_pen_col_ansi(state, VTERM_ATTR_BACKGROUND, value);
331dae0a4e0SAdrien Destugues - PulkoMandy      break;
332dae0a4e0SAdrien Destugues - PulkoMandy
333dae0a4e0SAdrien Destugues - PulkoMandy    case 48: // Background colour alternative palette
3341ec25517SAdrien Destugues      state->bg_index = -1;
335dae0a4e0SAdrien Destugues - PulkoMandy      if(argcount - argi < 1)
336dae0a4e0SAdrien Destugues - PulkoMandy        return;
3371ec25517SAdrien Destugues      argi += 1 + lookup_colour(state, CSI_ARG(args[argi+1]), args+argi+2, argcount-argi-2, &state->pen.bg, &state->bg_index);
338dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_col(state, VTERM_ATTR_BACKGROUND, state->pen.bg);
339dae0a4e0SAdrien Destugues - PulkoMandy      break;
340dae0a4e0SAdrien Destugues - PulkoMandy
341dae0a4e0SAdrien Destugues - PulkoMandy    case 49: // Default background
3421ec25517SAdrien Destugues      state->bg_index = -1;
343dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.bg = state->default_bg;
344dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_col(state, VTERM_ATTR_BACKGROUND, state->pen.bg);
345dae0a4e0SAdrien Destugues - PulkoMandy      break;
346dae0a4e0SAdrien Destugues - PulkoMandy
347dae0a4e0SAdrien Destugues - PulkoMandy    case 90: case 91: case 92: case 93:
348dae0a4e0SAdrien Destugues - PulkoMandy    case 94: case 95: case 96: case 97: // Foreground colour high-intensity palette
3491ec25517SAdrien Destugues      value = CSI_ARG(args[argi]) - 90 + 8;
3501ec25517SAdrien Destugues      state->fg_index = value;
3511ec25517SAdrien Destugues      set_pen_col_ansi(state, VTERM_ATTR_FOREGROUND, value);
352dae0a4e0SAdrien Destugues - PulkoMandy      break;
353dae0a4e0SAdrien Destugues - PulkoMandy
354dae0a4e0SAdrien Destugues - PulkoMandy    case 100: case 101: case 102: case 103:
355dae0a4e0SAdrien Destugues - PulkoMandy    case 104: case 105: case 106: case 107: // Background colour high-intensity palette
3561ec25517SAdrien Destugues      value = CSI_ARG(args[argi]) - 100 + 8;
3571ec25517SAdrien Destugues      state->bg_index = value;
3581ec25517SAdrien Destugues      set_pen_col_ansi(state, VTERM_ATTR_BACKGROUND, value);
359dae0a4e0SAdrien Destugues - PulkoMandy      break;
360dae0a4e0SAdrien Destugues - PulkoMandy
361dae0a4e0SAdrien Destugues - PulkoMandy    default:
362dae0a4e0SAdrien Destugues - PulkoMandy      done = 0;
363dae0a4e0SAdrien Destugues - PulkoMandy      break;
364dae0a4e0SAdrien Destugues - PulkoMandy    }
365dae0a4e0SAdrien Destugues - PulkoMandy
366dae0a4e0SAdrien Destugues - PulkoMandy    if(!done)
367dae0a4e0SAdrien Destugues - PulkoMandy      fprintf(stderr, "libvterm: Unhandled CSI SGR %lu\n", arg);
368dae0a4e0SAdrien Destugues - PulkoMandy
369dae0a4e0SAdrien Destugues - PulkoMandy    while(CSI_ARG_HAS_MORE(args[argi++]));
370dae0a4e0SAdrien Destugues - PulkoMandy  }
371dae0a4e0SAdrien Destugues - PulkoMandy}
372dae0a4e0SAdrien Destugues - PulkoMandy
3731ec25517SAdrien DestuguesINTERNAL int vterm_state_getpen(VTermState *state, long args[], int argcount)
3741ec25517SAdrien Destugues{
3751ec25517SAdrien Destugues  int argi = 0;
3761ec25517SAdrien Destugues
3771ec25517SAdrien Destugues  if(state->pen.bold)
3781ec25517SAdrien Destugues    args[argi++] = 1;
3791ec25517SAdrien Destugues
3801ec25517SAdrien Destugues  if(state->pen.italic)
3811ec25517SAdrien Destugues    args[argi++] = 3;
3821ec25517SAdrien Destugues
3831ec25517SAdrien Destugues  if(state->pen.underline == 1)
3841ec25517SAdrien Destugues    args[argi++] = 4;
3851ec25517SAdrien Destugues
3861ec25517SAdrien Destugues  if(state->pen.blink)
3871ec25517SAdrien Destugues    args[argi++] = 5;
3881ec25517SAdrien Destugues
3891ec25517SAdrien Destugues  if(state->pen.reverse)
3901ec25517SAdrien Destugues    args[argi++] = 7;
3911ec25517SAdrien Destugues
3921ec25517SAdrien Destugues  if(state->pen.strike)
3931ec25517SAdrien Destugues    args[argi++] = 9;
3941ec25517SAdrien Destugues
3951ec25517SAdrien Destugues  if(state->pen.font)
3961ec25517SAdrien Destugues    args[argi++] = 10 + state->pen.font;
3971ec25517SAdrien Destugues
3981ec25517SAdrien Destugues  if(state->pen.underline == 2)
3991ec25517SAdrien Destugues    args[argi++] = 21;
4001ec25517SAdrien Destugues
4011ec25517SAdrien Destugues  if(state->fg_index >= 0 && state->fg_index < 8)
4021ec25517SAdrien Destugues    args[argi++] = 30 + state->fg_index;
4031ec25517SAdrien Destugues  else if(state->fg_index >= 8 && state->fg_index < 16)
4041ec25517SAdrien Destugues    args[argi++] = 90 + state->fg_index - 8;
4051ec25517SAdrien Destugues  else if(state->fg_index >= 16 && state->fg_index < 256) {
4061ec25517SAdrien Destugues    args[argi++] = CSI_ARG_FLAG_MORE|38;
4071ec25517SAdrien Destugues    args[argi++] = CSI_ARG_FLAG_MORE|5;
4081ec25517SAdrien Destugues    args[argi++] = state->fg_index;
4091ec25517SAdrien Destugues  }
4101ec25517SAdrien Destugues
4111ec25517SAdrien Destugues  if(state->bg_index >= 0 && state->bg_index < 8)
4121ec25517SAdrien Destugues    args[argi++] = 40 + state->bg_index;
4131ec25517SAdrien Destugues  else if(state->bg_index >= 8 && state->bg_index < 16)
4141ec25517SAdrien Destugues    args[argi++] = 100 + state->bg_index - 8;
4151ec25517SAdrien Destugues  else if(state->bg_index >= 16 && state->bg_index < 256) {
4161ec25517SAdrien Destugues    args[argi++] = CSI_ARG_FLAG_MORE|48;
4171ec25517SAdrien Destugues    args[argi++] = CSI_ARG_FLAG_MORE|5;
4181ec25517SAdrien Destugues    args[argi++] = state->bg_index;
4191ec25517SAdrien Destugues  }
4201ec25517SAdrien Destugues
4211ec25517SAdrien Destugues  return argi;
4221ec25517SAdrien Destugues}
4231ec25517SAdrien Destugues
4241ec25517SAdrien Destuguesint vterm_state_get_penattr(const VTermState *state, VTermAttr attr, VTermValue *val)
425dae0a4e0SAdrien Destugues - PulkoMandy{
426dae0a4e0SAdrien Destugues - PulkoMandy  switch(attr) {
427dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_BOLD:
428dae0a4e0SAdrien Destugues - PulkoMandy    val->boolean = state->pen.bold;
429dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
430dae0a4e0SAdrien Destugues - PulkoMandy
431dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_UNDERLINE:
432dae0a4e0SAdrien Destugues - PulkoMandy    val->number = state->pen.underline;
433dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
434dae0a4e0SAdrien Destugues - PulkoMandy
435dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_ITALIC:
436dae0a4e0SAdrien Destugues - PulkoMandy    val->boolean = state->pen.italic;
437dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
438dae0a4e0SAdrien Destugues - PulkoMandy
439dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_BLINK:
440dae0a4e0SAdrien Destugues - PulkoMandy    val->boolean = state->pen.blink;
441dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
442dae0a4e0SAdrien Destugues - PulkoMandy
443dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_REVERSE:
444dae0a4e0SAdrien Destugues - PulkoMandy    val->boolean = state->pen.reverse;
445dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
446dae0a4e0SAdrien Destugues - PulkoMandy
447dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_STRIKE:
448dae0a4e0SAdrien Destugues - PulkoMandy    val->boolean = state->pen.strike;
449dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
450dae0a4e0SAdrien Destugues - PulkoMandy
451dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_FONT:
452dae0a4e0SAdrien Destugues - PulkoMandy    val->number = state->pen.font;
453dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
454dae0a4e0SAdrien Destugues - PulkoMandy
455dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_FOREGROUND:
456dae0a4e0SAdrien Destugues - PulkoMandy    val->color = state->pen.fg;
457dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
458dae0a4e0SAdrien Destugues - PulkoMandy
459dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_BACKGROUND:
460dae0a4e0SAdrien Destugues - PulkoMandy    val->color = state->pen.bg;
461dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
462dae0a4e0SAdrien Destugues - PulkoMandy  }
463dae0a4e0SAdrien Destugues - PulkoMandy
464dae0a4e0SAdrien Destugues - PulkoMandy  return 0;
465dae0a4e0SAdrien Destugues - PulkoMandy}
466