common.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. Skins engine.
  3. Interface functions
  4. Copyright (C) 2009-2024
  5. Free Software Foundation, Inc.
  6. Written by:
  7. Slava Zanko <slavazanko@gmail.com>, 2009
  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. #include <config.h>
  22. #include <stdlib.h>
  23. #include "internal.h"
  24. #include "lib/util.h"
  25. #include "lib/tty/color.h" /* tty_use_256colors(); */
  26. /*** global variables ****************************************************************************/
  27. mc_skin_t mc_skin__default;
  28. /*** file scope macro definitions ****************************************************************/
  29. /*** file scope type declarations ****************************************************************/
  30. /*** forward declarations (file scope functions) *************************************************/
  31. /*** file scope variables ************************************************************************/
  32. static gboolean mc_skin_is_init = FALSE;
  33. /* --------------------------------------------------------------------------------------------- */
  34. /*** file scope functions ************************************************************************/
  35. /* --------------------------------------------------------------------------------------------- */
  36. static void
  37. mc_skin_hash_destroy_value (gpointer data)
  38. {
  39. tty_color_pair_t *mc_skin_color = (tty_color_pair_t *) data;
  40. g_free (mc_skin_color->fg);
  41. g_free (mc_skin_color->bg);
  42. g_free (mc_skin_color->attrs);
  43. g_free (mc_skin_color);
  44. }
  45. /* --------------------------------------------------------------------------------------------- */
  46. static char *
  47. mc_skin_get_default_name (void)
  48. {
  49. char *tmp_str;
  50. /* from command line */
  51. if (mc_global.tty.skin != NULL)
  52. return g_strdup (mc_global.tty.skin);
  53. /* from envirovement variable */
  54. tmp_str = getenv ("MC_SKIN");
  55. if (tmp_str != NULL)
  56. return g_strdup (tmp_str);
  57. /* from config. Or 'default' if no present in config */
  58. return mc_config_get_string (mc_global.main_config, CONFIG_APP_SECTION, "skin", "default");
  59. }
  60. /* --------------------------------------------------------------------------------------------- */
  61. static void
  62. mc_skin_reinit (void)
  63. {
  64. mc_skin_deinit ();
  65. mc_skin__default.name = mc_skin_get_default_name ();
  66. mc_skin__default.colors = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
  67. mc_skin_hash_destroy_value);
  68. }
  69. /* --------------------------------------------------------------------------------------------- */
  70. static void
  71. mc_skin_try_to_load_default (void)
  72. {
  73. mc_skin_reinit ();
  74. g_free (mc_skin__default.name);
  75. mc_skin__default.name = g_strdup ("default");
  76. if (!mc_skin_ini_file_load (&mc_skin__default))
  77. {
  78. mc_skin_reinit ();
  79. mc_skin_set_hardcoded_skin (&mc_skin__default);
  80. }
  81. }
  82. /* --------------------------------------------------------------------------------------------- */
  83. /*** public functions ****************************************************************************/
  84. /* --------------------------------------------------------------------------------------------- */
  85. gboolean
  86. mc_skin_init (const gchar *skin_override, GError **mcerror)
  87. {
  88. gboolean is_good_init = TRUE;
  89. GError *error = NULL;
  90. mc_return_val_if_error (mcerror, FALSE);
  91. mc_skin__default.have_256_colors = FALSE;
  92. mc_skin__default.have_true_colors = FALSE;
  93. mc_skin__default.name = skin_override != NULL ? g_strdup (skin_override)
  94. : mc_skin_get_default_name ();
  95. mc_skin__default.colors = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
  96. mc_skin_hash_destroy_value);
  97. if (!mc_skin_ini_file_load (&mc_skin__default))
  98. {
  99. mc_propagate_error (mcerror, 0,
  100. _ ("Unable to load '%s' skin.\nDefault skin has been loaded"),
  101. mc_skin__default.name);
  102. mc_skin_try_to_load_default ();
  103. is_good_init = FALSE;
  104. }
  105. mc_skin_colors_old_configure (&mc_skin__default);
  106. if (!mc_skin_ini_file_parse (&mc_skin__default))
  107. {
  108. mc_propagate_error (mcerror, 0,
  109. _ ("Unable to parse '%s' skin.\nDefault skin has been loaded"),
  110. mc_skin__default.name);
  111. mc_skin_try_to_load_default ();
  112. mc_skin_colors_old_configure (&mc_skin__default);
  113. (void) mc_skin_ini_file_parse (&mc_skin__default);
  114. is_good_init = FALSE;
  115. }
  116. if (is_good_init && mc_skin__default.have_true_colors && !tty_use_truecolors (&error))
  117. {
  118. mc_propagate_error (mcerror, 0,
  119. _ ("Unable to use '%s' skin with true colors support:\n%s\nDefault "
  120. "skin has been loaded"),
  121. mc_skin__default.name, error->message);
  122. g_error_free (error);
  123. mc_skin_try_to_load_default ();
  124. mc_skin_colors_old_configure (&mc_skin__default);
  125. (void) mc_skin_ini_file_parse (&mc_skin__default);
  126. is_good_init = FALSE;
  127. }
  128. if (is_good_init && mc_skin__default.have_256_colors && !tty_use_256colors (&error))
  129. {
  130. mc_propagate_error (mcerror, 0,
  131. _ ("Unable to use '%s' skin with 256 colors support\non non-256 colors "
  132. "terminal.\nDefault skin has been loaded"),
  133. mc_skin__default.name);
  134. mc_skin_try_to_load_default ();
  135. mc_skin_colors_old_configure (&mc_skin__default);
  136. (void) mc_skin_ini_file_parse (&mc_skin__default);
  137. is_good_init = FALSE;
  138. }
  139. mc_skin_is_init = TRUE;
  140. return is_good_init;
  141. }
  142. /* --------------------------------------------------------------------------------------------- */
  143. void
  144. mc_skin_deinit (void)
  145. {
  146. tty_color_free_all ();
  147. MC_PTR_FREE (mc_skin__default.name);
  148. g_hash_table_destroy (mc_skin__default.colors);
  149. mc_skin__default.colors = NULL;
  150. MC_PTR_FREE (mc_skin__default.description);
  151. mc_config_deinit (mc_skin__default.config);
  152. mc_skin__default.config = NULL;
  153. mc_skin_is_init = FALSE;
  154. }
  155. /* --------------------------------------------------------------------------------------------- */
  156. gchar *
  157. mc_skin_get (const gchar *group, const gchar *key, const gchar *default_value)
  158. {
  159. if (mc_global.tty.ugly_line_drawing)
  160. return g_strdup (default_value);
  161. return mc_config_get_string (mc_skin__default.config, group, key, default_value);
  162. }
  163. /* --------------------------------------------------------------------------------------------- */