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