color-slang.c 6.6 KB

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