color-ncurses.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. Color setup for NCurses screen library
  3. Copyright (C) 1994-2017
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Andrew Borodin <aborodin@vmail.ru>, 2009
  7. Slava Zanko <slavazanko@gmail.com>, 2010
  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-ncurses.c
  22. * \brief Source: NCUrses-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-ncurses.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. static GHashTable *mc_tty_color_color_pair_attrs = NULL;
  38. /*** file scope functions ************************************************************************/
  39. /* --------------------------------------------------------------------------------------------- */
  40. static inline void
  41. mc_tty_color_attr_destroy_cb (gpointer data)
  42. {
  43. g_free (data);
  44. }
  45. /* --------------------------------------------------------------------------------------------- */
  46. static void
  47. mc_tty_color_save_attr (int color_pair, int color_attr)
  48. {
  49. int *attr, *key;
  50. attr = g_try_new0 (int, 1);
  51. if (attr == NULL)
  52. return;
  53. key = g_try_new (int, 1);
  54. if (key == NULL)
  55. {
  56. g_free (attr);
  57. return;
  58. }
  59. *key = color_pair;
  60. *attr = color_attr;
  61. g_hash_table_replace (mc_tty_color_color_pair_attrs, (gpointer) key, (gpointer) attr);
  62. }
  63. /* --------------------------------------------------------------------------------------------- */
  64. static int
  65. color_get_attr (int color_pair)
  66. {
  67. int *fnd = NULL;
  68. if (mc_tty_color_color_pair_attrs != NULL)
  69. fnd = (int *) g_hash_table_lookup (mc_tty_color_color_pair_attrs, (gpointer) & color_pair);
  70. return (fnd != NULL) ? *fnd : 0;
  71. }
  72. /* --------------------------------------------------------------------------------------------- */
  73. static void
  74. mc_tty_color_pair_init_special (tty_color_pair_t * mc_color_pair,
  75. int fg1, int bg1, int fg2, int bg2, int attr)
  76. {
  77. if (has_colors () && !mc_tty_color_disable)
  78. init_pair (mc_color_pair->pair_index, fg1, bg1);
  79. else
  80. init_pair (mc_color_pair->pair_index, fg2, bg2);
  81. mc_tty_color_save_attr (mc_color_pair->pair_index, attr);
  82. }
  83. /* --------------------------------------------------------------------------------------------- */
  84. /*** public functions ****************************************************************************/
  85. /* --------------------------------------------------------------------------------------------- */
  86. void
  87. tty_color_init_lib (gboolean disable, gboolean force)
  88. {
  89. (void) force;
  90. if (has_colors () && !disable)
  91. {
  92. use_colors = TRUE;
  93. start_color ();
  94. use_default_colors ();
  95. }
  96. mc_tty_color_color_pair_attrs = g_hash_table_new_full
  97. (g_int_hash, g_int_equal, mc_tty_color_attr_destroy_cb, mc_tty_color_attr_destroy_cb);
  98. }
  99. /* --------------------------------------------------------------------------------------------- */
  100. void
  101. tty_color_deinit_lib (void)
  102. {
  103. g_hash_table_destroy (mc_tty_color_color_pair_attrs);
  104. mc_tty_color_color_pair_attrs = NULL;
  105. }
  106. /* --------------------------------------------------------------------------------------------- */
  107. void
  108. tty_color_try_alloc_pair_lib (tty_color_pair_t * mc_color_pair)
  109. {
  110. if (mc_color_pair->ifg <= (int) SPEC_A_REVERSE)
  111. {
  112. switch (mc_color_pair->ifg)
  113. {
  114. case SPEC_A_REVERSE:
  115. mc_tty_color_pair_init_special (mc_color_pair,
  116. COLOR_BLACK, COLOR_WHITE,
  117. COLOR_BLACK, COLOR_WHITE | A_BOLD, A_REVERSE);
  118. break;
  119. case SPEC_A_BOLD:
  120. mc_tty_color_pair_init_special (mc_color_pair,
  121. COLOR_WHITE, COLOR_BLACK,
  122. COLOR_WHITE, COLOR_BLACK, A_BOLD);
  123. break;
  124. case SPEC_A_BOLD_REVERSE:
  125. mc_tty_color_pair_init_special (mc_color_pair,
  126. COLOR_WHITE, COLOR_WHITE,
  127. COLOR_WHITE, COLOR_WHITE, A_BOLD | A_REVERSE);
  128. break;
  129. case SPEC_A_UNDERLINE:
  130. mc_tty_color_pair_init_special (mc_color_pair,
  131. COLOR_WHITE, COLOR_BLACK,
  132. COLOR_WHITE, COLOR_BLACK, A_UNDERLINE);
  133. break;
  134. default:
  135. break;
  136. }
  137. }
  138. else
  139. {
  140. int ifg, ibg, attr;
  141. ifg = mc_color_pair->ifg;
  142. ibg = mc_color_pair->ibg;
  143. attr = mc_color_pair->attr;
  144. /* In legacy color mode, change bright colors into bold */
  145. if (!tty_use_256colors () && !tty_use_truecolors (NULL))
  146. {
  147. if (ifg >= 8 && ifg < 16)
  148. {
  149. ifg &= 0x07;
  150. attr |= A_BOLD;
  151. }
  152. if (ibg >= 8 && ibg < 16)
  153. {
  154. ibg &= 0x07;
  155. /* attr | = A_BOLD | A_REVERSE ; */
  156. }
  157. }
  158. init_pair (mc_color_pair->pair_index, ifg, ibg);
  159. mc_tty_color_save_attr (mc_color_pair->pair_index, attr);
  160. }
  161. }
  162. /* --------------------------------------------------------------------------------------------- */
  163. void
  164. tty_setcolor (int color)
  165. {
  166. attrset (COLOR_PAIR (color) | color_get_attr (color));
  167. }
  168. /* --------------------------------------------------------------------------------------------- */
  169. void
  170. tty_lowlevel_setcolor (int color)
  171. {
  172. tty_setcolor (color);
  173. }
  174. /* --------------------------------------------------------------------------------------------- */
  175. void
  176. tty_set_normal_attrs (void)
  177. {
  178. standend ();
  179. }
  180. /* --------------------------------------------------------------------------------------------- */
  181. gboolean
  182. tty_use_256colors (void)
  183. {
  184. return (COLORS == 256);
  185. }
  186. /* --------------------------------------------------------------------------------------------- */
  187. gboolean
  188. tty_use_truecolors (GError ** error)
  189. {
  190. /* Not yet supported in ncurses */
  191. g_set_error (error, MC_ERROR, -1, _("True color not supported with ncurses."));
  192. return FALSE;
  193. }
  194. /* --------------------------------------------------------------------------------------------- */