Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / libs / camd / drivers.c
blob654dd9fac7d1dc40ba7be530eba4ace0c265256e
1 /*
2 Copyright © 1995-2007, 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 #include <proto/camd.h>
12 #include <proto/utility.h>
14 #ifdef __amigaos4__
15 # include <proto/CamdDriver.h>
16 # include <exec/emulation.h>
17 #endif
20 #include "camd_intern.h"
22 # undef DEBUG
23 # define DEBUG 1
24 # include AROS_DEBUG_H_FILE
26 BOOL OpenDriver(struct DriverData *driverdata,ULONG *ErrorCode,struct CamdBase *CamdBase){
27 if(
28 CreateReceiverProc(
29 driverdata,
30 driverdata->mididevicedata->Name,
31 driverdata->portnum,
32 ErrorCode,
33 CamdBase
34 )==FALSE
36 return FALSE;
39 #ifndef __amigaos4__
40 driverdata->midiportdata=(*driverdata->mididevicedata->OpenPort)(
41 #else
42 driverdata->midiportdata= driverdata->mididevicedata->ICamdDriver->OpenPort(
43 #endif
44 driverdata->mididevicedata,
45 driverdata->portnum,
46 (ULONG (* ASM)(APTR REG(a2)))Transmitter,
47 (void (* ASM)(UWORD REG(d0),APTR REG(a2))) Receiver,
48 driverdata
51 if(driverdata->midiportdata==NULL){
52 D(bug("camd.library: drivers.c/OpenDriver. (*OpenPort) failed...\n"));
53 EndReceiverProc(driverdata,CamdBase);
54 if(ErrorCode!=NULL){
55 *ErrorCode=CME_NoUnit(driverdata->portnum);
57 return FALSE;
60 return TRUE;
64 void CloseDriver(struct DriverData *driverdata,struct CamdBase *CamdBase){
65 #ifndef __amigaos4__
66 (*driverdata->mididevicedata->ClosePort)(
67 #else
68 driverdata->mididevicedata->ICamdDriver->ClosePort(
69 #endif
70 driverdata->mididevicedata,
71 driverdata->portnum
73 EndReceiverProc(driverdata,CamdBase);
76 BOOL AllocDriverData(
77 struct Drivers *driver,
78 struct MidiDeviceData *mididevicedata,
79 struct CamdBase *CamdBase
81 ULONG nports;
82 LONG lokke;
84 struct DriverData *driverdata;
86 struct MidiCluster *cluster;
87 char nametemp[256];
89 nports=driver->numports;
91 driver->driverdatas=AllocMem((ULONG)(sizeof(struct DriverData *)*nports),MEMF_ANY|MEMF_CLEAR|MEMF_PUBLIC);
92 if(driver->driverdatas==NULL){
93 return FALSE;
96 for(lokke=0;lokke<nports;lokke++){
97 driverdata=AllocMem(sizeof(struct DriverData),MEMF_ANY | MEMF_CLEAR | MEMF_PUBLIC);
98 if(driverdata==NULL){
99 return FALSE;
101 driver->driverdatas[lokke]=driverdata;
103 driverdata->innode.ln_Type=NT_USER-MLTYPE_NTypes;
104 driverdata->innode.ln_Pri=127; // Should be set lower?
106 driverdata->outnode.ln_Type=NT_USER-MLTYPE_NTypes;
107 driverdata->outnode.ln_Pri=127; // Should be set lower?
109 driverdata->mididevicedata=mididevicedata;
111 driverdata->buffer=AllocVec(sizeof(ULONG)*OUTBUFFERSIZE,MEMF_ANY|MEMF_PUBLIC);
112 if(driverdata->buffer==NULL){
113 return FALSE;
116 driverdata->buffercurrsend=driverdata->buffer;
117 driverdata->buffercurr=driverdata->buffer;
118 driverdata->bufferend=driverdata->buffer+OUTBUFFERSIZE;
119 driverdata->sendpos=0;
121 driverdata->buffer_rt=AllocVec(sizeof(UBYTE)*OUTBUFFERSIZE_RT,MEMF_ANY|MEMF_PUBLIC);
122 if(driverdata->buffer_rt==NULL){
123 return FALSE;
125 driverdata->buffercurrsend_rt=driverdata->buffer_rt;
126 driverdata->buffercurr_rt=driverdata->buffer_rt;
127 driverdata->bufferend=driverdata->buffer+OUTBUFFERSIZE_RT;
130 driverdata->status=0;
131 driverdata->portnum=lokke;
133 driverdata->Input_Treat=Receiver_init;
135 driverdata->re_start=AllocVec((ULONG)(sizeof(UWORD)*RECEIVERPROCBUFFERSIZE),MEMF_ANY|MEMF_PUBLIC);
136 if(driverdata->re_start==NULL){
137 return FALSE;
139 driverdata->re_write=driverdata->re_start;
140 driverdata->re_end=driverdata->re_start+RECEIVERPROCBUFFERSIZE;
141 driverdata->re_read=driverdata->re_start;
144 mysprintf(CamdBase,nametemp,"%s.in.%ld",mididevicedata->Name,lokke);
145 if((cluster=NewCluster(nametemp,CamdBase))==FALSE){
146 return FALSE;
148 driverdata->incluster=(struct MyMidiCluster *)cluster;
150 mysprintf(CamdBase,nametemp,"%s.out.%ld",mididevicedata->Name,lokke);
151 if((cluster=NewCluster(nametemp,CamdBase))==FALSE){
152 return FALSE;
154 driverdata->outcluster=(struct MyMidiCluster *)cluster;
156 InitSemaphore(&driverdata->sendsemaphore);
157 InitSemaphore(&driverdata->sysexsemaphore);
159 driverdata->isOutOpen=FALSE;
160 driverdata->isInOpen=FALSE;
163 for(lokke=0;lokke<nports;lokke++){
164 driverdata=driver->driverdatas[lokke];
165 AddClusterSender(&driverdata->incluster->cluster,&driverdata->innode,NULL,CamdBase);
166 AddClusterReceiver(&driverdata->outcluster->cluster,&driverdata->outnode,NULL,CamdBase);
169 return TRUE;
173 void FreeDriverData(struct Drivers *driver,
174 struct CamdBase *CamdBase
176 struct DriverData *driverdata;
177 ULONG lokke;
179 if(driver->driverdatas!=NULL){
180 for(lokke=0;lokke<driver->numports;lokke++){
181 driverdata=driver->driverdatas[lokke];
182 RemoveCluster(&driverdata->incluster->cluster,CamdBase);
183 RemoveCluster(&driverdata->outcluster->cluster,CamdBase);
187 if(driver->driverdatas!=NULL){
188 for(lokke=0;lokke<driver->numports;lokke++){
189 driverdata=driver->driverdatas[lokke];
190 if(driverdata!=NULL){
191 if(driverdata->buffer!=NULL){
192 FreeVec(driverdata->buffer);
194 if(driverdata->buffer_rt!=NULL){
195 FreeVec(driverdata->buffer_rt);
197 if(driverdata->re_start!=NULL){
198 FreeVec(driverdata->re_start);
202 FreeMem(driver->driverdatas,sizeof(struct DriverData *)*driver->numports);
206 struct Drivers *FindPrevDriverForMidiDeviceData(
207 struct MidiDeviceData *mididevicedata,
208 struct CamdBase *CamdBase
210 struct Drivers *driver,*temp=NULL;
212 driver=CB(CamdBase)->drivers;
214 while(driver->mididevicedata!=mididevicedata){
215 temp=driver;
216 driver=driver->next;
219 return temp;
222 #ifdef __amigaos4__
223 void LoadDriver(char *name,
224 struct CamdIFace *ICamd
226 CAMD_LIBBASE_DECL(struct CamdBase *,CamdBase)
227 #else
228 void LoadDriver(char *name,
229 struct CamdBase *CamdBase
231 #endif
232 struct Drivers *driver = NULL;
233 struct MidiDeviceData *mididevicedata = NULL;
235 D(bug("camd.library: drivers.c/LoadDriver - trying to open %s..\n",name));
237 mididevicedata=OpenMidiDevice(name);
238 D(bug("camd.library: drivers.c/LoadDriver - It was%s a success..\n",mididevicedata==NULL?" not":""));
240 if(mididevicedata==NULL) return;
242 if((mididevicedata->Flags&1)==0){
243 #ifdef __AROS__
244 D(bug("%s: mididevicedata->Flags&1==0 is not not supported for AROS!\n",name));
245 CloseMidiDevice(mididevicedata);
246 return;
247 #else
248 D(bug("%s: old camd driver format. (Supported)\n",name));
249 #endif
252 #ifndef __amigaos4__
253 if((*mididevicedata->Init)(SysBase)==FALSE){
254 CloseMidiDevice(mididevicedata);
255 return;
257 #else
258 #endif
260 //D(bug("%s %s %u %u\n", mididevicedata->Name, mididevicedata->IDString, mididevicedata->NPorts,mididevicedata->Flags));
262 driver=FindPrevDriverForMidiDeviceData(mididevicedata,CamdBase);
263 if(driver==NULL){
264 driver=CB(CamdBase)->drivers;
265 }else{
266 driver=driver->next;
269 driver->numports=mididevicedata->NPorts;
271 if(AllocDriverData(driver,mididevicedata,CamdBase)==FALSE){
272 FreeDriverData(driver,CamdBase);
273 CloseMidiDevice(mididevicedata);
274 return;