aboutsummaryrefslogtreecommitdiffstats
path: root/src/native/hid/KeyboardUtil_mac.c
blob: b514be3934377f3d8c30ea8a6b88f0966eb5a92d (plain)
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
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

/**
 * \file KeyboardUtil_unix.c
 * \brief Mac OS X specific code to press/release keys.
 * \author Sebastien Vincent
 */

#include <ApplicationServices/ApplicationServices.h>
#include <Carbon/Carbon.h>

#include "KeyboardUtil.h"

/**
 * \brief Get the keycode from a specific keyboard layout.
 * \param c ascii character
 * \param uchrHeader keyboard layout
 * \param pshift if the character need shift modifier, value will have 1 otherwise 0
 * \param palt if the character need alt modifier, value will have 1 otherwise 0
 */
void checkModifiers(const char c, const UCKeyboardLayout* uchrHeader, CGKeyCode keycode, int* pshift, int* palt)
{
  int alt = (optionKey >> 8) & 0xff;
  int shift = (shiftKey >> 8) & 0xff;
  int altShift = ((optionKey | shiftKey) >> 8) & 0xff;
  UInt32 deadKeyState = 0;
  UniCharCount count;
  char character;

  if(UCKeyTranslate(uchrHeader, keycode, kUCKeyActionDown, alt, LMGetKbdType(), 0,
        &deadKeyState, 1, &count, &character) == 0 && character == c)
  {
    *palt = 1;
  }
  else if(UCKeyTranslate(uchrHeader, keycode, kUCKeyActionDown, shift, LMGetKbdType(), 0,
        &deadKeyState, 1, &count, &character) == 0 && character == c)
  {
    *pshift = 1;
  }
  else if(UCKeyTranslate(uchrHeader, keycode, kUCKeyActionDown, altShift, LMGetKbdType(), 0,
        &deadKeyState, 1, &count, &character) == 0 && character == c)
  {
    *pshift = 1;
    *palt = 1;
  }
}

/* following two functions has been taken from
 * http://stackoverflow.com/questions/1918841/how-to-convert-ascii-character-to-cgkeycode
 */

/**
 * \brief Get the keycode from a specific keyboard layout.
 * \param c ascii character
 * \param uchrHeader keyboard layout
 * \param shift if the character needs shift modifier, value will have 1 otherwise 0
 * \param alt if the character needs alt modifier, value will have 1 otherwise 0
 * \return keycode or UINT16_MAX if not found
 * \note Function taken from http://stackoverflow.com/questions/1918841/how-to-convert-ascii-character-to-cgkeycode
 */
CGKeyCode keyCodeForCharWithLayout(const char c,
    const UCKeyboardLayout *uchrHeader, int* shift, int* alt)
{
  uint8_t *uchrData = (uint8_t *)uchrHeader;
  UCKeyboardTypeHeader *uchrKeyboardList = (UCKeyboardTypeHeader*)uchrHeader->keyboardTypeList;

  /* Loop through the keyboard type list. */
  ItemCount i, j;
  for (i = 0; i < uchrHeader->keyboardTypeCount; ++i)
  {
    /* Get a pointer to the keyToCharTable structure. */
    UCKeyToCharTableIndex *uchrKeyIX = (UCKeyToCharTableIndex *)
      (uchrData + (uchrKeyboardList[i].keyToCharTableIndexOffset));

    /* Not sure what this is for but it appears to be a safeguard... */
    UCKeyStateRecordsIndex *stateRecordsIndex;
    if(uchrKeyboardList[i].keyStateRecordsIndexOffset != 0)
    {
      stateRecordsIndex = (UCKeyStateRecordsIndex *)
        (uchrData + (uchrKeyboardList[i].keyStateRecordsIndexOffset));

      if((stateRecordsIndex->keyStateRecordsIndexFormat) !=
          kUCKeyStateRecordsIndexFormat)
      {
        stateRecordsIndex = NULL;
      }
    }
    else
    {
      stateRecordsIndex = NULL;
    }

    /* Make sure structure is a table that can be searched. */
    if((uchrKeyIX->keyToCharTableIndexFormat) != kUCKeyToCharTableIndexFormat)
    {
      continue;
    }

    /* Check the table of each keyboard for character */
    for (j = 0; j < uchrKeyIX->keyToCharTableCount; ++j)
    {
      UCKeyOutput *keyToCharData =
        (UCKeyOutput *)(uchrData + (uchrKeyIX->keyToCharTableOffsets[j]));

      /* Check THIS table of the keyboard for the character. */
      UInt16 k;
      for (k = 0; k < uchrKeyIX->keyToCharTableSize; ++k)
      {
        /* Here's the strange safeguard again... */
        if((keyToCharData[k] & kUCKeyOutputTestForIndexMask) ==
            kUCKeyOutputStateIndexMask)
        {
          long keyIndex = (keyToCharData[k] & kUCKeyOutputGetIndexMask);
          if(stateRecordsIndex != NULL &&
              keyIndex <= (stateRecordsIndex->keyStateRecordCount))
          {
            UCKeyStateRecord *stateRecord = (UCKeyStateRecord *)
              (uchrData +
               (stateRecordsIndex->keyStateRecordOffsets[keyIndex]));

            if((stateRecord->stateZeroCharData) == c)
            {
              checkModifiers(c, uchrHeader, k, shift, alt);
              return (CGKeyCode)k;
            }
          }
          else if(keyToCharData[k] == c)
          {
            checkModifiers(c, uchrHeader, k, shift, alt);
            return (CGKeyCode)k;
          }
        }
        else if(((keyToCharData[k] & kUCKeyOutputTestForIndexMask)
              != kUCKeyOutputSequenceIndexMask) &&
            keyToCharData[k] != 0xFFFE &&
            keyToCharData[k] != 0xFFFF &&
            keyToCharData[k] == c)
        {
          /* try to see if character is obtained with modifiers such as 
           * Option (alt) or shift
           */
          checkModifiers(c, uchrHeader, k, shift, alt);
          return (CGKeyCode)k;
        }
      }
    }
  }

  return UINT16_MAX;
}

/**
 * \brief Get the keycode from a specific keyboard layout.
 * \param c ascii character
 * \param shift if the character need shift modifier, value will have 1 otherwise 0
 * \param alt if the character need shift modifier, value will have 1 otherwise 0
 * \return keycode or UINT16_MAX if not found
 * \note Function taken from http://stackoverflow.com/questions/1918841/how-to-convert-ascii-character-to-cgkeycode
 */
CGKeyCode keyCodeForChar(const char c, int* shift, int* alt)
{
  CFDataRef currentLayoutData;
  TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource();

  if(currentKeyboard == NULL)
  {
    printf("Could not find keyboard layout\n");
    return UINT16_MAX;
  }

  currentLayoutData = TISGetInputSourceProperty(currentKeyboard,
      kTISPropertyUnicodeKeyLayoutData);
  CFRelease(currentKeyboard);
  if(currentLayoutData == NULL)
  {
    printf("Could not find layout data\n");
    return UINT16_MAX;
  }

  return keyCodeForCharWithLayout(c,
      (const UCKeyboardLayout *)CFDataGetBytePtr(currentLayoutData), shift, alt);
}

void generateSymbol(const char* symbol, jboolean pressed)
{
  /* avoid warnings */
  symbol = symbol;
  pressed = pressed;
}

void generateKey(jchar key, jboolean pressed)
{
  int keycode = -1; 
  int shift = 0;
  int alt = 0;
  CGEventRef e = NULL;
  int flags = 0;

  keycode = keyCodeForChar((char)key, &shift, &alt);

  if(keycode == UINT16_MAX)
  {
    return;
  }

  e = CGEventCreateKeyboardEvent(NULL, keycode, pressed ? 1 : 0);

  if(pressed && shift)
  {
    flags |= kCGEventFlagMaskShift;
  }

  if(pressed && alt)
  {
    flags |= kCGEventFlagMaskAlternate;
  }

  CGEventSetFlags(e, flags);

  CGEventPost(kCGSessionEventTap, e);
  CFRelease(e);
}