2 Copyright © 1995-2006, The AROS Development Team. All rights reserved.
9 /*********************************************************************************************/
12 #include <aros/macros.h>
15 #include <aros/debug.h>
21 /*********************************************************************************************/
23 #define ARRAY_TO_LONG(x) ( ((x)[0] << 24UL) + ((x)[1] << 16UL) + ((x)[2] << 8UL) + ((x)[3]) )
24 #define ARRAY_TO_WORD(x) ( ((x)[0] << 8UL) + ((x)[1]) )
26 #define LONG_TO_ARRAY(x,y) (y)[0] = (UBYTE)(ULONG)((x) >> 24UL); \
27 (y)[1] = (UBYTE)(ULONG)((x) >> 16UL); \
28 (y)[2] = (UBYTE)(ULONG)((x) >> 8UL); \
29 (y)[3] = (UBYTE)(ULONG)((x));
31 #define WORD_TO_ARRAY(x,y) (y)[0] = (UBYTE)(ULONG)((x) >> 8UL); \
32 (y)[1] = (UBYTE)(ULONG)((x));
34 /*********************************************************************************************/
43 struct FileIControlPrefs
45 UBYTE ic_Reserved0
[4];
46 UBYTE ic_Reserved1
[4];
47 UBYTE ic_Reserved2
[4];
48 UBYTE ic_Reserved3
[4];
58 /*********************************************************************************************/
60 void InitPrefs(STRPTR filename
, BOOL use
, BOOL save
)
62 if (!LoadPrefs(filename
))
69 restore_prefs
= icontrolprefs
;
73 SavePrefs(CONFIGNAME_ENV
);
78 SavePrefs(CONFIGNAME_ENVARC
);
81 if (use
|| save
) Cleanup(NULL
);
84 /*********************************************************************************************/
86 void CleanupPrefs(void)
90 /*********************************************************************************************/
92 BOOL
LoadPrefs(STRPTR filename
)
94 static struct FileIControlPrefs loadprefs
;
95 struct IFFHandle
*iff
;
98 D(bug("LoadPrefs: Trying to open \"%s\"\n", filename
));
100 if ((iff
= AllocIFF()))
102 if ((iff
->iff_Stream
= (IPTR
)Open(filename
, MODE_OLDFILE
)))
104 D(bug("LoadPrefs: stream opened.\n"));
108 if (!OpenIFF(iff
, IFFF_READ
))
110 D(bug("LoadPrefs: OpenIFF okay.\n"));
112 if (!StopChunk(iff
, ID_PREF
, ID_ICTL
))
114 D(bug("LoadPrefs: StopChunk okay.\n"));
116 if (!ParseIFF(iff
, IFFPARSE_SCAN
))
118 struct ContextNode
*cn
;
120 D(bug("LoadPrefs: ParseIFF okay.\n"));
122 cn
= CurrentChunk(iff
);
124 if (cn
->cn_Size
== sizeof(loadprefs
))
126 D(bug("LoadPrefs: ID_ICTL chunk size okay.\n"));
128 if (ReadChunkBytes(iff
, &loadprefs
, sizeof(loadprefs
)) == sizeof(loadprefs
))
130 D(bug("LoadPrefs: Reading chunk successful.\n"));
132 icontrolprefs
.ic_Reserved
[0] = ARRAY_TO_LONG(loadprefs
.ic_Reserved0
);
133 icontrolprefs
.ic_Reserved
[1] = ARRAY_TO_LONG(loadprefs
.ic_Reserved1
);
134 icontrolprefs
.ic_Reserved
[2] = ARRAY_TO_LONG(loadprefs
.ic_Reserved2
);
135 icontrolprefs
.ic_Reserved
[3] = ARRAY_TO_LONG(loadprefs
.ic_Reserved3
);
136 icontrolprefs
.ic_TimeOut
= ARRAY_TO_WORD(loadprefs
.ic_TimeOut
);
137 icontrolprefs
.ic_MetaDrag
= ARRAY_TO_WORD(loadprefs
.ic_MetaDrag
);
138 icontrolprefs
.ic_Flags
= ARRAY_TO_LONG(loadprefs
.ic_Flags
);
139 icontrolprefs
.ic_WBtoFront
= loadprefs
.ic_WBtoFront
;
140 icontrolprefs
.ic_FrontToBack
= loadprefs
.ic_FrontToBack
;
141 icontrolprefs
.ic_ReqTrue
= loadprefs
.ic_ReqTrue
;
142 icontrolprefs
.ic_ReqFalse
= loadprefs
.ic_ReqFalse
;
144 D(bug("LoadPrefs: Everything okay :-)\n"));
150 } /* if (!ParseIFF(iff, IFFPARSE_SCAN)) */
152 } /* if (!StopChunk(iff, ID_PREF, ID_INPT)) */
156 } /* if (!OpenIFF(iff, IFFF_READ)) */
158 Close((BPTR
)iff
->iff_Stream
);
160 } /* if ((iff->iff_Stream = (IPTR)Open(filename, MODE_OLDFILE))) */
164 } /* if ((iff = AllocIFF())) */
169 /*********************************************************************************************/
171 BOOL
SavePrefs(STRPTR filename
)
173 static struct FileIControlPrefs saveprefs
;
174 struct IFFHandle
*iff
;
175 BOOL retval
= FALSE
, delete_if_error
= FALSE
;
177 LONG_TO_ARRAY(icontrolprefs
.ic_Reserved
[0], saveprefs
.ic_Reserved0
);
178 LONG_TO_ARRAY(icontrolprefs
.ic_Reserved
[1], saveprefs
.ic_Reserved1
);
179 LONG_TO_ARRAY(icontrolprefs
.ic_Reserved
[2], saveprefs
.ic_Reserved2
);
180 LONG_TO_ARRAY(icontrolprefs
.ic_Reserved
[3], saveprefs
.ic_Reserved3
);
181 WORD_TO_ARRAY(icontrolprefs
.ic_TimeOut
, saveprefs
.ic_TimeOut
);
182 WORD_TO_ARRAY(icontrolprefs
.ic_MetaDrag
, saveprefs
.ic_MetaDrag
);
183 LONG_TO_ARRAY(icontrolprefs
.ic_Flags
, saveprefs
.ic_Flags
);
184 saveprefs
.ic_WBtoFront
= icontrolprefs
.ic_WBtoFront
;
185 saveprefs
.ic_FrontToBack
= icontrolprefs
.ic_FrontToBack
;
186 saveprefs
.ic_ReqTrue
= icontrolprefs
.ic_ReqTrue
;
187 saveprefs
.ic_ReqFalse
= icontrolprefs
.ic_ReqFalse
;
189 D(bug("SavePrefs: Trying to open \"%s\"\n", filename
));
191 if ((iff
= AllocIFF()))
193 if ((iff
->iff_Stream
= (IPTR
)Open(filename
, MODE_NEWFILE
)))
195 D(bug("SavePrefs: stream opened.\n"));
197 delete_if_error
= TRUE
;
201 if (!OpenIFF(iff
, IFFF_WRITE
))
203 D(bug("SavePrefs: OpenIFF okay.\n"));
205 if (!PushChunk(iff
, ID_PREF
, ID_FORM
, IFFSIZE_UNKNOWN
))
207 D(bug("SavePrefs: PushChunk(FORM) okay.\n"));
209 if (!PushChunk(iff
, ID_PREF
, ID_PRHD
, sizeof(struct FilePrefHeader
)))
211 struct FilePrefHeader head
;
213 D(bug("SavePrefs: PushChunk(PRHD) okay.\n"));
215 head
.ph_Version
= 0; // FIXME: shouold be PHV_CURRENT, but see <prefs/prefhdr.h>
220 head
.ph_Flags
[3] = 0;
222 if (WriteChunkBytes(iff
, &head
, sizeof(head
)) == sizeof(head
))
224 D(bug("SavePrefs: WriteChunkBytes(PRHD) okay.\n"));
228 if (!PushChunk(iff
, ID_PREF
, ID_ICTL
, sizeof(saveprefs
)))
230 D(bug("SavePrefs: PushChunk(INPT) okay.\n"));
232 if (WriteChunkBytes(iff
, &saveprefs
, sizeof(saveprefs
)) == sizeof(saveprefs
))
234 D(bug("SavePrefs: WriteChunkBytes(ICTL) okay.\n"));
235 D(bug("SavePrefs: Everything okay :-)\n"));
242 } /* if (!PushChunk(iff, ID_PREF, ID_INPT, sizeof(saveprefs))) */
244 } /* if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head)) */
250 } /* if (!PushChunk(iff, ID_PREF, ID_PRHD, sizeof(struct PrefHeader))) */
254 } /* if (!PushChunk(iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN)) */
258 } /* if (!OpenIFF(iff, IFFFWRITE)) */
260 Close((BPTR
)iff
->iff_Stream
);
262 } /* if ((iff->iff_Stream = (IPTR)Open(filename, MODE_NEWFILE))) */
266 } /* if ((iff = AllocIFF())) */
268 if (!retval
&& delete_if_error
)
270 DeleteFile(filename
);
276 /*********************************************************************************************/
278 BOOL
DefaultPrefs(void)
280 icontrolprefs
.ic_Reserved
[0] = 0;
281 icontrolprefs
.ic_Reserved
[1] = 0;
282 icontrolprefs
.ic_Reserved
[2] = 0;
283 icontrolprefs
.ic_Reserved
[3] = 0;
284 icontrolprefs
.ic_TimeOut
= 50;
285 icontrolprefs
.ic_MetaDrag
= IEQUALIFIER_LCOMMAND
;
286 icontrolprefs
.ic_Flags
= ICF_3DMENUS
|
292 /* FIXME: check whether ICF_DEFPUBSCREEN is set as default */
293 icontrolprefs
.ic_WBtoFront
= 'N';
294 icontrolprefs
.ic_FrontToBack
= 'M';
295 icontrolprefs
.ic_ReqTrue
= 'V';
296 icontrolprefs
.ic_ReqFalse
= 'B';
302 /*********************************************************************************************/
304 void RestorePrefs(void)
306 icontrolprefs
= restore_prefs
;
309 /*********************************************************************************************/