revert between 56095 -> 55830 in arch
[AROS.git] / workbench / prefs / palette / prefs.c
blob8360b3776f9fb734b36a245aac69b5c5ddefebdf
1 /*
2 Copyright © 2010-2018, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: English
7 */
9 /*********************************************************************************************/
11 #include <aros/debug.h>
13 #include <proto/exec.h>
14 #include <proto/iffparse.h>
15 #include <proto/dos.h>
17 #include <aros/macros.h>
18 #include <prefs/prefhdr.h>
19 #include <datatypes/pictureclass.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
25 #include "prefs.h"
26 #include "misc.h"
28 //#define DELETEONERROR
30 /*********************************************************************************************/
32 #define PREFS_PATH_ENVARC "ENVARC:SYS/palette.prefs"
33 #define PREFS_PATH_ENV "ENV:SYS/palette.prefs"
35 /*********************************************************************************************/
37 #define MAXPENS 8
39 STATIC UWORD defaultpen[MAXPENS * 4] =
41 0, 1, 1, 2, 1, 3, 1, 0, 2, 1, 2, 1, 65535
44 STATIC struct ColorSpec defaultcolor[MAXPENS * 4] =
46 {0, 43690, 43690, 43690 },
47 {1, 0, 0, 0 },
48 {2, 65535, 65535, 65535 },
49 {3, 21845, 30583, 43690 },
50 {4, 26214, 26214, 26214 },
51 {5, 61166, 61166, 61166 },
52 {6, 56797, 30583, 17476 },
53 {7, 65535, 61166, 4369 },
54 {65535 }
57 /*********************************************************************************************/
58 struct FilePrefHeader
60 UBYTE ph_Version;
61 UBYTE ph_Type;
62 UBYTE ph_Flags[4];
65 /*********************************************************************************************/
67 struct PalettePrefs paletteprefs;
69 /*********************************************************************************************/
71 static BOOL Prefs_Load(STRPTR from)
73 BOOL retval = FALSE;
75 BPTR fh = Open(from, MODE_OLDFILE);
76 if (fh)
78 retval = Prefs_ImportFH(fh);
79 Close(fh);
82 return retval;
85 /*********************************************************************************************/
87 BOOL Prefs_ImportFH(BPTR fh)
89 struct IFFHandle *iff;
90 BOOL retval = FALSE;
91 LONG i;
93 if ((iff = AllocIFF()))
95 iff->iff_Stream = (IPTR)fh;
97 InitIFFasDOS(iff);
99 if (!OpenIFF(iff, IFFF_READ))
101 D(bug("Prefs_ImportFH: OpenIFF okay.\n"));
103 if (!StopChunk(iff, ID_PREF, ID_PALT))
105 struct PalettePrefs loadprefs;
106 D(bug("Prefs_ImportFH: ID_PREF->ID_PALT\n"));
108 if (!ParseIFF(iff, IFFPARSE_SCAN))
110 struct ContextNode *cn;
112 D(bug("Prefs_ImportFH: ParseIFF okay.\n"));
114 cn = CurrentChunk(iff);
116 if (cn->cn_Size == sizeof(struct PalettePrefs))
118 D(bug("Prefs_ImportFH: ID_PALT chunk size okay.\n"));
120 if (ReadChunkBytes(iff, &loadprefs, sizeof(struct PalettePrefs)) == sizeof(struct PalettePrefs))
122 D(bug("Prefs_ImportFH: Reading chunk successful.\n"));
124 #if (AROS_BIG_ENDIAN == 0)
125 CopyMem(loadprefs.pap_Reserved, paletteprefs.pap_Reserved, sizeof(paletteprefs.pap_Reserved));
126 for (i = 0; i < 32; i++)
128 paletteprefs.pap_4ColorPens[i] = AROS_BE2WORD(loadprefs.pap_4ColorPens[i]);
130 paletteprefs.pap_8ColorPens[i] = AROS_BE2WORD(loadprefs.pap_8ColorPens[i]);
132 paletteprefs.pap_Colors[i].ColorIndex = AROS_BE2WORD(loadprefs.pap_Colors[i].ColorIndex);
133 paletteprefs.pap_Colors[i].Red = AROS_BE2WORD(loadprefs.pap_Colors[i].Red);
134 paletteprefs.pap_Colors[i].Green = AROS_BE2WORD(loadprefs.pap_Colors[i].Green);
135 paletteprefs.pap_Colors[i].Blue = AROS_BE2WORD(loadprefs.pap_Colors[i].Blue);
137 #else
138 CopyMem(&loadprefs, &paletteprefs, sizeof(paletteprefs));
139 #endif
140 for (i = 0; i < MAXPENS; i++)
142 if (paletteprefs.pap_8ColorPens[i] >= PEN_C3)
143 paletteprefs.pap_8ColorPens[i] -= (PEN_C3 - 4);
145 D(bug("Prefs_ImportFH: Everything okay :-)\n"));
147 retval = TRUE;
150 } /* if (!ParseIFF(iff, IFFPARSE_SCAN)) */
152 else if (!StopChunk(iff, ID_ILBM, ID_CMAP))
154 struct ColorRegister colreg;
155 D(bug("Prefs_ImportFH: ID_ILBM->ID_CMAP\n"));
157 for (i = 0; i < MAXPENS; i++)
159 if (ReadChunkBytes(iff, &colreg, sizeof(struct ColorRegister)) != sizeof(struct ColorRegister))
160 break;
161 #if (AROS_BIG_ENDIAN == 0)
162 paletteprefs.pap_Colors[i].Red = AROS_BE2WORD(colreg.red);
163 paletteprefs.pap_Colors[i].Green = AROS_BE2WORD(colreg.green);
164 paletteprefs.pap_Colors[i].Blue = AROS_BE2WORD(colreg.blue);
165 #else
166 paletteprefs.pap_Colors[i].Red = (colreg.red << 24) | (colreg.red << 16) | (colreg.red << 8) | colreg.red;
167 paletteprefs.pap_Colors[i].Green = (colreg.green << 24) | (colreg.green << 16) | (colreg.green << 8) | colreg.green;
168 paletteprefs.pap_Colors[i].Blue = (colreg.blue << 24) | (colreg.blue << 16) | (colreg.blue << 8) | colreg.blue;
169 #endif
173 CloseIFF(iff);
174 } /* if (!OpenIFF(iff, IFFF_READ)) */
175 FreeIFF(iff);
176 } /* if ((iff = AllocIFF())) */
178 return retval;
181 /*********************************************************************************************/
183 BOOL Prefs_ExportFH(BPTR fh)
185 struct PalettePrefs saveprefs;
186 struct IFFHandle *iff;
187 BOOL retval = FALSE;
188 #if defined(DELETEONERROR)
189 BOOL delete_if_error = FALSE;
190 #endif
191 LONG i;
193 #if (AROS_BIG_ENDIAN == 0)
194 CopyMem(paletteprefs.pap_Reserved, saveprefs.pap_Reserved, sizeof(paletteprefs.pap_Reserved));
195 for (i = 0; i < 32; i++)
197 saveprefs.pap_4ColorPens[i] = AROS_WORD2BE(paletteprefs.pap_4ColorPens[i]);
198 if ((i >= MAXPENS) || (paletteprefs.pap_8ColorPens[i] < 4))
199 saveprefs.pap_8ColorPens[i] = AROS_WORD2BE(paletteprefs.pap_8ColorPens[i]);
200 else
201 saveprefs.pap_8ColorPens[i] = AROS_WORD2BE(paletteprefs.pap_8ColorPens[i] + (PEN_C3 - 4));
202 saveprefs.pap_Colors[i].ColorIndex = AROS_WORD2BE(paletteprefs.pap_Colors[i].ColorIndex);
203 saveprefs.pap_Colors[i].Red = AROS_WORD2BE(paletteprefs.pap_Colors[i].Red);
204 saveprefs.pap_Colors[i].Green = AROS_WORD2BE(paletteprefs.pap_Colors[i].Green);
205 saveprefs.pap_Colors[i].Blue = AROS_WORD2BE(paletteprefs.pap_Colors[i].Blue);
207 #else
208 CopyMem(&paletteprefs, &saveprefs, sizeof(paletteprefs));
209 for (i = 0; i < MAXPENS; i++)
211 if (saveprefs.pap_8ColorPens[i] >= 4)
212 saveprefs.pap_8ColorPens[i] += (PEN_C3 - 4);
214 #endif
216 D(bug("Prefs_ExportFH: fh: %lx\n", fh));
218 if ((iff = AllocIFF()))
220 iff->iff_Stream = (IPTR) fh;
221 D(bug("Prefs_ExportFH: stream opened.\n"));
223 #if defined(DELETEONERROR)
224 delete_if_error = TRUE;
225 #endif
227 InitIFFasDOS(iff);
229 if (!OpenIFF(iff, IFFF_WRITE))
231 D(bug("Prefs_ExportFH: OpenIFF okay.\n"));
233 if (!PushChunk(iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN))
235 D(bug("Prefs_ExportFH: PushChunk(FORM) okay.\n"));
237 if (!PushChunk(iff, ID_PREF, ID_PRHD, sizeof(struct FilePrefHeader)))
239 struct FilePrefHeader head;
241 D(bug("Prefs_ExportFH: PushChunk(PRHD) okay.\n"));
243 head.ph_Version = PHV_CURRENT;
244 head.ph_Type = 0;
245 head.ph_Flags[0] =
246 head.ph_Flags[1] =
247 head.ph_Flags[2] =
248 head.ph_Flags[3] = 0;
250 if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head))
252 D(bug("Prefs_ExportFH: WriteChunkBytes(PRHD) okay.\n"));
254 PopChunk(iff);
256 if (!PushChunk(iff, ID_PREF, ID_PALT, sizeof(struct PalettePrefs)))
258 D(bug("Prefs_ExportFH: PushChunk(LCLE) okay.\n"));
260 if (WriteChunkBytes(iff, &saveprefs, sizeof(saveprefs)) == sizeof(saveprefs))
262 D(bug("Prefs_ExportFH: WriteChunkBytes(PALT) okay.\n"));
263 D(bug("Prefs_ExportFH: Everything okay :-)\n"));
265 retval = TRUE;
267 PopChunk(iff);
268 } /* if (!PushChunk(iff, ID_PREF, ID_SERL, sizeof(struct LocalePrefs))) */
269 } /* if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head)) */
270 else
272 PopChunk(iff);
274 } /* if (!PushChunk(iff, ID_PREF, ID_PRHD, sizeof(struct PrefHeader))) */
275 PopChunk(iff);
276 } /* if (!PushChunk(iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN)) */
277 CloseIFF(iff);
278 } /* if (!OpenIFF(iff, IFFFWRITE)) */
279 FreeIFF(iff);
281 } /* if ((iff = AllocIFF())) */
283 #if defined(DELETEONERROR)
284 if (!retval && delete_if_error)
286 DeleteFile(filename);
288 #endif
290 return retval;
293 /*********************************************************************************************/
295 BOOL Prefs_HandleArgs(STRPTR from, BOOL use, BOOL save)
297 BPTR fh;
299 if (from)
301 if (!Prefs_Load(from))
303 ShowMessage("Can't read from input file");
304 return FALSE;
307 else
309 if (!Prefs_Load(PREFS_PATH_ENV))
311 if (!Prefs_Load(PREFS_PATH_ENVARC))
313 ShowMessage
315 "Can't read from file " PREFS_PATH_ENVARC
316 ".\nUsing default values."
318 Prefs_Default();
323 if (use || save)
325 fh = Open(PREFS_PATH_ENV, MODE_NEWFILE);
326 if (fh)
328 Prefs_ExportFH(fh);
329 Close(fh);
331 else
333 ShowMessage("Cant' open " PREFS_PATH_ENV " for writing.");
336 if (save)
338 fh = Open(PREFS_PATH_ENVARC, MODE_NEWFILE);
339 if (fh)
341 Prefs_ExportFH(fh);
342 Close(fh);
344 else
346 ShowMessage("Cant' open " PREFS_PATH_ENVARC " for writing.");
349 return TRUE;
352 /*********************************************************************************************/
354 BOOL Prefs_Default(VOID)
356 paletteprefs.pap_Reserved[0] = 0;
357 paletteprefs.pap_Reserved[1] = 0;
358 paletteprefs.pap_Reserved[2] = 0;
359 paletteprefs.pap_Reserved[3] = 0;
361 CopyMem(defaultpen, paletteprefs.pap_4ColorPens, sizeof paletteprefs.pap_4ColorPens);
362 CopyMem(defaultpen, paletteprefs.pap_8ColorPens, sizeof paletteprefs.pap_8ColorPens);
363 CopyMem(defaultcolor, paletteprefs.pap_Colors, sizeof paletteprefs.pap_Colors);
365 return TRUE;