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

File mc-3145-truecolor-v3.patch, 9.5 KB (added by egmont, 2 years ago)

truecolor support v3

  • doc/man/mc.1.in

    diff --git a/doc/man/mc.1.in b/doc/man/mc.1.in
    index 2413b1e..ff46bb7 100644
    a b with the assignment of colors, as described in Section 
    36543654Colors\&. 
    36553655.\"Colors" 
    36563656.PP 
    3657 If your skin contains any of 256\-color definitions, you should define 
    3658 the '256colors' key set to TRUE value in [skin] section. 
    3659  
     3657If your skin contains any true\-color definitions, you should define 
     3658the 'truecolors' key set to TRUE value in [skin] section. If true\-color 
     3659is not used but 256\-color is, you should define '256colors' instead. 
    36603660.PP 
    36613661A skin\-file is searched on the following algorithm (to the first one found): 
    36623662.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 46a5c3e..fa37e2a 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 a29167d..b4dc314 100644
    a b mc_skin_ini_file_parse (mc_skin_t * mc_skin) 
    187187 
    188188    mc_skin_lines_parse_ini_file (mc_skin); 
    189189    mc_skin->have_256_colors = mc_config_get_bool (mc_skin->config, "skin", "256colors", FALSE); 
     190    mc_skin->have_true_colors = mc_config_get_bool (mc_skin->config, "skin", "truecolors", FALSE); 
    190191 
    191192    return TRUE; 
    192193} 
  • lib/tty/color-internal.c

    diff --git a/lib/tty/color-internal.c b/lib/tty/color-internal.c
    index 13ee10e..d9bf3cd 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 f26927c..48ed521 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 d8646ca..38fd4e0 100644
    a b tty_use_256colors (void) 
    215215} 
    216216 
    217217/* --------------------------------------------------------------------------------------------- */ 
     218 
     219gboolean 
     220tty_use_truecolors (GError ** error) 
     221{ 
     222    char *colorterm; 
     223 
     224    /* True color is supported since slang-2.3.1 on 64-bit machines, 
     225       and expected to be supported from slang-3 on 32-bit machines: 
     226       http://lists.jedsoft.org/lists/slang-users/2016/0000014.html. 
     227       Check for sizeof (long) being 8, exactly as slang does. */ 
     228    if (SLang_Version < 20301 || (sizeof (long) != 8 && SLang_Version < 30000)) 
     229    { 
     230        g_set_error (error, MC_ERROR, -1, 
     231                     _("True color not supported in this slang version.")); 
     232        return FALSE; 
     233    } 
     234 
     235    /* Sanity check that at least 256 colors are supported. */ 
     236    if (!tty_use_256colors ()) 
     237    { 
     238        g_set_error (error, MC_ERROR, -1, 
     239                     _("Your terminal doesn't even seem to support 256 colors.")); 
     240        return FALSE; 
     241    } 
     242 
     243    /* Duplicate slang's check so that we can pop up an error message 
     244       rather than silently use wrong colors. */ 
     245    colorterm = getenv ("COLORTERM"); 
     246    if (colorterm == NULL || (strcmp (colorterm, "truecolor") != 0 && strcmp (colorterm, "24bit") != 0)) 
     247    { 
     248        g_set_error (error, MC_ERROR, -1, 
     249                     _("Set COLORTERM=truecolor if your terminal really supports true colors.")); 
     250        return FALSE; 
     251    } 
     252 
     253    return TRUE; 
     254} 
     255 
     256/* --------------------------------------------------------------------------------------------- */ 
  • 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 0427903..1341b14 100644
    a b tty_init (gboolean mouse_enable, gboolean is_xterm) 
    316316    tty_reset_prog_mode (); 
    317317    load_terminfo_keys (); 
    318318 
    319     SLtt_Blink_Mode = tty_use_256colors ()? 1 : 0; 
     319    SLtt_Blink_Mode = (tty_use_256colors () || tty_use_truecolors (NULL)) ? 1 : 0; 
    320320 
    321321    tty_start_interrupt_key (); 
    322322