added concrete implementations of putc(), getc(), getchar() and gets()
[tangerine.git] / workbench / prefs / icontrol / prefs.c
blob24b06151e98876055e49a54de4122a03e53ec68c
1 /*
2 Copyright © 1995-2006, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: English
7 */
9 /*********************************************************************************************/
11 #include "global.h"
12 #include <aros/macros.h>
14 #define DEBUG 0
15 #include <aros/debug.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.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 /*********************************************************************************************/
36 struct FilePrefHeader
38 UBYTE ph_Version;
39 UBYTE ph_Type;
40 UBYTE ph_Flags[4];
43 struct FileIControlPrefs
45 UBYTE ic_Reserved0[4];
46 UBYTE ic_Reserved1[4];
47 UBYTE ic_Reserved2[4];
48 UBYTE ic_Reserved3[4];
49 UBYTE ic_TimeOut[2];
50 UBYTE ic_MetaDrag[2];
51 UBYTE ic_Flags[4];
52 UBYTE ic_WBtoFront;
53 UBYTE ic_FrontToBack;
54 UBYTE ic_ReqTrue;
55 UBYTE ic_ReqFalse;
58 /*********************************************************************************************/
60 void InitPrefs(STRPTR filename, BOOL use, BOOL save)
62 if (!LoadPrefs(filename))
64 if (!DefaultPrefs())
69 restore_prefs = icontrolprefs;
71 if (use || save)
73 SavePrefs(CONFIGNAME_ENV);
76 if (save)
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;
96 BOOL retval = FALSE;
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"));
106 InitIFFasDOS(iff);
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"));
146 retval = TRUE;
150 } /* if (!ParseIFF(iff, IFFPARSE_SCAN)) */
152 } /* if (!StopChunk(iff, ID_PREF, ID_INPT)) */
154 CloseIFF(iff);
156 } /* if (!OpenIFF(iff, IFFF_READ)) */
158 Close((BPTR)iff->iff_Stream);
160 } /* if ((iff->iff_Stream = (IPTR)Open(filename, MODE_OLDFILE))) */
162 FreeIFF(iff);
164 } /* if ((iff = AllocIFF())) */
166 return retval;
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;
199 InitIFFasDOS(iff);
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>
216 head.ph_Type = 0;
217 head.ph_Flags[0] =
218 head.ph_Flags[1] =
219 head.ph_Flags[2] =
220 head.ph_Flags[3] = 0;
222 if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head))
224 D(bug("SavePrefs: WriteChunkBytes(PRHD) okay.\n"));
226 PopChunk(iff);
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"));
237 retval = TRUE;
240 PopChunk(iff);
242 } /* if (!PushChunk(iff, ID_PREF, ID_INPT, sizeof(saveprefs))) */
244 } /* if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head)) */
245 else
247 PopChunk(iff);
250 } /* if (!PushChunk(iff, ID_PREF, ID_PRHD, sizeof(struct PrefHeader))) */
252 PopChunk(iff);
254 } /* if (!PushChunk(iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN)) */
256 CloseIFF(iff);
258 } /* if (!OpenIFF(iff, IFFFWRITE)) */
260 Close((BPTR)iff->iff_Stream);
262 } /* if ((iff->iff_Stream = (IPTR)Open(filename, MODE_NEWFILE))) */
264 FreeIFF(iff);
266 } /* if ((iff = AllocIFF())) */
268 if (!retval && delete_if_error)
270 DeleteFile(filename);
273 return retval;
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 |
287 ICF_MODEPROMOTE |
288 ICF_MENUSNAP |
289 ICF_STRGAD_FILTER |
290 ICF_COERCE_LACE |
291 ICF_OFFSCREENLAYERS;
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';
298 return TRUE;
302 /*********************************************************************************************/
304 void RestorePrefs(void)
306 icontrolprefs = restore_prefs;
309 /*********************************************************************************************/