revert commit 56204.
[AROS.git] / rom / usb / classes / arosx / arosx.library.c
bloba0840b2b622dd05c5df97d7e7341b11d0237cc55
1 /*
2 Copyright © 2019, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <aros/libcall.h>
7 #include <aros/symbolsets.h>
9 #include <exec/types.h>
10 #include <exec/libraries.h>
12 #include <proto/exec.h>
14 #include "debug.h"
16 #include "arosx.class.h"
17 #include "include/arosx.h"
19 BOOL AROSXClass_SendEvent(LIBBASETYPEPTR arosxb, ULONG ehmt, APTR param1, APTR param2);
21 static const UBYTE libarosx[] = "arosx.library";
23 AROS_UFH3(struct AROSXBase *, libInit, AROS_UFHA(struct AROSXBase *, base, D0), AROS_UFHA(BPTR, seglist, A0), AROS_UFHA(struct ExecBase *, SysBase, A6)) {
24 AROS_USERFUNC_INIT
26 mybug(-1, ("[AROSXLib] libInit base: 0x%08lx seglist: 0x%08lx SysBase: 0x%08lx\n", base, seglist, SysBase));
28 base->arosx_LibNode.lib_Node.ln_Type = NT_LIBRARY;
29 base->arosx_LibNode.lib_Node.ln_Name = (UBYTE*)libarosx;
30 base->arosx_LibNode.lib_Flags = LIBF_SUMUSED|LIBF_CHANGED;
31 base->arosx_LibNode.lib_Version = 0;
32 base->arosx_LibNode.lib_Revision = 1;
33 base->arosx_LibNode.lib_IdString = (UBYTE*)libarosx;
35 /* Store segment, don't have one... */
36 //base->arosx_LibNode.np_SegList = seglist;
38 return(base);
40 AROS_USERFUNC_EXIT
43 AROS_LH1(struct AROSXBase *, libOpen, AROS_LHA(ULONG, version, D0), struct AROSXBase *, base, 1, lib) {
44 AROS_LIBFUNC_INIT
46 ++base->arosx_LibNode.lib_OpenCnt;
47 base->arosx_LibNode.lib_Flags &= ~LIBF_DELEXP;
49 return base;
51 AROS_LIBFUNC_EXIT
54 AROS_LH0(BPTR, libClose, struct AROSXBase *, base, 2, lib) {
55 AROS_LIBFUNC_INIT
57 BPTR ret;
58 ret = BNULL;
60 if(--base->arosx_LibNode.lib_OpenCnt == 0) {
61 if(base->arosx_LibNode.lib_Flags & LIBF_DELEXP)
63 mybug(-1, ("[AROSXLib] libClose: calling expunge...\n"));
64 ret = AROS_LC1(BPTR, libExpunge, AROS_LCA(struct AROSXBase *, base, D0), struct AROSXBase *, base, 3, lib);
68 return(ret);
70 AROS_LIBFUNC_EXIT
73 AROS_LH0(BPTR, libExpunge, struct AROSXBase *, base, 3, lib) {
74 AROS_LIBFUNC_INIT
76 BPTR ret;
79 CHECME: Our memory belongs to arosx.class, make sure we free only allocated memory
82 mybug(-1, ("[AROSXLib] libExpunge base: 0x%08lx\n", base));
84 ret = BNULL;
86 if(base->arosx_LibNode.lib_OpenCnt == 0)
88 mybug(-1, ("[AROSXLib] libExpunge: Unloading...\n"));
90 //ret = base->np_SegList;
92 mybug(-1, ("[AROSXLib] libExpunge: removing library node 0x%08lx\n", &base->arosx_LibNode.lib_Node));
93 Remove(&base->arosx_LibNode.lib_Node);
95 mybug(-1, ("[AROSXLib] libExpunge: FreeMem()...\n"));
96 FreeMem((char *) base - base->arosx_LibNode.lib_NegSize, (ULONG) (base->arosx_LibNode.lib_NegSize + base->arosx_LibNode.lib_PosSize));
98 mybug(-1, ("[AROSXLib] libExpunge: Unloading done! arosx.library expunged!\n"));
100 return(ret);
102 else
104 mybug(-1, ("[AROSXLib] libExpunge: Could not expunge, LIBF_DELEXP set!\n"));
105 base->arosx_LibNode.lib_Flags |= LIBF_DELEXP;
108 return(BNULL);
110 AROS_LIBFUNC_EXIT
113 AROS_LH0(IPTR, libReserved, struct AROSXBase *, base, 4, lib) {
114 AROS_LIBFUNC_INIT
115 return (IPTR)NULL;
116 AROS_LIBFUNC_EXIT
119 AROS_LH2(struct AROSX_EventHook *, AROSX_AddEventHandler,
120 AROS_LHA(struct MsgPort *, mp, A1),
121 AROS_LHA(ULONG, msgmask, D0),
122 struct AROSXBase *, base, 5, lib)
124 AROS_LIBFUNC_INIT
126 struct AROSXClassBase *arosxb;
127 arosxb = base->arosxb;
129 struct AROSXClassController *arosxc;
131 struct AROSX_EventHook *eh = NULL;
133 mybug(-1, ("AROSX_AddEventHandler(%p, %p)\n", mp, msgmask));
135 if(mp) {
136 if((eh = AllocVec(sizeof(struct AROSX_EventHook), (MEMF_CLEAR|MEMF_ANY)))) {
137 eh->eh_MsgPort = mp;
138 eh->eh_MsgMask = msgmask;
139 ObtainSemaphore(&arosxb->event_lock);
140 AddTail(&base->arosxb->event_port_list, &eh->eh_Node);
141 ReleaseSemaphore(&arosxb->event_lock);
144 Send connect events from those controllers that are already connected and inluded in the mask
147 ObtainSemaphore(&arosxb->arosxc_lock);
149 arosxc = arosxb->arosxc_0;
150 if( (((msgmask>>28) & 1) && (arosxc->status.connected)) ) {
151 AROSXClass_SendEvent(arosxb, ((1<<28) | ((arosxc->controller_type)<<20) | AROSX_EHMF_CONNECT), (APTR)1, (APTR)2);
152 mybug(-1, ("Sent connect event for 0\n"));
155 arosxc = arosxb->arosxc_1;
156 if( (((msgmask>>28) & 2) && (arosxc->status.connected)) ) {
157 AROSXClass_SendEvent(arosxb, ((2<<28) | ((arosxc->controller_type)<<20) | AROSX_EHMF_CONNECT), (APTR)1, (APTR)2);
158 mybug(-1, ("Sent connect event for 1\n"));
161 arosxc = arosxb->arosxc_2;
162 if( (((msgmask>>28) & 4) && (arosxc->status.connected)) ) {
163 AROSXClass_SendEvent(arosxb, ((4<<28) | ((arosxc->controller_type)<<20) | AROSX_EHMF_CONNECT), (APTR)1, (APTR)2);
164 mybug(-1, ("Sent connect event for 2\n"));
167 arosxc = arosxb->arosxc_3;
168 if( (((msgmask>>28) & 8) && (arosxc->status.connected)) ) {
169 AROSXClass_SendEvent(arosxb, ((8<<28) | ((arosxc->controller_type)<<20) | AROSX_EHMF_CONNECT), (APTR)1, (APTR)2);
170 mybug(-1, ("Sent connect event for 3\n"));
173 ReleaseSemaphore(&arosxb->arosxc_lock);
179 return(eh);
180 AROS_LIBFUNC_EXIT
183 AROS_LH1(void, AROSX_RemEventHandler,
184 AROS_LHA(struct AROSX_EventHook *, eh, A0),
185 struct AROSXBase *, base, 6, lib)
187 AROS_LIBFUNC_INIT
189 struct AROSXClassBase *arosxb;
190 arosxb = base->arosxb;
192 struct Message *msg;
194 mybug(-1, ("AROSX_RemEventHandler(%p)\n", eh));
195 if(!eh) {
196 return;
199 ObtainSemaphore(&arosxb->event_lock);
200 Remove(&eh->eh_Node);
201 while((msg = GetMsg(eh->eh_MsgPort))) {
202 ReplyMsg(msg);
204 ReleaseSemaphore(&arosxb->event_lock);
206 mybug(-1, ("AROSX_RemEventHandler garbage collector\n"));
208 struct AROSX_EventNote *en;
209 while((en = (struct AROSX_EventNote *) GetMsg(&arosxb->event_reply_port))) {
210 mybug(-1, (" Free AROSX_EventNote(%p)\n", en));
211 FreeVec(en);
214 FreeVec(eh);
216 AROS_LIBFUNC_EXIT
222 static const APTR libFuncTable[] = {
223 &AROS_SLIB_ENTRY(libOpen, lib, 1),
224 &AROS_SLIB_ENTRY(libClose, lib, 2),
225 &AROS_SLIB_ENTRY(libExpunge, lib, 3),
226 &AROS_SLIB_ENTRY(libReserved, lib, 4),
227 &AROS_SLIB_ENTRY(AROSX_AddEventHandler, lib, 5),
228 &AROS_SLIB_ENTRY(AROSX_RemEventHandler, lib, 6),
229 (APTR) -1,
232 struct AROSXBase * AROSXInit(void) {
234 struct AROSXBase *lib;
236 mybug(-1,("AROSXInit\n"));
238 if((lib = (struct AROSXBase *)MakeLibrary((APTR) libFuncTable, NULL, (APTR) libInit, sizeof(struct AROSXBase), NULL))) {
239 Forbid();
240 AddLibrary((struct Library *)lib);
241 lib->arosx_LibNode.lib_OpenCnt++;
242 Permit();
243 } else {
244 mybug(-1, ("failed to create arosx.library\n"));
247 mybug(-1,("AROSXInit base 0x%08lx\n", lib));
248 mybug(-1,("AROSXInit done\n"));
250 return lib;