comparison src/events/SDL_keyboard.c @ 1674:7688a73b25b1 SDL-1.3

Finishing up keyboard code revamp, at least for now...
author Sam Lantinga <slouken@libsdl.org>
date Sun, 11 Jun 2006 05:27:10 +0000
parents 624e1412fbba
children 153477a6cc31
comparison
equal deleted inserted replaced
1673:624e1412fbba 1674:7688a73b25b1
28 #include "SDL_events_c.h" 28 #include "SDL_events_c.h"
29 #include "SDL_sysevents.h" 29 #include "SDL_sysevents.h"
30 30
31 31
32 /* Global keyboard information */ 32 /* Global keyboard information */
33 int SDL_TranslateUNICODE = 0;
33 static int SDL_num_keyboards; 34 static int SDL_num_keyboards;
34 static int SDL_current_keyboard; 35 static int SDL_current_keyboard;
35 static SDL_Keyboard **SDL_keyboards; 36 static SDL_Keyboard **SDL_keyboards;
36 int SDL_TranslateUNICODE = 0;
37 37
38 static const char *SDL_keynames[SDLK_LAST]; /* Array of keycode names */ 38 static const char *SDL_keynames[SDLK_LAST]; /* Array of keycode names */
39 39
40 /* Public functions */ 40 /* Public functions */
41 int 41 int
43 { 43 {
44 int i; 44 int i;
45 45
46 /* Set default mode of UNICODE translation */ 46 /* Set default mode of UNICODE translation */
47 SDL_EnableUNICODE(DEFAULT_UNICODE_TRANSLATION); 47 SDL_EnableUNICODE(DEFAULT_UNICODE_TRANSLATION);
48
49 /* Set default keyboard repeat setting */
50 SDL_EnableKeyRepeat(0, 0);
51 48
52 /* Initialize the tables */ 49 /* Initialize the tables */
53 for (i = 0; i < SDL_arraysize(SDL_keynames); ++i) { 50 for (i = 0; i < SDL_arraysize(SDL_keynames); ++i) {
54 switch (i) { 51 switch (i) {
55 case SDLK_BACKSPACE: 52 case SDLK_BACKSPACE:
135 case SDLK_RIGHT: 132 case SDLK_RIGHT:
136 SDL_keynames[i] = "right"; 133 SDL_keynames[i] = "right";
137 break; 134 break;
138 case SDLK_LEFT: 135 case SDLK_LEFT:
139 SDL_keynames[i] = "left"; 136 SDL_keynames[i] = "left";
140 break;
141 case SDLK_DOWN:
142 SDL_keynames[i] = "down";
143 break; 137 break;
144 case SDLK_INSERT: 138 case SDLK_INSERT:
145 SDL_keynames[i] = "insert"; 139 SDL_keynames[i] = "insert";
146 break; 140 break;
147 case SDLK_HOME: 141 case SDLK_HOME:
345 void 339 void
346 SDL_ResetKeyboard(int index) 340 SDL_ResetKeyboard(int index)
347 { 341 {
348 SDL_Keyboard *keyboard = SDL_GetKeyboard(index); 342 SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
349 SDL_keysym keysym; 343 SDL_keysym keysym;
350 SDLKey key; 344 Uint16 key;
351 345
352 if (!keyboard) { 346 if (!keyboard) {
353 return; 347 return;
354 } 348 }
355 349
356 SDL_memset(&keysym, 0, (sizeof keysym)); 350 SDL_memset(&keysym, 0, (sizeof keysym));
357 for (key = SDLK_FIRST; key < SDLK_LAST; ++key) { 351 for (key = SDLK_FIRST; key < SDLK_LAST; ++key) {
358 if (keyboard->keystate[key] == SDL_PRESSED) { 352 if (keyboard->keystate[key] == SDL_PRESSED) {
359 keysym.sym = key; 353 keysym.sym = key;
360 SDL_SendKeyboardKey(index, SDL_RELEASED, &keysym); 354 SDL_SendKeyboardKey(index, 0, SDL_RELEASED, &keysym);
361 } 355 }
362 } 356 }
363 keyboard->keyrepeat.timestamp = 0; 357 keyboard->repeat.timestamp = 0;
364 } 358 }
365 359
366 void 360 void
367 SDL_KeyboardQuit(void) 361 SDL_KeyboardQuit(void)
368 { 362 {
408 } 402 }
409 403
410 Uint8 * 404 Uint8 *
411 SDL_GetKeyState(int *numkeys) 405 SDL_GetKeyState(int *numkeys)
412 { 406 {
413 if (numkeys != (int *) 0) 407 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
408
409 if (numkeys != (int *) 0) {
414 *numkeys = SDLK_LAST; 410 *numkeys = SDLK_LAST;
415 return (SDL_KeyState); 411 }
412
413 if (!keyboard) {
414 return NULL;
415 }
416 return keyboard->keystate;
416 } 417 }
417 418
418 SDLMod 419 SDLMod
419 SDL_GetModState(void) 420 SDL_GetModState(void)
420 { 421 {
421 SDL_Keyboard *keyboard = SDL_GetKeyboard(index); 422 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
422 423
423 if (!keyboard) { 424 if (!keyboard) {
424 return KMOD_NONE; 425 return KMOD_NONE;
425 } 426 }
426 return keyboard->modstate; 427 return keyboard->modstate;
427 } 428 }
428 429
429 void 430 void
430 SDL_SetModState(SDLMod modstate) 431 SDL_SetModState(SDLMod modstate)
431 { 432 {
432 SDL_Keyboard *keyboard = SDL_GetKeyboard(index); 433 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
433 434
434 if (!keyboard) { 435 if (!keyboard) {
435 return; 436 return;
436 } 437 }
437 keyboard->modstate = modstate; 438 keyboard->modstate = modstate;
440 const char * 441 const char *
441 SDL_GetKeyName(SDLKey key) 442 SDL_GetKeyName(SDLKey key)
442 { 443 {
443 const char *keyname; 444 const char *keyname;
444 445
445 keyname = keynames[key]; 446 if (key < SDL_tablesize(SDL_keynames)) {
447 keyname = SDL_keynames[key];
448 } else {
449 keyname = NULL;
450 }
446 if (keyname == NULL) { 451 if (keyname == NULL) {
447 if (key < 256) { 452 if (key < 256) {
448 static char temp[4]; 453 static char temp[4];
449 FIXME:Convert to UTF - 8 keyname = temp; 454 char *cvt;
455 temp[0] = (char) key;
456 temp[1] = '\0';
457 cvt = SDL_iconv_string("UTF-8", "LATIN1", temp, 1);
458 SDL_strlcpy(temp, cvt, SDL_arraysize(temp));
459 SDL_free(cvt);
460 keyname = temp;
450 } else { 461 } else {
451 keyname = "unknown key"; 462 keyname = "unknown key";
452 } 463 }
453 } 464 }
454 return keyname; 465 return keyname;
455 } 466 }
456 467
457 /* These are global for SDL_eventloop.c */
458 int 468 int
459 SDL_SendKeyboardKey(int index, Uint8 state, const SDL_keysym * keysym) 469 SDL_SendKeyboardKey(int index, SDL_WindowID windowID, Uint8 state,
460 { 470 SDL_keysym * keysym)
461 SDL_Event event; 471 {
472 SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
462 int posted, repeatable; 473 int posted, repeatable;
463 Uint16 modstate; 474 Uint16 modstate;
464 475 Uint8 type;
465 SDL_memset(&event, 0, sizeof(event)); 476
466 477 if (!keyboard) {
478 return 0;
479 }
480
481 if (windowID) {
482 keyboard->focus = windowID;
483 }
467 #if 0 484 #if 0
468 printf("The '%s' key has been %s\n", SDL_GetKeyName(keysym->sym), 485 printf("The '%s' key has been %s\n", SDL_GetKeyName(keysym->sym),
469 state == SDL_PRESSED ? "pressed" : "released"); 486 state == SDL_PRESSED ? "pressed" : "released");
470 #endif 487 #endif
471 /* Set up the keysym */ 488 /* Set up the keysym */
472 modstate = (Uint16) SDL_ModState; 489 modstate = keyboard->modstate;
473 490
474 repeatable = 0; 491 repeatable = 0;
475 492
476 if (state == SDL_PRESSED) { 493 if (state == SDL_PRESSED) {
477 keysym->mod = (SDLMod) modstate; 494 keysym->mod = modstate;
478 switch (keysym->sym) { 495 switch (keysym->sym) {
479 case SDLK_UNKNOWN: 496 case SDLK_UNKNOWN:
480 break; 497 break;
481 case SDLK_NUMLOCK: 498 case SDLK_NUMLOCK:
482 modstate ^= KMOD_NUM; 499 modstate ^= KMOD_NUM;
483 if (!(modstate & KMOD_NUM)) 500 if (!(modstate & KMOD_NUM))
484 state = SDL_RELEASED; 501 state = SDL_RELEASED;
485 keysym->mod = (SDLMod) modstate; 502 keysym->mod = modstate;
486 break; 503 break;
487 case SDLK_CAPSLOCK: 504 case SDLK_CAPSLOCK:
488 modstate ^= KMOD_CAPS; 505 modstate ^= KMOD_CAPS;
489 if (!(modstate & KMOD_CAPS)) 506 if (!(modstate & KMOD_CAPS))
490 state = SDL_RELEASED; 507 state = SDL_RELEASED;
491 keysym->mod = (SDLMod) modstate; 508 keysym->mod = modstate;
492 break; 509 break;
493 case SDLK_LCTRL: 510 case SDLK_LCTRL:
494 modstate |= KMOD_LCTRL; 511 modstate |= KMOD_LCTRL;
495 break; 512 break;
496 case SDLK_RCTRL: 513 case SDLK_RCTRL:
557 modstate &= ~KMOD_MODE; 574 modstate &= ~KMOD_MODE;
558 break; 575 break;
559 default: 576 default:
560 break; 577 break;
561 } 578 }
562 keysym->mod = (SDLMod) modstate; 579 keysym->mod = modstate;
563 } 580 }
564 581
565 /* Figure out what type of event this is */ 582 /* Figure out what type of event this is */
566 switch (state) { 583 switch (state) {
567 case SDL_PRESSED: 584 case SDL_PRESSED:
568 event.type = SDL_KEYDOWN; 585 type = SDL_KEYDOWN;
569 break; 586 break;
570 case SDL_RELEASED: 587 case SDL_RELEASED:
571 event.type = SDL_KEYUP; 588 type = SDL_KEYUP;
572 /* 589 /*
573 * jk 991215 - Added 590 * jk 991215 - Added
574 */ 591 */
575 if (SDL_KeyRepeat.timestamp && 592 if (keyboard->repeat.timestamp &&
576 SDL_KeyRepeat.evt.key.keysym.sym == keysym->sym) { 593 keyboard->repeat.evt.key.keysym.sym == keysym->sym) {
577 SDL_KeyRepeat.timestamp = 0; 594 keyboard->repeat.timestamp = 0;
578 } 595 }
579 break; 596 break;
580 default: 597 default:
581 /* Invalid state -- bail */ 598 /* Invalid state -- bail */
582 return (0); 599 return 0;
583 } 600 }
584 601
585 if (keysym->sym != SDLK_UNKNOWN) { 602 if (keysym->sym != SDLK_UNKNOWN) {
586 /* Drop events that don't change state */ 603 /* Drop events that don't change state */
587 if (SDL_KeyState[keysym->sym] == state) { 604 if (keyboard->keystate[keysym->sym] == state) {
588 #if 0 605 #if 0
589 printf("Keyboard event didn't change state - dropped!\n"); 606 printf("Keyboard event didn't change state - dropped!\n");
590 #endif 607 #endif
591 return (0); 608 return 0;
592 } 609 }
593 610
594 /* Update internal keyboard state */ 611 /* Update internal keyboard state */
595 SDL_ModState = (SDLMod) modstate; 612 keyboard->modstate = modstate;
596 SDL_KeyState[keysym->sym] = state; 613 keyboard->keystate[keysym->sym] = state;
597 } 614 }
598 615
599 /* Post the event, if desired */ 616 /* Post the event, if desired */
600 posted = 0; 617 posted = 0;
601 if (SDL_ProcessEvents[event.type] == SDL_ENABLE) { 618 if (SDL_ProcessEvents[type] == SDL_ENABLE) {
619 SDL_Event event;
620 event.key.type = type;
621 event.key.which = (Uint8) index;
602 event.key.state = state; 622 event.key.state = state;
603 event.key.keysym = *keysym; 623 event.key.keysym = *keysym;
624 event.key.windowID = keyboard->focus;
604 /* 625 /*
605 * jk 991215 - Added 626 * jk 991215 - Added
606 */ 627 */
607 if (repeatable && (SDL_KeyRepeat.delay != 0)) { 628 if (repeatable && (keyboard->repeat.delay != 0)) {
608 SDL_KeyRepeat.evt = event; 629 Uint32 timestamp = SDL_GetTicks();
609 SDL_KeyRepeat.firsttime = 1; 630 if (!timestamp) {
610 SDL_KeyRepeat.timestamp = SDL_GetTicks(); 631 timestamp = 1;
632 }
633 keyboard->repeat.evt = event;
634 keyboard->repeat.firsttime = 1;
635 keyboard->repeat.timestamp = 1;
611 } 636 }
612 if ((SDL_EventOK == NULL) || SDL_EventOK(&event)) { 637 if ((SDL_EventOK == NULL) || SDL_EventOK(&event)) {
613 posted = 1; 638 posted = 1;
614 SDL_PushEvent(&event); 639 SDL_PushEvent(&event);
615 } 640 }
621 * jk 991215 - Added 646 * jk 991215 - Added
622 */ 647 */
623 void 648 void
624 SDL_CheckKeyRepeat(void) 649 SDL_CheckKeyRepeat(void)
625 { 650 {
626 if (SDL_KeyRepeat.timestamp) { 651 int i;
627 Uint32 now, interval; 652
628 653 for (i = 0; i < SDL_num_keyboards; ++i) {
629 now = SDL_GetTicks(); 654 SDL_Keyboard *keyboard = SDL_keyboards[i];
630 interval = (now - SDL_KeyRepeat.timestamp); 655
631 if (SDL_KeyRepeat.firsttime) { 656 if (!keyboard) {
632 if (interval > (Uint32) SDL_KeyRepeat.delay) { 657 continue;
633 SDL_KeyRepeat.timestamp = now; 658 }
634 SDL_KeyRepeat.firsttime = 0; 659
635 } 660 if (keyboard->repeat.timestamp) {
636 } else { 661 Uint32 now, interval;
637 if (interval > (Uint32) SDL_KeyRepeat.interval) { 662
638 SDL_KeyRepeat.timestamp = now; 663 now = SDL_GetTicks();
639 if ((SDL_EventOK == NULL) 664 interval = (now - keyboard->repeat.timestamp);
640 || SDL_EventOK(&SDL_KeyRepeat.evt)) { 665 if (keyboard->repeat.firsttime) {
641 SDL_PushEvent(&SDL_KeyRepeat.evt); 666 if (interval > (Uint32) keyboard->repeat.delay) {
667 keyboard->repeat.timestamp = now;
668 keyboard->repeat.firsttime = 0;
669 }
670 } else {
671 if (interval > (Uint32) keyboard->repeat.interval) {
672 keyboard->repeat.timestamp = now;
673 if ((SDL_EventOK == NULL)
674 || SDL_EventOK(&keyboard->repeat.evt)) {
675 SDL_PushEvent(&keyboard->repeat.evt);
676 }
642 } 677 }
643 } 678 }
644 } 679 }
645 } 680 }
646 } 681 }
647 682
648 int 683 int
649 SDL_EnableKeyRepeat(int delay, int interval) 684 SDL_EnableKeyRepeat(int delay, int interval)
650 { 685 {
686 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
687
688 if (!keyboard) {
689 SDL_SetError("No keyboard is currently selected");
690 return -1;
691 }
692
651 if ((delay < 0) || (interval < 0)) { 693 if ((delay < 0) || (interval < 0)) {
652 SDL_SetError("keyboard repeat value less than zero"); 694 SDL_SetError("keyboard repeat value less than zero");
653 return (-1); 695 return -1;
654 } 696 }
655 SDL_KeyRepeat.firsttime = 0; 697
656 SDL_KeyRepeat.delay = delay; 698 keyboard->repeat.firsttime = 0;
657 SDL_KeyRepeat.interval = interval; 699 keyboard->repeat.delay = delay;
658 SDL_KeyRepeat.timestamp = 0; 700 keyboard->repeat.interval = interval;
659 return (0); 701 keyboard->repeat.timestamp = 0;
702
703 return 0;
660 } 704 }
661 705
662 void 706 void
663 SDL_GetKeyRepeat(int *delay, int *interval) 707 SDL_GetKeyRepeat(int *delay, int *interval)
664 { 708 {
665 *delay = SDL_KeyRepeat.delay; 709 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
666 *interval = SDL_KeyRepeat.interval; 710
711 if (!keyboard) {
712 if (delay) {
713 *delay = 0;
714 }
715 if (interval) {
716 *interval = 0;
717 }
718 return;
719 }
720 if (delay) {
721 *delay = keyboard->repeat.delay;
722 }
723 if (interval) {
724 *interval = keyboard->repeat.interval;
725 }
667 } 726 }
668 727
669 /* vi: set ts=4 sw=4 expandtab: */ 728 /* vi: set ts=4 sw=4 expandtab: */