Mercurial > sdl-ios-xcode
comparison src/video/x11/SDL_x11image.c @ 1668:4da1ee79c9af SDL-1.3
more tweaking indent options
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 29 May 2006 04:04:35 +0000 |
parents | 782fd950bd46 |
children |
comparison
equal
deleted
inserted
replaced
1667:1fddae038bc8 | 1668:4da1ee79c9af |
---|---|
32 | 32 |
33 /* Shared memory error handler routine */ | 33 /* Shared memory error handler routine */ |
34 static int shm_error; | 34 static int shm_error; |
35 static int (*X_handler) (Display *, XErrorEvent *) = NULL; | 35 static int (*X_handler) (Display *, XErrorEvent *) = NULL; |
36 static int | 36 static int |
37 shm_errhandler (Display * d, XErrorEvent * e) | 37 shm_errhandler(Display * d, XErrorEvent * e) |
38 { | 38 { |
39 if (e->error_code == BadAccess) { | 39 if (e->error_code == BadAccess) { |
40 shm_error = True; | 40 shm_error = True; |
41 return (0); | 41 return (0); |
42 } else | 42 } else |
43 return (X_handler (d, e)); | 43 return (X_handler(d, e)); |
44 } | 44 } |
45 | 45 |
46 static void | 46 static void |
47 try_mitshm (_THIS, SDL_Surface * screen) | 47 try_mitshm(_THIS, SDL_Surface * screen) |
48 { | 48 { |
49 /* Dynamic X11 may not have SHM entry points on this box. */ | 49 /* Dynamic X11 may not have SHM entry points on this box. */ |
50 if ((use_mitshm) && (!SDL_X11_HAVE_SHM)) | 50 if ((use_mitshm) && (!SDL_X11_HAVE_SHM)) |
51 use_mitshm = 0; | 51 use_mitshm = 0; |
52 | 52 |
53 if (!use_mitshm) | 53 if (!use_mitshm) |
54 return; | 54 return; |
55 shminfo.shmid = shmget (IPC_PRIVATE, screen->h * screen->pitch, | 55 shminfo.shmid = shmget(IPC_PRIVATE, screen->h * screen->pitch, |
56 IPC_CREAT | 0777); | 56 IPC_CREAT | 0777); |
57 if (shminfo.shmid >= 0) { | 57 if (shminfo.shmid >= 0) { |
58 shminfo.shmaddr = (char *) shmat (shminfo.shmid, 0, 0); | 58 shminfo.shmaddr = (char *) shmat(shminfo.shmid, 0, 0); |
59 shminfo.readOnly = False; | 59 shminfo.readOnly = False; |
60 if (shminfo.shmaddr != (char *) -1) { | 60 if (shminfo.shmaddr != (char *) -1) { |
61 shm_error = False; | 61 shm_error = False; |
62 X_handler = XSetErrorHandler (shm_errhandler); | 62 X_handler = XSetErrorHandler(shm_errhandler); |
63 XShmAttach (SDL_Display, &shminfo); | 63 XShmAttach(SDL_Display, &shminfo); |
64 XSync (SDL_Display, True); | 64 XSync(SDL_Display, True); |
65 XSetErrorHandler (X_handler); | 65 XSetErrorHandler(X_handler); |
66 if (shm_error) | 66 if (shm_error) |
67 shmdt (shminfo.shmaddr); | 67 shmdt(shminfo.shmaddr); |
68 } else { | 68 } else { |
69 shm_error = True; | 69 shm_error = True; |
70 } | 70 } |
71 shmctl (shminfo.shmid, IPC_RMID, NULL); | 71 shmctl(shminfo.shmid, IPC_RMID, NULL); |
72 } else { | 72 } else { |
73 shm_error = True; | 73 shm_error = True; |
74 } | 74 } |
75 if (shm_error) | 75 if (shm_error) |
76 use_mitshm = 0; | 76 use_mitshm = 0; |
78 screen->pixels = shminfo.shmaddr; | 78 screen->pixels = shminfo.shmaddr; |
79 } | 79 } |
80 #endif /* ! NO_SHARED_MEMORY */ | 80 #endif /* ! NO_SHARED_MEMORY */ |
81 | 81 |
82 /* Various screen update functions available */ | 82 /* Various screen update functions available */ |
83 static void X11_NormalUpdate (_THIS, int numrects, SDL_Rect * rects); | 83 static void X11_NormalUpdate(_THIS, int numrects, SDL_Rect * rects); |
84 static void X11_MITSHMUpdate (_THIS, int numrects, SDL_Rect * rects); | 84 static void X11_MITSHMUpdate(_THIS, int numrects, SDL_Rect * rects); |
85 | 85 |
86 int | 86 int |
87 X11_SetupImage (_THIS, SDL_Surface * screen) | 87 X11_SetupImage(_THIS, SDL_Surface * screen) |
88 { | 88 { |
89 #ifndef NO_SHARED_MEMORY | 89 #ifndef NO_SHARED_MEMORY |
90 try_mitshm (this, screen); | 90 try_mitshm(this, screen); |
91 if (use_mitshm) { | 91 if (use_mitshm) { |
92 SDL_Ximage = XShmCreateImage (SDL_Display, SDL_Visual, | 92 SDL_Ximage = XShmCreateImage(SDL_Display, SDL_Visual, |
93 this->hidden->depth, ZPixmap, | 93 this->hidden->depth, ZPixmap, |
94 shminfo.shmaddr, &shminfo, | 94 shminfo.shmaddr, &shminfo, |
95 screen->w, screen->h); | 95 screen->w, screen->h); |
96 if (!SDL_Ximage) { | 96 if (!SDL_Ximage) { |
97 XShmDetach (SDL_Display, &shminfo); | 97 XShmDetach(SDL_Display, &shminfo); |
98 XSync (SDL_Display, False); | 98 XSync(SDL_Display, False); |
99 shmdt (shminfo.shmaddr); | 99 shmdt(shminfo.shmaddr); |
100 screen->pixels = NULL; | 100 screen->pixels = NULL; |
101 goto error; | 101 goto error; |
102 } | 102 } |
103 this->UpdateRects = X11_MITSHMUpdate; | 103 this->UpdateRects = X11_MITSHMUpdate; |
104 } | 104 } |
105 if (!use_mitshm) | 105 if (!use_mitshm) |
106 #endif /* not NO_SHARED_MEMORY */ | 106 #endif /* not NO_SHARED_MEMORY */ |
107 { | 107 { |
108 int bpp; | 108 int bpp; |
109 screen->pixels = SDL_malloc (screen->h * screen->pitch); | 109 screen->pixels = SDL_malloc(screen->h * screen->pitch); |
110 if (screen->pixels == NULL) { | 110 if (screen->pixels == NULL) { |
111 SDL_OutOfMemory (); | 111 SDL_OutOfMemory(); |
112 return -1; | 112 return -1; |
113 } | 113 } |
114 bpp = screen->format->BytesPerPixel; | 114 bpp = screen->format->BytesPerPixel; |
115 SDL_Ximage = XCreateImage (SDL_Display, SDL_Visual, | 115 SDL_Ximage = XCreateImage(SDL_Display, SDL_Visual, |
116 this->hidden->depth, ZPixmap, 0, | 116 this->hidden->depth, ZPixmap, 0, |
117 (char *) screen->pixels, | 117 (char *) screen->pixels, |
118 screen->w, screen->h, 32, 0); | 118 screen->w, screen->h, 32, 0); |
119 if (SDL_Ximage == NULL) | 119 if (SDL_Ximage == NULL) |
120 goto error; | 120 goto error; |
121 /* XPutImage will convert byte sex automatically */ | 121 /* XPutImage will convert byte sex automatically */ |
122 SDL_Ximage->byte_order = (SDL_BYTEORDER == SDL_BIG_ENDIAN) | 122 SDL_Ximage->byte_order = (SDL_BYTEORDER == SDL_BIG_ENDIAN) |
123 ? MSBFirst : LSBFirst; | 123 ? MSBFirst : LSBFirst; |
125 } | 125 } |
126 screen->pitch = SDL_Ximage->bytes_per_line; | 126 screen->pitch = SDL_Ximage->bytes_per_line; |
127 return (0); | 127 return (0); |
128 | 128 |
129 error: | 129 error: |
130 SDL_SetError ("Couldn't create XImage"); | 130 SDL_SetError("Couldn't create XImage"); |
131 return 1; | 131 return 1; |
132 } | 132 } |
133 | 133 |
134 void | 134 void |
135 X11_DestroyImage (_THIS, SDL_Surface * screen) | 135 X11_DestroyImage(_THIS, SDL_Surface * screen) |
136 { | 136 { |
137 if (SDL_Ximage) { | 137 if (SDL_Ximage) { |
138 XDestroyImage (SDL_Ximage); | 138 XDestroyImage(SDL_Ximage); |
139 #ifndef NO_SHARED_MEMORY | 139 #ifndef NO_SHARED_MEMORY |
140 if (use_mitshm) { | 140 if (use_mitshm) { |
141 XShmDetach (SDL_Display, &shminfo); | 141 XShmDetach(SDL_Display, &shminfo); |
142 XSync (SDL_Display, False); | 142 XSync(SDL_Display, False); |
143 shmdt (shminfo.shmaddr); | 143 shmdt(shminfo.shmaddr); |
144 } | 144 } |
145 #endif /* ! NO_SHARED_MEMORY */ | 145 #endif /* ! NO_SHARED_MEMORY */ |
146 SDL_Ximage = NULL; | 146 SDL_Ximage = NULL; |
147 } | 147 } |
148 if (screen) { | 148 if (screen) { |
150 } | 150 } |
151 } | 151 } |
152 | 152 |
153 /* Determine the number of CPUs in the system */ | 153 /* Determine the number of CPUs in the system */ |
154 static int | 154 static int |
155 num_CPU (void) | 155 num_CPU(void) |
156 { | 156 { |
157 static int num_cpus = 0; | 157 static int num_cpus = 0; |
158 | 158 |
159 if (!num_cpus) { | 159 if (!num_cpus) { |
160 #if defined(__LINUX__) | 160 #if defined(__LINUX__) |
161 char line[BUFSIZ]; | 161 char line[BUFSIZ]; |
162 FILE *pstat = fopen ("/proc/stat", "r"); | 162 FILE *pstat = fopen("/proc/stat", "r"); |
163 if (pstat) { | 163 if (pstat) { |
164 while (fgets (line, sizeof (line), pstat)) { | 164 while (fgets(line, sizeof(line), pstat)) { |
165 if (SDL_memcmp (line, "cpu", 3) == 0 && line[3] != ' ') { | 165 if (SDL_memcmp(line, "cpu", 3) == 0 && line[3] != ' ') { |
166 ++num_cpus; | 166 ++num_cpus; |
167 } | 167 } |
168 } | 168 } |
169 fclose (pstat); | 169 fclose(pstat); |
170 } | 170 } |
171 #elif defined(__IRIX__) | 171 #elif defined(__IRIX__) |
172 num_cpus = sysconf (_SC_NPROC_ONLN); | 172 num_cpus = sysconf(_SC_NPROC_ONLN); |
173 #elif defined(_SC_NPROCESSORS_ONLN) | 173 #elif defined(_SC_NPROCESSORS_ONLN) |
174 /* number of processors online (SVR4.0MP compliant machines) */ | 174 /* number of processors online (SVR4.0MP compliant machines) */ |
175 num_cpus = sysconf (_SC_NPROCESSORS_ONLN); | 175 num_cpus = sysconf(_SC_NPROCESSORS_ONLN); |
176 #elif defined(_SC_NPROCESSORS_CONF) | 176 #elif defined(_SC_NPROCESSORS_CONF) |
177 /* number of processors configured (SVR4.0MP compliant machines) */ | 177 /* number of processors configured (SVR4.0MP compliant machines) */ |
178 num_cpus = sysconf (_SC_NPROCESSORS_CONF); | 178 num_cpus = sysconf(_SC_NPROCESSORS_CONF); |
179 #endif | 179 #endif |
180 if (num_cpus <= 0) { | 180 if (num_cpus <= 0) { |
181 num_cpus = 1; | 181 num_cpus = 1; |
182 } | 182 } |
183 } | 183 } |
184 return num_cpus; | 184 return num_cpus; |
185 } | 185 } |
186 | 186 |
187 int | 187 int |
188 X11_ResizeImage (_THIS, SDL_Surface * screen, Uint32 flags) | 188 X11_ResizeImage(_THIS, SDL_Surface * screen, Uint32 flags) |
189 { | 189 { |
190 int retval; | 190 int retval; |
191 | 191 |
192 X11_DestroyImage (this, screen); | 192 X11_DestroyImage(this, screen); |
193 if (flags & SDL_INTERNALOPENGL) { /* No image when using GL */ | 193 if (flags & SDL_INTERNALOPENGL) { /* No image when using GL */ |
194 retval = 0; | 194 retval = 0; |
195 } else { | 195 } else { |
196 retval = X11_SetupImage (this, screen); | 196 retval = X11_SetupImage(this, screen); |
197 /* We support asynchronous blitting on the display */ | 197 /* We support asynchronous blitting on the display */ |
198 if (flags & SDL_ASYNCBLIT) { | 198 if (flags & SDL_ASYNCBLIT) { |
199 /* This is actually slower on single-CPU systems, | 199 /* This is actually slower on single-CPU systems, |
200 probably because of CPU contention between the | 200 probably because of CPU contention between the |
201 X server and the application. | 201 X server and the application. |
202 Note: Is this still true with XFree86 4.0? | 202 Note: Is this still true with XFree86 4.0? |
203 */ | 203 */ |
204 if (num_CPU () > 1) { | 204 if (num_CPU() > 1) { |
205 screen->flags |= SDL_ASYNCBLIT; | 205 screen->flags |= SDL_ASYNCBLIT; |
206 } | 206 } |
207 } | 207 } |
208 } | 208 } |
209 return (retval); | 209 return (retval); |
210 } | 210 } |
211 | 211 |
212 /* We don't actually allow hardware surfaces other than the main one */ | 212 /* We don't actually allow hardware surfaces other than the main one */ |
213 int | 213 int |
214 X11_AllocHWSurface (_THIS, SDL_Surface * surface) | 214 X11_AllocHWSurface(_THIS, SDL_Surface * surface) |
215 { | 215 { |
216 return (-1); | 216 return (-1); |
217 } | 217 } |
218 | 218 |
219 void | 219 void |
220 X11_FreeHWSurface (_THIS, SDL_Surface * surface) | 220 X11_FreeHWSurface(_THIS, SDL_Surface * surface) |
221 { | 221 { |
222 return; | 222 return; |
223 } | 223 } |
224 | 224 |
225 int | 225 int |
226 X11_LockHWSurface (_THIS, SDL_Surface * surface) | 226 X11_LockHWSurface(_THIS, SDL_Surface * surface) |
227 { | 227 { |
228 if ((surface == SDL_VideoSurface) && blit_queued) { | 228 if ((surface == SDL_VideoSurface) && blit_queued) { |
229 XSync (GFX_Display, False); | 229 XSync(GFX_Display, False); |
230 blit_queued = 0; | 230 blit_queued = 0; |
231 } | 231 } |
232 return (0); | 232 return (0); |
233 } | 233 } |
234 | 234 |
235 void | 235 void |
236 X11_UnlockHWSurface (_THIS, SDL_Surface * surface) | 236 X11_UnlockHWSurface(_THIS, SDL_Surface * surface) |
237 { | 237 { |
238 return; | 238 return; |
239 } | 239 } |
240 | 240 |
241 int | 241 int |
242 X11_FlipHWSurface (_THIS, SDL_Surface * surface) | 242 X11_FlipHWSurface(_THIS, SDL_Surface * surface) |
243 { | 243 { |
244 return (0); | 244 return (0); |
245 } | 245 } |
246 | 246 |
247 static void | 247 static void |
248 X11_NormalUpdate (_THIS, int numrects, SDL_Rect * rects) | 248 X11_NormalUpdate(_THIS, int numrects, SDL_Rect * rects) |
249 { | 249 { |
250 int i; | 250 int i; |
251 | 251 |
252 for (i = 0; i < numrects; ++i) { | 252 for (i = 0; i < numrects; ++i) { |
253 if (rects[i].w == 0 || rects[i].h == 0) { /* Clipped? */ | 253 if (rects[i].w == 0 || rects[i].h == 0) { /* Clipped? */ |
254 continue; | 254 continue; |
255 } | 255 } |
256 XPutImage (GFX_Display, SDL_Window, SDL_GC, SDL_Ximage, | 256 XPutImage(GFX_Display, SDL_Window, SDL_GC, SDL_Ximage, |
257 rects[i].x, rects[i].y, | 257 rects[i].x, rects[i].y, |
258 rects[i].x, rects[i].y, rects[i].w, rects[i].h); | 258 rects[i].x, rects[i].y, rects[i].w, rects[i].h); |
259 } | 259 } |
260 if (SDL_VideoSurface->flags & SDL_ASYNCBLIT) { | 260 if (SDL_VideoSurface->flags & SDL_ASYNCBLIT) { |
261 XFlush (GFX_Display); | 261 XFlush(GFX_Display); |
262 blit_queued = 1; | 262 blit_queued = 1; |
263 } else { | 263 } else { |
264 XSync (GFX_Display, False); | 264 XSync(GFX_Display, False); |
265 } | 265 } |
266 } | 266 } |
267 | 267 |
268 static void | 268 static void |
269 X11_MITSHMUpdate (_THIS, int numrects, SDL_Rect * rects) | 269 X11_MITSHMUpdate(_THIS, int numrects, SDL_Rect * rects) |
270 { | 270 { |
271 #ifndef NO_SHARED_MEMORY | 271 #ifndef NO_SHARED_MEMORY |
272 int i; | 272 int i; |
273 | 273 |
274 for (i = 0; i < numrects; ++i) { | 274 for (i = 0; i < numrects; ++i) { |
275 if (rects[i].w == 0 || rects[i].h == 0) { /* Clipped? */ | 275 if (rects[i].w == 0 || rects[i].h == 0) { /* Clipped? */ |
276 continue; | 276 continue; |
277 } | 277 } |
278 XShmPutImage (GFX_Display, SDL_Window, SDL_GC, SDL_Ximage, | 278 XShmPutImage(GFX_Display, SDL_Window, SDL_GC, SDL_Ximage, |
279 rects[i].x, rects[i].y, | 279 rects[i].x, rects[i].y, |
280 rects[i].x, rects[i].y, rects[i].w, rects[i].h, False); | 280 rects[i].x, rects[i].y, rects[i].w, rects[i].h, False); |
281 } | 281 } |
282 if (SDL_VideoSurface->flags & SDL_ASYNCBLIT) { | 282 if (SDL_VideoSurface->flags & SDL_ASYNCBLIT) { |
283 XFlush (GFX_Display); | 283 XFlush(GFX_Display); |
284 blit_queued = 1; | 284 blit_queued = 1; |
285 } else { | 285 } else { |
286 XSync (GFX_Display, False); | 286 XSync(GFX_Display, False); |
287 } | 287 } |
288 #endif /* ! NO_SHARED_MEMORY */ | 288 #endif /* ! NO_SHARED_MEMORY */ |
289 } | 289 } |
290 | 290 |
291 /* There's a problem with the automatic refreshing of the display. | 291 /* There's a problem with the automatic refreshing of the display. |
295 This is a sort of a hacked workaround for the problem. | 295 This is a sort of a hacked workaround for the problem. |
296 */ | 296 */ |
297 static int enable_autorefresh = 1; | 297 static int enable_autorefresh = 1; |
298 | 298 |
299 void | 299 void |
300 X11_DisableAutoRefresh (_THIS) | 300 X11_DisableAutoRefresh(_THIS) |
301 { | 301 { |
302 --enable_autorefresh; | 302 --enable_autorefresh; |
303 } | 303 } |
304 | 304 |
305 void | 305 void |
306 X11_EnableAutoRefresh (_THIS) | 306 X11_EnableAutoRefresh(_THIS) |
307 { | 307 { |
308 ++enable_autorefresh; | 308 ++enable_autorefresh; |
309 } | 309 } |
310 | 310 |
311 void | 311 void |
312 X11_RefreshDisplay (_THIS) | 312 X11_RefreshDisplay(_THIS) |
313 { | 313 { |
314 /* Don't refresh a display that doesn't have an image (like GL) | 314 /* Don't refresh a display that doesn't have an image (like GL) |
315 Instead, post an expose event so the application can refresh. | 315 Instead, post an expose event so the application can refresh. |
316 */ | 316 */ |
317 if (!SDL_Ximage || (enable_autorefresh <= 0)) { | 317 if (!SDL_Ximage || (enable_autorefresh <= 0)) { |
318 SDL_PrivateExpose (); | 318 SDL_PrivateExpose(); |
319 return; | 319 return; |
320 } | 320 } |
321 #ifndef NO_SHARED_MEMORY | 321 #ifndef NO_SHARED_MEMORY |
322 if (this->UpdateRects == X11_MITSHMUpdate) { | 322 if (this->UpdateRects == X11_MITSHMUpdate) { |
323 XShmPutImage (SDL_Display, SDL_Window, SDL_GC, SDL_Ximage, | 323 XShmPutImage(SDL_Display, SDL_Window, SDL_GC, SDL_Ximage, |
324 0, 0, SDL_CurrentWindow.offset_x, | 324 0, 0, SDL_CurrentWindow.offset_x, |
325 SDL_CurrentWindow.offset_y, | 325 SDL_CurrentWindow.offset_y, |
326 SDL_CurrentDisplay.current_mode.w, | 326 SDL_CurrentDisplay.current_mode.w, |
327 SDL_CurrentDisplay.current_mode.h, False); | 327 SDL_CurrentDisplay.current_mode.h, False); |
328 } else | 328 } else |
329 #endif /* ! NO_SHARED_MEMORY */ | 329 #endif /* ! NO_SHARED_MEMORY */ |
330 { | 330 { |
331 XPutImage (SDL_Display, SDL_Window, SDL_GC, SDL_Ximage, | 331 XPutImage(SDL_Display, SDL_Window, SDL_GC, SDL_Ximage, |
332 0, 0, SDL_CurrentWindow.offset_x, | 332 0, 0, SDL_CurrentWindow.offset_x, |
333 SDL_CurrentWindow.offset_y, | 333 SDL_CurrentWindow.offset_y, |
334 SDL_CurrentDisplay.current_mode.w, | 334 SDL_CurrentDisplay.current_mode.w, |
335 SDL_CurrentDisplay.current_mode.h); | 335 SDL_CurrentDisplay.current_mode.h); |
336 } | 336 } |
337 XSync (SDL_Display, False); | 337 XSync(SDL_Display, False); |
338 } | 338 } |
339 | 339 |
340 /* vi: set ts=4 sw=4 expandtab: */ | 340 /* vi: set ts=4 sw=4 expandtab: */ |