Listtree.mcc: make Listtree inherit from List as it should
[AROS.git] / compiler / arossupport / rt_intuition.c
blobff16ef33bd7f78d79dd192ca42d2adac9d9f1fd5
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Basic functions for resource tracking
6 Lang: english
7 */
9 #include "rt.h"
10 #if 0
11 #define ENABLE_RT 0 /* no RT inside this file */
12 #define RT_INTERNAL 1
13 #include <aros/rt.h>
15 #include <exec/lists.h>
16 #include <aros/system.h>
17 #include <exec/tasks.h>
18 #include <exec/ports.h>
19 #include <exec/memory.h>
20 #include <exec/execbase.h>
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <proto/alib.h>
25 #include "etask.h"
26 #endif
27 #include <proto/exec.h>
28 #include <proto/arossupport.h>
29 #include <proto/intuition.h>
31 typedef struct
33 RTNode Node;
34 struct Screen * Screen;
36 ScreenResource;
38 typedef struct
40 RTNode Node;
41 struct Window * Window;
43 WindowResource;
45 static IPTR RT_OpenScreen (RTData * rtd, ScreenResource * rt, va_list args, BOOL * success);
46 static IPTR RT_CloseScreen (RTData * rtd, ScreenResource * rt);
47 static IPTR RT_ShowErrorScreen (RTData * rtd, int, ScreenResource *, IPTR, int, const char * file, ULONG line, va_list);
48 static IPTR RT_CheckScreen (RTData * rtd, int desc, const char * file, ULONG line, ULONG op, va_list args);
50 static IPTR RT_OpenWindow (RTData * rtd, WindowResource * rt, va_list args, BOOL * success);
51 static IPTR RT_CloseWindow (RTData * rtd, WindowResource * rt);
52 static IPTR RT_ShowErrorWindow (RTData * rtd, int, WindowResource *, IPTR, int, const char * file, ULONG line, va_list);
53 static IPTR RT_CheckWindow (RTData * rtd, int desc, const char * file, ULONG line, ULONG op, va_list args);
55 static const RTDesc RT_IntuitionResources[] =
57 { /* RTT_SCREEN */
58 sizeof (ScreenResource),
59 (RT_AllocFunc) RT_OpenScreen,
60 (RT_FreeFunc) RT_CloseScreen,
61 RT_Search,
62 (RT_ShowError) RT_ShowErrorScreen,
63 (RT_CheckFunc) RT_CheckScreen,
65 { /* RTT_WINDOW */
66 sizeof (WindowResource),
67 (RT_AllocFunc) RT_OpenWindow,
68 (RT_FreeFunc) RT_CloseWindow,
69 RT_Search,
70 (RT_ShowError) RT_ShowErrorWindow,
71 (RT_CheckFunc) RT_CheckWindow,
75 void RT_InitIntuition (void)
77 RT_Resources[RTT_SCREEN] = &RT_IntuitionResources[0];
78 RT_Resources[RTT_WINDOW] = &RT_IntuitionResources[1];
81 void RT_ExitIntuition (void)
85 /**************************************
86 RT Screens
87 **************************************/
89 static IPTR RT_OpenScreen (RTData * rtd, ScreenResource * rt, va_list args, BOOL * success)
91 struct NewScreen * ns;
92 struct TagItem * tags = NULL;
93 int op;
95 op = va_arg (args, int);
96 ns = va_arg (args, struct NewScreen *);
98 switch (op)
100 case RTTO_OpenScreenTags:
101 #ifdef AROS_SLOWSTACKTAGS
103 Tag t = va_arg(args, Tag);
104 tags = GetTagsFromStack(t, args);
106 #else
107 tags = (struct TagItem *)args;
108 #endif
109 break;
111 case RTTO_OpenScreenTagList:
112 tags = va_arg (args, struct TagItem *);
113 break;
117 if (!CheckPtr (ns, NULL_PTR))
119 kprintf ("OpenScreen(): Illegal NewScreen pointer\n"
120 " NewScreen=%p at %s:%d\n"
121 , ns
122 , rt->Node.File, rt->Node.Line
124 return 0ul;
126 else if (!CheckPtr (tags, NULL_PTR))
128 kprintf ("OpenScreenTagList(): Illegal TagItem pointer\n"
129 " tagList=%p at %s:%d\n"
130 , tags
131 , rt->Node.File, rt->Node.Line
133 return 0ul;
136 rt->Screen = OpenScreenTagList (ns, tags);
138 #ifdef AROS_SLOWSTACKTAGS
139 if (op == RTTO_OpenScreenTags)
140 FreeTagsFromStack(tags);
141 #endif
143 if (rt->Screen)
144 *success = TRUE;
146 return (IPTR)(rt->Screen);
147 } /* RT_OpenScreen */
149 /* To safely pass an empty va_list, we need to use
150 * the following wrapper.
152 static inline IPTR RT_Search_ap(RTData *rtd, int type, RTNode **prtwin, ...)
154 IPTR ret;
155 va_list ap;
157 va_start(ap, prtwin);
159 ret = RT_Search(rtd, type, prtwin, ap);
161 va_end(ap);
163 return ret;
166 static IPTR RT_CloseScreen (RTData * rtd, ScreenResource * rt)
168 if (rt->Screen->FirstWindow)
170 struct Window * win;
171 WindowResource * rtwin;
172 APTR prtwin = &rtwin;
174 kprintf ("CloseScreen(): There are still windows open on this screen\n"
175 " Screen=%p opened at %s:%d\n"
176 , rt->Screen
177 , rt->Node.File, rt->Node.Line
180 while ((win = rt->Screen->FirstWindow))
182 if (RT_Search_ap (rtd, RTT_WINDOW, (RTNode **)prtwin) == RT_SEARCH_FOUND)
184 RT_FreeResource (rtd, RTT_WINDOW, (RTNode *)rtwin);
186 else
188 kprintf (" Window=%p not tracked by the RT system\n"
189 , win
191 CloseWindow (win);
194 } /* Check for windows */
196 /* Close the screen */
197 CloseScreen (rt->Screen);
199 return TRUE;
200 } /* RT_CloseScreen */
202 static IPTR RT_ShowErrorScreen (RTData * rtd, int rtt, ScreenResource * rt,
203 IPTR ret, int mode, const char * file, ULONG line, va_list args)
205 if (mode != RT_EXIT)
207 const char * modestr = (mode == RT_FREE) ? "Close" : "Check";
208 struct Screen * scr;
210 scr = va_arg (args, struct Screen *);
212 switch (ret)
214 case RT_SEARCH_FOUND:
215 if (rt->Node.Flags & RTNF_DONT_FREE)
217 kprintf ("RT%s: Try to free read-only resource: Screen\n"
218 " %s at %s:%d\n"
219 " Added at %s:%d\n"
220 " Screen=%p\n"
221 , modestr
222 , modestr
223 , file, line
224 , rt->Node.File, rt->Node.Line
225 , rt->Screen
228 break;
230 case RT_SEARCH_NOT_FOUND:
231 kprintf ("RT%s: Screen not found\n"
232 " %s at %s:%d\n"
233 " Screen=%p\n"
234 , modestr
235 , modestr
236 , file, line
237 , scr
239 break;
241 } /* switch */
243 else
245 kprintf ("RTExit: Screen was not closed\n"
246 " Opened at %s:%d\n"
247 " Screen=%p\n"
248 , rt->Node.File, rt->Node.Line
249 , rt->Screen
253 return ret;
254 } /* RT_ShowErrorScreen */
256 static IPTR RT_CheckScreen (RTData * rtd, int rtt,
257 const char * file, ULONG line,
258 ULONG op, va_list args)
260 ScreenResource * rt;
261 APTR prt = &rt;
263 if (RT_Search (rtd, rtt, (RTNode **)prt, args) != RT_SEARCH_FOUND)
264 rt = NULL;
266 switch (op)
268 case RTTO_ScreenToFront:
270 struct Screen * scr = va_arg (args, struct Screen *);
272 if (!rt)
274 kprintf ("ScreenToFont(): Illegal window pointer\n"
275 " Screen=%p at %s:%d\n"
276 , scr
277 , file, line
280 return -1;
283 ScreenToFront (scr);
285 return 0;
288 case RTTO_ScreenToBack:
290 struct Screen * scr = va_arg (args, struct Screen *);
292 if (!rt)
294 kprintf ("ScreenToBack(): Illegal window pointer\n"
295 " Screen=%p at %s:%d\n"
296 , scr
297 , file, line
300 return -1;
303 ScreenToBack (scr);
305 return 0;
310 return 0L;
311 } /* RT_CheckScreen */
314 /**************************************
315 RT Windows
316 **************************************/
318 static IPTR RT_OpenWindow (RTData * rtd, WindowResource * rt, va_list args, BOOL * success)
320 struct NewWindow * nw;
321 struct TagItem * tags = NULL;
322 int op;
324 op = va_arg (args, int);
325 nw = va_arg (args, struct NewWindow *);
327 switch (op)
329 case RTTO_OpenWindowTags:
330 #ifdef AROS_SLOWSTACKTAGS
332 Tag t = va_arg (args, Tag);
333 tags = GetTagsFromStack(t, args);
335 #else
336 tags = (struct TagItem *)args;
337 #endif
338 break;
340 case RTTO_OpenWindowTagList:
341 tags = va_arg (args, struct TagItem *);
342 break;
346 if (!CheckPtr (nw, NULL_PTR))
348 kprintf ("OpenWindow(): Illegal NewWindow pointer\n"
349 " NewWindow=%p at %s:%d\n"
350 , nw
351 , rt->Node.File, rt->Node.Line
353 return 0ul;
355 else if (!CheckPtr (tags, NULL_PTR))
357 kprintf ("OpenWindowTagList(): Illegal TagList pointer\n"
358 " tagList=%p at %s:%d\n"
359 , nw
360 , rt->Node.File, rt->Node.Line
362 return 0ul;
365 rt->Window = OpenWindowTagList (nw, tags);
367 if (rt->Window->UserPort)
368 RT_IntTrack (RTT_PORT, __FILE__, __LINE__, rt->Window->UserPort);
370 if (rt->Window)
371 *success = TRUE;
373 #ifdef AROS_SLOWSTACKTAGS
374 if (op == RTTO_OpenWindowTags)
375 FreeTagsFromStack(tags);
376 #endif
378 return (IPTR)(rt->Window);
379 } /* RT_OpenWindow */
381 static IPTR RT_CloseWindow (RTData * rtd, WindowResource * rt)
383 CloseWindow (rt->Window);
385 return TRUE;
386 } /* RT_CloseWindow */
388 static IPTR RT_ShowErrorWindow (RTData * rtd, int rtt, WindowResource * rt,
389 IPTR ret, int mode, const char * file, ULONG line, va_list args)
391 if (mode != RT_EXIT)
393 const char * modestr = (mode == RT_FREE) ? "Close" : "Check";
394 struct Window * win;
396 win = va_arg (args, struct Window *);
398 switch (ret)
400 case RT_SEARCH_FOUND:
401 if (rt->Node.Flags & RTNF_DONT_FREE)
403 kprintf ("RT%s: Try to free read-only resource: Window\n"
404 " %s at %s:%d\n"
405 " Added at %s:%d\n"
406 " Window=%p\n"
407 , modestr
408 , modestr
409 , file, line
410 , rt->Node.File, rt->Node.Line
411 , rt->Window
414 break;
416 case RT_SEARCH_NOT_FOUND:
417 kprintf ("RT%s: Window not found\n"
418 " %s at %s:%d\n"
419 " Window=%p\n"
420 , modestr
421 , modestr
422 , file, line
423 , win
425 break;
427 } /* switch */
429 else
431 kprintf ("RTExit: Window was not closed\n"
432 " Opened at %s:%d\n"
433 " Window=%p\n"
434 , rt->Node.File, rt->Node.Line
435 , rt->Window
439 return ret;
440 } /* RT_ShowErrorWindow */
442 static IPTR RT_CheckWindow (RTData * rtd, int rtt,
443 const char * file, ULONG line,
444 ULONG op, va_list args)
446 WindowResource * rt;
447 APTR prt = &rt;
449 if (RT_Search (rtd, rtt, (RTNode **)prt, args) != RT_SEARCH_FOUND)
450 rt = NULL;
452 switch (op)
454 case RTTO_WindowToFront:
456 struct Window * win = va_arg (args, struct Window *);
458 if (!rt)
460 kprintf ("WindowToFont(): Illegal window pointer\n"
461 " Window=%p at %s:%d\n"
462 , win
463 , file, line
466 return -1;
469 WindowToFront (win);
471 return 0;
474 case RTTO_WindowToBack:
476 struct Window * win = va_arg (args, struct Window *);
478 if (!rt)
480 kprintf ("WindowToBack(): Illegal window pointer\n"
481 " Window=%p at %s:%d\n"
482 , win
483 , file, line
486 return -1;
489 WindowToBack (win);
491 return 0;
494 } /* switch (op) */
496 return 0L;
497 } /* RT_CheckWindow */