2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
5 Miscellaneous support functions.
11 #include "icon_intern.h"
15 # include <aros/debug.h>
17 extern const IPTR IconDesc
[];
19 BPTR
__OpenIcon_WB(CONST_STRPTR name
, LONG mode
, struct IconBase
*IconBase
)
22 ULONG nameLength
= strlen(name
);
24 if (name
[nameLength
- 1] == ':')
26 BPTR lock
= Lock(name
, ACCESS_READ
);
30 BPTR cd
= CurrentDir(lock
);
32 file
= Open("Disk.info", mode
);
40 ULONG length
= nameLength
+ 5 /* strlen(".info") */ + 1 /* '\0' */;
41 STRPTR path
= AllocVecPooled(POOL
, length
);
45 strlcpy(path
, name
, length
);
46 strlcat(path
, ".info", length
);
48 file
= Open(path
, mode
);
50 FreeVecPooled(POOL
, path
);
54 SetIoErr(ERROR_NO_FREE_STORE
);
62 BOOL
__CloseIcon_WB(BPTR file
, struct IconBase
*IconBase
)
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
;
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
)
102 BOOL
__CloseDefaultIcon_WB(BPTR file
, struct IconBase
*IconBase
)
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
119 // FIXME: is there any way to avoid this?
123 ICONDUPA_JustLoadedFromDisk
, TRUE
,
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
);
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
142 // FIXME: is there any way to avoid this?
146 ICONDUPA_JustLoadedFromDisk
, TRUE
,
150 FreeIconPNG(temp
, IconBase
);
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
);
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
);
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];
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
;
239 VOID
AddIconToList(struct NativeIcon
*icon
, struct IconBase
*IconBase
)
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
;
263 hash
= CalcIconHash(dobj
);
265 ObtainSemaphoreShared(&IconBase
->iconlistlock
);
266 ForeachNode((struct List
*)&IconBase
->iconlists
[hash
], icon
)
269 if (dobj
== &icon
->dobj
)
275 ReleaseSemaphore(&IconBase
->iconlistlock
);