Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / libs / camd / openmididevice.c
blob595f298a19987979bc1654190c041966c4f929fb
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: English
7 */
9 #include <proto/exec.h>
10 #include <proto/dos.h>
11 #ifdef __amigaos4__
12 # include <proto/CamdDriver.h>
13 #endif
14 #include "camd_intern.h"
16 #ifdef __AROS__
17 # undef DEBUG
18 # define DEBUG 1
19 # include <aros/debug.h>
20 #endif
22 BOOL isPointerInSeglist(APTR pointer,BPTR seglist,ULONG minsize);
24 /*****************************************************************************
26 NAME */
28 AROS_LH1(struct MidiDeviceData *, OpenMidiDevice,
30 /* SYNOPSIS */
31 AROS_LHA(UBYTE *, name, A0),
33 /* LOCATION */
34 struct CamdBase *, CamdBase, 34, Camd)
36 /* FUNCTION
37 Remind me to fill in things here later.
39 INPUTS
41 RESULT
43 NOTES
45 EXAMPLE
47 BUGS
49 SEE ALSO
50 CloseMidiDevice()
52 INTERNALS
54 HISTORY
56 2001-01-12 ksvalast first created
58 *****************************************************************************/
60 AROS_LIBFUNC_INIT
62 struct Drivers *driver;
64 #ifndef __amigaos4__
65 BPTR seglist,seg;
66 struct MidiDeviceData *mididevicedata;
69 STRPTR addr;
70 ULONG size;
72 seg=seglist=LoadSeg(name);
74 if(seglist==BNULL) return NULL;
76 // The code here is partly taken from AROS/rom/dos/lddemon.c - LDInit()
78 while(seg!=BNULL){
79 addr=(STRPTR)(BADDR(seg)-sizeof(ULONG));
80 size=*(ULONG *)addr;
82 for(
83 addr+=sizeof(BPTR)+sizeof(ULONG),
84 size-=sizeof(BPTR)+sizeof(ULONG); // Is this a bug? (- -> + ?)
85 size>=sizeof(struct MidiDeviceData);
86 size-=AROS_PTRALIGN,addr+=AROS_PTRALIGN
88 mididevicedata=(struct MidiDeviceData *)addr;
91 /* Do some tests to check that we have got a correct mididevicedata.
92 It's not failproof, but the chance for this to fail should be small.
94 mididevicedata->Magic==MDD_Magic && //Hopefully, this one should only succeed once.
95 mididevicedata->Name!=NULL &&
96 mididevicedata->Init!=NULL &&
97 isPointerInSeglist(mididevicedata->Init,seglist,4) &&
98 #ifndef __AROS__
99 (((ULONG)(mididevicedata->Init)&(AROS_PTRALIGN-1))==0) &&
100 #endif
101 isPointerInSeglist(
102 mididevicedata->Name,
103 seglist,mystrlen(findonlyfilename(name))
106 mystrcmp(findonlyfilename(name),mididevicedata->Name)==TRUE &&
108 mididevicedata->Expunge==NULL ||
110 isPointerInSeglist(mididevicedata->Expunge,seglist,4)
111 #ifndef __AROS__
112 && (((ULONG)(mididevicedata->Expunge)&(AROS_PTRALIGN-1))==0)
113 #endif
118 mididevicedata->OpenPort==NULL ||
120 #ifndef __AROS__
121 (((ULONG)(mididevicedata->OpenPort)&(AROS_PTRALIGN-1))==0) &&
122 #endif
123 isPointerInSeglist(mididevicedata->OpenPort,seglist,4)
128 mididevicedata->ClosePort==NULL ||
130 #ifndef __AROS__
131 (((ULONG)(mididevicedata->ClosePort)&(AROS_PTRALIGN-1))==0) &&
132 #endif
133 isPointerInSeglist(mididevicedata->ClosePort,seglist,4)
137 mididevicedata->IDString!=NULL &&
138 isPointerInSeglist(mididevicedata->IDString,seglist,4)
143 driver=AllocMem(sizeof(struct Drivers),MEMF_ANY | MEMF_CLEAR | MEMF_PUBLIC);
144 if(driver==NULL){
145 UnLoadSeg(seglist);
146 return NULL;
148 driver->seglist=seglist;
149 driver->mididevicedata=mididevicedata;
151 ObtainSemaphore(CB(CamdBase)->CLSemaphore);
152 driver->next=CB(CamdBase)->drivers;
153 CB(CamdBase)->drivers=driver;
154 ReleaseSemaphore(CB(CamdBase)->CLSemaphore);
155 return mididevicedata;
158 seg=*(BPTR *)BADDR(seg);
161 UnLoadSeg(seglist);
162 return NULL;
163 #else
165 struct CamdDriverIFace *ICamdDriver;
166 struct Library *CamdDriverBase = (struct CamdDriverLibrary *) OpenLibrary( name, 0 );
168 if( CamdDriverBase == NULL)
170 // DebugPrintF("Failed to load %s\n", name);
171 return NULL;
174 if ((ICamdDriver = (struct CamdDriverIFace *) GetInterface((struct Library *) CamdDriverBase, "main", 1, NULL)) == NULL)
176 // DebugPrintF("Failed to load interface for %s\n", name);
177 CloseLibrary(CamdDriverBase);
178 return NULL;
182 driver=AllocMem(sizeof(struct Drivers),MEMF_ANY | MEMF_CLEAR | MEMF_PUBLIC);
183 if(driver==NULL){
184 CloseLibrary(CamdDriverBase);
185 return NULL;
187 driver->mididevicedata = AllocMem(sizeof(struct MidiDeviceData), MEMF_ANY | MEMF_CLEAR | MEMF_PUBLIC);
188 driver->mididevicedata->Name = ICamdDriver->GetMidiDeviceData()->Name;
189 driver->mididevicedata->IDString = ICamdDriver->GetMidiDeviceData()->IDString;
190 driver->mididevicedata->Version = ICamdDriver->GetMidiDeviceData()->Version;
191 driver->mididevicedata->Revision = ICamdDriver->GetMidiDeviceData()->Revision;
192 driver->mididevicedata->NPorts = ICamdDriver->GetMidiDeviceData()->NPorts;
193 driver->mididevicedata->Flags = ICamdDriver->GetMidiDeviceData()->Flags;
194 driver->mididevicedata->CamdDriverBase = CamdDriverBase;
195 driver->mididevicedata->ICamdDriver = ICamdDriver;
198 ObtainSemaphore(CB(CamdBase)->CLSemaphore);
199 driver->next=CB(CamdBase)->drivers;
200 CB(CamdBase)->drivers=driver;
201 ReleaseSemaphore(CB(CamdBase)->CLSemaphore);
203 return driver->mididevicedata;
206 #endif
208 AROS_LIBFUNC_EXIT
212 BOOL isPointerInSeglist(APTR pointer,BPTR seglist,ULONG minsize){
213 STRPTR addr;
214 ULONG size;
216 while(seglist!=0){
217 addr=(STRPTR)(BADDR(seglist)-sizeof(ULONG));
218 size=*(ULONG *)addr;
219 addr+=sizeof(BPTR)+sizeof(ULONG);
220 size-=sizeof(BPTR)+sizeof(ULONG);
221 if((STRPTR)pointer>=addr && (STRPTR)pointer<=addr+size-minsize){
222 return TRUE;
224 seglist=*(BPTR *)BADDR(seglist);
227 return FALSE;