color-ncurses.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* Color setup for NCurses 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-ncurses.c
  18. * \brief Source: NCUrses-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-ncurses.h"
  27. #include "color.h" /* variables */
  28. #include "color-internal.h"
  29. /*** global variables ****************************************************************************/
  30. /*** file scope macro definitions ****************************************************************/
  31. /*** file scope type declarations ****************************************************************/
  32. /*** file scope variables ************************************************************************/
  33. static GHashTable *mc_tty_color_color_pair_attrs = NULL;
  34. /*** file scope functions ************************************************************************/
  35. /* --------------------------------------------------------------------------------------------- */
  36. static inline void
  37. mc_tty_color_attr_destroy_cb (gpointer data)
  38. {
  39. g_free (data);
  40. }
  41. /* --------------------------------------------------------------------------------------------- */
  42. static int
  43. mc_tty_color_save_attr_lib (int color_pair, int color_attr)
  44. {
  45. int *attr, *key;
  46. attr = g_try_new0 (int, 1);
  47. if (attr == NULL)
  48. return color_attr;
  49. key = g_try_new (int, 1);
  50. if (key == NULL)
  51. {
  52. g_free (attr);
  53. return color_attr;
  54. }
  55. *key = color_pair;
  56. if (color_attr != -1)
  57. *attr = color_attr & (A_BOLD | A_REVERSE | A_UNDERLINE);
  58. g_hash_table_replace (mc_tty_color_color_pair_attrs, (gpointer) key, (gpointer) attr);
  59. return color_attr & (~(*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 mask)
  74. {
  75. if (has_colors () && !mc_tty_color_disable)
  76. init_pair (mc_color_pair->pair_index,
  77. mc_tty_color_save_attr_lib (mc_color_pair->pair_index, fg1 | mask), bg1);
  78. else
  79. init_pair (mc_color_pair->pair_index,
  80. mc_tty_color_save_attr_lib (mc_color_pair->pair_index, fg2 | mask), bg2);
  81. }
  82. /* --------------------------------------------------------------------------------------------- */
  83. /*** public functions ****************************************************************************/
  84. /* --------------------------------------------------------------------------------------------- */
  85. void
  86. tty_color_init_lib (gboolean disable, gboolean force)
  87. {
  88. (void) force;
  89. if (has_colors () && !disable)
  90. {
  91. use_colors = TRUE;
  92. start_color ();
  93. use_default_colors ();
  94. }
  95. mc_tty_color_color_pair_attrs = g_hash_table_new_full
  96. (g_int_hash, g_int_equal, mc_tty_color_attr_destroy_cb, mc_tty_color_attr_destroy_cb);
  97. }
  98. /* --------------------------------------------------------------------------------------------- */
  99. void
  100. tty_color_deinit_lib (void)
  101. {
  102. g_hash_table_destroy (mc_tty_color_color_pair_attrs);
  103. mc_tty_color_color_pair_attrs = NULL;
  104. }
  105. /* --------------------------------------------------------------------------------------------- */
  106. void
  107. tty_color_try_alloc_pair_lib (tty_color_pair_t * mc_color_pair)
  108. {
  109. if (mc_color_pair->ifg <= (int) SPEC_A_REVERSE)
  110. {
  111. switch (mc_color_pair->ifg)
  112. {
  113. case SPEC_A_REVERSE:
  114. mc_tty_color_pair_init_special (mc_color_pair,
  115. COLOR_BLACK, COLOR_WHITE,
  116. COLOR_BLACK, COLOR_WHITE | A_BOLD, A_REVERSE);
  117. break;
  118. case SPEC_A_BOLD:
  119. mc_tty_color_pair_init_special (mc_color_pair,
  120. COLOR_WHITE, COLOR_BLACK,
  121. COLOR_WHITE, COLOR_BLACK, A_BOLD);
  122. break;
  123. case SPEC_A_BOLD_REVERSE:
  124. mc_tty_color_pair_init_special (mc_color_pair,
  125. COLOR_WHITE, COLOR_WHITE,
  126. COLOR_WHITE, COLOR_WHITE, A_BOLD | A_REVERSE);
  127. break;
  128. case SPEC_A_UNDERLINE:
  129. mc_tty_color_pair_init_special (mc_color_pair,
  130. COLOR_WHITE, COLOR_BLACK,
  131. COLOR_WHITE, COLOR_BLACK, A_UNDERLINE);
  132. break;
  133. }
  134. }
  135. else
  136. {
  137. int mask_fg = (mc_color_pair->ifg == -1) ? mc_color_pair->ifg : 0xff;
  138. int mask_bg = (mc_color_pair->ibg == -1) ? mc_color_pair->ibg : 0xff;
  139. init_pair (mc_color_pair->pair_index,
  140. mc_tty_color_save_attr_lib (mc_color_pair->pair_index,
  141. mc_color_pair->ifg) & mask_fg,
  142. mc_color_pair->ibg & mask_bg);
  143. }
  144. }
  145. /* --------------------------------------------------------------------------------------------- */
  146. void
  147. tty_setcolor (int color)
  148. {
  149. attrset (COLOR_PAIR (color) | color_get_attr (color));
  150. }
  151. /* --------------------------------------------------------------------------------------------- */
  152. void
  153. tty_lowlevel_setcolor (int color)
  154. {
  155. attrset (COLOR_PAIR (color) | color_get_attr (color));
  156. }
  157. /* --------------------------------------------------------------------------------------------- */
  158. void
  159. tty_set_normal_attrs (void)
  160. {
  161. standend ();
  162. }
  163. /* --------------------------------------------------------------------------------------------- */