2 Copyright © 2010-2018, The AROS Development Team. All rights reserved.
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>
28 //#define DELETEONERROR
30 /*********************************************************************************************/
32 #define PREFS_PATH_ENVARC "ENVARC:SYS/palette.prefs"
33 #define PREFS_PATH_ENV "ENV:SYS/palette.prefs"
35 /*********************************************************************************************/
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 },
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 },
57 /*********************************************************************************************/
65 /*********************************************************************************************/
67 struct PalettePrefs paletteprefs
;
69 /*********************************************************************************************/
71 static BOOL
Prefs_Load(STRPTR from
)
75 BPTR fh
= Open(from
, MODE_OLDFILE
);
78 retval
= Prefs_ImportFH(fh
);
85 /*********************************************************************************************/
87 BOOL
Prefs_ImportFH(BPTR fh
)
89 struct IFFHandle
*iff
;
93 if ((iff
= AllocIFF()))
95 iff
->iff_Stream
= (IPTR
)fh
;
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
);
138 CopyMem(&loadprefs
, &paletteprefs
, sizeof(paletteprefs
));
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"));
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
))
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
);
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
;
174 } /* if (!OpenIFF(iff, IFFF_READ)) */
176 } /* if ((iff = AllocIFF())) */
181 /*********************************************************************************************/
183 BOOL
Prefs_ExportFH(BPTR fh
)
185 struct PalettePrefs saveprefs
;
186 struct IFFHandle
*iff
;
188 #if defined(DELETEONERROR)
189 BOOL delete_if_error
= FALSE
;
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
]);
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
);
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);
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
;
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
;
248 head
.ph_Flags
[3] = 0;
250 if (WriteChunkBytes(iff
, &head
, sizeof(head
)) == sizeof(head
))
252 D(bug("Prefs_ExportFH: WriteChunkBytes(PRHD) okay.\n"));
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"));
268 } /* if (!PushChunk(iff, ID_PREF, ID_SERL, sizeof(struct LocalePrefs))) */
269 } /* if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head)) */
274 } /* if (!PushChunk(iff, ID_PREF, ID_PRHD, sizeof(struct PrefHeader))) */
276 } /* if (!PushChunk(iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN)) */
278 } /* if (!OpenIFF(iff, IFFFWRITE)) */
281 } /* if ((iff = AllocIFF())) */
283 #if defined(DELETEONERROR)
284 if (!retval
&& delete_if_error
)
286 DeleteFile(filename
);
293 /*********************************************************************************************/
295 BOOL
Prefs_HandleArgs(STRPTR from
, BOOL use
, BOOL save
)
301 if (!Prefs_Load(from
))
303 ShowMessage("Can't read from input file");
309 if (!Prefs_Load(PREFS_PATH_ENV
))
311 if (!Prefs_Load(PREFS_PATH_ENVARC
))
315 "Can't read from file " PREFS_PATH_ENVARC
316 ".\nUsing default values."
325 fh
= Open(PREFS_PATH_ENV
, MODE_NEWFILE
);
333 ShowMessage("Cant' open " PREFS_PATH_ENV
" for writing.");
338 fh
= Open(PREFS_PATH_ENVARC
, MODE_NEWFILE
);
346 ShowMessage("Cant' open " PREFS_PATH_ENVARC
" for writing.");
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
);