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
/*
#include <CoreFoundation/CoreFoundation.h>
#include <CoreGraphics/CoreGraphics.h>
#include <Carbon/Carbon.h>
#include <_ctype.h>

typedef enum VirtualKey {
    VK_AT_SIGN = 1,
} VirtualKey;

void PostKeyboardEvent(CGKeyCode virtualKey, bool keyDown)
{
    CGEventRef theEvent = CGEventCreateKeyboardEvent(NULL, virtualKey, keyDown);
    CGEventPost(kCGHIDEventTap, theEvent);
    CFRelease(theEvent);
}

void PostMouseEvent(CGMouseButton button, CGEventType type, const CGPoint point)
{
    CGEventRef theEvent = CGEventCreateMouseEvent(NULL, type, point, button);
    CGEventSetType(theEvent, type);
    CGEventPost(kCGHIDEventTap, theEvent);
    CFRelease(theEvent);
}

CFStringRef createStringForKey(CGKeyCode keyCode)
{
    TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource();
    CFDataRef layoutData = (CFDataRef)
        TISGetInputSourceProperty(currentKeyboard,
                                  kTISPropertyUnicodeKeyLayoutData);
    const UCKeyboardLayout *keyboardLayout =
        (const UCKeyboardLayout *)CFDataGetBytePtr(layoutData);

    UInt32 keysDown = 0;
    UniChar chars[4];
    UniCharCount realLength;

    UCKeyTranslate(keyboardLayout,
                   keyCode,
                   kUCKeyActionDisplay,
                   0,
                   LMGetKbdType(),
                   kUCKeyTranslateNoDeadKeysBit,
                   &keysDown,
                   sizeof(chars) / sizeof(chars[0]),
                   &realLength,
                   chars);
    CFRelease(currentKeyboard);

    return CFStringCreateWithCharacters(kCFAllocatorDefault, chars, 1);
}

CGKeyCode keyCodeForChar(const char c)
{
    static CFMutableDictionaryRef charToCodeDict = NULL;
    CGKeyCode code;
    UniChar character = c;
    CFStringRef charStr = NULL;

    /* Generate table of keycodes and characters. */
    if (charToCodeDict == NULL) {
        size_t i;
        charToCodeDict = CFDictionaryCreateMutable(kCFAllocatorDefault,
                                                   128,
                                                   &kCFCopyStringDictionaryKeyCallBacks,
                                                   NULL);
        if (charToCodeDict == NULL) return UINT16_MAX;

        /* Loop through every keycode (0 - 127) to find its current mapping. */
        for (i = 0; i < 128; ++i) {
            CFStringRef string = createStringForKey((CGKeyCode)i);
            if (string != NULL) {
                CFDictionaryAddValue(charToCodeDict, string, (const void *)i);
                CFRelease(string);
            }
        }
    }

    charStr = CFStringCreateWithCharacters(kCFAllocatorDefault, &character, 1);

    /* Our values may be NULL (0), so we need to use this function. */
    if (!CFDictionaryGetValueIfPresent(charToCodeDict, charStr,
                                       (const void **)&code)) {
        code = UINT16_MAX;
    }

    CFRelease(charStr);
    return code;
}

extern "C" void CodeGlassSystem_Initialize(void) {
    // Does nothing, just verifies DLL works
}

// CGDisplayMoveCursorToPoint(CGMainDisplayID(), point);
extern "C" void CodeGlassSystem_SetCursorLocation(float x, float y)
{
    CGPoint point = { .x = x, .y = y };
    CGDisplayMoveCursorToPoint(CGMainDisplayID(), point);
}

extern "C" void CodeGlassSystem_Click(float x, float y)
{
    CGPoint point = { x, y };
    PostMouseEvent(kCGMouseButtonLeft, kCGEventMouseMoved, point);
    PostMouseEvent(kCGMouseButtonLeft, kCGEventLeftMouseDown, point);
    PostMouseEvent(kCGMouseButtonLeft, kCGEventLeftMouseUp, point);
}

extern "C" void CodeGlassSystem_TypeChar(char character)
{
    if (isupper(character)) {
        PostKeyboardEvent(kVK_Shift, true);
    }

    CGKeyCode keyCode = keyCodeForChar(character);
    PostKeyboardEvent(keyCode, true);
    PostKeyboardEvent(keyCode, false);

    if (isupper(character)) {
        PostKeyboardEvent(kVK_Shift, false);
    }
}

extern "C" void CodeGlassSystem_TypeVirtualKey(VirtualKey key)
{
    switch (key) {
        case VK_AT_SIGN:
            PostKeyboardEvent(kVK_Shift, true);
            PostKeyboardEvent(kVK_ANSI_2, true);
            PostKeyboardEvent(kVK_ANSI_2, false);
            PostKeyboardEvent(kVK_Shift, false);
            break;
        default:
            break;
    }
}

*/