Mercurial > sdl-ios-xcode
comparison src/events/SDL_keyboard.c @ 4465:3e69e077cb95
Removed multi-mouse / multi-keyboard support in anticipation of a real multi-mouse and multi-touch API.
Plus, this lets me start implementing cursor support.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 09 May 2010 20:47:22 -0700 |
parents | 25e45611fa3d |
children | 06c7423f8c60 22aa6a631d34 |
comparison
equal
deleted
inserted
replaced
4464:fa77a6429698 | 4465:3e69e077cb95 |
---|---|
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', |
542 SDL_KeyboardInit(void) | 552 SDL_KeyboardInit(void) |
543 { | 553 { |
544 return (0); | 554 return (0); |
545 } | 555 } |
546 | 556 |
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 | 557 void |
587 SDL_DelKeyboard(int index) | 558 SDL_ResetKeyboard(void) |
588 { | 559 { |
589 SDL_Keyboard *keyboard = SDL_GetKeyboard(index); | 560 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; | 561 SDL_scancode scancode; |
608 | |
609 if (!keyboard) { | |
610 return; | |
611 } | |
612 | 562 |
613 for (scancode = 0; scancode < SDL_NUM_SCANCODES; ++scancode) { | 563 for (scancode = 0; scancode < SDL_NUM_SCANCODES; ++scancode) { |
614 if (keyboard->keystate[scancode] == SDL_PRESSED) { | 564 if (keyboard->keystate[scancode] == SDL_PRESSED) { |
615 SDL_SendKeyboardKey(index, SDL_RELEASED, scancode); | 565 SDL_SendKeyboardKey(SDL_RELEASED, scancode); |
616 } | 566 } |
617 } | 567 } |
618 } | 568 } |
619 | 569 |
620 void | 570 void |
622 { | 572 { |
623 SDL_memcpy(keymap, SDL_default_keymap, sizeof(SDL_default_keymap)); | 573 SDL_memcpy(keymap, SDL_default_keymap, sizeof(SDL_default_keymap)); |
624 } | 574 } |
625 | 575 |
626 void | 576 void |
627 SDL_SetKeymap(int index, int start, SDLKey * keys, int length) | 577 SDL_SetKeymap(int start, SDLKey * keys, int length) |
628 { | 578 { |
629 SDL_Keyboard *keyboard = SDL_GetKeyboard(index); | 579 SDL_Keyboard *keyboard = &SDL_keyboard; |
630 | |
631 if (!keyboard) { | |
632 return; | |
633 } | |
634 | 580 |
635 if (start < 0 || start + length > SDL_NUM_SCANCODES) { | 581 if (start < 0 || start + length > SDL_NUM_SCANCODES) { |
636 return; | 582 return; |
637 } | 583 } |
638 | 584 |
643 SDL_SetScancodeName(SDL_scancode scancode, const char *name) | 589 SDL_SetScancodeName(SDL_scancode scancode, const char *name) |
644 { | 590 { |
645 SDL_scancode_names[scancode] = name; | 591 SDL_scancode_names[scancode] = name; |
646 } | 592 } |
647 | 593 |
594 SDL_Window * | |
595 SDL_GetKeyboardFocus(void) | |
596 { | |
597 SDL_Keyboard *keyboard = &SDL_keyboard; | |
598 | |
599 return keyboard->focus; | |
600 } | |
601 | |
648 void | 602 void |
649 SDL_SetKeyboardFocus(int index, SDL_Window * window) | 603 SDL_SetKeyboardFocus(SDL_Window * window) |
650 { | 604 { |
651 SDL_Keyboard *keyboard = SDL_GetKeyboard(index); | 605 SDL_Keyboard *keyboard = &SDL_keyboard; |
652 int i; | |
653 SDL_bool focus; | |
654 | |
655 if (!keyboard) { | |
656 return; | |
657 } | |
658 | 606 |
659 /* See if the current window has lost focus */ | 607 /* See if the current window has lost focus */ |
660 if (keyboard->focus && keyboard->focus != window) { | 608 if (keyboard->focus && keyboard->focus != window) { |
661 focus = SDL_FALSE; | 609 SDL_SendWindowEvent(keyboard->focus, SDL_WINDOWEVENT_FOCUS_LOST, |
662 for (i = 0; i < SDL_num_keyboards; ++i) { | 610 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 } | 611 } |
676 | 612 |
677 keyboard->focus = window; | 613 keyboard->focus = window; |
678 | 614 |
679 if (keyboard->focus) { | 615 if (keyboard->focus) { |
685 } | 621 } |
686 } | 622 } |
687 } | 623 } |
688 | 624 |
689 int | 625 int |
690 SDL_SendKeyboardKey(int index, Uint8 state, SDL_scancode scancode) | 626 SDL_SendKeyboardKey(Uint8 state, SDL_scancode scancode) |
691 { | 627 { |
692 SDL_Keyboard *keyboard = SDL_GetKeyboard(index); | 628 SDL_Keyboard *keyboard = &SDL_keyboard; |
693 int posted; | 629 int posted; |
694 Uint16 modstate; | 630 Uint16 modstate; |
695 Uint32 type; | 631 Uint32 type; |
696 | 632 |
697 if (!keyboard || !scancode) { | 633 if (!scancode) { |
698 return 0; | 634 return 0; |
699 } | 635 } |
700 #if 0 | 636 #if 0 |
701 printf("The '%s' key has been %s\n", SDL_GetScancodeName(scancode), | 637 printf("The '%s' key has been %s\n", SDL_GetScancodeName(scancode), |
702 state == SDL_PRESSED ? "pressed" : "released"); | 638 state == SDL_PRESSED ? "pressed" : "released"); |
805 /* Post the event, if desired */ | 741 /* Post the event, if desired */ |
806 posted = 0; | 742 posted = 0; |
807 if (SDL_GetEventState(type) == SDL_ENABLE) { | 743 if (SDL_GetEventState(type) == SDL_ENABLE) { |
808 SDL_Event event; | 744 SDL_Event event; |
809 event.key.type = type; | 745 event.key.type = type; |
810 event.key.which = (Uint8) index; | |
811 event.key.state = state; | 746 event.key.state = state; |
812 event.key.keysym.scancode = scancode; | 747 event.key.keysym.scancode = scancode; |
813 event.key.keysym.sym = keyboard->keymap[scancode]; | 748 event.key.keysym.sym = keyboard->keymap[scancode]; |
814 event.key.keysym.mod = modstate; | 749 event.key.keysym.mod = modstate; |
815 event.key.keysym.unicode = 0; | 750 event.key.keysym.unicode = 0; |
818 } | 753 } |
819 return (posted); | 754 return (posted); |
820 } | 755 } |
821 | 756 |
822 int | 757 int |
823 SDL_SendKeyboardText(int index, const char *text) | 758 SDL_SendKeyboardText(const char *text) |
824 { | 759 { |
825 SDL_Keyboard *keyboard = SDL_GetKeyboard(index); | 760 SDL_Keyboard *keyboard = &SDL_keyboard; |
826 int posted; | 761 int posted; |
827 | |
828 if (!keyboard) { | |
829 return 0; | |
830 } | |
831 | 762 |
832 /* Post the event, if desired */ | 763 /* Post the event, if desired */ |
833 posted = 0; | 764 posted = 0; |
834 if (SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE) { | 765 if (SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE) { |
835 SDL_Event event; | 766 SDL_Event event; |
836 event.text.type = SDL_TEXTINPUT; | 767 event.text.type = SDL_TEXTINPUT; |
837 event.text.windowID = keyboard->focus ? keyboard->focus->id : 0; | 768 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)); | 769 SDL_strlcpy(event.text.text, text, SDL_arraysize(event.text.text)); |
840 event.text.windowID = keyboard->focus ? keyboard->focus->id : 0; | 770 event.text.windowID = keyboard->focus ? keyboard->focus->id : 0; |
841 posted = (SDL_PushEvent(&event) > 0); | 771 posted = (SDL_PushEvent(&event) > 0); |
842 } | 772 } |
843 return (posted); | 773 return (posted); |
844 } | 774 } |
845 | 775 |
846 int | 776 int |
847 SDL_SendEditingText(int index, const char *text, int start, int length) | 777 SDL_SendEditingText(const char *text, int start, int length) |
848 { | 778 { |
849 SDL_Keyboard *keyboard = SDL_GetKeyboard(index); | 779 SDL_Keyboard *keyboard = &SDL_keyboard; |
850 int posted; | 780 int posted; |
851 | |
852 if (!keyboard) { | |
853 return 0; | |
854 } | |
855 | 781 |
856 /* Post the event, if desired */ | 782 /* Post the event, if desired */ |
857 posted = 0; | 783 posted = 0; |
858 if (SDL_GetEventState(SDL_TEXTEDITING) == SDL_ENABLE) { | 784 if (SDL_GetEventState(SDL_TEXTEDITING) == SDL_ENABLE) { |
859 SDL_Event event; | 785 SDL_Event event; |
860 event.edit.type = SDL_TEXTEDITING; | 786 event.edit.type = SDL_TEXTEDITING; |
861 event.edit.windowID = keyboard->focus ? keyboard->focus->id : 0; | 787 event.edit.windowID = keyboard->focus ? keyboard->focus->id : 0; |
862 event.text.which = (Uint8) index; | |
863 event.edit.start = start; | 788 event.edit.start = start; |
864 event.edit.length = length; | 789 event.edit.length = length; |
865 SDL_strlcpy(event.edit.text, text, SDL_arraysize(event.edit.text)); | 790 SDL_strlcpy(event.edit.text, text, SDL_arraysize(event.edit.text)); |
866 posted = (SDL_PushEvent(&event) > 0); | 791 posted = (SDL_PushEvent(&event) > 0); |
867 } | 792 } |
869 } | 794 } |
870 | 795 |
871 void | 796 void |
872 SDL_KeyboardQuit(void) | 797 SDL_KeyboardQuit(void) |
873 { | 798 { |
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 } | 799 } |
902 | 800 |
903 Uint8 * | 801 Uint8 * |
904 SDL_GetKeyboardState(int *numkeys) | 802 SDL_GetKeyboardState(int *numkeys) |
905 { | 803 { |
906 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard); | 804 SDL_Keyboard *keyboard = &SDL_keyboard; |
907 | 805 |
908 if (numkeys != (int *) 0) { | 806 if (numkeys != (int *) 0) { |
909 *numkeys = SDL_NUM_SCANCODES; | 807 *numkeys = SDL_NUM_SCANCODES; |
910 } | 808 } |
911 | |
912 if (!keyboard) { | |
913 return NULL; | |
914 } | |
915 return keyboard->keystate; | 809 return keyboard->keystate; |
916 } | 810 } |
917 | 811 |
918 SDLMod | 812 SDLMod |
919 SDL_GetModState(void) | 813 SDL_GetModState(void) |
920 { | 814 { |
921 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard); | 815 SDL_Keyboard *keyboard = &SDL_keyboard; |
922 | 816 |
923 if (!keyboard) { | |
924 return KMOD_NONE; | |
925 } | |
926 return keyboard->modstate; | 817 return keyboard->modstate; |
927 } | 818 } |
928 | 819 |
929 void | 820 void |
930 SDL_SetModState(SDLMod modstate) | 821 SDL_SetModState(SDLMod modstate) |
931 { | 822 { |
932 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard); | 823 SDL_Keyboard *keyboard = &SDL_keyboard; |
933 | 824 |
934 if (!keyboard) { | |
935 return; | |
936 } | |
937 keyboard->modstate = modstate; | 825 keyboard->modstate = modstate; |
938 } | 826 } |
939 | 827 |
940 SDLKey | 828 SDLKey |
941 SDL_GetKeyFromScancode(SDL_scancode scancode) | 829 SDL_GetKeyFromScancode(SDL_scancode scancode) |
942 { | 830 { |
943 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard); | 831 SDL_Keyboard *keyboard = &SDL_keyboard; |
944 | 832 |
945 if (!keyboard) { | |
946 return SDLK_UNKNOWN; | |
947 } | |
948 return keyboard->keymap[scancode]; | 833 return keyboard->keymap[scancode]; |
949 } | 834 } |
950 | 835 |
951 SDL_scancode | 836 SDL_scancode |
952 SDL_GetScancodeFromKey(SDLKey key) | 837 SDL_GetScancodeFromKey(SDLKey key) |
953 { | 838 { |
954 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard); | 839 SDL_Keyboard *keyboard = &SDL_keyboard; |
955 SDL_scancode scancode; | 840 SDL_scancode scancode; |
956 | |
957 if (!keyboard) { | |
958 return SDL_SCANCODE_UNKNOWN; | |
959 } | |
960 | 841 |
961 for (scancode = SDL_SCANCODE_UNKNOWN; scancode < SDL_NUM_SCANCODES; | 842 for (scancode = SDL_SCANCODE_UNKNOWN; scancode < SDL_NUM_SCANCODES; |
962 ++scancode) { | 843 ++scancode) { |
963 if (keyboard->keymap[scancode] == key) { | 844 if (keyboard->keymap[scancode] == key) { |
964 return scancode; | 845 return scancode; |