color-ncurses.c 7.0 KB

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