revert between 56095 -> 55830 in arch
[AROS.git] / workbench / libs / commodities / commodities_init.c
blob8b1ab1caa0fe5206be0559e97c4b22b419d6eb7b
1 /*
2 Copyright © 1995-2006, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Commodities initialization code.
6 Lang: English.
7 */
9 #define USE_ZERO_OBJECT 0 /* stegerg: no idea why zero object is/was used at all */
11 #ifndef DEBUG
12 #define DEBUG 0
13 #endif
15 #include <utility/utility.h>
16 #include "cxintern.h" /* Must be included after utility.h */
18 #include <aros/debug.h>
20 #include <aros/symbolsets.h>
21 #include <exec/types.h>
22 #include <exec/resident.h>
23 #include <proto/exec.h>
24 #include <devices/timer.h>
26 #include <intuition/classusr.h>
27 #include <exec/libraries.h>
28 #include <exec/alerts.h>
29 #include <libraries/commodities.h>
30 #include <proto/commodities.h>
31 #include LC_LIBDEFS_FILE
33 BOOL InitCx(struct CommoditiesBase *CxBase);
34 BOOL ShutDownCx(struct CommoditiesBase *CxBase);
36 static int Init(struct CommoditiesBase *CxBase)
38 BOOL ok = TRUE;
41 This function is single-threaded by exec by calling Forbid.
42 If you break the Forbid() another task may enter this function
43 at the same time. Take care.
46 D(bug("commodities_open: Entering...\n"));
48 CxBase->cx_TimerMP.mp_Node.ln_Type = NT_MSGPORT;
49 CxBase->cx_TimerMP.mp_Flags = PA_IGNORE;
50 NEWLIST(&CxBase->cx_TimerMP.mp_MsgList);
52 CxBase->cx_TimerIO.tr_node.io_Message.mn_ReplyPort = &CxBase->cx_TimerMP;
53 CxBase->cx_TimerIO.tr_node.io_Message.mn_Length = sizeof(struct timerequest);
55 if (OpenDevice(TIMERNAME, UNIT_VBLANK,
56 (struct IORequest *)&CxBase->cx_TimerIO, 0) == 0)
58 CxBase->cx_TimerBase =
59 (struct Library *)(CxBase->cx_TimerIO.tr_node.io_Device);
61 if (CxBase->cx_TimerBase == NULL)
62 return FALSE;
64 D(bug("commodities_open: Setting up Zero object.\n"));
66 ok = InitCx((struct CommoditiesBase *)CxBase);
68 if (!ok)
70 D(bug("Error: Failed to initialize commodities.library.\n"));
72 return FALSE;
75 D(bug("commodities_open: Library correctly opened.\n"));
77 return TRUE;
81 BOOL InitCx(struct CommoditiesBase *CxBase)
83 #if USE_ZERO_OBJECT
84 CxObj *zero;
85 #endif
87 InitSemaphore(&CxBase->cx_SignalSemaphore);
88 NEWLIST(&CxBase->cx_BrokerList);
89 NEWLIST(&CxBase->cx_MessageList);
90 NEWLIST(&CxBase->cx_GeneratedInputEvents);
92 CxBase->cx_MsgPort.mp_Node.ln_Type = NT_MSGPORT;
93 CxBase->cx_MsgPort.mp_Flags = PA_IGNORE;
94 NEWLIST(&CxBase->cx_MsgPort.mp_MsgList);
96 #if USE_ZERO_OBJECT
97 zero = CreateCxObj(CX_ZERO, (IPTR)NULL, (IPTR)NULL);
99 if (zero == NULL)
101 return FALSE;
104 /* Make sure this object goes LAST in the list */
105 ((struct Node *)zero)->ln_Pri = -128;
107 zero->co_Flags |= COF_VALID;
108 AddHead(&CxBase->cx_BrokerList, (struct Node *)zero);
109 #endif
111 return TRUE;
115 BOOL ShutDownCx(struct CommoditiesBase *CxBase)
117 struct InputEvent *temp;
118 CxMsg *msg;
120 /* Free messages */
121 while ((msg = (CxMsg *)GetMsg(&CxBase->cx_MsgPort)) != NULL)
123 FreeCxStructure(msg, CX_MESSAGE, (struct Library *)CxBase);
126 /* Free input events */
127 while (CxBase->cx_IEvents != NULL)
129 temp = CxBase->cx_IEvents->ie_NextEvent;
130 FreeCxStructure(CxBase->cx_IEvents, CX_INPUTEVENT,
131 (struct Library *)CxBase);
132 CxBase->cx_IEvents = temp;
135 CxBase->cx_IEvents = NULL;
137 #if USE_ZERO_OBJECT
138 /* Remove the ZERO object, in case it exists. */
139 DeleteCxObj((CxObj *)RemHead(&CxBase->cx_BrokerList));
140 #endif
142 return TRUE;
145 ADD2INITLIB(Init, 0);
146 ADD2EXPUNGELIB(ShutDownCx, 0);