color.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 gboolean
  44. tty_color_free_condition_cb (gpointer key, gpointer value, gpointer user_data)
  45. {
  46. gboolean is_temp_color;
  47. tty_color_pair_t *mc_color_pair;
  48. (void) key;
  49. is_temp_color = user_data != NULL;
  50. mc_color_pair = (tty_color_pair_t *) value;
  51. return (mc_color_pair->is_temp == is_temp_color);
  52. }
  53. /* --------------------------------------------------------------------------------------------- */
  54. static void
  55. tty_color_free_all (gboolean is_temp_color)
  56. {
  57. g_hash_table_foreach_remove (mc_tty_color__hashtable, tty_color_free_condition_cb,
  58. is_temp_color ? GINT_TO_POINTER (1) : NULL);
  59. }
  60. /* --------------------------------------------------------------------------------------------- */
  61. static gboolean
  62. tty_color_get_next_cpn_cb (gpointer key, gpointer value, gpointer user_data)
  63. {
  64. int cp;
  65. tty_color_pair_t *mc_color_pair;
  66. (void) key;
  67. cp = GPOINTER_TO_INT (user_data);
  68. mc_color_pair = (tty_color_pair_t *) value;
  69. return (cp == mc_color_pair->pair_index);
  70. }
  71. /* --------------------------------------------------------------------------------------------- */
  72. static int
  73. tty_color_get_next__color_pair_number (void)
  74. {
  75. const size_t cp_count = g_hash_table_size (mc_tty_color__hashtable);
  76. size_t cp;
  77. for (cp = 0; cp < cp_count; cp++)
  78. if (g_hash_table_find (mc_tty_color__hashtable, tty_color_get_next_cpn_cb,
  79. GINT_TO_POINTER (cp)) == NULL)
  80. break;
  81. return cp;
  82. }
  83. /* --------------------------------------------------------------------------------------------- */
  84. /*** public functions ****************************************************************************/
  85. /* --------------------------------------------------------------------------------------------- */
  86. void
  87. tty_init_colors (gboolean disable, gboolean force)
  88. {
  89. tty_color_init_lib (disable, force);
  90. mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
  91. }
  92. /* --------------------------------------------------------------------------------------------- */
  93. void
  94. tty_colors_done (void)
  95. {
  96. tty_color_deinit_lib ();
  97. g_free (tty_color_defaults__fg);
  98. g_free (tty_color_defaults__bg);
  99. g_hash_table_destroy (mc_tty_color__hashtable);
  100. }
  101. /* --------------------------------------------------------------------------------------------- */
  102. gboolean
  103. tty_use_colors (void)
  104. {
  105. return use_colors;
  106. }
  107. /* --------------------------------------------------------------------------------------------- */
  108. int
  109. tty_try_alloc_color_pair2 (const char *fg, const char *bg, gboolean is_temp_color)
  110. {
  111. gchar *color_pair;
  112. tty_color_pair_t *mc_color_pair;
  113. const char *c_fg, *c_bg;
  114. if (fg == NULL)
  115. fg = tty_color_defaults__fg;
  116. if (bg == NULL)
  117. bg = tty_color_defaults__bg;
  118. c_fg = tty_color_get_valid_name (fg);
  119. c_bg = tty_color_get_valid_name (bg);
  120. color_pair = g_strdup_printf ("%s.%s", c_fg, c_bg);
  121. if (color_pair == NULL)
  122. return 0;
  123. mc_color_pair =
  124. (tty_color_pair_t *) g_hash_table_lookup (mc_tty_color__hashtable, (gpointer) color_pair);
  125. if (mc_color_pair != NULL)
  126. {
  127. g_free (color_pair);
  128. return mc_color_pair->pair_index;
  129. }
  130. mc_color_pair = g_try_new0 (tty_color_pair_t, 1);
  131. if (mc_color_pair == NULL)
  132. {
  133. g_free (color_pair);
  134. return 0;
  135. }
  136. mc_color_pair->is_temp = is_temp_color;
  137. mc_color_pair->cfg = c_fg;
  138. mc_color_pair->cbg = c_bg;
  139. mc_color_pair->ifg = tty_color_get_index_by_name (c_fg);
  140. mc_color_pair->ibg = tty_color_get_index_by_name (c_bg);
  141. mc_color_pair->pair_index = tty_color_get_next__color_pair_number ();
  142. tty_color_try_alloc_pair_lib (mc_color_pair);
  143. g_hash_table_insert (mc_tty_color__hashtable, (gpointer) color_pair, (gpointer) mc_color_pair);
  144. return mc_color_pair->pair_index;
  145. }
  146. /* --------------------------------------------------------------------------------------------- */
  147. int
  148. tty_try_alloc_color_pair (const char *fg, const char *bg)
  149. {
  150. return tty_try_alloc_color_pair2 (fg, bg, TRUE);
  151. }
  152. /* --------------------------------------------------------------------------------------------- */
  153. void
  154. tty_color_free_all_tmp (void)
  155. {
  156. tty_color_free_all (TRUE);
  157. }
  158. /* --------------------------------------------------------------------------------------------- */
  159. void
  160. tty_color_free_all_non_tmp (void)
  161. {
  162. tty_color_free_all (FALSE);
  163. }
  164. /* --------------------------------------------------------------------------------------------- */
  165. void
  166. tty_color_set_defaults (const char *fgcolor, const char *bgcolor)
  167. {
  168. g_free (tty_color_defaults__fg);
  169. g_free (tty_color_defaults__fg);
  170. tty_color_defaults__fg = (fgcolor != NULL) ? g_strdup (fgcolor) : NULL;
  171. tty_color_defaults__bg = (bgcolor != NULL) ? g_strdup (bgcolor) : NULL;
  172. }
  173. /* --------------------------------------------------------------------------------------------- */