pen.c revision dae0a4e0
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
36dae0a4e0SAdrien Destugues - PulkoMandystatic void lookup_colour_ansi(long index, char is_bg, VTermColor *col)
37dae0a4e0SAdrien Destugues - PulkoMandy{
38dae0a4e0SAdrien Destugues - PulkoMandy  if(index >= 0 && index < 16) {
39dae0a4e0SAdrien Destugues - PulkoMandy    *col = ansi_colors[index];
40dae0a4e0SAdrien Destugues - PulkoMandy  }
41dae0a4e0SAdrien Destugues - PulkoMandy}
42dae0a4e0SAdrien Destugues - PulkoMandy
43dae0a4e0SAdrien Destugues - PulkoMandystatic int lookup_colour(int palette, const long args[], int argcount, char is_bg, VTermColor *col)
44dae0a4e0SAdrien Destugues - PulkoMandy{
45dae0a4e0SAdrien Destugues - PulkoMandy  long index;
46dae0a4e0SAdrien Destugues - PulkoMandy
47dae0a4e0SAdrien Destugues - PulkoMandy  switch(palette) {
48dae0a4e0SAdrien Destugues - PulkoMandy  case 2: // RGB mode - 3 args contain colour values directly
49dae0a4e0SAdrien Destugues - PulkoMandy    if(argcount < 3)
50dae0a4e0SAdrien Destugues - PulkoMandy      return argcount;
51dae0a4e0SAdrien Destugues - PulkoMandy
52dae0a4e0SAdrien Destugues - PulkoMandy    col->red   = CSI_ARG(args[0]);
53dae0a4e0SAdrien Destugues - PulkoMandy    col->green = CSI_ARG(args[1]);
54dae0a4e0SAdrien Destugues - PulkoMandy    col->blue  = CSI_ARG(args[2]);
55dae0a4e0SAdrien Destugues - PulkoMandy
56dae0a4e0SAdrien Destugues - PulkoMandy    return 3;
57dae0a4e0SAdrien Destugues - PulkoMandy
58dae0a4e0SAdrien Destugues - PulkoMandy  case 5: // XTerm 256-colour mode
59dae0a4e0SAdrien Destugues - PulkoMandy    index = argcount ? CSI_ARG_OR(args[0], -1) : -1;
60dae0a4e0SAdrien Destugues - PulkoMandy
61dae0a4e0SAdrien Destugues - PulkoMandy    if(index >= 0 && index < 16) {
62dae0a4e0SAdrien Destugues - PulkoMandy      // Normal 8 colours or high intensity - parse as palette 0
63dae0a4e0SAdrien Destugues - PulkoMandy      lookup_colour_ansi(index, is_bg, col);
64dae0a4e0SAdrien Destugues - PulkoMandy    }
65dae0a4e0SAdrien Destugues - PulkoMandy    else if(index >= 16 && index < 232) {
66dae0a4e0SAdrien Destugues - PulkoMandy      // 216-colour cube
67dae0a4e0SAdrien Destugues - PulkoMandy      index -= 16;
68dae0a4e0SAdrien Destugues - PulkoMandy
69dae0a4e0SAdrien Destugues - PulkoMandy      col->blue  = ramp6[index     % 6];
70dae0a4e0SAdrien Destugues - PulkoMandy      col->green = ramp6[index/6   % 6];
71dae0a4e0SAdrien Destugues - PulkoMandy      col->red   = ramp6[index/6/6 % 6];
72dae0a4e0SAdrien Destugues - PulkoMandy    }
73dae0a4e0SAdrien Destugues - PulkoMandy    else if(index >= 232 && index < 256) {
74dae0a4e0SAdrien Destugues - PulkoMandy      // 24 greyscales
75dae0a4e0SAdrien Destugues - PulkoMandy      index -= 232;
76dae0a4e0SAdrien Destugues - PulkoMandy
77dae0a4e0SAdrien Destugues - PulkoMandy      col->red   = ramp24[index];
78dae0a4e0SAdrien Destugues - PulkoMandy      col->green = ramp24[index];
79dae0a4e0SAdrien Destugues - PulkoMandy      col->blue  = ramp24[index];
80dae0a4e0SAdrien Destugues - PulkoMandy    }
81dae0a4e0SAdrien Destugues - PulkoMandy
82dae0a4e0SAdrien Destugues - PulkoMandy    return argcount ? 1 : 0;
83dae0a4e0SAdrien Destugues - PulkoMandy
84dae0a4e0SAdrien Destugues - PulkoMandy  default:
85dae0a4e0SAdrien Destugues - PulkoMandy    fprintf(stderr, "Unrecognised colour palette %d\n", palette);
86dae0a4e0SAdrien Destugues - PulkoMandy    return 0;
87dae0a4e0SAdrien Destugues - PulkoMandy  }
88dae0a4e0SAdrien Destugues - PulkoMandy}
89dae0a4e0SAdrien Destugues - PulkoMandy
90dae0a4e0SAdrien Destugues - PulkoMandy// Some conveniences
91dae0a4e0SAdrien Destugues - PulkoMandy
92dae0a4e0SAdrien Destugues - PulkoMandystatic void setpenattr(VTermState *state, VTermAttr attr, VTermValueType type, VTermValue *val)
93dae0a4e0SAdrien Destugues - PulkoMandy{
94dae0a4e0SAdrien Destugues - PulkoMandy#ifdef DEBUG
95dae0a4e0SAdrien Destugues - PulkoMandy  if(type != vterm_get_attr_type(attr)) {
96dae0a4e0SAdrien Destugues - PulkoMandy    fprintf(stderr, "Cannot set attr %d as it has type %d, not type %d\n",
97dae0a4e0SAdrien Destugues - PulkoMandy        attr, vterm_get_attr_type(attr), type);
98dae0a4e0SAdrien Destugues - PulkoMandy    return;
99dae0a4e0SAdrien Destugues - PulkoMandy  }
100dae0a4e0SAdrien Destugues - PulkoMandy#endif
101dae0a4e0SAdrien Destugues - PulkoMandy  if(state->callbacks && state->callbacks->setpenattr)
102dae0a4e0SAdrien Destugues - PulkoMandy    (*state->callbacks->setpenattr)(attr, val, state->cbdata);
103dae0a4e0SAdrien Destugues - PulkoMandy}
104dae0a4e0SAdrien Destugues - PulkoMandy
105dae0a4e0SAdrien Destugues - PulkoMandystatic void setpenattr_bool(VTermState *state, VTermAttr attr, int boolean)
106dae0a4e0SAdrien Destugues - PulkoMandy{
107dae0a4e0SAdrien Destugues - PulkoMandy  VTermValue val = { .boolean = boolean };
108dae0a4e0SAdrien Destugues - PulkoMandy  setpenattr(state, attr, VTERM_VALUETYPE_BOOL, &val);
109dae0a4e0SAdrien Destugues - PulkoMandy}
110dae0a4e0SAdrien Destugues - PulkoMandy
111dae0a4e0SAdrien Destugues - PulkoMandystatic void setpenattr_int(VTermState *state, VTermAttr attr, int number)
112dae0a4e0SAdrien Destugues - PulkoMandy{
113dae0a4e0SAdrien Destugues - PulkoMandy  VTermValue val = { .number = number };
114dae0a4e0SAdrien Destugues - PulkoMandy  setpenattr(state, attr, VTERM_VALUETYPE_INT, &val);
115dae0a4e0SAdrien Destugues - PulkoMandy}
116dae0a4e0SAdrien Destugues - PulkoMandy
117dae0a4e0SAdrien Destugues - PulkoMandystatic void setpenattr_col(VTermState *state, VTermAttr attr, VTermColor color)
118dae0a4e0SAdrien Destugues - PulkoMandy{
119dae0a4e0SAdrien Destugues - PulkoMandy  VTermValue val = { .color = color };
120dae0a4e0SAdrien Destugues - PulkoMandy  setpenattr(state, attr, VTERM_VALUETYPE_COLOR, &val);
121dae0a4e0SAdrien Destugues - PulkoMandy}
122dae0a4e0SAdrien Destugues - PulkoMandy
123dae0a4e0SAdrien Destugues - PulkoMandystatic void set_pen_col_ansi(VTermState *state, VTermAttr attr, long col)
124dae0a4e0SAdrien Destugues - PulkoMandy{
125dae0a4e0SAdrien Destugues - PulkoMandy  VTermColor *colp = (attr == VTERM_ATTR_BACKGROUND) ? &state->pen.bg : &state->pen.fg;
126dae0a4e0SAdrien Destugues - PulkoMandy
127dae0a4e0SAdrien Destugues - PulkoMandy  lookup_colour_ansi(col, attr == VTERM_ATTR_BACKGROUND, colp);
128dae0a4e0SAdrien Destugues - PulkoMandy
129dae0a4e0SAdrien Destugues - PulkoMandy  setpenattr_col(state, attr, *colp);
130dae0a4e0SAdrien Destugues - PulkoMandy}
131dae0a4e0SAdrien Destugues - PulkoMandy
132dae0a4e0SAdrien Destugues - PulkoMandyvoid vterm_state_resetpen(VTermState *state)
133dae0a4e0SAdrien Destugues - PulkoMandy{
134dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.bold = 0;      setpenattr_bool(state, VTERM_ATTR_BOLD, 0);
135dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.underline = 0; setpenattr_int( state, VTERM_ATTR_UNDERLINE, 0);
136dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.italic = 0;    setpenattr_bool(state, VTERM_ATTR_ITALIC, 0);
137dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.blink = 0;     setpenattr_bool(state, VTERM_ATTR_BLINK, 0);
138dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.reverse = 0;   setpenattr_bool(state, VTERM_ATTR_REVERSE, 0);
139dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.strike = 0;    setpenattr_bool(state, VTERM_ATTR_STRIKE, 0);
140dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.font = 0;      setpenattr_int( state, VTERM_ATTR_FONT, 0);
141dae0a4e0SAdrien Destugues - PulkoMandy
142dae0a4e0SAdrien Destugues - PulkoMandy  state->fg_ansi = -1;
143dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.fg = state->default_fg;  setpenattr_col(state, VTERM_ATTR_FOREGROUND, state->default_fg);
144dae0a4e0SAdrien Destugues - PulkoMandy  state->pen.bg = state->default_bg;  setpenattr_col(state, VTERM_ATTR_BACKGROUND, state->default_bg);
145dae0a4e0SAdrien Destugues - PulkoMandy}
146dae0a4e0SAdrien Destugues - PulkoMandy
147dae0a4e0SAdrien Destugues - PulkoMandyvoid vterm_state_savepen(VTermState *state, int save)
148dae0a4e0SAdrien Destugues - PulkoMandy{
149dae0a4e0SAdrien Destugues - PulkoMandy  if(save) {
150dae0a4e0SAdrien Destugues - PulkoMandy    state->saved.pen = state->pen;
151dae0a4e0SAdrien Destugues - PulkoMandy  }
152dae0a4e0SAdrien Destugues - PulkoMandy  else {
153dae0a4e0SAdrien Destugues - PulkoMandy    state->pen = state->saved.pen;
154dae0a4e0SAdrien Destugues - PulkoMandy
155dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_bool(state, VTERM_ATTR_BOLD,       state->pen.bold);
156dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_int( state, VTERM_ATTR_UNDERLINE,  state->pen.underline);
157dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_bool(state, VTERM_ATTR_ITALIC,     state->pen.italic);
158dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_bool(state, VTERM_ATTR_BLINK,      state->pen.blink);
159dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_bool(state, VTERM_ATTR_REVERSE,    state->pen.reverse);
160dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_bool(state, VTERM_ATTR_STRIKE,     state->pen.strike);
161dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_int( state, VTERM_ATTR_FONT,       state->pen.font);
162dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_col( state, VTERM_ATTR_FOREGROUND, state->pen.fg);
163dae0a4e0SAdrien Destugues - PulkoMandy    setpenattr_col( state, VTERM_ATTR_BACKGROUND, state->pen.bg);
164dae0a4e0SAdrien Destugues - PulkoMandy  }
165dae0a4e0SAdrien Destugues - PulkoMandy}
166dae0a4e0SAdrien Destugues - PulkoMandy
167dae0a4e0SAdrien Destugues - PulkoMandyvoid vterm_state_set_default_colors(VTermState *state, VTermColor *default_fg, VTermColor *default_bg)
168dae0a4e0SAdrien Destugues - PulkoMandy{
169dae0a4e0SAdrien Destugues - PulkoMandy  state->default_fg = *default_fg;
170dae0a4e0SAdrien Destugues - PulkoMandy  state->default_bg = *default_bg;
171dae0a4e0SAdrien Destugues - PulkoMandy}
172dae0a4e0SAdrien Destugues - PulkoMandy
173dae0a4e0SAdrien Destugues - PulkoMandyvoid vterm_state_set_bold_highbright(VTermState *state, int bold_is_highbright)
174dae0a4e0SAdrien Destugues - PulkoMandy{
175dae0a4e0SAdrien Destugues - PulkoMandy  state->bold_is_highbright = bold_is_highbright;
176dae0a4e0SAdrien Destugues - PulkoMandy}
177dae0a4e0SAdrien Destugues - PulkoMandy
178dae0a4e0SAdrien Destugues - PulkoMandyvoid vterm_state_setpen(VTermState *state, const long args[], int argcount)
179dae0a4e0SAdrien Destugues - PulkoMandy{
180dae0a4e0SAdrien Destugues - PulkoMandy  // SGR - ECMA-48 8.3.117
181dae0a4e0SAdrien Destugues - PulkoMandy
182dae0a4e0SAdrien Destugues - PulkoMandy  int argi = 0;
183dae0a4e0SAdrien Destugues - PulkoMandy  int value;
184dae0a4e0SAdrien Destugues - PulkoMandy
185dae0a4e0SAdrien Destugues - PulkoMandy  while(argi < argcount) {
186dae0a4e0SAdrien Destugues - PulkoMandy    // This logic is easier to do 'done' backwards; set it true, and make it
187dae0a4e0SAdrien Destugues - PulkoMandy    // false again in the 'default' case
188dae0a4e0SAdrien Destugues - PulkoMandy    int done = 1;
189dae0a4e0SAdrien Destugues - PulkoMandy
190dae0a4e0SAdrien Destugues - PulkoMandy    long arg;
191dae0a4e0SAdrien Destugues - PulkoMandy    switch(arg = CSI_ARG(args[argi])) {
192dae0a4e0SAdrien Destugues - PulkoMandy    case CSI_ARG_MISSING:
193dae0a4e0SAdrien Destugues - PulkoMandy    case 0: // Reset
194dae0a4e0SAdrien Destugues - PulkoMandy      vterm_state_resetpen(state);
195dae0a4e0SAdrien Destugues - PulkoMandy      break;
196dae0a4e0SAdrien Destugues - PulkoMandy
197dae0a4e0SAdrien Destugues - PulkoMandy    case 1: // Bold on
198dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.bold = 1;
199dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_BOLD, 1);
200dae0a4e0SAdrien Destugues - PulkoMandy      if(state->fg_ansi > -1 && state->bold_is_highbright)
201dae0a4e0SAdrien Destugues - PulkoMandy        set_pen_col_ansi(state, VTERM_ATTR_FOREGROUND, state->fg_ansi + (state->pen.bold ? 8 : 0));
202dae0a4e0SAdrien Destugues - PulkoMandy      break;
203dae0a4e0SAdrien Destugues - PulkoMandy
204dae0a4e0SAdrien Destugues - PulkoMandy    case 3: // Italic on
205dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.italic = 1;
206dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_ITALIC, 1);
207dae0a4e0SAdrien Destugues - PulkoMandy      break;
208dae0a4e0SAdrien Destugues - PulkoMandy
209dae0a4e0SAdrien Destugues - PulkoMandy    case 4: // Underline single
210dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.underline = 1;
211dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_int(state, VTERM_ATTR_UNDERLINE, 1);
212dae0a4e0SAdrien Destugues - PulkoMandy      break;
213dae0a4e0SAdrien Destugues - PulkoMandy
214dae0a4e0SAdrien Destugues - PulkoMandy    case 5: // Blink
215dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.blink = 1;
216dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_BLINK, 1);
217dae0a4e0SAdrien Destugues - PulkoMandy      break;
218dae0a4e0SAdrien Destugues - PulkoMandy
219dae0a4e0SAdrien Destugues - PulkoMandy    case 7: // Reverse on
220dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.reverse = 1;
221dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_REVERSE, 1);
222dae0a4e0SAdrien Destugues - PulkoMandy      break;
223dae0a4e0SAdrien Destugues - PulkoMandy
224dae0a4e0SAdrien Destugues - PulkoMandy    case 9: // Strikethrough on
225dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.strike = 1;
226dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_STRIKE, 1);
227dae0a4e0SAdrien Destugues - PulkoMandy      break;
228dae0a4e0SAdrien Destugues - PulkoMandy
229dae0a4e0SAdrien Destugues - PulkoMandy    case 10: case 11: case 12: case 13: case 14:
230dae0a4e0SAdrien Destugues - PulkoMandy    case 15: case 16: case 17: case 18: case 19: // Select font
231dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.font = CSI_ARG(args[argi]) - 10;
232dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_int(state, VTERM_ATTR_FONT, state->pen.font);
233dae0a4e0SAdrien Destugues - PulkoMandy      break;
234dae0a4e0SAdrien Destugues - PulkoMandy
235dae0a4e0SAdrien Destugues - PulkoMandy    case 21: // Underline double
236dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.underline = 2;
237dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_int(state, VTERM_ATTR_UNDERLINE, 2);
238dae0a4e0SAdrien Destugues - PulkoMandy      break;
239dae0a4e0SAdrien Destugues - PulkoMandy
240dae0a4e0SAdrien Destugues - PulkoMandy    case 22: // Bold off
241dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.bold = 0;
242dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_BOLD, 0);
243dae0a4e0SAdrien Destugues - PulkoMandy      break;
244dae0a4e0SAdrien Destugues - PulkoMandy
245dae0a4e0SAdrien Destugues - PulkoMandy    case 23: // Italic and Gothic (currently unsupported) off
246dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.italic = 0;
247dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_ITALIC, 0);
248dae0a4e0SAdrien Destugues - PulkoMandy      break;
249dae0a4e0SAdrien Destugues - PulkoMandy
250dae0a4e0SAdrien Destugues - PulkoMandy    case 24: // Underline off
251dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.underline = 0;
252dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_int(state, VTERM_ATTR_UNDERLINE, 0);
253dae0a4e0SAdrien Destugues - PulkoMandy      break;
254dae0a4e0SAdrien Destugues - PulkoMandy
255dae0a4e0SAdrien Destugues - PulkoMandy    case 25: // Blink off
256dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.blink = 0;
257dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_BLINK, 0);
258dae0a4e0SAdrien Destugues - PulkoMandy      break;
259dae0a4e0SAdrien Destugues - PulkoMandy
260dae0a4e0SAdrien Destugues - PulkoMandy    case 27: // Reverse off
261dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.reverse = 0;
262dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_REVERSE, 0);
263dae0a4e0SAdrien Destugues - PulkoMandy      break;
264dae0a4e0SAdrien Destugues - PulkoMandy
265dae0a4e0SAdrien Destugues - PulkoMandy    case 29: // Strikethrough off
266dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.strike = 0;
267dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_bool(state, VTERM_ATTR_STRIKE, 0);
268dae0a4e0SAdrien Destugues - PulkoMandy      break;
269dae0a4e0SAdrien Destugues - PulkoMandy
270dae0a4e0SAdrien Destugues - PulkoMandy    case 30: case 31: case 32: case 33:
271dae0a4e0SAdrien Destugues - PulkoMandy    case 34: case 35: case 36: case 37: // Foreground colour palette
272dae0a4e0SAdrien Destugues - PulkoMandy      value = CSI_ARG(args[argi]) - 30;
273dae0a4e0SAdrien Destugues - PulkoMandy      state->fg_ansi = value;
274dae0a4e0SAdrien Destugues - PulkoMandy      if(state->pen.bold && state->bold_is_highbright)
275dae0a4e0SAdrien Destugues - PulkoMandy        value += 8;
276dae0a4e0SAdrien Destugues - PulkoMandy      set_pen_col_ansi(state, VTERM_ATTR_FOREGROUND, value);
277dae0a4e0SAdrien Destugues - PulkoMandy      break;
278dae0a4e0SAdrien Destugues - PulkoMandy
279dae0a4e0SAdrien Destugues - PulkoMandy    case 38: // Foreground colour alternative palette
280dae0a4e0SAdrien Destugues - PulkoMandy      state->fg_ansi = -1;
281dae0a4e0SAdrien Destugues - PulkoMandy      if(argcount - argi < 1)
282dae0a4e0SAdrien Destugues - PulkoMandy        return;
283dae0a4e0SAdrien Destugues - PulkoMandy      argi += 1 + lookup_colour(CSI_ARG(args[argi+1]), args+argi+2, argcount-argi-2, 0, &state->pen.fg);
284dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_col(state, VTERM_ATTR_FOREGROUND, state->pen.fg);
285dae0a4e0SAdrien Destugues - PulkoMandy      break;
286dae0a4e0SAdrien Destugues - PulkoMandy
287dae0a4e0SAdrien Destugues - PulkoMandy    case 39: // Foreground colour default
288dae0a4e0SAdrien Destugues - PulkoMandy      state->fg_ansi = -1;
289dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.fg = state->default_fg;
290dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_col(state, VTERM_ATTR_FOREGROUND, state->pen.fg);
291dae0a4e0SAdrien Destugues - PulkoMandy      break;
292dae0a4e0SAdrien Destugues - PulkoMandy
293dae0a4e0SAdrien Destugues - PulkoMandy    case 40: case 41: case 42: case 43:
294dae0a4e0SAdrien Destugues - PulkoMandy    case 44: case 45: case 46: case 47: // Background colour palette
295dae0a4e0SAdrien Destugues - PulkoMandy      set_pen_col_ansi(state, VTERM_ATTR_BACKGROUND, CSI_ARG(args[argi]) - 40);
296dae0a4e0SAdrien Destugues - PulkoMandy      break;
297dae0a4e0SAdrien Destugues - PulkoMandy
298dae0a4e0SAdrien Destugues - PulkoMandy    case 48: // Background colour alternative palette
299dae0a4e0SAdrien Destugues - PulkoMandy      if(argcount - argi < 1)
300dae0a4e0SAdrien Destugues - PulkoMandy        return;
301dae0a4e0SAdrien Destugues - PulkoMandy      argi += 1 + lookup_colour(CSI_ARG(args[argi+1]), args+argi+2, argcount-argi-2, 1, &state->pen.bg);
302dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_col(state, VTERM_ATTR_BACKGROUND, state->pen.bg);
303dae0a4e0SAdrien Destugues - PulkoMandy      break;
304dae0a4e0SAdrien Destugues - PulkoMandy
305dae0a4e0SAdrien Destugues - PulkoMandy    case 49: // Default background
306dae0a4e0SAdrien Destugues - PulkoMandy      state->pen.bg = state->default_bg;
307dae0a4e0SAdrien Destugues - PulkoMandy      setpenattr_col(state, VTERM_ATTR_BACKGROUND, state->pen.bg);
308dae0a4e0SAdrien Destugues - PulkoMandy      break;
309dae0a4e0SAdrien Destugues - PulkoMandy
310dae0a4e0SAdrien Destugues - PulkoMandy    case 90: case 91: case 92: case 93:
311dae0a4e0SAdrien Destugues - PulkoMandy    case 94: case 95: case 96: case 97: // Foreground colour high-intensity palette
312dae0a4e0SAdrien Destugues - PulkoMandy      set_pen_col_ansi(state, VTERM_ATTR_FOREGROUND, CSI_ARG(args[argi]) - 90 + 8);
313dae0a4e0SAdrien Destugues - PulkoMandy      break;
314dae0a4e0SAdrien Destugues - PulkoMandy
315dae0a4e0SAdrien Destugues - PulkoMandy    case 100: case 101: case 102: case 103:
316dae0a4e0SAdrien Destugues - PulkoMandy    case 104: case 105: case 106: case 107: // Background colour high-intensity palette
317dae0a4e0SAdrien Destugues - PulkoMandy      set_pen_col_ansi(state, VTERM_ATTR_BACKGROUND, CSI_ARG(args[argi]) - 100 + 8);
318dae0a4e0SAdrien Destugues - PulkoMandy      break;
319dae0a4e0SAdrien Destugues - PulkoMandy
320dae0a4e0SAdrien Destugues - PulkoMandy    default:
321dae0a4e0SAdrien Destugues - PulkoMandy      done = 0;
322dae0a4e0SAdrien Destugues - PulkoMandy      break;
323dae0a4e0SAdrien Destugues - PulkoMandy    }
324dae0a4e0SAdrien Destugues - PulkoMandy
325dae0a4e0SAdrien Destugues - PulkoMandy    if(!done)
326dae0a4e0SAdrien Destugues - PulkoMandy      fprintf(stderr, "libvterm: Unhandled CSI SGR %lu\n", arg);
327dae0a4e0SAdrien Destugues - PulkoMandy
328dae0a4e0SAdrien Destugues - PulkoMandy    while(CSI_ARG_HAS_MORE(args[argi++]));
329dae0a4e0SAdrien Destugues - PulkoMandy  }
330dae0a4e0SAdrien Destugues - PulkoMandy}
331dae0a4e0SAdrien Destugues - PulkoMandy
332dae0a4e0SAdrien Destugues - PulkoMandyint vterm_state_get_penattr(VTermState *state, VTermAttr attr, VTermValue *val)
333dae0a4e0SAdrien Destugues - PulkoMandy{
334dae0a4e0SAdrien Destugues - PulkoMandy  switch(attr) {
335dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_BOLD:
336dae0a4e0SAdrien Destugues - PulkoMandy    val->boolean = state->pen.bold;
337dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
338dae0a4e0SAdrien Destugues - PulkoMandy
339dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_UNDERLINE:
340dae0a4e0SAdrien Destugues - PulkoMandy    val->number = state->pen.underline;
341dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
342dae0a4e0SAdrien Destugues - PulkoMandy
343dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_ITALIC:
344dae0a4e0SAdrien Destugues - PulkoMandy    val->boolean = state->pen.italic;
345dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
346dae0a4e0SAdrien Destugues - PulkoMandy
347dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_BLINK:
348dae0a4e0SAdrien Destugues - PulkoMandy    val->boolean = state->pen.blink;
349dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
350dae0a4e0SAdrien Destugues - PulkoMandy
351dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_REVERSE:
352dae0a4e0SAdrien Destugues - PulkoMandy    val->boolean = state->pen.reverse;
353dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
354dae0a4e0SAdrien Destugues - PulkoMandy
355dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_STRIKE:
356dae0a4e0SAdrien Destugues - PulkoMandy    val->boolean = state->pen.strike;
357dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
358dae0a4e0SAdrien Destugues - PulkoMandy
359dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_FONT:
360dae0a4e0SAdrien Destugues - PulkoMandy    val->number = state->pen.font;
361dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
362dae0a4e0SAdrien Destugues - PulkoMandy
363dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_FOREGROUND:
364dae0a4e0SAdrien Destugues - PulkoMandy    val->color = state->pen.fg;
365dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
366dae0a4e0SAdrien Destugues - PulkoMandy
367dae0a4e0SAdrien Destugues - PulkoMandy  case VTERM_ATTR_BACKGROUND:
368dae0a4e0SAdrien Destugues - PulkoMandy    val->color = state->pen.bg;
369dae0a4e0SAdrien Destugues - PulkoMandy    return 1;
370dae0a4e0SAdrien Destugues - PulkoMandy  }
371dae0a4e0SAdrien Destugues - PulkoMandy
372dae0a4e0SAdrien Destugues - PulkoMandy  return 0;
373dae0a4e0SAdrien Destugues - PulkoMandy}
374