Skip to content

Commit 968476c

Browse files
committed
(#20) Split keycode.c
1 parent 2e3ec50 commit 968476c

File tree

5 files changed

+168
-167
lines changed

5 files changed

+168
-167
lines changed

CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ set(CMAKE_CXX_STANDARD 17)
44
project(libnut)
55

66
# Source
7-
set(SOURCE_FILES "src/main.cc" "src/deadbeef_rand.c" "src/keycode.c" "src/MMBitmap.c")
7+
set(SOURCE_FILES "src/main.cc" "src/deadbeef_rand.c" "src/MMBitmap.c")
88
if (UNIX AND NOT APPLE)
9-
set(SOURCE_FILES "${SOURCE_FILES}" "src/linux/keypress.c" "src/linux/mouse.c" "src/linux/screen.c" "src/linux/screengrab.c" "src/linux/xdisplay.c" "src/linux/highlightwindow.c" "src/linux/window_manager.cc")
9+
set(SOURCE_FILES "${SOURCE_FILES}" "src/linux/keycode.c" "src/linux/keypress.c" "src/linux/mouse.c" "src/linux/screen.c" "src/linux/screengrab.c" "src/linux/xdisplay.c" "src/linux/highlightwindow.c" "src/linux/window_manager.cc")
1010
elseif (UNIX AND APPLE)
11-
set(SOURCE_FILES "${SOURCE_FILES}" "src/macos/keypress.c" "src/macos/mouse.c" "src/macos/screen.c" "src/macos/screengrab.c" "src/macos/highlightwindow.m" "src/macos/window_manager.mm")
11+
set(SOURCE_FILES "${SOURCE_FILES}" "src/macos/keycode.c" "src/macos/keypress.c" "src/macos/mouse.c" "src/macos/screen.c" "src/macos/screengrab.c" "src/macos/highlightwindow.m" "src/macos/window_manager.mm")
1212
elseif (WIN32)
13-
set(SOURCE_FILES "${SOURCE_FILES}" "src/win32/keypress.c" "src/win32/mouse.c" "src/win32/screen.c" "src/win32/screengrab.c" "src/win32/highlightwindow.c" "src/win32/window_manager.cc")
13+
set(SOURCE_FILES "${SOURCE_FILES}" "src/win32/keycode.c" "src/win32/keypress.c" "src/win32/mouse.c" "src/win32/screen.c" "src/win32/screengrab.c" "src/win32/highlightwindow.c" "src/win32/window_manager.cc")
1414
endif()
1515
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
1616

src/keycode.c

Lines changed: 0 additions & 163 deletions
This file was deleted.

src/linux/keycode.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include "../keycode.h"
2+
3+
/*
4+
* Structs to store key mappings not handled by XStringToKeysym() on some
5+
* Linux systems.
6+
*/
7+
8+
struct XSpecialCharacterMapping {
9+
char name;
10+
MMKeyCode code;
11+
};
12+
13+
struct XSpecialCharacterMapping XSpecialCharacterTable[] = {
14+
{'~', XK_asciitilde},
15+
{'_', XK_underscore},
16+
{'[', XK_bracketleft},
17+
{']', XK_bracketright},
18+
{'!', XK_exclam},
19+
{'\'', XK_quotedbl},
20+
{'#', XK_numbersign},
21+
{'$', XK_dollar},
22+
{'%', XK_percent},
23+
{'&', XK_ampersand},
24+
{'\'', XK_quoteright},
25+
{'*', XK_asterisk},
26+
{'+', XK_plus},
27+
{',', XK_comma},
28+
{'-', XK_minus},
29+
{'.', XK_period},
30+
{'?', XK_question},
31+
{'<', XK_less},
32+
{'>', XK_greater},
33+
{'=', XK_equal},
34+
{'@', XK_at},
35+
{':', XK_colon},
36+
{';', XK_semicolon},
37+
{'\\', XK_backslash},
38+
{'`', XK_grave},
39+
{'{', XK_braceleft},
40+
{'}', XK_braceright},
41+
{'|', XK_bar},
42+
{'^', XK_asciicircum},
43+
{'(', XK_parenleft},
44+
{')', XK_parenright},
45+
{' ', XK_space},
46+
{'/', XK_slash},
47+
{'\t', XK_Tab},
48+
{'\n', XK_Return}
49+
};
50+
51+
MMKeyCode keyCodeForChar(const char c)
52+
{
53+
MMKeyCode code;
54+
55+
char buf[2];
56+
buf[0] = c;
57+
buf[1] = '\0';
58+
59+
code = XStringToKeysym(buf);
60+
if (code == NoSymbol) {
61+
/* Some special keys are apparently not handled properly by
62+
* XStringToKeysym() on some systems, so search for them instead in our
63+
* mapping table. */
64+
size_t i;
65+
const size_t specialCharacterCount =
66+
sizeof(XSpecialCharacterTable) / sizeof(XSpecialCharacterTable[0]);
67+
for (i = 0; i < specialCharacterCount; ++i) {
68+
if (c == XSpecialCharacterTable[i].name) {
69+
code = XSpecialCharacterTable[i].code;
70+
break;
71+
}
72+
}
73+
}
74+
75+
return code;
76+
}

src/macos/keycode.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "../keycode.h"
2+
3+
#include <CoreFoundation/CoreFoundation.h>
4+
#include <Carbon/Carbon.h> /* For kVK_ constants, and TIS functions. */
5+
6+
/* Returns string representation of key, if it is printable.
7+
* Ownership follows the Create Rule; that is, it is the caller's
8+
* responsibility to release the returned object. */
9+
CFStringRef createStringForKey(CGKeyCode keyCode);
10+
11+
MMKeyCode keyCodeForChar(const char c)
12+
{
13+
/* OS X does not appear to have a built-in function for this, so instead we
14+
* have to write our own. */
15+
static CFMutableDictionaryRef charToCodeDict = NULL;
16+
CGKeyCode code;
17+
UniChar character = c;
18+
CFStringRef charStr = NULL;
19+
20+
/* Generate table of keycodes and characters. */
21+
if (charToCodeDict == NULL)
22+
{
23+
size_t i;
24+
charToCodeDict = CFDictionaryCreateMutable(kCFAllocatorDefault,
25+
128,
26+
&kCFCopyStringDictionaryKeyCallBacks,
27+
NULL);
28+
if (charToCodeDict == NULL)
29+
return UINT16_MAX;
30+
31+
/* Loop through every keycode (0 - 127) to find its current mapping. */
32+
for (i = 0; i < 128; ++i)
33+
{
34+
CFStringRef string = createStringForKey((CGKeyCode)i);
35+
if (string != NULL)
36+
{
37+
CFDictionaryAddValue(charToCodeDict, string, (const void *)i);
38+
CFRelease(string);
39+
}
40+
}
41+
}
42+
43+
charStr = CFStringCreateWithCharacters(kCFAllocatorDefault, &character, 1);
44+
45+
/* Our values may be NULL (0), so we need to use this function. */
46+
if (!CFDictionaryGetValueIfPresent(charToCodeDict, charStr,
47+
(const void **)&code))
48+
{
49+
code = UINT16_MAX; /* Error */
50+
}
51+
52+
CFRelease(charStr);
53+
return (MMKeyCode)code;
54+
}
55+
56+
CFStringRef createStringForKey(CGKeyCode keyCode)
57+
{
58+
TISInputSourceRef currentKeyboard = TISCopyCurrentASCIICapableKeyboardInputSource();
59+
CFDataRef layoutData =
60+
TISGetInputSourceProperty(currentKeyboard,
61+
kTISPropertyUnicodeKeyLayoutData);
62+
const UCKeyboardLayout *keyboardLayout =
63+
(const UCKeyboardLayout *)CFDataGetBytePtr(layoutData);
64+
65+
UInt32 keysDown = 0;
66+
UniChar chars[4];
67+
UniCharCount realLength;
68+
69+
UCKeyTranslate(keyboardLayout,
70+
keyCode,
71+
kUCKeyActionDisplay,
72+
0,
73+
LMGetKbdType(),
74+
kUCKeyTranslateNoDeadKeysBit,
75+
&keysDown,
76+
sizeof(chars) / sizeof(chars[0]),
77+
&realLength,
78+
chars);
79+
CFRelease(currentKeyboard);
80+
81+
return CFStringCreateWithCharacters(kCFAllocatorDefault, chars, 1);
82+
}

src/win32/keycode.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "../keycode.h"
2+
3+
MMKeyCode keyCodeForChar(const char c)
4+
{
5+
return VkKeyScan(c);
6+
}

0 commit comments

Comments
 (0)