2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
9 /*********************************************************************************************/
12 #include <aros/macros.h>
15 #include <aros/debug.h>
21 /*********************************************************************************************/
30 /*********************************************************************************************/
32 static struct SerialPrefs restore_prefs
;
35 /*********************************************************************************************/
37 #define SER_DEFAULT_BAUDRATE 19200
38 #define SER_DEFAULT_IBUFFER 512
39 #define SER_DEFAULT_OBUFFER 512
41 #define SER_DEFAULT_IHANDSHAKE HSHAKE_NONE
42 #define SER_DEFAULT_OHANDSHAKE HSHAKE_NONE
44 #define SER_DEFAULT_PARITY PARITY_NONE
45 #define SER_DEFAULT_BITS_PER_CHAR 8
46 #define SER_DEFAULT_STOP_BITS 1
48 /*********************************************************************************************/
51 void InitPrefs(STRPTR filename
, BOOL use
, BOOL save
)
53 mempool
= CreatePool(MEMF_PUBLIC
| MEMF_CLEAR
, 2048, 2048);
54 if (!mempool
) Cleanup("Out of memory!");
56 if (!LoadPrefs(filename
))
60 Cleanup("Panic! Cannot setup default prefs!");
64 restore_prefs
= serialprefs
;
68 SavePrefs(CONFIGNAME_ENV
);
73 SavePrefs(CONFIGNAME_ENVARC
);
76 if (use
|| save
) Cleanup(NULL
);
79 /*********************************************************************************************/
81 void CleanupPrefs(void)
83 if (mempool
) DeletePool(mempool
);
86 /*********************************************************************************************/
88 BOOL
LoadPrefs(STRPTR filename
)
90 static struct SerialPrefs loadprefs
;
91 struct IFFHandle
*iff
;
94 D(bug("LoadPrefs: Trying to open \"%s\"\n", filename
));
96 if ((iff
= AllocIFF()))
98 if ((iff
->iff_Stream
= (IPTR
)Open(filename
, MODE_OLDFILE
)))
100 D(bug("LoadPrefs: stream opened.\n"));
104 if (!OpenIFF(iff
, IFFF_READ
))
106 D(bug("LoadPrefs: OpenIFF okay.\n"));
108 if (!StopChunk(iff
, ID_PREF
, ID_SERL
))
110 D(bug("LoadPrefs: StopChunk okay.\n"));
112 if (!ParseIFF(iff
, IFFPARSE_SCAN
))
114 struct ContextNode
*cn
;
116 D(bug("LoadPrefs: ParseIFF okay.\n"));
118 cn
= CurrentChunk(iff
);
120 if (cn
->cn_Size
== sizeof(struct SerialPrefs
))
122 D(bug("LoadPrefs: ID_SERL chunk size okay.\n"));
124 if (ReadChunkBytes(iff
, &loadprefs
, sizeof(struct SerialPrefs
)) == sizeof(struct SerialPrefs
))
126 D(bug("LoadPrefs: Reading chunk successful.\n"));
128 TellGUI(PAGECMD_PREFS_CHANGING
);
130 serialprefs
= loadprefs
;
132 TellGUI(PAGECMD_PREFS_CHANGED
);
134 D(bug("LoadPrefs: Everything okay :-)\n"));
140 } /* if (!ParseIFF(iff, IFFPARSE_SCAN)) */
142 } /* if (!StopChunk(iff, ID_PREF, ID_SERL)) */
146 } /* if (!OpenIFF(iff, IFFF_READ)) */
148 Close((BPTR
)iff
->iff_Stream
);
150 } /* if ((iff->iff_Stream = (IPTR)Open(filename, MODE_OLDFILE))) */
154 } /* if ((iff = AllocIFF())) */
159 /*********************************************************************************************/
161 BOOL
SavePrefs(STRPTR filename
)
163 static struct SerialPrefs saveprefs
;
164 struct IFFHandle
*iff
;
165 BOOL retval
= FALSE
, delete_if_error
= FALSE
;
167 saveprefs
= serialprefs
;
170 D(bug("SavePrefs: Trying to open \"%s\"\n", filename
));
172 if ((iff
= AllocIFF()))
174 if ((iff
->iff_Stream
= (IPTR
)Open(filename
, MODE_NEWFILE
)))
176 D(bug("SavePrefs: stream opened.\n"));
178 delete_if_error
= TRUE
;
182 if (!OpenIFF(iff
, IFFF_WRITE
))
184 D(bug("SavePrefs: OpenIFF okay.\n"));
186 if (!PushChunk(iff
, ID_PREF
, ID_FORM
, IFFSIZE_UNKNOWN
))
188 D(bug("SavePrefs: PushChunk(FORM) okay.\n"));
190 if (!PushChunk(iff
, ID_PREF
, ID_PRHD
, sizeof(struct FilePrefHeader
)))
192 struct FilePrefHeader head
;
194 D(bug("SavePrefs: PushChunk(PRHD) okay.\n"));
196 head
.ph_Version
= 0; // FIXME: should be PHV_CURRENT, but see <prefs/prefhdr.h>
201 head
.ph_Flags
[3] = 0;
203 if (WriteChunkBytes(iff
, &head
, sizeof(head
)) == sizeof(head
))
205 D(bug("SavePrefs: WriteChunkBytes(PRHD) okay.\n"));
209 if (!PushChunk(iff
, ID_PREF
, ID_SERL
, sizeof(struct SerialPrefs
)))
211 D(bug("SavePrefs: PushChunk(LCLE) okay.\n"));
213 if (WriteChunkBytes(iff
, &saveprefs
, sizeof(saveprefs
)) == sizeof(saveprefs
))
215 D(bug("SavePrefs: WriteChunkBytes(SERL) okay.\n"));
216 D(bug("SavePrefs: Everything okay :-)\n"));
223 } /* if (!PushChunk(iff, ID_PREF, ID_SERL, sizeof(struct LocalePrefs))) */
225 } /* if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head)) */
231 } /* if (!PushChunk(iff, ID_PREF, ID_PRHD, sizeof(struct PrefHeader))) */
235 } /* if (!PushChunk(iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN)) */
239 } /* if (!OpenIFF(iff, IFFFWRITE)) */
241 Close((BPTR
)iff
->iff_Stream
);
243 } /* if ((iff->iff_Stream = (IPTR)Open(filename, MODE_NEWFILE))) */
247 } /* if ((iff = AllocIFF())) */
249 if (!retval
&& delete_if_error
)
251 DeleteFile(filename
);
257 /*********************************************************************************************/
259 BOOL
DefaultPrefs(void)
261 TellGUI(PAGECMD_PREFS_CHANGING
);
263 serialprefs
.sp_Reserved
[0] = 0;
264 serialprefs
.sp_Reserved
[1] = 0;
265 serialprefs
.sp_Reserved
[2] = 0;
266 serialprefs
.sp_Unit0Map
= 0;
267 serialprefs
.sp_BaudRate
= SER_DEFAULT_BAUDRATE
;
268 serialprefs
.sp_InputBuffer
= SER_DEFAULT_IBUFFER
;
269 serialprefs
.sp_OutputBuffer
= SER_DEFAULT_OBUFFER
;
270 serialprefs
.sp_InputHandshake
= SER_DEFAULT_IHANDSHAKE
;
271 serialprefs
.sp_OutputHandshake
= SER_DEFAULT_OHANDSHAKE
;
272 serialprefs
.sp_Parity
= SER_DEFAULT_PARITY
;
273 serialprefs
.sp_BitsPerChar
= SER_DEFAULT_BITS_PER_CHAR
;
274 serialprefs
.sp_StopBits
= SER_DEFAULT_STOP_BITS
;
276 TellGUI(PAGECMD_PREFS_CHANGED
);
281 /*********************************************************************************************/
283 void RestorePrefs(void)
285 TellGUI(PAGECMD_PREFS_CHANGING
);
286 serialprefs
= restore_prefs
;
287 TellGUI(PAGECMD_PREFS_CHANGED
);
290 /*********************************************************************************************/