Updated PCI IDs to latest snapshot.
[tangerine.git] / workbench / libs / icon / support.c
blob82e484b2ac4a00eac5e501f97f3b0b8d9ff51db4
1 /*
2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
3 $Id$
5 Miscellaneous support functions.
6 */
8 #include <string.h>
9 #include <stdio.h>
11 #include "icon_intern.h"
12 #include "support.h"
14 #define DEBUG 1
15 # include <aros/debug.h>
17 extern const IPTR IconDesc[];
19 BPTR __OpenIcon_WB(CONST_STRPTR name, LONG mode, struct IconBase *IconBase)
21 BPTR file = NULL;
22 ULONG nameLength = strlen(name);
24 if (name[nameLength - 1] == ':')
26 BPTR lock = Lock(name, ACCESS_READ);
28 if (lock != NULL)
30 BPTR cd = CurrentDir(lock);
32 file = Open("Disk.info", mode);
34 CurrentDir(cd);
35 UnLock(lock);
38 else
40 ULONG length = nameLength + 5 /* strlen(".info") */ + 1 /* '\0' */;
41 STRPTR path = AllocVecPooled(POOL, length);
43 if(path != NULL)
45 strlcpy(path, name, length);
46 strlcat(path, ".info", length);
48 file = Open(path, mode);
50 FreeVecPooled(POOL, path);
52 else
54 SetIoErr(ERROR_NO_FREE_STORE);
55 return NULL;
59 return file;
62 BOOL __CloseIcon_WB(BPTR file, struct IconBase *IconBase)
64 return Close(file);
67 BPTR __OpenDefaultIcon_WB(CONST_STRPTR name, LONG mode, struct IconBase *IconBase)
69 static const char * const readPaths[] = { "ENV:SYS", "ENVARC:SYS", NULL };
70 static const char * const writePaths[] = { "ENV:SYS", NULL };
71 const char * const *paths = NULL;
72 CONST_STRPTR path = NULL;
73 BPTR file = NULL;
74 UBYTE i;
76 /* Make sure it's a plain filename; paths are not allowed */
77 if (strpbrk(name, "/:") != NULL) return NULL;
79 if (mode == MODE_OLDFILE) paths = readPaths;
80 else paths = writePaths;
82 /* Attempt to open the icon from each path in turn */
83 for (i = 0, path = paths[0]; path != NULL; i++, path = paths[i])
85 TEXT buffer[256]; /* Filename buffer; should be more than large enough */
86 ULONG copied; /* Number of bytes copied */
88 copied = snprintf(buffer, sizeof(buffer), "%s/def_%s.info", path, name);
90 if (copied < sizeof(buffer)) /* check for truncation */
92 if ((file = Open(buffer, mode)) != NULL)
94 break;
99 return file;
102 BOOL __CloseDefaultIcon_WB(BPTR file, struct IconBase *IconBase)
104 return Close(file);
107 struct DiskObject *__ReadIcon_WB(BPTR file, struct IconBase *IconBase)
109 struct DiskObject *temp = NULL, /* Temporary icon data */
110 *icon = NULL; /* Final icon data */
112 if (ReadStruct(&(LB(IconBase)->dsh), (APTR *) &temp, file, IconDesc))
114 // FIXME: consistency checks! (ie that WBDISK IS for a disk, WBDRAWER for a dir, WBTOOL for an executable)
116 Duplicate the disk object so it can be freed with
117 FreeDiskObject().
119 // FIXME: is there any way to avoid this?
120 icon = DupDiskObject
122 temp,
123 ICONDUPA_JustLoadedFromDisk, TRUE,
124 TAG_DONE
128 // FIXME: Read/FreeStruct seem a bit broken in memory handling
129 // FIXME: shouldn't ReadStruct deallocate memory if it fails?!?!
130 if (temp != NULL) FreeStruct(temp, IconDesc);
132 if (!icon)
134 if (!PNGBase) PNGBase = OpenLibrary("datatypes/png.datatype", 0);
136 if (PNGBase && ReadIconPNG(&temp, file, IconBase))
139 Duplicate the disk object so it can be freed with
140 FreeDiskObject().
142 // FIXME: is there any way to avoid this?
143 icon = DupDiskObject
145 temp,
146 ICONDUPA_JustLoadedFromDisk, TRUE,
147 TAG_DONE
150 FreeIconPNG(temp, IconBase);
155 return icon;
158 BOOL __WriteIcon_WB(BPTR file, struct DiskObject *icon, struct IconBase *IconBase)
160 struct NativeIcon *nativeicon = GetNativeIcon(icon, IconBase);
162 if (nativeicon && nativeicon->iconPNG.handle)
164 return WriteIconPNG(file, icon, IconBase);
166 else
168 return WriteStruct(&(LB(IconBase)->dsh), (APTR) icon, file, IconDesc);
172 BPTR __LockObject_WB(CONST_STRPTR name, LONG mode, struct Library *IconBase)
174 BPTR lock = Lock(name, mode);
176 if (lock == NULL && strcasecmp(name + strlen(name) - 5, ":Disk") == 0)
178 // FIXME: perhaps allocate buffer from heap?
179 TEXT buffer[256]; /* Path buffer */
180 ULONG length = strlen(name) - 3; /* Amount to copy + NULL */
182 if (sizeof(buffer) >= length)
184 strlcpy(buffer, name, length);
185 lock = Lock(buffer, mode);
189 return lock;
192 VOID __UnLockObject_WB(BPTR lock, struct Library *IconBase)
194 if (lock != NULL) UnLock(lock);
198 CONST_STRPTR GetDefaultIconName(LONG type)
200 static const char * const defaultNames[] =
202 "Disk", /* WBDISK (1) */
203 "Drawer", /* WBDRAWER (2) */
204 "Tool", /* WBTOOL (3) */
205 "Project", /* WBPROJECT (4) */
206 "Trashcan", /* WBGARBAGE (5) */
207 "Device", /* WBDEVICE (6) */
208 "Kick", /* WBKICK (7) */
209 "AppIcon" /* WBAPPICON (8) */
212 if (type >= 1 && type <= 8)
214 return defaultNames[type - 1];
216 else
218 return NULL;
223 LONG CalcIconHash(struct DiskObject *dobj)
225 LONG l1, l2, l3, l4, hash;
227 /* FIXME: Probably sucks. I have no clue about this hash stuff */
229 l1 = ((LONG)dobj) & 0xFF;
230 l2 = (((LONG)dobj) >> 8) & 0xFF;
231 l3 = (((LONG)dobj) >> 16) & 0xFF;
232 l4 = (((LONG)dobj) >> 24) & 0xFF;
234 hash = (l1 + l2 + l3 + l4) % ICONLIST_HASHSIZE;
236 return hash;
239 VOID AddIconToList(struct NativeIcon *icon, struct IconBase *IconBase)
241 LONG hash;
243 hash = CalcIconHash(&icon->dobj);
245 ObtainSemaphore(&IconBase->iconlistlock);
246 AddTail((struct List *)&IconBase->iconlists[hash], (struct Node *)&icon->node);
247 ReleaseSemaphore(&IconBase->iconlistlock);
250 VOID RemoveIconFromList(struct NativeIcon *icon, struct IconBase *IconBase)
252 ObtainSemaphore(&IconBase->iconlistlock);
253 Remove((struct Node *)&icon->node);
254 ReleaseSemaphore(&IconBase->iconlistlock);
257 struct NativeIcon *GetNativeIcon(struct DiskObject *dobj, struct IconBase *IconBase)
259 struct NativeIcon *icon, *reticon = NULL;
260 LONG hash;
261 LONG i = 0;
263 hash = CalcIconHash(dobj);
265 ObtainSemaphoreShared(&IconBase->iconlistlock);
266 ForeachNode((struct List *)&IconBase->iconlists[hash], icon)
268 i++;
269 if (dobj == &icon->dobj)
271 reticon = icon;
272 break;
275 ReleaseSemaphore(&IconBase->iconlistlock);
277 return reticon;