forwarding build fix when MUIA_Scrollgroup_AutoBars is defined (NicJA).
[AROS-Contrib.git] / scalos / main / Patches-aos4.c
blob05fd918c71312646f2f34a316506b1ae3bb7014e
1 // Patches-aos4.c
2 // $Date$
3 // $Revision$
6 #include <exec/types.h>
7 #include <graphics/rastport.h>
8 #include <utility/hooks.h>
9 #include <intuition/gadgetclass.h>
10 #include <workbench/workbench.h>
11 #include <workbench/startup.h>
12 #include <workbench/icon.h>
13 #include <libraries/gadtools.h>
14 #include <libraries/iffparse.h>
15 #include <datatypes/pictureclass.h>
16 #include <dos/dostags.h>
17 #include <guigfx/guigfx.h>
19 #define __USE_SYSBASE
21 #include <proto/dos.h>
22 #include <proto/exec.h>
23 #include <proto/layers.h>
24 #include <proto/intuition.h>
25 #include <proto/graphics.h>
26 #include <proto/utility.h>
27 #include <proto/icon.h>
28 #include <proto/iconobject.h>
29 #include <proto/guigfx.h>
30 #include <proto/datatypes.h>
31 #include <proto/gadtools.h>
32 #include <proto/wb.h>
33 #include "debug.h"
34 #include <proto/scalos.h>
36 #include <clib/alib_protos.h>
38 #include <defs.h>
39 #include <datatypes/iconobject.h>
40 #include <scalos/palette.h>
41 #include <scalos/pattern.h>
42 #include <scalos/scalos.h>
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <string.h>
48 #include "scalos_structures.h"
49 #include "functions.h"
50 #include "Variables.h"
51 #include "Patches.h"
53 //----------------------------------------------------------------------------
54 // Revision history :
56 // 20011203 jl initial history
57 //----------------------------------------------------------------------------
59 // local data structures
61 struct PatchEntry
63 struct Interface **pe_Interface;
64 LONG pe_IFaceOffset;
65 APTR pe_NewFunction;
66 APTR *pe_OldFunction;
69 //----------------------------------------------------------------------------
71 // local functions
73 static void SetPatches(const struct PatchEntry *PatchList);
74 static BOOL RemovePatches(const struct PatchEntry *PatchList);
76 //----------------------------------------------------------------------------
78 // public data items
80 //----------------------------------------------------------------------------
82 // local data items
83 static LIBFUNC_P7VA_PROTO(struct AppIcon *, sca_AddAppIcon,
84 D0, ULONG, Id,
85 D1, ULONG, UserData,
86 A0, CONST_STRPTR, IconText,
87 A1, struct MsgPort *, iconPort,
88 A2, BPTR, lock,
89 A3, struct DiskObject *, DiskObj,
90 A6, struct Library *, wbBase);
91 static LIBFUNC_P5VA_PROTO(struct AppWindow *, sca_AddAppWindow,
92 D0, ULONG, Id,
93 D1, ULONG, UserData,
94 A0, struct Window *, window,
95 A1, struct MsgPort *, iconPort,
96 A6, struct Library *, wbBase);
97 static LIBFUNC_P5VA_PROTO(struct AppMenuItem *, sca_AddAppMenuItem,
98 D0, ULONG, Id,
99 D1, ULONG, UserData,
100 A0, CONST_STRPTR, text,
101 A1, struct MsgPort *, iconPort,
102 A6, struct Library *, wbBase);
103 static LIBFUNC_P2VA_PROTO(struct Screen *, sca_OpenScreen,
104 A0, struct NewScreen *, newScr,
105 A6, struct IntuitionBase *, iBase);
107 static APTR OldAddAppIcon;
108 static APTR OldAddAppWindow;
109 static APTR OldAppAppMenuItem;
110 static APTR OldOpenScreen;
112 //----------------------------------------------------------------------------
114 #define OffsetCloseWorkBench offsetof(struct IntuitionIFace, CloseWorkBench)
115 #define OffsetOpenWorkBench offsetof(struct IntuitionIFace, OpenWorkBench)
116 #define OffsetOpenScreenTagList offsetof(struct IntuitionIFace, OpenScreenTagList)
117 #define OffsetOpenScreen offsetof(struct IntuitionIFace, OpenScreen)
119 #define OffsetUpdateWorkbench offsetof(struct WorkbenchIFace, UpdateWorkbench)
120 //#define OffsetwbPrivate2 offsetof(struct WorkbenchIFace, Private1)
121 #define OffsetAddAppWindowA offsetof(struct WorkbenchIFace, AddAppWindowA)
122 #define OffsetAddAppWindow offsetof(struct WorkbenchIFace, AddAppWindow)
123 #define OffsetRemoveAppWindow offsetof(struct WorkbenchIFace, RemoveAppWindow)
124 #define OffsetAddAppIconA offsetof(struct WorkbenchIFace, AddAppIconA)
125 #define OffsetAddAppIcon offsetof(struct WorkbenchIFace, AddAppIcon)
126 #define OffsetRemoveAppIcon offsetof(struct WorkbenchIFace, RemoveAppIcon)
127 #define OffsetAddAppMenuItemA offsetof(struct WorkbenchIFace, AddAppMenuItemA)
128 #define OffsetAddAppMenuItem offsetof(struct WorkbenchIFace, AddAppMenuItem)
129 #define OffsetRemoveAppMenuItem offsetof(struct WorkbenchIFace, RemoveAppMenuItem)
130 #define OffsetWBInfo offsetof(struct WorkbenchIFace, WBInfo)
131 //#define OffsetSetBackFill offsetof(struct WorkbenchIFace, ...)
133 #define OffsetPutDiskObject offsetof(struct IconIFace, PutDiskObject)
134 #define OffsetDeleteDiskObject offsetof(struct IconIFace, DeleteDiskObject)
135 #define OffsetPutIconTagList offsetof(struct IconIFace, PutIconTagList)
137 #define OffsetRename offsetof(struct DOSIFace, Rename)
138 #define OffsetDeleteFile offsetof(struct DOSIFace, DeleteFile)
139 #define OffsetOpen offsetof(struct DOSIFace, Open)
140 #define OffsetClose offsetof(struct DOSIFace, Close)
141 #define OffsetCreateDir offsetof(struct DOSIFace, CreateDir)
143 //----------------------------------------------------------------------------
144 static const struct PatchEntry EmulationPatches[] =
146 { (struct Interface **)(APTR) &IIntuition, OffsetCloseWorkBench, (APTR)sca_CloseWorkBench, (APTR) &OldCloseWB },
147 { (struct Interface **)(APTR) &IIntuition, OffsetOpenWorkBench, (APTR)sca_OpenWorkBench, (APTR) &OldOpenWB },
148 { (struct Interface **)(APTR) &IIntuition, OffsetOpenScreenTagList, (APTR)sca_OpenScreenTagList, (APTR) &OldOpenScreenTagList },
149 { (struct Interface **)(APTR) &IIntuition, OffsetOpenScreen, (APTR)sca_OpenScreen, (APTR) &OldOpenScreen },
150 { (struct Interface **)(APTR) &IWorkbench, OffsetAddAppIconA, (APTR)sca_AddAppIconA, (APTR) &OldAddAppIconA },
151 { (struct Interface **)(APTR) &IWorkbench, OffsetAddAppIcon, (APTR)sca_AddAppIcon, (APTR) &OldAddAppIcon },
152 { (struct Interface **)(APTR) &IWorkbench, OffsetRemoveAppIcon, (APTR)sca_RemoveAppIcon, (APTR) &OldRemoveAppIcon },
153 { (struct Interface **)(APTR) &IWorkbench, OffsetAddAppWindowA, (APTR)sca_AddAppWindowA, (APTR) &OldAddAppWindowA },
154 { (struct Interface **)(APTR) &IWorkbench, OffsetAddAppWindow, (APTR)sca_AddAppWindow, (APTR) &OldAddAppWindow },
155 { (struct Interface **)(APTR) &IWorkbench, OffsetRemoveAppWindow, (APTR)sca_RemoveAppWindow, (APTR) &OldRemoveAppWindow },
156 { (struct Interface **)(APTR) &IWorkbench, OffsetAddAppMenuItemA, (APTR)sca_AddAppMenuItemA, (APTR) &OldAppAppMenuItemA },
157 { (struct Interface **)(APTR) &IWorkbench, OffsetAddAppMenuItem, (APTR)sca_AddAppMenuItem, (APTR) &OldAppAppMenuItem },
158 { (struct Interface **)(APTR) &IWorkbench, OffsetRemoveAppMenuItem, (APTR)sca_RemoveAppMenuItem, (APTR) &OldRemoveAppMenuItem },
159 { (struct Interface **)(APTR) &IWorkbench, OffsetWBInfo, (APTR)sca_WBInfo, (APTR) &OldWBInfo },
161 // { (struct Interface **)(APTR) &IWorkbench, OffsetwbPrivate2, (APTR)sca_GetLocString, (APTR) &OldwbPrivate2 },
162 { NULL, 0, (APTR)NULL, NULL },
164 static const struct PatchEntry StandardPatches[] =
166 { (struct Interface **)(APTR) &IIcon, OffsetPutDiskObject, (APTR)sca_PutDiskObject, (APTR) &OldPutDiskObject },
167 { (struct Interface **)(APTR) &IIcon, OffsetDeleteDiskObject, (APTR)sca_DeleteDiskObject, (APTR) &OldDeleteDiskObject },
168 { (struct Interface **)(APTR) &IIcon, OffsetPutIconTagList, (APTR)sca_PutIconTagList, (APTR) &OldPutIconTagList},
170 { (struct Interface **)(APTR) &IDOS, OffsetDeleteFile, (APTR)sca_DeleteFile, (APTR) &OldDeleteFile },
171 { (struct Interface **)(APTR) &IDOS, OffsetRename, (APTR)sca_Rename, (APTR) &OldRename },
172 { (struct Interface **)(APTR) &IDOS, OffsetOpen, (APTR)sca_Open, (APTR) &OldOpen },
173 { (struct Interface **)(APTR) &IDOS, OffsetClose, (APTR)sca_Close, (APTR) &OldClose },
174 { (struct Interface **)(APTR) &IDOS, OffsetCreateDir, (APTR)sca_CreateDir, (APTR) &OldCreateDir },
175 { NULL, 0, (APTR)NULL, NULL },
177 static const struct PatchEntry HardEmulationPatches[] =
179 { (struct Interface **)(APTR) &IWorkbench, OffsetUpdateWorkbench, (APTR)sca_UpdateWorkbench, (APTR) &OldUpdateWorkbench },
180 // { (struct Interface **)(APTR) &IWorkbench, OffsetSetBackFill, (APTR)sca_SetBackFill, (APTR) &OldSetBackFill },
181 { NULL, 0, (APTR)NULL, NULL },
184 //----------------------------------------------------------------------------
186 void SetAllPatches(struct MainTask *mt)
188 PatchInit();
190 SetPatches(StandardPatches);
192 if (mt->emulation)
194 if (CurrentPrefs.pref_HardEmulation)
195 SetPatches(HardEmulationPatches);
197 SetPatches(EmulationPatches);
202 ULONG RemoveAllPatches(struct MainTask *mt)
204 if (!RemovePatches(StandardPatches))
205 return FALSE;
207 if (mt->emulation)
209 if (!RemovePatches(EmulationPatches))
211 SetPatches(StandardPatches);
212 return FALSE;
215 if (CurrentPrefs.pref_HardEmulation)
217 if (!RemovePatches(HardEmulationPatches))
219 SetPatches(EmulationPatches);
220 SetPatches(StandardPatches);
226 PatchCleanup();
228 return TRUE;
231 static void SetPatches(const struct PatchEntry *PatchList)
233 Forbid();
235 d1(kprintf("%s/%s/%ld: PatchList=%08lx\n", __FILE__, __FUNC__, __LINE__, PatchList));
237 while (PatchList->pe_Interface)
239 if (NULL == *PatchList->pe_OldFunction)
241 *PatchList->pe_OldFunction = SetMethod(*PatchList->pe_Interface,
242 PatchList->pe_IFaceOffset, PatchList->pe_NewFunction);
244 d1(KPrintF("%s/%s/%ld: Library=%08lx Offset=%ld NewFunc=%08lx OldFunc=%08lx\n", \
245 __FILE__, __FUNC__, __LINE__, *PatchList->pe_Interface, \
246 PatchList->pe_IFaceOffset, PatchList->pe_NewFunction, \
247 *PatchList->pe_OldFunction));
250 PatchList++;
253 // CacheClearU();
254 Permit();
258 static BOOL RemovePatches(const struct PatchEntry *PatchList)
260 const struct PatchEntry *pl = PatchList;
261 BOOL Success = TRUE;
263 d1(KPrintF("%s/%s/%ld: PatchList=%08lx\n", __FILE__, __FUNC__, __LINE__, PatchList));
265 Forbid();
267 while (pl->pe_Interface)
269 if (pl->pe_OldFunction)
271 APTR patchedFunction;
273 patchedFunction = SetMethod(*pl->pe_Interface,
274 pl->pe_IFaceOffset, *pl->pe_OldFunction);
275 *pl->pe_OldFunction = NULL;
277 d1(KPrintF("%s/%s/%ld: Library=%08lx Offset=%ld NewFunc=%08lx patchedFunc=%08lx\n", \
278 __FILE__, __FUNC__, __LINE__, *pl->pe_Interface, \
279 pl->pe_IFaceOffset, pl->pe_NewFunction, patchedFunction));
280 #if !defined(__MORPHOS__)
281 if (patchedFunction != pl->pe_NewFunction)
283 *pl->pe_OldFunction = SetMethod(*pl->pe_Interface,
284 pl->pe_IFaceOffset, patchedFunction);
286 Success = FALSE;
288 #endif /* __MORPHOS__ */
291 pl++;
294 // CacheClearU();
295 Permit();
297 d1(KPrintF("%s/%s/%ld: Success=%ld\n", __FILE__, __FUNC__, __LINE__, Success));
299 if (!Success)
300 SetPatches(PatchList);
302 return Success;
305 static LIBFUNC_P7VA(struct AppIcon *, sca_AddAppIcon,
306 D0, ULONG, Id,
307 D1, ULONG, UserData,
308 A0, CONST_STRPTR, IconText,
309 A1, struct MsgPort *, iconPort,
310 A2, BPTR, lock,
311 A3, struct DiskObject *, DiskObj,
312 A6, struct Library *, wbBase)
314 struct AppIcon *ret;
315 struct WorkbenchIFace *IWorkbench = (struct WorkbenchIFace *)self;
316 va_list args;
317 va_startlinear(args, DiskObj);
319 (void)wbBase;
321 ret = AddAppIconA(Id, UserData, IconText, iconPort, lock, DiskObj,
322 va_getlinearva(args, const struct TagItem *));
324 va_end(args);
326 return(ret);
328 LIBFUNC_END
330 static LIBFUNC_P5VA(struct AppWindow *, sca_AddAppWindow,
331 D0, ULONG, Id,
332 D1, ULONG, UserData,
333 A0, struct Window *, window,
334 A1, struct MsgPort *, iconPort,
335 A6, struct Library *, wbBase)
337 struct AppWindow *ret;
338 struct WorkbenchIFace *IWorkbench = (struct WorkbenchIFace *)self;
339 va_list args;
340 va_startlinear(args, iconPort);
342 (void)wbBase;
344 ret = AddAppWindowA(Id, UserData, window, iconPort, va_getlinearva(args, const struct TagItem *));
346 va_end(args);
348 return(ret);
350 LIBFUNC_END
352 static LIBFUNC_P5VA(struct AppMenuItem *, sca_AddAppMenuItem,
353 D0, ULONG, Id,
354 D1, ULONG, UserData,
355 A0, CONST_STRPTR, text,
356 A1, struct MsgPort *, iconPort,
357 A6, struct Library *, wbBase)
359 struct AppMenuItem *ret;
360 struct WorkbenchIFace *IWorkbench = (struct WorkbenchIFace *)self;
361 va_list args;
362 va_startlinear(args, iconPort);
364 (void)wbBase;
366 ret = AddAppMenuItemA(Id, UserData, text, iconPort, va_getlinearva(args, const struct TagItem *));
368 va_end(args);
370 return(ret);
372 LIBFUNC_END
374 static LIBFUNC_P2VA(struct Screen *, sca_OpenScreen,
375 A0, struct NewScreen *, newScr,
376 A6, struct IntuitionBase *, iBase)
378 struct Screen *ret;
379 struct IntuitionIFace *IIntuition = (struct IntuitionIFace *)self;
380 va_list args;
381 va_startlinear(args, newScr);
383 (void)iBase;
385 ret = OpenScreenTagList(newScr, va_getlinearva(args, const struct TagItem *));
387 va_end(args);
389 return(ret);
391 LIBFUNC_END