Mercurial > sdl-ios-xcode
comparison src/events/SDL_keyboard.c @ 4661:03dcb795c583
Merged changes from the main SDL codebase
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 12 Jul 2010 21:09:23 -0700 |
parents | 06c7423f8c60 |
children | 05ab4141ce93 |
comparison
equal
deleted
inserted
replaced
4660:b15e7017409b | 4661:03dcb795c583 |
---|---|
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 static int SDL_num_keyboards; | 33 |
34 static int SDL_current_keyboard; | 34 typedef struct SDL_Keyboard SDL_Keyboard; |
35 static SDL_Keyboard **SDL_keyboards; | 35 |
36 struct SDL_Keyboard | |
37 { | |
38 /* Data common to all keyboards */ | |
39 SDL_Window *focus; | |
40 Uint16 modstate; | |
41 Uint8 keystate[SDL_NUM_SCANCODES]; | |
42 SDLKey keymap[SDL_NUM_SCANCODES]; | |
43 }; | |
44 | |
45 static SDL_Keyboard SDL_keyboard; | |
36 | 46 |
37 static const SDLKey SDL_default_keymap[SDL_NUM_SCANCODES] = { | 47 static const SDLKey SDL_default_keymap[SDL_NUM_SCANCODES] = { |
38 0, 0, 0, 0, | 48 0, 0, 0, 0, |
39 'a', | 49 'a', |
40 'b', | 50 'b', |
539 | 549 |
540 /* Public functions */ | 550 /* Public functions */ |
541 int | 551 int |
542 SDL_KeyboardInit(void) | 552 SDL_KeyboardInit(void) |
543 { | 553 { |
554 SDL_Keyboard *keyboard = &SDL_keyboard; | |
555 | |
556 /* Set the default keymap */ | |
557 SDL_memcpy(keyboard->keymap, SDL_default_keymap, sizeof(SDL_default_keymap)); | |
544 return (0); | 558 return (0); |
545 } | 559 } |
546 | 560 |
547 SDL_Keyboard * | |
548 SDL_GetKeyboard(int index) | |
549 { | |
550 if (index < 0 || index >= SDL_num_keyboards) { | |
551 return NULL; | |
552 } | |
553 return SDL_keyboards[index]; | |
554 } | |
555 | |
556 int | |
557 SDL_AddKeyboard(const SDL_Keyboard * keyboard, int index) | |
558 { | |
559 SDL_Keyboard **keyboards; | |
560 | |
561 /* Add the keyboard to the list of keyboards */ | |
562 if (index < 0 || index >= SDL_num_keyboards || SDL_keyboards[index]) { | |
563 keyboards = | |
564 (SDL_Keyboard **) SDL_realloc(SDL_keyboards, | |
565 (SDL_num_keyboards + | |
566 1) * sizeof(*keyboards)); | |
567 if (!keyboards) { | |
568 SDL_OutOfMemory(); | |
569 return -1; | |
570 } | |
571 | |
572 SDL_keyboards = keyboards; | |
573 index = SDL_num_keyboards++; | |
574 } | |
575 SDL_keyboards[index] = | |
576 (SDL_Keyboard *) SDL_malloc(sizeof(*SDL_keyboards[index])); | |
577 if (!SDL_keyboards[index]) { | |
578 SDL_OutOfMemory(); | |
579 return -1; | |
580 } | |
581 *SDL_keyboards[index] = *keyboard; | |
582 | |
583 return index; | |
584 } | |
585 | |
586 void | 561 void |
587 SDL_DelKeyboard(int index) | 562 SDL_ResetKeyboard(void) |
588 { | 563 { |
589 SDL_Keyboard *keyboard = SDL_GetKeyboard(index); | 564 SDL_Keyboard *keyboard = &SDL_keyboard; |
590 | |
591 if (!keyboard) { | |
592 return; | |
593 } | |
594 | |
595 if (keyboard->FreeKeyboard) { | |
596 keyboard->FreeKeyboard(keyboard); | |
597 } | |
598 SDL_free(keyboard); | |
599 | |
600 SDL_keyboards[index] = NULL; | |
601 } | |
602 | |
603 void | |
604 SDL_ResetKeyboard(int index) | |
605 { | |
606 SDL_Keyboard *keyboard = SDL_GetKeyboard(index); | |
607 SDL_scancode scancode; | 565 SDL_scancode scancode; |
608 | |
609 if (!keyboard) { | |
610 return; | |
611 } | |
612 | 566 |
613 for (scancode = 0; scancode < SDL_NUM_SCANCODES; ++scancode) { | 567 for (scancode = 0; scancode < SDL_NUM_SCANCODES; ++scancode) { |
614 if (keyboard->keystate[scancode] == SDL_PRESSED) { | 568 if (keyboard->keystate[scancode] == SDL_PRESSED) { |
615 SDL_SendKeyboardKey(index, SDL_RELEASED, scancode); | 569 SDL_SendKeyboardKey(SDL_RELEASED, scancode); |
616 } | 570 } |
617 } | 571 } |
618 } | 572 } |
619 | 573 |
620 void | 574 void |
622 { | 576 { |
623 SDL_memcpy(keymap, SDL_default_keymap, sizeof(SDL_default_keymap)); | 577 SDL_memcpy(keymap, SDL_default_keymap, sizeof(SDL_default_keymap)); |
624 } | 578 } |
625 | 579 |
626 void | 580 void |
627 SDL_SetKeymap(int index, int start, SDLKey * keys, int length) | 581 SDL_SetKeymap(int start, SDLKey * keys, int length) |
628 { | 582 { |
629 SDL_Keyboard *keyboard = SDL_GetKeyboard(index); | 583 SDL_Keyboard *keyboard = &SDL_keyboard; |
630 | |
631 if (!keyboard) { | |
632 return; | |
633 } | |
634 | 584 |
635 if (start < 0 || start + length > SDL_NUM_SCANCODES) { | 585 if (start < 0 || start + length > SDL_NUM_SCANCODES) { |
636 return; | 586 return; |
637 } | 587 } |
638 | 588 |
643 SDL_SetScancodeName(SDL_scancode scancode, const char *name) | 593 SDL_SetScancodeName(SDL_scancode scancode, const char *name) |
644 { | 594 { |
645 SDL_scancode_names[scancode] = name; | 595 SDL_scancode_names[scancode] = name; |
646 } | 596 } |
647 | 597 |
598 SDL_Window * | |
599 SDL_GetKeyboardFocus(void) | |
600 { | |
601 SDL_Keyboard *keyboard = &SDL_keyboard; | |
602 | |
603 return keyboard->focus; | |
604 } | |
605 | |
648 void | 606 void |
649 SDL_SetKeyboardFocus(int index, SDL_Window * window) | 607 SDL_SetKeyboardFocus(SDL_Window * window) |
650 { | 608 { |
651 SDL_Keyboard *keyboard = SDL_GetKeyboard(index); | 609 SDL_Keyboard *keyboard = &SDL_keyboard; |
652 int i; | |
653 SDL_bool focus; | |
654 | |
655 if (!keyboard) { | |
656 return; | |
657 } | |
658 | 610 |
659 /* See if the current window has lost focus */ | 611 /* See if the current window has lost focus */ |
660 if (keyboard->focus && keyboard->focus != window) { | 612 if (keyboard->focus && keyboard->focus != window) { |
661 focus = SDL_FALSE; | 613 SDL_SendWindowEvent(keyboard->focus, SDL_WINDOWEVENT_FOCUS_LOST, |
662 for (i = 0; i < SDL_num_keyboards; ++i) { | 614 0, 0); |
663 if (i != index) { | |
664 SDL_Keyboard *check = SDL_GetKeyboard(i); | |
665 if (check && check->focus == keyboard->focus) { | |
666 focus = SDL_TRUE; | |
667 break; | |
668 } | |
669 } | |
670 } | |
671 if (!focus) { | |
672 SDL_SendWindowEvent(keyboard->focus, SDL_WINDOWEVENT_FOCUS_LOST, | |
673 0, 0); | |
674 } | |
675 } | 615 } |
676 | 616 |
677 keyboard->focus = window; | 617 keyboard->focus = window; |
678 | 618 |
679 if (keyboard->focus) { | 619 if (keyboard->focus) { |
685 } | 625 } |
686 } | 626 } |
687 } | 627 } |
688 | 628 |
689 int | 629 int |
690 SDL_SendKeyboardKey(int index, Uint8 state, SDL_scancode scancode) | 630 SDL_SendKeyboardKey(Uint8 state, SDL_scancode scancode) |
691 { | 631 { |
692 SDL_Keyboard *keyboard = SDL_GetKeyboard(index); | 632 SDL_Keyboard *keyboard = &SDL_keyboard; |
693 int posted; | 633 int posted; |
694 Uint16 modstate; | 634 Uint16 modstate; |
695 Uint32 type; | 635 Uint32 type; |
696 | 636 |
697 if (!keyboard || !scancode) { | 637 if (!scancode) { |
698 return 0; | 638 return 0; |
699 } | 639 } |
700 #if 0 | 640 #if 0 |
701 printf("The '%s' key has been %s\n", SDL_GetScancodeName(scancode), | 641 printf("The '%s' key has been %s\n", SDL_GetScancodeName(scancode), |
702 state == SDL_PRESSED ? "pressed" : "released"); | 642 state == SDL_PRESSED ? "pressed" : "released"); |
805 /* Post the event, if desired */ | 745 /* Post the event, if desired */ |
806 posted = 0; | 746 posted = 0; |
807 if (SDL_GetEventState(type) == SDL_ENABLE) { | 747 if (SDL_GetEventState(type) == SDL_ENABLE) { |
808 SDL_Event event; | 748 SDL_Event event; |
809 event.key.type = type; | 749 event.key.type = type; |
810 event.key.which = (Uint8) index; | |
811 event.key.state = state; | 750 event.key.state = state; |
812 event.key.keysym.scancode = scancode; | 751 event.key.keysym.scancode = scancode; |
813 event.key.keysym.sym = keyboard->keymap[scancode]; | 752 event.key.keysym.sym = keyboard->keymap[scancode]; |
814 event.key.keysym.mod = modstate; | 753 event.key.keysym.mod = modstate; |
815 event.key.keysym.unicode = 0; | 754 event.key.keysym.unicode = 0; |
818 } | 757 } |
819 return (posted); | 758 return (posted); |
820 } | 759 } |
821 | 760 |
822 int | 761 int |
823 SDL_SendKeyboardText(int index, const char *text) | 762 SDL_SendKeyboardText(const char *text) |
824 { | 763 { |
825 SDL_Keyboard *keyboard = SDL_GetKeyboard(index); | 764 SDL_Keyboard *keyboard = &SDL_keyboard; |
826 int posted; | 765 int posted; |
827 | |
828 if (!keyboard) { | |
829 return 0; | |
830 } | |
831 | 766 |
832 /* Post the event, if desired */ | 767 /* Post the event, if desired */ |
833 posted = 0; | 768 posted = 0; |
834 if (SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE) { | 769 if (SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE) { |
835 SDL_Event event; | 770 SDL_Event event; |
836 event.text.type = SDL_TEXTINPUT; | 771 event.text.type = SDL_TEXTINPUT; |
837 event.text.windowID = keyboard->focus ? keyboard->focus->id : 0; | 772 event.text.windowID = keyboard->focus ? keyboard->focus->id : 0; |
838 event.text.which = (Uint8) index; | |
839 SDL_strlcpy(event.text.text, text, SDL_arraysize(event.text.text)); | 773 SDL_strlcpy(event.text.text, text, SDL_arraysize(event.text.text)); |
840 event.text.windowID = keyboard->focus ? keyboard->focus->id : 0; | 774 event.text.windowID = keyboard->focus ? keyboard->focus->id : 0; |
841 posted = (SDL_PushEvent(&event) > 0); | 775 posted = (SDL_PushEvent(&event) > 0); |
842 } | 776 } |
843 return (posted); | 777 return (posted); |
844 } | 778 } |
845 | 779 |
846 int | 780 int |
847 SDL_SendEditingText(int index, const char *text, int start, int length) | 781 SDL_SendEditingText(const char *text, int start, int length) |
848 { | 782 { |
849 SDL_Keyboard *keyboard = SDL_GetKeyboard(index); | 783 SDL_Keyboard *keyboard = &SDL_keyboard; |
850 int posted; | 784 int posted; |
851 | |
852 if (!keyboard) { | |
853 return 0; | |
854 } | |
855 | 785 |
856 /* Post the event, if desired */ | 786 /* Post the event, if desired */ |
857 posted = 0; | 787 posted = 0; |
858 if (SDL_GetEventState(SDL_TEXTEDITING) == SDL_ENABLE) { | 788 if (SDL_GetEventState(SDL_TEXTEDITING) == SDL_ENABLE) { |
859 SDL_Event event; | 789 SDL_Event event; |
860 event.edit.type = SDL_TEXTEDITING; | 790 event.edit.type = SDL_TEXTEDITING; |
861 event.edit.windowID = keyboard->focus ? keyboard->focus->id : 0; | 791 event.edit.windowID = keyboard->focus ? keyboard->focus->id : 0; |
862 event.text.which = (Uint8) index; | |
863 event.edit.start = start; | 792 event.edit.start = start; |
864 event.edit.length = length; | 793 event.edit.length = length; |
865 SDL_strlcpy(event.edit.text, text, SDL_arraysize(event.edit.text)); | 794 SDL_strlcpy(event.edit.text, text, SDL_arraysize(event.edit.text)); |
866 posted = (SDL_PushEvent(&event) > 0); | 795 posted = (SDL_PushEvent(&event) > 0); |
867 } | 796 } |
869 } | 798 } |
870 | 799 |
871 void | 800 void |
872 SDL_KeyboardQuit(void) | 801 SDL_KeyboardQuit(void) |
873 { | 802 { |
874 int i; | |
875 | |
876 for (i = 0; i < SDL_num_keyboards; ++i) { | |
877 SDL_DelKeyboard(i); | |
878 } | |
879 SDL_num_keyboards = 0; | |
880 SDL_current_keyboard = 0; | |
881 | |
882 if (SDL_keyboards) { | |
883 SDL_free(SDL_keyboards); | |
884 SDL_keyboards = NULL; | |
885 } | |
886 } | |
887 | |
888 int | |
889 SDL_GetNumKeyboards(void) | |
890 { | |
891 return SDL_num_keyboards; | |
892 } | |
893 | |
894 int | |
895 SDL_SelectKeyboard(int index) | |
896 { | |
897 if (index >= 0 && index < SDL_num_keyboards) { | |
898 SDL_current_keyboard = index; | |
899 } | |
900 return SDL_current_keyboard; | |
901 } | 803 } |
902 | 804 |
903 Uint8 * | 805 Uint8 * |
904 SDL_GetKeyboardState(int *numkeys) | 806 SDL_GetKeyboardState(int *numkeys) |
905 { | 807 { |
906 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard); | 808 SDL_Keyboard *keyboard = &SDL_keyboard; |
907 | 809 |
908 if (numkeys != (int *) 0) { | 810 if (numkeys != (int *) 0) { |
909 *numkeys = SDL_NUM_SCANCODES; | 811 *numkeys = SDL_NUM_SCANCODES; |
910 } | 812 } |
911 | |
912 if (!keyboard) { | |
913 return NULL; | |
914 } | |
915 return keyboard->keystate; | 813 return keyboard->keystate; |
916 } | 814 } |
917 | 815 |
918 SDLMod | 816 SDLMod |
919 SDL_GetModState(void) | 817 SDL_GetModState(void) |
920 { | 818 { |
921 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard); | 819 SDL_Keyboard *keyboard = &SDL_keyboard; |
922 | 820 |
923 if (!keyboard) { | |
924 return KMOD_NONE; | |
925 } | |
926 return keyboard->modstate; | 821 return keyboard->modstate; |
927 } | 822 } |
928 | 823 |
929 void | 824 void |
930 SDL_SetModState(SDLMod modstate) | 825 SDL_SetModState(SDLMod modstate) |
931 { | 826 { |
932 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard); | 827 SDL_Keyboard *keyboard = &SDL_keyboard; |
933 | 828 |
934 if (!keyboard) { | |
935 return; | |
936 } | |
937 keyboard->modstate = modstate; | 829 keyboard->modstate = modstate; |
938 } | 830 } |
939 | 831 |
940 SDLKey | 832 SDLKey |
941 SDL_GetKeyFromScancode(SDL_scancode scancode) | 833 SDL_GetKeyFromScancode(SDL_scancode scancode) |
942 { | 834 { |
943 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard); | 835 SDL_Keyboard *keyboard = &SDL_keyboard; |
944 | 836 |
945 if (!keyboard) { | |
946 return SDLK_UNKNOWN; | |
947 } | |
948 return keyboard->keymap[scancode]; | 837 return keyboard->keymap[scancode]; |
949 } | 838 } |
950 | 839 |
951 SDL_scancode | 840 SDL_scancode |
952 SDL_GetScancodeFromKey(SDLKey key) | 841 SDL_GetScancodeFromKey(SDLKey key) |
953 { | 842 { |
954 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard); | 843 SDL_Keyboard *keyboard = &SDL_keyboard; |
955 SDL_scancode scancode; | 844 SDL_scancode scancode; |
956 | |
957 if (!keyboard) { | |
958 return SDL_SCANCODE_UNKNOWN; | |
959 } | |
960 | 845 |
961 for (scancode = SDL_SCANCODE_UNKNOWN; scancode < SDL_NUM_SCANCODES; | 846 for (scancode = SDL_SCANCODE_UNKNOWN; scancode < SDL_NUM_SCANCODES; |
962 ++scancode) { | 847 ++scancode) { |
963 if (keyboard->keymap[scancode] == key) { | 848 if (keyboard->keymap[scancode] == key) { |
964 return scancode; | 849 return scancode; |