add casts to zune macros to silence some warnings
[tangerine.git] / workbench / prefs / serial / prefs.c
blob36bc870c93d5ce0cc98fa24afbe9a91328d8689d
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: English
7 */
9 /*********************************************************************************************/
11 #include "global.h"
12 #include <aros/macros.h>
14 #define DEBUG 1
15 #include <aros/debug.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
21 /*********************************************************************************************/
23 struct FilePrefHeader
25 UBYTE ph_Version;
26 UBYTE ph_Type;
27 UBYTE ph_Flags[4];
30 /*********************************************************************************************/
32 static struct SerialPrefs restore_prefs;
33 static APTR mempool;
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))
58 if (!DefaultPrefs())
60 Cleanup("Panic! Cannot setup default prefs!");
64 restore_prefs = serialprefs;
66 if (use || save)
68 SavePrefs(CONFIGNAME_ENV);
71 if (save)
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;
92 BOOL retval = FALSE;
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"));
102 InitIFFasDOS(iff);
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"));
136 retval = TRUE;
140 } /* if (!ParseIFF(iff, IFFPARSE_SCAN)) */
142 } /* if (!StopChunk(iff, ID_PREF, ID_SERL)) */
144 CloseIFF(iff);
146 } /* if (!OpenIFF(iff, IFFF_READ)) */
148 Close((BPTR)iff->iff_Stream);
150 } /* if ((iff->iff_Stream = (IPTR)Open(filename, MODE_OLDFILE))) */
152 FreeIFF(iff);
154 } /* if ((iff = AllocIFF())) */
156 return retval;
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;
180 InitIFFasDOS(iff);
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>
197 head.ph_Type = 0;
198 head.ph_Flags[0] =
199 head.ph_Flags[1] =
200 head.ph_Flags[2] =
201 head.ph_Flags[3] = 0;
203 if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head))
205 D(bug("SavePrefs: WriteChunkBytes(PRHD) okay.\n"));
207 PopChunk(iff);
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"));
218 retval = TRUE;
221 PopChunk(iff);
223 } /* if (!PushChunk(iff, ID_PREF, ID_SERL, sizeof(struct LocalePrefs))) */
225 } /* if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head)) */
226 else
228 PopChunk(iff);
231 } /* if (!PushChunk(iff, ID_PREF, ID_PRHD, sizeof(struct PrefHeader))) */
233 PopChunk(iff);
235 } /* if (!PushChunk(iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN)) */
237 CloseIFF(iff);
239 } /* if (!OpenIFF(iff, IFFFWRITE)) */
241 Close((BPTR)iff->iff_Stream);
243 } /* if ((iff->iff_Stream = (IPTR)Open(filename, MODE_NEWFILE))) */
245 FreeIFF(iff);
247 } /* if ((iff = AllocIFF())) */
249 if (!retval && delete_if_error)
251 DeleteFile(filename);
254 return retval;
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);
278 return TRUE;
281 /*********************************************************************************************/
283 void RestorePrefs(void)
285 TellGUI(PAGECMD_PREFS_CHANGING);
286 serialprefs = restore_prefs;
287 TellGUI(PAGECMD_PREFS_CHANGED);
290 /*********************************************************************************************/