1 #include <stdio.h>
2 #include "../led_driver.h"
3 #include "../led_defines.h"
4 
5 #include "../../configuration.h"
6 #include "../../retroarch.h"
7 
8 #undef MAX_LEDS
9 #define MAX_LEDS 3
10 
11 #ifdef _WIN32
12 #include <windows.h>
13 #endif
14 
key_translate(int * key)15 static void key_translate(int *key)
16 {
17 #ifdef _WIN32
18    switch (*key)
19    {
20       case 0:
21          *key = VK_NUMLOCK;
22          break;
23       case 1:
24          *key = VK_CAPITAL;
25          break;
26       case 2:
27          *key = VK_SCROLL;
28          break;
29    }
30 #endif
31 }
32 
33 typedef struct
34 {
35    int setup[MAX_LEDS];
36    int state[MAX_LEDS];
37    int map[MAX_LEDS];
38    bool init;
39 } keyboard_led_t;
40 
41 /* TODO/FIXME - static globals */
42 static keyboard_led_t win32kb_curins;
43 static keyboard_led_t *win32kb_cur = &win32kb_curins;
44 
keyboard_led(int led,int state)45 static int keyboard_led(int led, int state)
46 {
47    int status;
48    int key = led;
49 
50    if ((led < 0) || (led >= MAX_LEDS))
51       return -1;
52 
53    key_translate(&key);
54 
55 #ifdef _WIN32
56    status = GetKeyState(key);
57 #endif
58 
59    if (state == -1)
60       return status;
61 
62    if ((state && !status) ||
63        (!state && status))
64    {
65 #ifdef _WIN32
66       keybd_event(key, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
67       keybd_event(key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
68       win32kb_cur->state[led] = state;
69 #endif
70    }
71    return -1;
72 }
73 
keyboard_init(void)74 static void keyboard_init(void)
75 {
76    int i;
77    settings_t *settings = config_get_ptr();
78 
79    if (!settings || win32kb_cur->init)
80       return;
81 
82    for (i = 0; i < MAX_LEDS; i++)
83    {
84       win32kb_cur->setup[i] = keyboard_led(i, -1);
85       win32kb_cur->state[i] = -1;
86       win32kb_cur->map[i]   = settings->uints.led_map[i];
87       if (win32kb_cur->map[i] < 0)
88          win32kb_cur->map[i] = i;
89    }
90    win32kb_cur->init = true;
91 }
92 
keyboard_free(void)93 static void keyboard_free(void)
94 {
95    int i;
96 
97    for (i = 0; i < MAX_LEDS; i++)
98    {
99       if (win32kb_cur->state[i] != -1 &&
100           win32kb_cur->state[i] != win32kb_cur->setup[i])
101          keyboard_led(i, win32kb_cur->setup[i]);
102    }
103 }
104 
keyboard_set(int led,int state)105 static void keyboard_set(int led, int state)
106 {
107    if ((led < 0) || (led >= MAX_LEDS))
108       return;
109 
110    keyboard_led(win32kb_cur->map[led], state);
111 }
112 
113 const led_driver_t keyboard_led_driver = {
114    keyboard_init,
115    keyboard_free,
116    keyboard_set,
117    "Keyboard"
118 };
119