2 Copyright © 2010-2011, The AROS Development Team. All rights reserved.
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>
25 #ifdef BIGENDIAN_PREFS
26 #define GET_WORD AROS_BE2WORD
27 #define GET_LONG AROS_BE2LONG
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
)
48 BPTR fh
= Open(from
, MODE_OLDFILE
);
51 retval
= Prefs_ImportFH(fh
);
58 /*********************************************************************************************/
60 BOOL
Prefs_ImportFH(BPTR fh
)
62 struct IFFHandle
*handle
;
63 struct ScreenModePrefs loadprefs
= {{0},0};
67 if (!(handle
= AllocIFF()))
70 handle
->iff_Stream
= (IPTR
)fh
;
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
)
87 printf("Error: ReadChunkBytes() returned %d!\n", (int)error
);
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
);
101 printf("ParseIFF() failed, returncode %d!\n", (int)error
);
107 printf("StopChunk() failed, returncode %d!\n", (int)error
);
115 //ShowError(_(MSG_CANT_OPEN_STREAM));
123 /*********************************************************************************************/
125 BOOL
Prefs_ExportFH(BPTR fh
)
127 struct PrefHeader header
= { 0 };
128 struct IFFHandle
*handle
;
129 struct ScreenModePrefs saveprefs
;
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? */
152 header
.ph_Version
= PHV_CURRENT
;
155 error
= PushChunk(handle
, ID_PREF
, ID_PRHD
, IFFSIZE_UNKNOWN
); /* FIXME: IFFSIZE_UNKNOWN? */
159 WriteChunkBytes(handle
, &header
, sizeof(struct PrefHeader
));
165 error
= PushChunk(handle
, ID_PREF
, ID_SCRM
, sizeof(struct ScreenModePrefs
));
168 WriteChunkBytes(handle
, &saveprefs
, sizeof(struct ScreenModePrefs
));
173 // Terminate the FORM
177 if (error
!= 0) // TODO: We need some error checking here!
181 NameFromFH(fh
, buf
, sizeof(buf
));
182 printf("Error saving prefs file %s!\n", buf
);
187 //ShowError(_(MSG_CANT_OPEN_STREAM));
196 // Do something more here - if IFF allocation has failed, something isn't right
197 //ShowError(_(MSG_CANT_ALLOCATE_IFFPTR));
204 /*********************************************************************************************/
206 BOOL
Prefs_HandleArgs(STRPTR from
, BOOL use
, BOOL save
)
212 if (!Prefs_Load(from
))
214 ShowMessage("Can't read from input file");
220 if (!Prefs_Load(PREFS_PATH_ENV
))
222 if (!Prefs_Load(PREFS_PATH_ENVARC
))
226 "Can't read from file " PREFS_PATH_ENVARC
227 ".\nUsing default values."
236 fh
= Open(PREFS_PATH_ENV
, MODE_NEWFILE
);
244 ShowMessage("Can't open " PREFS_PATH_ENV
" for writing.");
249 fh
= Open(PREFS_PATH_ENVARC
, MODE_NEWFILE
);
257 ShowMessage("Can't open " PREFS_PATH_ENVARC
" for writing.");
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
));