Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / devs / AHI / Drivers / Common / library.c
blobc2d087b96e13f33eb1fbd87904a5624ac64887c6
2 /*
3 * Shared library code
4 */
6 #include <config.h>
8 #include <exec/alerts.h>
9 #include <exec/libraries.h>
10 #include <exec/resident.h>
11 #include <utility/utility.h>
13 #include <clib/alib_protos.h>
14 #include <proto/ahi_sub.h>
15 #include <proto/exec.h>
16 #include <proto/intuition.h>
17 #include <proto/utility.h>
19 #define DRIVER_LIBSTUB
21 #include "gatestubs.h"
22 #include "library.h"
24 #include "DriverData.h"
25 #include "version.h"
27 #ifndef INTUITIONNAME
28 #define INTUITIONNAME "intuition.library"
29 #endif
31 #ifdef __amithlon__
32 # define RTF_NATIVE (1<<3)
33 # define FUNCARRAY_32BIT_NATIVE 0xfffefffe
34 #endif
36 #if !defined( __AROS__ ) && !defined( __amithlon__ )
37 extern void _etext;
38 #else
39 # define _etext RomTag+1 // Fake it
40 #endif
42 /******************************************************************************
43 ** Function prototypes ********************************************************
44 ******************************************************************************/
46 struct DriverBase*
47 _LibInit( struct DriverBase* AHIsubBase,
48 BPTR seglist,
49 struct ExecBase* sysbase );
51 BPTR
52 _LibExpunge( struct DriverBase* AHIsubBase );
54 struct DriverBase*
55 _LibOpen( ULONG version,
56 struct DriverBase* AHIsubBase );
58 BPTR
59 _LibClose( struct DriverBase* AHIsubBase );
61 ULONG
62 _LibNull( struct DriverBase* AHIsubBase );
65 /******************************************************************************
66 ** Driver entry ***************************************************************
67 ******************************************************************************/
69 #if defined( __amithlon__ )
70 __asm( "\n\
71 .text;\n\
72 .byte 0x4e, 0xfa, 0x00, 0x03\n\
73 jmp _start" );
74 #endif
76 int
77 _start( void )
79 return -1;
82 #if defined( __MORPHOS__ )
83 ULONG __abox__=1;
84 ULONG __amigappc__=1; // deprecated, used in MOS 0.4
85 #endif
87 /******************************************************************************
88 ** Driver resident structure **************************************************
89 ******************************************************************************/
91 #if defined( __AMIGAOS4__ )
92 static const struct TagItem InitTable[];
93 #else
94 static const APTR InitTable[4];
95 #endif
97 // This structure must reside in the text segment or the read-only
98 // data segment! "const" makes it happen.
99 const struct Resident RomTag __attribute__((used)) =
101 RTC_MATCHWORD,
102 (struct Resident *) &RomTag,
103 (struct Resident *) &_etext,
104 #if defined( __MORPHOS__ )
105 RTF_EXTENDED | RTF_PPC | RTF_AUTOINIT,
106 #elif defined( __AROS__ )
107 RTF_EXTENDED | RTF_AUTOINIT,
108 #elif defined( __amithlon__ )
109 RTF_NATIVE | RTF_AUTOINIT,
110 #elif defined( __AMIGAOS4__ )
111 RTF_NATIVE | RTF_AUTOINIT,
112 #else
113 RTF_AUTOINIT,
114 #endif
115 VERSION,
116 NT_LIBRARY,
117 0, /* priority */
118 (BYTE *) LibName,
119 (BYTE *) LibIDString,
120 (APTR) &InitTable
121 #if defined( __MORPHOS__ ) || defined( __AROS__ )
122 , REVISION, NULL
123 #endif
127 /******************************************************************************
128 ** Globals ********************************************************************
129 ******************************************************************************/
131 const char LibName[] = DRIVER;
132 const char LibIDString[] = DRIVER " " VERS "\r\n";
133 const UWORD LibVersion = VERSION;
134 const UWORD LibRevision = REVISION;
137 /******************************************************************************
138 ** Library stuff **************************************************************
139 ******************************************************************************/
141 #ifndef __AMIGAOS4__
143 static const APTR FuncTable[] =
145 #if defined( __MORPHOS__ ) || defined( __amithlon__ )
146 (APTR) FUNCARRAY_32BIT_NATIVE,
147 #endif
149 gwLibOpen,
150 gwLibClose,
151 gwLibExpunge,
152 gwLibNull,
154 gwAHIsub_AllocAudio,
155 gwAHIsub_FreeAudio,
156 gwAHIsub_Disable,
157 gwAHIsub_Enable,
158 gwAHIsub_Start,
159 gwAHIsub_Update,
160 gwAHIsub_Stop,
161 gwAHIsub_SetVol,
162 gwAHIsub_SetFreq,
163 gwAHIsub_SetSound,
164 gwAHIsub_SetEffect,
165 gwAHIsub_LoadSound,
166 gwAHIsub_UnloadSound,
167 gwAHIsub_GetAttr,
168 gwAHIsub_HardwareControl,
170 (APTR) -1
174 static const APTR InitTable[4] =
176 (APTR) DRIVERBASE_SIZEOF,
177 (APTR) &FuncTable,
178 NULL,
179 #if defined( __MORPHOS__ ) || defined( __amithlon__ )
180 (APTR) _LibInit
181 #else
182 (APTR) gwLibInit
183 #endif
186 #else // __AMIGAOS4__
188 static ULONG generic_Obtain(struct Interface *Self)
190 return Self->Data.RefCount++;
194 static ULONG generic_Release(struct Interface *Self)
196 return Self->Data.RefCount--;
200 static const CONST_APTR LibManagerVectors[] =
202 generic_Obtain,
203 generic_Release,
204 NULL,
205 NULL,
207 gwLibOpen,
208 gwLibClose,
209 gwLibExpunge,
210 NULL,
212 (CONST_APTR) -1,
215 static const struct TagItem LibManagerTags[] =
217 { MIT_Name, (ULONG) "__library" },
218 { MIT_VectorTable, (ULONG) LibManagerVectors },
219 { MIT_Version, 1 },
220 { TAG_DONE, 0 }
224 static const CONST_APTR MainVectors[] = {
225 generic_Obtain,
226 generic_Release,
227 NULL,
228 NULL,
230 gwAHIsub_AllocAudio,
231 gwAHIsub_FreeAudio,
232 gwAHIsub_Disable,
233 gwAHIsub_Enable,
234 gwAHIsub_Start,
235 gwAHIsub_Update,
236 gwAHIsub_Stop,
237 gwAHIsub_SetVol,
238 gwAHIsub_SetFreq,
239 gwAHIsub_SetSound,
240 gwAHIsub_SetEffect,
241 gwAHIsub_LoadSound,
242 gwAHIsub_UnloadSound,
243 gwAHIsub_GetAttr,
244 gwAHIsub_HardwareControl,
246 (CONST_APTR) -1
249 static const struct TagItem MainTags[] =
251 { MIT_Name, (ULONG) "main" },
252 { MIT_VectorTable, (ULONG) MainVectors },
253 { MIT_Version, 1 },
254 { TAG_DONE, 0 }
258 /* MLT_INTERFACES array */
259 static const CONST_APTR Interfaces[] =
261 LibManagerTags,
262 MainTags,
263 NULL
266 /* m68k library vectors */
267 static const CONST_APTR VecTable68K[] = {
268 (CONST_APTR) &m68kgwLibOpen,
269 (CONST_APTR) &m68kgwLibClose,
270 (CONST_APTR) &m68kgwLibExpunge,
271 (CONST_APTR) &m68kgwLibNull,
273 (CONST_APTR) &m68kgwAHIsub_AllocAudio,
274 (CONST_APTR) &m68kgwAHIsub_FreeAudio,
275 (CONST_APTR) &m68kgwAHIsub_Disable,
276 (CONST_APTR) &m68kgwAHIsub_Enable,
277 (CONST_APTR) &m68kgwAHIsub_Start,
278 (CONST_APTR) &m68kgwAHIsub_Update,
279 (CONST_APTR) &m68kgwAHIsub_Stop,
280 (CONST_APTR) &m68kgwAHIsub_SetVol,
281 (CONST_APTR) &m68kgwAHIsub_SetFreq,
282 (CONST_APTR) &m68kgwAHIsub_SetSound,
283 (CONST_APTR) &m68kgwAHIsub_SetEffect,
284 (CONST_APTR) &m68kgwAHIsub_LoadSound,
285 (CONST_APTR) &m68kgwAHIsub_UnloadSound,
286 (CONST_APTR) &m68kgwAHIsub_GetAttr,
287 (CONST_APTR) &m68kgwAHIsub_HardwareControl,
289 (CONST_APTR) -1
292 /* CreateLibrary() tag list */
293 static const struct TagItem InitTable[] =
295 { CLT_DataSize, DRIVERBASE_SIZEOF },
296 { CLT_Interfaces, (ULONG) Interfaces },
297 { CLT_Vector68K, (ULONG) VecTable68K },
298 { CLT_InitFunc, (ULONG) gwLibInit },
299 { TAG_DONE, 0 }
302 #endif // __AMIGAOS4__
305 /******************************************************************************
306 ** Message requester **********************************************************
307 ******************************************************************************/
309 void
310 ReqA( const char* text,
311 APTR args,
312 struct DriverBase* AHIsubBase )
314 struct IntuitionBase *IntuitionBase = AHIsubBase->intuitionbase;
315 struct EasyStruct es =
317 sizeof (struct EasyStruct),
319 (STRPTR) LibName,
320 (STRPTR) text,
321 "OK"
322 #ifdef __AMIGAOS4__
323 , NULL, NULL
324 #endif
327 EasyRequestArgs( NULL, &es, NULL, args );
330 /******************************************************************************
331 ** Serial port debugging ******************************************************
332 ******************************************************************************/
334 #ifndef __AMIGAOS4__
336 #if defined( __AROS__ ) && !defined( __mc68000__ )
338 #include <aros/asmcall.h>
340 AROS_UFH2( void,
341 rawputchar_m68k,
342 AROS_UFHA( UBYTE, c, D0 ),
343 AROS_UFHA( struct ExecBase*, sysbase, A3 ) )
345 AROS_USERFUNC_INIT
347 AROS_LC1NR(void, RawPutChar, AROS_LCA(UBYTE, c, D0), struct ExecBase *, sysbase, 86, Exec);
349 AROS_USERFUNC_EXIT
352 #else
354 static const UWORD rawputchar_m68k[] =
356 0x2C4B, // MOVEA.L A3,A6
357 0x4EAE, 0xFDFC, // JSR -$0204(A6)
358 0x4E75 // RTS
361 #endif
363 void
364 MyKPrintFArgs( UBYTE* fmt,
365 ULONG* args,
366 struct DriverBase* AHIsubBase )
368 RawDoFmt( fmt, args, (void(*)(void)) rawputchar_m68k, SysBase );
371 #endif
373 /******************************************************************************
374 ** HookEntry ******************************************************************
375 ******************************************************************************/
377 #if defined( __MORPHOS__ )
379 /* Should be in libamiga, but isn't? */
381 static ULONG
382 gw_HookEntry( void )
384 struct Hook* h = (struct Hook*) REG_A0;
385 void* o = (void*) REG_A2;
386 void* msg = (void*) REG_A1;
388 return ( ( (ULONG(*)(struct Hook*, void*, void*)) *h->h_SubEntry)( h, o, msg ) );
391 struct EmulLibEntry _HookEntry =
393 TRAP_LIB, 0, (void (*)(void)) &gw_HookEntry
396 __asm( ".globl HookEntry;HookEntry=_HookEntry" );
398 #endif
400 /******************************************************************************
401 ** Library init ***************************************************************
402 ******************************************************************************/
404 #ifdef __AROS__
405 #include <aros/symbolsets.h>
407 THIS_PROGRAM_HANDLES_SYMBOLSET(INIT)
408 THIS_PROGRAM_HANDLES_SYMBOLSET(EXIT)
409 DEFINESET(INIT)
410 DEFINESET(EXIT)
411 #endif
413 struct DriverBase*
414 _LibInit( struct DriverBase* AHIsubBase,
415 BPTR seglist,
416 struct ExecBase* sysbase )
418 SysBase = sysbase;
420 #ifdef __AROS__
421 if (!set_call_funcs(SETNAME(INIT), 1, 1))
422 return NULL;
423 #endif
425 #ifdef __AMIGAOS4__
426 IExec = (struct ExecIFace*) SysBase->MainInterface;
427 #endif
429 AHIsubBase->library.lib_Node.ln_Type = NT_LIBRARY;
430 AHIsubBase->library.lib_Node.ln_Name = (STRPTR) LibName;
431 AHIsubBase->library.lib_Flags = LIBF_SUMUSED | LIBF_CHANGED;
432 AHIsubBase->library.lib_Version = VERSION;
433 AHIsubBase->library.lib_Revision = REVISION;
434 AHIsubBase->library.lib_IdString = (STRPTR) LibIDString;
435 AHIsubBase->seglist = seglist;
437 AHIsubBase->intuitionbase = (struct IntuitionBase *)OpenLibrary( INTUITIONNAME, 37 );
438 AHIsubBase->utilitybase = (struct UtilityBase *)OpenLibrary( UTILITYNAME, 37 );
440 if( AHIsubBase->intuitionbase == NULL )
442 Alert( AN_Unknown|AG_OpenLib|AO_Intuition );
443 goto error;
446 #ifdef __AMIGAOS4__
447 if ((IIntuition = (struct IntuitionIFace *) GetInterface((struct Library*) AHIsubBase->intuitionbase, "main", 1, NULL)) == NULL)
449 Alert( AN_Unknown|AG_OpenLib|AO_Intuition );
450 goto error;
452 #endif
454 if( AHIsubBase->utilitybase == NULL )
456 Req( "Unable to open 'utility.library' version 37.\n" );
457 goto error;
460 #ifdef __AMIGAOS4__
461 if ((IUtility = (struct UtilityIFace *) GetInterface((struct Library*) AHIsubBase->utilitybase, "main", 1, NULL)) == NULL)
463 Req("Couldn't open IUtility interface!\n");
464 goto error;
467 if ((IAHIsub = (struct AHIsubIFace *) GetInterface((struct Library *) AHIsubBase, "main", 1, NULL)) == NULL)
469 Req("Couldn't open IAHIsub interface!\n");
470 return FALSE;
472 #endif
474 if( ! DriverInit( AHIsubBase ) )
476 goto error;
479 return AHIsubBase;
481 error:
482 _LibExpunge( AHIsubBase );
483 return NULL;
487 /******************************************************************************
488 ** Library clean-up ***********************************************************
489 ******************************************************************************/
491 #ifndef __AMIGAOS4__
492 # define DeleteLibrary(base) FreeMem((APTR)(((char*) base) - \
493 ((struct Library*) base)->lib_NegSize), \
494 ((struct Library*) base)->lib_NegSize + \
495 ((struct Library*) base)->lib_PosSize)
496 #endif
498 BPTR
499 _LibExpunge( struct DriverBase* AHIsubBase )
501 BPTR seglist = 0;
503 if( AHIsubBase->library.lib_OpenCnt == 0 )
505 seglist = AHIsubBase->seglist;
507 /* Since LibInit() calls us on failure, we have to check if we're
508 really added to the library list before removing us. */
510 if( AHIsubBase->library.lib_Node.ln_Succ != NULL )
512 Remove( (struct Node *) AHIsubBase );
515 DriverCleanup( AHIsubBase );
517 #ifdef __AMIGAOS4__
518 DropInterface((struct Interface *) IAHIsub);
519 DropInterface((struct Interface *) IIntuition);
520 DropInterface((struct Interface *) IUtility);
521 #endif
523 /* Close libraries */
524 CloseLibrary(&AHIsubBase->intuitionbase->LibNode );
525 CloseLibrary(&AHIsubBase->utilitybase->ub_LibNode );
527 #ifdef __AROS__
528 set_call_funcs(SETNAME(EXIT), -1, 0);
529 #endif
531 DeleteLibrary(&AHIsubBase->library);
533 else
535 AHIsubBase->library.lib_Flags |= LIBF_DELEXP;
538 return seglist;
542 /******************************************************************************
543 ** Library opening ************************************************************
544 ******************************************************************************/
546 struct DriverBase*
547 _LibOpen( ULONG version,
548 struct DriverBase* AHIsubBase )
550 AHIsubBase->library.lib_Flags &= ~LIBF_DELEXP;
551 AHIsubBase->library.lib_OpenCnt++;
553 return AHIsubBase;
557 /******************************************************************************
558 ** Library closing ************************************************************
559 ******************************************************************************/
561 BPTR
562 _LibClose( struct DriverBase* AHIsubBase )
564 BPTR seglist = 0;
566 AHIsubBase->library.lib_OpenCnt--;
568 if( AHIsubBase->library.lib_OpenCnt == 0 )
570 if( AHIsubBase->library.lib_Flags & LIBF_DELEXP )
572 seglist = _LibExpunge( AHIsubBase );
576 return seglist;
580 /******************************************************************************
581 ** Unused function ************************************************************
582 ******************************************************************************/
584 ULONG
585 _LibNull( struct DriverBase* AHIsubBase )
587 return 0;