| 1 | /* x-hook.c |
| 2 | * |
| 3 | * Copyright (c) 2002-2012 Apple Inc. All rights reserved. |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person |
| 6 | * obtaining a copy of this software and associated documentation files |
| 7 | * (the "Software"), to deal in the Software without restriction, |
| 8 | * including without limitation the rights to use, copy, modify, merge, |
| 9 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 10 | * and to permit persons to whom the Software is furnished to do so, |
| 11 | * subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be |
| 14 | * included in all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT |
| 20 | * HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 23 | * DEALINGS IN THE SOFTWARE. |
| 24 | * |
| 25 | * Except as contained in this notice, the name(s) of the above |
| 26 | * copyright holders shall not be used in advertising or otherwise to |
| 27 | * promote the sale, use or other dealings in this Software without |
| 28 | * prior written authorization. |
| 29 | */ |
| 30 | |
| 31 | #ifdef HAVE_DIX_CONFIG_H |
| 32 | #include <dix-config.h> |
| 33 | #endif |
| 34 | |
| 35 | #include "x-hook.h" |
| 36 | #include <stdlib.h> |
| 37 | #include <assert.h> |
| 38 | #include "os.h" |
| 39 | |
| 40 | #define CELL_NEW(f, d) X_PFX(list_prepend) ((x_list *)(f), (d)) |
| 41 | #define CELL_FREE(c) X_PFX(list_free_1) (c) |
| 42 | #define CELL_FUN(c) ((x_hook_function *)((c)->next)) |
| 43 | #define CELL_DATA(c) ((c)->data) |
| 44 | |
| 45 | X_EXTERN x_list * |
| 46 | X_PFX(hook_add) (x_list * lst, x_hook_function * fun, void *data) { |
| 47 | return X_PFX(list_prepend) (lst, CELL_NEW(fun, data)); |
| 48 | } |
| 49 | |
| 50 | X_EXTERN x_list * |
| 51 | X_PFX(hook_remove) (x_list * lst, x_hook_function * fun, void *data) { |
| 52 | x_list *node, *cell; |
| 53 | x_list *to_delete = NULL; |
| 54 | |
| 55 | for (node = lst; node != NULL; node = node->next) { |
| 56 | cell = node->data; |
| 57 | if (CELL_FUN(cell) == fun && CELL_DATA(cell) == data) |
| 58 | to_delete = X_PFX(list_prepend) (to_delete, cell); |
| 59 | } |
| 60 | |
| 61 | for (node = to_delete; node != NULL; node = node->next) { |
| 62 | cell = node->data; |
| 63 | lst = X_PFX(list_remove) (lst, cell); |
| 64 | CELL_FREE(cell); |
| 65 | } |
| 66 | |
| 67 | X_PFX(list_free) (to_delete); |
| 68 | return lst; |
| 69 | } |
| 70 | |
| 71 | X_EXTERN void |
| 72 | X_PFX(hook_run) (x_list * lst, void *arg) { |
| 73 | x_list *node; |
| 74 | |
| 75 | if (!lst) |
| 76 | return; |
| 77 | |
| 78 | for (node = lst; node != NULL; node = node->next) { |
| 79 | x_list *cell = node->data; |
| 80 | |
| 81 | x_hook_function *fun = CELL_FUN(cell); |
| 82 | void *data = CELL_DATA(cell); |
| 83 | |
| 84 | (*fun)(arg, data); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | X_EXTERN void |
| 89 | X_PFX(hook_free) (x_list * lst) { |
| 90 | x_list *node; |
| 91 | |
| 92 | for (node = lst; node != NULL; node = node->next) { |
| 93 | CELL_FREE(node->data); |
| 94 | } |
| 95 | |
| 96 | X_PFX(list_free) (lst); |
| 97 | } |