color-slang.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. Color setup for S_Lang screen library
  3. Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2007, 2008, 2009, 2010, 2011
  5. The Free Software Foundation, Inc.
  6. Written by:
  7. Andrew Borodin <aborodin@vmail.ru>, 2009
  8. Egmont Koblinger <egmont@gmail.com>, 2010
  9. This file is part of the Midnight Commander.
  10. The Midnight Commander is free software: you can redistribute it
  11. and/or modify it under the terms of the GNU General Public License as
  12. published by the Free Software Foundation, either version 3 of the License,
  13. or (at your option) any later version.
  14. The Midnight Commander is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /** \file color-slang.c
  22. * \brief Source: S-Lang-specific color setup
  23. */
  24. #include <config.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/types.h> /* size_t */
  29. #include "lib/global.h"
  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. char *s;
  51. size_t i;
  52. /* check mc_global.tty.color_terminal_string */
  53. while (*cts != '\0')
  54. {
  55. while (*cts == ' ' || *cts == '\t')
  56. cts++;
  57. s = cts;
  58. i = 0;
  59. while (*cts != '\0' && *cts != ',')
  60. {
  61. cts++;
  62. i++;
  63. }
  64. if ((i != 0) && (i == len) && (strncmp (s, terminal, i) == 0))
  65. SLtt_Use_Ansi_Colors = 1;
  66. if (*cts == ',')
  67. cts++;
  68. }
  69. }
  70. return SLtt_Use_Ansi_Colors;
  71. }
  72. /* --------------------------------------------------------------------------------------------- */
  73. static void
  74. mc_tty_color_pair_init_special (tty_color_pair_t * mc_color_pair,
  75. const char *fg1, const char *bg1,
  76. const char *fg2, const char *bg2, SLtt_Char_Type mask)
  77. {
  78. if (SLtt_Use_Ansi_Colors != 0)
  79. {
  80. if (!mc_tty_color_disable)
  81. {
  82. SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg1, (char *) bg1);
  83. }
  84. else
  85. {
  86. SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg2, (char *) bg2);
  87. }
  88. }
  89. else
  90. {
  91. SLtt_set_mono (mc_color_pair->pair_index, NULL, mask);
  92. }
  93. }
  94. /* --------------------------------------------------------------------------------------------- */
  95. /*** public functions ****************************************************************************/
  96. /* --------------------------------------------------------------------------------------------- */
  97. void
  98. tty_color_init_lib (gboolean disable, gboolean force)
  99. {
  100. /* FIXME: if S-Lang is used, has_colors() must be called regardless
  101. of whether we are interested in its result */
  102. if (has_colors (disable, force) && !disable)
  103. {
  104. use_colors = TRUE;
  105. }
  106. }
  107. /* --------------------------------------------------------------------------------------------- */
  108. void
  109. tty_color_deinit_lib (void)
  110. {
  111. }
  112. /* --------------------------------------------------------------------------------------------- */
  113. void
  114. tty_color_try_alloc_pair_lib (tty_color_pair_t * mc_color_pair)
  115. {
  116. const char *fg, *bg;
  117. if (mc_color_pair->ifg <= (int) SPEC_A_REVERSE)
  118. {
  119. switch (mc_color_pair->ifg)
  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. }
  139. }
  140. else
  141. {
  142. fg = tty_color_get_name_by_index (mc_color_pair->ifg);
  143. bg = tty_color_get_name_by_index (mc_color_pair->ibg);
  144. SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg, (char *) bg);
  145. SLtt_add_color_attribute (mc_color_pair->pair_index, mc_color_pair->attr);
  146. }
  147. }
  148. /* --------------------------------------------------------------------------------------------- */
  149. void
  150. tty_setcolor (int color)
  151. {
  152. SLsmg_set_color (color);
  153. }
  154. /* --------------------------------------------------------------------------------------------- */
  155. /**
  156. * Set colorpair by index, don't interpret S-Lang "emulated attributes"
  157. */
  158. void
  159. tty_lowlevel_setcolor (int color)
  160. {
  161. SLsmg_set_color (color & 0x7F);
  162. }
  163. /* --------------------------------------------------------------------------------------------- */
  164. void
  165. tty_set_normal_attrs (void)
  166. {
  167. SLsmg_normal_video ();
  168. }
  169. /* --------------------------------------------------------------------------------------------- */
  170. gboolean
  171. tty_use_256colors (void)
  172. {
  173. return (SLtt_Use_Ansi_Colors && SLtt_tgetnum ((char *) "Co") == 256);
  174. }
  175. /* --------------------------------------------------------------------------------------------- */