2 Copyright © 2019, The AROS Development Team. All rights reserved.
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>
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
)) {
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;
43 AROS_LH1(struct AROSXBase
*, libOpen
, AROS_LHA(ULONG
, version
, D0
), struct AROSXBase
*, base
, 1, lib
) {
46 ++base
->arosx_LibNode
.lib_OpenCnt
;
47 base
->arosx_LibNode
.lib_Flags
&= ~LIBF_DELEXP
;
54 AROS_LH0(BPTR
, libClose
, struct AROSXBase
*, base
, 2, lib
) {
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
);
73 AROS_LH0(BPTR
, libExpunge
, struct AROSXBase
*, base
, 3, lib
) {
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
));
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"));
104 mybug(-1, ("[AROSXLib] libExpunge: Could not expunge, LIBF_DELEXP set!\n"));
105 base
->arosx_LibNode
.lib_Flags
|= LIBF_DELEXP
;
113 AROS_LH0(IPTR
, libReserved
, struct AROSXBase
*, base
, 4, lib
) {
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
)
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
));
136 if((eh
= AllocVec(sizeof(struct AROSX_EventHook
), (MEMF_CLEAR
|MEMF_ANY
)))) {
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
);
183 AROS_LH1(void, AROSX_RemEventHandler
,
184 AROS_LHA(struct AROSX_EventHook
*, eh
, A0
),
185 struct AROSXBase
*, base
, 6, lib
)
189 struct AROSXClassBase
*arosxb
;
190 arosxb
= base
->arosxb
;
194 mybug(-1, ("AROSX_RemEventHandler(%p)\n", eh
));
199 ObtainSemaphore(&arosxb
->event_lock
);
200 Remove(&eh
->eh_Node
);
201 while((msg
= GetMsg(eh
->eh_MsgPort
))) {
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
));
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),
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
))) {
240 AddLibrary((struct Library
*)lib
);
241 lib
->arosx_LibNode
.lib_OpenCnt
++;
244 mybug(-1, ("failed to create arosx.library\n"));
247 mybug(-1,("AROSXInit base 0x%08lx\n", lib
));
248 mybug(-1,("AROSXInit done\n"));