comparison src/video/cocoa/SDL_cocoakeyboard.m @ 3247:2dcfb4e07a57

From: Jjgod Jiang <gzjjgod@gmail.com> Date: Sat, 5 Sep 2009 17:32:28 +0800 Replace old Keyboard Layout APIs with new Text Input Sources APIs for 10.{5,6}
author Sam Lantinga <slouken@libsdl.org>
date Sat, 05 Sep 2009 09:55:25 +0000
parents 99210400e8b9
children 00cace2d9080
comparison
equal deleted inserted replaced
3246:c843261f74a4 3247:2dcfb4e07a57
349 } 349 }
350 350
351 static void 351 static void
352 UpdateKeymap(SDL_VideoData *data) 352 UpdateKeymap(SDL_VideoData *data)
353 { 353 {
354 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1050
355 TISInputSourceRef key_layout;
356 #else
354 KeyboardLayoutRef key_layout; 357 KeyboardLayoutRef key_layout;
358 #endif
355 const void *chr_data; 359 const void *chr_data;
356 int i; 360 int i;
357 SDL_scancode scancode; 361 SDL_scancode scancode;
358 SDLKey keymap[SDL_NUM_SCANCODES]; 362 SDLKey keymap[SDL_NUM_SCANCODES];
359 363
360 /* See if the keymap needs to be updated */ 364 /* See if the keymap needs to be updated */
365 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1050
366 key_layout = TISCopyCurrentKeyboardLayoutInputSource();
367 #else
361 KLGetCurrentKeyboardLayout(&key_layout); 368 KLGetCurrentKeyboardLayout(&key_layout);
369 #endif
362 if (key_layout == data->key_layout) { 370 if (key_layout == data->key_layout) {
363 return; 371 return;
364 } 372 }
365 data->key_layout = key_layout; 373 data->key_layout = key_layout;
366 374
367 SDL_GetDefaultKeymap(keymap); 375 SDL_GetDefaultKeymap(keymap);
368 376
369 /* Try Unicode data first (preferred as of Mac OS X 10.5) */ 377 /* Try Unicode data first (preferred as of Mac OS X 10.5) */
378 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1050
379 CFDataRef uchrDataRef = TISGetInputSourceProperty(key_layout, kTISPropertyUnicodeKeyLayoutData);
380 if (uchrDataRef)
381 chr_data = CFDataGetBytePtr(uchrDataRef);
382 else
383 goto cleanup;
384 #else
370 KLGetKeyboardLayoutProperty(key_layout, kKLuchrData, &chr_data); 385 KLGetKeyboardLayoutProperty(key_layout, kKLuchrData, &chr_data);
386 #endif
371 if (chr_data) { 387 if (chr_data) {
372 UInt32 keyboard_type = LMGetKbdType(); 388 UInt32 keyboard_type = LMGetKbdType();
373 OSStatus err; 389 OSStatus err;
374 390
375 for (i = 0; i < SDL_arraysize(darwin_scancode_table); i++) { 391 for (i = 0; i < SDL_arraysize(darwin_scancode_table); i++) {
383 (keymap[scancode] & SDLK_SCANCODE_MASK)) { 399 (keymap[scancode] & SDLK_SCANCODE_MASK)) {
384 continue; 400 continue;
385 } 401 }
386 402
387 dead_key_state = 0; 403 dead_key_state = 0;
388 err = UCKeyTranslate (chr_data, i, kUCKeyActionDown, 404 err = UCKeyTranslate ((UCKeyboardLayout *) chr_data,
405 i, kUCKeyActionDown,
389 0, keyboard_type, 406 0, keyboard_type,
390 kUCKeyTranslateNoDeadKeysMask, 407 kUCKeyTranslateNoDeadKeysMask,
391 &dead_key_state, 8, &len, s); 408 &dead_key_state, 8, &len, s);
392 if (err != noErr) 409 if (err != noErr)
393 continue; 410 continue;
398 } 415 }
399 SDL_SetKeymap(data->keyboard, 0, keymap, SDL_NUM_SCANCODES); 416 SDL_SetKeymap(data->keyboard, 0, keymap, SDL_NUM_SCANCODES);
400 return; 417 return;
401 } 418 }
402 419
420 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1050
421 cleanup:
422 CFRelease(key_layout);
423 #else
403 /* Fall back to older style key map data */ 424 /* Fall back to older style key map data */
404 KLGetKeyboardLayoutProperty(key_layout, kKLKCHRData, &chr_data); 425 KLGetKeyboardLayoutProperty(key_layout, kKLKCHRData, &chr_data);
405 if (chr_data) { 426 if (chr_data) {
406 for (i = 0; i < 128; i++) { 427 for (i = 0; i < 128; i++) {
407 UInt32 c, state = 0; 428 UInt32 c, state = 0;
447 } 468 }
448 } 469 }
449 SDL_SetKeymap(data->keyboard, 0, keymap, SDL_NUM_SCANCODES); 470 SDL_SetKeymap(data->keyboard, 0, keymap, SDL_NUM_SCANCODES);
450 return; 471 return;
451 } 472 }
473 #endif
452 } 474 }
453 475
454 void 476 void
455 Cocoa_InitKeyboard(_THIS) 477 Cocoa_InitKeyboard(_THIS)
456 { 478 {