color-slang.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. Color setup for S_Lang screen library
  3. Copyright (C) 1994-2018
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Andrew Borodin <aborodin@vmail.ru>, 2009
  7. Egmont Koblinger <egmont@gmail.com>, 2010
  8. This file is part of the Midnight Commander.
  9. The Midnight Commander is free software: you can redistribute it
  10. and/or modify it under the terms of the GNU General Public License as
  11. published by the Free Software Foundation, either version 3 of the License,
  12. or (at your option) any later version.
  13. The Midnight Commander is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /** \file color-slang.c
  21. * \brief Source: S-Lang-specific color setup
  22. */
  23. #include <config.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/types.h> /* size_t */
  28. #include "lib/global.h"
  29. #include "lib/util.h" /* whitespace() */
  30. #include "tty-slang.h"
  31. #include "color.h" /* variables */
  32. #include "color-internal.h"
  33. /*** global variables ****************************************************************************/
  34. /*** file scope macro definitions ****************************************************************/
  35. /*** file scope type declarations ****************************************************************/
  36. /*** file scope variables ************************************************************************/
  37. /*** file scope functions ************************************************************************/
  38. /* --------------------------------------------------------------------------------------------- */
  39. static int
  40. has_colors (gboolean disable, gboolean force)
  41. {
  42. mc_tty_color_disable = disable;
  43. if (force || (getenv ("COLORTERM") != NULL))
  44. SLtt_Use_Ansi_Colors = 1;
  45. if (!mc_tty_color_disable)
  46. {
  47. const char *terminal = getenv ("TERM");
  48. const size_t len = strlen (terminal);
  49. char *cts = mc_global.tty.color_terminal_string;
  50. /* check mc_global.tty.color_terminal_string */
  51. while (*cts != '\0')
  52. {
  53. char *s;
  54. size_t i = 0;
  55. while (whitespace (*cts))
  56. cts++;
  57. s = cts;
  58. while (*cts != '\0' && *cts != ',')
  59. {
  60. cts++;
  61. i++;
  62. }
  63. if ((i != 0) && (i == len) && (strncmp (s, terminal, i) == 0))
  64. SLtt_Use_Ansi_Colors = 1;
  65. if (*cts == ',')
  66. cts++;
  67. }
  68. }
  69. return SLtt_Use_Ansi_Colors;
  70. }
  71. /* --------------------------------------------------------------------------------------------- */
  72. static void
  73. mc_tty_color_pair_init_special (tty_color_pair_t * mc_color_pair,
  74. const char *fg1, const char *bg1,
  75. const char *fg2, const char *bg2, SLtt_Char_Type mask)
  76. {
  77. if (SLtt_Use_Ansi_Colors != 0)
  78. {
  79. if (!mc_tty_color_disable)
  80. {
  81. SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg1, (char *) bg1);
  82. }
  83. else
  84. {
  85. SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg2, (char *) bg2);
  86. }
  87. }
  88. else
  89. {
  90. SLtt_set_mono (mc_color_pair->pair_index, NULL, mask);
  91. }
  92. }
  93. /* --------------------------------------------------------------------------------------------- */
  94. /*** public functions ****************************************************************************/
  95. /* --------------------------------------------------------------------------------------------- */
  96. void
  97. tty_color_init_lib (gboolean disable, gboolean force)
  98. {
  99. /* FIXME: if S-Lang is used, has_colors() must be called regardless
  100. of whether we are interested in its result */
  101. if (has_colors (disable, force) && !disable)
  102. {
  103. use_colors = TRUE;
  104. }
  105. }
  106. /* --------------------------------------------------------------------------------------------- */
  107. void
  108. tty_color_deinit_lib (void)
  109. {
  110. }
  111. /* --------------------------------------------------------------------------------------------- */
  112. void
  113. tty_color_try_alloc_pair_lib (tty_color_pair_t * mc_color_pair)
  114. {
  115. if (mc_color_pair->ifg <= (int) SPEC_A_REVERSE)
  116. {
  117. switch (mc_color_pair->ifg)
  118. {
  119. case SPEC_A_REVERSE:
  120. mc_tty_color_pair_init_special (mc_color_pair,
  121. "black", "white", "black", "lightgray", SLTT_REV_MASK);
  122. break;
  123. case SPEC_A_BOLD:
  124. mc_tty_color_pair_init_special (mc_color_pair,
  125. "white", "black", "white", "black", SLTT_BOLD_MASK);
  126. break;
  127. case SPEC_A_BOLD_REVERSE:
  128. mc_tty_color_pair_init_special (mc_color_pair,
  129. "white", "white",
  130. "white", "white", SLTT_BOLD_MASK | SLTT_REV_MASK);
  131. break;
  132. case SPEC_A_UNDERLINE:
  133. mc_tty_color_pair_init_special (mc_color_pair,
  134. "white", "black", "white", "black", SLTT_ULINE_MASK);
  135. break;
  136. default:
  137. break;
  138. }
  139. }
  140. else
  141. {
  142. const char *fg, *bg;
  143. fg = tty_color_get_name_by_index (mc_color_pair->ifg);
  144. bg = tty_color_get_name_by_index (mc_color_pair->ibg);
  145. SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg, (char *) bg);
  146. SLtt_add_color_attribute (mc_color_pair->pair_index, mc_color_pair->attr);
  147. }
  148. }
  149. /* --------------------------------------------------------------------------------------------- */
  150. void
  151. tty_setcolor (int color)
  152. {
  153. SLsmg_set_color (color);
  154. }
  155. /* --------------------------------------------------------------------------------------------- */
  156. /**
  157. * Set colorpair by index, don't interpret S-Lang "emulated attributes"
  158. */
  159. void
  160. tty_lowlevel_setcolor (int color)
  161. {
  162. SLsmg_set_color (color & 0x7F);
  163. }
  164. /* --------------------------------------------------------------------------------------------- */
  165. void
  166. tty_set_normal_attrs (void)
  167. {
  168. SLsmg_normal_video ();
  169. }
  170. /* --------------------------------------------------------------------------------------------- */
  171. gboolean
  172. tty_use_256colors (void)
  173. {
  174. return (SLtt_Use_Ansi_Colors && SLtt_tgetnum ((char *) "Co") == 256);
  175. }
  176. /* --------------------------------------------------------------------------------------------- */
  177. gboolean
  178. tty_use_truecolors (GError ** error)
  179. {
  180. char *colorterm;
  181. /* True color is supported since slang-2.3.1 on 64-bit machines,
  182. and expected to be supported from slang-3 on 32-bit machines:
  183. http://lists.jedsoft.org/lists/slang-users/2016/0000014.html.
  184. Check for sizeof (long) being 8, exactly as slang does. */
  185. if (SLang_Version < 20301 || (sizeof (long) != 8 && SLang_Version < 30000))
  186. {
  187. g_set_error (error, MC_ERROR, -1, _("True color not supported in this slang version."));
  188. return FALSE;
  189. }
  190. /* Sanity check that at least 256 colors are supported. */
  191. if (!tty_use_256colors ())
  192. {
  193. g_set_error (error, MC_ERROR, -1,
  194. _("Your terminal doesn't even seem to support 256 colors."));
  195. return FALSE;
  196. }
  197. /* Duplicate slang's check so that we can pop up an error message
  198. rather than silently use wrong colors. */
  199. colorterm = getenv ("COLORTERM");
  200. if (colorterm == NULL
  201. || (strcmp (colorterm, "truecolor") != 0 && strcmp (colorterm, "24bit") != 0))
  202. {
  203. g_set_error (error, MC_ERROR, -1,
  204. _("Set COLORTERM=truecolor if your terminal really supports true colors."));
  205. return FALSE;
  206. }
  207. return TRUE;
  208. }
  209. /* --------------------------------------------------------------------------------------------- */