color.c 7.3 KB

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