color.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. Color setup.
  3. Interface functions.
  4. Copyright (C) 1994-2024
  5. Free Software Foundation, Inc.
  6. Written by:
  7. Andrew Borodin <aborodin@vmail.ru>, 2009
  8. Slava Zanko <slavazanko@gmail.com>, 2009
  9. Egmont Koblinger <egmont@gmail.com>, 2010
  10. This file is part of the Midnight Commander.
  11. The Midnight Commander is free software: you can redistribute it
  12. and/or modify it under the terms of the GNU General Public License as
  13. published by the Free Software Foundation, either version 3 of the License,
  14. or (at your option) any later version.
  15. The Midnight Commander is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /** \file color.c
  23. * \brief Source: color setup
  24. */
  25. #include <config.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <sys/types.h> /* size_t */
  30. #include "lib/global.h"
  31. #include "tty.h"
  32. #include "color.h"
  33. #include "color-internal.h"
  34. /*** global variables ****************************************************************************/
  35. /* *INDENT-OFF* */
  36. static tty_color_pair_t tty_color_defaults =
  37. {
  38. .fg = NULL,
  39. .bg = NULL,
  40. .attrs = NULL,
  41. .pair_index = 0
  42. };
  43. /* *INDENT-ON* */
  44. /* Set if we are actually using colors */
  45. gboolean use_colors = FALSE;
  46. /*** file scope macro definitions ****************************************************************/
  47. /*** file scope type declarations ****************************************************************/
  48. /*** forward declarations (file scope functions) *************************************************/
  49. /*** file scope variables ************************************************************************/
  50. static GHashTable *mc_tty_color__hashtable = NULL;
  51. /* --------------------------------------------------------------------------------------------- */
  52. /*** file scope functions ************************************************************************/
  53. /* --------------------------------------------------------------------------------------------- */
  54. static void
  55. mc_color__deinit (tty_color_pair_t *color)
  56. {
  57. g_free (color->fg);
  58. g_free (color->bg);
  59. g_free (color->attrs);
  60. }
  61. /* --------------------------------------------------------------------------------------------- */
  62. static gboolean
  63. tty_color_free_temp_cb (gpointer key, gpointer value, gpointer user_data)
  64. {
  65. (void) key;
  66. (void) user_data;
  67. return ((tty_color_lib_pair_t *) value)->is_temp;
  68. }
  69. /* --------------------------------------------------------------------------------------------- */
  70. static gboolean
  71. tty_color_get_next_cpn_cb (gpointer key, gpointer value, gpointer user_data)
  72. {
  73. tty_color_lib_pair_t *mc_color_pair = (tty_color_lib_pair_t *) value;
  74. size_t cp = GPOINTER_TO_SIZE (user_data);
  75. (void) key;
  76. return (cp == mc_color_pair->pair_index);
  77. }
  78. /* --------------------------------------------------------------------------------------------- */
  79. static size_t
  80. tty_color_get_next__color_pair_number (void)
  81. {
  82. size_t cp_count, cp;
  83. cp_count = g_hash_table_size (mc_tty_color__hashtable);
  84. for (cp = 0; cp < cp_count; cp++)
  85. if (g_hash_table_find (mc_tty_color__hashtable, tty_color_get_next_cpn_cb,
  86. GSIZE_TO_POINTER (cp)) == NULL)
  87. break;
  88. return cp;
  89. }
  90. /* --------------------------------------------------------------------------------------------- */
  91. /*** public functions ****************************************************************************/
  92. /* --------------------------------------------------------------------------------------------- */
  93. void
  94. tty_init_colors (gboolean disable, gboolean force)
  95. {
  96. tty_color_init_lib (disable, force);
  97. mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
  98. }
  99. /* --------------------------------------------------------------------------------------------- */
  100. void
  101. tty_colors_done (void)
  102. {
  103. tty_color_deinit_lib ();
  104. mc_color__deinit (&tty_color_defaults);
  105. g_hash_table_destroy (mc_tty_color__hashtable);
  106. }
  107. /* --------------------------------------------------------------------------------------------- */
  108. gboolean
  109. tty_use_colors (void)
  110. {
  111. return use_colors;
  112. }
  113. /* --------------------------------------------------------------------------------------------- */
  114. int
  115. tty_try_alloc_color_pair (const tty_color_pair_t *color, gboolean is_temp)
  116. {
  117. gboolean is_base;
  118. gchar *color_pair;
  119. tty_color_lib_pair_t *mc_color_pair;
  120. int ifg, ibg, attr;
  121. is_base = (color->fg == NULL || strcmp (color->fg, "base") == 0);
  122. ifg = tty_color_get_index_by_name (is_base ? tty_color_defaults.fg : color->fg);
  123. is_base = (color->bg == NULL || strcmp (color->bg, "base") == 0);
  124. ibg = tty_color_get_index_by_name (is_base ? tty_color_defaults.bg : color->bg);
  125. is_base = (color->attrs == NULL || strcmp (color->attrs, "base") == 0);
  126. attr = tty_attr_get_bits (is_base ? tty_color_defaults.attrs : color->attrs);
  127. color_pair = g_strdup_printf ("%d.%d.%d", ifg, ibg, attr);
  128. if (color_pair == NULL)
  129. return 0;
  130. mc_color_pair =
  131. (tty_color_lib_pair_t *) g_hash_table_lookup (mc_tty_color__hashtable,
  132. (gpointer) color_pair);
  133. if (mc_color_pair != NULL)
  134. {
  135. g_free (color_pair);
  136. return mc_color_pair->pair_index;
  137. }
  138. mc_color_pair = g_try_new0 (tty_color_lib_pair_t, 1);
  139. if (mc_color_pair == NULL)
  140. {
  141. g_free (color_pair);
  142. return 0;
  143. }
  144. mc_color_pair->is_temp = is_temp;
  145. mc_color_pair->fg = ifg;
  146. mc_color_pair->bg = ibg;
  147. mc_color_pair->attr = attr;
  148. mc_color_pair->pair_index = tty_color_get_next__color_pair_number ();
  149. tty_color_try_alloc_lib_pair (mc_color_pair);
  150. g_hash_table_insert (mc_tty_color__hashtable, (gpointer) color_pair, (gpointer) mc_color_pair);
  151. return mc_color_pair->pair_index;
  152. }
  153. /* --------------------------------------------------------------------------------------------- */
  154. void
  155. tty_color_free_temp (void)
  156. {
  157. g_hash_table_foreach_remove (mc_tty_color__hashtable, tty_color_free_temp_cb, NULL);
  158. }
  159. /* --------------------------------------------------------------------------------------------- */
  160. void
  161. tty_color_free_all (void)
  162. {
  163. g_hash_table_remove_all (mc_tty_color__hashtable);
  164. }
  165. /* --------------------------------------------------------------------------------------------- */
  166. void
  167. tty_color_set_defaults (const tty_color_pair_t *color)
  168. {
  169. mc_color__deinit (&tty_color_defaults);
  170. tty_color_defaults.fg = g_strdup (color->fg);
  171. tty_color_defaults.bg = g_strdup (color->bg);
  172. tty_color_defaults.attrs = g_strdup (color->attrs);
  173. tty_color_defaults.pair_index = 0;
  174. }
  175. /* --------------------------------------------------------------------------------------------- */