Mercurial > sdl-ios-xcode
annotate src/SDL_getenv.c @ 1295:c3e36ac8a94c
Date: Sun, 6 Mar 2005 17:06:20 +0100
From: Per Inge Mathisen
Subject: [SDL] Fullscreen refresh on win32
Windows has a terrible default for fullscreen 3D apps of 60mhz refresh
rate. This can be fixed by the user by going into his driver's
control panel and forcing the refresh rate higher. However, this not a
very user friendly way about it, and in any case SDL contains no code
that could figure out this that condition has afflicted the user.
So the question is, could SDL fix this for the user? It is possible
under Windows to request a higher refresh rate. The danger is of
course that if the user has an old monitor, and you request a too high
refresh rate, the monitor could be damaged. However, I believe there
might be a way around that: Check before switching what refresh rate
the user's desktop runs in, and if our fullscreen dimensions are equal
or less than those of the desktop, use the higher refresh rate of 60
and the desktop rate.
Since most users run their desktops in the same or higher resolution
something sane, this should fix this problem for most users.
Thoughts?
An alternative is to add an SDL_GL_GetAttribute(SDL_GL_REFRESH_RATE)
option so that programs can bitch at their users at their own
convenience.
- Per
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 30 Jan 2006 06:56:10 +0000 |
parents | 05d5d36b71f4 |
children | c9b51268668f |
rev | line source |
---|---|
249 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
769
b8d311d90021
Updated copyright information for 2004 (Happy New Year!)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
3 Copyright (C) 1997-2004 Sam Lantinga |
249 | 4 |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
249
diff
changeset
|
20 slouken@libsdl.org |
249 | 21 */ |
22 | |
23 #ifdef SAVE_RCSID | |
24 static char rcsid = | |
25 "@(#) $Id$"; | |
26 #endif | |
0 | 27 |
28 /* Not all environments have a working getenv()/putenv() */ | |
29 | |
30 #ifdef TEST_MAIN | |
31 #define NEED_SDL_GETENV | |
32 #endif | |
33 | |
34 #include "SDL_getenv.h" | |
35 | |
36 #ifdef NEED_SDL_GETENV | |
37 | |
1273
05d5d36b71f4
Who'd have thought that Windows CE wouldn't have the environment APIs?
Sam Lantinga <slouken@libsdl.org>
parents:
1268
diff
changeset
|
38 #if defined(WIN32) && !defined(_WIN32_WCE) |
1268
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
39 |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
40 #define WIN32_LEAN_AND_MEAN |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
41 #include <windows.h> |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
42 #include <malloc.h> |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
43 #include <string.h> |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
44 |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
45 /* Note this isn't thread-safe! */ |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
46 |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
47 static char *SDL_envmem = NULL; /* Ugh, memory leak */ |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
48 static DWORD SDL_envmemlen = 0; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
49 |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
50 /* Put a variable of the form "name=value" into the environment */ |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
51 int SDL_putenv(const char *variable) |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
52 { |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
53 DWORD bufferlen; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
54 char *value; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
55 const char *sep; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
56 |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
57 sep = strchr(variable, '='); |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
58 if ( sep == NULL ) { |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
59 return -1; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
60 } |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
61 bufferlen = strlen(variable)+1; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
62 if ( bufferlen > SDL_envmemlen ) { |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
63 char *newmem = (char *)realloc(SDL_envmem, bufferlen); |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
64 if ( newmem == NULL ) { |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
65 return -1; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
66 } |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
67 SDL_envmem = newmem; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
68 SDL_envmemlen = bufferlen; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
69 } |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
70 strcpy(SDL_envmem, variable); |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
71 value = SDL_envmem + (sep - variable); |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
72 *value++ = '\0'; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
73 if ( !SetEnvironmentVariable(SDL_envmem, *value ? value : NULL) ) { |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
74 return -1; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
75 } |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
76 return 0; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
77 } |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
78 |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
79 /* Retrieve a variable named "name" from the environment */ |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
80 char *SDL_getenv(const char *name) |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
81 { |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
82 DWORD bufferlen; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
83 |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
84 bufferlen = GetEnvironmentVariable(name, SDL_envmem, SDL_envmemlen); |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
85 if ( bufferlen == 0 ) { |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
86 return NULL; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
87 } |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
88 if ( bufferlen > SDL_envmemlen ) { |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
89 char *newmem = (char *)realloc(SDL_envmem, bufferlen); |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
90 if ( newmem == NULL ) { |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
91 return NULL; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
92 } |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
93 SDL_envmem = newmem; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
94 SDL_envmemlen = bufferlen; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
95 GetEnvironmentVariable(name, SDL_envmem, SDL_envmemlen); |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
96 } |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
97 return SDL_envmem; |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
98 } |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
99 |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
100 #else /* roll our own */ |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
101 |
0 | 102 #include <stdlib.h> |
103 #include <string.h> | |
104 | |
105 static char **SDL_env = (char **)0; | |
106 | |
107 /* Put a variable of the form "name=value" into the environment */ | |
108 int SDL_putenv(const char *variable) | |
109 { | |
110 const char *name, *value; | |
111 int added; | |
112 int len, i; | |
113 char **new_env; | |
114 char *new_variable; | |
115 | |
116 /* A little error checking */ | |
117 if ( ! variable ) { | |
118 return(-1); | |
119 } | |
120 name = variable; | |
121 for ( value=variable; *value && (*value != '='); ++value ) { | |
122 /* Keep looking for '=' */ ; | |
123 } | |
124 if ( *value ) { | |
125 ++value; | |
126 } else { | |
127 return(-1); | |
128 } | |
129 | |
130 /* Allocate memory for the variable */ | |
131 new_variable = (char *)malloc(strlen(variable)+1); | |
132 if ( ! new_variable ) { | |
133 return(-1); | |
134 } | |
135 strcpy(new_variable, variable); | |
136 | |
137 /* Actually put it into the environment */ | |
138 added = 0; | |
139 i = 0; | |
140 if ( SDL_env ) { | |
141 /* Check to see if it's already there... */ | |
142 len = (value - name); | |
143 for ( ; SDL_env[i]; ++i ) { | |
144 if ( strncmp(SDL_env[i], name, len) == 0 ) { | |
145 break; | |
146 } | |
147 } | |
148 /* If we found it, just replace the entry */ | |
149 if ( SDL_env[i] ) { | |
150 free(SDL_env[i]); | |
151 SDL_env[i] = new_variable; | |
152 added = 1; | |
153 } | |
154 } | |
155 | |
156 /* Didn't find it in the environment, expand and add */ | |
157 if ( ! added ) { | |
158 new_env = realloc(SDL_env, (i+2)*sizeof(char *)); | |
159 if ( new_env ) { | |
160 SDL_env = new_env; | |
161 SDL_env[i++] = new_variable; | |
162 SDL_env[i++] = (char *)0; | |
163 added = 1; | |
164 } else { | |
165 free(new_variable); | |
166 } | |
167 } | |
168 return (added ? 0 : -1); | |
169 } | |
170 | |
171 /* Retrieve a variable named "name" from the environment */ | |
172 char *SDL_getenv(const char *name) | |
173 { | |
174 int len, i; | |
175 char *value; | |
176 | |
177 value = (char *)0; | |
178 if ( SDL_env ) { | |
179 len = strlen(name); | |
180 for ( i=0; SDL_env[i] && !value; ++i ) { | |
181 if ( (strncmp(SDL_env[i], name, len) == 0) && | |
182 (SDL_env[i][len] == '=') ) { | |
183 value = &SDL_env[i][len+1]; | |
184 } | |
185 } | |
186 } | |
187 return value; | |
188 } | |
189 | |
1268
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
190 #endif /* WIN32 */ |
f098b247299d
Use Win32 API for putenv/getenv to avoid C runtime conflicts
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
191 |
0 | 192 #endif /* NEED_GETENV */ |
193 | |
194 #ifdef TEST_MAIN | |
195 #include <stdio.h> | |
196 | |
197 int main(int argc, char *argv[]) | |
198 { | |
199 char *value; | |
200 | |
201 printf("Checking for non-existent variable... "); | |
202 fflush(stdout); | |
203 if ( ! getenv("EXISTS") ) { | |
204 printf("okay\n"); | |
205 } else { | |
206 printf("failed\n"); | |
207 } | |
208 printf("Setting FIRST=VALUE1 in the environment... "); | |
209 fflush(stdout); | |
210 if ( putenv("FIRST=VALUE1") == 0 ) { | |
211 printf("okay\n"); | |
212 } else { | |
213 printf("failed\n"); | |
214 } | |
215 printf("Getting FIRST from the environment... "); | |
216 fflush(stdout); | |
217 value = getenv("FIRST"); | |
218 if ( value && (strcmp(value, "VALUE1") == 0) ) { | |
219 printf("okay\n"); | |
220 } else { | |
221 printf("failed\n"); | |
222 } | |
223 printf("Setting SECOND=VALUE2 in the environment... "); | |
224 fflush(stdout); | |
225 if ( putenv("SECOND=VALUE2") == 0 ) { | |
226 printf("okay\n"); | |
227 } else { | |
228 printf("failed\n"); | |
229 } | |
230 printf("Getting SECOND from the environment... "); | |
231 fflush(stdout); | |
232 value = getenv("SECOND"); | |
233 if ( value && (strcmp(value, "VALUE2") == 0) ) { | |
234 printf("okay\n"); | |
235 } else { | |
236 printf("failed\n"); | |
237 } | |
238 printf("Setting FIRST=NOVALUE in the environment... "); | |
239 fflush(stdout); | |
240 if ( putenv("FIRST=NOVALUE") == 0 ) { | |
241 printf("okay\n"); | |
242 } else { | |
243 printf("failed\n"); | |
244 } | |
245 printf("Getting FIRST from the environment... "); | |
246 fflush(stdout); | |
247 value = getenv("FIRST"); | |
248 if ( value && (strcmp(value, "NOVALUE") == 0) ) { | |
249 printf("okay\n"); | |
250 } else { | |
251 printf("failed\n"); | |
252 } | |
253 printf("Checking for non-existent variable... "); | |
254 fflush(stdout); | |
255 if ( ! getenv("EXISTS") ) { | |
256 printf("okay\n"); | |
257 } else { | |
258 printf("failed\n"); | |
259 } | |
260 return(0); | |
261 } | |
262 #endif /* TEST_MAIN */ | |
263 |