Import of "OpenUrl"
[cake.git] / workbench / libs / openurl / library / libinit.c
blobc0160a1eb693bf6ec3cd62562ad04e3b62b89079
1 /*
2 ** openurl.library - universal URL display and browser
3 ** launcher library
4 **
5 ** Written by Troels Walsted Hansen <troels@thule.no>
6 ** Placed in the public domain.
7 **
8 ** Developed by:
9 ** - Alfonso Ranieri <alforan@tin.it>
10 ** - Stefan Kost <ensonic@sonicpulse.de>
12 ** Ported to OS4 by Alexandre Balaban <alexandre@balaban.name>
16 #include "lib.h"
17 #include "openurl.library_rev.h"
19 /****************************************************************************/
21 UBYTE lib_name[] = PRG;
22 UBYTE lib_ver[] = VSTRING;
23 UBYTE lib_fullName[] = PRGNAME;
24 ULONG lib_version = VERSION;
25 ULONG lib_revision = REVISION;
27 struct ExecBase *SysBase = NULL;
28 struct DosLibrary *DOSBase = NULL;
29 struct Library *UtilityBase = NULL;
30 struct Library *IFFParseBase = NULL;
31 struct RxsLib *RexxSysBase = NULL;
33 struct SignalSemaphore lib_sem = {0};
34 struct SignalSemaphore lib_prefsSem = {0};
35 struct SignalSemaphore lib_memSem = {0};
37 APTR lib_pool = NULL;
38 struct URL_Prefs *lib_prefs = NULL;
40 struct Library *lib_base = NULL;
41 ULONG lib_segList = (ULONG)NULL;
42 ULONG lib_use = 0;
43 ULONG lib_flags = 0;
45 /****************************************************************************/
47 #ifdef __MORPHOS__
48 #include <exec/resident.h>
50 static struct Library *initLib ( struct Library *base , BPTR segList , struct ExecBase *sys );
51 static struct Library *openLib ( void );
52 static ULONG expungeLib ( void );
53 static ULONG closeLib ( void );
54 static ULONG nil (void );
55 static ULONG first(void) __attribute((unused));
57 /****************************************************************************/
59 static ULONG
60 first(void)
62 return -1;
65 /****************************************************************************/
67 static const APTR funcTable[] =
69 (APTR)FUNCARRAY_BEGIN,
70 (APTR)FUNCARRAY_32BIT_NATIVE,
71 (APTR)openLib,
72 (APTR)closeLib,
73 (APTR)expungeLib,
74 (APTR)nil,
76 (APTR)LIB_URL_OpenA,
77 (APTR)LIB_URL_OldGetPrefs,
78 (APTR)LIB_URL_OldFreePrefs,
79 (APTR)LIB_URL_OldSetPrefs,
80 (APTR)LIB_URL_OldGetDefaultPrefs,
81 (APTR)LIB_URL_OldLaunchPrefsApp,
82 (APTR)0xffffffff,
84 (APTR)FUNCARRAY_32BIT_D0D1A0A1SR_NATIVE,
85 (APTR)dispatch,
86 (APTR)0xffffffff,
88 (APTR)FUNCARRAY_32BIT_NATIVE,
89 (APTR)LIB_URL_GetPrefsA,
90 (APTR)LIB_URL_FreePrefsA,
91 (APTR)LIB_URL_SetPrefsA,
92 (APTR)LIB_URL_LaunchPrefsAppA,
93 (APTR)LIB_URL_GetAttr,
94 (APTR)0xffffffff,
96 (APTR)FUNCARRAY_END
99 static const ULONG initTable[] =
101 sizeof(struct Library),
102 (ULONG)funcTable,
103 NULL,
104 (ULONG)initLib
107 const struct Resident romTag =
109 RTC_MATCHWORD,
110 (struct Resident *)&romTag,
111 (struct Resident *)&romTag+1,
112 RTF_AUTOINIT|RTF_PPC|RTF_EXTENDED,
113 VERSION,
114 NT_LIBRARY,
116 (UBYTE *)lib_name,
117 (UBYTE *)lib_ver,
118 (APTR)initTable,
119 REVISION,
120 NULL
123 const ULONG __abox__ = 1;
125 #elif defined(__amigaos4__)
126 #include <proto/exec.h>
127 /* amigaos4 *****************************************************************/
129 struct ExecIFace *IExec = NULL;
130 struct DOSIFace *IDOS = NULL;
131 struct UtilityIFace *IUtility = NULL;
132 struct IFFParseIFace *IIFFParse = NULL;
133 struct RexxSysIFace *IRexxSys = NULL;
135 /* amigaos4 *****************************************************************/
137 struct Library * SAVEDS ASM initLib (REG(a0,ULONG segList), REG(a6,struct ExecBase *sys), REG(d0, struct Library *base));
138 //uint32 libObtain (struct LibraryManagerInterface *Self);
139 //uint32 libRelease (struct LibraryManagerInterface *Self);
140 struct Library * SAVEDS ASM openLib (REG(a6,struct Library *base));
141 ULONG SAVEDS ASM closeLib(REG(a6,struct Library *base));
142 ULONG SAVEDS ASM expungeLib (REG(a6,struct Library *base));
144 struct Library * mgr_Init (struct Library *base, BPTR segList, struct ExecIFace *ISys);
145 uint32 mgr_Obtain (struct LibraryManagerInterface *Self);
146 uint32 mgr_Release (struct LibraryManagerInterface *Self);
147 struct Library * mgr_Open (struct LibraryManagerInterface *Self, uint32 version);
148 APTR mgr_Close (struct LibraryManagerInterface *Self);
149 APTR mgr_Expunge (struct LibraryManagerInterface *Self);
151 uint32 VARARGS68K OS4_URL_Obtain( struct OpenURLIFace *Self );
152 uint32 VARARGS68K OS4_URL_Release( struct OpenURLIFace *Self );
154 /* amigaos4 *****************************************************************/
156 static BOOL bAlreadyHasSemaphore = FALSE;
158 /* amigaos4 *****************************************************************/
160 static APTR lib_manager_vectors[] = {
161 mgr_Obtain,
162 mgr_Release,
163 NULL,
164 NULL,
165 mgr_Open,
166 mgr_Close,
167 mgr_Expunge,
168 NULL,
169 (APTR)-1,
172 static struct TagItem lib_managerTags[] = {
173 { MIT_Name, (uint32)"__library" },
174 { MIT_VectorTable, (uint32)lib_manager_vectors },
175 { MIT_Version, 1 },
176 { TAG_END, 0 }
179 void *main_vectors[] = {
180 (void *) OS4_URL_Obtain,
181 (void *) OS4_URL_Release,
182 (void *) NULL,
183 (void *) NULL,
184 (void *) OS4_URL_OpenA,
185 (void *) OS4_URL_Open,
186 (void *) OS4_URL_OldGetPrefs,
187 (void *) OS4_URL_OldFreePrefs,
188 (void *) OS4_URL_OldSetPrefs,
189 (void *) OS4_URL_OldGetDefaultPrefs,
190 (void *) OS4_URL_OldLaunchPrefsApp,
191 (void *) OS4_dispatch,
192 (void *) OS4_URL_GetPrefsA,
193 (void *) OS4_URL_GetPrefs,
194 (void *) OS4_URL_FreePrefsA,
195 (void *) OS4_URL_FreePrefs,
196 (void *) OS4_URL_SetPrefsA,
197 (void *) OS4_URL_SetPrefs,
198 (void *) OS4_URL_LaunchPrefsAppA,
199 (void *) OS4_URL_LaunchPrefsApp,
200 (void *) OS4_URL_GetAttr,
201 (void *) -1
204 static struct TagItem lib_mainTags[] = {
205 { MIT_Name, (uint32)"main" },
206 { MIT_VectorTable, (uint32)main_vectors },
207 { MIT_Version, 1 },
208 { TAG_END, 0 }
211 static APTR libInterfaces[] = {
212 lib_managerTags,
213 lib_mainTags,
214 NULL
217 extern uint32 VecTable68K[];
219 static struct TagItem libCreateTags[] = {
220 { CLT_DataSize, (uint32)sizeof(struct Library) },
221 { CLT_InitFunc, (uint32)mgr_Init },
222 { CLT_Interfaces, (uint32)libInterfaces },
223 { CLT_Vector68K, (uint32)VecTable68K },
224 { TAG_END, 0 }
227 #ifdef __GNUC__
228 static struct Resident __attribute__((used)) romTag = {
229 #else
230 static struct Resident romTag = {
231 #endif
232 RTC_MATCHWORD, // rt_MatchWord
233 &romTag, // rt_MatchTag
234 &romTag+1, // rt_EndSkip
235 RTF_NATIVE | RTF_AUTOINIT, // rt_Flags
236 VERSION, // rt_Version
237 NT_LIBRARY, // rt_Type
238 0, // rt_Pri
239 lib_name, // rt_Name
240 lib_ver, // rt_IdString
241 libCreateTags // rt_Init
244 #endif
246 /****************************************************************************/
248 #ifdef __MORPHOS__
249 static struct Library *initLib(struct Library *base,BPTR segList,struct ExecBase *sys)
250 #elif defined(__amigaos4__)
251 struct Library * initLib(struct Library *base, BPTR segList, struct ExecIFace *ISys)
252 #else
253 struct Library *SAVEDS ASM initLib(REG(a0,ULONG segList),REG(a6,struct ExecBase *sys),REG(d0, struct Library *base))
254 #endif
256 #if defined(__amigaos4__)
257 base->lib_Node.ln_Type = NT_LIBRARY;
258 base->lib_Node.ln_Pri = 0;
259 base->lib_Node.ln_Name = lib_name;
260 base->lib_Flags = LIBF_SUMUSED|LIBF_CHANGED;
261 base->lib_Version = lib_version;
262 base->lib_Revision = lib_revision;
263 base->lib_IdString = lib_ver;
265 IExec = ISys;
266 //IExec->Obtain();
267 SysBase = (struct ExecBase*)ISys->Data.LibBase;
268 #else
269 SysBase = sys;
270 #endif
272 InitSemaphore(&lib_sem);
273 InitSemaphore(&lib_prefsSem);
274 InitSemaphore(&lib_memSem);
276 lib_segList = segList;
278 return lib_base = base;
281 #ifdef __MORPHOS__
282 static struct Library *initLib(struct Library *base,BPTR segList,struct ExecBase *sys)
283 #else
284 struct Library * SAVEDS ASM initLib (REG(a0,ULONG segList), REG(a6,struct ExecBase *sys), REG(d0, struct Library *base))
285 #endif
287 SysBase = sys;
289 InitSemaphore(&lib_sem);
290 InitSemaphore(&lib_prefsSem);
291 InitSemaphore(&lib_memSem);
293 lib_segList = segList;
295 return lib_base = base;
298 /****************************************************************************/
300 #ifdef __MORPHOS__
301 static struct Library *openLib(void)
303 struct Library *base = (struct Library *)REG_A6;
304 #else
305 struct Library * SAVEDS ASM openLib(REG(a6,struct Library *base))
307 #endif
309 struct Library *res;
311 ObtainSemaphore(&lib_sem);
313 base->lib_OpenCnt++;
314 base->lib_Flags &= ~LIBF_DELEXP;
316 if (!(lib_flags & BASEFLG_Init) && !initBase())
318 base->lib_OpenCnt--;
319 res = NULL;
321 else res = base;
323 ReleaseSemaphore(&lib_sem);
325 return res;
328 /****************************************************************************/
330 #ifdef __MORPHOS__
331 static ULONG expungeLib(void)
333 struct Library *base = (struct Library *)REG_A6;
334 #else
335 ULONG SAVEDS ASM expungeLib(REG(a6,struct Library *base))
337 #endif
339 ULONG res;
341 #if defined(__amigaos4__)
342 // prevents a deadlock if called from closeLib
343 if( !bAlreadyHasSemaphore )
344 #endif
345 ObtainSemaphore(&lib_sem);
347 if (!base->lib_OpenCnt && !lib_use)
349 Remove((struct Node *)base);
351 #if defined(__amigaos4__)
352 DeleteLibrary(base);
353 //IExec->Release();
354 #else
355 FreeMem((UBYTE *)base-base->lib_NegSize,base->lib_NegSize+base->lib_PosSize);
356 #endif
358 res = lib_segList;
360 else
362 base->lib_Flags |= LIBF_DELEXP;
363 res = (ULONG)NULL;
366 #if defined(__amigaos4__)
367 // prevents a deadlock if called from closeLib
368 if( !bAlreadyHasSemaphore )
369 #endif
370 ReleaseSemaphore(&lib_sem);
372 return res;
375 /****************************************************************************/
377 #ifdef __MORPHOS__
378 static ULONG closeLib(void)
380 struct Library *base = (struct Library *)REG_A6;
381 #else
382 ULONG SAVEDS ASM closeLib(REG(a6,struct Library *base))
384 #endif
386 ULONG res = (ULONG)NULL;
388 ObtainSemaphore(&lib_sem);
390 base->lib_OpenCnt--;
392 if (!base->lib_OpenCnt && !lib_use)
394 freeBase();
396 if (base->lib_Flags & LIBF_DELEXP)
398 #if defined(__amigaos4__)
399 bAlreadyHasSemaphore = TRUE;
400 res = (ULONG)expungeLib( base );
401 bAlreadyHasSemaphore = FALSE;
402 #else
403 Remove((struct Node *)base);
404 FreeMem((UBYTE *)base-base->lib_NegSize,base->lib_NegSize+base->lib_PosSize);
406 res = lib_segList;
407 #endif
411 ReleaseSemaphore(&lib_sem);
413 return res;
416 /****************************************************************************/
418 #ifdef __MORPHOS__
419 static ULONG nil(void)
421 return 0;
423 #endif
425 /****************************************************************************/