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