color-slang.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. Color setup for S_Lang screen library
  3. Copyright (C) 1994-2015
  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 "tty-slang.h"
  30. #include "color.h" /* variables */
  31. #include "color-internal.h"
  32. /*** global variables ****************************************************************************/
  33. /*** file scope macro definitions ****************************************************************/
  34. /*** file scope type declarations ****************************************************************/
  35. /*** file scope variables ************************************************************************/
  36. /*** file scope functions ************************************************************************/
  37. /* --------------------------------------------------------------------------------------------- */
  38. static int
  39. has_colors (gboolean disable, gboolean force)
  40. {
  41. mc_tty_color_disable = disable;
  42. if (force || (getenv ("COLORTERM") != NULL))
  43. SLtt_Use_Ansi_Colors = 1;
  44. if (!mc_tty_color_disable)
  45. {
  46. const char *terminal = getenv ("TERM");
  47. const size_t len = strlen (terminal);
  48. char *cts = mc_global.tty.color_terminal_string;
  49. /* check mc_global.tty.color_terminal_string */
  50. while (*cts != '\0')
  51. {
  52. char *s;
  53. size_t i = 0;
  54. while (*cts == ' ' || *cts == '\t')
  55. cts++;
  56. s = cts;
  57. while (*cts != '\0' && *cts != ',')
  58. {
  59. cts++;
  60. i++;
  61. }
  62. if ((i != 0) && (i == len) && (strncmp (s, terminal, i) == 0))
  63. SLtt_Use_Ansi_Colors = 1;
  64. if (*cts == ',')
  65. cts++;
  66. }
  67. }
  68. return SLtt_Use_Ansi_Colors;
  69. }
  70. /* --------------------------------------------------------------------------------------------- */
  71. static void
  72. mc_tty_color_pair_init_special (tty_color_pair_t * mc_color_pair,
  73. const char *fg1, const char *bg1,
  74. const char *fg2, const char *bg2, SLtt_Char_Type mask)
  75. {
  76. if (SLtt_Use_Ansi_Colors != 0)
  77. {
  78. if (!mc_tty_color_disable)
  79. {
  80. SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg1, (char *) bg1);
  81. }
  82. else
  83. {
  84. SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg2, (char *) bg2);
  85. }
  86. }
  87. else
  88. {
  89. SLtt_set_mono (mc_color_pair->pair_index, NULL, mask);
  90. }
  91. }
  92. /* --------------------------------------------------------------------------------------------- */
  93. /*** public functions ****************************************************************************/
  94. /* --------------------------------------------------------------------------------------------- */
  95. void
  96. tty_color_init_lib (gboolean disable, gboolean force)
  97. {
  98. /* FIXME: if S-Lang is used, has_colors() must be called regardless
  99. of whether we are interested in its result */
  100. if (has_colors (disable, force) && !disable)
  101. {
  102. use_colors = TRUE;
  103. }
  104. }
  105. /* --------------------------------------------------------------------------------------------- */
  106. void
  107. tty_color_deinit_lib (void)
  108. {
  109. }
  110. /* --------------------------------------------------------------------------------------------- */
  111. void
  112. tty_color_try_alloc_pair_lib (tty_color_pair_t * mc_color_pair)
  113. {
  114. if (mc_color_pair->ifg <= (int) SPEC_A_REVERSE)
  115. {
  116. switch (mc_color_pair->ifg)
  117. {
  118. case SPEC_A_REVERSE:
  119. mc_tty_color_pair_init_special (mc_color_pair,
  120. "black", "white", "black", "lightgray", SLTT_REV_MASK);
  121. break;
  122. case SPEC_A_BOLD:
  123. mc_tty_color_pair_init_special (mc_color_pair,
  124. "white", "black", "white", "black", SLTT_BOLD_MASK);
  125. break;
  126. case SPEC_A_BOLD_REVERSE:
  127. mc_tty_color_pair_init_special (mc_color_pair,
  128. "white", "white",
  129. "white", "white", SLTT_BOLD_MASK | SLTT_REV_MASK);
  130. break;
  131. case SPEC_A_UNDERLINE:
  132. mc_tty_color_pair_init_special (mc_color_pair,
  133. "white", "black", "white", "black", SLTT_ULINE_MASK);
  134. break;
  135. }
  136. }
  137. else
  138. {
  139. const char *fg, *bg;
  140. fg = tty_color_get_name_by_index (mc_color_pair->ifg);
  141. bg = tty_color_get_name_by_index (mc_color_pair->ibg);
  142. SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg, (char *) bg);
  143. SLtt_add_color_attribute (mc_color_pair->pair_index, mc_color_pair->attr);
  144. }
  145. }
  146. /* --------------------------------------------------------------------------------------------- */
  147. void
  148. tty_setcolor (int color)
  149. {
  150. SLsmg_set_color (color);
  151. }
  152. /* --------------------------------------------------------------------------------------------- */
  153. /**
  154. * Set colorpair by index, don't interpret S-Lang "emulated attributes"
  155. */
  156. void
  157. tty_lowlevel_setcolor (int color)
  158. {
  159. SLsmg_set_color (color & 0x7F);
  160. }
  161. /* --------------------------------------------------------------------------------------------- */
  162. void
  163. tty_set_normal_attrs (void)
  164. {
  165. SLsmg_normal_video ();
  166. }
  167. /* --------------------------------------------------------------------------------------------- */
  168. gboolean
  169. tty_use_256colors (void)
  170. {
  171. return (SLtt_Use_Ansi_Colors && SLtt_tgetnum ((char *) "Co") == 256);
  172. }
  173. /* --------------------------------------------------------------------------------------------- */