1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | #ifdef HAVE_CONFIG_H1 |
55 | #include <config.h> |
56 | #endif |
57 | #include <stdio.h> /* for NULL */ |
58 | #include <X11/Xos.h> |
59 | #include <X11/Xlib.h> |
60 | #include <X11/Xmu/CloseHook.h> |
61 | #include <stdlib.h> |
62 | |
63 | |
64 | |
65 | |
66 | |
67 | |
68 | |
69 | |
70 | typedef struct _CallbackRec { |
71 | struct _CallbackRec *next; |
72 | XmuCloseHookProc func; |
73 | XPointer arg; |
74 | } CallbackRec; |
75 | |
76 | |
77 | typedef struct _DisplayEntry { |
78 | struct _DisplayEntry *next; |
79 | Display *dpy; |
80 | int extension; |
81 | struct _CallbackRec *start, *end; |
82 | struct _CallbackRec *calling; |
83 | } DisplayEntry; |
84 | |
85 | |
86 | |
87 | |
88 | static DisplayEntry *_FindDisplayEntry(Display*, DisplayEntry**); |
89 | static Boolint _MakeExtension(Display*, int*); |
90 | |
91 | static DisplayEntry *elist = NULL((void*)0); |
92 | |
93 | |
94 | |
95 | |
96 | |
97 | |
98 | |
99 | |
100 | |
101 | |
102 | |
103 | |
104 | |
105 | |
106 | |
107 | |
108 | |
109 | |
110 | |
111 | |
112 | |
113 | CloseHook |
114 | XmuAddCloseDisplayHook(Display *dpy, XmuCloseHookProc func, XPointer arg) |
115 | { |
116 | DisplayEntry *de; |
117 | CallbackRec *cb; |
118 | |
119 | |
120 | cb = (CallbackRec *) malloc (sizeof (CallbackRec)); |
121 | if (!cb) return ((XPointer) NULL((void*)0)); |
122 | |
123 | de = _FindDisplayEntry (dpy, NULL((void*)0)); |
124 | if (!de) { |
125 | if ((de = (DisplayEntry *) malloc (sizeof (DisplayEntry))) == NULL((void*)0) || |
126 | !_MakeExtension (dpy, &de->extension)) { |
127 | free ((char *) cb); |
128 | if (de) free ((char *) de); |
129 | return ((CloseHook) NULL((void*)0)); |
130 | } |
131 | de->dpy = dpy; |
132 | de->start = de->end = NULL((void*)0); |
133 | de->calling = NULL((void*)0); |
134 | de->next = elist; |
135 | elist = de; |
136 | } |
137 | |
138 | |
139 | cb->func = func; |
140 | cb->arg = arg; |
141 | cb->next = NULL((void*)0); |
142 | if (de->end) { |
143 | de->end->next = cb; |
144 | } else { |
145 | de->start = cb; |
146 | } |
147 | de->end = cb; |
148 | |
149 | return ((CloseHook) cb); |
150 | } |
151 | |
152 | |
153 | |
154 | |
155 | |
156 | |
157 | Boolint |
158 | XmuRemoveCloseDisplayHook(Display *dpy, CloseHook handle, |
159 | XmuCloseHookProc func, XPointer arg) |
160 | { |
161 | DisplayEntry *de = _FindDisplayEntry (dpy, NULL((void*)0)); |
162 | register CallbackRec *h, *prev; |
163 | |
164 | if (!de) return False0; |
165 | |
166 | |
167 | for (h = de->start, prev = NULL((void*)0); h; h = h->next) { |
168 | if (handle) { |
169 | if (h == (CallbackRec *) handle) break; |
170 | } else { |
171 | if (h->func == func && h->arg == arg) break; |
172 | } |
173 | prev = h; |
174 | } |
175 | if (!h) return False0; |
176 | |
177 | |
178 | |
179 | if (de->start == h) { |
180 | de->start = h->next; |
181 | } else { |
182 | prev->next = h->next; |
183 | } |
184 | if (de->end == h) de->end = prev; |
185 | if (de->calling != h) free ((char *) h); |
186 | return True1; |
187 | } |
188 | |
189 | |
190 | |
191 | |
192 | |
193 | |
194 | |
195 | Boolint |
196 | XmuLookupCloseDisplayHook(Display *dpy, CloseHook handle, |
197 | XmuCloseHookProc func, XPointer arg) |
198 | { |
199 | DisplayEntry *de = _FindDisplayEntry (dpy, NULL((void*)0)); |
200 | register CallbackRec *h; |
201 | |
202 | if (!de) return False0; |
203 | |
204 | for (h = de->start; h; h = h->next) { |
205 | if (handle) { |
206 | if (h == (CallbackRec *) handle) break; |
207 | } else { |
208 | if (h->func == func && h->arg == arg) break; |
209 | } |
210 | } |
211 | return (h ? True1 : False0); |
212 | } |
213 | |
214 | |
215 | |
216 | |
217 | |
218 | |
219 | |
220 | |
221 | |
222 | |
223 | |
224 | |
225 | |
226 | |
227 | static DisplayEntry * |
228 | _FindDisplayEntry(register Display *dpy, DisplayEntry **prevp) |
229 | { |
230 | register DisplayEntry *d, *prev; |
231 | |
232 | for (d = elist, prev = NULL((void*)0); d; d = d->next) { |
| 2 | | Null pointer value stored to 'prev' | |
|
| 3 | | Loop condition is true. Entering loop body | |
|
233 | if (d->dpy == dpy) { |
| |
234 | if (prevp) *prevp = prev; |
| |
| 6 | | Null pointer value stored to 'prev' | |
|
235 | return d; |
236 | } |
237 | prev = d; |
238 | } |
239 | return NULL((void*)0); |
240 | } |
241 | |
242 | |
243 | |
244 | |
245 | |
246 | |
247 | |
248 | |
249 | static int |
250 | _DoCallbacks(Display *dpy, XExtCodes *codes) |
251 | { |
252 | register CallbackRec *h; |
253 | DisplayEntry *prev; |
254 | DisplayEntry *de = _FindDisplayEntry (dpy, &prev); |
| 1 | Calling '_FindDisplayEntry' | |
|
| 7 | | Returning from '_FindDisplayEntry' | |
|
255 | |
256 | if (!de) return 0; |
| |
257 | |
258 | |
259 | for (h = de->start; h;) { |
| 9 | | Loop condition is true. Entering loop body | |
|
| 10 | | Loop condition is false. Execution continues on line 269 | |
|
260 | register CallbackRec *nexth = h->next; |
261 | de->calling = h; |
262 | (*(h->func)) (dpy, h->arg); |
263 | de->calling = NULL((void*)0); |
264 | free ((char *) h); |
265 | h = nexth; |
266 | } |
267 | |
268 | |
269 | if (elist == de) { |
| 11 | | Assuming 'elist' is not equal to 'de' | |
|
| |
270 | elist = de->next; |
271 | } else { |
272 | prev->next = de->next; |
| 13 | | Access to field 'next' results in a dereference of a null pointer (loaded from variable 'prev') |
|
273 | } |
274 | free ((char *) de); |
275 | return 1; |
276 | } |
277 | |
278 | |
279 | |
280 | |
281 | |
282 | static Boolint |
283 | _MakeExtension(Display *dpy, int *extensionp) |
284 | { |
285 | XExtCodes *codes; |
286 | |
287 | codes = XAddExtension (dpy); |
288 | if (!codes) return False0; |
289 | |
290 | (void) XESetCloseDisplay (dpy, codes->extension, _DoCallbacks); |
291 | |
292 | *extensionp = codes->extension; |
293 | return True1; |
294 | } |