colors.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. Skins engine.
  3. Work with colors
  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. Andrew Borodin <aborodin@vmail.ru>, 2012
  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. #include <config.h>
  23. #include <string.h>
  24. #include "internal.h"
  25. #include "lib/tty/color.h"
  26. /*** global variables ****************************************************************************/
  27. int mc_skin_color__cache[MC_SKIN_COLOR_CACHE_COUNT];
  28. /*** file scope macro definitions ****************************************************************/
  29. /*** file scope type declarations ****************************************************************/
  30. /*** forward declarations (file scope functions) *************************************************/
  31. /*** file scope variables ************************************************************************/
  32. /* --------------------------------------------------------------------------------------------- */
  33. /*** file scope functions ************************************************************************/
  34. /* --------------------------------------------------------------------------------------------- */
  35. static tty_color_pair_t *
  36. mc_skin_color_get_from_hash (mc_skin_t *mc_skin, const gchar *group, const gchar *key)
  37. {
  38. gchar kname[BUF_TINY];
  39. tty_color_pair_t *mc_skin_color;
  40. if (group == NULL || key == NULL)
  41. return NULL;
  42. if (mc_skin == NULL)
  43. mc_skin = &mc_skin__default;
  44. g_snprintf (kname, sizeof (kname), "%s.%s", group, key);
  45. mc_skin_color = (tty_color_pair_t *) g_hash_table_lookup (mc_skin->colors, (gpointer) kname);
  46. return mc_skin_color;
  47. }
  48. /* --------------------------------------------------------------------------------------------- */
  49. #if 0
  50. static void
  51. mc_skin_color_remove_from_hash (mc_skin_t *mc_skin, const gchar *group, const gchar *key)
  52. {
  53. gchar kname[BUF_TINY];
  54. if (group == NULL || key == NULL)
  55. return;
  56. if (mc_skin == NULL)
  57. mc_skin = &mc_skin__default;
  58. g_snprintf (kname, sizeof (kname), "%s.%s", group, key);
  59. g_hash_table_remove (mc_skin->colors, (gpointer) kname);
  60. }
  61. #endif
  62. /* --------------------------------------------------------------------------------------------- */
  63. static void
  64. mc_skin_color_add_to_hash (mc_skin_t *mc_skin, const gchar *group, const gchar *key,
  65. tty_color_pair_t *mc_skin_color)
  66. {
  67. gchar *kname;
  68. kname = g_strdup_printf ("%s.%s", group, key);
  69. if (kname != NULL)
  70. {
  71. if (g_hash_table_lookup (mc_skin->colors, (gpointer) kname) != NULL)
  72. g_hash_table_remove (mc_skin->colors, (gpointer) kname);
  73. g_hash_table_insert (mc_skin->colors, (gpointer) kname, (gpointer) mc_skin_color);
  74. }
  75. }
  76. /* --------------------------------------------------------------------------------------------- */
  77. static tty_color_pair_t *
  78. mc_skin_color_get_with_defaults (const gchar *group, const gchar *name)
  79. {
  80. tty_color_pair_t *mc_skin_color;
  81. mc_skin_color = mc_skin_color_get_from_hash (NULL, group, name);
  82. if (mc_skin_color != NULL)
  83. return mc_skin_color;
  84. mc_skin_color = mc_skin_color_get_from_hash (NULL, group, "_default_");
  85. if (mc_skin_color != NULL)
  86. return mc_skin_color;
  87. mc_skin_color = mc_skin_color_get_from_hash (NULL, "core", "_default_");
  88. return mc_skin_color;
  89. }
  90. /* --------------------------------------------------------------------------------------------- */
  91. /* If an alias is found, alloc a new string for the resolved value and free the input parameter.
  92. Otherwise it's a no-op returning the original string. */
  93. static gchar *
  94. mc_skin_color_look_up_alias (mc_skin_t *mc_skin, gchar *str)
  95. {
  96. gchar *orig, *str2;
  97. int hop = 0;
  98. orig = g_strdup (str);
  99. str2 = g_strdup (str);
  100. while (TRUE)
  101. {
  102. gchar **values;
  103. gsize items_count;
  104. values = mc_config_get_string_list (mc_skin->config, "aliases", str, &items_count);
  105. if (items_count != 1)
  106. {
  107. /* No such alias declaration found, that is, we've got the resolved value. */
  108. g_strfreev (values);
  109. g_free (str2);
  110. g_free (orig);
  111. return str;
  112. }
  113. g_free (str);
  114. str = g_strdup (values[0]);
  115. g_strfreev (values);
  116. /* str2 resolves at half speed than str. This is used for loop detection. */
  117. if (hop++ % 2 != 0)
  118. {
  119. values = mc_config_get_string_list (mc_skin->config, "aliases", str2, &items_count);
  120. g_assert (items_count == 1);
  121. g_free (str2);
  122. str2 = g_strdup (values[0]);
  123. g_strfreev (values);
  124. if (strcmp (str, str2) == 0)
  125. {
  126. /* Loop detected. */
  127. fprintf (stderr,
  128. "Loop detected while trying to resolve alias \"%s\" in skin \"%s\"\n",
  129. orig, mc_skin->name);
  130. g_free (str);
  131. g_free (str2);
  132. return orig;
  133. }
  134. }
  135. }
  136. }
  137. /* --------------------------------------------------------------------------------------------- */
  138. static tty_color_pair_t *
  139. mc_skin_color_get_from_ini_file (mc_skin_t *mc_skin, const gchar *group, const gchar *key)
  140. {
  141. gsize items_count;
  142. gchar **values;
  143. tty_color_pair_t *mc_skin_color, *tmp;
  144. values = mc_config_get_string_list (mc_skin->config, group, key, &items_count);
  145. if (values == NULL || values[0] == NULL)
  146. {
  147. g_strfreev (values);
  148. return NULL;
  149. }
  150. mc_skin_color = g_try_new0 (tty_color_pair_t, 1);
  151. if (mc_skin_color == NULL)
  152. {
  153. g_strfreev (values);
  154. return NULL;
  155. }
  156. tmp = mc_skin_color_get_with_defaults (group, "_default_");
  157. mc_skin_color->fg = (items_count > 0 && values[0][0]) ?
  158. mc_skin_color_look_up_alias (mc_skin, g_strstrip (g_strdup (values[0]))) :
  159. (tmp != NULL) ? g_strdup (tmp->fg) : NULL;
  160. mc_skin_color->bg = (items_count > 1 && values[1][0]) ?
  161. mc_skin_color_look_up_alias (mc_skin, g_strstrip (g_strdup (values[1]))) :
  162. (tmp != NULL) ? g_strdup (tmp->bg) : NULL;
  163. mc_skin_color->attrs = (items_count > 2 && values[2][0]) ?
  164. mc_skin_color_look_up_alias (mc_skin, g_strstrip (g_strdup (values[2]))) :
  165. (tmp != NULL) ? g_strdup (tmp->attrs) : NULL;
  166. g_strfreev (values);
  167. mc_skin_color->pair_index = tty_try_alloc_color_pair (mc_skin_color, FALSE);
  168. return mc_skin_color;
  169. }
  170. /* --------------------------------------------------------------------------------------------- */
  171. static void
  172. mc_skin_color_set_default_for_terminal (mc_skin_t *mc_skin)
  173. {
  174. tty_color_pair_t *mc_skin_color;
  175. mc_skin_color = g_try_new0 (tty_color_pair_t, 1);
  176. if (mc_skin_color != NULL)
  177. {
  178. mc_skin_color->fg = g_strdup ("default");
  179. mc_skin_color->bg = g_strdup ("default");
  180. mc_skin_color->attrs = NULL;
  181. mc_skin_color->pair_index = tty_try_alloc_color_pair (mc_skin_color, FALSE);
  182. mc_skin_color_add_to_hash (mc_skin, "skin", "terminal_default_color", mc_skin_color);
  183. }
  184. }
  185. /* --------------------------------------------------------------------------------------------- */
  186. static void
  187. mc_skin_color_cache_init (void)
  188. {
  189. DEFAULT_COLOR = mc_skin_color_get ("skin", "terminal_default_color");
  190. NORMAL_COLOR = mc_skin_color_get ("core", "_default_");
  191. MARKED_COLOR = mc_skin_color_get ("core", "marked");
  192. SELECTED_COLOR = mc_skin_color_get ("core", "selected");
  193. MARKED_SELECTED_COLOR = mc_skin_color_get ("core", "markselect");
  194. DISABLED_COLOR = mc_skin_color_get ("core", "disabled");
  195. REVERSE_COLOR = mc_skin_color_get ("core", "reverse");
  196. HEADER_COLOR = mc_skin_color_get ("core", "header");
  197. COMMAND_MARK_COLOR = mc_skin_color_get ("core", "commandlinemark");
  198. SHADOW_COLOR = mc_skin_color_get ("core", "shadow");
  199. COLOR_NORMAL = mc_skin_color_get ("dialog", "_default_");
  200. COLOR_FOCUS = mc_skin_color_get ("dialog", "dfocus");
  201. COLOR_HOT_NORMAL = mc_skin_color_get ("dialog", "dhotnormal");
  202. COLOR_HOT_FOCUS = mc_skin_color_get ("dialog", "dhotfocus");
  203. COLOR_TITLE = mc_skin_color_get ("dialog", "dtitle");
  204. ERROR_COLOR = mc_skin_color_get ("error", "_default_");
  205. ERROR_FOCUS = mc_skin_color_get ("error", "errdfocus");
  206. ERROR_HOT_NORMAL = mc_skin_color_get ("error", "errdhotnormal");
  207. ERROR_HOT_FOCUS = mc_skin_color_get ("error", "errdhotfocus");
  208. ERROR_TITLE = mc_skin_color_get ("error", "errdtitle");
  209. MENU_ENTRY_COLOR = mc_skin_color_get ("menu", "_default_");
  210. MENU_SELECTED_COLOR = mc_skin_color_get ("menu", "menusel");
  211. MENU_HOT_COLOR = mc_skin_color_get ("menu", "menuhot");
  212. MENU_HOTSEL_COLOR = mc_skin_color_get ("menu", "menuhotsel");
  213. MENU_INACTIVE_COLOR = mc_skin_color_get ("menu", "menuinactive");
  214. PMENU_ENTRY_COLOR = mc_skin_color_get ("popupmenu", "_default_");
  215. PMENU_SELECTED_COLOR = mc_skin_color_get ("popupmenu", "menusel");
  216. PMENU_TITLE_COLOR = mc_skin_color_get ("popupmenu", "menutitle");
  217. BUTTONBAR_HOTKEY_COLOR = mc_skin_color_get ("buttonbar", "hotkey");
  218. BUTTONBAR_BUTTON_COLOR = mc_skin_color_get ("buttonbar", "button");
  219. STATUSBAR_COLOR = mc_skin_color_get ("statusbar", "_default_");
  220. GAUGE_COLOR = mc_skin_color_get ("core", "gauge");
  221. INPUT_COLOR = mc_skin_color_get ("core", "input");
  222. INPUT_HISTORY_COLOR = mc_skin_color_get ("core", "inputhistory");
  223. COMMAND_HISTORY_COLOR = mc_skin_color_get ("core", "commandhistory");
  224. INPUT_MARK_COLOR = mc_skin_color_get ("core", "inputmark");
  225. INPUT_UNCHANGED_COLOR = mc_skin_color_get ("core", "inputunchanged");
  226. HELP_NORMAL_COLOR = mc_skin_color_get ("help", "_default_");
  227. HELP_ITALIC_COLOR = mc_skin_color_get ("help", "helpitalic");
  228. HELP_BOLD_COLOR = mc_skin_color_get ("help", "helpbold");
  229. HELP_LINK_COLOR = mc_skin_color_get ("help", "helplink");
  230. HELP_SLINK_COLOR = mc_skin_color_get ("help", "helpslink");
  231. HELP_TITLE_COLOR = mc_skin_color_get ("help", "helptitle");
  232. VIEW_NORMAL_COLOR = mc_skin_color_get ("viewer", "_default_");
  233. VIEW_BOLD_COLOR = mc_skin_color_get ("viewer", "viewbold");
  234. VIEW_UNDERLINED_COLOR = mc_skin_color_get ("viewer", "viewunderline");
  235. VIEW_SELECTED_COLOR = mc_skin_color_get ("viewer", "viewselected");
  236. EDITOR_NORMAL_COLOR = mc_skin_color_get ("editor", "_default_");
  237. EDITOR_BOLD_COLOR = mc_skin_color_get ("editor", "editbold");
  238. EDITOR_MARKED_COLOR = mc_skin_color_get ("editor", "editmarked");
  239. EDITOR_WHITESPACE_COLOR = mc_skin_color_get ("editor", "editwhitespace");
  240. EDITOR_NONPRINTABLE_COLOR = mc_skin_color_get ("editor", "editnonprintable");
  241. EDITOR_RIGHT_MARGIN_COLOR = mc_skin_color_get ("editor", "editrightmargin");
  242. LINE_STATE_COLOR = mc_skin_color_get ("editor", "editlinestate");
  243. EDITOR_BACKGROUND = mc_skin_color_get ("editor", "editbg");
  244. EDITOR_FRAME = mc_skin_color_get ("editor", "editframe");
  245. EDITOR_FRAME_ACTIVE = mc_skin_color_get ("editor", "editframeactive");
  246. EDITOR_FRAME_DRAG = mc_skin_color_get ("editor", "editframedrag");
  247. BOOK_MARK_COLOR = mc_skin_color_get ("editor", "bookmark");
  248. BOOK_MARK_FOUND_COLOR = mc_skin_color_get ("editor", "bookmarkfound");
  249. DFF_ADD_COLOR = mc_skin_color_get ("diffviewer", "added");
  250. DFF_CHG_COLOR = mc_skin_color_get ("diffviewer", "changedline");
  251. DFF_CHH_COLOR = mc_skin_color_get ("diffviewer", "changednew");
  252. DFF_CHD_COLOR = mc_skin_color_get ("diffviewer", "changed");
  253. DFF_DEL_COLOR = mc_skin_color_get ("diffviewer", "removed");
  254. DFF_ERROR_COLOR = mc_skin_color_get ("diffviewer", "error");
  255. }
  256. /* --------------------------------------------------------------------------------------------- */
  257. static gboolean
  258. mc_skin_color_check_inisection (const gchar *group)
  259. {
  260. return !((strcasecmp ("skin", group) == 0) || (strcasecmp ("aliases", group) == 0)
  261. || (strcasecmp ("lines", group) == 0) || (strncasecmp ("widget-", group, 7) == 0));
  262. }
  263. /* --------------------------------------------------------------------------------------------- */
  264. static void
  265. mc_skin_color_check_bw_mode (mc_skin_t *mc_skin)
  266. {
  267. gchar **groups, **orig_groups;
  268. if (tty_use_colors () && !mc_global.tty.disable_colors)
  269. return;
  270. orig_groups = mc_config_get_groups (mc_skin->config, NULL);
  271. for (groups = orig_groups; *groups != NULL; groups++)
  272. if (mc_skin_color_check_inisection (*groups))
  273. mc_config_del_group (mc_skin->config, *groups);
  274. g_strfreev (orig_groups);
  275. mc_skin_hardcoded_blackwhite_colors (mc_skin);
  276. }
  277. /* --------------------------------------------------------------------------------------------- */
  278. /*** public functions ****************************************************************************/
  279. /* --------------------------------------------------------------------------------------------- */
  280. gboolean
  281. mc_skin_color_parse_ini_file (mc_skin_t *mc_skin)
  282. {
  283. gboolean ret = FALSE;
  284. gsize items_count;
  285. gchar **groups, **orig_groups;
  286. tty_color_pair_t *mc_skin_color;
  287. mc_skin_color_check_bw_mode (mc_skin);
  288. orig_groups = mc_config_get_groups (mc_skin->config, &items_count);
  289. if (*orig_groups == NULL)
  290. goto ret;
  291. /* as first, need to set up default colors */
  292. mc_skin_color_set_default_for_terminal (mc_skin);
  293. mc_skin_color = mc_skin_color_get_from_ini_file (mc_skin, "core", "_default_");
  294. if (mc_skin_color == NULL)
  295. goto ret;
  296. tty_color_set_defaults (mc_skin_color);
  297. mc_skin_color_add_to_hash (mc_skin, "core", "_default_", mc_skin_color);
  298. for (groups = orig_groups; *groups != NULL; groups++)
  299. {
  300. gchar **keys, **orig_keys;
  301. if (!mc_skin_color_check_inisection (*groups))
  302. continue;
  303. orig_keys = mc_config_get_keys (mc_skin->config, *groups, NULL);
  304. for (keys = orig_keys; *keys != NULL; keys++)
  305. {
  306. mc_skin_color = mc_skin_color_get_from_ini_file (mc_skin, *groups, *keys);
  307. if (mc_skin_color != NULL)
  308. mc_skin_color_add_to_hash (mc_skin, *groups, *keys, mc_skin_color);
  309. }
  310. g_strfreev (orig_keys);
  311. }
  312. mc_skin_color_cache_init ();
  313. ret = TRUE;
  314. ret:
  315. g_strfreev (orig_groups);
  316. return ret;
  317. }
  318. /* --------------------------------------------------------------------------------------------- */
  319. int
  320. mc_skin_color_get (const gchar *group, const gchar *name)
  321. {
  322. tty_color_pair_t *mc_skin_color;
  323. mc_skin_color = mc_skin_color_get_with_defaults (group, name);
  324. return (mc_skin_color != NULL) ? mc_skin_color->pair_index : 0;
  325. }
  326. /* --------------------------------------------------------------------------------------------- */