Updated PCI IDs to latest snapshot.
[tangerine.git] / workbench / prefs / serial / prefs.c
blob8690bb4b957088b81920d708f64e31d89f567edc
1 /*
2 Copyright © 1995-2008, 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 /* #define DEBUG 1 */
18 #include <aros/debug.h>
20 #include <proto/iffparse.h>
21 #include <proto/dos.h>
23 #include <prefs/prefhdr.h>
25 #include "global.h"
28 /*********************************************************************************************/
30 struct FilePrefHeader
32 UBYTE ph_Version;
33 UBYTE ph_Type;
34 UBYTE ph_Flags[4];
37 /*********************************************************************************************/
39 static struct SerialPrefs restore_prefs;
40 struct SerialPrefs serialprefs;
41 static APTR mempool;
43 /*********************************************************************************************/
45 #define SER_DEFAULT_BAUDRATE 19200
46 #define SER_DEFAULT_IBUFFER 512
47 #define SER_DEFAULT_OBUFFER 512
49 #define SER_DEFAULT_IHANDSHAKE HSHAKE_NONE
50 #define SER_DEFAULT_OHANDSHAKE HSHAKE_NONE
52 #define SER_DEFAULT_PARITY PARITY_NONE
53 #define SER_DEFAULT_BITS_PER_CHAR 8
54 #define SER_DEFAULT_STOP_BITS 1
56 /*********************************************************************************************/
58 VOID CleanupPrefs(void)
60 D(bug("[serial prefs] CleanupPrefs\n"));
61 if (mempool) DeletePool(mempool);
64 /*********************************************************************************************/
66 VOID CopyPrefs(struct SerialPrefs *s, struct SerialPrefs *d)
68 CopyMem(s, d, sizeof(struct SerialPrefs));
71 VOID BackupPrefs(void)
73 CopyPrefs(&serialprefs, &restore_prefs);
76 VOID RestorePrefs(void)
78 CopyPrefs(&restore_prefs, &serialprefs);
81 /*********************************************************************************************/
83 /* 1 is success */
85 ULONG InitPrefs(STRPTR filename, BOOL use, BOOL save)
87 D(bug("[serial prefs] InitPrefs\n"));
88 mempool = CreatePool(MEMF_PUBLIC | MEMF_CLEAR, 2048, 2048);
89 if (!mempool)
91 ShowMsg("Out of memory!");
92 return 0;
95 if (!LoadPrefs(filename))
97 if (!DefaultPrefs())
99 CleanupPrefs();
100 ShowMsg("Panic! Cannot setup default prefs!");
101 return 0;
105 restore_prefs = serialprefs;
107 if (use || save)
109 SavePrefs((CONST STRPTR) CONFIGNAME_ENV);
112 if (save)
114 SavePrefs((CONST STRPTR) CONFIGNAME_ENVARC);
117 if (use || save) CleanupPrefs();
119 return 1;
122 /*********************************************************************************************/
124 BOOL LoadPrefsFH(BPTR fh)
126 static struct SerialPrefs loadprefs;
127 struct IFFHandle *iff;
128 BOOL retval = FALSE;
131 if ((iff = AllocIFF()))
133 iff->iff_Stream = (IPTR)fh;
135 InitIFFasDOS(iff);
137 if (!OpenIFF(iff, IFFF_READ))
139 D(bug("LoadPrefs: OpenIFF okay.\n"));
141 if (!StopChunk(iff, ID_PREF, ID_SERL))
143 D(bug("LoadPrefs: StopChunk okay.\n"));
145 if (!ParseIFF(iff, IFFPARSE_SCAN))
147 struct ContextNode *cn;
149 D(bug("LoadPrefs: ParseIFF okay.\n"));
151 cn = CurrentChunk(iff);
153 if (cn->cn_Size == sizeof(struct SerialPrefs))
155 D(bug("LoadPrefs: ID_SERL chunk size okay.\n"));
157 if (ReadChunkBytes(iff, &loadprefs, sizeof(struct SerialPrefs)) == sizeof(struct SerialPrefs))
159 D(bug("LoadPrefs: Reading chunk successful.\n"));
161 serialprefs = loadprefs;
163 D(bug("LoadPrefs: Everything okay :-)\n"));
165 retval = TRUE;
169 } /* if (!ParseIFF(iff, IFFPARSE_SCAN)) */
171 } /* if (!StopChunk(iff, ID_PREF, ID_SERL)) */
173 CloseIFF(iff);
175 } /* if (!OpenIFF(iff, IFFF_READ)) */
178 FreeIFF(iff);
180 } /* if ((iff = AllocIFF())) */
182 return retval;
185 BOOL LoadPrefs(STRPTR filename)
187 BPTR fh;
188 BOOL ret;
190 D(bug("[serial prefs] LoadPrefsFH: Trying to open \"%s\"\n", filename));
192 fh=Open(filename, MODE_OLDFILE);
194 if(!fh) return FALSE;
196 ret=LoadPrefsFH(fh);
198 Close(fh);
199 return ret;
201 /*********************************************************************************************/
203 BOOL SavePrefsFH(BPTR fh)
205 static struct SerialPrefs saveprefs;
206 struct IFFHandle *iff;
207 BOOL retval = FALSE, delete_if_error = FALSE;
209 saveprefs = serialprefs;
212 D(bug("SavePrefsFH: fh: %lx\n", fh));
213 //if ((iff->iff_Stream = (IPTR)Open(filename, MODE_NEWFILE)))
215 if ((iff = AllocIFF()))
217 iff->iff_Stream = (IPTR) fh;
218 D(bug("SavePrefsFH: stream opened.\n"));
220 delete_if_error = TRUE;
222 InitIFFasDOS(iff);
224 if (!OpenIFF(iff, IFFF_WRITE))
226 D(bug("SavePrefsFH: OpenIFF okay.\n"));
228 if (!PushChunk(iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN))
230 D(bug("SavePrefsFH: PushChunk(FORM) okay.\n"));
232 if (!PushChunk(iff, ID_PREF, ID_PRHD, sizeof(struct FilePrefHeader)))
234 struct FilePrefHeader head;
236 D(bug("SavePrefsFH: PushChunk(PRHD) okay.\n"));
238 head.ph_Version = PHV_CURRENT;
239 head.ph_Type = 0;
240 head.ph_Flags[0] =
241 head.ph_Flags[1] =
242 head.ph_Flags[2] =
243 head.ph_Flags[3] = 0;
245 if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head))
247 D(bug("SavePrefsFH: WriteChunkBytes(PRHD) okay.\n"));
249 PopChunk(iff);
251 if (!PushChunk(iff, ID_PREF, ID_SERL, sizeof(struct SerialPrefs)))
253 D(bug("SavePrefsFH: PushChunk(LCLE) okay.\n"));
255 if (WriteChunkBytes(iff, &saveprefs, sizeof(saveprefs)) == sizeof(saveprefs))
257 D(bug("SavePrefsFH: WriteChunkBytes(SERL) okay.\n"));
258 D(bug("SavePrefsFH: Everything okay :-)\n"));
260 retval = TRUE;
263 PopChunk(iff);
265 } /* if (!PushChunk(iff, ID_PREF, ID_SERL, sizeof(struct LocalePrefs))) */
267 } /* if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head)) */
268 else
270 PopChunk(iff);
273 } /* if (!PushChunk(iff, ID_PREF, ID_PRHD, sizeof(struct PrefHeader))) */
275 PopChunk(iff);
277 } /* if (!PushChunk(iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN)) */
279 CloseIFF(iff);
281 } /* if (!OpenIFF(iff, IFFFWRITE)) */
284 FreeIFF(iff);
286 } /* if ((iff = AllocIFF())) */
288 #if 0
289 if (!retval && delete_if_error)
291 DeleteFile(filename);
293 #endif
296 return retval;
299 BOOL SavePrefs(CONST STRPTR filename)
301 BPTR fh;
302 BOOL ret;
304 D(bug("[serial prefs] SavePrefs: Trying to open \"%s\"\n", filename));
306 fh=Open(filename, MODE_NEWFILE);
308 if(fh == NULL)
310 D(bug("[serial prefs] open \"%s\" failed!\n", filename));
311 return FALSE;
314 ret=SavePrefsFH(fh);
316 Close(fh);
317 return ret;
320 /*********************************************************************************************/
322 BOOL SaveEnv() {
323 BPTR fh;
324 BOOL result;
326 D(bug("[serial prefs] SaveEnv: Trying to open \"%s\"\n", CONFIGNAME_ENV));
328 fh=Open((CONST_STRPTR) CONFIGNAME_ENV, MODE_NEWFILE);
330 if(fh == NULL)
332 D(bug("[serial prefs] open \"%s\" failed!\n", CONFIGNAME_ENV));
333 return FALSE;
336 result=SavePrefsFH(fh);
338 Close(fh);
340 return result;
343 /*********************************************************************************************/
345 BOOL DefaultPrefs(void)
347 serialprefs.sp_Reserved[0] = 0;
348 serialprefs.sp_Reserved[1] = 0;
349 serialprefs.sp_Reserved[2] = 0;
350 serialprefs.sp_Unit0Map = 0;
351 serialprefs.sp_BaudRate = SER_DEFAULT_BAUDRATE;
352 serialprefs.sp_InputBuffer = SER_DEFAULT_IBUFFER;
353 serialprefs.sp_OutputBuffer = SER_DEFAULT_OBUFFER;
354 serialprefs.sp_InputHandshake = SER_DEFAULT_IHANDSHAKE;
355 serialprefs.sp_OutputHandshake = SER_DEFAULT_OHANDSHAKE;
356 serialprefs.sp_Parity = SER_DEFAULT_PARITY;
357 serialprefs.sp_BitsPerChar = SER_DEFAULT_BITS_PER_CHAR;
358 serialprefs.sp_StopBits = SER_DEFAULT_STOP_BITS;
360 return TRUE;