Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / devs / diskimage / device / main_vectors.c
blobac832c9089b1e890daa300da5cb22701dd3bc2c5
1 /* Copyright 2007-2012 Fredrik Wikstrom. All rights reserved.
2 **
3 ** Redistribution and use in source and binary forms, with or without
4 ** modification, are permitted provided that the following conditions
5 ** are met:
6 **
7 ** 1. Redistributions of source code must retain the above copyright
8 ** notice, this list of conditions and the following disclaimer.
9 **
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
14 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 ** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 ** POSSIBILITY OF SUCH DAMAGE.
27 #include "diskimage_device.h"
29 static struct DiskImageUnit *FindUnit (struct DiskImageBase *libBase, ULONG unit_num) {
30 struct DiskImageUnit *unit;
31 unit = (struct DiskImageUnit *)libBase->Units->lh_Head;
32 while (unit->Node.ln_Succ) {
33 if (unit->UnitNum == unit_num) {
34 return unit;
36 unit = (struct DiskImageUnit *)unit->Node.ln_Succ;
38 return NULL;
41 static LONG Internal_UnitControl (ULONG unit_num, struct TagItem *tags, struct DiskImageBase *libBase) {
42 struct Library *SysBase = libBase->SysBase;
43 struct DiskImageUnit *unit;
44 struct DiskImageMsg *msg;
45 struct MsgPort *replyport, *port;
47 ObtainSemaphoreShared(libBase->UnitSemaphore);
48 unit = FindUnit(libBase, unit_num);
49 if (!unit) {
50 ReleaseSemaphore(libBase->UnitSemaphore);
51 return ERROR_BAD_NUMBER;
54 ObtainSemaphore(unit->MsgSemaphore);
55 replyport = unit->ReplyPort;
56 port = unit->MsgPort;
57 msg = unit->DiskImageMsg;
59 replyport->mp_SigTask = FindTask(NULL);
60 replyport->mp_Flags = PA_SIGNAL;
61 msg->dim_Command = DICMD_TAGLIST;
62 msg->dim_Tags = tags;
64 PutMsg(port, &msg->dim_Msg);
65 WaitPort(replyport);
66 GetMsg(replyport);
68 replyport->mp_Flags = PA_IGNORE;
69 replyport->mp_SigTask = NULL;
71 ReleaseSemaphore(unit->MsgSemaphore);
72 ReleaseSemaphore(libBase->UnitSemaphore);
74 return NO_ERROR;
77 #ifdef __AROS__
78 AROS_LH2(LONG, MountImage,
79 AROS_LHA(ULONG, unit_num, D0),
80 AROS_LHA(CONST_STRPTR, filename, A0),
81 struct DiskImageBase *, libBase, 7, DiskImage
84 AROS_LIBFUNC_INIT
85 #else
86 LONG MountImage (REG(d0, ULONG unit_num), REG(a0, CONST_STRPTR filename),
87 REG(a6, struct DiskImageBase *libBase))
89 #endif
90 LONG error = NO_ERROR;
91 LONG error2;
92 struct TagItem tags[4];
93 tags[0].ti_Tag = DITAG_Error;
94 tags[0].ti_Data = (IPTR)&error;
95 tags[1].ti_Tag = DITAG_CurrentDir;
96 tags[1].ti_Data = (IPTR)GetCurrentDir();
97 tags[2].ti_Tag = DITAG_Filename;
98 tags[2].ti_Data = (IPTR)filename;
99 tags[3].ti_Tag = TAG_END;
100 error2 = Internal_UnitControl(unit_num, tags, libBase);
101 return error ? error : error2;
102 #ifdef __AROS__
103 AROS_LIBFUNC_EXIT
104 #endif
107 #ifdef __AROS__
108 AROS_LH3(LONG, UnitInfo,
109 AROS_LHA(ULONG, unit_num, D0),
110 AROS_LHA(STRPTR *, filename, A0),
111 AROS_LHA(BOOL *, writeprotect, A1),
112 struct DiskImageBase *, libBase, 8, DiskImage
115 AROS_LIBFUNC_INIT
116 #else
117 LONG UnitInfo (REG(d0, ULONG unit_num), REG(a0, STRPTR *filename), REG(a1, BOOL *writeprotect),
118 REG(a6, struct DiskImageBase *libBase))
120 #endif
121 LONG error = NO_ERROR;
122 LONG error2;
123 struct TagItem tags[4];
124 tags[0].ti_Tag = DITAG_Error;
125 tags[0].ti_Data = (IPTR)&error;
126 tags[1].ti_Tag = DITAG_GetImageName;
127 tags[1].ti_Data = (IPTR)filename;
128 tags[2].ti_Tag = DITAG_GetWriteProtect;
129 tags[2].ti_Data = (IPTR)writeprotect;
130 tags[3].ti_Tag = TAG_END;
131 error2 = Internal_UnitControl(unit_num, tags, libBase);
132 return error ? error : error2;
133 #ifdef __AROS__
134 AROS_LIBFUNC_EXIT
135 #endif
138 #ifdef __AROS__
139 AROS_LH2(LONG, WriteProtect,
140 AROS_LHA(ULONG, unit_num, D0),
141 AROS_LHA(BOOL, writeprotect, D1),
142 struct DiskImageBase *, libBase, 9, DiskImage
145 AROS_LIBFUNC_INIT
146 #else
147 LONG WriteProtect (REG(d0, ULONG unit_num), REG(d1, BOOL writeprotect),
148 REG(a6, struct DiskImageBase *libBase))
150 #endif
151 LONG error = NO_ERROR;
152 LONG error2;
153 struct TagItem tags[3];
154 tags[0].ti_Tag = DITAG_Error;
155 tags[0].ti_Data = (IPTR)&error;
156 tags[1].ti_Tag = DITAG_WriteProtect;
157 tags[1].ti_Data = writeprotect;
158 tags[2].ti_Tag = TAG_END;
159 error2 = Internal_UnitControl(unit_num, tags, libBase);
160 return error ? error : error2;
161 #ifdef __AROS__
162 AROS_LIBFUNC_EXIT
163 #endif
166 #ifdef __AROS__
167 AROS_LH2(LONG, UnitControlA,
168 AROS_LHA(ULONG, unit_num, D0),
169 AROS_LHA(struct TagItem *, tags, A0),
170 struct DiskImageBase *, libBase, 10, DiskImage
173 AROS_LIBFUNC_INIT
174 #else
175 LONG UnitControlA (REG(d0, ULONG unit_num), REG(a0, struct TagItem *tags),
176 REG(a6, struct DiskImageBase *libBase))
178 #endif
179 return Internal_UnitControl(unit_num, tags, libBase);
180 #ifdef __AROS__
181 AROS_LIBFUNC_EXIT
182 #endif
185 static const struct TagItem unmount_tags[] = {
186 { DITAG_Filename, (IPTR)NULL },
187 { TAG_END }
190 #ifdef __AROS__
191 AROS_LH0(LONG, ReloadPlugins,
192 struct DiskImageBase *, libBase, 11, DiskImage
195 AROS_LIBFUNC_INIT
196 #else
197 LONG ReloadPlugins (REG(a6, struct DiskImageBase *libBase)) {
198 #endif
199 struct Library *SysBase = libBase->SysBase;
200 struct Library *UtilityBase = libBase->UtilityBase;
201 struct MsgPort *replyport, *port;
202 struct DiskImageMsg *msg;
203 struct DiskImageUnit *unit;
204 struct Hook *hook;
206 ObtainSemaphore(libBase->UnitSemaphore);
208 /* Unmount all units */
209 unit = (struct DiskImageUnit *)libBase->Units->lh_Head;
210 while (unit->Node.ln_Succ) {
211 ObtainSemaphore(unit->MsgSemaphore);
213 replyport = unit->ReplyPort;
214 port = unit->MsgPort;
215 msg = unit->DiskImageMsg;
217 replyport->mp_SigTask = FindTask(NULL);
218 replyport->mp_Flags = PA_SIGNAL;
219 msg->dim_Command = DICMD_TAGLIST;
220 msg->dim_Tags = unmount_tags;
222 PutMsg(port, &msg->dim_Msg);
223 WaitPort(replyport);
224 GetMsg(replyport);
226 ReleaseSemaphore(unit->MsgSemaphore);
228 unit = (struct DiskImageUnit *)unit->Node.ln_Succ;
231 ObtainSemaphore(libBase->PluginSemaphore);
232 FreePlugins(libBase);
233 LoadPlugins(libBase);
234 hook = (struct Hook *)libBase->ReloadPluginsHooks->lh_Head;
235 while (hook->h_MinNode.mln_Succ) {
236 CallHookPkt(hook, SysBase, hook->h_Data);
237 hook = (struct Hook *)hook->h_MinNode.mln_Succ;
239 ReleaseSemaphore(libBase->PluginSemaphore);
241 ReleaseSemaphore(libBase->UnitSemaphore);
243 return NO_ERROR;
244 #ifdef __AROS__
245 AROS_LIBFUNC_EXIT
246 #endif
249 #ifdef __AROS__
250 AROS_LH1(void, DoHookPlugins,
251 AROS_LHA(struct Hook *, hook, A0),
252 struct DiskImageBase *, libBase, 12, DiskImage
255 AROS_LIBFUNC_INIT
256 #else
257 void DoHookPlugins (REG(a0, struct Hook *hook), REG(a6, struct DiskImageBase *libBase)) {
258 #endif
259 struct Library *SysBase = libBase->SysBase;
260 struct Library *UtilityBase = libBase->UtilityBase;
261 struct Node *plugin;
263 ObtainSemaphoreShared(libBase->PluginSemaphore);
264 plugin = libBase->Plugins->lh_Head;
265 while (plugin->ln_Succ) {
266 CallHookPkt(hook, plugin, hook->h_Data);
267 plugin = plugin->ln_Succ;
269 ReleaseSemaphore(libBase->PluginSemaphore);
270 #ifdef __AROS__
271 AROS_LIBFUNC_EXIT
272 #endif
275 #ifdef __AROS__
276 AROS_LH2(void, AddDiskChangeHook,
277 AROS_LHA(struct Hook *, hook, A0),
278 AROS_LHA(BOOL, add_or_remove, D0),
279 struct DiskImageBase *, libBase, 13, DiskImage
282 AROS_LIBFUNC_INIT
283 #else
284 void AddDiskChangeHook (REG(a0, struct Hook *hook), REG(d0, BOOL add_or_remove),
285 REG(a6, struct DiskImageBase *libBase))
287 #endif
288 struct Library *SysBase = libBase->SysBase;
290 ObtainSemaphore(libBase->DiskChangeSemaphore);
291 if (add_or_remove) {
292 AddTail(libBase->DiskChangeHooks, (struct Node *)hook);
293 } else {
294 Remove((struct Node *)hook);
296 ReleaseSemaphore(libBase->DiskChangeSemaphore);
297 #ifdef __AROS__
298 AROS_LIBFUNC_EXIT
299 #endif
302 #ifdef __AROS__
303 AROS_LH2(void, AddReloadPluginsHook,
304 AROS_LHA(struct Hook *, hook, A0),
305 AROS_LHA(BOOL, add_or_remove, D0),
306 struct DiskImageBase *, libBase, 14, DiskImage
309 AROS_LIBFUNC_INIT
310 #else
311 void AddReloadPluginsHook (REG(a0, struct Hook *hook), REG(d0, BOOL add_or_remove),
312 REG(a6, struct DiskImageBase *libBase))
314 #endif
315 struct Library *SysBase = libBase->SysBase;
317 ObtainSemaphore(libBase->PluginSemaphore);
318 if (add_or_remove) {
319 AddTail(libBase->ReloadPluginsHooks, (struct Node *)hook);
320 } else {
321 Remove((struct Node *)hook);
323 ReleaseSemaphore(libBase->PluginSemaphore);
324 #ifdef __AROS__
325 AROS_LIBFUNC_EXIT
326 #endif