1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
|
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2020 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef _MSC_VER
#pragma comment(lib, "dinput8")
#endif
#define WIN32_LEAN_AND_MEAN
#undef DIRECTINPUT_VERSION
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>
#include <dbt.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <boolean.h>
#include <windowsx.h>
#ifndef WM_MOUSEHWHEEL
#define WM_MOUSEHWHEEL 0x20e
#endif
#ifndef WM_MOUSEWHEEL
#define WM_MOUSEWHEEL 0x020A
#endif
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
#include <string/stdstring.h>
#ifndef _XBOX
#include "../../gfx/common/win32_common.h"
#endif
#include "../input_keymaps.h"
#include "../../configuration.h"
#include "../../retroarch.h"
#include "../../verbosity.h"
/* Context has to be global as joypads also ride on this context. */
LPDIRECTINPUT8 g_dinput_ctx;
struct dinput_pointer_status
{
struct dinput_pointer_status *next;
int pointer_id;
int pointer_x;
int pointer_y;
};
enum dinput_input_flags
{
DINP_FLAG_SHIFT_L = (1 << 0),
DINP_FLAG_SHIFT_R = (1 << 1),
DINP_FLAG_ALT_L = (1 << 2),
DINP_FLAG_ALT_R = (1 << 3),
DINP_FLAG_DBCLK_ON_TITLEBAR = (1 << 4),
DINP_FLAG_MOUSE_L_BTN = (1 << 5),
DINP_FLAG_MOUSE_R_BTN = (1 << 6),
DINP_FLAG_MOUSE_M_BTN = (1 << 7),
DINP_FLAG_MOUSE_B4_BTN = (1 << 8),
DINP_FLAG_MOUSE_B5_BTN = (1 << 9),
DINP_FLAG_MOUSE_WU_BTN = (1 << 10),
DINP_FLAG_MOUSE_WD_BTN = (1 << 11),
DINP_FLAG_MOUSE_HWU_BTN = (1 << 12),
DINP_FLAG_MOUSE_HWD_BTN = (1 << 13),
DINP_FLAG_MOUSE_IGNORE = (1 << 14)
};
struct dinput_input
{
char *joypad_drv_name;
LPDIRECTINPUTDEVICE8 keyboard;
LPDIRECTINPUTDEVICE8 mouse;
const input_device_driver_t *joypad;
struct dinput_pointer_status pointer_head; /* dummy head for easy iteration */
int window_pos_x;
int window_pos_y;
int mouse_rel_x;
int mouse_rel_y;
int mouse_x;
int mouse_y;
uint16_t flags;
uint8_t state[256];
};
void dinput_destroy_context(void)
{
if (!g_dinput_ctx)
return;
IDirectInput8_Release(g_dinput_ctx);
g_dinput_ctx = NULL;
}
bool dinput_init_context(void)
{
if (!g_dinput_ctx)
{
/* Who said we shouldn't have same call signature in a COM API? <_< */
#ifdef __cplusplus
if (!(SUCCEEDED(DirectInput8Create(
GetModuleHandle(NULL), DIRECTINPUT_VERSION,
IID_IDirectInput8,
(void**)&g_dinput_ctx, NULL))))
#else
if (!(SUCCEEDED(DirectInput8Create(
GetModuleHandle(NULL), DIRECTINPUT_VERSION,
&IID_IDirectInput8,
(void**)&g_dinput_ctx, NULL))))
#endif
return false;
}
return true;
}
static void *dinput_init(const char *joypad_driver)
{
struct dinput_input *di = NULL;
if (!dinput_init_context())
return NULL;
if (!(di = (struct dinput_input*)calloc(1, sizeof(*di))))
return NULL;
if (!string_is_empty(joypad_driver))
di->joypad_drv_name = strdup(joypad_driver);
#ifdef __cplusplus
if (FAILED(IDirectInput8_CreateDevice(g_dinput_ctx,
GUID_SysKeyboard,
&di->keyboard, NULL)))
#else
if (FAILED(IDirectInput8_CreateDevice(g_dinput_ctx,
&GUID_SysKeyboard,
&di->keyboard, NULL)))
#endif
{
di->keyboard = NULL;
}
#ifdef __cplusplus
if (FAILED(IDirectInput8_CreateDevice(g_dinput_ctx,
GUID_SysMouse,
&di->mouse, NULL)))
#else
if (FAILED(IDirectInput8_CreateDevice(g_dinput_ctx,
&GUID_SysMouse,
&di->mouse, NULL)))
#endif
{
di->mouse = NULL;
}
if (di->keyboard)
{
bool input_nowinkey_enable = config_get_ptr()->bools.input_nowinkey_enable;
DWORD flags = DISCL_NONEXCLUSIVE | DISCL_FOREGROUND;
if (input_nowinkey_enable)
flags |= DISCL_NOWINKEY;
IDirectInputDevice8_SetDataFormat(di->keyboard, &c_dfDIKeyboard);
IDirectInputDevice8_SetCooperativeLevel(di->keyboard,
(HWND)video_driver_window_get(), flags);
IDirectInputDevice8_Acquire(di->keyboard);
}
if (di->mouse)
{
IDirectInputDevice8_SetDataFormat(di->mouse, &c_dfDIMouse2);
IDirectInputDevice8_SetCooperativeLevel(di->mouse, (HWND)video_driver_window_get(),
DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
IDirectInputDevice8_Acquire(di->mouse);
}
input_keymaps_init_keyboard_lut(rarch_key_map_dinput);
#ifndef _XBOX
SetWindowLongPtr(main_window.hwnd, GWLP_USERDATA, (LONG_PTR)di);
#endif
return di;
}
static uint16_t dinput_get_active_keyboard_mods()
{
uint16_t mod = 0;
if (GetKeyState(VK_SHIFT) & 0x80)
mod |= RETROKMOD_SHIFT;
if (GetKeyState(VK_CONTROL) & 0x80)
mod |= RETROKMOD_CTRL;
if (GetKeyState(VK_MENU) & 0x80)
mod |= RETROKMOD_ALT;
if (GetKeyState(VK_CAPITAL) & 0x81)
mod |= RETROKMOD_CAPSLOCK;
if (GetKeyState(VK_SCROLL) & 0x81)
mod |= RETROKMOD_SCROLLOCK;
if (GetKeyState(VK_NUMLOCK) & 0x81)
mod |= RETROKMOD_NUMLOCK;
if ((GetKeyState(VK_LWIN) | GetKeyState(VK_RWIN)) & 0x80)
mod |= RETROKMOD_META;
return mod;
}
static void dinput_poll(void *data)
{
struct dinput_input *di = (struct dinput_input*)data;
uint8_t *kb_state = NULL;
if (!di)
return;
kb_state = &di->state[0];
for (
; kb_state < di->state + 256
; kb_state++)
*kb_state = 0;
if (di->keyboard)
{
if (FAILED(IDirectInputDevice8_GetDeviceState(
di->keyboard, sizeof(di->state), di->state)))
{
IDirectInputDevice8_Acquire(di->keyboard);
if (FAILED(IDirectInputDevice8_GetDeviceState(
di->keyboard, sizeof(di->state), di->state)))
{
for (
; kb_state < di->state + 256
; kb_state++)
*kb_state = 0;
}
}
else
{
/* Ignore 'unknown/undefined' key */
di->state[RETROK_UNKNOWN] = 0;
}
/* If both shift keys are pressed simultaneously, the OS will not issue
* a WM_KEYUP for the first one. That up event will be issued here. */
if ((di->flags & DINP_FLAG_SHIFT_L) && !(GetAsyncKeyState(VK_LSHIFT) >> 1))
{
input_keyboard_event(false, RETROK_LSHIFT, 0,
dinput_get_active_keyboard_mods(), RETRO_DEVICE_KEYBOARD);
di->flags &= ~DINP_FLAG_SHIFT_L;
}
if ((di->flags & DINP_FLAG_SHIFT_R) && !(GetAsyncKeyState(VK_RSHIFT) >> 1))
{
input_keyboard_event(false, RETROK_RSHIFT, 0,
dinput_get_active_keyboard_mods(), RETRO_DEVICE_KEYBOARD);
di->flags &= ~DINP_FLAG_SHIFT_R;
}
/* When using alt-tab, the alt key won't get a WM_KEYUP message from the
* OS. Instead we issue it here when ALT isn't pressed down anymore. */
if ((di->flags & DINP_FLAG_ALT_L) && !(GetAsyncKeyState(VK_LMENU) >> 1))
{
input_keyboard_event(false, RETROK_LALT, 0,
dinput_get_active_keyboard_mods(), RETRO_DEVICE_KEYBOARD);
di->flags &= ~DINP_FLAG_ALT_L;
}
if ((di->flags & DINP_FLAG_ALT_R) && !(GetAsyncKeyState(VK_RMENU) >> 1))
{
input_keyboard_event(false, RETROK_RALT, 0,
dinput_get_active_keyboard_mods(), RETRO_DEVICE_KEYBOARD);
di->flags &= ~DINP_FLAG_ALT_R;
}
}
if (di->mouse)
{
POINT point;
DIMOUSESTATE2 mouse_state;
BYTE *rgb_buttons_ptr = &mouse_state.rgbButtons[0];
bool swap_mouse_buttons = (g_win32_flags & WIN32_CMN_FLAG_SWAP_MOUSE_BTNS) ? true : false;
point.x = 0;
point.y = 0;
mouse_state.lX = 0;
mouse_state.lY = 0;
mouse_state.lZ = 0;
for (
; rgb_buttons_ptr < mouse_state.rgbButtons + 8
; rgb_buttons_ptr++)
*rgb_buttons_ptr = 0;
if (FAILED(IDirectInputDevice8_GetDeviceState(
di->mouse, sizeof(mouse_state), &mouse_state)))
{
IDirectInputDevice8_Acquire(di->mouse);
if (FAILED(IDirectInputDevice8_GetDeviceState(
di->mouse, sizeof(mouse_state), &mouse_state)))
{
mouse_state.lX = 0;
mouse_state.lY = 0;
mouse_state.lZ = 0;
for (
; rgb_buttons_ptr < mouse_state.rgbButtons + 8
; rgb_buttons_ptr++)
*rgb_buttons_ptr = 0;
}
}
di->mouse_rel_x = mouse_state.lX;
di->mouse_rel_y = mouse_state.lY;
if (swap_mouse_buttons)
{
if (!mouse_state.rgbButtons[1])
di->flags &= ~DINP_FLAG_DBCLK_ON_TITLEBAR;
if (di->flags & DINP_FLAG_DBCLK_ON_TITLEBAR)
di->flags &= ~DINP_FLAG_MOUSE_R_BTN;
else
{
if (mouse_state.rgbButtons[0])
di->flags |= DINP_FLAG_MOUSE_R_BTN;
else
di->flags &= ~DINP_FLAG_MOUSE_R_BTN;
}
if (mouse_state.rgbButtons[1])
di->flags |= DINP_FLAG_MOUSE_L_BTN;
else
di->flags &= ~DINP_FLAG_MOUSE_L_BTN;
}
else
{
if (!mouse_state.rgbButtons[0])
di->flags &= ~DINP_FLAG_DBCLK_ON_TITLEBAR;
if (di->flags & DINP_FLAG_DBCLK_ON_TITLEBAR)
di->flags &= ~DINP_FLAG_MOUSE_L_BTN;
else
{
if (mouse_state.rgbButtons[0])
di->flags |= DINP_FLAG_MOUSE_L_BTN;
else
di->flags &= ~DINP_FLAG_MOUSE_L_BTN;
}
if (mouse_state.rgbButtons[1])
di->flags |= DINP_FLAG_MOUSE_R_BTN;
else
di->flags &= ~DINP_FLAG_MOUSE_R_BTN;
}
if (mouse_state.rgbButtons[2])
di->flags |= DINP_FLAG_MOUSE_M_BTN;
else
di->flags &= ~DINP_FLAG_MOUSE_M_BTN;
if (mouse_state.rgbButtons[3])
di->flags |= DINP_FLAG_MOUSE_B4_BTN;
else
di->flags &= ~DINP_FLAG_MOUSE_B4_BTN;
if (mouse_state.rgbButtons[4])
di->flags |= DINP_FLAG_MOUSE_B5_BTN;
else
di->flags &= ~DINP_FLAG_MOUSE_B5_BTN;
/* No simple way to get absolute coordinates
* for RETRO_DEVICE_POINTER. Just use Win32 APIs. */
GetCursorPos(&point);
ScreenToClient((HWND)video_driver_window_get(), &point);
di->mouse_x = point.x;
di->mouse_y = point.y;
/* Ignore application focusing mouse clicks */
if (di->flags & DINP_FLAG_MOUSE_IGNORE)
{
if (mouse_state.rgbButtons[0] || mouse_state.rgbButtons[1])
di->flags &= ~(DINP_FLAG_MOUSE_L_BTN | DINP_FLAG_MOUSE_R_BTN);
else if (!mouse_state.rgbButtons[0] && !mouse_state.rgbButtons[1])
di->flags &= ~DINP_FLAG_MOUSE_IGNORE;
}
}
}
static bool dinput_mouse_button_pressed(
struct dinput_input *di, unsigned port, unsigned key)
{
switch (key)
{
case RETRO_DEVICE_ID_MOUSE_LEFT:
return (di->flags & DINP_FLAG_MOUSE_L_BTN) ? true : false;
case RETRO_DEVICE_ID_MOUSE_RIGHT:
return (di->flags & DINP_FLAG_MOUSE_R_BTN) ? true : false;
case RETRO_DEVICE_ID_MOUSE_MIDDLE:
return (di->flags & DINP_FLAG_MOUSE_M_BTN) ? true : false;
case RETRO_DEVICE_ID_MOUSE_BUTTON_4:
return (di->flags & DINP_FLAG_MOUSE_B4_BTN) ? true : false;
case RETRO_DEVICE_ID_MOUSE_BUTTON_5:
return (di->flags & DINP_FLAG_MOUSE_B5_BTN) ? true : false;
case RETRO_DEVICE_ID_MOUSE_WHEELUP:
if (di->flags & DINP_FLAG_MOUSE_WU_BTN)
{
di->flags &= ~DINP_FLAG_MOUSE_WU_BTN;
return true;
}
break;
case RETRO_DEVICE_ID_MOUSE_WHEELDOWN:
if (di->flags & DINP_FLAG_MOUSE_WD_BTN)
{
di->flags &= ~DINP_FLAG_MOUSE_WD_BTN;
return true;
}
break;
case RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELUP:
if (di->flags & DINP_FLAG_MOUSE_HWU_BTN)
{
di->flags &= ~DINP_FLAG_MOUSE_HWU_BTN;
return true;
}
break;
case RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELDOWN:
if (di->flags & DINP_FLAG_MOUSE_HWD_BTN)
{
di->flags &= ~DINP_FLAG_MOUSE_HWD_BTN;
return true;
}
break;
}
return false;
}
static int16_t dinput_lightgun_aiming_state(
struct dinput_input *di, unsigned idx, unsigned id)
{
struct video_viewport vp = {0};
int16_t res_x = 0;
int16_t res_y = 0;
int16_t res_screen_x = 0;
int16_t res_screen_y = 0;
int x = 0;
int y = 0;
unsigned num = 0;
struct dinput_pointer_status
*check_pos = di->pointer_head.next;
while (check_pos && num < idx)
{
num++;
check_pos = check_pos->next;
}
if (!check_pos && idx > 0) /* idx = 0 has mouse fallback. */
return 0;
x = di->mouse_x;
y = di->mouse_y;
if (check_pos)
{
x = check_pos->pointer_x;
y = check_pos->pointer_y;
}
if (video_driver_translate_coord_viewport_wrap(
&vp, x, y,
&res_x, &res_y, &res_screen_x, &res_screen_y))
{
switch (id)
{
case RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X:
return res_x;
case RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y:
return res_y;
case RETRO_DEVICE_ID_LIGHTGUN_IS_OFFSCREEN:
return input_driver_pointer_is_offscreen(res_x, res_y);
default:
break;
}
}
return 0;
}
static int16_t dinput_input_state(
void *data,
const input_device_driver_t *joypad,
const input_device_driver_t *sec_joypad,
rarch_joypad_info_t *joypad_info,
const retro_keybind_set *binds,
bool keyboard_mapping_blocked,
unsigned port,
unsigned device,
unsigned idx,
unsigned id)
{
settings_t *settings;
struct dinput_input *di = (struct dinput_input*)data;
if (port < MAX_USERS)
{
switch (device)
{
case RETRO_DEVICE_JOYPAD:
{
int16_t ret = 0;
settings = config_get_ptr();
if (id == RETRO_DEVICE_ID_JOYPAD_MASK)
{
unsigned i;
if (settings->uints.input_mouse_index[port] == 0)
{
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
{
if (binds[port][i].valid)
{
if (dinput_mouse_button_pressed(di, port, binds[port][i].mbutton))
ret |= (1 << i);
}
}
}
if (!keyboard_mapping_blocked)
{
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
{
if (binds[port][i].valid)
{
if ( (binds[port][i].key && binds[port][i].key < RETROK_LAST)
&& di->state[rarch_keysym_lut[(enum retro_key)binds[port][i].key]] & 0x80)
ret |= (1 << i);
}
}
}
return ret;
}
if (id < RARCH_BIND_LIST_END)
{
if (binds[port][id].valid)
{
if ( binds[port][id].key && binds[port][id].key < RETROK_LAST
&& (di->state[rarch_keysym_lut[(enum retro_key)binds[port][id].key]] & 0x80)
&& (id == RARCH_GAME_FOCUS_TOGGLE || !keyboard_mapping_blocked)
)
return 1;
else if (settings->uints.input_mouse_index[port] == 0)
{
if (dinput_mouse_button_pressed(di, port, binds[port][id].mbutton))
return 1;
}
}
}
}
break;
case RETRO_DEVICE_KEYBOARD:
return (id && id < RETROK_LAST) && di->state[rarch_keysym_lut[(enum retro_key)id]] & 0x80;
case RETRO_DEVICE_ANALOG:
{
int16_t ret = 0;
int id_minus_key = 0;
int id_plus_key = 0;
unsigned id_minus = 0;
unsigned id_plus = 0;
bool id_plus_valid = false;
bool id_minus_valid = false;
input_conv_analog_id_to_bind_id(idx, id, id_minus, id_plus);
id_minus_valid = binds[port][id_minus].valid;
id_plus_valid = binds[port][id_plus].valid;
id_minus_key = binds[port][id_minus].key;
id_plus_key = binds[port][id_plus].key;
if (id_plus_valid && id_plus_key && id_plus_key < RETROK_LAST)
{
unsigned sym = rarch_keysym_lut[(enum retro_key)id_plus_key];
if (di->state[sym] & 0x80)
ret = 0x7fff;
}
if (id_minus_valid && id_minus_key && id_minus_key < RETROK_LAST)
{
unsigned sym = rarch_keysym_lut[(enum retro_key)id_minus_key];
if (di->state[sym] & 0x80)
ret += -0x7fff;
}
return ret;
}
break;
case RARCH_DEVICE_MOUSE_SCREEN:
settings = config_get_ptr();
if (settings->uints.input_mouse_index[port] != 0)
break;
switch (id)
{
case RETRO_DEVICE_ID_MOUSE_X:
return di->mouse_x;
case RETRO_DEVICE_ID_MOUSE_Y:
return di->mouse_y;
default:
break;
}
/* fall-through */
case RETRO_DEVICE_MOUSE:
settings = config_get_ptr();
if (settings->uints.input_mouse_index[port] == 0)
{
switch (id)
{
case RETRO_DEVICE_ID_MOUSE_X:
return di->mouse_rel_x;
case RETRO_DEVICE_ID_MOUSE_Y:
return di->mouse_rel_y;
case RETRO_DEVICE_ID_MOUSE_LEFT:
return (di->flags & DINP_FLAG_MOUSE_L_BTN) > 0;
case RETRO_DEVICE_ID_MOUSE_RIGHT:
return (di->flags & DINP_FLAG_MOUSE_R_BTN) > 0;
case RETRO_DEVICE_ID_MOUSE_WHEELUP:
if (di->flags & DINP_FLAG_MOUSE_WU_BTN)
{
di->flags &= ~DINP_FLAG_MOUSE_WU_BTN;
return 1;
}
di->flags &= ~DINP_FLAG_MOUSE_WU_BTN;
break;
case RETRO_DEVICE_ID_MOUSE_WHEELDOWN:
if (di->flags & DINP_FLAG_MOUSE_WD_BTN)
{
di->flags &= ~DINP_FLAG_MOUSE_WD_BTN;
return 1;
}
di->flags &= ~DINP_FLAG_MOUSE_WD_BTN;
break;
case RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELUP:
if (di->flags & DINP_FLAG_MOUSE_HWU_BTN)
{
di->flags &= ~DINP_FLAG_MOUSE_HWU_BTN;
return 1;
}
di->flags &= ~DINP_FLAG_MOUSE_HWU_BTN;
break;
case RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELDOWN:
if (di->flags & DINP_FLAG_MOUSE_HWD_BTN)
{
di->flags &= ~DINP_FLAG_MOUSE_HWD_BTN;
return 1;
}
di->flags &= ~DINP_FLAG_MOUSE_HWD_BTN;
break;
case RETRO_DEVICE_ID_MOUSE_MIDDLE:
return (di->flags & DINP_FLAG_MOUSE_M_BTN) > 0;
case RETRO_DEVICE_ID_MOUSE_BUTTON_4:
return (di->flags & DINP_FLAG_MOUSE_B4_BTN) > 0;
case RETRO_DEVICE_ID_MOUSE_BUTTON_5:
return (di->flags & DINP_FLAG_MOUSE_B5_BTN) > 0;
}
}
break;
case RETRO_DEVICE_POINTER:
case RARCH_DEVICE_POINTER_SCREEN:
{
struct video_viewport vp = {0};
int x = 0;
int y = 0;
int16_t res_x = 0;
int16_t res_y = 0;
int16_t res_screen_x = 0;
int16_t res_screen_y = 0;
unsigned num = 0;
struct dinput_pointer_status *
check_pos = di->pointer_head.next;
while (check_pos && num < idx)
{
num++;
check_pos = check_pos->next;
}
if (!check_pos && idx > 0) /* idx = 0 has mouse fallback. */
return 0;
x = di->mouse_x;
y = di->mouse_y;
if (check_pos)
{
x = check_pos->pointer_x;
y = check_pos->pointer_y;
}
if (video_driver_translate_coord_viewport_confined_wrap(&vp, x, y,
&res_x, &res_y, &res_screen_x, &res_screen_y))
{
if (device == RARCH_DEVICE_POINTER_SCREEN)
{
res_x = res_screen_x;
res_y = res_screen_y;
}
switch (id)
{
case RETRO_DEVICE_ID_POINTER_X:
return res_x;
case RETRO_DEVICE_ID_POINTER_Y:
return res_y;
case RETRO_DEVICE_ID_POINTER_PRESSED:
return check_pos ? 1 : (di->flags & DINP_FLAG_MOUSE_L_BTN) > 0;
case RETRO_DEVICE_ID_POINTER_IS_OFFSCREEN:
return input_driver_pointer_is_offscreen(res_x, res_y);
default:
break;
}
}
}
break;
case RETRO_DEVICE_LIGHTGUN:
switch (id)
{
/*aiming*/
case RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X:
case RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y:
case RETRO_DEVICE_ID_LIGHTGUN_IS_OFFSCREEN:
return dinput_lightgun_aiming_state(di, idx, id);
/*buttons*/
case RETRO_DEVICE_ID_LIGHTGUN_TRIGGER:
case RETRO_DEVICE_ID_LIGHTGUN_RELOAD:
case RETRO_DEVICE_ID_LIGHTGUN_AUX_A:
case RETRO_DEVICE_ID_LIGHTGUN_AUX_B:
case RETRO_DEVICE_ID_LIGHTGUN_AUX_C:
case RETRO_DEVICE_ID_LIGHTGUN_START:
case RETRO_DEVICE_ID_LIGHTGUN_SELECT:
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_UP:
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_DOWN:
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_LEFT:
case RETRO_DEVICE_ID_LIGHTGUN_DPAD_RIGHT:
case RETRO_DEVICE_ID_LIGHTGUN_PAUSE:
{
unsigned new_id = input_driver_lightgun_id_convert(id);
const uint64_t bind_joykey = input_config_binds[port][new_id].joykey;
const uint64_t bind_joyaxis = input_config_binds[port][new_id].joyaxis;
const uint64_t autobind_joykey = input_autoconf_binds[port][new_id].joykey;
const uint64_t autobind_joyaxis= input_autoconf_binds[port][new_id].joyaxis;
uint16_t joyport = joypad_info->joy_idx;
float axis_threshold = joypad_info->axis_threshold;
const uint64_t joykey = (bind_joykey != NO_BTN)
? bind_joykey : autobind_joykey;
const uint32_t joyaxis = (bind_joyaxis != AXIS_NONE)
? bind_joyaxis : autobind_joyaxis;
if (binds[port][new_id].valid)
{
if ((uint16_t)joykey != NO_BTN && joypad->button(
joyport, (uint16_t)joykey))
return 1;
if (joyaxis != AXIS_NONE &&
((float)abs(joypad->axis(joyport, joyaxis))
/ 0x8000) > axis_threshold)
return 1;
else if ((binds[port][new_id].key && binds[port][new_id].key < RETROK_LAST)
&& !keyboard_mapping_blocked
&& di->state[rarch_keysym_lut[(enum retro_key)binds[port][new_id].key]] & 0x80)
return 1;
else
{
settings = config_get_ptr();
if (settings->uints.input_mouse_index[port] == 0)
{
if (dinput_mouse_button_pressed(di, port, binds[port][new_id].mbutton))
return 1;
}
}
}
}
break;
/*deprecated*/
case RETRO_DEVICE_ID_LIGHTGUN_X:
return di->mouse_rel_x;
case RETRO_DEVICE_ID_LIGHTGUN_Y:
return di->mouse_rel_y;
}
break;
}
}
return 0;
}
/* These are defined in later SDKs, thus ifdeffed. */
#ifndef WM_POINTERUPDATE
#define WM_POINTERUPDATE 0x0245
#endif
#ifndef WM_POINTERDOWN
#define WM_POINTERDOWN 0x0246
#endif
#ifndef WM_POINTERUP
#define WM_POINTERUP 0x0247
#endif
#ifndef GET_POINTERID_WPARAM
#define GET_POINTERID_WPARAM(wParam) (LOWORD(wParam))
#endif
/* Stores X/Y in client coordinates. */
static void dinput_pointer_store_pos(
struct dinput_pointer_status *pointer, WPARAM lParam)
{
POINT point;
point.x = GET_X_LPARAM(lParam);
point.y = GET_Y_LPARAM(lParam);
ScreenToClient((HWND)video_driver_window_get(), &point);
pointer->pointer_x = point.x;
pointer->pointer_y = point.y;
}
static void dinput_add_pointer(struct dinput_input *di,
struct dinput_pointer_status *new_pointer)
{
struct dinput_pointer_status *insert_pos = NULL;
new_pointer->next = NULL;
insert_pos = &di->pointer_head;
while (insert_pos->next)
insert_pos = insert_pos->next;
insert_pos->next = new_pointer;
}
static void dinput_delete_pointer(struct dinput_input *di, int pointer_id)
{
struct dinput_pointer_status *check_pos = &di->pointer_head;
while (check_pos && check_pos->next)
{
if (check_pos->next->pointer_id == pointer_id)
{
struct dinput_pointer_status *to_delete = check_pos->next;
check_pos->next = check_pos->next->next;
free(to_delete);
}
check_pos = check_pos->next;
}
}
static struct dinput_pointer_status *dinput_find_pointer(
struct dinput_input *di, int pointer_id)
{
struct dinput_pointer_status *check_pos = di->pointer_head.next;
while (check_pos)
{
if (check_pos->pointer_id == pointer_id)
break;
check_pos = check_pos->next;
}
return check_pos;
}
static void dinput_clear_pointers(struct dinput_input *di)
{
struct dinput_pointer_status *pointer = &di->pointer_head;
while (pointer->next)
{
struct dinput_pointer_status *del = pointer->next;
pointer->next = pointer->next->next;
free(del);
}
}
bool dinput_handle_message(void *data,
UINT message, WPARAM wParam, LPARAM lParam)
{
struct dinput_input *di = (struct dinput_input *)data;
/* WM_POINTERDOWN : Arrives for each new touch event
* with a new ID - add to list.
* WM_POINTERUP : Arrives once the pointer is no
* longer down - remove from list.
* WM_POINTERUPDATE : arrives for both pressed and
* hovering pointers - ignore hovering
*/
switch (message)
{
case WM_SETFOCUS:
case WM_KILLFOCUS:
di->flags |= DINP_FLAG_MOUSE_IGNORE;
break;
case WM_NCLBUTTONDBLCLK:
di->flags |= DINP_FLAG_DBCLK_ON_TITLEBAR;
break;
case WM_MOUSEMOVE:
di->window_pos_x = GET_X_LPARAM(lParam);
di->window_pos_y = GET_Y_LPARAM(lParam);
break;
case WM_POINTERDOWN:
{
struct dinput_pointer_status *new_pointer =
(struct dinput_pointer_status *)malloc(sizeof(struct dinput_pointer_status));
if (new_pointer)
{
new_pointer->pointer_id = GET_POINTERID_WPARAM(wParam);
dinput_pointer_store_pos(new_pointer, lParam);
dinput_add_pointer(di, new_pointer);
return true;
}
}
break;
case WM_POINTERUP:
{
int pointer_id = GET_POINTERID_WPARAM(wParam);
dinput_delete_pointer(di, pointer_id);
}
return true;
case WM_POINTERUPDATE:
{
int pointer_id = GET_POINTERID_WPARAM(wParam);
struct dinput_pointer_status *pointer = dinput_find_pointer(di, pointer_id);
if (pointer)
dinput_pointer_store_pos(pointer, lParam);
}
return true;
case WM_DEVICECHANGE:
#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0500 /* 2K */
if ( wParam == DBT_DEVICEARRIVAL ||
wParam == DBT_DEVICEREMOVECOMPLETE)
{
PDEV_BROADCAST_HDR pHdr = (PDEV_BROADCAST_HDR)lParam;
/* TODO/FIXME: Don't destroy everything, let's just
* handle new devices gracefully */
if (pHdr->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
joypad_driver_reinit(di, di->joypad_drv_name);
}
#endif
break;
case WM_MOUSEWHEEL:
if (((short) HIWORD(wParam))/120 > 0)
di->flags |= DINP_FLAG_MOUSE_WU_BTN;
if (((short) HIWORD(wParam))/120 < 0)
di->flags |= DINP_FLAG_MOUSE_WD_BTN;
break;
case WM_MOUSEHWHEEL:
if (((short) HIWORD(wParam))/120 > 0)
di->flags |= DINP_FLAG_MOUSE_HWU_BTN;
if (((short) HIWORD(wParam))/120 < 0)
di->flags |= DINP_FLAG_MOUSE_HWD_BTN;
break;
case WM_KEYUP: /* Key released */
case WM_SYSKEYUP: /* Key released */
case WM_KEYDOWN: /* Key pressed */
case WM_SYSKEYDOWN: /* Key pressed */
{
unsigned keysym = (lParam >> 16) & 0xff;
bool extended = (lParam >> 24) & 0x1;
uint16_t flag = 0;
/* extended keys will map to dinput if the high bit is set */
if (extended)
keysym |= 0x80;
switch (keysym)
{
case DIK_LSHIFT: flag = DINP_FLAG_SHIFT_L; break;
case DIK_RSHIFT: flag = DINP_FLAG_SHIFT_R; break;
case DIK_LMENU: flag = DINP_FLAG_ALT_L; break;
case DIK_RMENU: flag = DINP_FLAG_ALT_R; break;
}
if (message == WM_KEYDOWN || message == WM_SYSKEYDOWN)
di->flags |= flag;
else if (di->flags & flag)
di->flags &= ~flag;
else if (flag) /* key up already issued or down never happened */
return true;
}
break;
}
return false;
}
static void dinput_free(void *data)
{
struct dinput_input *di = (struct dinput_input*)data;
LPDIRECTINPUT8 hold_ctx = g_dinput_ctx;
if (!di)
return;
/* Prevent a joypad driver to kill our context prematurely. */
g_dinput_ctx = NULL;
#ifndef _XBOX
SetWindowLongPtr(main_window.hwnd, GWLP_USERDATA, 0);
#endif
g_dinput_ctx = hold_ctx;
/* Clear any leftover pointers. */
dinput_clear_pointers(di);
if (di->keyboard)
IDirectInputDevice8_Release(di->keyboard);
if (di->mouse)
IDirectInputDevice8_Release(di->mouse);
if (di->joypad_drv_name)
free(di->joypad_drv_name);
di->joypad_drv_name = NULL;
free(di);
dinput_destroy_context();
}
static void dinput_grab_mouse(void *data, bool state)
{
struct dinput_input *di = (struct dinput_input*)data;
if (!di->mouse)
return;
IDirectInputDevice8_Unacquire(di->mouse);
IDirectInputDevice8_SetCooperativeLevel(di->mouse,
(HWND)video_driver_window_get(),
(DISCL_NONEXCLUSIVE | DISCL_FOREGROUND));
IDirectInputDevice8_Acquire(di->mouse);
#ifndef _XBOX
win32_clip_window(state);
#endif
}
static uint64_t dinput_get_capabilities(void *data)
{
return (1 << RETRO_DEVICE_JOYPAD)
| (1 << RETRO_DEVICE_MOUSE)
| (1 << RETRO_DEVICE_KEYBOARD)
| (1 << RETRO_DEVICE_LIGHTGUN)
| (1 << RETRO_DEVICE_POINTER)
| (1 << RETRO_DEVICE_ANALOG);
}
input_driver_t input_dinput = {
dinput_init,
dinput_poll,
dinput_input_state,
dinput_free,
NULL,
NULL,
dinput_get_capabilities,
"dinput",
dinput_grab_mouse,
NULL,
NULL
};
|