hook.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Hooks.
  3. Slavaz: Warning! this file is deprecated and should be replaced
  4. by mcevents functional.
  5. Copyright (C) 1994-2024
  6. Free Software Foundation, Inc.
  7. Written by:
  8. Miguel de Icaza, 1994, 1995, 1996
  9. Janne Kukonlehto, 1994, 1995, 1996
  10. Dugan Porter, 1994, 1995, 1996
  11. Jakub Jelinek, 1994, 1995, 1996
  12. Mauricio Plaza, 1994, 1995, 1996
  13. This file is part of the Midnight Commander.
  14. The Midnight Commander is free software: you can redistribute it
  15. and/or modify it under the terms of the GNU General Public License as
  16. published by the Free Software Foundation, either version 3 of the License,
  17. or (at your option) any later version.
  18. The Midnight Commander is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. You should have received a copy of the GNU General Public License
  23. along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. /** \file
  26. * \brief Source: hooks
  27. */
  28. #include <config.h>
  29. #include "lib/global.h"
  30. #include "lib/hook.h"
  31. /*** global variables ****************************************************************************/
  32. /*** file scope macro definitions ****************************************************************/
  33. /*** file scope type declarations ****************************************************************/
  34. /*** file scope variables ************************************************************************/
  35. /*** file scope functions ************************************************************************/
  36. /* --------------------------------------------------------------------------------------------- */
  37. /*** public functions ****************************************************************************/
  38. /* --------------------------------------------------------------------------------------------- */
  39. void
  40. add_hook (hook_t **hook_list, void (*hook_fn) (void *), void *data)
  41. {
  42. hook_t *new_hook = g_new (hook_t, 1);
  43. new_hook->hook_fn = hook_fn;
  44. new_hook->next = *hook_list;
  45. new_hook->hook_data = data;
  46. *hook_list = new_hook;
  47. }
  48. /* --------------------------------------------------------------------------------------------- */
  49. void
  50. execute_hooks (hook_t *hook_list)
  51. {
  52. hook_t *new_hook = NULL;
  53. hook_t *p;
  54. /* We copy the hook list first so tahat we let the hook
  55. * function call delete_hook
  56. */
  57. while (hook_list != NULL)
  58. {
  59. add_hook (&new_hook, hook_list->hook_fn, hook_list->hook_data);
  60. hook_list = hook_list->next;
  61. }
  62. p = new_hook;
  63. while (new_hook != NULL)
  64. {
  65. new_hook->hook_fn (new_hook->hook_data);
  66. new_hook = new_hook->next;
  67. }
  68. for (hook_list = p; hook_list != NULL;)
  69. {
  70. p = hook_list;
  71. hook_list = hook_list->next;
  72. g_free (p);
  73. }
  74. }
  75. /* --------------------------------------------------------------------------------------------- */
  76. void
  77. delete_hook (hook_t **hook_list, void (*hook_fn) (void *))
  78. {
  79. hook_t *new_list = NULL;
  80. hook_t *current, *next;
  81. for (current = *hook_list; current != NULL; current = next)
  82. {
  83. next = current->next;
  84. if (current->hook_fn == hook_fn)
  85. g_free (current);
  86. else
  87. add_hook (&new_list, current->hook_fn, current->hook_data);
  88. }
  89. *hook_list = new_list;
  90. }
  91. /* --------------------------------------------------------------------------------------------- */
  92. gboolean
  93. hook_present (hook_t *hook_list, void (*hook_fn) (void *))
  94. {
  95. hook_t *p;
  96. for (p = hook_list; p != NULL; p = p->next)
  97. if (p->hook_fn == hook_fn)
  98. return TRUE;
  99. return FALSE;
  100. }
  101. /* --------------------------------------------------------------------------------------------- */