color-ncurses.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. *attr = color_attr;
  58. g_hash_table_replace (mc_tty_color_color_pair_attrs, (gpointer) key, (gpointer) attr);
  59. }
  60. /* --------------------------------------------------------------------------------------------- */
  61. static int
  62. color_get_attr (int color_pair)
  63. {
  64. int *fnd = NULL;
  65. if (mc_tty_color_color_pair_attrs != NULL)
  66. fnd = (int *) g_hash_table_lookup (mc_tty_color_color_pair_attrs, (gpointer) & color_pair);
  67. return (fnd != NULL) ? *fnd : 0;
  68. }
  69. /* --------------------------------------------------------------------------------------------- */
  70. static void
  71. mc_tty_color_pair_init_special (tty_color_pair_t * mc_color_pair,
  72. int fg1, int bg1, int fg2, int bg2, int attr)
  73. {
  74. if (has_colors () && !mc_tty_color_disable)
  75. init_pair (mc_color_pair->pair_index, fg1, bg1);
  76. else
  77. init_pair (mc_color_pair->pair_index, fg2, bg2);
  78. mc_tty_color_save_attr (mc_color_pair->pair_index, attr);
  79. }
  80. /* --------------------------------------------------------------------------------------------- */
  81. /*** public functions ****************************************************************************/
  82. /* --------------------------------------------------------------------------------------------- */
  83. void
  84. tty_color_init_lib (gboolean disable, gboolean force)
  85. {
  86. (void) force;
  87. if (has_colors () && !disable)
  88. {
  89. use_colors = TRUE;
  90. start_color ();
  91. use_default_colors ();
  92. }
  93. mc_tty_color_color_pair_attrs = g_hash_table_new_full
  94. (g_int_hash, g_int_equal, mc_tty_color_attr_destroy_cb, mc_tty_color_attr_destroy_cb);
  95. }
  96. /* --------------------------------------------------------------------------------------------- */
  97. void
  98. tty_color_deinit_lib (void)
  99. {
  100. g_hash_table_destroy (mc_tty_color_color_pair_attrs);
  101. mc_tty_color_color_pair_attrs = NULL;
  102. }
  103. /* --------------------------------------------------------------------------------------------- */
  104. void
  105. tty_color_try_alloc_pair_lib (tty_color_pair_t * mc_color_pair)
  106. {
  107. if (mc_color_pair->ifg <= (int) SPEC_A_REVERSE)
  108. {
  109. switch (mc_color_pair->ifg)
  110. {
  111. case SPEC_A_REVERSE:
  112. mc_tty_color_pair_init_special (mc_color_pair,
  113. COLOR_BLACK, COLOR_WHITE,
  114. COLOR_BLACK, COLOR_WHITE | A_BOLD, A_REVERSE);
  115. break;
  116. case SPEC_A_BOLD:
  117. mc_tty_color_pair_init_special (mc_color_pair,
  118. COLOR_WHITE, COLOR_BLACK,
  119. COLOR_WHITE, COLOR_BLACK, A_BOLD);
  120. break;
  121. case SPEC_A_BOLD_REVERSE:
  122. mc_tty_color_pair_init_special (mc_color_pair,
  123. COLOR_WHITE, COLOR_WHITE,
  124. COLOR_WHITE, COLOR_WHITE, A_BOLD | A_REVERSE);
  125. break;
  126. case SPEC_A_UNDERLINE:
  127. mc_tty_color_pair_init_special (mc_color_pair,
  128. COLOR_WHITE, COLOR_BLACK,
  129. COLOR_WHITE, COLOR_BLACK, A_UNDERLINE);
  130. break;
  131. }
  132. }
  133. else
  134. {
  135. int ifg, ibg, attr;
  136. ifg = mc_color_pair->ifg;
  137. ibg = mc_color_pair->ibg;
  138. attr = mc_color_pair->attr;
  139. /* In 8 color mode, change bright colors into bold */
  140. if (COLORS == 8 && ifg >= 8 && ifg < 16)
  141. {
  142. ifg &= 0x07;
  143. attr |= A_BOLD;
  144. }
  145. init_pair (mc_color_pair->pair_index, ifg, ibg);
  146. mc_tty_color_save_attr (mc_color_pair->pair_index, attr);
  147. }
  148. }
  149. /* --------------------------------------------------------------------------------------------- */
  150. void
  151. tty_setcolor (int color)
  152. {
  153. attrset (COLOR_PAIR (color) | color_get_attr (color));
  154. }
  155. /* --------------------------------------------------------------------------------------------- */
  156. void
  157. tty_lowlevel_setcolor (int color)
  158. {
  159. attrset (COLOR_PAIR (color) | color_get_attr (color));
  160. }
  161. /* --------------------------------------------------------------------------------------------- */
  162. void
  163. tty_set_normal_attrs (void)
  164. {
  165. standend ();
  166. }
  167. /* --------------------------------------------------------------------------------------------- */