dbc3bc58eccfedb24a2efc5518167ee1255a155a
[pcsx_rearmed.git] / plugins / dfxvideo / cfg.c
1 /***************************************************************************
2                           cfg.c  -  description
3                              -------------------
4     begin                : Sun Oct 28 2001
5     copyright            : (C) 2001 by Pete Bernert
6     email                : BlackDove@addcom.de
7  ***************************************************************************/
8 /***************************************************************************
9  *                                                                         *
10  *   This program is free software; you can redistribute it and/or modify  *
11  *   it under the terms of the GNU General Public License as published by  *
12  *   the Free Software Foundation; either version 2 of the License, or     *
13  *   (at your option) any later version. See also the license.txt file for *
14  *   additional informations.                                              *
15  *                                                                         *
16  ***************************************************************************/
17
18 #define _IN_CFG
19
20 #include <sys/stat.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23
24 #undef FALSE
25 #undef TRUE
26 #define MAKELONG(low,high)     ((unsigned long)(((unsigned short)(low)) | (((unsigned long)((unsigned short)(high))) << 16)))
27
28 #include "externals.h"
29 #include "cfg.h"
30 #include "gpu.h"
31
32 char * pConfigFile = NULL;
33
34 // CONFIG FILE helpers....
35 // some helper macros:
36
37 #define GetValue(name, var) \
38  p = strstr(pB, name); \
39  if (p != NULL) { \
40   p+=strlen(name); \
41   while ((*p == ' ') || (*p == '=')) p++; \
42   if (*p != '\n') var = atoi(p); \
43  }
44
45 #define GetFloatValue(name, var) \
46  p = strstr(pB, name); \
47  if (p != NULL) { \
48   p+=strlen(name); \
49   while ((*p == ' ') || (*p == '=')) p++; \
50   if (*p != '\n') var = (float)atof(p); \
51  }
52
53 #define SetValue(name, var) \
54  p = strstr(pB, name); \
55  if (p != NULL) { \
56   p+=strlen(name); \
57   while ((*p == ' ') || (*p == '=')) p++; \
58   if (*p != '\n') { \
59    len = sprintf(t1, "%d", var); \
60    strncpy(p, t1, len); \
61    if (p[len] != ' ' && p[len] != '\n' && p[len] != 0) p[len] = ' '; \
62   } \
63  } \
64  else { \
65   size+=sprintf(pB+size, "%s = %d\n", name, var); \
66  }
67
68 #define SetFloatValue(name, var) \
69  p = strstr(pB, name); \
70  if (p != NULL) { \
71   p+=strlen(name); \
72   while ((*p == ' ') || (*p == '=')) p++; \
73   if (*p != '\n') { \
74    len = sprintf(t1, "%.1f", (double)var); \
75    strncpy(p, t1, len); \
76    if (p[len] != ' ' && p[len] != '\n' && p[len] != 0) p[len] = ' '; \
77   } \
78  } \
79  else { \
80   size+=sprintf(pB+size, "%s = %.1f\n", name, (double)var); \
81  }
82
83 static void ReadConfigFile()
84 {
85  struct stat buf;
86  FILE *in;char t[256];int len, size;
87  char * pB, * p;
88
89  if(pConfigFile)
90       strcpy(t,pConfigFile);
91  else
92   {
93    strcpy(t,"dfxvideo.cfg");
94    in = fopen(t,"rb");
95    if (!in)
96     {
97      strcpy(t,"cfg/dfxvideo.cfg");
98      in = fopen(t,"rb");
99      if(!in) sprintf(t,"%s/.pcsx/plugins/dfxvideo.cfg",getenv("HOME"));
100      else    fclose(in);
101     }
102    else     fclose(in);
103   }
104
105  if (stat(t, &buf) == -1) return;
106  size = buf.st_size;
107
108  in = fopen(t,"rb");
109  if (!in) return;
110
111  pB=(char *)malloc(size + 1);
112  memset(pB,0,size + 1);
113
114  len = fread(pB, 1, size, in);
115  fclose(in);
116
117  GetValue("ResX", iResX);
118  if(iResX<20) iResX=20;
119  iResX=(iResX/4)*4;
120
121  GetValue("ResY", iResY);
122  if(iResY<20) iResY=20;
123  iResY=(iResY/4)*4;
124
125  iWinSize=MAKELONG(iResX,iResY);
126
127  GetValue("NoStretch", iUseNoStretchBlt);
128
129  GetValue("Dithering", iUseDither);
130
131  GetValue("FullScreen", iWindowMode);
132  if(iWindowMode!=0) iWindowMode=0;
133  else               iWindowMode=1;
134
135  GetValue("ShowFPS", iShowFPS);
136  if(iShowFPS<0) iShowFPS=0;
137  if(iShowFPS>1) iShowFPS=1;
138
139  GetValue("Maintain43", iMaintainAspect);
140  if(iMaintainAspect<0) iMaintainAspect=0;
141  if(iMaintainAspect>1) iMaintainAspect=1;
142
143  GetValue("UseFrameLimit", UseFrameLimit);
144  if(UseFrameLimit<0) UseFrameLimit=0;
145  if(UseFrameLimit>1) UseFrameLimit=1;
146
147  GetValue("UseFrameSkip", UseFrameSkip);
148  if(UseFrameSkip<0) UseFrameSkip=0;
149  if(UseFrameSkip>1) UseFrameSkip=1;
150
151  GetValue("FPSDetection", iFrameLimit);
152  if(iFrameLimit<1) iFrameLimit=1;
153  if(iFrameLimit>2) iFrameLimit=2;
154
155  GetFloatValue("FrameRate", fFrameRate);
156  fFrameRate/=10;
157  if(fFrameRate<10.0f)   fFrameRate=10.0f;
158  if(fFrameRate>1000.0f) fFrameRate=1000.0f;
159
160  GetValue("CfgFixes", dwCfgFixes);
161
162  GetValue("UseFixes", iUseFixes);
163  if(iUseFixes<0) iUseFixes=0;
164  if(iUseFixes>1) iUseFixes=1;
165
166  free(pB);
167 }
168
169 void ExecCfg(char *arg) {
170         char cfg[256];
171         struct stat buf;
172
173         strcpy(cfg, "./cfgDFXVideo");
174         if (stat(cfg, &buf) != -1) {
175                 if (fork() == 0) {
176                         execl(cfg, "cfgDFXVideo", arg, NULL);
177                         exit(0);
178                 }
179                 return;
180         }
181
182         strcpy(cfg, "./cfg/cfgDFXVideo");
183         if (stat(cfg, &buf) != -1) {
184                 if (fork() == 0) {
185                         execl(cfg, "cfgDFXVideo", arg, NULL);
186                         exit(0);
187                 }
188                 return;
189         }
190
191         sprintf(cfg, "%s/.pcsx/plugins/cfg/cfgDFXVideo", getenv("HOME"));
192         if (stat(cfg, &buf) != -1) {
193                 if (fork() == 0) {
194                         execl(cfg, "cfgDFXVideo", arg, NULL);
195                         exit(0);
196                 }
197                 return;
198         }
199
200         printf("ERROR: cfgDFXVideo file not found!\n");
201 }
202
203 void SoftDlgProc(void)
204 {
205         ExecCfg("CFG");
206 }
207
208 void AboutDlgProc(void)
209 {
210         char args[256];
211
212         sprintf(args, "ABOUT");
213         ExecCfg(args);
214 }
215
216 void ReadConfigGPU(void)
217 {
218  // defaults
219  iResX=640;iResY=480;
220  iWinSize=MAKELONG(iResX,iResY);
221  iColDepth=32;
222  iWindowMode=1;
223  iMaintainAspect=0;
224  UseFrameLimit=1;
225  UseFrameSkip=0;
226  iFrameLimit=2;
227  fFrameRate=200.0f;
228  dwCfgFixes=0;
229  iUseFixes=0;
230  iUseNoStretchBlt=0;
231  iUseDither=0;
232  iShowFPS=0;
233
234  // read sets
235  ReadConfigFile();
236
237  // additional checks
238  if(!iColDepth)       iColDepth=32;
239  if(iUseFixes)        dwActFixes=dwCfgFixes;
240  SetFixes();
241 }
242
243 void WriteConfig(void) {
244
245  struct stat buf;
246  FILE *out;char t[256];int len, size;
247  char * pB, * p; char t1[8];
248
249  if(pConfigFile) 
250       strcpy(t,pConfigFile);
251  else 
252   {
253    strcpy(t,"dfxvideo.cfg");
254    out = fopen(t,"rb");
255    if (!out) 
256     {
257      strcpy(t,"cfg/dfxvideo.cfg");
258      out = fopen(t,"rb");
259      if(!out) sprintf(t,"%s/.pcsx/plugins/dfxvideo.cfg",getenv("HOME"));
260      else     fclose(out);
261     }
262    else     fclose(out);
263   }
264
265  if (stat(t, &buf) != -1) size = buf.st_size;
266  else size = 0;
267
268  out = fopen(t,"rb");
269  if (!out) {
270   // defaults
271   iResX=640;iResY=480;
272   iColDepth=32;
273   iWindowMode=1;
274   iMaintainAspect=0;
275   UseFrameLimit=0;
276   UseFrameSkip=0;
277   iFrameLimit=2;
278   fFrameRate=200.0f;
279   dwCfgFixes=0;
280   iUseFixes=0;
281   iUseNoStretchBlt=0;
282   iUseDither=0;
283   iShowFPS=0;
284
285   size = 0;
286   pB=(char *)malloc(4096);
287   memset(pB,0,4096);
288  }
289  else {
290   pB=(char *)malloc(size+4096);
291   memset(pB,0,size+4096);
292
293   len = fread(pB, 1, size, out);
294   fclose(out);
295  }
296
297  SetValue("ResX", iResX);
298  SetValue("ResY", iResY);
299  SetValue("NoStretch", iUseNoStretchBlt);
300  SetValue("Dithering", iUseDither);
301  SetValue("FullScreen", !iWindowMode);
302  SetValue("ShowFPS", iShowFPS);
303  SetValue("Maintain43", iMaintainAspect);
304  SetValue("UseFrameLimit", UseFrameLimit);
305  SetValue("UseFrameSkip", UseFrameSkip);
306  SetValue("FPSDetection", iFrameLimit);
307  SetFloatValue("FrameRate", fFrameRate);
308  SetValue("CfgFixes", (unsigned int)dwCfgFixes);
309  SetValue("UseFixes", iUseFixes);
310
311  out = fopen(t,"wb");
312  if (!out) return;
313
314  len = fwrite(pB, 1, size, out);
315  fclose(out);
316
317  free(pB);
318 }