-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.c
executable file
·364 lines (314 loc) · 11.5 KB
/
init.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
** X1kTemp.docky
** (c) 2013-2015 Alexandre Balaban <alexandre -(@)- balaban -(.)- fr>
**
** Based upon Datetime.docky (c) by Fredrik Wikstrom
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
**
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
** POSSIBILITY OF SUCH DAMAGE.
**
*/
#include "dockybase.h"
#include <exec/exec.h>
#include <proto/exec.h>
#include <dos/dos.h>
#include <exec/types.h>
#include <stdarg.h>
#define LIBNAME "X1kTemp.docky"
/* Version Tag */
#include "X1kTemp.docky_rev.h"
#include "readtemp.h"
CONST UBYTE USED verstag[] = VERSTAG;
struct Library *DOSLib;
struct Library *UtilityLib;
struct Library *IconLib;
struct DOSIFace *IDOS;
struct UtilityIFace *IUtility;
struct IconIFace *IIcon;
/*
* The system (and compiler) rely on a symbol named _start which marks
* the beginning of execution of an ELF file. To prevent others from
* executing this library, and to keep the compiler/linker happy, we
* define an empty _start symbol here.
*
* On the classic system (pre-AmigaOS4) this was usually done by
* moveq #0,d0
* rts
*
*/
int32 _start(void);
int32 _start(void)
{
/* If you feel like it, open DOS and print something to the user */
return 100;
}
/* Open the library */
STATIC struct Library *libOpen(struct LibraryManagerInterface *Self, ULONG version)
{
struct DockyBase *db = (struct DockyBase *)Self->Data.LibBase;
if (version > VERSION)
{
return NULL;
}
/* Add any specific open code here
Return 0 before incrementing OpenCnt to fail opening */
/* Add up the open count */
db->libNode.lib_OpenCnt++;
return &db->libNode;
}
/* Close the library */
STATIC APTR libClose(struct LibraryManagerInterface *Self)
{
struct DockyBase *db = (struct DockyBase *)Self->Data.LibBase;
/* Make sure to undo what open did */
/* Make the close count */
db->libNode.lib_OpenCnt--;
return 0;
}
int openLibs (struct DockyBase *db);
void closeLibs (struct DockyBase *db);
/* Expunge the library */
STATIC BPTR libExpunge(struct LibraryManagerInterface *Self)
{
/* If your library cannot be expunged, return 0 */
BPTR result = 0;
struct DockyBase *db = (struct DockyBase *)Self->Data.LibBase;
if (db->libNode.lib_OpenCnt == 0)
{
if(db->pMutex)
{
d(bug("Mutex obtaining...\n"));
IExec->MutexObtain(db->pMutex);
d(bug("Mutex obtaineded\n"));
}
result = db->segList;
/* Undo what the init code did */
smbus_shutdown(db);
closeLibs(db);
IExec->Remove((struct Node *)db);
if(db->pMutex)
{
d(bug("Mutex freeing...\n"));
IExec->FreeSysObject(ASOT_MUTEX, db->pMutex);
db->pMutex = NULL;
}
IExec->DeleteLibrary((struct Library *)db);
}
else
{
result = 0;
db->libNode.lib_Flags |= LIBF_DELEXP;
}
return result;
}
/* The ROMTAG Init Function */
STATIC struct Library *libInit(struct DockyBase *db, BPTR seglist, struct Interface *exec)
{
db->libNode.lib_Node.ln_Type = NT_LIBRARY;
db->libNode.lib_Node.ln_Pri = 0;
db->libNode.lib_Node.ln_Name = LIBNAME;
db->libNode.lib_Flags = LIBF_SUMUSED|LIBF_CHANGED;
db->libNode.lib_Version = VERSION;
db->libNode.lib_Revision = REVISION;
db->libNode.lib_IdString = VSTRING;
db->segList = seglist;
IExec = (struct ExecIFace *)exec;
d(bug("Lib Init with DockyBase %08x\n", db));
db->pMutex = IExec->AllocSysObjectTags(ASOT_MUTEX, ASOMUTEX_Recursive, FALSE, TAG_END);
if (db->pMutex)
{
d(bug("Lib Init mutex allocated, trying to obtain...\n"));
IExec->MutexObtain(db->pMutex);
d(bug("Mutex obtained\n"));
if (openLibs(db)) {
ULONG lMachine = MACHINETYPE_UNKNOWN;
IExpansion->GetMachineInfoTags(GMIT_Machine,&lMachine,TAG_END);
if (MACHINETYPE_X1000 == lMachine)
{
if (smbus_startup(db))
{
d(bug("Mutex releasing...\n"));
IExec->MutexRelease(db->pMutex);
d(bug("Mutex released\n"));
return &db->libNode;
}
}
else
{
IDOS->TimedDosRequesterTags(TDR_TitleString, "X1KTemp - Error",
TDR_FormatString, "X1KTemp can only operate on AmigaOne X1000.",
TDR_GadgetString, "Exit",
TDR_ImageType, TDRIMAGE_ERROR,
TAG_DONE);
}
closeLibs(db);
}
d(bug("Mutex releasing...\n"));
IExec->MutexRelease(db->pMutex);
d(bug("Mutex released\n"));
IExec->FreeSysObject(ASOT_MUTEX, db->pMutex);
db->pMutex = NULL;
}
return NULL;
}
int openLibs (struct DockyBase *db) {
d(bug("Opening libs from DockyBase %08x\n", db));
DOSLib = IExec->OpenLibrary("dos.library", 52);
if (!DOSLib) return FALSE;
IDOS = (struct DOSIFace *)IExec->GetInterface(DOSLib, "main", 1, NULL);
if (!IDOS) return FALSE;
UtilityLib = IExec->OpenLibrary("utility.library", 52);
if (!UtilityLib) return FALSE;
IUtility = (struct UtilityIFace *)IExec->GetInterface(UtilityLib, "main", 1, NULL);
if (!IUtility) return FALSE;
IconLib = IExec->OpenLibrary("icon.library", 52);
if (!IconLib) return FALSE;
IIcon = (struct IconIFace *)IExec->GetInterface(IconLib, "main", 1, NULL);
if (!IIcon) return FALSE;
IntuitionLib = IExec->OpenLibrary("intuition.library", 52);
if (!IntuitionLib) return FALSE;
IIntuition = (struct IntuitionIFace *)IExec->GetInterface(IntuitionLib, "main", 1, NULL);
if (!IIntuition) return FALSE;
GfxLib = (struct GfxBase *)IExec->OpenLibrary("graphics.library", 52);
if (!GfxLib) return FALSE;
IGraphics = (struct GraphicsIFace *)IExec->GetInterface((struct Library *)GfxLib, "main",
1, NULL);
if (!IGraphics) return FALSE;
DiskfontLib = IExec->OpenLibrary("diskfont.library", 52);
if (!DiskfontLib) return FALSE;
IDiskfont = (struct DiskfontIFace *)IExec->GetInterface(DiskfontLib, "main", 1, NULL);
if (!IDiskfont) return FALSE;
ApplicationLib = IExec->OpenLibrary("application.library", 53);
if (!ApplicationLib) return FALSE;
IApplication = (struct ApplicationIFace *)IExec->GetInterface(ApplicationLib, "application", 2, NULL);
if (!IApplication) return FALSE;
ExpansionLib = IExec->OpenLibrary("expansion.library", 53);
if (!ExpansionLib) return FALSE;
IExpansion = (struct ExpansionIFace*)IExec->GetInterface(ExpansionLib, "main", 1, NULL);
if (!IExpansion) return FALSE;
d(bug("Libs opened from DockyBase %08x\n", db));
return TRUE;
}
void closeLibs (struct DockyBase *db) {
d(bug("Closing libs from DockyBase %08x\n", db));
if(IDiskfont) IExec->DropInterface((struct Interface *)IDiskfont);
if(DiskfontLib) IExec->CloseLibrary(DiskfontLib);
if(IGraphics) IExec->DropInterface((struct Interface *)IGraphics);
if(GfxLib) IExec->CloseLibrary((struct Library *)GfxLib);
if(IIntuition) IExec->DropInterface((struct Interface *)IIntuition);
if(IntuitionLib) IExec->CloseLibrary(IntuitionLib);
if(IIcon) IExec->DropInterface((struct Interface *)IIcon);
if(IconLib) IExec->CloseLibrary(IconLib);
if(IUtility) IExec->DropInterface((struct Interface *)IUtility);
if(UtilityLib) IExec->CloseLibrary(UtilityLib);
if(IDOS) IExec->DropInterface((struct Interface *)IDOS);
if(DOSLib) IExec->CloseLibrary(DOSLib);
if(IApplication) IExec->DropInterface((struct Interface *)IApplication);
if(ApplicationLib) IExec->CloseLibrary(ApplicationLib);
if(IExpansion) IExec->DropInterface((struct Interface *)IExpansion);
if(ExpansionLib) IExec->CloseLibrary(ExpansionLib);
d(bug("Libs closed from DockyBase %08x\n", db));
}
/* ------------------- Manager Interface ------------------------ */
/* These are generic. Replace if you need more fancy stuff */
STATIC LONG _manager_Obtain(struct LibraryManagerInterface *Self)
{
return Self->Data.RefCount++;
}
STATIC ULONG _manager_Release(struct LibraryManagerInterface *Self)
{
return Self->Data.RefCount--;
}
/* Manager interface vectors */
STATIC CONST APTR lib_manager_vectors[] =
{
(APTR)_manager_Obtain,
(APTR)_manager_Release,
NULL,
NULL,
(APTR)libOpen,
(APTR)libClose,
(APTR)libExpunge,
NULL,
(APTR)-1
};
/* "__library" interface tag list */
STATIC CONST struct TagItem lib_managerTags[] =
{
{ MIT_Name, (Tag)"__library" },
{ MIT_VectorTable, (Tag)lib_manager_vectors },
{ MIT_Version, 1 },
{ TAG_END, 0 }
};
/* ------------------- Library Interface(s) ------------------------ */
STATIC CONST APTR docky_vectors[] =
{
(APTR)DockyObtain,
(APTR)DockyRelease,
(APTR)DockyExpunge,
(APTR)DockyClone,
(APTR)DockyGet,
(APTR)DockySet,
(APTR)DockyProcess,
(APTR)-1
};
/* Uncomment this line (and see below) if your library has a 68k jump table */
//extern APTR VecTable68K[];
CONST struct TagItem dockyTags[] =
{
{ MIT_Name, (Tag)"docky" },
{ MIT_VectorTable, (Tag)docky_vectors },
{ MIT_DataSize, sizeof(struct DockyData) },
{ MIT_Flags, IFLF_PRIVATE },
{ MIT_Version, 1 },
{ TAG_END, 0 }
};
STATIC CONST CONST_APTR libInterfaces[] =
{
lib_managerTags,
dockyTags,
NULL
};
STATIC CONST struct TagItem libCreateTags[] =
{
{ CLT_DataSize, sizeof(struct DockyBase) },
{ CLT_InitFunc, (Tag)libInit },
{ CLT_Interfaces, (Tag)libInterfaces },
/* Uncomment the following line if you have a 68k jump table */
//{ CLT_Vector68K, (Tag)VecTable68K },
{ TAG_END, 0 }
};
/* ------------------- ROM Tag ------------------------ */
CONST struct Resident USED lib_res =
{
RTC_MATCHWORD,
(struct Resident *)&lib_res,
(APTR)(&lib_res + 1),
RTF_NATIVE|RTF_AUTOINIT, /* Add RTF_COLDSTART if you want to be resident */
VERSION,
NT_LIBRARY, /* Make this NT_DEVICE if needed */
0, /* PRI, usually not needed unless you're resident */
LIBNAME,
VSTRING,
(APTR)libCreateTags
};