Don't keep asking for S2EVENT_CONNECT reports if driver doen't
[AROS.git] / workbench / prefs / screenmode / prefs.c
blobb43ab6dd085419ff7735161110382249dbe74f13
1 /*
2 Copyright © 2010-2011, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #define DEBUG 0
7 #include <aros/debug.h>
9 #include <intuition/preferences.h>
10 #include <prefs/screenmode.h>
11 #include <prefs/prefhdr.h>
12 #include <graphics/modeid.h>
14 #include <proto/dos.h>
15 #include <proto/exec.h>
16 #include <proto/iffparse.h>
17 #include <proto/intuition.h>
19 #include <stdio.h>
20 #include <string.h>
22 #include "prefs.h"
23 #include "misc.h"
25 #ifdef BIGENDIAN_PREFS
26 #define GET_WORD AROS_BE2WORD
27 #define GET_LONG AROS_BE2LONG
28 #else
29 #define GET_WORD(x) x
30 #define GET_LONG(x) x
31 #endif
33 /*********************************************************************************************/
35 #define PREFS_PATH_ENVARC "ENVARC:SYS/screenmode.prefs"
36 #define PREFS_PATH_ENV "ENV:SYS/screenmode.prefs"
38 /*********************************************************************************************/
40 struct ScreenModePrefs screenmodeprefs;
42 /*********************************************************************************************/
44 static BOOL Prefs_Load(STRPTR from)
46 BOOL retval = FALSE;
48 BPTR fh = Open(from, MODE_OLDFILE);
49 if (fh)
51 retval = Prefs_ImportFH(fh);
52 Close(fh);
55 return retval;
58 /*********************************************************************************************/
60 BOOL Prefs_ImportFH(BPTR fh)
62 struct IFFHandle *handle;
63 struct ScreenModePrefs loadprefs = {{0},0};
64 BOOL success = TRUE;
65 LONG error;
67 if (!(handle = AllocIFF()))
68 return FALSE;
70 handle->iff_Stream = (IPTR)fh;
71 InitIFFasDOS(handle);
73 if ((error = OpenIFF(handle, IFFF_READ)) == 0)
75 // FIXME: We want some sanity checking here!
76 if ((error = StopChunk(handle, ID_PREF, ID_SCRM)) == 0)
78 if ((error = ParseIFF(handle, IFFPARSE_SCAN)) == 0)
80 error = ReadChunkBytes
82 handle, &loadprefs, sizeof(struct ScreenModePrefs)
85 if (error < 0)
87 printf("Error: ReadChunkBytes() returned %d!\n", (int)error);
89 else
91 CopyMem(loadprefs.smp_Reserved, screenmodeprefs.smp_Reserved, sizeof(screenmodeprefs.smp_Reserved));
92 screenmodeprefs.smp_DisplayID = GET_LONG(loadprefs.smp_DisplayID);
93 screenmodeprefs.smp_Width = GET_WORD(loadprefs.smp_Width);
94 screenmodeprefs.smp_Height = GET_WORD(loadprefs.smp_Height);
95 screenmodeprefs.smp_Depth = GET_WORD(loadprefs.smp_Depth);
96 screenmodeprefs.smp_Control = AROS_BE2WORD(loadprefs.smp_Control);
99 else
101 printf("ParseIFF() failed, returncode %d!\n", (int)error);
102 success = FALSE;
105 else
107 printf("StopChunk() failed, returncode %d!\n", (int)error);
108 success = FALSE;
111 CloseIFF(handle);
113 else
115 //ShowError(_(MSG_CANT_OPEN_STREAM));
118 FreeIFF(handle);
120 return success;
123 /*********************************************************************************************/
125 BOOL Prefs_ExportFH(BPTR fh)
127 struct PrefHeader header = { 0 };
128 struct IFFHandle *handle;
129 struct ScreenModePrefs saveprefs;
130 BOOL success = TRUE;
131 LONG error = 0;
133 CopyMem(screenmodeprefs.smp_Reserved, saveprefs.smp_Reserved, sizeof(screenmodeprefs.smp_Reserved));
134 saveprefs.smp_DisplayID = GET_LONG(screenmodeprefs.smp_DisplayID);
135 saveprefs.smp_Width = GET_WORD(screenmodeprefs.smp_Width);
136 saveprefs.smp_Height = GET_WORD(screenmodeprefs.smp_Height);
137 saveprefs.smp_Depth = GET_WORD(screenmodeprefs.smp_Depth);
138 saveprefs.smp_Control = AROS_WORD2BE(screenmodeprefs.smp_Control);
140 if ((handle = AllocIFF()))
142 handle->iff_Stream = (IPTR)fh;
144 InitIFFasDOS(handle);
146 if (!(error = OpenIFF(handle, IFFF_WRITE))) /* NULL = successful! */
148 error = PushChunk(handle, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN); /* FIXME: IFFSIZE_UNKNOWN? */
150 if (!error)
152 header.ph_Version = PHV_CURRENT;
153 header.ph_Type = 0;
155 error = PushChunk(handle, ID_PREF, ID_PRHD, IFFSIZE_UNKNOWN); /* FIXME: IFFSIZE_UNKNOWN? */
157 if (!error)
159 WriteChunkBytes(handle, &header, sizeof(struct PrefHeader));
160 PopChunk(handle);
163 if (!error)
165 error = PushChunk(handle, ID_PREF, ID_SCRM, sizeof(struct ScreenModePrefs));
166 if (!error)
168 WriteChunkBytes(handle, &saveprefs, sizeof(struct ScreenModePrefs));
169 PopChunk(handle);
173 // Terminate the FORM
174 PopChunk(handle);
177 if (error != 0) // TODO: We need some error checking here!
179 char buf[256];
181 NameFromFH(fh, buf, sizeof(buf));
182 printf("Error saving prefs file %s!\n", buf);
185 else
187 //ShowError(_(MSG_CANT_OPEN_STREAM));
188 success = FALSE;
191 CloseIFF(handle);
192 FreeIFF(handle);
194 else // AllocIFF()
196 // Do something more here - if IFF allocation has failed, something isn't right
197 //ShowError(_(MSG_CANT_ALLOCATE_IFFPTR));
198 success = FALSE;
201 return success;
204 /*********************************************************************************************/
206 BOOL Prefs_HandleArgs(STRPTR from, BOOL use, BOOL save)
208 BPTR fh;
210 if (from)
212 if (!Prefs_Load(from))
214 ShowMessage("Can't read from input file");
215 return FALSE;
218 else
220 if (!Prefs_Load(PREFS_PATH_ENV))
222 if (!Prefs_Load(PREFS_PATH_ENVARC))
224 ShowMessage
226 "Can't read from file " PREFS_PATH_ENVARC
227 ".\nUsing default values."
229 Prefs_Default();
234 if (use || save)
236 fh = Open(PREFS_PATH_ENV, MODE_NEWFILE);
237 if (fh)
239 Prefs_ExportFH(fh);
240 Close(fh);
242 else
244 ShowMessage("Can't open " PREFS_PATH_ENV " for writing.");
247 if (save)
249 fh = Open(PREFS_PATH_ENVARC, MODE_NEWFILE);
250 if (fh)
252 Prefs_ExportFH(fh);
253 Close(fh);
255 else
257 ShowMessage("Can't open " PREFS_PATH_ENVARC " for writing.");
260 return TRUE;
263 /*********************************************************************************************/
265 BOOL Prefs_Default(VOID)
267 static struct Preferences def;
269 GetDefPrefs(&def, sizeof(def));
270 screenmodeprefs.smp_Reserved[0] = 0;
271 screenmodeprefs.smp_Reserved[1] = 0;
272 screenmodeprefs.smp_Reserved[2] = 0;
273 screenmodeprefs.smp_Reserved[3] = 0;
274 screenmodeprefs.smp_DisplayID = INVALID_ID;
275 screenmodeprefs.smp_Width = def.wb_Width;
276 screenmodeprefs.smp_Height = def.wb_Height;
277 screenmodeprefs.smp_Depth = def.wb_Depth;
278 screenmodeprefs.smp_Control = 0;
280 D(Printf("[Prefs_Default] Default Workbench screen: %ldx%ldx%ld\n",
281 screenmodeprefs.smp_Width, screenmodeprefs.smp_Height, screenmodeprefs.smp_Depth));
283 return TRUE;