color-ncurses.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. Color setup for NCurses 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. Slava Zanko <slavazanko@gmail.com>, 2010
  9. Egmont Koblinger <egmont@gmail.com>, 2010
  10. This file is part of the Midnight Commander.
  11. The Midnight Commander is free software: you can redistribute it
  12. and/or modify it under the terms of the GNU General Public License as
  13. published by the Free Software Foundation, either version 3 of the License,
  14. or (at your option) any later version.
  15. The Midnight Commander is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /** \file color-ncurses.c
  23. * \brief Source: NCUrses-specific color setup
  24. */
  25. #include <config.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <sys/types.h> /* size_t */
  30. #include "lib/global.h"
  31. #include "tty-ncurses.h"
  32. #include "color.h" /* variables */
  33. #include "color-internal.h"
  34. /*** global variables ****************************************************************************/
  35. /*** file scope macro definitions ****************************************************************/
  36. /*** file scope type declarations ****************************************************************/
  37. /*** file scope variables ************************************************************************/
  38. static GHashTable *mc_tty_color_color_pair_attrs = NULL;
  39. /*** file scope functions ************************************************************************/
  40. /* --------------------------------------------------------------------------------------------- */
  41. static inline void
  42. mc_tty_color_attr_destroy_cb (gpointer data)
  43. {
  44. g_free (data);
  45. }
  46. /* --------------------------------------------------------------------------------------------- */
  47. static void
  48. mc_tty_color_save_attr (int color_pair, int color_attr)
  49. {
  50. int *attr, *key;
  51. attr = g_try_new0 (int, 1);
  52. if (attr == NULL)
  53. return;
  54. key = g_try_new (int, 1);
  55. if (key == NULL)
  56. {
  57. g_free (attr);
  58. return;
  59. }
  60. *key = color_pair;
  61. *attr = color_attr;
  62. g_hash_table_replace (mc_tty_color_color_pair_attrs, (gpointer) key, (gpointer) attr);
  63. }
  64. /* --------------------------------------------------------------------------------------------- */
  65. static int
  66. color_get_attr (int color_pair)
  67. {
  68. int *fnd = NULL;
  69. if (mc_tty_color_color_pair_attrs != NULL)
  70. fnd = (int *) g_hash_table_lookup (mc_tty_color_color_pair_attrs, (gpointer) & color_pair);
  71. return (fnd != NULL) ? *fnd : 0;
  72. }
  73. /* --------------------------------------------------------------------------------------------- */
  74. static void
  75. mc_tty_color_pair_init_special (tty_color_pair_t * mc_color_pair,
  76. int fg1, int bg1, int fg2, int bg2, int attr)
  77. {
  78. if (has_colors () && !mc_tty_color_disable)
  79. init_pair (mc_color_pair->pair_index, fg1, bg1);
  80. else
  81. init_pair (mc_color_pair->pair_index, fg2, bg2);
  82. mc_tty_color_save_attr (mc_color_pair->pair_index, attr);
  83. }
  84. /* --------------------------------------------------------------------------------------------- */
  85. /*** public functions ****************************************************************************/
  86. /* --------------------------------------------------------------------------------------------- */
  87. void
  88. tty_color_init_lib (gboolean disable, gboolean force)
  89. {
  90. (void) force;
  91. if (has_colors () && !disable)
  92. {
  93. use_colors = TRUE;
  94. start_color ();
  95. use_default_colors ();
  96. }
  97. mc_tty_color_color_pair_attrs = g_hash_table_new_full
  98. (g_int_hash, g_int_equal, mc_tty_color_attr_destroy_cb, mc_tty_color_attr_destroy_cb);
  99. }
  100. /* --------------------------------------------------------------------------------------------- */
  101. void
  102. tty_color_deinit_lib (void)
  103. {
  104. g_hash_table_destroy (mc_tty_color_color_pair_attrs);
  105. mc_tty_color_color_pair_attrs = NULL;
  106. }
  107. /* --------------------------------------------------------------------------------------------- */
  108. void
  109. tty_color_try_alloc_pair_lib (tty_color_pair_t * mc_color_pair)
  110. {
  111. if (mc_color_pair->ifg <= (int) SPEC_A_REVERSE)
  112. {
  113. switch (mc_color_pair->ifg)
  114. {
  115. case SPEC_A_REVERSE:
  116. mc_tty_color_pair_init_special (mc_color_pair,
  117. COLOR_BLACK, COLOR_WHITE,
  118. COLOR_BLACK, COLOR_WHITE | A_BOLD, A_REVERSE);
  119. break;
  120. case SPEC_A_BOLD:
  121. mc_tty_color_pair_init_special (mc_color_pair,
  122. COLOR_WHITE, COLOR_BLACK,
  123. COLOR_WHITE, COLOR_BLACK, A_BOLD);
  124. break;
  125. case SPEC_A_BOLD_REVERSE:
  126. mc_tty_color_pair_init_special (mc_color_pair,
  127. COLOR_WHITE, COLOR_WHITE,
  128. COLOR_WHITE, COLOR_WHITE, A_BOLD | A_REVERSE);
  129. break;
  130. case SPEC_A_UNDERLINE:
  131. mc_tty_color_pair_init_special (mc_color_pair,
  132. COLOR_WHITE, COLOR_BLACK,
  133. COLOR_WHITE, COLOR_BLACK, A_UNDERLINE);
  134. break;
  135. }
  136. }
  137. else
  138. {
  139. int ifg, ibg, attr;
  140. ifg = mc_color_pair->ifg;
  141. ibg = mc_color_pair->ibg;
  142. attr = mc_color_pair->attr;
  143. /* In non-256 color mode, change bright colors into bold */
  144. if (!tty_use_256colors ())
  145. {
  146. if (ifg >= 8 && ifg < 16)
  147. {
  148. ifg &= 0x07;
  149. attr |= A_BOLD;
  150. }
  151. if (ibg >= 8 && ibg < 16)
  152. {
  153. ibg &= 0x07;
  154. /* attr | = A_BOLD | A_REVERSE ; */
  155. }
  156. }
  157. init_pair (mc_color_pair->pair_index, ifg, ibg);
  158. mc_tty_color_save_attr (mc_color_pair->pair_index, attr);
  159. }
  160. }
  161. /* --------------------------------------------------------------------------------------------- */
  162. void
  163. tty_setcolor (int color)
  164. {
  165. attrset (COLOR_PAIR (color) | color_get_attr (color));
  166. }
  167. /* --------------------------------------------------------------------------------------------- */
  168. void
  169. tty_lowlevel_setcolor (int color)
  170. {
  171. tty_setcolor (color);
  172. }
  173. /* --------------------------------------------------------------------------------------------- */
  174. void
  175. tty_set_normal_attrs (void)
  176. {
  177. standend ();
  178. }
  179. /* --------------------------------------------------------------------------------------------- */
  180. gboolean
  181. tty_use_256colors (void)
  182. {
  183. return (COLORS == 256);
  184. }
  185. /* --------------------------------------------------------------------------------------------- */