disable float support on m68k to prevent unwanted symbols being used in rom code...
[AROS.git] / workbench / prefs / screenmode / prefs.c
blob42832268b22694a47cd75272a27a3d43ae9b0a82
1 /*
2 Copyright © 2010-2017, 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/graphics.h>
17 #include <proto/iffparse.h>
18 #include <proto/intuition.h>
20 #include <stdio.h>
21 #include <string.h>
23 #include "prefs.h"
24 #include "misc.h"
26 /*********************************************************************************************/
28 #define PREFS_PATH_ENVARC "ENVARC:SYS/screenmode.prefs"
29 #define PREFS_PATH_ENV "ENV:SYS/screenmode.prefs"
31 /*********************************************************************************************/
33 struct ScreenModePrefs screenmodeprefs;
35 /*********************************************************************************************/
37 static BOOL Prefs_Load(STRPTR from)
39 BOOL retval = FALSE;
41 BPTR fh = Open(from, MODE_OLDFILE);
42 if (fh)
44 retval = Prefs_ImportFH(fh);
45 Close(fh);
48 return retval;
51 /*********************************************************************************************/
53 BOOL Prefs_ImportFH(BPTR fh)
55 struct IFFHandle *handle;
56 struct ScreenModePrefs loadprefs = {{0},0};
57 BOOL success = TRUE;
58 LONG error;
60 if (!(handle = AllocIFF()))
61 return FALSE;
63 handle->iff_Stream = (IPTR)fh;
64 InitIFFasDOS(handle);
66 if ((error = OpenIFF(handle, IFFF_READ)) == 0)
68 // FIXME: We want some sanity checking here!
69 if ((error = StopChunk(handle, ID_PREF, ID_SCRM)) == 0)
71 if ((error = ParseIFF(handle, IFFPARSE_SCAN)) == 0)
73 error = ReadChunkBytes
75 handle, &loadprefs, sizeof(struct ScreenModePrefs)
78 if (error < 0)
80 printf("Error: ReadChunkBytes() returned %d!\n", (int)error);
82 else
84 CopyMem(loadprefs.smp_Reserved, screenmodeprefs.smp_Reserved, sizeof(screenmodeprefs.smp_Reserved));
85 screenmodeprefs.smp_DisplayID =
86 AROS_BE2LONG(loadprefs.smp_DisplayID);
87 screenmodeprefs.smp_Width =
88 AROS_BE2WORD(loadprefs.smp_Width);
89 screenmodeprefs.smp_Height =
90 AROS_BE2WORD(loadprefs.smp_Height);
91 screenmodeprefs.smp_Depth =
92 AROS_BE2WORD(loadprefs.smp_Depth);
93 screenmodeprefs.smp_Control =
94 AROS_BE2WORD(loadprefs.smp_Control);
97 else
99 printf("ParseIFF() failed, returncode %d!\n", (int)error);
100 success = FALSE;
103 else
105 printf("StopChunk() failed, returncode %d!\n", (int)error);
106 success = FALSE;
109 CloseIFF(handle);
111 else
113 //ShowError(_(MSG_CANT_OPEN_STREAM));
116 FreeIFF(handle);
118 return success;
121 /*********************************************************************************************/
123 BOOL Prefs_ExportFH(BPTR fh)
125 struct PrefHeader header = { 0 };
126 struct IFFHandle *handle;
127 struct ScreenModePrefs saveprefs;
128 BOOL success = TRUE;
129 LONG error = 0;
131 CopyMem(screenmodeprefs.smp_Reserved, saveprefs.smp_Reserved, sizeof(screenmodeprefs.smp_Reserved));
132 saveprefs.smp_DisplayID = AROS_LONG2BE(screenmodeprefs.smp_DisplayID);
133 saveprefs.smp_Width = AROS_WORD2BE(screenmodeprefs.smp_Width);
134 saveprefs.smp_Height = AROS_WORD2BE(screenmodeprefs.smp_Height);
135 saveprefs.smp_Depth = AROS_WORD2BE(screenmodeprefs.smp_Depth);
136 saveprefs.smp_Control = AROS_WORD2BE(screenmodeprefs.smp_Control);
138 if ((handle = AllocIFF()))
140 handle->iff_Stream = (IPTR)fh;
142 InitIFFasDOS(handle);
144 if (!(error = OpenIFF(handle, IFFF_WRITE))) /* NULL = successful! */
146 error = PushChunk(handle, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN); /* FIXME: IFFSIZE_UNKNOWN? */
148 if (!error)
150 header.ph_Version = PHV_CURRENT;
151 header.ph_Type = 0;
153 error = PushChunk(handle, ID_PREF, ID_PRHD, IFFSIZE_UNKNOWN); /* FIXME: IFFSIZE_UNKNOWN? */
155 if (!error)
157 WriteChunkBytes(handle, &header, sizeof(struct PrefHeader));
158 PopChunk(handle);
161 if (!error)
163 error = PushChunk(handle, ID_PREF, ID_SCRM, sizeof(struct ScreenModePrefs));
164 if (!error)
166 WriteChunkBytes(handle, &saveprefs, sizeof(struct ScreenModePrefs));
167 PopChunk(handle);
171 // Terminate the FORM
172 PopChunk(handle);
175 if (error != 0) // TODO: We need some error checking here!
177 char buf[256];
179 NameFromFH(fh, buf, sizeof(buf));
180 printf("Error saving prefs file %s!\n", buf);
183 else
185 //ShowError(_(MSG_CANT_OPEN_STREAM));
186 success = FALSE;
189 CloseIFF(handle);
190 FreeIFF(handle);
192 else // AllocIFF()
194 // Do something more here - if IFF allocation has failed, something isn't right
195 //ShowError(_(MSG_CANT_ALLOCATE_IFFPTR));
196 success = FALSE;
199 return success;
202 /*********************************************************************************************/
204 BOOL Prefs_HandleArgs(STRPTR from, BOOL use, BOOL save)
206 BPTR fh;
208 if (from)
210 if (!Prefs_Load(from))
212 ShowMessage("Can't read from input file");
213 return FALSE;
216 else
218 if (!Prefs_Load(PREFS_PATH_ENV))
220 if (!Prefs_Load(PREFS_PATH_ENVARC))
222 ShowMessage
224 "Can't read from file " PREFS_PATH_ENVARC
225 ".\nUsing default values."
227 Prefs_Default();
232 if (use || save)
234 fh = Open(PREFS_PATH_ENV, MODE_NEWFILE);
235 if (fh)
237 Prefs_ExportFH(fh);
238 Close(fh);
240 else
242 ShowMessage("Can't open " PREFS_PATH_ENV " for writing.");
245 if (save)
247 fh = Open(PREFS_PATH_ENVARC, MODE_NEWFILE);
248 if (fh)
250 Prefs_ExportFH(fh);
251 Close(fh);
253 else
255 ShowMessage("Can't open " PREFS_PATH_ENVARC " for writing.");
258 return TRUE;
261 /*********************************************************************************************/
263 BOOL Prefs_Default(VOID)
265 struct Screen *defScreen;
266 static struct Preferences def;
268 GetPrefs(&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 if ((defScreen = LockPubScreen(NULL)) != NULL)
276 screenmodeprefs.smp_DisplayID = GetVPModeID(&defScreen->ViewPort);
277 UnlockPubScreen(NULL, defScreen);
279 else
280 screenmodeprefs.smp_DisplayID = INVALID_ID;
281 screenmodeprefs.smp_Width = def.wb_Width;
282 screenmodeprefs.smp_Height = def.wb_Height;
283 screenmodeprefs.smp_Depth = def.wb_Depth;
284 screenmodeprefs.smp_Control = 0;
286 D(Printf("[Prefs_Default] Workbench screen: %ldx%ldx%ld\n",
287 screenmodeprefs.smp_Width, screenmodeprefs.smp_Height, screenmodeprefs.smp_Depth));
289 return TRUE;