Git init
[framework/uifw/xorg/lib/libxt.git] / src / Hooks.c
1 /*
2
3 Copyright 1994, 1998  The Open Group
4
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
24
25 */
26
27 /*LINTLIBRARY*/
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32 #include "IntrinsicI.h"
33 #include "CreateI.h"
34
35 static void FreeBlockHookList(
36     Widget widget,              /* unused (and invalid) */
37     XtPointer closure,          /* ActionHook* */
38     XtPointer call_data)        /* unused */
39 {
40     BlockHook list = *(BlockHook*)closure;
41     while (list != NULL) {
42         BlockHook next = list->next;
43         XtFree( (XtPointer)list );
44         list = next;
45     }
46 }
47
48
49 XtBlockHookId XtAppAddBlockHook(
50     XtAppContext app,
51     XtBlockHookProc proc,
52     XtPointer closure)
53 {
54     BlockHook hook = XtNew(BlockHookRec);
55     LOCK_APP(app);
56     hook->next = app->block_hook_list;
57     hook->app = app;
58     hook->proc = proc;
59     hook->closure = closure;
60     if (app->block_hook_list == NULL) {
61         _XtAddCallback( &app->destroy_callbacks,
62                         FreeBlockHookList,
63                         (XtPointer)&app->block_hook_list
64                       );
65     }
66     app->block_hook_list = hook;
67     UNLOCK_APP(app);
68     return (XtBlockHookId)hook;
69 }
70
71
72 void XtRemoveBlockHook(
73     XtBlockHookId id)
74 {
75     BlockHook *p, hook = (BlockHook)id;
76     XtAppContext app = hook->app;
77     LOCK_APP(app);
78     for (p = &app->block_hook_list; p != NULL && *p != hook; p = &(*p)->next);
79     if (p == NULL) {
80 #ifdef DEBUG
81         XtAppWarningMsg(app, "badId", "xtRemoveBlockHook", XtCXtToolkitError,
82                         "XtRemoveBlockHook called with bad or old hook id",
83                         (String*)NULL, (Cardinal*)NULL);
84 #endif /*DEBUG*/
85         UNLOCK_APP(app);
86         return;
87     }
88     *p = hook->next;
89     XtFree( (XtPointer)hook );
90     UNLOCK_APP(app);
91 }
92
93 static void DeleteShellFromHookObj(
94     Widget shell,
95     XtPointer closure,
96     XtPointer call_data)
97 {
98     /* app_con is locked when this function is called */
99     Cardinal ii, jj;
100     HookObject ho = (HookObject) closure;
101
102     for (ii = 0; ii < ho->hooks.num_shells; ii++)
103         if (ho->hooks.shells[ii] == shell) {
104             /* collapse the list */
105             for (jj = ii; jj < ho->hooks.num_shells; jj++) {
106                 if ((jj+1) < ho->hooks.num_shells)
107                     ho->hooks.shells[jj] = ho->hooks.shells[jj+1];
108             }
109             break;
110         }
111     ho->hooks.num_shells--;
112 }
113
114 #define SHELL_INCR 4
115
116 void _XtAddShellToHookObj(
117     Widget shell)
118 {
119     /* app_con is locked when this function is called */
120     HookObject ho = (HookObject) XtHooksOfDisplay(XtDisplay(shell));
121
122     if (ho->hooks.num_shells == ho->hooks.max_shells) {
123         ho->hooks.max_shells += SHELL_INCR;
124         ho->hooks.shells =
125             (WidgetList)XtRealloc((char*)ho->hooks.shells,
126                 ho->hooks.max_shells * sizeof (Widget));
127     }
128     ho->hooks.shells[ho->hooks.num_shells++] = shell;
129
130     XtAddCallback(shell, XtNdestroyCallback, DeleteShellFromHookObj,
131                   (XtPointer)ho);
132 }
133
134 Boolean _XtIsHookObject(
135     Widget widget)
136 {
137     return (widget->core.widget_class == hookObjectClass);
138 }
139
140 Widget XtHooksOfDisplay(
141     Display* dpy)
142 {
143     Widget retval;
144     XtPerDisplay pd;
145     DPY_TO_APPCON(dpy);
146
147     LOCK_APP(app);
148     pd = _XtGetPerDisplay(dpy);
149     if (pd->hook_object == NULL)
150         pd->hook_object =
151             _XtCreateHookObj((Screen*)DefaultScreenOfDisplay(dpy));
152     retval = pd->hook_object;
153     UNLOCK_APP(app);
154     return retval;
155 }