Debian Codegraph @

src/win/wgl_disp.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * Windows OpenGL display driver
12 *
13 * By Milan Mimica.
14 * Based on AllegroGL Windows display driver.
15 *
16 */
17
18 #if 0
19 /* Raw input */
20 #define _WIN32_WINNT 0x0501
21 #ifndef WINVER
22 #define WINVER 0x0501
23 #endif
24 #endif
25
26 #include <windows.h>
27
28 #include "allegro5/allegro.h"
29 #include "allegro5/allegro_opengl.h"
30 #include "allegro5/system.h"
31 #include "allegro5/internal/aintern.h"
32 #include "allegro5/internal/aintern_bitmap.h"
33 #include "allegro5/internal/aintern_opengl.h"
34 #include "allegro5/internal/aintern_vector.h"
35 #include "allegro5/internal/aintern_wclipboard.h"
36 #include "allegro5/platform/aintwin.h"
37 #include "allegro5/internal/aintern_wunicode.h"
38
39 #include "wgl.h"
40
41 #include <process.h>
42
43 ALLEGRO_DEBUG_CHANNEL("display")
44
45 static ALLEGRO_DISPLAY_INTERFACE vt;
46
47 /* Forward declarations: */
48 static void display_thread_proc(void *arg);
49 static void destroy_display_internals(ALLEGRO_DISPLAY_WGL *wgl_disp);
50 static bool wgl_acknowledge_resize(ALLEGRO_DISPLAY *d);
51
52 /* Prevents switching to desktop resolution when destroying the display. Used
53 on full screen resize. */
54 static bool _wgl_do_not_change_display_mode = false;
55
56 /*
57 * These parameters cannot be gotten by the display thread because
58 * they're thread local. We get them in the calling thread first.
59 */
60 typedef struct WGL_DISPLAY_PARAMETERS {
61 ALLEGRO_DISPLAY_WGL *display;
62 volatile bool init_failed;
63 HANDLE AckEvent;
64 int window_x, window_y;
65 /* Not owned. */
66 const char* window_title;
67 } WGL_DISPLAY_PARAMETERS;
68
69 static bool is_wgl_extension_supported(_ALLEGRO_wglGetExtensionsStringARB_t _wglGetExtensionsStringARB, const char *extension, HDC dc)
70 {
71 bool ret = false;
72 const GLubyte* extensions;
73
74 if (_wglGetExtensionsStringARB) {
75 extensions = (const GLubyte*)_wglGetExtensionsStringARB(dc);
76 }
77 else {
78 /* XXX deprecated in OpenGL 3.0 */
79 extensions = glGetString(GL_EXTENSIONS);
80 }
81
82 if (extensions) {
83 ret = _al_ogl_look_for_an_extension(extension, extensions);
84 }
85
86 return ret;
87 }
88
89
90 static HGLRC init_temp_context(HWND wnd)
91 {
92 PIXELFORMATDESCRIPTOR pfd;
93 int pf;
94 HDC dc;
95 HGLRC glrc;
96
97 dc = GetDC(wnd);
98
99 memset(&pfd, 0, sizeof(pfd));
100 pfd.nSize = sizeof(pfd);
101 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL
102 | PFD_DOUBLEBUFFER_DONTCARE | PFD_STEREO_DONTCARE;
103 pfd.iPixelType = PFD_TYPE_RGBA;
104 pfd.iLayerType = PFD_MAIN_PLANE;
105 pfd.cColorBits = 32;
106
107 pf = ChoosePixelFormat(dc, &pfd);
108 if (!pf) {
109 ALLEGRO_ERROR("Unable to chose a temporary pixel format. %s\n",
110 _al_win_last_error());
111 return NULL;
112 }
113
114 memset(&pfd, 0, sizeof(pfd));
115 if (!SetPixelFormat(dc, pf, &pfd)) {
116 ALLEGRO_ERROR("Unable to set a temporary pixel format. %s\n",
117 _al_win_last_error());
118 return NULL;
119 }
120
121 glrc = wglCreateContext(dc);
122 if (!glrc) {
123 ALLEGRO_ERROR("Unable to create a render context. %s\n",
124 _al_win_last_error());
125 return NULL;
126 }
127
128 if (!wglMakeCurrent(dc, glrc)) {
129 ALLEGRO_ERROR("Unable to set the render context as current. %s\n",
130 _al_win_last_error());
131 wglDeleteContext(glrc);
132 return NULL;
133 }
134
135 return glrc;
136 }
137
138
139 static _ALLEGRO_wglGetPixelFormatAttribivARB_t _wglGetPixelFormatAttribivARB = NULL;
140 static _ALLEGRO_wglGetPixelFormatAttribivEXT_t _wglGetPixelFormatAttribivEXT = NULL;
141
142 static bool init_pixel_format_extensions(void)
143 {
144 /* Load the ARB_p_f symbol - Note, we shouldn't use the extension
145 * mechanism here, because it hasn't been initialized yet!
146 */
147 _wglGetPixelFormatAttribivARB =
148 (_ALLEGRO_wglGetPixelFormatAttribivARB_t)wglGetProcAddress("wglGetPixelFormatAttribivARB");
149 _wglGetPixelFormatAttribivEXT =
150 (_ALLEGRO_wglGetPixelFormatAttribivEXT_t)wglGetProcAddress("wglGetPixelFormatAttribivEXT");
151
152 if (!_wglGetPixelFormatAttribivARB && !_wglGetPixelFormatAttribivEXT) {
153 ALLEGRO_ERROR("WGL_ARB/EXT_pf not supported.\n");
154 return false;
155 }
156
157 return true;
158 }
159
160
161 static _ALLEGRO_wglCreateContextAttribsARB_t _wglCreateContextAttribsARB = NULL;
162
163 static bool init_context_creation_extensions(void)
164 {
165 _wglCreateContextAttribsARB =
166 (_ALLEGRO_wglCreateContextAttribsARB_t)wglGetProcAddress("wglCreateContextAttribsARB");
167
168 if (!_wglCreateContextAttribsARB) {
169 ALLEGRO_ERROR("wglCreateContextAttribs not supported!\n");
170 return false;
171 }
172
173 return true;
174 }
175
176
177 static int get_pixel_formats_count_old(HDC dc)
178 {
179 PIXELFORMATDESCRIPTOR pfd;
180 int ret;
181
182 ret = DescribePixelFormat(dc, 1, sizeof(pfd), &pfd);
183 if (!ret) {
184 ALLEGRO_ERROR("DescribePixelFormat failed! %s\n",
185 _al_win_last_error());
186 }
187
188 return ret;
189 }
190
191
192 static int get_pixel_formats_count_ext(HDC dc)
193 {
194 int attrib[1];
195 int value[1];
196
197 attrib[0] = WGL_NUMBER_PIXEL_FORMATS_ARB;
198 if ((_wglGetPixelFormatAttribivARB(dc, 0, 0, 1, attrib, value) == GL_FALSE)
199 && (_wglGetPixelFormatAttribivEXT(dc, 0, 0, 1, attrib, value) == GL_FALSE)) {
200 ALLEGRO_ERROR("WGL_ARB/EXT_pixel_format use failed! %s\n",
201 _al_win_last_error());
202 }
203
204 return value[0];
205 }
206
207
208 static void display_pixel_format(ALLEGRO_EXTRA_DISPLAY_SETTINGS *eds)
209 {
210 ALLEGRO_INFO("Accelerated: %s\n", eds->settings[ALLEGRO_RENDER_METHOD] ? "yes" : "no");
211 ALLEGRO_INFO("Single-buffer: %s\n", eds->settings[ALLEGRO_SINGLE_BUFFER] ? "yes" : "no");
212 if (eds->settings[ALLEGRO_SWAP_METHOD] > 0)
213 ALLEGRO_INFO("Swap method: %s\n", eds->settings[ALLEGRO_SWAP_METHOD] == 2 ? "flip" : "copy");
214 else
215 ALLEGRO_INFO("Swap method: undefined\n");
216 ALLEGRO_INFO("Color format: r%i g%i b%i a%i, %i bit\n",
217 eds->settings[ALLEGRO_RED_SIZE],
218 eds->settings[ALLEGRO_GREEN_SIZE],
219 eds->settings[ALLEGRO_BLUE_SIZE],
220 eds->settings[ALLEGRO_ALPHA_SIZE],
221 eds->settings[ALLEGRO_COLOR_SIZE]);
222 ALLEGRO_INFO("Depth buffer: %i bits\n", eds->settings[ALLEGRO_DEPTH_SIZE]);
223 ALLEGRO_INFO("Sample buffers: %s\n", eds->settings[ALLEGRO_SAMPLE_BUFFERS] ? "yes" : "no");
224 ALLEGRO_INFO("Samples: %i\n", eds->settings[ALLEGRO_SAMPLES]);
225 }
226
227
228 static int decode_pixel_format_old(PIXELFORMATDESCRIPTOR *pfd,
229 ALLEGRO_EXTRA_DISPLAY_SETTINGS *eds)
230 {
231 ALLEGRO_INFO("Decoding:\n");
232
233 /* Not interested if it doesn't support OpenGL and RGBA */
234 if (!(pfd->dwFlags & PFD_SUPPORT_OPENGL)) {
235 ALLEGRO_INFO("OpenGL Unsupported\n");
236 return false;
237 }
238 if (pfd->iPixelType != PFD_TYPE_RGBA) {
239 ALLEGRO_INFO("Not RGBA mode\n");
240 return false;
241 }
242
243 /* hardware acceleration */
244 if (((pfd->dwFlags & PFD_GENERIC_ACCELERATED) && (pfd->dwFlags & PFD_GENERIC_FORMAT))
245 || (!(pfd->dwFlags & PFD_GENERIC_ACCELERATED) && !(pfd->dwFlags & PFD_GENERIC_FORMAT)))
246 eds->settings[ALLEGRO_RENDER_METHOD] = 1;
247 else
248 eds->settings[ALLEGRO_RENDER_METHOD] = 0;
249
250 /* Depths of colour buffers */
251 eds->settings[ALLEGRO_RED_SIZE] = pfd->cRedBits;
252 eds->settings[ALLEGRO_GREEN_SIZE] = pfd->cGreenBits;
253 eds->settings[ALLEGRO_BLUE_SIZE] = pfd->cBlueBits;
254 eds->settings[ALLEGRO_ALPHA_SIZE] = pfd->cAlphaBits;
255
256 /* Depths of accumulation buffer */
257 eds->settings[ALLEGRO_ACC_RED_SIZE] = pfd->cAccumRedBits;
258 eds->settings[ALLEGRO_ACC_GREEN_SIZE] = pfd->cAccumGreenBits;
259 eds->settings[ALLEGRO_ACC_BLUE_SIZE] = pfd->cAccumBlueBits;
260 eds->settings[ALLEGRO_ACC_ALPHA_SIZE] = pfd->cAccumAlphaBits;
261
262 /* Miscellaneous settings */
263 eds->settings[ALLEGRO_SINGLE_BUFFER] = !(pfd->dwFlags & PFD_DOUBLEBUFFER);
264 eds->settings[ALLEGRO_DEPTH_SIZE] = pfd->cDepthBits;
265 eds->settings[ALLEGRO_STENCIL_SIZE] = pfd->cStencilBits;
266 eds->settings[ALLEGRO_COLOR_SIZE] = pfd->cColorBits;
267 eds->settings[ALLEGRO_STEREO] = pfd->dwFlags & PFD_STEREO;
268 eds->settings[ALLEGRO_AUX_BUFFERS] = pfd->cAuxBuffers;
269
270 /* These are the component shifts. */
271 eds->settings[ALLEGRO_RED_SHIFT] = pfd->cRedShift;
272 eds->settings[ALLEGRO_GREEN_SHIFT] = pfd->cGreenShift;
273 eds->settings[ALLEGRO_BLUE_SHIFT] = pfd->cBlueShift;
274 eds->settings[ALLEGRO_ALPHA_SHIFT] = pfd->cAlphaShift;
275
276 /* Multisampling isn't supported under Windows if we don't also use
277 * WGL_ARB_pixel_format or WGL_EXT_pixel_format.
278 */
279 eds->settings[ALLEGRO_SAMPLE_BUFFERS] = 0;
280 eds->settings[ALLEGRO_SAMPLES] = 0;
281
282 /* Swap method can't be detected without WGL_ARB_pixel_format or
283 * WGL_EXT_pixel_format
284 */
285 eds->settings[ALLEGRO_SWAP_METHOD] = 0;
286
287 /* Float depth/color isn't supported under Windows if we don't also use
288 * AGL_ARB_pixel_format or WGL_EXT_pixel_format.
289 */
290 eds->settings[ALLEGRO_FLOAT_COLOR] = 0;
291 eds->settings[ALLEGRO_FLOAT_DEPTH] = 0;
292
293 // FIXME
294
295 eds->settings[ALLEGRO_COMPATIBLE_DISPLAY] = 1;
296
297 return true;
298 }
299
300
301 static bool decode_pixel_format_attrib(ALLEGRO_EXTRA_DISPLAY_SETTINGS *eds, int num_attribs,
302 const int *attrib, const int *value)
303 {
304 int i;
305
306 ALLEGRO_INFO("Decoding:\n");
307
308 eds->settings[ALLEGRO_SAMPLES] = 0;
309 eds->settings[ALLEGRO_SAMPLE_BUFFERS] = 0;
310 eds->settings[ALLEGRO_FLOAT_DEPTH] = 0;
311 eds->settings[ALLEGRO_FLOAT_COLOR] = 0;
312 eds->settings[ALLEGRO_COMPATIBLE_DISPLAY] = 1;
313
314 for (i = 0; i < num_attribs; i++) {
315 /* Not interested if it doesn't support OpenGL or window drawing or RGBA. */
316 if (attrib[i] == WGL_SUPPORT_OPENGL_ARB && value[i] == 0) {
317 ALLEGRO_INFO("OpenGL Unsupported\n");
318 return false;
319 }
320 else if (attrib[i] == WGL_DRAW_TO_WINDOW_ARB && value[i] == 0) {
321 ALLEGRO_INFO("Can't draw to window\n");
322 return false;
323 }
324 else if (attrib[i] == WGL_PIXEL_TYPE_ARB
325 && (value[i] != WGL_TYPE_RGBA_ARB && value[i] != WGL_TYPE_RGBA_FLOAT_ARB)) {
326 ALLEGRO_INFO("Not RGBA mode\n");
327 return false;
328 }
329 /* hardware acceleration */
330 else if (attrib[i] == WGL_ACCELERATION_ARB) {
331 eds->settings[ALLEGRO_RENDER_METHOD] = (value[i] == WGL_NO_ACCELERATION_ARB) ? 0 : 1;
332 }
333 /* Depths of colour buffers */
334 else if (attrib[i] == WGL_RED_BITS_ARB) {
335 eds->settings[ALLEGRO_RED_SIZE] = value[i];
336 }
337 else if (attrib[i] == WGL_GREEN_BITS_ARB) {
338 eds->settings[ALLEGRO_GREEN_SIZE] = value[i];
339 }
340 else if (attrib[i] == WGL_BLUE_BITS_ARB) {
341 eds->settings[ALLEGRO_BLUE_SIZE] = value[i];
342 }
343 else if (attrib[i] == WGL_ALPHA_BITS_ARB) {
344 eds->settings[ALLEGRO_ALPHA_SIZE] = value[i];
345 }
346 /* Shift of color components */
347 else if (attrib[i] == WGL_RED_SHIFT_ARB) {
348 eds->settings[ALLEGRO_RED_SHIFT] = value[i];
349 }
350 else if (attrib[i] == WGL_GREEN_SHIFT_ARB) {
351 eds->settings[ALLEGRO_GREEN_SHIFT] = value[i];
352 }
353 else if (attrib[i] == WGL_BLUE_SHIFT_ARB) {
354 eds->settings[ALLEGRO_BLUE_SHIFT] = value[i];
355 }
356 else if (attrib[i] == WGL_ALPHA_SHIFT_ARB) {
357 eds->settings[ALLEGRO_ALPHA_SHIFT] = value[i];
358 }
359 /* Miscellaneous settings */
360 else if (attrib[i] == WGL_DOUBLE_BUFFER_ARB) {
361 eds->settings[ALLEGRO_SINGLE_BUFFER] = !(value[i]);
362 }
363 else if (attrib[i] == WGL_SWAP_METHOD_ARB) {
364 if (value[i] == WGL_SWAP_UNDEFINED_ARB)
365 eds->settings[ALLEGRO_SWAP_METHOD] = 0;
366 else if (value[i] == WGL_SWAP_COPY_ARB)
367 eds->settings[ALLEGRO_SWAP_METHOD] = 1;
368 else if (value[i] == WGL_SWAP_EXCHANGE_ARB)
369 eds->settings[ALLEGRO_SWAP_METHOD] = 2;
370 }
371
372 else if (attrib[i] == WGL_STEREO_ARB) {
373 eds->settings[ALLEGRO_STEREO] = value[i];
374 }
375 else if (attrib[i] == WGL_AUX_BUFFERS_ARB) {
376 eds->settings[ALLEGRO_AUX_BUFFERS] = value[i];
377 }
378 else if (attrib[i] == WGL_STENCIL_BITS_ARB) {
379 eds->settings[ALLEGRO_STENCIL_SIZE] = value[i];
380 }
381 /* Depths of accumulation buffer */
382 else if (attrib[i] == WGL_ACCUM_RED_BITS_ARB) {
383 eds->settings[ALLEGRO_ACC_RED_SIZE] = value[i];
384 }
385 else if (attrib[i] == WGL_ACCUM_GREEN_BITS_ARB) {
386 eds->settings[ALLEGRO_ACC_GREEN_SIZE] = value[i];
387 }
388 else if (attrib[i] == WGL_ACCUM_BLUE_BITS_ARB) {
389 eds->settings[ALLEGRO_ACC_BLUE_SIZE] = value[i];
390 }
391 else if (attrib[i] == WGL_ACCUM_ALPHA_BITS_ARB) {
392 eds->settings[ALLEGRO_ACC_ALPHA_SIZE] = value[i];
393 }
394
395 else if (attrib[i] == WGL_DEPTH_BITS_ARB) {
396 eds->settings[ALLEGRO_DEPTH_SIZE] = value[i];
397 }
398 else if (attrib[i] == WGL_COLOR_BITS_ARB) {
399 eds->settings[ALLEGRO_COLOR_SIZE] = value[i];
400 }
401 /* Multisampling bits */
402 else if (attrib[i] == WGL_SAMPLE_BUFFERS_ARB) {
403 eds->settings[ALLEGRO_SAMPLE_BUFFERS] = value[i];
404 }
405 else if (attrib[i] == WGL_SAMPLES_ARB) {
406 eds->settings[ALLEGRO_SAMPLES] = value[i];
407 }
408 /* Float color */
409 if (attrib[i] == WGL_PIXEL_TYPE_ARB && value[i] == WGL_TYPE_RGBA_FLOAT_ARB) {
410 eds->settings[ALLEGRO_FLOAT_COLOR] = true;
411 }
412 /* Float depth */
413 else if (attrib[i] == WGL_DEPTH_FLOAT_EXT) {
414 eds->settings[ALLEGRO_FLOAT_DEPTH] = value[i];
415 }
416 }
417
418 return true;
419 }
420
421
422 static ALLEGRO_EXTRA_DISPLAY_SETTINGS* read_pixel_format_old(int fmt, HDC dc)
423 {
424 ALLEGRO_EXTRA_DISPLAY_SETTINGS *eds = NULL;
425 PIXELFORMATDESCRIPTOR pfd;
426 int result;
427
428 result = DescribePixelFormat(dc, fmt+1, sizeof(pfd), &pfd);
429 if (!result) {
430 ALLEGRO_WARN("DescribePixelFormat() failed. %s\n", _al_win_last_error());
431 return NULL;
432 }
433
434 eds = al_calloc(1, sizeof *eds);
435 if (!decode_pixel_format_old(&pfd, eds)) {
436 al_free(eds);
437 return NULL;
438 }
439
440 return eds;
441 }
442
443
444 static ALLEGRO_EXTRA_DISPLAY_SETTINGS* read_pixel_format_ext(_ALLEGRO_wglGetExtensionsStringARB_t _wglGetExtensionsStringARB, int fmt, HDC dc)
445 {
446 ALLEGRO_EXTRA_DISPLAY_SETTINGS *eds = NULL;
447
448 /* Note: Even though we use te ARB suffix, all those enums are compatible
449 * with EXT_pixel_format.
450 */
451 int attrib[] = {
452 WGL_SUPPORT_OPENGL_ARB,
453 WGL_DRAW_TO_WINDOW_ARB,
454 WGL_PIXEL_TYPE_ARB,
455 WGL_ACCELERATION_ARB,
456 WGL_DOUBLE_BUFFER_ARB,
457 WGL_DEPTH_BITS_ARB,
458 WGL_SWAP_METHOD_ARB,
459 WGL_COLOR_BITS_ARB,
460 WGL_RED_BITS_ARB,
461 WGL_GREEN_BITS_ARB,
462 WGL_BLUE_BITS_ARB,
463 WGL_ALPHA_BITS_ARB,
464 WGL_RED_SHIFT_ARB,
465 WGL_GREEN_SHIFT_ARB,
466 WGL_BLUE_SHIFT_ARB,
467 WGL_ALPHA_SHIFT_ARB,
468 WGL_STENCIL_BITS_ARB,
469 WGL_STEREO_ARB,
470 WGL_ACCUM_BITS_ARB,
471 WGL_ACCUM_RED_BITS_ARB,
472 WGL_ACCUM_GREEN_BITS_ARB,
473 WGL_ACCUM_BLUE_BITS_ARB,
474 WGL_ACCUM_ALPHA_BITS_ARB,
475 WGL_AUX_BUFFERS_ARB,
476
477 /* The following are used by extensions that add to WGL_pixel_format.
478 * If WGL_p_f isn't supported though, we can't use the (then invalid)
479 * enums. We can't use any magic number either, so we settle for
480 * replicating one. The pixel format decoder
481 * (decode_pixel_format_attrib()) doesn't care about duplicates.
482 */
483 WGL_AUX_BUFFERS_ARB, /* placeholder for WGL_SAMPLE_BUFFERS_ARB */
484 WGL_AUX_BUFFERS_ARB, /* placeholder for WGL_SAMPLES_ARB */
485 WGL_AUX_BUFFERS_ARB, /* placeholder for WGL_DEPTH_FLOAT_EXT */
486 };
487
488 const int num_attribs = sizeof(attrib) / sizeof(attrib[0]);
489 int *value = (int*)al_malloc(sizeof(int) * num_attribs);
490 int ret;
491
492 if (!value)
493 return NULL;
494
495 /* If multisampling is supported, query for it. */
496 if (is_wgl_extension_supported(_wglGetExtensionsStringARB, "WGL_ARB_multisample", dc)) {
497 attrib[num_attribs - 3] = WGL_SAMPLE_BUFFERS_ARB;
498 attrib[num_attribs - 2] = WGL_SAMPLES_ARB;
499 }
500 if (is_wgl_extension_supported(_wglGetExtensionsStringARB, "WGL_EXT_depth_float", dc)) {
501 attrib[num_attribs - 1] = WGL_DEPTH_FLOAT_EXT;
502 }
503
504 /* Get the pf attributes */
505 if (_wglGetPixelFormatAttribivARB) {
506 ret = _wglGetPixelFormatAttribivARB(dc, fmt+1, 0, num_attribs, attrib, value);
507 }
508 else if (_wglGetPixelFormatAttribivEXT) {
509 ret = _wglGetPixelFormatAttribivEXT(dc, fmt+1, 0, num_attribs, attrib, value);
510 }
511 else {
512 ret = 0;
513 }
514
515 if (!ret) {
516 ALLEGRO_ERROR("wglGetPixelFormatAttrib failed! %s\n", _al_win_last_error());
517 al_free(value);
518 return NULL;
519 }
520
521 eds = al_calloc(1, sizeof *eds);
522 if (!decode_pixel_format_attrib(eds, num_attribs, attrib, value)) {
523 al_free(eds);
524 eds = NULL;
525 }
526
527 al_free(value);
528
529 /* Hack: for some reason this happens for me under Wine. */
530 if (eds &&
531 eds->settings[ALLEGRO_RED_SHIFT] == 0 &&
532 eds->settings[ALLEGRO_GREEN_SHIFT] == 0 &&
533 eds->settings[ALLEGRO_BLUE_SHIFT] == 0 &&
534 eds->settings[ALLEGRO_ALPHA_SHIFT] == 0) {
535 eds->settings[ALLEGRO_RED_SHIFT] = 0;
536 eds->settings[ALLEGRO_GREEN_SHIFT] = 8;
537 eds->settings[ALLEGRO_BLUE_SHIFT] = 16;
538 eds->settings[ALLEGRO_ALPHA_SHIFT] = 24;
539 }
540
541 return eds;
542 }
543
544
545 static bool change_display_mode(ALLEGRO_DISPLAY *d)
546 {
547 DEVMODE dm;
548 DEVMODE fallback_dm;
549 DISPLAY_DEVICE dd;
550 TCHAR* dev_name = NULL;
551 int i, modeswitch, result;
552 int fallback_dm_valid = 0;
553 int bpp;
554 int adapter = al_get_new_display_adapter();
555
556 if (adapter >= 0) {
557 memset(&dd, 0, sizeof(dd));
558 dd.cb = sizeof(dd);
559 if (EnumDisplayDevices(NULL, adapter, &dd, 0) == false)
560 return false;
561 dev_name = dd.DeviceName;
562 }
563
564 memset(&fallback_dm, 0, sizeof(fallback_dm));
565 memset(&dm, 0, sizeof(dm));
566 dm.dmSize = sizeof(DEVMODE);
567
568 bpp = d->extra_settings.settings[ALLEGRO_COLOR_SIZE];
569 if (!bpp)
570 bpp = 32;
571
572 i = 0;
573 do {
574 modeswitch = EnumDisplaySettings(dev_name, i, &dm);
575 if (!modeswitch)
576 break;
577
578 if ((dm.dmPelsWidth == (unsigned) d->w)
579 && (dm.dmPelsHeight == (unsigned) d->h)
580 && (dm.dmBitsPerPel == (unsigned) bpp)
581 && (dm.dmDisplayFrequency != (unsigned) d->refresh_rate))
582 {
583 /* Keep it as fallback if refresh rate request could not
584 * be satisfied. Try to get as close to 60Hz as possible though,
585 * it's a bit better for a fallback than just blindly picking
586 * something like 47Hz or 200Hz.
587 */
588 if (!fallback_dm_valid) {
589 fallback_dm = dm;
590 fallback_dm_valid = 1;
591 }
592 else if (dm.dmDisplayFrequency >= 60) {
593 if (dm.dmDisplayFrequency < fallback_dm.dmDisplayFrequency) {
594 fallback_dm = dm;
595 }
596 }
597 }
598 i++;
599 }
600 while ((dm.dmPelsWidth != (unsigned) d->w)
601 || (dm.dmPelsHeight != (unsigned) d->h)
602 || (dm.dmBitsPerPel != (unsigned) bpp)
603 || (dm.dmDisplayFrequency != (unsigned) d->refresh_rate));
604
605 if (!modeswitch && !fallback_dm_valid) {
606 ALLEGRO_ERROR("Mode not found.\n");
607 return false;
608 }
609
610 if (!modeswitch && fallback_dm_valid)
611 dm = fallback_dm;
612
613 dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
614 result = ChangeDisplaySettingsEx(dev_name, &dm, NULL, CDS_FULLSCREEN, 0);
615
616 d->refresh_rate = dm.dmDisplayFrequency;
617
618 if (result != DISP_CHANGE_SUCCESSFUL) {
619 ALLEGRO_ERROR("Unable to set mode. %s\n", _al_win_last_error());
620 return false;
621 }
622
623 ALLEGRO_INFO("Mode seccessfuly set.\n");
624 return true;
625 }
626
627
628 static HGLRC init_ogl_context_ex(HDC dc, bool fc, int major, int minor)
629 {
630 HWND testwnd = NULL;
631 HDC testdc = NULL;
632 HGLRC testrc = NULL;
633 HGLRC old_rc = NULL;
634 HDC old_dc = NULL;
635 HGLRC glrc = NULL;
636
637 testwnd = _al_win_create_hidden_window();
638 if (!testwnd)
639 return NULL;
640
641 old_rc = wglGetCurrentContext();
642 old_dc = wglGetCurrentDC();
643
644 testdc = GetDC(testwnd);
645 testrc = init_temp_context(testwnd);
646 if (!testrc)
647 goto bail;
648
649 _ALLEGRO_wglGetExtensionsStringARB_t _wglGetExtensionsStringARB
650 = (_ALLEGRO_wglGetExtensionsStringARB_t)wglGetProcAddress("wglGetExtensionsStringARB");
651 ALLEGRO_INFO("_wglGetExtensionsStringARB %p\n", _wglGetExtensionsStringARB);
652
653 if (is_wgl_extension_supported(_wglGetExtensionsStringARB, "WGL_ARB_create_context", testdc)) {
654 int attrib[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, major,
655 WGL_CONTEXT_MINOR_VERSION_ARB, minor,
656 WGL_CONTEXT_FLAGS_ARB, 0,
657 0};
658 if (fc)
659 attrib[5] = WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
660 if (!init_context_creation_extensions())
661 goto bail;
662 /* TODO: we could use the context sharing feature */
663 glrc = _wglCreateContextAttribsARB(dc, 0, attrib);
664 }
665 else
666 goto bail;
667
668 bail:
669 wglMakeCurrent(NULL, NULL);
670 if (testrc) {
671 wglDeleteContext(testrc);
672 }
673
674 wglMakeCurrent(old_dc, old_rc);
675
676 _wglCreateContextAttribsARB = NULL;
677
678 if (testwnd) {
679 ReleaseDC(testwnd, testdc);
680 DestroyWindow(testwnd);
681 }
682
683 return glrc;
684 }
685
686
687 static ALLEGRO_EXTRA_DISPLAY_SETTINGS** get_available_pixel_formats_ext(int *count)
688 {
689 HWND testwnd = NULL;
690 HDC testdc = NULL;
691 HGLRC testrc = NULL;
692 HGLRC old_rc = NULL;
693 HDC old_dc = NULL;
694 ALLEGRO_EXTRA_DISPLAY_SETTINGS **eds_list = NULL;
695 ALLEGRO_EXTRA_DISPLAY_SETTINGS *ref;
696 int maxindex;
697 int i, j;
698
699 *count = 0;
700 ref = _al_get_new_display_settings();
701
702 /* We need to create a dummy window with a pixel format to get the
703 * list of valid PFDs
704 */
705 testwnd = _al_win_create_hidden_window();
706 if (!testwnd)
707 return false;
708
709 old_rc = wglGetCurrentContext();
710 old_dc = wglGetCurrentDC();
711
712 testdc = GetDC(testwnd);
713 testrc = init_temp_context(testwnd);
714 if (!testrc)
715 goto bail;
716
717 _ALLEGRO_wglGetExtensionsStringARB_t _wglGetExtensionsStringARB
718 = (_ALLEGRO_wglGetExtensionsStringARB_t)wglGetProcAddress("wglGetExtensionsStringARB");
719 ALLEGRO_INFO("_wglGetExtensionsStringARB %p\n", _wglGetExtensionsStringARB);
720
721 if (!is_wgl_extension_supported(_wglGetExtensionsStringARB, "WGL_ARB_pixel_format", testdc) &&
722 !is_wgl_extension_supported(_wglGetExtensionsStringARB, "WGL_EXT_pixel_format", testdc)) {
723 ALLEGRO_ERROR("WGL_ARB/EXT_pf not supported.\n");
724 goto bail;
725 }
726
727 if (!init_pixel_format_extensions())
728 goto bail;
729
730 maxindex = get_pixel_formats_count_ext(testdc);
731 if (maxindex < 1)
732 goto bail;
733
734 ALLEGRO_INFO("Got %i visuals.\n", maxindex);
735
736 eds_list = al_calloc(maxindex, sizeof(*eds_list));
737 if (!eds_list)
738 goto bail;
739
740 for (j = i = 0; i < maxindex; i++) {
741 ALLEGRO_INFO("-- \n");
742 ALLEGRO_INFO("Decoding visual no. %i...\n", i+1);
743 eds_list[j] = read_pixel_format_ext(_wglGetExtensionsStringARB, i, testdc);
744 if (!eds_list[j])
745 continue;
746 // Fill vsync setting here and enable/disable it after display creation
747 eds_list[j]->settings[ALLEGRO_VSYNC] = ref->settings[ALLEGRO_VSYNC];
748 display_pixel_format(eds_list[j]);
749 eds_list[j]->score = _al_score_display_settings(eds_list[j], ref);
750 if (eds_list[j]->score == -1) {
751 al_free(eds_list[j]);
752 eds_list[j] = NULL;
753 continue;
754 }
755 /* In WinAPI first index is 1 ::) */
756 eds_list[j]->index = i+1;
757 j++;
758 }
759
760 ALLEGRO_INFO("%i visuals are good enough.\n", j);
761 *count = j;
762
763 bail:
764 wglMakeCurrent(NULL, NULL);
765 if (testrc) {
766 wglDeleteContext(testrc);
767 }
768
769 wglMakeCurrent(old_dc, old_rc);
770
771 _wglGetPixelFormatAttribivARB = NULL;
772 _wglGetPixelFormatAttribivEXT = NULL;
773
774 if (testwnd) {
775 ReleaseDC(testwnd, testdc);
776 DestroyWindow(testwnd);
777 }
778
779 return eds_list;
780 }
781
782
783 static ALLEGRO_EXTRA_DISPLAY_SETTINGS** get_available_pixel_formats_old(int *count, HDC dc)
784 {
785 ALLEGRO_EXTRA_DISPLAY_SETTINGS **eds_list = NULL;
786 ALLEGRO_EXTRA_DISPLAY_SETTINGS *ref;
787 int maxindex;
788 int i, j;
789
790 *count = 0;
791 ref = _al_get_new_display_settings();
792
793 maxindex = get_pixel_formats_count_old(dc);
794 if (maxindex < 1)
795 return NULL;
796
797 ALLEGRO_INFO("Got %i visuals.\n", maxindex);
798
799 eds_list = al_calloc(maxindex, sizeof(*eds_list));
800 if (!eds_list)
801 return NULL;
802
803 for (j = i = 0; i < maxindex; i++) {
804 ALLEGRO_INFO("-- \n");
805 ALLEGRO_INFO("Decoding visual no. %i...\n", i+1);
806 eds_list[j] = read_pixel_format_old(i, dc);
807 if (!eds_list[j])
808 continue;
809 #ifdef DEBUGMODE
810 display_pixel_format(eds_list[j]);
811 #endif
812 eds_list[j]->score = _al_score_display_settings(eds_list[j], ref);
813 if (eds_list[j]->score == -1) {
814 al_free(eds_list[j]);
815 eds_list[j] = NULL;
816 continue;
817 }
818 /* In WinAPI first index is 1 ::) */
819 eds_list[j]->index = i+1;
820 j++;
821 }
822
823 ALLEGRO_INFO("%i visuals are good enough.\n", j);
824 *count = j;
825
826 return eds_list;
827 }
828
829
830 static bool select_pixel_format(ALLEGRO_DISPLAY_WGL *d, HDC dc)
831 {
832 ALLEGRO_EXTRA_DISPLAY_SETTINGS **eds = NULL;
833 ALLEGRO_CONFIG *sys_cfg = al_get_system_config();
834 int eds_count = 0;
835 int i;
836 bool force_old = false;
837 const char *selection_mode;
838
839 selection_mode = al_get_config_value(sys_cfg, "graphics",
840 "config_selection");
841 if (selection_mode && selection_mode[0] != '\0') {
842 if (!_al_stricmp(selection_mode, "old")) {
843 ALLEGRO_INFO("Forcing OLD visual selection method.\n");
844 force_old = true;
845 }
846 else if (!_al_stricmp(selection_mode, "new"))
847 force_old = false;
848 }
849
850 if (!force_old)
851 eds = get_available_pixel_formats_ext(&eds_count);
852 if (!eds)
853 eds = get_available_pixel_formats_old(&eds_count, dc);
854
855 if (!eds || !eds_count) {
856 ALLEGRO_ERROR("Didn't find any suitable pixel format!\n");
857 return false;
858 }
859
860 qsort(eds, eds_count, sizeof(eds[0]), _al_display_settings_sorter);
861
862 for (i = 0; i < eds_count ; i++) {
863 if (SetPixelFormat(d->dc, eds[i]->index, NULL)) {
864 ALLEGRO_INFO("Chose visual no. %i\n\n", eds[i]->index);
865 display_pixel_format(eds[i]);
866 break;
867 }
868 else {
869 ALLEGRO_WARN("Unable to set pixel format! %s\n", _al_win_last_error());
870 ALLEGRO_WARN("Trying next one.\n");
871 }
872 }
873
874 if (i == eds_count) {
875 ALLEGRO_ERROR("Unable to set any pixel format! %s\n", _al_win_last_error());
876 for (i = 0; i < eds_count; i++)
877 al_free(eds[i]);
878 al_free(eds);
879 return false;
880 }
881
882 memcpy(&d->win_display.display.extra_settings, eds[i], sizeof(ALLEGRO_EXTRA_DISPLAY_SETTINGS));
883
884 for (i = 0; i < eds_count; i++)
885 al_free(eds[i]);
886 if (eds)
887 al_free(eds);
888
889 return true;
890 }
891
892 static bool create_display_internals(ALLEGRO_DISPLAY_WGL *wgl_disp)
893 {
894 ALLEGRO_DISPLAY *disp = (void*)wgl_disp;
895 ALLEGRO_DISPLAY_WIN *win_disp = (void*)wgl_disp;
896 WGL_DISPLAY_PARAMETERS ndp;
897 int window_x, window_y;
898 int major, minor;
899
900 /* The window is created in a separate thread so we need to pass this
901 * TLS on
902 */
903 al_get_new_window_position(&window_x, &window_y);
904 ndp.window_x = window_x;
905 ndp.window_y = window_y;
906 ndp.window_title = al_get_new_window_title();
907
908 /* _beginthread closes the handle automatically. */
909 ndp.display = wgl_disp;
910 ndp.init_failed = true;
911 ndp.AckEvent = CreateEvent(NULL, false, false, NULL);
912 _beginthread(display_thread_proc, 0, &ndp);
913
914 /* Wait some _finite_ time (10 secs or so) for display thread to init, and
915 * give up if something horrible happened to it, unless we're in debug mode
916 * and we may have intentionally stopped the execution to analyze the code.
917 */
918 #ifdef DEBUGMODE
919 WaitForSingleObject(ndp.AckEvent, INFINITE);
920 #else
921 WaitForSingleObject(ndp.AckEvent, 10*1000);
922 #endif
923
924 CloseHandle(ndp.AckEvent);
925
926 if (ndp.init_failed) {
927 ALLEGRO_ERROR("Failed to create display.\n");
928 return false;
929 }
930
931 /* WGL display lists cannot be shared with the API currently in use. */
932 disp->ogl_extras->is_shared = false;
933
934 if (!select_pixel_format(wgl_disp, wgl_disp->dc)) {
935 destroy_display_internals(wgl_disp);
936 return false;
937 }
938
939 major = al_get_new_display_option(ALLEGRO_OPENGL_MAJOR_VERSION, 0);
940 minor = al_get_new_display_option(ALLEGRO_OPENGL_MINOR_VERSION, 0);
941
942 // TODO: request GLES context in GLES builds
943 if ((disp->flags & ALLEGRO_OPENGL_3_0) || major != 0) {
944 if (major == 0)
945 major = 3;
946 bool fc = (disp->flags & ALLEGRO_OPENGL_FORWARD_COMPATIBLE) != 0;
947 wgl_disp->glrc = init_ogl_context_ex(wgl_disp->dc, fc, major,
948 minor);
949 }
950 else {
951 wgl_disp->glrc = wglCreateContext(wgl_disp->dc);
952 }
953
954 if (!wgl_disp->glrc) {
955 ALLEGRO_ERROR("Unable to create a render context! %s\n", _al_win_last_error());
956 destroy_display_internals(wgl_disp);
957 return false;
958 }
959
960 /* make the context the current one */
961 if (!wglMakeCurrent(wgl_disp->dc, wgl_disp->glrc)) {
962 ALLEGRO_ERROR("Unable to make the context current! %s\n", _al_win_last_error());
963 destroy_display_internals(wgl_disp);
964 return false;
965 }
966
967 _al_ogl_manage_extensions(disp);
968 _al_ogl_set_extensions(disp->ogl_extras->extension_api);
969
970 if (disp->ogl_extras->ogl_info.version < _ALLEGRO_OPENGL_VERSION_1_2) {
971 ALLEGRO_EXTRA_DISPLAY_SETTINGS *eds = _al_get_new_display_settings();
972 if (eds->required & (1<<ALLEGRO_COMPATIBLE_DISPLAY)) {
973 ALLEGRO_WARN("Allegro requires at least OpenGL version 1.2 to work.\n");
974 destroy_display_internals(wgl_disp);
975 return false;
976 }
977 disp->extra_settings.settings[ALLEGRO_COMPATIBLE_DISPLAY] = 0;
978 }
979
980 /* Fill in the display settings for opengl major and minor versions...*/
981 const int v = disp->ogl_extras->ogl_info.version;
982 disp->extra_settings.settings[ALLEGRO_OPENGL_MAJOR_VERSION] = (v >> 24) & 0xFF;
983 disp->extra_settings.settings[ALLEGRO_OPENGL_MINOR_VERSION] = (v >> 16) & 0xFF;
984
985 /* Try to enable or disable vsync as requested */
986 /* NOTE: my drivers claim I don't have WGL_EXT_swap_control
987 * (according to al_have_opengl_extension), but wglSwapIntervalEXT
988 * does get loaded, so just check for that.
989 */
990 if (wglSwapIntervalEXT) {
991 if (disp->extra_settings.settings[ALLEGRO_VSYNC] == 1) {
992 wglSwapIntervalEXT(1);
993 }
994 else if (disp->extra_settings.settings[ALLEGRO_VSYNC] == 2) {
995 wglSwapIntervalEXT(0);
996 }
997 }
998
999 win_disp->mouse_selected_hcursor = 0;
1000 win_disp->mouse_cursor_shown = false;
1001 win_disp->hide_mouse_on_move = false;
1002
1003 _al_win_grab_input(win_disp);
1004
1005 if (disp->extra_settings.settings[ALLEGRO_COMPATIBLE_DISPLAY])
1006 _al_ogl_setup_gl(disp);
1007
1008 return true;
1009 }
1010
1011
1012 static ALLEGRO_DISPLAY* wgl_create_display(int w, int h)
1013 {
1014 ALLEGRO_SYSTEM_WIN *system = (ALLEGRO_SYSTEM_WIN *)al_get_system_driver();
1015 ALLEGRO_DISPLAY_WGL **add;
1016 ALLEGRO_DISPLAY_WGL *wgl_display = al_calloc(1, sizeof *wgl_display);
1017 ALLEGRO_DISPLAY *ogl_display = (void*)wgl_display;
1018 ALLEGRO_DISPLAY *display = (void*)ogl_display;
1019 ALLEGRO_DISPLAY_WIN *win_disp = (ALLEGRO_DISPLAY_WIN *)display;
1020
1021 win_disp->adapter = _al_win_determine_adapter();
1022
1023 display->w = w;
1024 display->h = h;
1025 display->refresh_rate = al_get_new_display_refresh_rate();
1026 display->flags = al_get_new_display_flags();
1027 #ifdef ALLEGRO_CFG_OPENGLES2
1028 display->flags |= ALLEGRO_PROGRAMMABLE_PIPELINE;
1029 #endif
1030 #ifdef ALLEGRO_CFG_OPENGLES
1031 display->flags |= ALLEGRO_OPENGL_ES_PROFILE;
1032 #endif
1033 display->vt = &vt;
1034
1035 display->ogl_extras = al_calloc(1, sizeof(ALLEGRO_OGL_EXTRAS));
1036
1037 if (!create_display_internals(wgl_display)) {
1038 al_free(display->ogl_extras);
1039 al_free(display);
1040 return NULL;
1041 }
1042
1043 /* Print out OpenGL version info */
1044 ALLEGRO_INFO("OpenGL Version: %s\n", (const char*)glGetString(GL_VERSION));
1045 ALLEGRO_INFO("Vendor: %s\n", (const char*)glGetString(GL_VENDOR));
1046 ALLEGRO_INFO("Renderer: %s\n\n", (const char*)glGetString(GL_RENDERER));
1047
1048 /* Add ourself to the list of displays. */
1049 add = _al_vector_alloc_back(&system->system.displays);
1050 *add = wgl_display;
1051
1052 /* Each display is an event source. */
1053 _al_event_source_init(&display->es);
1054
1055 _al_win_set_system_mouse_cursor(display, ALLEGRO_SYSTEM_MOUSE_CURSOR_ARROW);
1056 _al_win_show_mouse_cursor(display);
1057
1058 _al_win_post_create_window(display);
1059
1060 return display;
1061 }
1062
1063
1064 static void destroy_display_internals(ALLEGRO_DISPLAY_WGL *wgl_disp)
1065 {
1066 ALLEGRO_DISPLAY *disp = (ALLEGRO_DISPLAY *)wgl_disp;
1067 ALLEGRO_DISPLAY_WIN *win_disp = (ALLEGRO_DISPLAY_WIN *)wgl_disp;
1068
1069 /* We need to convert all our bitmaps to display independent (memory)
1070 * bitmaps because WGL driver doesn't support sharing of resources. */
1071 while (disp->bitmaps._size > 0) {
1072 ALLEGRO_BITMAP **bptr = _al_vector_ref_back(&disp->bitmaps);
1073 ALLEGRO_BITMAP *bmp = *bptr;
1074 _al_convert_to_memory_bitmap(bmp);
1075 }
1076
1077 if (disp->ogl_extras->backbuffer)
1078 _al_ogl_destroy_backbuffer(disp->ogl_extras->backbuffer);
1079 disp->ogl_extras->backbuffer = NULL;
1080
1081 _al_ogl_unmanage_extensions(disp);
1082
1083 PostMessage(win_disp->window, _al_win_msg_suicide, (WPARAM)win_disp, 0);
1084
1085 while (!win_disp->thread_ended)
1086 al_rest(0.001);
1087
1088 if (wgl_disp->glrc) {
1089 wglDeleteContext(wgl_disp->glrc);
1090 wgl_disp->glrc = NULL;
1091 }
1092 if (wgl_disp->dc) {
1093 ReleaseDC(win_disp->window, wgl_disp->dc);
1094 wgl_disp->dc = NULL;
1095 }
1096
1097 if (disp->flags & ALLEGRO_FULLSCREEN && !_wgl_do_not_change_display_mode) {
1098 ChangeDisplaySettings(NULL, 0);
1099 }
1100
1101 if (win_disp->window) {
1102 DestroyWindow(win_disp->window);
1103 win_disp->window = NULL;
1104 }
1105 }
1106
1107
1108 static void wgl_destroy_display(ALLEGRO_DISPLAY *disp)
1109 {
1110 ALLEGRO_SYSTEM_WIN *system = (ALLEGRO_SYSTEM_WIN *)al_get_system_driver();
1111 ALLEGRO_DISPLAY_WGL *wgl_disp = (ALLEGRO_DISPLAY_WGL *)disp;
1112 ALLEGRO_DISPLAY *old_disp = al_get_current_display();
1113
1114 if (old_disp != disp)
1115 _al_set_current_display_only(disp);
1116
1117 if (system->mouse_grab_display == disp)
1118 system->mouse_grab_display = NULL;
1119
1120 _al_win_destroy_display_icons(disp);
1121
1122 destroy_display_internals(wgl_disp);
1123 _al_event_source_free(&disp->es);
1124 _al_vector_find_and_delete(&system->system.displays, &disp);
1125
1126 _al_vector_free(&disp->bitmaps);
1127 _al_vector_free(&((ALLEGRO_DISPLAY_WIN*) disp)->msg_callbacks);
1128 al_free(disp->ogl_extras);
1129
1130 if (old_disp != disp)
1131 _al_set_current_display_only(old_disp);
1132
1133 al_free(disp->vertex_cache);
1134 al_free(wgl_disp);
1135 }
1136
1137
1138 static bool wgl_set_current_display(ALLEGRO_DISPLAY *d)
1139 {
1140 ALLEGRO_DISPLAY_WGL *wgl_disp = (ALLEGRO_DISPLAY_WGL *)d;
1141 HGLRC current_glrc;
1142
1143 current_glrc = wglGetCurrentContext();
1144
1145 if (!current_glrc || (current_glrc && current_glrc != wgl_disp->glrc)) {
1146 /* make the context the current one */
1147 if (!wglMakeCurrent(wgl_disp->dc, wgl_disp->glrc)) {
1148 ALLEGRO_ERROR("Unable to make the context current! %s\n",
1149 _al_win_last_error());
1150 return false;
1151 }
1152
1153 _al_ogl_set_extensions(d->ogl_extras->extension_api);
1154 }
1155
1156 _al_ogl_update_render_state(d);
1157
1158 return true;
1159 }
1160
1161
1162 static void wgl_unset_current_display(ALLEGRO_DISPLAY *d)
1163 {
1164 (void)d;
1165
1166 if (!wglMakeCurrent(NULL, NULL)) {
1167 ALLEGRO_ERROR("Unable unset the current context! %s\n", _al_win_last_error());
1168 }
1169 }
1170
1171 /*
1172 * The window must be created in the same thread that
1173 * runs the message loop.
1174 */
1175 static void display_thread_proc(void *arg)
1176 {
1177 WGL_DISPLAY_PARAMETERS *ndp = arg;
1178 ALLEGRO_DISPLAY *disp = (ALLEGRO_DISPLAY*)ndp->display;
1179 ALLEGRO_DISPLAY_WGL *wgl_disp = (void*)disp;
1180 ALLEGRO_DISPLAY_WIN *win_disp = (void*)disp;
1181 MSG msg;
1182
1183 /* So that we can call the functions using TLS from this thread. */
1184 al_set_new_display_flags(disp->flags);
1185 al_set_new_window_position(ndp->window_x, ndp->window_y);
1186 al_set_new_window_title(ndp->window_title);
1187
1188 if (disp->flags & ALLEGRO_FULLSCREEN) {
1189 if (!change_display_mode(disp)) {
1190 win_disp->thread_ended = true;
1191 destroy_display_internals(wgl_disp);
1192 SetEvent(ndp->AckEvent);
1193 return;
1194 }
1195 }
1196 else if (disp->flags & ALLEGRO_FULLSCREEN_WINDOW) {
1197 ALLEGRO_MONITOR_INFO mi;
1198 int adapter = win_disp->adapter;
1199 al_get_monitor_info(adapter, &mi);
1200
1201 win_disp->toggle_w = disp->w;
1202 win_disp->toggle_h = disp->h;
1203
1204 disp->w = mi.x2 - mi.x1;
1205 disp->h = mi.y2 - mi.y1;
1206
1207 disp->refresh_rate = al_get_monitor_refresh_rate(adapter);
1208 }
1209 else {
1210 int adapter = win_disp->adapter;
1211 win_disp->toggle_w = disp->w;
1212 win_disp->toggle_h = disp->h;
1213
1214 disp->refresh_rate = al_get_monitor_refresh_rate(adapter);
1215 }
1216
1217 win_disp->window = _al_win_create_window(disp, disp->w, disp->h, disp->flags);
1218
1219 if (!win_disp->window) {
1220 win_disp->thread_ended = true;
1221 destroy_display_internals(wgl_disp);
1222 SetEvent(ndp->AckEvent);
1223 return;
1224 }
1225
1226 /* FIXME: can't _al_win_create_window() do this? */
1227 if ((disp->flags & ALLEGRO_FULLSCREEN) ||
1228 (disp->flags & ALLEGRO_FULLSCREEN_WINDOW)) {
1229 RECT rect;
1230 rect.left = 0;
1231 rect.right = disp->w;
1232 rect.top = 0;
1233 rect.bottom = disp->h;
1234 SetWindowPos(win_disp->window, 0, rect.left, rect.top,
1235 rect.right - rect.left, rect.bottom - rect.top,
1236 SWP_NOZORDER | SWP_FRAMECHANGED);
1237 }
1238
1239 if (disp->flags & ALLEGRO_FULLSCREEN_WINDOW) {
1240 _al_win_set_window_frameless(disp, win_disp->window, true);
1241 }
1242
1243 {
1244 DWORD lock_time;
1245 HWND wnd = win_disp->window;
1246
1247 if ((disp->flags & ALLEGRO_FULLSCREEN) == 0) {
1248 ShowWindow(wnd, SW_SHOWNORMAL);
1249 SetForegroundWindow(wnd);
1250 UpdateWindow(wnd);
1251 }
1252 else {
1253
1254 /* Yep, the following is really needed sometimes. */
1255 /* ... Or is it now that we have dumped DInput? */
1256 /* <rohannessian> Win98/2k/XP's window foreground rules don't let us
1257 * make our window the topmost window on launch. This causes issues on
1258 * full-screen apps, as DInput loses input focus on them.
1259 * We use this trick to force the window to be topmost, when switching
1260 * to full-screen only. Note that this only works for Win98 and greater.
1261 * Win95 will ignore our SystemParametersInfo() calls.
1262 *
1263 * See http://support.microsoft.com:80/support/kb/articles/Q97/9/25.asp
1264 * for details.
1265 */
1266
1267 #define SPI_GETFOREGROUNDLOCKTIMEOUT 0x2000
1268 #define SPI_SETFOREGROUNDLOCKTIMEOUT 0x2001
1269 SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT,
1270 0, (LPVOID)&lock_time, 0);
1271 SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT,
1272 0, (LPVOID)0, SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
1273
1274 ShowWindow(wnd, SW_SHOWNORMAL);
1275 SetForegroundWindow(wnd);
1276 /* In some rare cases, it doesn't seem to work without the loop. And we
1277 * absolutely need this to succeed, else we trap the user in a
1278 * fullscreen window without input.
1279 */
1280 while (GetForegroundWindow() != wnd) {
1281 al_rest(0.1);
1282 SetForegroundWindow(wnd);
1283 }
1284 UpdateWindow(wnd);
1285
1286 SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT,
1287 0, (LPVOID)&lock_time, SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
1288 #undef SPI_GETFOREGROUNDLOCKTIMEOUT
1289 #undef SPI_SETFOREGROUNDLOCKTIMEOUT
1290 }
1291 }
1292
1293 #if 0
1294 if (disp->flags & ALLEGRO_FULLSCREEN && al_is_mouse_installed()) {
1295 RAWINPUTDEVICE rid[1];
1296 rid[0].usUsagePage = 0x01;
1297 rid[0].usUsage = 0x02;
1298 rid[0].dwFlags = RIDEV_NOLEGACY;
1299 rid[0].hwndTarget = 0;
1300 if (RegisterRawInputDevices(rid, 1, sizeof(rid[0])) == FALSE) {
1301 ALLEGRO_ERROR(
1302 "Failed to init mouse. %s\n", get_error_desc(GetLastError()));
1303 }
1304 }
1305 #endif
1306
1307 /* get the device context of our window */
1308 wgl_disp->dc = GetDC(win_disp->window);
1309
1310 win_disp->thread_ended = false;
1311 win_disp->end_thread = false;
1312 ndp->init_failed = false;
1313 SetEvent(ndp->AckEvent);
1314
1315 while (!win_disp->end_thread) {
1316 /* get a message from the queue */
1317 if (GetMessage(&msg, NULL, 0, 0) != 0)
1318 DispatchMessage(&msg);
1319 else
1320 break; /* WM_QUIT received or error (GetMessage returned -1) */
1321 }
1322
1323 ALLEGRO_INFO("wgl display thread exits\n");
1324 win_disp->thread_ended = true;
1325 }
1326
1327
1328 static void wgl_flip_display(ALLEGRO_DISPLAY *d)
1329 {
1330 ALLEGRO_DISPLAY_WGL* disp = (ALLEGRO_DISPLAY_WGL*)d;
1331 glFlush();
1332 if (!d->extra_settings.settings[ALLEGRO_SINGLE_BUFFER])
1333 SwapBuffers(disp->dc);
1334 }
1335
1336
1337 static void wgl_update_display_region(ALLEGRO_DISPLAY *d,
1338 int x, int y, int width, int height)
1339 {
1340 if (al_get_opengl_extension_list()->ALLEGRO_WGL_WIN_swap_hint) {
1341 /* FIXME: This is just a driver hint and there is no guarantee that the
1342 * contens of the front buffer outside the given rectangle will be preserved,
1343 * thus we should really return false here and do nothing. */
1344 ALLEGRO_DISPLAY_WGL* disp = (ALLEGRO_DISPLAY_WGL*)d;
1345 wglAddSwapHintRectWIN(x, y, width, height);
1346 glFlush();
1347 SwapBuffers(disp->dc);
1348 return;
1349 }
1350 wgl_flip_display(d);
1351 }
1352
1353
1354 static bool wgl_resize_helper(ALLEGRO_DISPLAY *d, int width, int height)
1355 {
1356 ALLEGRO_DISPLAY_WGL *wgl_disp = (ALLEGRO_DISPLAY_WGL *)d;
1357 ALLEGRO_DISPLAY_WIN *win_disp = (ALLEGRO_DISPLAY_WIN *)d;
1358 int full_w, full_h;
1359 ALLEGRO_MONITOR_INFO mi;
1360 int adapter = win_disp->adapter;
1361 al_get_monitor_info(adapter, &mi);
1362 full_w = mi.x2 - mi.x1;
1363 full_h = mi.y2 - mi.y1;
1364
1365 if ((d->flags & ALLEGRO_FULLSCREEN_WINDOW) && (full_w != width || full_h != height)) {
1366 win_disp->toggle_w = width;
1367 win_disp->toggle_h = height;
1368 return true;
1369 }
1370
1371 if (d->flags & ALLEGRO_FULLSCREEN) {
1372 ALLEGRO_BITMAP *target_bmp;
1373 _AL_VECTOR disp_bmps;
1374 bool was_backbuffer = false;
1375 size_t i;
1376
1377 target_bmp = al_get_target_bitmap();
1378 if (target_bmp && target_bmp->vt) {
1379 ALLEGRO_BITMAP_EXTRA_OPENGL *extra = target_bmp->parent ?
1380 target_bmp->parent->extra : target_bmp->extra;
1381 was_backbuffer = extra->is_backbuffer;
1382 }
1383
1384 /* Remember display bitmaps. */
1385 _al_vector_init(&disp_bmps, sizeof(ALLEGRO_BITMAP*));
1386 for (i = 0; i < _al_vector_size(&d->bitmaps); i++) {
1387 ALLEGRO_BITMAP **dis = _al_vector_ref(&d->bitmaps, i);
1388 ALLEGRO_BITMAP **mem = _al_vector_alloc_back(&disp_bmps);
1389 *mem = *dis;
1390 }
1391
1392 /* This flag prevents from switching to desktop resolution in between. */
1393 _wgl_do_not_change_display_mode = true;
1394 destroy_display_internals(wgl_disp);
1395 _wgl_do_not_change_display_mode = false;
1396
1397 d->w = width;
1398 d->h = height;
1399 if(!create_display_internals(wgl_disp))
1400 return false;
1401
1402 /* We have a new backbuffer now. */
1403 if (was_backbuffer)
1404 al_set_target_bitmap(al_get_backbuffer(d));
1405
1406 /* Reupload bitmaps. */
1407 while (_al_vector_is_nonempty(&disp_bmps)) {
1408 ALLEGRO_BITMAP **back = _al_vector_ref_back(&disp_bmps);
1409 _al_convert_to_display_bitmap(*back);
1410 _al_vector_delete_at(&disp_bmps, _al_vector_size(&disp_bmps) - 1);
1411 }
1412 }
1413 else {
1414 RECT win_size;
1415 WINDOWINFO wi;
1416
1417 win_size.left = 0;
1418 win_size.top = 0;
1419 win_size.right = width;
1420 win_size.bottom = height;
1421
1422 wi.cbSize = sizeof(WINDOWINFO);
1423 GetWindowInfo(win_disp->window, &wi);
1424
1425 AdjustWindowRectEx(&win_size, wi.dwStyle, GetMenu(win_disp->window) ? TRUE : FALSE, wi.dwExStyle);
1426
1427 if (!SetWindowPos(win_disp->window, HWND_TOP,
1428 0, 0,
1429 win_size.right - win_size.left,
1430 win_size.bottom - win_size.top,
1431 SWP_NOMOVE|SWP_NOZORDER))
1432 return false;
1433
1434 d->w = width;
1435 d->h = height;
1436 if (!(d->flags & ALLEGRO_FULLSCREEN_WINDOW)) {
1437 win_disp->toggle_w = width;
1438 win_disp->toggle_h = height;
1439 }
1440 }
1441
1442 return true;
1443 }
1444
1445 static bool wgl_resize_display(ALLEGRO_DISPLAY *d, int width, int height)
1446 {
1447 ALLEGRO_DISPLAY_WIN *win_display = (ALLEGRO_DISPLAY_WIN *)d;
1448 int orig_w = d->w;
1449 int orig_h = d->h;
1450 bool ret;
1451
1452 win_display->ignore_resize = true;
1453
1454 if (!wgl_resize_helper(d, width, height)) {
1455 wgl_resize_helper(d, orig_w, orig_h);
1456 ret = false;
1457 } else {
1458 ret = true;
1459 wgl_acknowledge_resize(d);
1460 }
1461
1462 win_display->ignore_resize = false;
1463
1464 return ret;
1465 }
1466
1467 static bool wgl_acknowledge_resize(ALLEGRO_DISPLAY *d)
1468 {
1469 WINDOWINFO wi;
1470 ALLEGRO_DISPLAY_WIN *win_disp = (ALLEGRO_DISPLAY_WIN *)d;
1471 int w, h;
1472 ALLEGRO_STATE state;
1473
1474 wi.cbSize = sizeof(WINDOWINFO);
1475 GetWindowInfo(win_disp->window, &wi);
1476 w = wi.rcClient.right - wi.rcClient.left;
1477 h = wi.rcClient.bottom - wi.rcClient.top;
1478
1479 d->w = w;
1480 d->h = h;
1481
1482 _al_ogl_setup_gl(d);
1483 _al_ogl_update_render_state(d);
1484
1485 al_store_state(&state, ALLEGRO_STATE_DISPLAY | ALLEGRO_STATE_TARGET_BITMAP);
1486 al_set_target_backbuffer(d);
1487 al_set_clipping_rectangle(0, 0, w, h);
1488 al_restore_state(&state);
1489
1490 return true;
1491 }
1492
1493
1494 static bool wgl_is_compatible_bitmap(ALLEGRO_DISPLAY *display, ALLEGRO_BITMAP *bitmap)
1495 {
1496 /* Note: WGL driver doesn't support sharing of resources between contexts,
1497 * thus all bitmaps are tied to the display which was current at the time
1498 * al_create_bitmap was called.
1499 */
1500 return display == _al_get_bitmap_display(bitmap);
1501 }
1502
1503
1504 static void wgl_switch_in(ALLEGRO_DISPLAY *display)
1505 {
1506 (void)display;
1507 /*
1508 ALLEGRO_DISPLAY_WIN *win_disp = (ALLEGRO_DISPLAY_WIN *)display;
1509
1510 if (al_is_mouse_installed())
1511 al_set_mouse_range(win_disp->mouse_range_x1, win_disp->mouse_range_y1,
1512 win_disp->mouse_range_x2, win_disp->mouse_range_y2);
1513 */
1514 }
1515
1516
1517 static void wgl_switch_out(ALLEGRO_DISPLAY *display)
1518 {
1519 (void)display;
1520 }
1521
1522
1523 static void wgl_set_window_position(ALLEGRO_DISPLAY *display, int x, int y)
1524 {
1525 _al_win_set_window_position(((ALLEGRO_DISPLAY_WIN *)display)->window, x, y);
1526 }
1527
1528
1529 static void wgl_get_window_position(ALLEGRO_DISPLAY *display, int *x, int *y)
1530 {
1531 _al_win_get_window_position(((ALLEGRO_DISPLAY_WIN *)display)->window, x, y);
1532 }
1533
1534
1535 /* Obtain a reference to this driver. */
1536 ALLEGRO_DISPLAY_INTERFACE *_al_display_wgl_driver(void)
1537 {
1538 if (vt.create_display)
1539 return &vt;
1540
1541 vt.create_display = wgl_create_display;
1542 vt.destroy_display = wgl_destroy_display;
1543 vt.resize_display = wgl_resize_display;
1544 vt.set_current_display = wgl_set_current_display;
1545 vt.unset_current_display = wgl_unset_current_display;
1546 vt.flip_display = wgl_flip_display;
1547 vt.update_display_region = wgl_update_display_region;
1548 vt.acknowledge_resize = wgl_acknowledge_resize;
1549 vt.create_bitmap = _al_ogl_create_bitmap;
1550 vt.get_backbuffer = _al_ogl_get_backbuffer;
1551 vt.set_target_bitmap = _al_ogl_set_target_bitmap;
1552 vt.is_compatible_bitmap = wgl_is_compatible_bitmap;
1553 vt.switch_in = wgl_switch_in;
1554 vt.switch_out = wgl_switch_out;
1555
1556 vt.set_mouse_cursor = _al_win_set_mouse_cursor;
1557 vt.set_system_mouse_cursor = _al_win_set_system_mouse_cursor;
1558 vt.show_mouse_cursor = _al_win_show_mouse_cursor;
1559 vt.hide_mouse_cursor = _al_win_hide_mouse_cursor;
1560
1561 vt.set_icons = _al_win_set_display_icons;
1562 vt.set_window_position = wgl_set_window_position;
1563 vt.get_window_position = wgl_get_window_position;
1564 vt.get_window_borders = _al_win_get_window_borders;
1565 vt.set_window_constraints = _al_win_set_window_constraints;
1566 vt.get_window_constraints = _al_win_get_window_constraints;
1567 vt.apply_window_constraints = _al_win_apply_window_constraints;
1568 vt.set_display_flag = _al_win_set_display_flag;
1569 vt.set_window_title = _al_win_set_window_title;
1570
1571 vt.update_render_state = _al_ogl_update_render_state;
1572 _al_ogl_add_drawing_functions(&vt);
1573 _al_win_add_clipboard_functions(&vt);
1574
1575 return &vt;
1576 }
1577
1578 int _al_wgl_get_num_display_modes(int format, int refresh_rate, int flags)
1579 {
1580 DEVMODE dm;
1581 int count = 0;
1582
1583 /* FIXME: Ignoring format.
1584 * To get a list of pixel formats we have to create a window with a valid DC, which
1585 * would even require to change the video mode in fullscreen cases and we really
1586 * don't want to do that.
1587 */
1588 (void)format;
1589 (void)refresh_rate;
1590 (void)flags;
1591
1592 memset(&dm, 0, sizeof(dm));
1593 dm.dmSize = sizeof(dm);
1594
1595 while (EnumDisplaySettings(NULL, count, &dm) != false) {
1596 count++;
1597 }
1598
1599 return count;
1600 }
1601
1602
1603 ALLEGRO_DISPLAY_MODE *_al_wgl_get_display_mode(int index, int format,
1604 int refresh_rate, int flags,
1605 ALLEGRO_DISPLAY_MODE *mode)
1606 {
1607 DEVMODE dm;
1608
1609 /*
1610 * FIXME: see the comment in _al_wgl_get_num_display_modes
1611 */
1612 (void)format;
1613 (void)refresh_rate;
1614 (void)flags;
1615
1616 memset(&dm, 0, sizeof(dm));
1617 dm.dmSize = sizeof(dm);
1618
1619 if (!EnumDisplaySettings(NULL, index, &dm))
1620 return NULL;
1621
1622 mode->width = dm.dmPelsWidth;
1623 mode->height = dm.dmPelsHeight;
1624 mode->refresh_rate = dm.dmDisplayFrequency;
1625 mode->format = format;
1626 switch (dm.dmBitsPerPel) {
1627 case 32:
1628 if (format == ALLEGRO_PIXEL_FORMAT_ANY)
1629 mode->format = ALLEGRO_PIXEL_FORMAT_ANY_32_WITH_ALPHA;
1630 else if (format == ALLEGRO_PIXEL_FORMAT_ANY_NO_ALPHA)
1631 mode->format = ALLEGRO_PIXEL_FORMAT_ANY_32_NO_ALPHA;
1632 break;
1633 case 24:
1634 mode->format = ALLEGRO_PIXEL_FORMAT_ANY_24_NO_ALPHA;
1635 break;
1636 case 16:
1637 if (format == ALLEGRO_PIXEL_FORMAT_ANY)
1638 mode->format = ALLEGRO_PIXEL_FORMAT_ANY_16_WITH_ALPHA;
1639 else if(format == ALLEGRO_PIXEL_FORMAT_ANY_NO_ALPHA)
1640 mode->format = ALLEGRO_PIXEL_FORMAT_ANY_16_NO_ALPHA;
1641 break;
1642 default:
1643 break;
1644 }
1645
1646 return mode;
1647 }
1648
1649 /* vi: set sts=3 sw=3 et: */
1650