pen.c revision 1ec25517
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{
1381ec25517SAdrien Destugues  // 90% grey so that pure white is brighter
1391ec25517SAdrien Destugues  state->default_fg.red = state->default_fg.green = state->default_fg.blue = 240;
1401ec25517SAdrien Destugues  state->default_bg.red = state->default_bg.green = state->default_bg.blue = 0;
1411ec25517SAdrien Destugues
1421ec25517SAdrien Destugues  for(int col = 0; col < 16; col++)
1431ec25517SAdrien Destugues    state->colors[col] = ansi_colors[col];
1441ec25517SAdrien Destugues}
1451ec25517SAdrien Destugues
1461ec25517SAdrien DestuguesINTERNAL void vterm_state_resetpen(VTermState *state)
147dae0a4e0SAdrien Destugues - PulkoMandy{
148dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.bold = 0;      setpenattr_bool(state, VTERM_ATTR_BOLD, 0);
149dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.underline = 0; setpenattr_int( state, VTERM_ATTR_UNDERLINE, 0);
150dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.italic = 0;    setpenattr_bool(state, VTERM_ATTR_ITALIC, 0);
151dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.blink = 0;     setpenattr_bool(state, VTERM_ATTR_BLINK, 0);
152dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.reverse = 0;   setpenattr_bool(state, VTERM_ATTR_REVERSE, 0);
153dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.strike = 0;    setpenattr_bool(state, VTERM_ATTR_STRIKE, 0);
154dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.font = 0;      setpenattr_int( state, VTERM_ATTR_FONT, 0);
155dae0a4e0SAdrien Destugues - PulkoMandy
1561ec25517SAdrien Destugues  state->fg_index = -1;
1571ec25517SAdrien Destugues  state->bg_index = -1;
158dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.fg = state->default_fg;  setpenattr_col(state, VTERM_ATTR_FOREGROUND, state->default_fg);
159dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.bg = state->default_bg;  setpenattr_col(state, VTERM_ATTR_BACKGROUND, state->default_bg);
160dae0a4e0SAdrien Destugues - PulkoMandy}
161dae0a4e0SAdrien Destugues - PulkoMandy
1621ec25517SAdrien DestuguesINTERNAL void vterm_state_savepen(VTermState *state, int save)
163dae0a4e0SAdrien Destugues - PulkoMandy{
164dae0a4e0SAdrien Destugues - PulkoMandy  if(save) {
165dae0a4e0SAdrien Destugues - PulkoMandy    state->saved.pen = state->pen;
166dae0a4e0SAdrien Destugues - PulkoMandy  }
167dae0a4e0SAdrien Destugues - PulkoMandy  else {
168dae0a4e0SAdrien Destugues - PulkoMandy    state->pen = state->saved.pen;
169dae0a4e0SAdrien Destugues - PulkoMandy
170dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_bool(state, VTERM_ATTR_BOLD,       state->pen.bold);
171dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_int( state, VTERM_ATTR_UNDERLINE,  state->pen.underline);
172dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_bool(state, VTERM_ATTR_ITALIC,     state->pen.italic);
173dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_bool(state, VTERM_ATTR_BLINK,      state->pen.blink);
174dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_bool(state, VTERM_ATTR_REVERSE,    state->pen.reverse);
175dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_bool(state, VTERM_ATTR_STRIKE,     state->pen.strike);
176dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_int( state, VTERM_ATTR_FONT,       state->pen.font);
177dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_col( state, VTERM_ATTR_FOREGROUND, state->pen.fg);
178dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_col( state, VTERM_ATTR_BACKGROUND, state->pen.bg);
179dae0a4e0SAdrien Destugues - PulkoMandy  }
180dae0a4e0SAdrien Destugues - PulkoMandy}
181dae0a4e0SAdrien Destugues - PulkoMandy
1821ec25517SAdrien Destuguesvoid vterm_state_get_default_colors(const VTermState *state, VTermColor *default_fg, VTermColor *default_bg)
1831ec25517SAdrien Destugues{
1841ec25517SAdrien Destugues  *default_fg = state->default_fg;
1851ec25517SAdrien Destugues  *default_bg = state->default_bg;
1861ec25517SAdrien Destugues}
1871ec25517SAdrien Destugues
1881ec25517SAdrien Destuguesvoid vterm_state_get_palette_color(const VTermState *state, int index, VTermColor *col)
1891ec25517SAdrien Destugues{
1901ec25517SAdrien Destugues  lookup_colour_palette(state, index, col);
1911ec25517SAdrien Destugues}
1921ec25517SAdrien Destugues
1931ec25517SAdrien Destuguesvoid vterm_state_set_default_colors(VTermState *state, const VTermColor *default_fg, const VTermColor *default_bg)
194dae0a4e0SAdrien Destugues - PulkoMandy{
195dae0a4e0SAdrien Destugues - PulkoMandy  state->default_fg = *default_fg;
196dae0a4e0SAdrien Destugues - PulkoMandy  state->default_bg = *default_bg;
197dae0a4e0SAdrien Destugues - PulkoMandy}
198dae0a4e0SAdrien Destugues - PulkoMandy
1991ec25517SAdrien Destuguesvoid vterm_state_set_palette_color(VTermState *state, int index, const VTermColor *col)
2001ec25517SAdrien Destugues{
2011ec25517SAdrien Destugues  if(index >= 0 && index < 16)
2021ec25517SAdrien Destugues    state->colors[index] = *col;
2031ec25517SAdrien Destugues}
2041ec25517SAdrien Destugues
205dae0a4e0SAdrien Destugues - PulkoMandyvoid vterm_state_set_bold_highbright(VTermState *state, int bold_is_highbright)
206dae0a4e0SAdrien Destugues - PulkoMandy{
207dae0a4e0SAdrien Destugues - PulkoMandy  state->bold_is_highbright = bold_is_highbright;
208dae0a4e0SAdrien Destugues - PulkoMandy}
209dae0a4e0SAdrien Destugues - PulkoMandy
2101ec25517SAdrien DestuguesINTERNAL void vterm_state_setpen(VTermState *state, const long args[], int argcount)
211dae0a4e0SAdrien Destugues - PulkoMandy{
212dae0a4e0SAdrien Destugues - PulkoMandy  // SGR - ECMA-48 8.3.117
213dae0a4e0SAdrien Destugues - PulkoMandy
214dae0a4e0SAdrien Destugues - PulkoMandy  int argi = 0;
215dae0a4e0SAdrien Destugues - PulkoMandy  int value;
216dae0a4e0SAdrien Destugues - PulkoMandy
217dae0a4e0SAdrien Destugues - PulkoMandy  while(argi < argcount) {
218dae0a4e0SAdrien Destugues - PulkoMandy    // This logic is easier to do 'done' backwards; set it true, and make it
219dae0a4e0SAdrien Destugues - PulkoMandy    // false again in the 'default' case
220dae0a4e0SAdrien Destugues - PulkoMandy    int done = 1;
221dae0a4e0SAdrien Destugues - PulkoMandy
222dae0a4e0SAdrien Destugues - PulkoMandy    long arg;
223dae0a4e0SAdrien Destugues - PulkoMandy    switch(arg = CSI_ARG(args[argi])) {
224dae0a4e0SAdrien Destugues - PulkoMandy    case CSI_ARG_MISSING:
225dae0a4e0SAdrien Destugues - PulkoMandy    case 0: // Reset
226dae0a4e0SAdrien Destugues - PulkoMandy      vterm_state_resetpen(state);
227dae0a4e0SAdrien Destugues - PulkoMandy      break;
228dae0a4e0SAdrien Destugues - PulkoMandy
229dae0a4e0SAdrien Destugues - PulkoMandy    case 1: // Bold on
230dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.bold = 1;
231dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_BOLD, 1);
2321ec25517SAdrien Destugues      if(state->fg_index > -1 && state->fg_index < 8 && state->bold_is_highbright)
2331ec25517SAdrien Destugues        set_pen_col_ansi(state, VTERM_ATTR_FOREGROUND, state->fg_index + (state->pen.bold ? 8 : 0));
234dae0a4e0SAdrien Destugues - PulkoMandy      break;
235dae0a4e0SAdrien Destugues - PulkoMandy
236dae0a4e0SAdrien Destugues - PulkoMandy    case 3: // Italic on
237dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.italic = 1;
238dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_ITALIC, 1);
239dae0a4e0SAdrien Destugues - PulkoMandy      break;
240dae0a4e0SAdrien Destugues - PulkoMandy
241dae0a4e0SAdrien Destugues - PulkoMandy    case 4: // Underline single
242dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.underline = 1;
243dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_int(state, VTERM_ATTR_UNDERLINE, 1);
244dae0a4e0SAdrien Destugues - PulkoMandy      break;
245dae0a4e0SAdrien Destugues - PulkoMandy
246dae0a4e0SAdrien Destugues - PulkoMandy    case 5: // Blink
247dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.blink = 1;
248dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_BLINK, 1);
249dae0a4e0SAdrien Destugues - PulkoMandy      break;
250dae0a4e0SAdrien Destugues - PulkoMandy
251dae0a4e0SAdrien Destugues - PulkoMandy    case 7: // Reverse on
252dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.reverse = 1;
253dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_REVERSE, 1);
254dae0a4e0SAdrien Destugues - PulkoMandy      break;
255dae0a4e0SAdrien Destugues - PulkoMandy
256dae0a4e0SAdrien Destugues - PulkoMandy    case 9: // Strikethrough on
257dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.strike = 1;
258dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_STRIKE, 1);
259dae0a4e0SAdrien Destugues - PulkoMandy      break;
260dae0a4e0SAdrien Destugues - PulkoMandy
261dae0a4e0SAdrien Destugues - PulkoMandy    case 10: case 11: case 12: case 13: case 14:
262dae0a4e0SAdrien Destugues - PulkoMandy    case 15: case 16: case 17: case 18: case 19: // Select font
263dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.font = CSI_ARG(args[argi]) - 10;
264dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_int(state, VTERM_ATTR_FONT, state->pen.font);
265dae0a4e0SAdrien Destugues - PulkoMandy      break;
266dae0a4e0SAdrien Destugues - PulkoMandy
267dae0a4e0SAdrien Destugues - PulkoMandy    case 21: // Underline double
268dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.underline = 2;
269dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_int(state, VTERM_ATTR_UNDERLINE, 2);
270dae0a4e0SAdrien Destugues - PulkoMandy      break;
271dae0a4e0SAdrien Destugues - PulkoMandy
272dae0a4e0SAdrien Destugues - PulkoMandy    case 22: // Bold off
273dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.bold = 0;
274dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_BOLD, 0);
275dae0a4e0SAdrien Destugues - PulkoMandy      break;
276dae0a4e0SAdrien Destugues - PulkoMandy
277dae0a4e0SAdrien Destugues - PulkoMandy    case 23: // Italic and Gothic (currently unsupported) off
278dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.italic = 0;
279dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_ITALIC, 0);
280dae0a4e0SAdrien Destugues - PulkoMandy      break;
281dae0a4e0SAdrien Destugues - PulkoMandy
282dae0a4e0SAdrien Destugues - PulkoMandy    case 24: // Underline off
283dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.underline = 0;
284dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_int(state, VTERM_ATTR_UNDERLINE, 0);
285dae0a4e0SAdrien Destugues - PulkoMandy      break;
286dae0a4e0SAdrien Destugues - PulkoMandy
287dae0a4e0SAdrien Destugues - PulkoMandy    case 25: // Blink off
288dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.blink = 0;
289dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_BLINK, 0);
290dae0a4e0SAdrien Destugues - PulkoMandy      break;
291dae0a4e0SAdrien Destugues - PulkoMandy
292dae0a4e0SAdrien Destugues - PulkoMandy    case 27: // Reverse off
293dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.reverse = 0;
294dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_REVERSE, 0);
295dae0a4e0SAdrien Destugues - PulkoMandy      break;
296dae0a4e0SAdrien Destugues - PulkoMandy
297dae0a4e0SAdrien Destugues - PulkoMandy    case 29: // Strikethrough off
298dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.strike = 0;
299dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_STRIKE, 0);
300dae0a4e0SAdrien Destugues - PulkoMandy      break;
301dae0a4e0SAdrien Destugues - PulkoMandy
302dae0a4e0SAdrien Destugues - PulkoMandy    case 30: case 31: case 32: case 33:
303dae0a4e0SAdrien Destugues - PulkoMandy    case 34: case 35: case 36: case 37: // Foreground colour palette
304dae0a4e0SAdrien Destugues - PulkoMandy      value = CSI_ARG(args[argi]) - 30;
3051ec25517SAdrien Destugues      state->fg_index = value;
306dae0a4e0SAdrien Destugues - PulkoMandy      if(state->pen.bold && state->bold_is_highbright)
307dae0a4e0SAdrien Destugues - PulkoMandy        value += 8;
308dae0a4e0SAdrien Destugues - PulkoMandy      set_pen_col_ansi(state, VTERM_ATTR_FOREGROUND, value);
309dae0a4e0SAdrien Destugues - PulkoMandy      break;
310dae0a4e0SAdrien Destugues - PulkoMandy
311dae0a4e0SAdrien Destugues - PulkoMandy    case 38: // Foreground colour alternative palette
3121ec25517SAdrien Destugues      state->fg_index = -1;
313dae0a4e0SAdrien Destugues - PulkoMandy      if(argcount - argi < 1)
314dae0a4e0SAdrien Destugues - PulkoMandy        return;
3151ec25517SAdrien Destugues      argi += 1 + lookup_colour(state, CSI_ARG(args[argi+1]), args+argi+2, argcount-argi-2, &state->pen.fg, &state->fg_index);
316dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_col(state, VTERM_ATTR_FOREGROUND, state->pen.fg);
317dae0a4e0SAdrien Destugues - PulkoMandy      break;
318dae0a4e0SAdrien Destugues - PulkoMandy
319dae0a4e0SAdrien Destugues - PulkoMandy    case 39: // Foreground colour default
3201ec25517SAdrien Destugues      state->fg_index = -1;
321dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.fg = state->default_fg;
322dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_col(state, VTERM_ATTR_FOREGROUND, state->pen.fg);
323dae0a4e0SAdrien Destugues - PulkoMandy      break;
324dae0a4e0SAdrien Destugues - PulkoMandy
325dae0a4e0SAdrien Destugues - PulkoMandy    case 40: case 41: case 42: case 43:
326dae0a4e0SAdrien Destugues - PulkoMandy    case 44: case 45: case 46: case 47: // Background colour palette
3271ec25517SAdrien Destugues      value = CSI_ARG(args[argi]) - 40;
3281ec25517SAdrien Destugues      state->bg_index = value;
3291ec25517SAdrien Destugues      set_pen_col_ansi(state, VTERM_ATTR_BACKGROUND, value);
330dae0a4e0SAdrien Destugues - PulkoMandy      break;
331dae0a4e0SAdrien Destugues - PulkoMandy
332dae0a4e0SAdrien Destugues - PulkoMandy    case 48: // Background colour alternative palette
3331ec25517SAdrien Destugues      state->bg_index = -1;
334dae0a4e0SAdrien Destugues - PulkoMandy      if(argcount - argi < 1)
335dae0a4e0SAdrien Destugues - PulkoMandy        return;
3361ec25517SAdrien Destugues      argi += 1 + lookup_colour(state, CSI_ARG(args[argi+1]), args+argi+2, argcount-argi-2, &state->pen.bg, &state->bg_index);
337dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_col(state, VTERM_ATTR_BACKGROUND, state->pen.bg);
338dae0a4e0SAdrien Destugues - PulkoMandy      break;
339dae0a4e0SAdrien Destugues - PulkoMandy
340dae0a4e0SAdrien Destugues - PulkoMandy    case 49: // Default background
3411ec25517SAdrien Destugues      state->bg_index = -1;
342dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.bg = state->default_bg;
343dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_col(state, VTERM_ATTR_BACKGROUND, state->pen.bg);
344dae0a4e0SAdrien Destugues - PulkoMandy      break;
345dae0a4e0SAdrien Destugues - PulkoMandy
346dae0a4e0SAdrien Destugues - PulkoMandy    case 90: case 91: case 92: case 93:
347dae0a4e0SAdrien Destugues - PulkoMandy    case 94: case 95: case 96: case 97: // Foreground colour high-intensity palette
3481ec25517SAdrien Destugues      value = CSI_ARG(args[argi]) - 90 + 8;
3491ec25517SAdrien Destugues      state->fg_index = value;
3501ec25517SAdrien Destugues      set_pen_col_ansi(state, VTERM_ATTR_FOREGROUND, value);
351dae0a4e0SAdrien Destugues - PulkoMandy      break;
352dae0a4e0SAdrien Destugues - PulkoMandy
353dae0a4e0SAdrien Destugues - PulkoMandy    case 100: case 101: case 102: case 103:
354dae0a4e0SAdrien Destugues - PulkoMandy    case 104: case 105: case 106: case 107: // Background colour high-intensity palette
3551ec25517SAdrien Destugues      value = CSI_ARG(args[argi]) - 100 + 8;
3561ec25517SAdrien Destugues      state->bg_index = value;
3571ec25517SAdrien Destugues      set_pen_col_ansi(state, VTERM_ATTR_BACKGROUND, value);
358dae0a4e0SAdrien Destugues - PulkoMandy      break;
359dae0a4e0SAdrien Destugues - PulkoMandy
360dae0a4e0SAdrien Destugues - PulkoMandy    default:
361dae0a4e0SAdrien Destugues - PulkoMandy      done = 0;
362dae0a4e0SAdrien Destugues - PulkoMandy      break;
363dae0a4e0SAdrien Destugues - PulkoMandy    }
364dae0a4e0SAdrien Destugues - PulkoMandy
365dae0a4e0SAdrien Destugues - PulkoMandy    if(!done)
366dae0a4e0SAdrien Destugues - PulkoMandy      fprintf(stderr, "libvterm: Unhandled CSI SGR %lu\n", arg);
367dae0a4e0SAdrien Destugues - PulkoMandy
368dae0a4e0SAdrien Destugues - PulkoMandy    while(CSI_ARG_HAS_MORE(args[argi++]));
369dae0a4e0SAdrien Destugues - PulkoMandy  }
370dae0a4e0SAdrien Destugues - PulkoMandy}
371dae0a4e0SAdrien Destugues - PulkoMandy
3721ec25517SAdrien DestuguesINTERNAL int vterm_state_getpen(VTermState *state, long args[], int argcount)
3731ec25517SAdrien Destugues{
3741ec25517SAdrien Destugues  int argi = 0;
3751ec25517SAdrien Destugues
3761ec25517SAdrien Destugues  if(state->pen.bold)
3771ec25517SAdrien Destugues    args[argi++] = 1;
3781ec25517SAdrien Destugues
3791ec25517SAdrien Destugues  if(state->pen.italic)
3801ec25517SAdrien Destugues    args[argi++] = 3;
3811ec25517SAdrien Destugues
3821ec25517SAdrien Destugues  if(state->pen.underline == 1)
3831ec25517SAdrien Destugues    args[argi++] = 4;
3841ec25517SAdrien Destugues
3851ec25517SAdrien Destugues  if(state->pen.blink)
3861ec25517SAdrien Destugues    args[argi++] = 5;
3871ec25517SAdrien Destugues
3881ec25517SAdrien Destugues  if(state->pen.reverse)
3891ec25517SAdrien Destugues    args[argi++] = 7;
3901ec25517SAdrien Destugues
3911ec25517SAdrien Destugues  if(state->pen.strike)
3921ec25517SAdrien Destugues    args[argi++] = 9;
3931ec25517SAdrien Destugues
3941ec25517SAdrien Destugues  if(state->pen.font)
3951ec25517SAdrien Destugues    args[argi++] = 10 + state->pen.font;
3961ec25517SAdrien Destugues
3971ec25517SAdrien Destugues  if(state->pen.underline == 2)
3981ec25517SAdrien Destugues    args[argi++] = 21;
3991ec25517SAdrien Destugues
4001ec25517SAdrien Destugues  if(state->fg_index >= 0 && state->fg_index < 8)
4011ec25517SAdrien Destugues    args[argi++] = 30 + state->fg_index;
4021ec25517SAdrien Destugues  else if(state->fg_index >= 8 && state->fg_index < 16)
4031ec25517SAdrien Destugues    args[argi++] = 90 + state->fg_index - 8;
4041ec25517SAdrien Destugues  else if(state->fg_index >= 16 && state->fg_index < 256) {
4051ec25517SAdrien Destugues    args[argi++] = CSI_ARG_FLAG_MORE|38;
4061ec25517SAdrien Destugues    args[argi++] = CSI_ARG_FLAG_MORE|5;
4071ec25517SAdrien Destugues    args[argi++] = state->fg_index;
4081ec25517SAdrien Destugues  }
4091ec25517SAdrien Destugues
4101ec25517SAdrien Destugues  if(state->bg_index >= 0 && state->bg_index < 8)
4111ec25517SAdrien Destugues    args[argi++] = 40 + state->bg_index;
4121ec25517SAdrien Destugues  else if(state->bg_index >= 8 && state->bg_index < 16)
4131ec25517SAdrien Destugues    args[argi++] = 100 + state->bg_index - 8;
4141ec25517SAdrien Destugues  else if(state->bg_index >= 16 && state->bg_index < 256) {
4151ec25517SAdrien Destugues    args[argi++] = CSI_ARG_FLAG_MORE|48;
4161ec25517SAdrien Destugues    args[argi++] = CSI_ARG_FLAG_MORE|5;
4171ec25517SAdrien Destugues    args[argi++] = state->bg_index;
4181ec25517SAdrien Destugues  }
4191ec25517SAdrien Destugues
4201ec25517SAdrien Destugues  return argi;
4211ec25517SAdrien Destugues}
4221ec25517SAdrien Destugues
4231ec25517SAdrien Destuguesint vterm_state_get_penattr(const VTermState *state, VTermAttr attr, VTermValue *val)
424dae0a4e0SAdrien Destugues - PulkoMandy{
425dae0a4e0SAdrien Destugues - PulkoMandy  switch(attr) {
426dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_BOLD:
427dae0a4e0SAdrien Destugues - PulkoMandy    val->boolean = state->pen.bold;
428dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
429dae0a4e0SAdrien Destugues - PulkoMandy
430dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_UNDERLINE:
431dae0a4e0SAdrien Destugues - PulkoMandy    val->number = state->pen.underline;
432dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
433dae0a4e0SAdrien Destugues - PulkoMandy
434dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_ITALIC:
435dae0a4e0SAdrien Destugues - PulkoMandy    val->boolean = state->pen.italic;
436dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
437dae0a4e0SAdrien Destugues - PulkoMandy
438dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_BLINK:
439dae0a4e0SAdrien Destugues - PulkoMandy    val->boolean = state->pen.blink;
440dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
441dae0a4e0SAdrien Destugues - PulkoMandy
442dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_REVERSE:
443dae0a4e0SAdrien Destugues - PulkoMandy    val->boolean = state->pen.reverse;
444dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
445dae0a4e0SAdrien Destugues - PulkoMandy
446dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_STRIKE:
447dae0a4e0SAdrien Destugues - PulkoMandy    val->boolean = state->pen.strike;
448dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
449dae0a4e0SAdrien Destugues - PulkoMandy
450dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_FONT:
451dae0a4e0SAdrien Destugues - PulkoMandy    val->number = state->pen.font;
452dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
453dae0a4e0SAdrien Destugues - PulkoMandy
454dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_FOREGROUND:
455dae0a4e0SAdrien Destugues - PulkoMandy    val->color = state->pen.fg;
456dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
457dae0a4e0SAdrien Destugues - PulkoMandy
458dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_BACKGROUND:
459dae0a4e0SAdrien Destugues - PulkoMandy    val->color = state->pen.bg;
460dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
461dae0a4e0SAdrien Destugues - PulkoMandy  }
462dae0a4e0SAdrien Destugues - PulkoMandy
463dae0a4e0SAdrien Destugues - PulkoMandy  return 0;
464dae0a4e0SAdrien Destugues - PulkoMandy}
465