color-ncurses.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. g_free (attr);
  52. return color_attr;
  53. }
  54. *key = color_pair;
  55. if (color_attr != -1)
  56. *attr = color_attr & (A_BOLD | A_REVERSE | A_UNDERLINE);
  57. g_hash_table_replace (mc_tty_color_color_pair_attrs, (gpointer) key, (gpointer) attr);
  58. return color_attr & (~(*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 mask)
  73. {
  74. if (has_colors () && !mc_tty_color_disable)
  75. init_pair (mc_color_pair->pair_index,
  76. mc_tty_color_save_attr_lib (mc_color_pair->pair_index, fg1 | mask), bg1);
  77. else
  78. init_pair (mc_color_pair->pair_index,
  79. mc_tty_color_save_attr_lib (mc_color_pair->pair_index, fg2 | mask), bg2);
  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. 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. switch (mc_color_pair->ifg) {
  109. case SPEC_A_REVERSE:
  110. mc_tty_color_pair_init_special (mc_color_pair,
  111. COLOR_BLACK, COLOR_WHITE,
  112. COLOR_BLACK, COLOR_WHITE | A_BOLD, A_REVERSE);
  113. break;
  114. case SPEC_A_BOLD:
  115. mc_tty_color_pair_init_special (mc_color_pair,
  116. COLOR_WHITE, COLOR_BLACK,
  117. COLOR_WHITE, COLOR_BLACK, A_BOLD);
  118. break;
  119. case SPEC_A_BOLD_REVERSE:
  120. mc_tty_color_pair_init_special (mc_color_pair,
  121. COLOR_WHITE, COLOR_WHITE,
  122. COLOR_WHITE, COLOR_WHITE, A_BOLD | A_REVERSE);
  123. break;
  124. case SPEC_A_UNDERLINE:
  125. mc_tty_color_pair_init_special (mc_color_pair,
  126. COLOR_WHITE, COLOR_BLACK,
  127. COLOR_WHITE, COLOR_BLACK, A_UNDERLINE);
  128. break;
  129. }
  130. } else {
  131. int mask_fg = (mc_color_pair->ifg == -1) ? mc_color_pair->ifg : 0xff;
  132. int mask_bg = (mc_color_pair->ibg == -1) ? mc_color_pair->ibg : 0xff;
  133. init_pair (mc_color_pair->pair_index,
  134. mc_tty_color_save_attr_lib (mc_color_pair->pair_index,
  135. mc_color_pair->ifg) & mask_fg,
  136. mc_color_pair->ibg & mask_bg);
  137. }
  138. }
  139. /* --------------------------------------------------------------------------------------------- */
  140. void
  141. tty_setcolor (int color)
  142. {
  143. attrset (COLOR_PAIR (color) | color_get_attr (color));
  144. }
  145. /* --------------------------------------------------------------------------------------------- */
  146. void
  147. tty_lowlevel_setcolor (int color)
  148. {
  149. attrset (COLOR_PAIR (color) | color_get_attr (color));
  150. }
  151. /* --------------------------------------------------------------------------------------------- */
  152. void
  153. tty_set_normal_attrs (void)
  154. {
  155. standend ();
  156. }
  157. /* --------------------------------------------------------------------------------------------- */