revert between 56095 -> 55830 in arch
[AROS.git] / workbench / prefs / printer / prefs.c
blob1154167d736d3c01c4ad4993c7c7e866497ecc64
1 /*
2 Copyright © 1995-2010, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: English
7 */
9 /*********************************************************************************************/
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
15 #include <aros/macros.h>
17 #include <aros/debug.h>
19 #include <proto/exec.h>
20 #include <proto/iffparse.h>
21 #include <proto/dos.h>
23 #include <prefs/prefhdr.h>
25 #include "prefs.h"
26 #include "misc.h"
28 #ifdef BIGENDIAN_PREFS
29 #define GET_WORD AROS_BE2WORD
30 #define GET_LONG AROS_BE2LONG
31 #else
32 #define GET_WORD(x) x
33 #define GET_LONG(x) x
34 #endif
36 /*********************************************************************************************/
38 #define PREFS_PATH_ENVARC "ENVARC:SYS/printer.prefs"
39 #define PREFS_PATH_ENV "ENV:SYS/printer.prefs"
41 /*********************************************************************************************/
43 struct FilePrefHeader
45 UBYTE ph_Version;
46 UBYTE ph_Type;
47 UBYTE ph_Flags[4];
50 /*********************************************************************************************/
52 struct PrinterPrefs printerprefs;
54 /*********************************************************************************************/
56 /* DEFAULTs */
58 /*********************************************************************************************/
60 static BOOL Prefs_Load(STRPTR from)
62 BOOL retval = FALSE;
64 BPTR fh = Open(from, MODE_OLDFILE);
65 if (fh)
67 retval = Prefs_ImportFH(fh);
68 Close(fh);
71 return retval;
74 /*********************************************************************************************/
76 #define IMPORT_WORD(x) do { x = AROS_BE2WORD(x); } while (0)
77 #define IMPORT_LONG(x) do { x = AROS_BE2LONG(x); } while (0)
79 BOOL Prefs_ImportFH(BPTR fh)
81 struct PrinterTxtPrefs txt = printerprefs.pp_Txt;
82 struct PrinterUnitPrefs unit = printerprefs.pp_Unit;
83 struct PrinterDeviceUnitPrefs devunit = printerprefs.pp_DeviceUnit;
84 struct PrinterGfxPrefs gfx = printerprefs.pp_Gfx;
86 struct IFFHandle *iff;
87 LONG chunk_map = 0;
88 LONG stop_chunks[] = {
89 ID_PREF, ID_PTXT,
90 ID_PREF, ID_PUNT,
91 ID_PREF, ID_PDEV,
92 ID_PREF, ID_PGFX,
96 D(bug("LoadPrefs: Begin\n"));
98 if ((iff = AllocIFF()))
100 iff->iff_Stream = (IPTR)fh;
102 InitIFFasDOS(iff);
104 if (!OpenIFF(iff, IFFF_READ))
106 D(bug("LoadPrefs: OpenIFF okay.\n"));
108 if (!StopChunks(iff, stop_chunks, 4))
110 D(bug("LoadPrefs: StopChunks okay.\n"));
112 while (ParseIFF(iff, IFFPARSE_SCAN) == 0)
114 struct ContextNode *cn;
116 cn = CurrentChunk(iff);
118 D(bug("LoadPrefs: ParseIFF okay: 0x%04x 0x%04x\n", cn->cn_ID, cn->cn_Type));
120 if (cn->cn_ID == ID_PTXT && cn->cn_Size == sizeof(txt))
122 D(bug("LoadPrefs: ID_PTXT chunk size okay.\n"));
123 if (ReadChunkBytes(iff, &txt, sizeof(txt)) == sizeof(txt))
125 D(bug("LoadPrefs: Reading chunk successful.\n"));
127 chunk_map |= (1 << 0);
130 if (cn->cn_ID == ID_PUNT && cn->cn_Size == sizeof(unit))
132 D(bug("LoadPrefs: ID_PUNT chunk size okay.\n"));
133 if (ReadChunkBytes(iff, &unit, sizeof(unit)) == sizeof(unit))
135 D(bug("LoadPrefs: Reading chunk successful.\n"));
137 chunk_map |= (1 << 1);
140 if (cn->cn_ID == ID_PDEV && cn->cn_Size == sizeof(devunit))
142 D(bug("LoadPrefs: ID_PDEV chunk size okay.\n"));
143 if (ReadChunkBytes(iff, &devunit, sizeof(devunit)) == sizeof(devunit))
145 D(bug("LoadPrefs: Reading chunk successful.\n"));
147 chunk_map |= (1 << 2);
150 if (cn->cn_ID == ID_PGFX && cn->cn_Size == sizeof(gfx))
152 D(bug("LoadPrefs: ID_PGFX chunk size okay.\n"));
153 if (ReadChunkBytes(iff, &gfx, sizeof(gfx)) == sizeof(gfx))
155 D(bug("LoadPrefs: Reading chunk successful.\n"));
157 chunk_map |= (1 << 3);
162 CloseIFF(iff);
164 FreeIFF(iff);
167 if (chunk_map & (1 << 0)) {
168 D(bug("LoadPrefs: PTXT\n"));
170 IMPORT_WORD(txt.pt_PaperType);
171 IMPORT_WORD(txt.pt_PaperSize);
172 IMPORT_WORD(txt.pt_PaperLength);
173 IMPORT_WORD(txt.pt_Pitch);
174 IMPORT_WORD(txt.pt_Spacing);
175 IMPORT_WORD(txt.pt_LeftMargin);
176 IMPORT_WORD(txt.pt_RightMargin);
177 IMPORT_WORD(txt.pt_Quality);
178 printerprefs.pp_Txt = txt;
181 if (chunk_map & (1 << 1)) {
182 D(bug("LoadPrefs: PUNT\n"));
184 IMPORT_LONG(unit.pu_UnitNum);
185 IMPORT_LONG(unit.pu_OpenDeviceFlags);
186 printerprefs.pp_Unit = unit;
189 if (chunk_map & (1 << 2)) {
190 D(bug("LoadPrefs: PDEV\n"));
192 IMPORT_LONG(devunit.pd_UnitNum);
193 printerprefs.pp_DeviceUnit = devunit;
196 if (chunk_map & (1 << 3)) {
197 D(bug("LoadPrefs: PGFX\n"));
199 IMPORT_WORD(gfx.pg_Aspect);
200 IMPORT_WORD(gfx.pg_Shade);
201 IMPORT_WORD(gfx.pg_Image);
202 IMPORT_WORD(gfx.pg_Threshold);
203 IMPORT_WORD(gfx.pg_GraphicFlags);
204 IMPORT_WORD(gfx.pg_PrintMaxWidth);
205 IMPORT_WORD(gfx.pg_PrintMaxHeight);
207 printerprefs.pp_Gfx = gfx;
210 D(bug("LoadPrefs: Done\n"));
211 return (chunk_map & (1 << 0)) ? TRUE : FALSE;
214 /*********************************************************************************************/
216 #define EXPORT_WORD(x) do { x = AROS_WORD2BE(x); } while (0)
217 #define EXPORT_LONG(x) do { x = AROS_LONG2BE(x); } while (0)
219 BOOL Prefs_ExportFH(BPTR fh)
221 struct IFFHandle *iff;
222 BOOL retval = FALSE;
224 struct PrinterTxtPrefs txt = printerprefs.pp_Txt;
225 struct PrinterUnitPrefs unit = printerprefs.pp_Unit;
226 struct PrinterDeviceUnitPrefs devunit = printerprefs.pp_DeviceUnit;
227 struct PrinterGfxPrefs gfx = printerprefs.pp_Gfx;
229 EXPORT_WORD(txt.pt_PaperType);
230 EXPORT_WORD(txt.pt_PaperSize);
231 EXPORT_WORD(txt.pt_PaperLength);
232 EXPORT_WORD(txt.pt_Pitch);
233 EXPORT_WORD(txt.pt_Spacing);
234 EXPORT_WORD(txt.pt_LeftMargin);
235 EXPORT_WORD(txt.pt_RightMargin);
236 EXPORT_WORD(txt.pt_Quality);
238 EXPORT_LONG(unit.pu_UnitNum);
239 EXPORT_LONG(unit.pu_OpenDeviceFlags);
241 EXPORT_LONG(devunit.pd_UnitNum);
243 EXPORT_WORD(gfx.pg_Aspect);
244 EXPORT_WORD(gfx.pg_Shade);
245 EXPORT_WORD(gfx.pg_Image);
246 EXPORT_WORD(gfx.pg_Threshold);
247 EXPORT_WORD(gfx.pg_GraphicFlags);
248 EXPORT_WORD(gfx.pg_PrintMaxWidth);
249 EXPORT_WORD(gfx.pg_PrintMaxHeight);
251 D(bug("SavePrefsFH: fh: %lx\n", fh));
253 if ((iff = AllocIFF()))
255 iff->iff_Stream = (IPTR) fh;
256 D(bug("SavePrefsFH: stream opened.\n"));
258 InitIFFasDOS(iff);
260 if (!OpenIFF(iff, IFFF_WRITE))
262 D(bug("SavePrefsFH: OpenIFF okay.\n"));
264 if (!PushChunk(iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN))
266 D(bug("SavePrefsFH: PushChunk(FORM) okay.\n"));
268 if (!PushChunk(iff, ID_PREF, ID_PRHD, sizeof(struct FilePrefHeader)))
270 struct FilePrefHeader head;
272 D(bug("SavePrefsFH: PushChunk(PRHD) okay.\n"));
274 head.ph_Version = PHV_CURRENT;
275 head.ph_Type = 0;
276 head.ph_Flags[0] =
277 head.ph_Flags[1] =
278 head.ph_Flags[2] =
279 head.ph_Flags[3] = 0;
281 if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head))
283 D(bug("SavePrefsFH: WriteChunkBytes(PRHD) okay.\n"));
285 PopChunk(iff);
287 if (!PushChunk(iff, ID_PREF, ID_PTXT, sizeof(struct PrinterTxtPrefs)) &&
288 WriteChunkBytes(iff, &txt, sizeof(txt)) == sizeof(txt) &&
289 !PopChunk(iff) &&
290 !PushChunk(iff, ID_PREF, ID_PUNT, sizeof(struct PrinterUnitPrefs)) &&
291 WriteChunkBytes(iff, &unit, sizeof(unit)) == sizeof(unit) &&
292 !PopChunk(iff) &&
293 !PushChunk(iff, ID_PREF, ID_PDEV, sizeof(struct PrinterDeviceUnitPrefs)) &&
294 WriteChunkBytes(iff, &devunit, sizeof(devunit)) == sizeof(devunit) &&
295 !PopChunk(iff) &&
296 !PushChunk(iff, ID_PREF, ID_PGFX, sizeof(struct PrinterGfxPrefs)) &&
297 WriteChunkBytes(iff, &gfx, sizeof(gfx)) == sizeof(gfx) &&
298 !PopChunk(iff))
300 retval = TRUE;
303 else
305 PopChunk(iff);
308 PopChunk(iff);
310 CloseIFF(iff);
312 FreeIFF(iff);
315 return retval;
318 /*********************************************************************************************/
320 BOOL Prefs_HandleArgs(STRPTR from, BOOL use, BOOL save)
322 BPTR fh;
324 if (from)
326 if (!Prefs_Load(from))
328 ShowMessage("Can't read from input file");
329 return FALSE;
332 else
334 if (!Prefs_Load(PREFS_PATH_ENV))
336 if (!Prefs_Load(PREFS_PATH_ENVARC))
338 ShowMessage
340 "Can't read from file " PREFS_PATH_ENVARC
341 ".\nUsing default values."
343 Prefs_Default(0);
348 if (use || save)
350 fh = Open(PREFS_PATH_ENV, MODE_NEWFILE);
351 if (fh)
353 Prefs_ExportFH(fh);
354 Close(fh);
356 else
358 ShowMessage("Cant' open " PREFS_PATH_ENV " for writing.");
361 if (save)
363 fh = Open(PREFS_PATH_ENVARC, MODE_NEWFILE);
364 if (fh)
366 Prefs_ExportFH(fh);
367 Close(fh);
369 else
371 ShowMessage("Cant' open " PREFS_PATH_ENVARC " for writing.");
374 return TRUE;
377 /*********************************************************************************************/
379 BOOL Prefs_Default(int PrinterUnit)
381 struct PrinterTxtPrefs *txt = &printerprefs.pp_Txt;
382 struct PrinterUnitPrefs *unit = &printerprefs.pp_Unit;
383 struct PrinterDeviceUnitPrefs *devunit = &printerprefs.pp_DeviceUnit;
384 struct PrinterGfxPrefs *gfx = &printerprefs.pp_Gfx;
386 memset(&printerprefs, 0, sizeof(printerprefs));
388 D(bug("Prefs_Default: Unit %d\n", PrinterUnit));
390 strcpy(txt->pt_Driver, "PostScript");
391 txt->pt_Port = PP_PARALLEL; /* Actually, unused */
392 txt->pt_PaperType = PT_SINGLE;
393 txt->pt_PaperSize = PS_US_LETTER;
394 txt->pt_PaperLength = 65;
395 txt->pt_Pitch = PP_PICA;
396 txt->pt_Spacing = PS_SIX_LPI;
397 txt->pt_LeftMargin = 5;
398 txt->pt_RightMargin = 75;
399 txt->pt_Quality = PQ_LETTER;
401 unit->pu_UnitNum = PrinterUnit; /* printtofile.device unit == printer.device unit */
402 unit->pu_OpenDeviceFlags = 0;
403 strcpy(unit->pu_DeviceName, "printtofile");
405 devunit->pd_UnitNum = PrinterUnit;
406 snprintf(devunit->pd_UnitName, sizeof(devunit->pd_UnitName), "Unit %d", PrinterUnit);
408 gfx->pg_Aspect = PA_VERTICAL;
409 gfx->pg_Shade = PS_COLOR;
410 gfx->pg_Image = PI_POSITIVE;
411 gfx->pg_Threshold = 256;
412 gfx->pg_ColorCorrect = 0; /* No color correction enabled */
413 gfx->pg_Dimensions = PD_IGNORE;
414 gfx->pg_GraphicFlags = PGFB_CENTER_IMAGE;
415 gfx->pg_PrintDensity = PD_FLOYD;
416 gfx->pg_PrintMaxWidth = 0; /* Will be set by driver */
417 gfx->pg_PrintMaxHeight = 0; /* Will be set by driver */
418 gfx->pg_PrintXOffset = 0;
419 gfx->pg_PrintYOffset = 0;
421 return TRUE;