color.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* Color setup.
  2. Interface functions.
  3. Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2007, 2008, 2009 Free Software Foundation, Inc.
  5. Written by:
  6. Andrew Borodin <aborodin@vmail.ru>, 2009.
  7. Slava Zanko <slavazanko@gmail.com>, 2009.
  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.c
  20. * \brief Source: 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.h"
  29. #include "color.h"
  30. #include "color-internal.h"
  31. /*** global variables ****************************************************************************/
  32. char *command_line_colors = NULL;
  33. static char *tty_color_defaults__fg = NULL;
  34. static char *tty_color_defaults__bg = NULL;
  35. /* Set if we are actually using colors */
  36. gboolean use_colors = FALSE;
  37. /*** file scope macro definitions ****************************************************************/
  38. /*** file scope type declarations ****************************************************************/
  39. /*** file scope variables ************************************************************************/
  40. static GHashTable *mc_tty_color__hashtable = NULL;
  41. /*** file scope functions ************************************************************************/
  42. /* --------------------------------------------------------------------------------------------- */
  43. static inline void
  44. color_hash_destroy_key (gpointer data)
  45. {
  46. g_free (data);
  47. }
  48. /* --------------------------------------------------------------------------------------------- */
  49. static void
  50. color_hash_destroy_value (gpointer data)
  51. {
  52. tty_color_pair_t *mc_color_pair = (tty_color_pair_t *) data;
  53. g_free (mc_color_pair);
  54. }
  55. /* --------------------------------------------------------------------------------------------- */
  56. static gboolean
  57. tty_color_free_condition_cb (gpointer key, gpointer value, gpointer user_data)
  58. {
  59. gboolean is_temp_color;
  60. tty_color_pair_t *mc_color_pair;
  61. (void) key;
  62. is_temp_color = user_data != NULL;
  63. mc_color_pair = (tty_color_pair_t *) value;
  64. return (mc_color_pair->is_temp == is_temp_color);
  65. }
  66. /* --------------------------------------------------------------------------------------------- */
  67. static void
  68. tty_color_free_all (gboolean is_temp_color)
  69. {
  70. g_hash_table_foreach_remove (mc_tty_color__hashtable, tty_color_free_condition_cb,
  71. (is_temp_color) ? (gpointer) 1 : NULL);
  72. }
  73. /* --------------------------------------------------------------------------------------------- */
  74. static gboolean
  75. tty_color_get_next_cpn_cb (gpointer key, gpointer value, gpointer user_data)
  76. {
  77. size_t cp;
  78. tty_color_pair_t *mc_color_pair;
  79. (void) key;
  80. cp = (size_t) user_data;
  81. mc_color_pair = (tty_color_pair_t *) value;
  82. if (cp == mc_color_pair->pair_index)
  83. return TRUE;
  84. return FALSE;
  85. }
  86. /* --------------------------------------------------------------------------------------------- */
  87. static int
  88. tty_color_get_next__color_pair_number ()
  89. {
  90. size_t cp_count = g_hash_table_size (mc_tty_color__hashtable);
  91. size_t cp = 0;
  92. for (cp = 0; cp < cp_count; cp++)
  93. {
  94. if (g_hash_table_find (mc_tty_color__hashtable, tty_color_get_next_cpn_cb, (gpointer) cp) ==
  95. NULL)
  96. return cp;
  97. }
  98. return cp;
  99. }
  100. /* --------------------------------------------------------------------------------------------- */
  101. /*** public functions ****************************************************************************/
  102. /* --------------------------------------------------------------------------------------------- */
  103. void
  104. tty_init_colors (gboolean disable, gboolean force)
  105. {
  106. tty_color_init_lib (disable, force);
  107. mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal,
  108. color_hash_destroy_key,
  109. color_hash_destroy_value);
  110. }
  111. /* --------------------------------------------------------------------------------------------- */
  112. void
  113. tty_colors_done (void)
  114. {
  115. tty_color_deinit_lib ();
  116. g_free (tty_color_defaults__fg);
  117. g_free (tty_color_defaults__bg);
  118. g_hash_table_destroy (mc_tty_color__hashtable);
  119. }
  120. /* --------------------------------------------------------------------------------------------- */
  121. gboolean
  122. tty_use_colors (void)
  123. {
  124. return use_colors;
  125. }
  126. /* --------------------------------------------------------------------------------------------- */
  127. int
  128. tty_try_alloc_color_pair2 (const char *fg, const char *bg, gboolean is_temp_color)
  129. {
  130. gchar *color_pair;
  131. tty_color_pair_t *mc_color_pair;
  132. const char *c_fg, *c_bg;
  133. if (fg == NULL)
  134. fg = tty_color_defaults__fg;
  135. if (bg == NULL)
  136. {
  137. bg = tty_color_defaults__bg;
  138. }
  139. c_fg = tty_color_get_valid_name (fg);
  140. c_bg = tty_color_get_valid_name (bg);
  141. color_pair = g_strdup_printf ("%s.%s", c_fg, c_bg);
  142. if (color_pair == NULL)
  143. return 0;
  144. mc_color_pair =
  145. (tty_color_pair_t *) g_hash_table_lookup (mc_tty_color__hashtable, (gpointer) color_pair);
  146. if (mc_color_pair != NULL)
  147. {
  148. g_free (color_pair);
  149. return mc_color_pair->pair_index;
  150. }
  151. mc_color_pair = g_try_new0 (tty_color_pair_t, 1);
  152. if (mc_color_pair == NULL)
  153. {
  154. g_free (color_pair);
  155. return 0;
  156. }
  157. mc_color_pair->is_temp = is_temp_color;
  158. mc_color_pair->cfg = c_fg;
  159. mc_color_pair->cbg = c_bg;
  160. mc_color_pair->ifg = tty_color_get_index_by_name (c_fg);
  161. mc_color_pair->ibg = tty_color_get_index_by_name (c_bg);
  162. mc_color_pair->pair_index = tty_color_get_next__color_pair_number ();
  163. tty_color_try_alloc_pair_lib (mc_color_pair);
  164. g_hash_table_insert (mc_tty_color__hashtable, (gpointer) color_pair, (gpointer) mc_color_pair);
  165. return mc_color_pair->pair_index;
  166. }
  167. /* --------------------------------------------------------------------------------------------- */
  168. int
  169. tty_try_alloc_color_pair (const char *fg, const char *bg)
  170. {
  171. return tty_try_alloc_color_pair2 (fg, bg, TRUE);
  172. }
  173. /* --------------------------------------------------------------------------------------------- */
  174. void
  175. tty_color_free_all_tmp (void)
  176. {
  177. tty_color_free_all (TRUE);
  178. }
  179. /* --------------------------------------------------------------------------------------------- */
  180. void
  181. tty_color_free_all_non_tmp (void)
  182. {
  183. tty_color_free_all (FALSE);
  184. }
  185. /* --------------------------------------------------------------------------------------------- */
  186. void
  187. tty_color_set_defaults (const char *fgcolor, const char *bgcolor)
  188. {
  189. g_free (tty_color_defaults__fg);
  190. g_free (tty_color_defaults__fg);
  191. tty_color_defaults__fg = (fgcolor != NULL) ? g_strdup (fgcolor) : NULL;
  192. tty_color_defaults__bg = (bgcolor != NULL) ? g_strdup (bgcolor) : NULL;
  193. }
  194. /* --------------------------------------------------------------------------------------------- */