hook.c 3.9 KB

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