summaryrefslogtreecommitdiff
path: root/indra/llwindow/llkeyboardmacosx.cpp
blob: 4ce98ee32b90f4034ac164fb04a8b7258ee6f8c7 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
 * @file llkeyboardmacosx.cpp
 * @brief Handler for assignable key bindings
 *
 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#if LL_DARWIN

#include "linden_common.h"
#include "llkeyboardmacosx.h"
#include "llwindowcallbacks.h"

#include "llwindowmacosx-objc.h"

LLKeyboardMacOSX::LLKeyboardMacOSX()
{
    // Virtual keycode mapping table.  Yes, this was as annoying to generate as it looks.
    mTranslateKeyMap[0x00] = 'A';
    mTranslateKeyMap[0x01] = 'S';
    mTranslateKeyMap[0x02] = 'D';
    mTranslateKeyMap[0x03] = 'F';
    mTranslateKeyMap[0x04] = 'H';
    mTranslateKeyMap[0x05] = 'G';
    mTranslateKeyMap[0x06] = 'Z';
    mTranslateKeyMap[0x07] = 'X';
    mTranslateKeyMap[0x08] = 'C';
    mTranslateKeyMap[0x09] = 'V';
    mTranslateKeyMap[0x0b] = 'B';
    mTranslateKeyMap[0x0c] = 'Q';
    mTranslateKeyMap[0x0d] = 'W';
    mTranslateKeyMap[0x0e] = 'E';
    mTranslateKeyMap[0x0f] = 'R';
    mTranslateKeyMap[0x10] = 'Y';
    mTranslateKeyMap[0x11] = 'T';
    mTranslateKeyMap[0x12] = '1';
    mTranslateKeyMap[0x13] = '2';
    mTranslateKeyMap[0x14] = '3';
    mTranslateKeyMap[0x15] = '4';
    mTranslateKeyMap[0x16] = '6';
    mTranslateKeyMap[0x17] = '5';
    mTranslateKeyMap[0x18] = '=';   // KEY_EQUALS
    mTranslateKeyMap[0x19] = '9';
    mTranslateKeyMap[0x1a] = '7';
    mTranslateKeyMap[0x1b] = '-';   // KEY_HYPHEN
    mTranslateKeyMap[0x1c] = '8';
    mTranslateKeyMap[0x1d] = '0';
    mTranslateKeyMap[0x1e] = ']';
    mTranslateKeyMap[0x1f] = 'O';
    mTranslateKeyMap[0x20] = 'U';
    mTranslateKeyMap[0x21] = '[';
    mTranslateKeyMap[0x22] = 'I';
    mTranslateKeyMap[0x23] = 'P';
    mTranslateKeyMap[0x24] = KEY_RETURN,
    mTranslateKeyMap[0x25] = 'L';
    mTranslateKeyMap[0x26] = 'J';
    mTranslateKeyMap[0x27] = '\'';
    mTranslateKeyMap[0x28] = 'K';
    mTranslateKeyMap[0x29] = ';';
    mTranslateKeyMap[0x2a] = '\\';
    mTranslateKeyMap[0x2b] = ',';
    mTranslateKeyMap[0x2c] = KEY_DIVIDE;
    mTranslateKeyMap[0x2d] = 'N';
    mTranslateKeyMap[0x2e] = 'M';
    mTranslateKeyMap[0x2f] = '.';
    mTranslateKeyMap[0x30] = KEY_TAB;
    mTranslateKeyMap[0x31] = ' ';   // space!
    mTranslateKeyMap[0x32] = '`';
    mTranslateKeyMap[0x33] = KEY_BACKSPACE;
    mTranslateKeyMap[0x35] = KEY_ESCAPE;
    //mTranslateKeyMap[0x37] = 0;   // Command key.  (not used yet)
    mTranslateKeyMap[0x38] = KEY_SHIFT;
    mTranslateKeyMap[0x39] = KEY_CAPSLOCK;
    mTranslateKeyMap[0x3a] = KEY_ALT;
    mTranslateKeyMap[0x3b] = KEY_CONTROL;
    mTranslateKeyMap[0x41] = '.';   // keypad
    mTranslateKeyMap[0x43] = '*';   // keypad
    mTranslateKeyMap[0x45] = '+';   // keypad
    mTranslateKeyMap[0x4b] = KEY_PAD_DIVIDE;    // keypad
    mTranslateKeyMap[0x4c] = KEY_RETURN;    // keypad enter
    mTranslateKeyMap[0x4e] = '-';   // keypad
    mTranslateKeyMap[0x51] = '=';   // keypad
    mTranslateKeyMap[0x52] = '0';   // keypad
    mTranslateKeyMap[0x53] = '1';   // keypad
    mTranslateKeyMap[0x54] = '2';   // keypad
    mTranslateKeyMap[0x55] = '3';   // keypad
    mTranslateKeyMap[0x56] = '4';   // keypad
    mTranslateKeyMap[0x57] = '5';   // keypad
    mTranslateKeyMap[0x58] = '6';   // keypad
    mTranslateKeyMap[0x59] = '7';   // keypad
    mTranslateKeyMap[0x5b] = '8';   // keypad
    mTranslateKeyMap[0x5c] = '9';   // keypad
    mTranslateKeyMap[0x60] = KEY_F5;
    mTranslateKeyMap[0x61] = KEY_F6;
    mTranslateKeyMap[0x62] = KEY_F7;
    mTranslateKeyMap[0x63] = KEY_F3;
    mTranslateKeyMap[0x64] = KEY_F8;
    mTranslateKeyMap[0x65] = KEY_F9;
    mTranslateKeyMap[0x67] = KEY_F11;
    mTranslateKeyMap[0x6d] = KEY_F10;
    mTranslateKeyMap[0x6f] = KEY_F12;
    mTranslateKeyMap[0x72] = KEY_INSERT;
    mTranslateKeyMap[0x73] = KEY_HOME;
    mTranslateKeyMap[0x74] = KEY_PAGE_UP;
    mTranslateKeyMap[0x75] = KEY_DELETE;
    mTranslateKeyMap[0x76] = KEY_F4;
    mTranslateKeyMap[0x77] = KEY_END;
    mTranslateKeyMap[0x78] = KEY_F2;
    mTranslateKeyMap[0x79] = KEY_PAGE_DOWN;
    mTranslateKeyMap[0x7a] = KEY_F1;
    mTranslateKeyMap[0x7b] = KEY_LEFT;
    mTranslateKeyMap[0x7c] = KEY_RIGHT;
    mTranslateKeyMap[0x7d] = KEY_DOWN;
    mTranslateKeyMap[0x7e] = KEY_UP;

    // Build inverse map
    std::map<U16, KEY>::iterator iter;
    for (iter = mTranslateKeyMap.begin(); iter != mTranslateKeyMap.end(); iter++)
    {
        mInvTranslateKeyMap[iter->second] = iter->first;
    }

    // build numpad maps
    mTranslateNumpadMap[0x52] = KEY_PAD_INS;    // keypad 0
    mTranslateNumpadMap[0x53] = KEY_PAD_END;   // keypad 1
    mTranslateNumpadMap[0x54] = KEY_PAD_DOWN;   // keypad 2
    mTranslateNumpadMap[0x55] = KEY_PAD_PGDN;   // keypad 3
    mTranslateNumpadMap[0x56] = KEY_PAD_LEFT;   // keypad 4
    mTranslateNumpadMap[0x57] = KEY_PAD_CENTER; // keypad 5
    mTranslateNumpadMap[0x58] = KEY_PAD_RIGHT;  // keypad 6
    mTranslateNumpadMap[0x59] = KEY_PAD_HOME;   // keypad 7
    mTranslateNumpadMap[0x5b] = KEY_PAD_UP;     // keypad 8
    mTranslateNumpadMap[0x5c] = KEY_PAD_PGUP;   // keypad 9
    mTranslateNumpadMap[0x41] = KEY_PAD_DEL;    // keypad .
    mTranslateNumpadMap[0x4c] = KEY_PAD_RETURN; // keypad enter

    // Build inverse numpad map
    for (iter = mTranslateNumpadMap.begin(); iter != mTranslateNumpadMap.end(); iter++)
    {
        mInvTranslateNumpadMap[iter->second] = iter->first;
    }
}

void LLKeyboardMacOSX::resetMaskKeys()
{
    U32 mask = getModifiers();

    // MBW -- XXX -- This mirrors the operation of the Windows version of resetMaskKeys().
    //    It looks a bit suspicious, as it won't correct for keys that have been released.
    //    Is this the way it's supposed to work?

    // We apply the modifier masks directly within getModifiers.  So check to see which masks we've applied.

    if(mask & MAC_SHIFT_KEY)
    {
        mKeyLevel[KEY_SHIFT] = true;
    }

    if(mask & MAC_CTRL_KEY)
    {
        mKeyLevel[KEY_CONTROL] = true;
    }

    if(mask & MAC_ALT_KEY)
    {
        mKeyLevel[KEY_ALT] = true;
    }
}

/*
static bool translateKeyMac(const U16 key, const U32 mask, KEY &outKey, U32 &outMask)
{
    // Translate the virtual keycode into the keycodes the keyboard system expects.
    U16 virtualKey = (mask >> 24) & 0x0000007F;
    outKey = macKeyTransArray[virtualKey];


    return(outKey != 0);
}
*/

void LLKeyboardMacOSX::handleModifier(MASK mask)
{
    updateModifiers(mask);
}

MASK LLKeyboardMacOSX::updateModifiers(U32 mask)
{
    // translate the mask
    MASK out_mask = 0;

    if(mask & MAC_SHIFT_KEY)
    {
        out_mask |= MASK_SHIFT;
    }

    if(mask & (MAC_CTRL_KEY | MAC_CMD_KEY))
    {
        out_mask |= MASK_CONTROL;
    }

    if(mask & MAC_ALT_KEY)
    {
        out_mask |= MASK_ALT;
    }

    return out_mask;
}

bool LLKeyboardMacOSX::handleKeyDown(const U16 key, const U32 mask)
{
    KEY     translated_key = 0;
    U32     translated_mask = 0;
    bool    handled = false;

    translated_mask = updateModifiers(mask);

    if(translateNumpadKey(key, &translated_key))
    {
        handled = handleTranslatedKeyDown(translated_key, translated_mask);
    }

    return handled;
}


bool LLKeyboardMacOSX::handleKeyUp(const U16 key, const U32 mask)
{
    KEY     translated_key = 0;
    U32     translated_mask = 0;
    bool    handled = false;

    translated_mask = updateModifiers(mask);

    if(translateNumpadKey(key, &translated_key))
    {
        handled = handleTranslatedKeyUp(translated_key, translated_mask);
    }

    return handled;
}

MASK LLKeyboardMacOSX::currentMask(bool for_mouse_event)
{
    MASK result = MASK_NONE;
    U32 mask = getModifiers();

    if (mask & MAC_SHIFT_KEY)           result |= MASK_SHIFT;
    if (mask & MAC_CTRL_KEY)            result |= MASK_CONTROL;
    if (mask & MAC_ALT_KEY)             result |= MASK_ALT;

    // For keyboard events, consider Command equivalent to Control
    if (!for_mouse_event)
    {
        if (mask & MAC_CMD_KEY) result |= MASK_CONTROL;
    }

    return result;
}

void LLKeyboardMacOSX::scanKeyboard()
{
    S32 key;
    for (key = 0; key < KEY_COUNT; key++)
    {
        // Generate callback if any event has occurred on this key this frame.
        // Can't just test mKeyLevel, because this could be a slow frame and
        // key might have gone down then up. JC
        if (mKeyLevel[key] || mKeyDown[key] || mKeyUp[key])
        {
            mCurScanKey = key;
            mCallbacks->handleScanKey(key, mKeyDown[key], mKeyUp[key], mKeyLevel[key]);
        }
    }
    mCurScanKey = KEY_NONE;

    // Reset edges for next frame
    for (key = 0; key < KEY_COUNT; key++)
    {
        mKeyUp[key] = false;
        mKeyDown[key] = false;
        if (mKeyLevel[key])
        {
            mKeyLevelFrameCount[key]++;
        }
    }
}

bool LLKeyboardMacOSX::translateNumpadKey( const U16 os_key, KEY *translated_key )
{
    return translateKey(os_key, translated_key);
}

U16 LLKeyboardMacOSX::inverseTranslateNumpadKey(const KEY translated_key)
{
    return inverseTranslateKey(translated_key);
}

#endif // LL_DARWIN