color-slang.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. Color setup for S_Lang screen library
  3. Copyright (C) 1994-2024
  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. /*** forward declarations (file scope functions) *************************************************/
  37. /*** file scope variables ************************************************************************/
  38. /* --------------------------------------------------------------------------------------------- */
  39. /*** file scope functions ************************************************************************/
  40. /* --------------------------------------------------------------------------------------------- */
  41. static int
  42. has_colors (gboolean disable, gboolean force)
  43. {
  44. mc_tty_color_disable = disable;
  45. if (force || (getenv ("COLORTERM") != NULL))
  46. SLtt_Use_Ansi_Colors = 1;
  47. if (!mc_tty_color_disable)
  48. {
  49. const char *terminal = getenv ("TERM");
  50. const size_t len = strlen (terminal);
  51. char *cts = mc_global.tty.color_terminal_string;
  52. /* check mc_global.tty.color_terminal_string */
  53. while (*cts != '\0')
  54. {
  55. char *s;
  56. size_t i = 0;
  57. while (whitespace (*cts))
  58. cts++;
  59. s = cts;
  60. while (*cts != '\0' && *cts != ',')
  61. {
  62. cts++;
  63. i++;
  64. }
  65. if ((i != 0) && (i == len) && (strncmp (s, terminal, i) == 0))
  66. SLtt_Use_Ansi_Colors = 1;
  67. if (*cts == ',')
  68. cts++;
  69. }
  70. }
  71. return SLtt_Use_Ansi_Colors;
  72. }
  73. /* --------------------------------------------------------------------------------------------- */
  74. static void
  75. mc_tty_color_pair_init_special (tty_color_lib_pair_t *mc_color_pair,
  76. const char *fg1, const char *bg1,
  77. const char *fg2, const char *bg2, SLtt_Char_Type mask)
  78. {
  79. if (SLtt_Use_Ansi_Colors != 0)
  80. {
  81. if (!mc_tty_color_disable)
  82. {
  83. SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg1, (char *) bg1);
  84. }
  85. else
  86. {
  87. SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg2, (char *) bg2);
  88. }
  89. }
  90. else
  91. {
  92. SLtt_set_mono (mc_color_pair->pair_index, NULL, mask);
  93. }
  94. }
  95. /* --------------------------------------------------------------------------------------------- */
  96. /*** public functions ****************************************************************************/
  97. /* --------------------------------------------------------------------------------------------- */
  98. void
  99. tty_color_init_lib (gboolean disable, gboolean force)
  100. {
  101. /* FIXME: if S-Lang is used, has_colors() must be called regardless
  102. of whether we are interested in its result */
  103. if (has_colors (disable, force) && !disable)
  104. {
  105. use_colors = TRUE;
  106. }
  107. }
  108. /* --------------------------------------------------------------------------------------------- */
  109. void
  110. tty_color_deinit_lib (void)
  111. {
  112. }
  113. /* --------------------------------------------------------------------------------------------- */
  114. void
  115. tty_color_try_alloc_lib_pair (tty_color_lib_pair_t *mc_color_pair)
  116. {
  117. if (mc_color_pair->fg <= (int) SPEC_A_REVERSE)
  118. {
  119. switch (mc_color_pair->fg)
  120. {
  121. case SPEC_A_REVERSE:
  122. mc_tty_color_pair_init_special (mc_color_pair,
  123. "black", "white", "black", "lightgray", SLTT_REV_MASK);
  124. break;
  125. case SPEC_A_BOLD:
  126. mc_tty_color_pair_init_special (mc_color_pair,
  127. "white", "black", "white", "black", SLTT_BOLD_MASK);
  128. break;
  129. case SPEC_A_BOLD_REVERSE:
  130. mc_tty_color_pair_init_special (mc_color_pair,
  131. "white", "white",
  132. "white", "white", SLTT_BOLD_MASK | SLTT_REV_MASK);
  133. break;
  134. case SPEC_A_UNDERLINE:
  135. mc_tty_color_pair_init_special (mc_color_pair,
  136. "white", "black", "white", "black", SLTT_ULINE_MASK);
  137. break;
  138. default:
  139. break;
  140. }
  141. }
  142. else
  143. {
  144. const char *fg, *bg;
  145. fg = tty_color_get_name_by_index (mc_color_pair->fg);
  146. bg = tty_color_get_name_by_index (mc_color_pair->bg);
  147. SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg, (char *) bg);
  148. SLtt_add_color_attribute (mc_color_pair->pair_index, mc_color_pair->attr);
  149. }
  150. }
  151. /* --------------------------------------------------------------------------------------------- */
  152. void
  153. tty_setcolor (int color)
  154. {
  155. SLsmg_set_color (color);
  156. }
  157. /* --------------------------------------------------------------------------------------------- */
  158. /**
  159. * Set colorpair by index, don't interpret S-Lang "emulated attributes"
  160. */
  161. void
  162. tty_lowlevel_setcolor (int color)
  163. {
  164. SLsmg_set_color (color & 0x7F);
  165. }
  166. /* --------------------------------------------------------------------------------------------- */
  167. void
  168. tty_set_normal_attrs (void)
  169. {
  170. SLsmg_normal_video ();
  171. }
  172. /* --------------------------------------------------------------------------------------------- */
  173. gboolean
  174. tty_use_256colors (GError **error)
  175. {
  176. gboolean ret;
  177. ret = (SLtt_Use_Ansi_Colors && SLtt_tgetnum ((char *) "Co") == 256);
  178. if (!ret)
  179. g_set_error (error, MC_ERROR, -1,
  180. _("Your terminal doesn't even seem to support 256 colors."));
  181. return ret;
  182. }
  183. /* --------------------------------------------------------------------------------------------- */
  184. gboolean
  185. tty_use_truecolors (GError **error)
  186. {
  187. char *colorterm;
  188. /* True color is supported since slang-2.3.1 on 64-bit machines,
  189. and expected to be supported from slang-3 on 32-bit machines:
  190. http://lists.jedsoft.org/lists/slang-users/2016/0000014.html.
  191. Check for sizeof (long) being 8, exactly as slang does. */
  192. if (SLang_Version < 20301 || (sizeof (long) != 8 && SLang_Version < 30000))
  193. {
  194. g_set_error (error, MC_ERROR, -1, _("True color not supported in this slang version."));
  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. /* --------------------------------------------------------------------------------------------- */