RetroArch/led/drivers/led_win32_keyboard.c

119 lines
2.1 KiB
C
Raw Normal View History

2020-09-20 03:16:11 +03:00
#include <stdio.h>
#include "../led_driver.h"
#include "../led_defines.h"
#include "../../configuration.h"
#include "../../retroarch.h"
#undef MAX_LEDS
#define MAX_LEDS 3
#ifdef _WIN32
#include <windows.h>
#endif
static void key_translate(int *key)
{
#ifdef _WIN32
switch (*key)
{
2021-01-02 05:45:02 +02:00
case 0:
2020-09-20 03:16:11 +03:00
*key = VK_NUMLOCK;
break;
2021-01-02 05:45:02 +02:00
case 1:
2020-09-20 03:16:11 +03:00
*key = VK_CAPITAL;
break;
2021-01-02 05:45:02 +02:00
case 2:
2020-09-20 03:16:11 +03:00
*key = VK_SCROLL;
break;
}
#endif
}
2021-01-02 05:45:02 +02:00
typedef struct
2020-09-20 03:16:11 +03:00
{
2021-01-02 05:45:02 +02:00
int setup[MAX_LEDS];
int state[MAX_LEDS];
int map[MAX_LEDS];
bool init;
} keyboard_led_t;
2020-09-20 03:16:11 +03:00
2021-01-02 05:45:02 +02:00
/* TODO/FIXME - static globals */
static keyboard_led_t win32kb_curins;
static keyboard_led_t *win32kb_cur = &win32kb_curins;
2020-09-20 03:16:11 +03:00
2021-01-02 05:45:02 +02:00
static int keyboard_led(int led, int state)
2020-09-20 03:16:11 +03:00
{
2021-01-02 05:45:02 +02:00
int status;
int key = led;
if ((led < 0) || (led >= MAX_LEDS))
return -1;
2020-09-20 03:16:11 +03:00
key_translate(&key);
#ifdef _WIN32
status = GetKeyState(key);
#endif
2021-01-02 05:45:02 +02:00
if (state == -1)
return status;
2020-09-20 03:16:11 +03:00
2021-01-02 05:45:02 +02:00
if ((state && !status) ||
(!state && status))
{
#ifdef _WIN32
keybd_event(key, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
keybd_event(key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
win32kb_cur->state[led] = state;
#endif
}
return -1;
}
2020-09-20 03:16:11 +03:00
static void keyboard_init(void)
{
int i;
settings_t *settings = config_get_ptr();
2021-01-02 05:45:02 +02:00
if (!settings || win32kb_cur->init)
2020-09-20 03:16:11 +03:00
return;
for (i = 0; i < MAX_LEDS; i++)
{
2021-01-02 05:45:02 +02:00
win32kb_cur->setup[i] = keyboard_led(i, -1);
win32kb_cur->state[i] = -1;
win32kb_cur->map[i] = settings->uints.led_map[i];
2020-09-22 03:05:58 +02:00
if (win32kb_cur->map[i] < 0)
2021-01-02 05:45:02 +02:00
win32kb_cur->map[i] = i;
2020-09-20 03:16:11 +03:00
}
2021-01-02 05:45:02 +02:00
win32kb_cur->init = true;
2020-09-20 03:16:11 +03:00
}
static void keyboard_free(void)
{
int i;
for (i = 0; i < MAX_LEDS; i++)
{
2021-01-02 05:45:02 +02:00
if (win32kb_cur->state[i] != -1 &&
win32kb_cur->state[i] != win32kb_cur->setup[i])
keyboard_led(i, win32kb_cur->setup[i]);
2020-09-20 03:16:11 +03:00
}
}
static void keyboard_set(int led, int state)
{
if ((led < 0) || (led >= MAX_LEDS))
return;
2021-01-02 05:45:02 +02:00
keyboard_led(win32kb_cur->map[led], state);
2020-09-20 03:16:11 +03:00
}
const led_driver_t keyboard_led_driver = {
keyboard_init,
keyboard_free,
keyboard_set,
"Keyboard"
};