2 Copyright © 2010, The AROS Development Team. All rights reserved.
9 /*********************************************************************************************/
15 #include <aros/macros.h>
18 #include <aros/debug.h>
20 #include <proto/exec.h>
21 #include <proto/iffparse.h>
22 #include <proto/dos.h>
24 #include <prefs/prefhdr.h>
29 /*********************************************************************************************/
31 #define PREFS_PATH_ENVARC "ENVARC:SYS/palette.prefs"
32 #define PREFS_PATH_ENV "ENV:SYS/palette.prefs"
34 /*********************************************************************************************/
36 STATIC UWORD defaultpen
[32] =
38 0, 1, 1, 2, 1, 3, 1, 0, 2, 1, 2, 1, 65535
41 STATIC
struct ColorSpec defaultcolor
[32] =
43 {0, 43690, 43690, 43690 },
45 {2, 65535, 65535, 65535 },
46 {3, 21845, 30583, 43690 },
47 {4, 26214, 26214, 26214 },
48 {5, 61166, 61166, 61166 },
49 {6, 56797, 30583, 17476 },
50 {7, 65535, 61166, 4369 },
54 /*********************************************************************************************/
62 /*********************************************************************************************/
64 struct PalettePrefs paletteprefs
;
66 /*********************************************************************************************/
68 static BOOL
Prefs_Load(STRPTR from
)
72 BPTR fh
= Open(from
, MODE_OLDFILE
);
75 retval
= Prefs_ImportFH(fh
);
82 /*********************************************************************************************/
84 BOOL
Prefs_ImportFH(BPTR fh
)
86 struct PalettePrefs loadprefs
;
87 struct IFFHandle
*iff
;
91 if ((iff
= AllocIFF()))
93 iff
->iff_Stream
= (IPTR
)fh
;
97 if (!OpenIFF(iff
, IFFF_READ
))
99 D(bug("Prefs_ImportFH: OpenIFF okay.\n"));
101 if (!StopChunk(iff
, ID_PREF
, ID_PALT
))
103 D(bug("Prefs_ImportFH: StopChunk okay.\n"));
105 if (!ParseIFF(iff
, IFFPARSE_SCAN
))
107 struct ContextNode
*cn
;
109 D(bug("Prefs_ImportFH: ParseIFF okay.\n"));
111 cn
= CurrentChunk(iff
);
113 if (cn
->cn_Size
== sizeof(struct PalettePrefs
))
115 D(bug("Prefs_ImportFH: ID_PALT chunk size okay.\n"));
117 if (ReadChunkBytes(iff
, &loadprefs
, sizeof(struct PalettePrefs
)) == sizeof(struct PalettePrefs
))
119 D(bug("Prefs_ImportFH: Reading chunk successful.\n"));
121 CopyMem(loadprefs
.pap_Reserved
, paletteprefs
.pap_Reserved
, sizeof(paletteprefs
.pap_Reserved
));
122 for (i
= 0; i
< 32; i
++)
124 paletteprefs
.pap_4ColorPens
[i
] = AROS_BE2WORD(loadprefs
.pap_4ColorPens
[i
]);
125 paletteprefs
.pap_8ColorPens
[i
] = AROS_BE2WORD(loadprefs
.pap_8ColorPens
[i
]);
126 paletteprefs
.pap_Colors
[i
].ColorIndex
= AROS_BE2WORD(loadprefs
.pap_Colors
[i
].ColorIndex
);
127 paletteprefs
.pap_Colors
[i
].Red
= AROS_BE2WORD(loadprefs
.pap_Colors
[i
].Red
);
128 paletteprefs
.pap_Colors
[i
].Green
= AROS_BE2WORD(loadprefs
.pap_Colors
[i
].Green
);
129 paletteprefs
.pap_Colors
[i
].Blue
= AROS_BE2WORD(loadprefs
.pap_Colors
[i
].Blue
);
132 D(bug("Prefs_ImportFH: Everything okay :-)\n"));
137 } /* if (!ParseIFF(iff, IFFPARSE_SCAN)) */
138 } /* if (!StopChunk(iff, ID_PREF, ID_SERL)) */
140 } /* if (!OpenIFF(iff, IFFF_READ)) */
142 } /* if ((iff = AllocIFF())) */
147 /*********************************************************************************************/
149 BOOL
Prefs_ExportFH(BPTR fh
)
151 struct PalettePrefs saveprefs
;
152 struct IFFHandle
*iff
;
154 BOOL delete_if_error
= FALSE
;
157 CopyMem(paletteprefs
.pap_Reserved
, saveprefs
.pap_Reserved
, sizeof(paletteprefs
.pap_Reserved
));
158 for (i
= 0; i
< 32; i
++)
160 saveprefs
.pap_4ColorPens
[i
] = AROS_WORD2BE(paletteprefs
.pap_4ColorPens
[i
]);
161 saveprefs
.pap_8ColorPens
[i
] = AROS_WORD2BE(paletteprefs
.pap_8ColorPens
[i
]);
162 saveprefs
.pap_Colors
[i
].ColorIndex
= AROS_WORD2BE(paletteprefs
.pap_Colors
[i
].ColorIndex
);
163 saveprefs
.pap_Colors
[i
].Red
= AROS_WORD2BE(paletteprefs
.pap_Colors
[i
].Red
);
164 saveprefs
.pap_Colors
[i
].Green
= AROS_WORD2BE(paletteprefs
.pap_Colors
[i
].Green
);
165 saveprefs
.pap_Colors
[i
].Blue
= AROS_WORD2BE(paletteprefs
.pap_Colors
[i
].Blue
);
168 D(bug("Prefs_ExportFH: fh: %lx\n", fh
));
170 if ((iff
= AllocIFF()))
172 iff
->iff_Stream
= (IPTR
) fh
;
173 D(bug("Prefs_ExportFH: stream opened.\n"));
175 delete_if_error
= TRUE
;
179 if (!OpenIFF(iff
, IFFF_WRITE
))
181 D(bug("Prefs_ExportFH: OpenIFF okay.\n"));
183 if (!PushChunk(iff
, ID_PREF
, ID_FORM
, IFFSIZE_UNKNOWN
))
185 D(bug("Prefs_ExportFH: PushChunk(FORM) okay.\n"));
187 if (!PushChunk(iff
, ID_PREF
, ID_PRHD
, sizeof(struct FilePrefHeader
)))
189 struct FilePrefHeader head
;
191 D(bug("Prefs_ExportFH: PushChunk(PRHD) okay.\n"));
193 head
.ph_Version
= PHV_CURRENT
;
198 head
.ph_Flags
[3] = 0;
200 if (WriteChunkBytes(iff
, &head
, sizeof(head
)) == sizeof(head
))
202 D(bug("Prefs_ExportFH: WriteChunkBytes(PRHD) okay.\n"));
206 if (!PushChunk(iff
, ID_PREF
, ID_PALT
, sizeof(struct PalettePrefs
)))
208 D(bug("Prefs_ExportFH: PushChunk(LCLE) okay.\n"));
210 if (WriteChunkBytes(iff
, &saveprefs
, sizeof(saveprefs
)) == sizeof(saveprefs
))
212 D(bug("Prefs_ExportFH: WriteChunkBytes(PALT) okay.\n"));
213 D(bug("Prefs_ExportFH: Everything okay :-)\n"));
218 } /* if (!PushChunk(iff, ID_PREF, ID_SERL, sizeof(struct LocalePrefs))) */
219 } /* if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head)) */
224 } /* if (!PushChunk(iff, ID_PREF, ID_PRHD, sizeof(struct PrefHeader))) */
226 } /* if (!PushChunk(iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN)) */
228 } /* if (!OpenIFF(iff, IFFFWRITE)) */
231 } /* if ((iff = AllocIFF())) */
234 if (!retval
&& delete_if_error
)
236 DeleteFile(filename
);
243 /*********************************************************************************************/
245 BOOL
Prefs_HandleArgs(STRPTR from
, BOOL use
, BOOL save
)
251 if (!Prefs_Load(from
))
253 ShowMessage("Can't read from input file");
259 if (!Prefs_Load(PREFS_PATH_ENV
))
261 if (!Prefs_Load(PREFS_PATH_ENVARC
))
265 "Can't read from file " PREFS_PATH_ENVARC
266 ".\nUsing default values."
275 fh
= Open(PREFS_PATH_ENV
, MODE_NEWFILE
);
283 ShowMessage("Cant' open " PREFS_PATH_ENV
" for writing.");
288 fh
= Open(PREFS_PATH_ENVARC
, MODE_NEWFILE
);
296 ShowMessage("Cant' open " PREFS_PATH_ENVARC
" for writing.");
302 /*********************************************************************************************/
304 BOOL
Prefs_Default(VOID
)
306 paletteprefs
.pap_Reserved
[0] = 0;
307 paletteprefs
.pap_Reserved
[1] = 0;
308 paletteprefs
.pap_Reserved
[2] = 0;
309 paletteprefs
.pap_Reserved
[3] = 0;
311 CopyMem(defaultpen
, paletteprefs
.pap_4ColorPens
, sizeof paletteprefs
.pap_4ColorPens
);
312 CopyMem(defaultpen
, paletteprefs
.pap_8ColorPens
, sizeof paletteprefs
.pap_8ColorPens
);
313 CopyMem(defaultcolor
, paletteprefs
.pap_Colors
, sizeof paletteprefs
.pap_Colors
);