Ticket #3145: mc-3145-truecolor-v2.patch

File mc-3145-truecolor-v2.patch, 9.2 KB (added by egmont, 9 years ago)

truecolor support v2

  • doc/man/mc.1.in

    diff --git a/doc/man/mc.1.in b/doc/man/mc.1.in
    index e94e4e8..7a8eb0a 100644
    a b with the assignment of colors, as described in Section 
    36323632Colors\&. 
    36333633.\"Colors" 
    36343634.PP 
    3635 If your skin contains any of 256\-color definitions, you should define 
    3636 the '256colors' key set to TRUE value in [skin] section. 
    3637  
     3635If your skin contains any of true\-color definitions, you should define 
     3636the 'truecolors' key set to TRUE value in [skin] section. If true\-color 
     3637is not used but 256\-color is, you should define '256colors' instead. 
    36383638.PP 
    36393639A skin\-file is searched on the following algorithm (to the first one found): 
    36403640.IP 
  • lib/skin.h

    diff --git a/lib/skin.h b/lib/skin.h
    index 8d32180..747a504 100644
    a b typedef struct mc_skin_struct 
    120120    mc_config_t *config; 
    121121    GHashTable *colors; 
    122122    gboolean have_256_colors; 
     123    gboolean have_true_colors; 
    123124} mc_skin_t; 
    124125 
    125126/*** global variables defined in .c file *********************************************************/ 
  • lib/skin/common.c

    diff --git a/lib/skin/common.c b/lib/skin/common.c
    index f620ec8..180eb1b 100644
    a b gboolean 
    114114mc_skin_init (const gchar * skin_override, GError ** mcerror) 
    115115{ 
    116116    gboolean is_good_init = TRUE; 
     117    GError *error = NULL; 
    117118 
    118119    mc_return_val_if_error (mcerror, FALSE); 
    119120 
    120121    mc_skin__default.have_256_colors = FALSE; 
     122    mc_skin__default.have_true_colors = FALSE; 
    121123 
    122124    mc_skin__default.name = 
    123125        skin_override != NULL ? g_strdup (skin_override) : mc_skin_get_default_name (); 
    mc_skin_init (const gchar * skin_override, GError ** mcerror) 
    145147        (void) mc_skin_ini_file_parse (&mc_skin__default); 
    146148        is_good_init = FALSE; 
    147149    } 
     150    if (is_good_init && !tty_use_truecolors (&error) && mc_skin__default.have_true_colors) 
     151    { 
     152        mc_propagate_error (mcerror, 0, 
     153                            _ 
     154                            ("Unable to use '%s' skin with true colors support:\n%s\nDefault skin has been loaded"), 
     155                            mc_skin__default.name, error->message); 
     156        g_error_free (error); 
     157        mc_skin_try_to_load_default (); 
     158        mc_skin_colors_old_configure (&mc_skin__default); 
     159        (void) mc_skin_ini_file_parse (&mc_skin__default); 
     160        is_good_init = FALSE; 
     161    } 
    148162    if (is_good_init && !tty_use_256colors () && mc_skin__default.have_256_colors) 
    149163    { 
    150164        mc_propagate_error (mcerror, 0, 
  • lib/skin/ini-file.c

    diff --git a/lib/skin/ini-file.c b/lib/skin/ini-file.c
    index 923b45a..fad3492 100644
    a b mc_skin_ini_file_parse (mc_skin_t * mc_skin) 
    188188 
    189189    mc_skin_lines_parse_ini_file (mc_skin); 
    190190    mc_skin->have_256_colors = mc_config_get_bool (mc_skin->config, "skin", "256colors", FALSE); 
     191    mc_skin->have_true_colors = mc_config_get_bool (mc_skin->config, "skin", "truecolors", FALSE); 
    191192 
    192193    return TRUE; 
    193194} 
  • lib/tty/color-internal.c

    diff --git a/lib/tty/color-internal.c b/lib/tty/color-internal.c
    index 5359943..3a982c8 100644
    a b static mc_tty_color_table_t const attributes_table[] = { 
    9696/*** file scope functions ************************************************************************/ 
    9797/* --------------------------------------------------------------------------------------------- */ 
    9898 
     99static inline int 
     100parse_hex_digit (char c) 
     101{ 
     102    if (c >= '0' && c <= '9') 
     103        return c - '0'; 
     104    c |= 0x20; 
     105    if (c >= 'a' && c <= 'f') 
     106        return c - 'a' + 10; 
     107    return -1; 
     108} 
     109 
    99110static int 
    100 parse_256_color_name (const char *color_name) 
     111parse_256_or_true_color_name (const char *color_name) 
    101112{ 
    102113    int i; 
    103114    char dummy; 
    parse_256_color_name (const char *color_name) 
    119130    { 
    120131        return 16 + 36 * (color_name[3] - '0') + 6 * (color_name[4] - '0') + (color_name[5] - '0'); 
    121132    } 
     133    if (color_name[0] == '#') 
     134    { 
     135        int h[6]; 
     136        color_name++; 
     137        if (strlen (color_name) != 3 && strlen (color_name) != 6) 
     138            return -1; 
     139        for (i = 0; color_name[i] != '\0'; i++) 
     140        { 
     141            h[i] = parse_hex_digit (color_name[i]); 
     142            if (h[i] == -1) 
     143                return -1; 
     144        } 
     145        if (i == 3) 
     146            i = (h[0] << 20) | (h[0] << 16) | (h[1] << 12) | (h[1] << 8) | (h[2] << 4) | h[2]; 
     147        else 
     148            i = (h[0] << 20) | (h[1] << 16) | (h[2] << 12) | (h[3] << 8) | (h[4] << 4) | h[5]; 
     149        return (1 << 24) | i; 
     150    } 
    122151    return -1; 
    123152} 
    124153 
    tty_color_get_name_by_index (int idx) 
    136165    for (i = 0; color_table[i].name != NULL; i++) 
    137166        if (idx == color_table[i].value) 
    138167            return color_table[i].name; 
    139     /* Create and return the strings "color16" to "color255". */ 
    140     if (idx >= 16 && idx < 256) 
     168    /* Create and return the strings in "colorNNN" or "#rrggbb" format. */ 
     169    if ((idx >= 16 && idx < 256) || idx & (1 << 24)) 
    141170    { 
    142         static char **color_N_names = NULL; 
    143  
    144         if (color_N_names == NULL) 
    145         { 
    146             color_N_names = g_try_malloc0 (240 * sizeof (char *)); 
    147         } 
    148         if (color_N_names[idx - 16] == NULL) 
    149         { 
    150             color_N_names[idx - 16] = g_try_malloc (9); 
    151             sprintf (color_N_names[idx - 16], "color%d", idx); 
    152         } 
    153         return color_N_names[idx - 16]; 
     171        char name[9]; 
     172        if (idx < 256) 
     173            sprintf (name, "color%d", idx); 
     174        else 
     175            sprintf (name, "#%06X", idx & 0xFFFFFF); 
     176        return g_intern_string (name); 
    154177    } 
    155178    return "default"; 
    156179} 
    tty_color_get_index_by_name (const char *color_name) 
    167190        for (i = 0; color_table[i].name != NULL; i++) 
    168191            if (strcmp (color_name, color_table[i].name) == 0) 
    169192                return color_table[i].value; 
    170         return parse_256_color_name (color_name); 
     193        return parse_256_or_true_color_name (color_name); 
    171194    } 
    172195    return -1; 
    173196} 
  • lib/tty/color-ncurses.c

    diff --git a/lib/tty/color-ncurses.c b/lib/tty/color-ncurses.c
    index 1688520..7f8222f 100644
    a b tty_color_try_alloc_pair_lib (tty_color_pair_t * mc_color_pair) 
    180180        ibg = mc_color_pair->ibg; 
    181181        attr = mc_color_pair->attr; 
    182182 
    183         /* In non-256 color mode, change bright colors into bold */ 
    184         if (!tty_use_256colors ()) 
     183        /* In legacy color mode, change bright colors into bold */ 
     184        if (!tty_use_256colors () && !tty_use_truecolors (NULL)) 
    185185        { 
    186186            if (ifg >= 8 && ifg < 16) 
    187187            { 
    tty_use_256colors (void) 
    234234} 
    235235 
    236236/* --------------------------------------------------------------------------------------------- */ 
     237 
     238gboolean 
     239tty_use_truecolors (GError ** error) 
     240{ 
     241    /* Not yet supported in ncurses */ 
     242    g_set_error (error, MC_ERROR, -1, _("True color not supported with ncurses.")); 
     243    return FALSE; 
     244} 
     245 
     246/* --------------------------------------------------------------------------------------------- */ 
  • lib/tty/color-slang.c

    diff --git a/lib/tty/color-slang.c b/lib/tty/color-slang.c
    index b9d5a3c..9d0768d 100644
    a b tty_use_256colors (void) 
    215215} 
    216216 
    217217/* --------------------------------------------------------------------------------------------- */ 
     218 
     219gboolean 
     220tty_use_truecolors (GError ** error) 
     221{ 
     222    /* Ideally this should check slang's runtime version, but there's no API for that. */ 
     223#if SLANG_VERSION < 20301 
     224    g_set_error (error, MC_ERROR, -1, _("True color not supported in this slang version.")); 
     225    return FALSE; 
     226#else 
     227    char *colorterm; 
     228 
     229    /* Sanity check that at least 256 colors are supported. */ 
     230    if (!tty_use_256colors ()) 
     231    { 
     232        g_set_error (error, MC_ERROR, -1, 
     233                     _("Your terminal doesn't even seem to support 256 colors.")); 
     234        return FALSE; 
     235    } 
     236 
     237    /* Duplicate slang's check so that we can pop up an error message 
     238       rather than silently use wrong colors. */ 
     239    colorterm = getenv ("COLORTERM"); 
     240    if (colorterm == NULL || (strcmp (colorterm, "truecolor") != 0 && strcmp (colorterm, "24bit"))) 
     241    { 
     242        g_set_error (error, MC_ERROR, -1, 
     243                     _("Set COLORTERM=truecolor if your terminal really supports true colors.")); 
     244        return FALSE; 
     245    } 
     246 
     247    return TRUE; 
     248#endif /* SLANG_VERSION */ 
     249} 
     250 
     251/* --------------------------------------------------------------------------------------------- */ 
  • lib/tty/color.h

    diff --git a/lib/tty/color.h b/lib/tty/color.h
    index 00ce671..1a9f573 100644
    a b void tty_set_normal_attrs (void); 
    4848void tty_color_set_defaults (const char *, const char *, const char *); 
    4949 
    5050extern gboolean tty_use_256colors (void); 
     51extern gboolean tty_use_truecolors (GError **); 
    5152 
    5253/*** inline functions ****************************************************************************/ 
    5354#endif /* MC_COLOR_H */ 
  • lib/tty/tty-slang.c

    diff --git a/lib/tty/tty-slang.c b/lib/tty/tty-slang.c
    index dfb0fa8..caa090f 100644
    a b tty_init (gboolean mouse_enable, gboolean is_xterm) 
    309309    tty_reset_prog_mode (); 
    310310    load_terminfo_keys (); 
    311311 
    312     SLtt_Blink_Mode = tty_use_256colors ()? 1 : 0; 
     312    SLtt_Blink_Mode = (tty_use_256colors () || tty_use_truecolors (NULL)) ? 1 : 0; 
    313313 
    314314    tty_start_interrupt_key (); 
    315315