2 Copyright © 2010-2017, 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/graphics.h>
17 #include <proto/iffparse.h>
18 #include <proto/intuition.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
)
41 BPTR fh
= Open(from
, MODE_OLDFILE
);
44 retval
= Prefs_ImportFH(fh
);
51 /*********************************************************************************************/
53 BOOL
Prefs_ImportFH(BPTR fh
)
55 struct IFFHandle
*handle
;
56 struct ScreenModePrefs loadprefs
= {{0},0};
60 if (!(handle
= AllocIFF()))
63 handle
->iff_Stream
= (IPTR
)fh
;
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
)
80 printf("Error: ReadChunkBytes() returned %d!\n", (int)error
);
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
);
99 printf("ParseIFF() failed, returncode %d!\n", (int)error
);
105 printf("StopChunk() failed, returncode %d!\n", (int)error
);
113 //ShowError(_(MSG_CANT_OPEN_STREAM));
121 /*********************************************************************************************/
123 BOOL
Prefs_ExportFH(BPTR fh
)
125 struct PrefHeader header
= { 0 };
126 struct IFFHandle
*handle
;
127 struct ScreenModePrefs saveprefs
;
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? */
150 header
.ph_Version
= PHV_CURRENT
;
153 error
= PushChunk(handle
, ID_PREF
, ID_PRHD
, IFFSIZE_UNKNOWN
); /* FIXME: IFFSIZE_UNKNOWN? */
157 WriteChunkBytes(handle
, &header
, sizeof(struct PrefHeader
));
163 error
= PushChunk(handle
, ID_PREF
, ID_SCRM
, sizeof(struct ScreenModePrefs
));
166 WriteChunkBytes(handle
, &saveprefs
, sizeof(struct ScreenModePrefs
));
171 // Terminate the FORM
175 if (error
!= 0) // TODO: We need some error checking here!
179 NameFromFH(fh
, buf
, sizeof(buf
));
180 printf("Error saving prefs file %s!\n", buf
);
185 //ShowError(_(MSG_CANT_OPEN_STREAM));
194 // Do something more here - if IFF allocation has failed, something isn't right
195 //ShowError(_(MSG_CANT_ALLOCATE_IFFPTR));
202 /*********************************************************************************************/
204 BOOL
Prefs_HandleArgs(STRPTR from
, BOOL use
, BOOL save
)
210 if (!Prefs_Load(from
))
212 ShowMessage("Can't read from input file");
218 if (!Prefs_Load(PREFS_PATH_ENV
))
220 if (!Prefs_Load(PREFS_PATH_ENVARC
))
224 "Can't read from file " PREFS_PATH_ENVARC
225 ".\nUsing default values."
234 fh
= Open(PREFS_PATH_ENV
, MODE_NEWFILE
);
242 ShowMessage("Can't open " PREFS_PATH_ENV
" for writing.");
247 fh
= Open(PREFS_PATH_ENVARC
, MODE_NEWFILE
);
255 ShowMessage("Can't open " PREFS_PATH_ENVARC
" for writing.");
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
);
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
));