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