Initial import of Scalos. To decrease size I have
[AROS-Contrib.git] / scalos / Plugins / OOP / wb39_plugin / DefIcons.c
blobcbd55773dfeada23ea00831e6420ee6ac2dc3ca3
1 // DefIcons.c
2 // 13 Nov 2004 19:37:42
5 #include <stdlib.h>
6 #include <string.h>
7 #include <exec/types.h>
8 #include <intuition/classes.h>
9 #include <datatypes/iconobject.h>
10 #include <dos/dos.h>
11 #include <dos/dosextens.h>
12 #include <dos/dostags.h>
13 #include <scalos/scalos.h>
14 #include <workbench/workbench.h>
15 #include <workbench/icon.h>
17 #include <clib/alib_protos.h>
19 #include <proto/iconobject.h>
20 #include <proto/utility.h>
21 #include <proto/dos.h>
22 #include <proto/exec.h>
23 #include <proto/icon.h>
24 #include <proto/intuition.h>
25 #include <proto/scalos.h>
28 #include <defs.h>
30 /* How it works:
31 gets the "Global Identify Hook" from icon.library and calls this hook itself.
32 If DefIcons is being started automatically, like it is default in OS3.9,
33 its identify hook will be got from icon.library.
36 //----------------------------------------------------------------------------
37 // Revision history :
39 // 20010405 jl 44.5
40 // Forgot to set DTA_Name for generated default icons. Fixed.
41 //----------------------------------------------------------------------------
43 struct SM_GetDefIcon
45 ULONG MethodID;
46 STRPTR filename;
47 ULONG filetype; // from FileInfoBlock fib_DirEntryType
48 ULONG protectionbits;
52 #define d(x) ;
53 #define d1(x) ;
54 #define d2 (x) x;
57 // aus debug.lib
58 extern int kprintf(const char *fmt, ...);
60 static BOOL Init(void);
61 SAVEDS(void) INTERRUPT ASM DefIconsCleanup(void);
62 static BOOL OpenLibraries(void);
63 static void CloseLibraries(void);
64 static Object *GetDefIcon(Class *cl, Object *obj, struct SM_GetDefIcon *msg);
67 struct ExecBase *SysBase;
68 struct DosLibrary *DOSBase;
69 struct Library *IconobjectBase;
70 struct Library *UtilityBase;
71 struct Library *IconBase;
72 struct Library *ScalosBase;
73 struct IntuitionBase *IntuitionBase;
76 static BOOL fInit;
79 SAVEDS(ULONG) INTERRUPT ASM myHookFunc(REG(a0, Class *cl),
80 REG(a2, Object *obj),
81 REG(a1, Msg msg))
83 ULONG Result;
85 d(kprintf(__FUNC__ "/%ld: MethodID=%08lx\n", __LINE__, msg->MethodID));
87 switch (msg->MethodID)
89 case SCCM_IconWin_GetDefIcon:
90 Result = (ULONG) GetDefIcon(cl, obj, (struct SM_GetDefIcon *) msg);
92 d(kprintf(__FUNC__ "/%ld: RootList=%08lx InternInfos=%08lx\n", __LINE__, \
93 rList, rList->rl_internInfos));
95 break;
97 default:
98 d(kprintf(__FUNC__ "/%ld: MethodID=%08lx\n", __LINE__, msg->MethodID));
99 Result = DoSuperMethodA(cl, obj, msg);
100 break;
103 return Result;
107 SAVEDS(ULONG) INTERRUPT ASM DefIconsInit(void)
109 d(kprintf(__FUNC__ "/%ld: \n", __LINE__));
111 if (!fInit)
113 BOOL Success = fInit = Init();
115 if (!Success)
116 DefIconsCleanup();
118 return Success;
121 return TRUE;
125 static BOOL Init(void)
127 if (!OpenLibraries())
128 return FALSE;
130 d(kprintf(__FUNC__ "/%ld: IconobjectBase=%08lx UtilityBase=%08lx\n", \
131 __LINE__, IconobjectBase, UtilityBase));
133 return TRUE;
137 SAVEDS(void) INTERRUPT ASM DefIconsCleanup(void)
139 CloseLibraries();
141 fInit = FALSE;
145 static BOOL OpenLibraries(void)
147 #ifndef __AROS__
148 SysBase = *( (struct ExecBase **) 4l);
149 #endif
151 DOSBase = (struct DosLibrary *) OpenLibrary(DOSNAME, 39);
152 if (NULL == DOSBase)
153 return FALSE;
155 IconobjectBase = OpenLibrary("iconobject.library", 39);
156 if (NULL == IconobjectBase)
157 return FALSE;
159 UtilityBase = OpenLibrary("utility.library", 40);
160 if (NULL == UtilityBase)
161 return FALSE;
163 IconBase = OpenLibrary("icon.library", 44);
164 if (NULL == IconBase)
165 return FALSE;
167 IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 39);
168 if (NULL == IntuitionBase)
169 return FALSE;
171 ScalosBase = OpenLibrary(SCALOSNAME, 41);
172 if (NULL == ScalosBase)
173 return FALSE;
175 return TRUE;
179 static void CloseLibraries(void)
181 if (ScalosBase)
183 CloseLibrary(ScalosBase);
184 ScalosBase = NULL;
186 if (IntuitionBase)
188 CloseLibrary((struct Library *) IntuitionBase);
189 IntuitionBase = NULL;
191 if (IconBase)
193 CloseLibrary(IconBase);
194 IconBase = NULL;
196 if (DOSBase)
198 CloseLibrary((struct Library *) DOSBase);
199 DOSBase = NULL;
201 if (IconobjectBase)
203 CloseLibrary(IconobjectBase);
204 IconobjectBase = NULL;
206 if (UtilityBase)
208 CloseLibrary(UtilityBase);
209 UtilityBase = NULL;
214 void _XCEXIT(long x)
219 static Object *GetDefIcon(Class *cl, Object *obj, struct SM_GetDefIcon *msg)
221 Object *IconObj = NULL;
222 struct DiskObject *diskObj = NULL;
223 struct Hook *pHook = NULL;
225 d1(kprintf(__FUNC__ "/%ld: filename=<%s> type=%08lx protection=%08lx\n", __LINE__, \
226 msg->filename, msg->filetype, msg->protectionbits));
228 // IconControlA()
229 IconControl(NULL,
230 ICONCTRLA_GetGlobalIdentifyHook, &pHook,
231 TAG_END);
233 d1(kprintf(__FUNC__ "/%ld: pHook=%08lx\n", __LINE__, pHook));
234 if (pHook)
236 struct TagItem EmptyTagList = { TAG_END, 0 };
237 BPTR fLock;
238 struct FileInfoBlock *fib;
240 fLock = Lock(msg->filename, ACCESS_READ);
241 d1(kprintf(__FUNC__ "/%ld: fLock=%08lx\n", __LINE__, fLock));
242 if (fLock)
244 BPTR fh;
245 BPTR parentLock = ParentDir(fLock);
247 fh = Open(msg->filename, MODE_OLDFILE);
249 fib = AllocDosObject(DOS_FIB, TAG_END);
250 d1(kprintf(__FUNC__ "/%ld: fib=%08lx fh=%08lx\n", __LINE__, fib, fh));
251 if (fib)
253 struct IconIdentifyMsg iim;
255 Examine(fLock, fib);
257 iim.iim_SysBase = (struct Library *) SysBase;
258 iim.iim_DOSBase = (struct Library *) DOSBase;
259 iim.iim_UtilityBase = UtilityBase;
260 iim.iim_IconBase = IconBase;
262 iim.iim_FileLock = fLock;
263 iim.iim_ParentLock = parentLock;
264 iim.iim_FIB = fib;
265 iim.iim_FileHandle = fh;
267 iim.iim_Tags = &EmptyTagList;
269 diskObj = (struct DiskObject *) CallHookPkt(pHook, NULL, &iim);
271 d1(kprintf(__FUNC__ "/%ld: diskobj=%08lx\n", __LINE__, diskObj));
273 FreeDosObject(DOS_FIB, fib);
276 if (parentLock)
277 UnLock(parentLock);
278 if (fh)
279 Close(fh);
281 UnLock(fLock);
285 d1(kprintf(__FUNC__ "/%ld: diskobj=%08lx FileType=%ld\n", __LINE__, diskObj, msg->filetype));
287 if (NULL == diskObj && IconBase->lib_Version >= 44)
289 ULONG iconType;
291 switch (msg->filetype)
293 case ST_ROOT:
294 iconType = WBDISK;
295 break;
297 case ST_USERDIR:
298 case ST_LINKDIR:
299 iconType = WBDRAWER;
300 break;
302 default:
303 iconType = WBPROJECT;
304 break;
307 // GetIconTagList
308 diskObj = GetIconTags(msg->filename,
309 ICONGETA_GetDefaultType, iconType,
310 ICONGETA_FailIfUnavailable, FALSE,
311 TAG_END);
312 d1(kprintf(__FUNC__ "/%ld: diskobj=%08lx\n", __LINE__, diskObj));
315 if (NULL == diskObj)
317 diskObj = GetDiskObjectNew(msg->filename);
318 d1(kprintf(__FUNC__ "/%ld: diskobj=%08lx\n", __LINE__, diskObj));
321 d1(kprintf(__FUNC__ "/%ld: diskobj=%08lx\n", __LINE__, diskObj));
323 if (diskObj)
325 ULONG SupportedIconTypes = ~0;
327 if (ScalosBase->lib_Version >= 40)
329 SCA_ScalosControl(NULL,
330 SCALOSCTRLA_GetSupportedIconTypes, &SupportedIconTypes,
331 TAG_END);
334 IconObj = Convert2IconObjectTags(diskObj,
335 IDTA_SupportedIconTypes, SupportedIconTypes,
336 TAG_END);
338 if (IconObj)
340 SetAttrs(IconObj,
341 DTA_Name, msg->filename,
342 IDTA_DoFreeDiskObject, TRUE,
343 TAG_END);
345 else
346 FreeDiskObject(diskObj);
349 d1(if (diskObj && diskObj->do_DrawerData)\
350 kprintf(__FUNC__ "/%ld: dd_Flags=%08lx\n", __LINE__, diskObj->do_DrawerData->dd_Flags));
352 d1(kprintf(__FUNC__ "/%ld: IconObj=%08lx\n", __LINE__, diskObj, IconObj));
354 return IconObj;