| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* New Windows system driver |
| 12 |
* |
| 13 |
* Based on the X11 OpenGL driver by Elias Pschernig. |
| 14 |
* |
| 15 |
* Heavily modified by Trent Gamblin. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include <stdlib.h> |
| 19 |
#include <string.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <ctype.h> |
| 22 |
#include <math.h> |
| 23 |
|
| 24 |
#include "allegro5/allegro.h" |
| 25 |
#include "allegro5/internal/aintern.h" |
| 26 |
#include "allegro5/internal/aintern_bitmap.h" |
| 27 |
#include "allegro5/internal/aintern_system.h" |
| 28 |
#include "allegro5/platform/aintwin.h" |
| 29 |
#include "allegro5/internal/aintern_wunicode.h" |
| 30 |
|
| 31 |
#if defined ALLEGRO_CFG_OPENGL |
| 32 |
#include "allegro5/allegro_opengl.h" |
| 33 |
#endif |
| 34 |
|
| 35 |
#include <windows.h> |
| 36 |
#include <mmsystem.h> |
| 37 |
|
| 38 |
ALLEGRO_DEBUG_CHANNEL("system") |
| 39 |
|
| 40 |
|
| 41 |
/* FIXME: should we check for psapi _WIN32_IE and shlobj? |
| 42 |
{ */ |
| 43 |
#include <psapi.h> |
| 44 |
|
| 45 |
#if _WIN32_IE < 0x500 |
| 46 |
#undef _WIN32_IE |
| 47 |
#define _WIN32_IE 0x500 |
| 48 |
#endif |
| 49 |
#include <shlobj.h> |
| 50 |
#include <shlwapi.h> |
| 51 |
/* } */ |
| 52 |
|
| 53 |
bool _al_win_disable_screensaver = false; |
| 54 |
|
| 55 |
static ALLEGRO_SYSTEM_INTERFACE *vt = 0; |
| 56 |
static bool using_higher_res_timer; |
| 57 |
|
| 58 |
static ALLEGRO_SYSTEM_WIN *_al_win_system; |
| 59 |
|
| 60 |
/* This is mostly here for _al_display_d3d_driver, which is stateful. */ |
| 61 |
static ALLEGRO_MUTEX* win_mutex; |
| 62 |
|
| 63 |
/* _WinMain: |
| 64 |
* Entry point for Windows GUI programs, hooked by a macro in alwin.h, |
| 65 |
* which makes it look as if the application can still have a normal |
| 66 |
* main() function. |
| 67 |
*/ |
| 68 |
int _WinMain(void *_main, void *hInst, void *hPrev, char *Cmd, int nShow) |
| 69 |
{ |
| 70 |
int (*mainfunc) (int argc, char *argv[]) = (int (*)(int, char *[]))_main; |
| 71 |
char *argbuf; |
| 72 |
char **argv; |
| 73 |
int argc; |
| 74 |
int argc_max; |
| 75 |
int i, q; |
| 76 |
|
| 77 |
(void)hInst; |
| 78 |
(void)hPrev; |
| 79 |
(void)Cmd; |
| 80 |
(void)nShow; |
| 81 |
|
| 82 |
/* can't use parameter because it doesn't include the executable name */ |
| 83 |
argbuf = _twin_tchar_to_utf8(GetCommandLine()); |
| 84 |
|
| 85 |
argc = 0; |
| 86 |
argc_max = 64; |
| 87 |
argv = al_malloc(sizeof(char *) * argc_max); |
| 88 |
if (!argv) { |
| 89 |
al_free(argbuf); |
| 90 |
return 1; |
| 91 |
} |
| 92 |
|
| 93 |
i = 0; |
| 94 |
|
| 95 |
/* parse commandline into argc/argv format */ |
| 96 |
while (argbuf[i]) { |
| 97 |
while ((argbuf[i]) && (isspace(argbuf[i]))) |
| 98 |
i++; |
| 99 |
|
| 100 |
if (argbuf[i]) { |
| 101 |
if ((argbuf[i] == '\'') || (argbuf[i] == '"')) { |
| 102 |
q = argbuf[i++]; |
| 103 |
if (!argbuf[i]) |
| 104 |
break; |
| 105 |
} |
| 106 |
else |
| 107 |
q = 0; |
| 108 |
|
| 109 |
argv[argc++] = &argbuf[i]; |
| 110 |
|
| 111 |
if (argc >= argc_max) { |
| 112 |
argc_max += 64; |
| 113 |
argv = al_realloc(argv, sizeof(char *) * argc_max); |
| 114 |
if (!argv) { |
| 115 |
al_free(argbuf); |
| 116 |
return 1; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
while ((argbuf[i]) && ((q) ? (argbuf[i] != q) : (!isspace(argbuf[i])))) |
| 121 |
i++; |
| 122 |
|
| 123 |
if (argbuf[i]) { |
| 124 |
argbuf[i] = 0; |
| 125 |
i++; |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
argv[argc] = NULL; |
| 131 |
|
| 132 |
/* call the application entry point */ |
| 133 |
i = mainfunc(argc, argv); |
| 134 |
|
| 135 |
al_free(argv); |
| 136 |
al_free(argbuf); |
| 137 |
|
| 138 |
return i; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
static void set_dpi_awareness(void) |
| 143 |
{ |
| 144 |
bool dpi_awareness_set = false; |
| 145 |
/* We load the shcore DLL and the APIs dynamically because these are |
| 146 |
* not often included in MinGW headers. */ |
| 147 |
HMODULE shcore_dll = _al_win_safe_load_library("shcore.dll"); |
| 148 |
if (shcore_dll) { |
| 149 |
typedef enum _AL_PROCESS_DPI_AWARENESS { |
| 150 |
AL_PROCESS_DPI_UNAWARE = 0, |
| 151 |
AL_PROCESS_SYSTEM_DPI_AWARE = 1, |
| 152 |
AL_PROCESS_PER_MONITOR_DPI_AWARE = 2 |
| 153 |
} AL_PROCESS_DPI_AWARENESS; |
| 154 |
typedef HRESULT (WINAPI *SetProcessDpiAwarenessPROC)(AL_PROCESS_DPI_AWARENESS); |
| 155 |
SetProcessDpiAwarenessPROC imp_SetProcessDpiAwareness = |
| 156 |
(SetProcessDpiAwarenessPROC)GetProcAddress(shcore_dll, "SetProcessDpiAwareness"); |
| 157 |
if (imp_SetProcessDpiAwareness) { |
| 158 |
/* Try setting the per-monitor awareness first. It might fail on Win 8.1. */ |
| 159 |
HRESULT ret = imp_SetProcessDpiAwareness(AL_PROCESS_PER_MONITOR_DPI_AWARE); |
| 160 |
if (ret == E_INVALIDARG) { |
| 161 |
ret = imp_SetProcessDpiAwareness(AL_PROCESS_SYSTEM_DPI_AWARE); |
| 162 |
} |
| 163 |
if (ret == S_OK) { |
| 164 |
dpi_awareness_set = true; |
| 165 |
} |
| 166 |
} |
| 167 |
FreeLibrary(shcore_dll); |
| 168 |
} |
| 169 |
|
| 170 |
/* SetProcessDPIAware is an older API that corresponds to system dpi |
| 171 |
* awareness above. This is the only option on pre-8.1 systems. */ |
| 172 |
if (!dpi_awareness_set) { |
| 173 |
HMODULE user32_dll = _al_win_safe_load_library("user32.dll"); |
| 174 |
if (user32_dll) { |
| 175 |
typedef BOOL (WINAPI *SetProcessDPIAwarePROC)(void); |
| 176 |
SetProcessDPIAwarePROC imp_SetProcessDPIAware = |
| 177 |
(SetProcessDPIAwarePROC)GetProcAddress(user32_dll, "SetProcessDPIAware"); |
| 178 |
if (imp_SetProcessDPIAware) { |
| 179 |
imp_SetProcessDPIAware(); |
| 180 |
} |
| 181 |
FreeLibrary(user32_dll); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
/* Create a new system object. */ |
| 188 |
static ALLEGRO_SYSTEM *win_initialize(int flags) |
| 189 |
{ |
| 190 |
(void)flags; |
| 191 |
|
| 192 |
set_dpi_awareness(); |
| 193 |
|
| 194 |
_al_win_system = al_calloc(1, sizeof *_al_win_system); |
| 195 |
|
| 196 |
// Request a 1ms resolution from our timer |
| 197 |
if (timeBeginPeriod(1) != TIMERR_NOCANDO) { |
| 198 |
using_higher_res_timer = true; |
| 199 |
} |
| 200 |
_al_win_init_time(); |
| 201 |
|
| 202 |
_al_win_init_window(); |
| 203 |
|
| 204 |
_al_vector_init(&_al_win_system->system.displays, sizeof (ALLEGRO_SYSTEM_WIN *)); |
| 205 |
|
| 206 |
_al_win_system->system.vt = vt; |
| 207 |
|
| 208 |
win_mutex = al_create_mutex(); |
| 209 |
|
| 210 |
return &_al_win_system->system; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
static void win_shutdown(void) |
| 215 |
{ |
| 216 |
ALLEGRO_SYSTEM *s; |
| 217 |
ASSERT(vt); |
| 218 |
|
| 219 |
/* Close all open displays. */ |
| 220 |
s = al_get_system_driver(); |
| 221 |
while (_al_vector_size(&s->displays) > 0) { |
| 222 |
ALLEGRO_DISPLAY **dptr = _al_vector_ref(&s->displays, 0); |
| 223 |
ALLEGRO_DISPLAY *d = *dptr; |
| 224 |
al_destroy_display(d); |
| 225 |
} |
| 226 |
|
| 227 |
_al_vector_free(&s->displays); |
| 228 |
|
| 229 |
#ifdef ALLEGRO_CFG_D3D |
| 230 |
_al_d3d_shutdown_display(); |
| 231 |
#endif |
| 232 |
|
| 233 |
_al_win_shutdown_window(); |
| 234 |
|
| 235 |
_al_win_shutdown_time(); |
| 236 |
|
| 237 |
if (using_higher_res_timer) { |
| 238 |
timeEndPeriod(1); |
| 239 |
} |
| 240 |
|
| 241 |
al_free(vt); |
| 242 |
vt = NULL; |
| 243 |
|
| 244 |
al_destroy_mutex(win_mutex); |
| 245 |
|
| 246 |
ASSERT(_al_win_system); |
| 247 |
al_free(_al_win_system); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
static ALLEGRO_DISPLAY_INTERFACE *win_get_display_driver(void) |
| 252 |
{ |
| 253 |
const int flags = al_get_new_display_flags(); |
| 254 |
ALLEGRO_SYSTEM *sys = al_get_system_driver(); |
| 255 |
ALLEGRO_CONFIG *sys_cfg = al_get_system_config(); |
| 256 |
ALLEGRO_SYSTEM_WIN *syswin = (ALLEGRO_SYSTEM_WIN *)sys; |
| 257 |
const char *s; |
| 258 |
|
| 259 |
/* Look up the toggle_mouse_grab_key binding. This isn't such a great place |
| 260 |
* to do it, but the config file is not available in win_initialize, |
| 261 |
* and this is neutral between the D3D and OpenGL display drivers. |
| 262 |
*/ |
| 263 |
if (!syswin->toggle_mouse_grab_keycode) { |
| 264 |
const char *binding = al_get_config_value(sys_cfg, "keyboard", |
| 265 |
"toggle_mouse_grab_key"); |
| 266 |
if (binding) { |
| 267 |
syswin->toggle_mouse_grab_keycode = _al_parse_key_binding(binding, |
| 268 |
&syswin->toggle_mouse_grab_modifiers); |
| 269 |
if (syswin->toggle_mouse_grab_keycode) { |
| 270 |
ALLEGRO_DEBUG("Toggle mouse grab key: '%s'\n", binding); |
| 271 |
} |
| 272 |
else { |
| 273 |
ALLEGRO_WARN("Cannot parse key binding '%s'\n", binding); |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
/* Programmatic selection. */ |
| 279 |
#ifdef ALLEGRO_CFG_D3D |
| 280 |
if (flags & ALLEGRO_DIRECT3D_INTERNAL) { |
| 281 |
al_lock_mutex(win_mutex); |
| 282 |
ALLEGRO_DISPLAY_INTERFACE* iface = _al_display_d3d_driver(); |
| 283 |
al_unlock_mutex(win_mutex); |
| 284 |
if (iface == NULL) |
| 285 |
ALLEGRO_WARN("Direct3D graphics driver not available.\n"); |
| 286 |
return iface; |
| 287 |
} |
| 288 |
#endif |
| 289 |
#ifdef ALLEGRO_CFG_OPENGL |
| 290 |
if (flags & ALLEGRO_OPENGL) { |
| 291 |
return _al_display_wgl_driver(); |
| 292 |
} |
| 293 |
#endif |
| 294 |
|
| 295 |
/* Selection by configuration file. The configuration value is non-binding. |
| 296 |
* The user may unknowingly set a value which was configured out at compile |
| 297 |
* time. The value should have no effect instead of causing a failure. |
| 298 |
*/ |
| 299 |
s = al_get_config_value(sys_cfg, "graphics", "driver"); |
| 300 |
if (s) { |
| 301 |
ALLEGRO_DEBUG("Configuration value graphics.driver = %s\n", s); |
| 302 |
if (0 == _al_stricmp(s, "DIRECT3D") || 0 == _al_stricmp(s, "D3D")) { |
| 303 |
#ifdef ALLEGRO_CFG_D3D |
| 304 |
al_lock_mutex(win_mutex); |
| 305 |
ALLEGRO_DISPLAY_INTERFACE* iface = _al_display_d3d_driver(); |
| 306 |
al_unlock_mutex(win_mutex); |
| 307 |
if (iface != NULL) { |
| 308 |
al_set_new_display_flags(flags | ALLEGRO_DIRECT3D_INTERNAL); |
| 309 |
return iface; |
| 310 |
} |
| 311 |
#endif |
| 312 |
} |
| 313 |
else if (0 == _al_stricmp(s, "OPENGL")) { |
| 314 |
#ifdef ALLEGRO_CFG_OPENGL |
| 315 |
al_set_new_display_flags(flags | ALLEGRO_OPENGL); |
| 316 |
return _al_display_wgl_driver(); |
| 317 |
#endif |
| 318 |
} |
| 319 |
else if (0 != _al_stricmp(s, "DEFAULT")) { |
| 320 |
ALLEGRO_WARN("Graphics driver selection unrecognised: %s\n", s); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
/* Automatic graphics driver selection. */ |
| 325 |
/* XXX is implicitly setting new_display_flags the desired behaviour? */ |
| 326 |
#ifdef ALLEGRO_CFG_D3D |
| 327 |
{ |
| 328 |
al_lock_mutex(win_mutex); |
| 329 |
ALLEGRO_DISPLAY_INTERFACE* iface = _al_display_d3d_driver(); |
| 330 |
al_unlock_mutex(win_mutex); |
| 331 |
if (iface != NULL) { |
| 332 |
al_set_new_display_flags(flags | ALLEGRO_DIRECT3D_INTERNAL); |
| 333 |
return iface; |
| 334 |
} |
| 335 |
} |
| 336 |
#endif |
| 337 |
#ifdef ALLEGRO_CFG_OPENGL |
| 338 |
{ |
| 339 |
al_set_new_display_flags(flags | ALLEGRO_OPENGL); |
| 340 |
return _al_display_wgl_driver(); |
| 341 |
} |
| 342 |
#endif |
| 343 |
ALLEGRO_WARN("No graphics driver available.\n"); |
| 344 |
return NULL; |
| 345 |
} |
| 346 |
|
| 347 |
/* FIXME: use the list */ |
| 348 |
static ALLEGRO_KEYBOARD_DRIVER *win_get_keyboard_driver(void) |
| 349 |
{ |
| 350 |
return _al_keyboard_driver_list[0].driver; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
/* Checks whether the configured joystick driver is of the given name. |
| 355 |
* Also returns false if the configuration entry was not set. |
| 356 |
*/ |
| 357 |
static bool win_configured_joystick_driver_is(const char * name) |
| 358 |
{ |
| 359 |
const char * driver; |
| 360 |
ALLEGRO_CONFIG * sysconf = al_get_system_config(); |
| 361 |
if (!sysconf) return false; |
| 362 |
driver = al_get_config_value(sysconf, "joystick", "driver"); |
| 363 |
if (!driver) return false; |
| 364 |
ALLEGRO_DEBUG("Configuration value joystick.driver = %s\n", driver); |
| 365 |
return (0 == _al_stricmp(driver, name)); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
/* Checks whether xinput should be used or not not according |
| 370 |
* to configuration. |
| 371 |
*/ |
| 372 |
static bool win_use_xinput(void) |
| 373 |
{ |
| 374 |
return win_configured_joystick_driver_is("XINPUT"); |
| 375 |
} |
| 376 |
|
| 377 |
/* Checks whether directinput should be used or not not according |
| 378 |
* to configuration. |
| 379 |
*/ |
| 380 |
static bool win_use_directinput(void) |
| 381 |
{ |
| 382 |
return win_configured_joystick_driver_is("DIRECTINPUT"); |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
/* By default the combined xinput/directinput driver is used unless directinput |
| 387 |
* or xinput exclusive is set.*/ |
| 388 |
static ALLEGRO_JOYSTICK_DRIVER *win_get_joystick_driver(void) |
| 389 |
{ |
| 390 |
if (win_use_directinput()) { |
| 391 |
ALLEGRO_DEBUG("Selected DirectInput joystick driver.\n"); |
| 392 |
return &_al_joydrv_directx; |
| 393 |
} |
| 394 |
|
| 395 |
if (win_use_xinput()) { |
| 396 |
#ifdef ALLEGRO_CFG_XINPUT |
| 397 |
ALLEGRO_DEBUG("Selected XInput joystick driver.\n"); |
| 398 |
return &_al_joydrv_xinput; |
| 399 |
#else |
| 400 |
ALLEGRO_WARN("XInput joystick driver not supported.\n"); |
| 401 |
#endif |
| 402 |
} |
| 403 |
|
| 404 |
#ifdef ALLEGRO_CFG_XINPUT |
| 405 |
ALLEGRO_DEBUG("Selected combined XInput/DirectInput joystick driver.\n"); |
| 406 |
return &_al_joydrv_windows_all; |
| 407 |
#else |
| 408 |
ALLEGRO_WARN("Combined XInput/DirectInput joystick driver not supported. Usign DirectInput in stead.\n"); |
| 409 |
return &_al_joydrv_directx; |
| 410 |
#endif |
| 411 |
} |
| 412 |
|
| 413 |
/* By default the combined haptic driver is used unless directinput or |
| 414 |
* xinput exclusive is set in the configuration.*/ |
| 415 |
static ALLEGRO_HAPTIC_DRIVER *win_get_haptic_driver(void) |
| 416 |
{ |
| 417 |
if (win_use_directinput()) { |
| 418 |
ALLEGRO_DEBUG("Selected DirectInput haptic driver.\n"); |
| 419 |
return &_al_hapdrv_directx; |
| 420 |
} |
| 421 |
|
| 422 |
if (win_use_xinput()) { |
| 423 |
#ifdef ALLEGRO_CFG_XINPUT |
| 424 |
ALLEGRO_DEBUG("Selected XInput haptic driver.\n"); |
| 425 |
return &_al_hapdrv_xinput; |
| 426 |
#else |
| 427 |
ALLEGRO_WARN("XInput haptic driver not supported.\n"); |
| 428 |
#endif |
| 429 |
} |
| 430 |
|
| 431 |
#ifdef ALLEGRO_CFG_XINPUT |
| 432 |
ALLEGRO_DEBUG("Selected combined XInput/DirectInput haptic driver.\n"); |
| 433 |
return &_al_hapdrv_windows_all; |
| 434 |
#else |
| 435 |
ALLEGRO_WARN("Combined XInput/DirectInput haptic driver not supported. Using DirectInput in stead.\n"); |
| 436 |
return &_al_hapdrv_directx; |
| 437 |
#endif |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
static int win_get_num_display_modes(void) |
| 442 |
{ |
| 443 |
int format = _al_deduce_color_format(_al_get_new_display_settings()); |
| 444 |
int refresh_rate = al_get_new_display_refresh_rate(); |
| 445 |
int flags = al_get_new_display_flags(); |
| 446 |
|
| 447 |
#if defined ALLEGRO_CFG_OPENGL |
| 448 |
if (flags & ALLEGRO_OPENGL) { |
| 449 |
return _al_wgl_get_num_display_modes(format, refresh_rate, flags); |
| 450 |
} |
| 451 |
#endif |
| 452 |
#if defined ALLEGRO_CFG_D3D |
| 453 |
return _al_d3d_get_num_display_modes(format, refresh_rate, flags); |
| 454 |
#endif |
| 455 |
|
| 456 |
return 0; |
| 457 |
} |
| 458 |
|
| 459 |
static ALLEGRO_DISPLAY_MODE *win_get_display_mode(int index, |
| 460 |
ALLEGRO_DISPLAY_MODE *mode) |
| 461 |
{ |
| 462 |
int format = _al_deduce_color_format(_al_get_new_display_settings()); |
| 463 |
int refresh_rate = al_get_new_display_refresh_rate(); |
| 464 |
int flags = al_get_new_display_flags(); |
| 465 |
|
| 466 |
#if defined ALLEGRO_CFG_OPENGL |
| 467 |
if (flags & ALLEGRO_OPENGL) { |
| 468 |
return _al_wgl_get_display_mode(index, format, refresh_rate, flags, mode); |
| 469 |
} |
| 470 |
#endif |
| 471 |
#if defined ALLEGRO_CFG_D3D |
| 472 |
return _al_d3d_get_display_mode(index, format, refresh_rate, flags, mode); |
| 473 |
#endif |
| 474 |
|
| 475 |
|
| 476 |
return NULL; |
| 477 |
} |
| 478 |
|
| 479 |
static int win_get_num_video_adapters(void) |
| 480 |
{ |
| 481 |
DISPLAY_DEVICE dd; |
| 482 |
int count = 0; |
| 483 |
int c = 0; |
| 484 |
|
| 485 |
memset(&dd, 0, sizeof(dd)); |
| 486 |
dd.cb = sizeof(dd); |
| 487 |
|
| 488 |
while (EnumDisplayDevices(NULL, count, &dd, 0) != false) { |
| 489 |
if (dd.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) |
| 490 |
c++; |
| 491 |
count++; |
| 492 |
} |
| 493 |
|
| 494 |
return c; |
| 495 |
} |
| 496 |
|
| 497 |
static int win_find_nth_adapter_with_desktop(DISPLAY_DEVICE* pdd, int adapter) |
| 498 |
{ |
| 499 |
int count = 0; |
| 500 |
int c = 0; |
| 501 |
|
| 502 |
while (EnumDisplayDevices(NULL, count, pdd, 0)) { |
| 503 |
if (pdd->StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) { |
| 504 |
if (c == adapter) |
| 505 |
return true; |
| 506 |
c++; |
| 507 |
} |
| 508 |
count++; |
| 509 |
} |
| 510 |
|
| 511 |
return false; |
| 512 |
} |
| 513 |
|
| 514 |
static int win_get_monitor_refresh_rate(int adapter) |
| 515 |
{ |
| 516 |
DISPLAY_DEVICE dd; |
| 517 |
DEVMODE dm; |
| 518 |
|
| 519 |
memset(&dd, 0, sizeof(dd)); |
| 520 |
dd.cb = sizeof(dd); |
| 521 |
if (!win_find_nth_adapter_with_desktop(&dd, adapter)) { |
| 522 |
return 0; |
| 523 |
} |
| 524 |
|
| 525 |
memset(&dm, 0, sizeof(dm)); |
| 526 |
dm.dmSize = sizeof(dm); |
| 527 |
if (!EnumDisplaySettings(dd.DeviceName, ENUM_CURRENT_SETTINGS, &dm)) { |
| 528 |
return 0; |
| 529 |
} |
| 530 |
|
| 531 |
return dm.dmDisplayFrequency; |
| 532 |
} |
| 533 |
|
| 534 |
static bool win_get_monitor_info(int adapter, ALLEGRO_MONITOR_INFO *info) |
| 535 |
{ |
| 536 |
DISPLAY_DEVICE dd; |
| 537 |
DEVMODE dm; |
| 538 |
|
| 539 |
memset(&dd, 0, sizeof(dd)); |
| 540 |
dd.cb = sizeof(dd); |
| 541 |
if (!win_find_nth_adapter_with_desktop(&dd, adapter)) { |
| 542 |
return false; |
| 543 |
} |
| 544 |
|
| 545 |
memset(&dm, 0, sizeof(dm)); |
| 546 |
dm.dmSize = sizeof(dm); |
| 547 |
if (!EnumDisplaySettings(dd.DeviceName, ENUM_CURRENT_SETTINGS, &dm)) { |
| 548 |
return false; |
| 549 |
} |
| 550 |
|
| 551 |
ASSERT(dm.dmFields & DM_PELSHEIGHT); |
| 552 |
ASSERT(dm.dmFields & DM_PELSWIDTH); |
| 553 |
/* Disabled this assertion for now as it fails under Wine 1.2. */ |
| 554 |
/* ASSERT(dm.dmFields & DM_POSITION); */ |
| 555 |
|
| 556 |
info->x1 = dm.dmPosition.x; |
| 557 |
info->y1 = dm.dmPosition.y; |
| 558 |
info->x2 = info->x1 + dm.dmPelsWidth; |
| 559 |
info->y2 = info->y1 + dm.dmPelsHeight; |
| 560 |
return true; |
| 561 |
} |
| 562 |
|
| 563 |
static BOOL CALLBACK monitor_enum_proc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) |
| 564 |
{ |
| 565 |
HMONITOR *h_monitor = (HMONITOR *) dwData; |
| 566 |
(void)hdcMonitor; |
| 567 |
(void)lprcMonitor; |
| 568 |
|
| 569 |
if (hMonitor) { |
| 570 |
*h_monitor = hMonitor; |
| 571 |
return false; |
| 572 |
} |
| 573 |
|
| 574 |
return true; |
| 575 |
} |
| 576 |
|
| 577 |
static HMONITOR win_get_monitor(ALLEGRO_MONITOR_INFO *info) |
| 578 |
{ |
| 579 |
HMONITOR h_monitor; |
| 580 |
RECT rect; |
| 581 |
|
| 582 |
rect.left = info->x1; |
| 583 |
rect.top = info->y1; |
| 584 |
rect.right = info->x2; |
| 585 |
rect.bottom = info->y2; |
| 586 |
|
| 587 |
EnumDisplayMonitors(NULL, &rect, monitor_enum_proc, (LPARAM) &h_monitor); |
| 588 |
|
| 589 |
return h_monitor; |
| 590 |
} |
| 591 |
|
| 592 |
static int win_get_monitor_dpi(int adapter) |
| 593 |
{ |
| 594 |
ALLEGRO_MONITOR_INFO info; |
| 595 |
HMODULE shcore_dll = _al_win_safe_load_library("shcore.dll"); |
| 596 |
UINT dpi_hori; |
| 597 |
UINT dpi_vert; |
| 598 |
|
| 599 |
typedef enum _AL_MONITOR_DPI_TYPE { |
| 600 |
AL_MDT_EFFECTIVE_DPI = 0, |
| 601 |
AL_MDT_ANGULAR_DPI = 1, |
| 602 |
AL_MDT_RAW_DPI = 2, |
| 603 |
AL_MDT_DEFAULT = AL_MDT_EFFECTIVE_DPI |
| 604 |
} AL_MONITOR_DPI_TYPE; |
| 605 |
|
| 606 |
typedef HRESULT (WINAPI *GetDpiForMonitorPROC)(HMONITOR hmonitor, AL_MONITOR_DPI_TYPE dpiType, UINT *dpiX, UINT *dpiY); |
| 607 |
|
| 608 |
GetDpiForMonitorPROC imp_GetDpiForMonitor = |
| 609 |
(GetDpiForMonitorPROC) GetProcAddress(shcore_dll, "GetDpiForMonitor"); |
| 610 |
|
| 611 |
if (!shcore_dll) { |
| 612 |
FreeLibrary(shcore_dll); |
| 613 |
return 0; |
| 614 |
} |
| 615 |
|
| 616 |
if (!win_get_monitor_info(adapter, &info)) { |
| 617 |
FreeLibrary(shcore_dll); |
| 618 |
return 0; |
| 619 |
} |
| 620 |
|
| 621 |
imp_GetDpiForMonitor(win_get_monitor(&info), 0, &dpi_hori, &dpi_vert); |
| 622 |
|
| 623 |
FreeLibrary(shcore_dll); |
| 624 |
|
| 625 |
return sqrt(dpi_hori * dpi_vert); |
| 626 |
} |
| 627 |
|
| 628 |
static bool win_get_cursor_position(int *ret_x, int *ret_y) |
| 629 |
{ |
| 630 |
POINT p; |
| 631 |
GetCursorPos(&p); |
| 632 |
*ret_x = p.x; |
| 633 |
*ret_y = p.y; |
| 634 |
return true; |
| 635 |
} |
| 636 |
|
| 637 |
static bool win_grab_mouse(ALLEGRO_DISPLAY *display) |
| 638 |
{ |
| 639 |
ALLEGRO_SYSTEM_WIN *system = (void *)al_get_system_driver(); |
| 640 |
ALLEGRO_DISPLAY_WIN *win_disp = (void *)display; |
| 641 |
RECT rect; |
| 642 |
|
| 643 |
GetWindowRect(win_disp->window, &rect); |
| 644 |
if (ClipCursor(&rect) != 0) { |
| 645 |
system->mouse_grab_display = display; |
| 646 |
return true; |
| 647 |
} |
| 648 |
|
| 649 |
return false; |
| 650 |
} |
| 651 |
|
| 652 |
static bool win_ungrab_mouse(void) |
| 653 |
{ |
| 654 |
ALLEGRO_SYSTEM_WIN *system = (void *)al_get_system_driver(); |
| 655 |
|
| 656 |
ClipCursor(NULL); |
| 657 |
system->mouse_grab_display = NULL; |
| 658 |
return true; |
| 659 |
} |
| 660 |
|
| 661 |
static ALLEGRO_MOUSE_DRIVER *win_get_mouse_driver(void) |
| 662 |
{ |
| 663 |
return _al_mouse_driver_list[0].driver; |
| 664 |
} |
| 665 |
|
| 666 |
static ALLEGRO_TOUCH_INPUT_DRIVER *win_get_touch_input_driver(void) |
| 667 |
{ |
| 668 |
return _al_touch_input_driver_list[0].driver; |
| 669 |
} |
| 670 |
|
| 671 |
/* _al_win_get_path: |
| 672 |
* Returns full path to various system and user diretories |
| 673 |
*/ |
| 674 |
ALLEGRO_PATH *_al_win_get_path(int id) |
| 675 |
{ |
| 676 |
char path[MAX_PATH]; |
| 677 |
wchar_t pathw[MAX_PATH]; |
| 678 |
ALLEGRO_USTR *pathu; |
| 679 |
uint32_t csidl = 0; |
| 680 |
HRESULT ret = 0; |
| 681 |
ALLEGRO_PATH *cisdl_path = NULL; |
| 682 |
|
| 683 |
switch (id) { |
| 684 |
case ALLEGRO_TEMP_PATH: { |
| 685 |
/* Check: TMP, TMPDIR, TEMP or TEMPDIR */ |
| 686 |
|
| 687 |
DWORD ret = GetTempPathW(MAX_PATH, pathw); |
| 688 |
if (ret > MAX_PATH) { |
| 689 |
/* should this ever happen, windows is more broken than I ever thought */ |
| 690 |
return NULL; |
| 691 |
} |
| 692 |
pathu = al_ustr_new_from_utf16(pathw); |
| 693 |
al_ustr_to_buffer(pathu, path, sizeof path); |
| 694 |
al_ustr_free(pathu); |
| 695 |
return al_create_path_for_directory(path); |
| 696 |
|
| 697 |
} break; |
| 698 |
|
| 699 |
case ALLEGRO_RESOURCES_PATH: { /* where the program is in */ |
| 700 |
HANDLE process = GetCurrentProcess(); |
| 701 |
char *ptr; |
| 702 |
|
| 703 |
GetModuleFileNameExW(process, NULL, pathw, MAX_PATH); |
| 704 |
pathu = al_ustr_new_from_utf16(pathw); |
| 705 |
al_ustr_to_buffer(pathu, path, sizeof path); |
| 706 |
al_ustr_free(pathu); |
| 707 |
ptr = strrchr(path, '\\'); |
| 708 |
if (!ptr) { /* shouldn't happen */ |
| 709 |
return NULL; |
| 710 |
} |
| 711 |
|
| 712 |
/* chop off everything including and after the last slash */ |
| 713 |
/* should this not chop the slash? */ |
| 714 |
*ptr = '\0'; |
| 715 |
|
| 716 |
return al_create_path_for_directory(path); |
| 717 |
} break; |
| 718 |
|
| 719 |
case ALLEGRO_USER_DATA_PATH: /* CSIDL_APPDATA */ |
| 720 |
case ALLEGRO_USER_SETTINGS_PATH: |
| 721 |
csidl = CSIDL_APPDATA; |
| 722 |
break; |
| 723 |
|
| 724 |
case ALLEGRO_USER_HOME_PATH: /* CSIDL_PROFILE */ |
| 725 |
csidl = CSIDL_PROFILE; |
| 726 |
break; |
| 727 |
|
| 728 |
case ALLEGRO_USER_DOCUMENTS_PATH: /* CSIDL_PERSONAL */ |
| 729 |
csidl = CSIDL_PERSONAL; |
| 730 |
break; |
| 731 |
|
| 732 |
case ALLEGRO_EXENAME_PATH: { /* full path to the exe including its name */ |
| 733 |
HANDLE process = GetCurrentProcess(); |
| 734 |
|
| 735 |
GetModuleFileNameExW(process, NULL, pathw, MAX_PATH); |
| 736 |
pathu = al_ustr_new_from_utf16(pathw); |
| 737 |
al_ustr_to_buffer(pathu, path, sizeof path); |
| 738 |
al_ustr_free(pathu); |
| 739 |
|
| 740 |
return al_create_path(path); |
| 741 |
} break; |
| 742 |
|
| 743 |
default: |
| 744 |
return NULL; |
| 745 |
} |
| 746 |
|
| 747 |
ret = SHGetFolderPathW(NULL, csidl, NULL, SHGFP_TYPE_CURRENT, pathw); |
| 748 |
if (ret != S_OK) { |
| 749 |
return NULL; |
| 750 |
} |
| 751 |
|
| 752 |
pathu = al_ustr_new_from_utf16(pathw); |
| 753 |
al_ustr_to_buffer(pathu, path, sizeof path); |
| 754 |
al_ustr_free(pathu); |
| 755 |
|
| 756 |
cisdl_path = al_create_path_for_directory(path); |
| 757 |
if (!cisdl_path) |
| 758 |
return NULL; |
| 759 |
|
| 760 |
if (csidl == CSIDL_APPDATA) { |
| 761 |
const char *org_name = al_get_org_name(); |
| 762 |
const char *app_name = al_get_app_name(); |
| 763 |
|
| 764 |
if (!app_name || !app_name[0]) { |
| 765 |
/* this shouldn't ever happen. */ |
| 766 |
al_destroy_path(cisdl_path); |
| 767 |
return NULL; |
| 768 |
} |
| 769 |
|
| 770 |
if (org_name && org_name[0]) { |
| 771 |
al_append_path_component(cisdl_path, org_name); |
| 772 |
} |
| 773 |
|
| 774 |
al_append_path_component(cisdl_path, app_name); |
| 775 |
} |
| 776 |
|
| 777 |
return cisdl_path; |
| 778 |
} |
| 779 |
|
| 780 |
static bool win_inhibit_screensaver(bool inhibit) |
| 781 |
{ |
| 782 |
_al_win_disable_screensaver = inhibit; |
| 783 |
return true; |
| 784 |
} |
| 785 |
|
| 786 |
static HMODULE load_library_at_path(const TCHAR *path_str) |
| 787 |
{ |
| 788 |
HMODULE lib; |
| 789 |
|
| 790 |
/* |
| 791 |
* XXX LoadLibrary will search the current directory for any dependencies of |
| 792 |
* the library we are loading. Using LoadLibraryEx with the appropriate |
| 793 |
* flags would fix that, but when I tried it I was unable to load dsound.dll |
| 794 |
* on Vista. |
| 795 |
*/ |
| 796 |
char* upath_str = _twin_tchar_to_utf8(path_str); |
| 797 |
ALLEGRO_DEBUG("Calling LoadLibrary %s\n", upath_str); |
| 798 |
lib = LoadLibrary(path_str); |
| 799 |
if (lib) { |
| 800 |
ALLEGRO_INFO("Loaded %s\n", upath_str); |
| 801 |
} |
| 802 |
else { |
| 803 |
DWORD error = GetLastError(); |
| 804 |
HRESULT hr = HRESULT_FROM_WIN32(error); |
| 805 |
/* XXX do something with it */ |
| 806 |
(void)hr; |
| 807 |
ALLEGRO_WARN("Failed to load %s (error: %ld)\n", upath_str, error); |
| 808 |
} |
| 809 |
al_free(upath_str); |
| 810 |
return lib; |
| 811 |
} |
| 812 |
|
| 813 |
static bool is_build_config_name(const char *s) |
| 814 |
{ |
| 815 |
return s && |
| 816 |
( 0 == strcmp(s, "Debug") |
| 817 |
|| 0 == strcmp(s, "Release") |
| 818 |
|| 0 == strcmp(s, "RelWithDebInfo") |
| 819 |
|| 0 == strcmp(s, "Profile")); |
| 820 |
} |
| 821 |
|
| 822 |
static ALLEGRO_PATH *maybe_parent_dir(const ALLEGRO_PATH *path) |
| 823 |
{ |
| 824 |
ALLEGRO_PATH *path2; |
| 825 |
|
| 826 |
if (!path) |
| 827 |
return NULL; |
| 828 |
|
| 829 |
if (!is_build_config_name(al_get_path_tail(path))) |
| 830 |
return NULL; |
| 831 |
|
| 832 |
path2 = al_clone_path(path); |
| 833 |
if (path2) { |
| 834 |
al_drop_path_tail(path2); |
| 835 |
al_set_path_filename(path2, NULL); |
| 836 |
ALLEGRO_DEBUG("Also searching %s\n", al_path_cstr(path2, '\\')); |
| 837 |
} |
| 838 |
|
| 839 |
return path2; |
| 840 |
} |
| 841 |
|
| 842 |
/* |
| 843 |
* Calling LoadLibrary with a relative file name is a security risk: |
| 844 |
* see e.g. Microsoft Security Advisory (2269637) |
| 845 |
* "Insecure Library Loading Could Allow Remote Code Execution" |
| 846 |
*/ |
| 847 |
HMODULE _al_win_safe_load_library(const char *filename) |
| 848 |
{ |
| 849 |
ALLEGRO_PATH *path1 = NULL; |
| 850 |
ALLEGRO_PATH *path2 = NULL; |
| 851 |
TCHAR buf[MAX_PATH]; |
| 852 |
const TCHAR *other_dirs[3]; |
| 853 |
HMODULE lib = NULL; |
| 854 |
bool msvc_only = false; |
| 855 |
TCHAR* tfilename; |
| 856 |
|
| 857 |
/* MSVC only: if the executable is in the build configuration directory, |
| 858 |
* which is also just under the current directory, then also try to load the |
| 859 |
* library from the current directory. This leads to less surprises when |
| 860 |
* running example programs. |
| 861 |
*/ |
| 862 |
#if defined(ALLEGRO_MSVC) |
| 863 |
msvc_only = true; |
| 864 |
#endif |
| 865 |
|
| 866 |
/* Try to load the library from the directory containing the running |
| 867 |
* executable, the Windows system directories, or directories on the PATH. |
| 868 |
* Do NOT try to load the library from the current directory. |
| 869 |
*/ |
| 870 |
|
| 871 |
if (al_is_system_installed()) { |
| 872 |
path1 = al_get_standard_path(ALLEGRO_RESOURCES_PATH); |
| 873 |
} |
| 874 |
else if (GetModuleFileName(NULL, buf, sizeof(buf)) < sizeof(buf)) { |
| 875 |
char* tmp = _twin_tchar_to_utf8(buf); |
| 876 |
path1 = al_create_path(tmp); |
| 877 |
al_free(tmp); |
| 878 |
} |
| 879 |
if (msvc_only) { |
| 880 |
path2 = maybe_parent_dir(path1); |
| 881 |
} |
| 882 |
|
| 883 |
other_dirs[0] = path1 ? _twin_ustr_to_tchar(al_path_ustr(path1, '\\')) : NULL; |
| 884 |
other_dirs[1] = path2 ? _twin_ustr_to_tchar(al_path_ustr(path2, '\\')) : NULL; |
| 885 |
other_dirs[2] = NULL; /* sentinel */ |
| 886 |
tfilename = _twin_utf8_to_tchar(filename); |
| 887 |
_tcsncpy(buf, tfilename, MAX_PATH); |
| 888 |
al_free(tfilename); |
| 889 |
if (PathFindOnPath(buf, other_dirs)) { |
| 890 |
char* tmp = _twin_tchar_to_utf8(buf); |
| 891 |
ALLEGRO_DEBUG("PathFindOnPath found: %s\n", tmp); |
| 892 |
al_free(tmp); |
| 893 |
lib = load_library_at_path(buf); |
| 894 |
} |
| 895 |
else { |
| 896 |
ALLEGRO_WARN("PathFindOnPath failed to find %s\n", filename); |
| 897 |
} |
| 898 |
al_free((void*) other_dirs[0]); |
| 899 |
al_free((void*) other_dirs[1]); |
| 900 |
al_destroy_path(path1); |
| 901 |
al_destroy_path(path2); |
| 902 |
|
| 903 |
return lib; |
| 904 |
} |
| 905 |
|
| 906 |
static void *win_open_library(const char *filename) |
| 907 |
{ |
| 908 |
return _al_win_safe_load_library(filename); |
| 909 |
} |
| 910 |
|
| 911 |
static void *win_import_symbol(void *library, const char *symbol) |
| 912 |
{ |
| 913 |
ASSERT(library); |
| 914 |
return GetProcAddress(library, symbol); |
| 915 |
} |
| 916 |
|
| 917 |
static void win_close_library(void *library) |
| 918 |
{ |
| 919 |
ASSERT(library); |
| 920 |
FreeLibrary(library); |
| 921 |
} |
| 922 |
|
| 923 |
static ALLEGRO_SYSTEM_INTERFACE *_al_system_win_driver(void) |
| 924 |
{ |
| 925 |
if (vt) return vt; |
| 926 |
|
| 927 |
vt = al_calloc(1, sizeof *vt); |
| 928 |
|
| 929 |
vt->id = ALLEGRO_SYSTEM_ID_WINDOWS; |
| 930 |
vt->initialize = win_initialize; |
| 931 |
vt->get_display_driver = win_get_display_driver; |
| 932 |
vt->get_keyboard_driver = win_get_keyboard_driver; |
| 933 |
vt->get_mouse_driver = win_get_mouse_driver; |
| 934 |
vt->get_touch_input_driver = win_get_touch_input_driver; |
| 935 |
vt->get_haptic_driver = win_get_haptic_driver; |
| 936 |
vt->get_joystick_driver = win_get_joystick_driver; |
| 937 |
vt->get_num_display_modes = win_get_num_display_modes; |
| 938 |
vt->get_display_mode = win_get_display_mode; |
| 939 |
vt->shutdown_system = win_shutdown; |
| 940 |
vt->get_num_video_adapters = win_get_num_video_adapters; |
| 941 |
vt->create_mouse_cursor = _al_win_create_mouse_cursor; |
| 942 |
vt->destroy_mouse_cursor = _al_win_destroy_mouse_cursor; |
| 943 |
vt->get_monitor_info = win_get_monitor_info; |
| 944 |
vt->get_monitor_dpi = win_get_monitor_dpi; |
| 945 |
vt->get_monitor_refresh_rate = win_get_monitor_refresh_rate; |
| 946 |
vt->get_cursor_position = win_get_cursor_position; |
| 947 |
vt->grab_mouse = win_grab_mouse; |
| 948 |
vt->ungrab_mouse = win_ungrab_mouse; |
| 949 |
vt->get_path = _al_win_get_path; |
| 950 |
vt->inhibit_screensaver = win_inhibit_screensaver; |
| 951 |
vt->open_library = win_open_library; |
| 952 |
vt->import_symbol = win_import_symbol; |
| 953 |
vt->close_library = win_close_library; |
| 954 |
vt->get_time = _al_win_get_time; |
| 955 |
vt->rest = _al_win_rest; |
| 956 |
vt->init_timeout = _al_win_init_timeout; |
| 957 |
|
| 958 |
return vt; |
| 959 |
} |
| 960 |
|
| 961 |
void _al_register_system_interfaces() |
| 962 |
{ |
| 963 |
ALLEGRO_SYSTEM_INTERFACE **add; |
| 964 |
|
| 965 |
add = _al_vector_alloc_back(&_al_system_interfaces); |
| 966 |
*add = _al_system_win_driver(); |
| 967 |
} |
| 968 |
|
| 969 |
const char* _al_win_error(DWORD err) { |
| 970 |
LPTSTR msg = NULL; |
| 971 |
int len; |
| 972 |
static char buf[2048]; |
| 973 |
|
| 974 |
len = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, err, 0, (LPTSTR)&msg, 0, NULL); |
| 975 |
if (len == 0) { |
| 976 |
_al_sane_strncpy(buf, "(Unable to decode the error code)", sizeof(buf)); |
| 977 |
} |
| 978 |
else { |
| 979 |
/* Truncate trailing CRLF if it has one */ |
| 980 |
if (len >= 2 && _tcscmp(msg + len - 2, TEXT("\r\n")) == 0) { |
| 981 |
msg[len - 2] = (TCHAR)0; |
| 982 |
} |
| 983 |
_twin_copy_tchar_to_utf8(buf, msg, sizeof(buf)); |
| 984 |
LocalFree(msg); |
| 985 |
} |
| 986 |
return buf; |
| 987 |
} |
| 988 |
const char* _al_win_last_error() { |
| 989 |
return _al_win_error(GetLastError()); |
| 990 |
} |
| 991 |
/* vim: set sts=3 sw=3 et: */ |