color-ncurses.c 7.6 KB

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