1 /* $Id: winConfig.c,v 5.2 2001/06/22 05:00:57 dik Exp $
2  *
3  * XPilot, a multiplayer gravity war game.  Copyright (C) 1991-2001 by
4  *
5  *      Bj�rn Stabell        <bjoern@xpilot.org>
6  *      Ken Ronny Schouten   <ken@xpilot.org>
7  *      Bert Gijsbers        <bert@xpilot.org>
8  *      Dick Balaska         <dick@xpilot.org>
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.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24 #include <windows.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <errno.h>
30 
31 #include "version.h"
32 #include "config.h"
33 #include "const.h"
34 #include "error.h"
35 #include "commonproto.h"
36 #include "../paint.h"
37 #include "keys.h"
38 #include "../default.h"
39 #include "winConfig.h"
40 #include "winClient.h"
41 #include "winXXPilot.h"
42 #ifndef	lint
43 static char sourceid[] =
44     "@(#)$Id: winConfig.c,v 5.2 2001/06/22 05:00:57 dik Exp $";
45 #endif
46 
47 
48 extern  char    **Argv;
49 extern  int     Argc;
50 
51 const char*	winHelpFile;
52 
53 
Config_get_name(char * name)54 void Config_get_name(char *name)
55 {
56     if (*name == '\0') {
57 	/* Windows may have no default name */
58 	strcpy(name, "NoName");
59     }
60 }
61 
62 
Config_get_team(int * my_team)63 void Config_get_team(int *my_team)
64 {
65     if (Argc > 2) {
66 	/* oh, it's those fixed pos variables again! */
67 	if (Argv[2] && !strncmp(Argv[2], "-team", 5)) {
68 	    /* i've really got to fix that... */
69 	    *my_team = Argv[2][6] & 0x0f;
70 	}
71     }
72 }
73 
74 
Config_save_resource(FILE * fp,const char * resource,char * value)75 void Config_save_resource(FILE *fp, const char *resource, char *value)
76 {
77     WritePrivateProfileString("Settings",
78 			      resource,
79 			      value,
80 			      Get_xpilotini_file(1));
81 }
82 
83 
84 /* save our window's position */
Config_save_window_positions(void)85 void Config_save_window_positions(void)
86 {
87     WINDOWPLACEMENT	wp;
88     Window w;
89     RECT rect;
90     char	s[50];
91     w = WinXGetParent(top);
92     WinXGetWindowRect(w, &rect);
93     WinXGetWindowPlacement(w, &wp);
94     if (wp.showCmd != SW_SHOWMINIMIZED)
95     {
96 	extern	const char* s_WindowMet;
97 	extern	const char* s_L;
98 	extern	const char* s_T;
99 	extern	const char* s_R;
100 	extern	const char* s_B;
101 	itoa(rect.left, s, 10);
102 	WritePrivateProfileString(s_WindowMet, s_L,
103 				  itoa(rect.left, s, 10),
104 				  Get_xpilotini_file(1));
105 	WritePrivateProfileString(s_WindowMet, s_T,
106 				  itoa(rect.top, s, 10),
107 				  Get_xpilotini_file(1));
108 	WritePrivateProfileString(s_WindowMet, s_R,
109 				  itoa(rect.right, s, 10),
110 				  Get_xpilotini_file(1));
111 	WritePrivateProfileString(s_WindowMet, s_B,
112 				  itoa(rect.bottom, s, 10),
113 				  Get_xpilotini_file(1));
114     }
115 }
116 
117 
Config_get_profile_string(const char * key,const char * def,char * result,int size)118 void Config_get_profile_string(
119 	const char* key,
120 	const char* def,
121 	char* result,
122 	int size)
123 {
124     int		i;
125 
126     for (i = 0; i < 3; i++)
127     {
128 	GetPrivateProfileString("Settings", key, "", result, size,
129 				Get_xpilotini_file(i));
130 	if (result[0] != '\0')
131 	    return;
132     }
133     strncpy(result, def, size);
134 }
135 
136 
Config_get_resource(const char * resource,char * result,unsigned size,int * index)137 void Config_get_resource(
138 	const char *resource,
139 	char *result,
140 	unsigned size,
141 	int* index)
142 {
143     unsigned		hash = String_hash(resource);
144 	int	i;
145 
146     for (i = 0;;)
147 	{
148 		if (!strcmp(resource, options[i].name))
149 		{
150 			*index = i;
151 			break;
152 		}
153 		if (++i >= optionsCount)
154 		{
155 			errno = 0;
156 			error("BUG: Can't find option \"%s\"", resource);
157 			exit(1);
158 		}
159     }
160     Config_get_profile_string(resource, options[i].fallback, result, size);
161 
162 #if 0
163     GetPrivateProfileString("Settings", resource, "", result, size,
164 			    Get_xpilotini_file(1));
165     if (result[0] == '\0') {
166 	GetPrivateProfileString("Settings", resource, "", result, size,
167 				Get_xpilotini_file(2));
168 	if (result[0] == '\0') {
169 	    strncpy(result, options[*index].fallback, size);
170 	}
171     }
172 #endif
173 }
174 
175 
176 /* Get the name for XPilot.ini .  Hopefully, this will be the fully qualified
177 path to where XPilot.exe started from.  Right now, we guess that is the location
178 of the default help file.
179 the parameter level is used to determine which ini file to use.  This allows us
180 to cascade through a series of default ini files.
181 level = -1 = free private memory.
182 level =  0 = current directory, XPilot.host.ini  (currently returns defini)
183 level =  1 = current directory, XPilot.ini
184 level =  2 = Windows directory, XPilot.ini
185 */
Get_xpilotini_file(int level)186 char *Get_xpilotini_file(int level)
187 {
188     static char *xpini = NULL;
189     static char *winxpini = NULL;
190     static char *defini = "XPilot.ini";
191 
192     char *end;
193     int size;
194 
195     switch (level) {
196     case 0:
197     case 1:
198 	if (xpini)
199 	    return (xpini);
200 	if (!winHelpFile)	/* do we have the help default to build from? */
201 	    return (defini);	/* no, return generic name (wherever that is) */
202 
203 	end = strrchr(winHelpFile, '\\');
204 	if (!end)
205 	    return (defini);	/* no backslash? bail out */
206 	size = (end - winHelpFile) + 13;
207 	xpini = malloc(size + 1);
208 	memset(xpini, 0, size);
209 	strncpy(xpini, winHelpFile, end - winHelpFile);
210 	strcat(xpini, "\\");
211 	strcat(xpini, defini);
212 	return (xpini);
213     case 2:
214 	if (winxpini)
215 	    return (winxpini);
216 	size = GetWindowsDirectory(winxpini, 0);	/* returns size needed */
217 	winxpini = malloc(size + 15);
218 	GetWindowsDirectory(winxpini, size + 13);
219 	if (winxpini[strlen(winxpini)] != '\\')
220 	    strcat(winxpini, "\\");
221 	strcat(winxpini, defini);
222 	return (winxpini);
223     case -1:
224 	if (xpini) {
225 	    free(xpini);
226 	    xpini = NULL;
227 	}
228 	if (winxpini) {
229 	    free(winxpini);
230 	    winxpini = NULL;
231 	}
232 	return (NULL);
233     }
234     error("BUG: bad level in Get_xpilotini_file");
235     return (defini);
236 }
237