color-slang.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* Color setup for S_Lang screen library
  2. Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  3. 2007, 2008, 2009 Free Software Foundation, Inc.
  4. Written by:
  5. Andrew Borodin <aborodin@vmail.ru>, 2009.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  17. /** \file color-slang.c
  18. * \brief Source: S-Lang-specific color setup
  19. */
  20. #include <config.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/types.h> /* size_t */
  25. #include "lib/global.h"
  26. #include "tty-slang.h"
  27. #include "color.h" /* variables */
  28. #include "color-internal.h"
  29. #include "src/setup.h" /* color_terminal_string */
  30. static int
  31. has_colors (gboolean disable, gboolean force)
  32. {
  33. mc_tty_color_disable = disable;
  34. if (force || (getenv ("COLORTERM") != NULL))
  35. SLtt_Use_Ansi_Colors = 1;
  36. if (!mc_tty_color_disable) {
  37. const char *terminal = getenv ("TERM");
  38. const size_t len = strlen (terminal);
  39. char *cts = color_terminal_string;
  40. char *s;
  41. size_t i;
  42. /* check color_terminal_string */
  43. while (*cts != '\0') {
  44. while (*cts == ' ' || *cts == '\t')
  45. cts++;
  46. s = cts;
  47. i = 0;
  48. while (*cts != '\0' && *cts != ',') {
  49. cts++;
  50. i++;
  51. }
  52. if ((i != 0) && (i == len) && (strncmp (s, terminal, i) == 0))
  53. SLtt_Use_Ansi_Colors = 1;
  54. if (*cts == ',')
  55. cts++;
  56. }
  57. }
  58. return SLtt_Use_Ansi_Colors;
  59. }
  60. static void
  61. mc_tty_color_pair_init_special (tty_color_pair_t * mc_color_pair,
  62. const char *fg1, const char *bg1,
  63. const char *fg2, const char *bg2, SLtt_Char_Type mask)
  64. {
  65. if (SLtt_Use_Ansi_Colors != 0) {
  66. if (!mc_tty_color_disable) {
  67. SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg1, (char *) bg1);
  68. } else {
  69. SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg2, (char *) bg2);
  70. }
  71. } else {
  72. SLtt_set_mono (mc_color_pair->pair_index, NULL, mask);
  73. }
  74. }
  75. void
  76. tty_color_init_lib (gboolean disable, gboolean force)
  77. {
  78. /* FIXME: if S-Lang is used, has_colors() must be called regardless
  79. of whether we are interested in its result */
  80. if (has_colors (disable, force) && !disable) {
  81. use_colors = TRUE;
  82. }
  83. }
  84. void
  85. tty_color_deinit_lib (void)
  86. {
  87. }
  88. void
  89. tty_color_try_alloc_pair_lib (tty_color_pair_t * mc_color_pair)
  90. {
  91. const char *fg, *bg;
  92. if (mc_color_pair->ifg <= (int) SPEC_A_REVERSE) {
  93. switch (mc_color_pair->ifg) {
  94. case SPEC_A_REVERSE:
  95. mc_tty_color_pair_init_special (mc_color_pair,
  96. "black", "white", "black", "lightgray", SLTT_REV_MASK);
  97. break;
  98. case SPEC_A_BOLD:
  99. mc_tty_color_pair_init_special (mc_color_pair,
  100. "white", "black", "white", "black", SLTT_BOLD_MASK);
  101. break;
  102. case SPEC_A_BOLD_REVERSE:
  103. mc_tty_color_pair_init_special (mc_color_pair,
  104. "white", "white",
  105. "white", "white", SLTT_BOLD_MASK | SLTT_REV_MASK);
  106. break;
  107. case SPEC_A_UNDERLINE:
  108. mc_tty_color_pair_init_special (mc_color_pair,
  109. "white", "black", "white", "black", SLTT_ULINE_MASK);
  110. break;
  111. }
  112. } else {
  113. fg = (mc_color_pair->cfg) ? mc_color_pair->cfg : "default";
  114. bg = (mc_color_pair->cbg) ? mc_color_pair->cbg : "default";
  115. SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg, (char *) bg);
  116. }
  117. }
  118. void
  119. tty_setcolor (int color)
  120. {
  121. SLsmg_set_color (color);
  122. }
  123. /* Set colorpair by index, don't interpret S-Lang "emulated attributes" */
  124. void
  125. tty_lowlevel_setcolor (int color)
  126. {
  127. SLsmg_set_color (color & 0x7F);
  128. }
  129. void
  130. tty_set_normal_attrs (void)
  131. {
  132. SLsmg_normal_video ();
  133. }