transfer scheduler for CTRL and INT. Rest to be added soon
[AROS.git] / workbench / prefs / pointer / prefs.c
blob542613382d38f335e58c693a881677ccc200e10f
1 /*
2 Copyright © 2010, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 /*********************************************************************************************/
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
12 #include <aros/macros.h>
14 // #define DEBUG 1
15 #include <aros/debug.h>
17 #include <proto/exec.h>
18 #include <proto/iffparse.h>
19 #include <proto/dos.h>
21 #include <prefs/prefhdr.h>
23 #include "prefs.h"
24 #include "misc.h"
26 /*********************************************************************************************/
28 #define PREFS_PATH_ENVARC "ENVARC:SYS/pointer.prefs"
29 #define PREFS_PATH_ENV "ENV:SYS/pointer.prefs"
31 /*********************************************************************************************/
33 struct FilePrefHeader
35 UBYTE ph_Version;
36 UBYTE ph_Type;
37 UBYTE ph_Flags[4];
40 /*********************************************************************************************/
42 struct ExtPointerPrefs pointerprefs[MAXPOINTER];
44 /*********************************************************************************************/
46 static BOOL Prefs_Load(STRPTR from)
48 BOOL retval = FALSE;
50 BPTR fh = Open(from, MODE_OLDFILE);
51 if (fh)
53 retval = Prefs_ImportFH(fh);
54 Close(fh);
57 return retval;
60 /*********************************************************************************************/
62 BOOL Prefs_ImportFH(BPTR fh)
64 struct ExtPointerPrefs loadprefs;
65 struct IFFHandle *iff;
66 BOOL retval = FALSE;
68 if ((iff = AllocIFF()))
70 iff->iff_Stream = (IPTR)fh;
72 InitIFFasDOS(iff);
74 if (!OpenIFF(iff, IFFF_READ))
76 D(bug("Prefs_ImportFH: OpenIFF okay.\n"));
78 if (!StopChunk(iff, ID_PREF, ID_NPTR))
80 D(bug("Prefs_ImportFH: StopChunk okay.\n"));
82 while (!ParseIFF(iff, IFFPARSE_SCAN))
84 struct ContextNode *cn;
86 D(bug("Prefs_ImportFH: ParseIFF okay.\n"));
88 memset(&loadprefs, 0, sizeof(loadprefs));
89 cn = CurrentChunk(iff);
91 if (cn->cn_Size >= sizeof(struct NewPointerPrefs))
93 D(bug("Prefs_ImportFH: ID_NPTR chunk size okay.\n"));
95 if (ReadChunkBytes(iff, &loadprefs, sizeof(struct ExtPointerPrefs)) >= sizeof(struct NewPointerPrefs))
97 D(bug("Prefs_ImportFH: Reading chunk successful.\n"));
99 UWORD which = AROS_BE2WORD(loadprefs.npp.npp_Which);
100 if (which < MAXPOINTER)
102 pointerprefs[which].npp.npp_Which = which;
103 pointerprefs[which].npp.npp_AlphaValue = AROS_BE2WORD(loadprefs.npp.npp_AlphaValue);
104 pointerprefs[which].npp.npp_WhichInFile = AROS_BE2LONG(loadprefs.npp.npp_WhichInFile);
105 pointerprefs[which].npp.npp_X = AROS_BE2WORD(loadprefs.npp.npp_X);
106 pointerprefs[which].npp.npp_Y = AROS_BE2WORD(loadprefs.npp.npp_Y);
107 strlcpy(pointerprefs[which].filename, loadprefs.filename, NAMEBUFLEN);
109 D(bug("Prefs_ImportFH: which %d name %s\n", which, pointerprefs[which].filename));
112 D(bug("Prefs_ImportFH: Everything okay :-)\n"));
114 retval = TRUE;
117 } /* if (!ParseIFF(iff, IFFPARSE_SCAN)) */
118 } /* if (!StopChunk(iff, ID_PREF, ID_SERL)) */
119 CloseIFF(iff);
120 } /* if (!OpenIFF(iff, IFFF_READ)) */
121 FreeIFF(iff);
122 } /* if ((iff = AllocIFF())) */
124 return retval;
127 /*********************************************************************************************/
129 BOOL Prefs_ExportFH(BPTR fh)
131 struct ExtPointerPrefs saveprefs;
132 struct IFFHandle *iff;
133 BOOL retval = FALSE;
134 #if 0 /* unused */
135 BOOL delete_if_error = FALSE;
136 #endif
137 LONG i;
139 D(bug("Prefs_ExportFH: fh: %lx\n", fh));
141 if ((iff = AllocIFF()))
143 iff->iff_Stream = (IPTR) fh;
144 D(bug("Prefs_ExportFH: stream opened.\n"));
146 #if 0 /* unused */
147 delete_if_error = TRUE;
148 #endif
150 InitIFFasDOS(iff);
152 if (!OpenIFF(iff, IFFF_WRITE))
154 D(bug("Prefs_ExportFH: OpenIFF okay.\n"));
156 if (!PushChunk(iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN))
158 D(bug("Prefs_ExportFH: PushChunk(FORM) okay.\n"));
160 if (!PushChunk(iff, ID_PREF, ID_PRHD, sizeof(struct FilePrefHeader)))
162 struct FilePrefHeader head;
164 D(bug("Prefs_ExportFH: PushChunk(PRHD) okay.\n"));
166 head.ph_Version = PHV_CURRENT;
167 head.ph_Type = 0;
168 head.ph_Flags[0] =
169 head.ph_Flags[1] =
170 head.ph_Flags[2] =
171 head.ph_Flags[3] = 0;
173 if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head))
175 D(bug("Prefs_ExportFH: WriteChunkBytes(PRHD) okay.\n"));
177 PopChunk(iff);
179 for (i = 0; i < MAXPOINTER; i++)
181 saveprefs.npp.npp_Which = AROS_WORD2BE(i);
182 saveprefs.npp.npp_AlphaValue = AROS_WORD2BE(pointerprefs[i].npp.npp_AlphaValue);
183 saveprefs.npp.npp_WhichInFile = AROS_LONG2BE(pointerprefs[i].npp.npp_WhichInFile);
184 saveprefs.npp.npp_X = AROS_WORD2BE(pointerprefs[i].npp.npp_X);
185 saveprefs.npp.npp_Y = AROS_WORD2BE(pointerprefs[i].npp.npp_Y);
186 strlcpy(saveprefs.filename, pointerprefs[i].filename, NAMEBUFLEN);
188 ULONG chunksize = sizeof(struct NewPointerPrefs) + strlen(saveprefs.filename) + 1;
189 D(bug("Prefs_ExportFH: size %d name %s\n", chunksize, saveprefs.filename));
191 if (!PushChunk(iff, ID_PREF, ID_NPTR, chunksize))
193 D(bug("Prefs_ExportFH: PushChunk(NPTR) okay.\n"));
195 if (WriteChunkBytes(iff, &saveprefs, chunksize) == chunksize)
197 D(bug("Prefs_ExportFH: WriteChunkBytes(NPTR) okay.\n"));
198 D(bug("Prefs_ExportFH: Everything okay :-)\n"));
200 retval = TRUE;
202 PopChunk(iff);
203 } /* if (!PushChunk(iff, ID_PREF, ID_SERL, sizeof(struct LocalePrefs))) */
204 } /* for */
205 } /* if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head)) */
206 else
208 PopChunk(iff);
210 } /* if (!PushChunk(iff, ID_PREF, ID_PRHD, sizeof(struct PrefHeader))) */
211 PopChunk(iff);
212 } /* if (!PushChunk(iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN)) */
213 CloseIFF(iff);
214 } /* if (!OpenIFF(iff, IFFFWRITE)) */
215 FreeIFF(iff);
217 } /* if ((iff = AllocIFF())) */
219 #if 0 /* unused */
220 if (!retval && delete_if_error)
222 DeleteFile(filename);
224 #endif
226 return retval;
229 /*********************************************************************************************/
231 BOOL Prefs_HandleArgs(STRPTR from, BOOL use, BOOL save)
233 BPTR fh;
235 if (from)
237 if (!Prefs_Load(from))
239 ShowMessage("Can't read from input file");
240 return FALSE;
243 else
245 if (!Prefs_Load(PREFS_PATH_ENV))
247 if (!Prefs_Load(PREFS_PATH_ENVARC))
249 ShowMessage
251 "Can't read from file " PREFS_PATH_ENVARC
252 ".\nUsing default values."
254 Prefs_Default();
259 if (use || save)
261 fh = Open(PREFS_PATH_ENV, MODE_NEWFILE);
262 if (fh)
264 Prefs_ExportFH(fh);
265 Close(fh);
267 else
269 ShowMessage("Can't open " PREFS_PATH_ENV " for writing.");
272 if (save)
274 fh = Open(PREFS_PATH_ENVARC, MODE_NEWFILE);
275 if (fh)
277 Prefs_ExportFH(fh);
278 Close(fh);
280 else
282 ShowMessage("Can't open " PREFS_PATH_ENVARC " for writing.");
285 return TRUE;
288 /*********************************************************************************************/
290 BOOL Prefs_Default(VOID)
292 ULONG i;
294 memset(pointerprefs, 0, sizeof(pointerprefs));
295 for (i = 0; i < MAXPOINTER; i++)
297 pointerprefs[i].npp.npp_Which = i;
298 pointerprefs[i].npp.npp_AlphaValue = 0xffff;
299 strcpy(pointerprefs[i].filename, "Images:Pointers/");
302 return TRUE;