Mercurial > sdl-ios-xcode
comparison src/video/x11/SDL_x11events.c @ 111:53e3d8ba4321
Now gets correct keyboard state when starting up on X11
author | Sam Lantinga <slouken@lokigames.com> |
---|---|
date | Sat, 14 Jul 2001 20:37:24 +0000 |
parents | 71774090f286 |
children | eb6b76a95f2d |
comparison
equal
deleted
inserted
replaced
110:7edee9f0f2cc | 111:53e3d8ba4321 |
---|---|
841 char keys_return[32]; | 841 char keys_return[32]; |
842 int i, gen_event; | 842 int i, gen_event; |
843 KeyCode xcode[SDLK_LAST]; | 843 KeyCode xcode[SDLK_LAST]; |
844 Uint8 new_kstate[SDLK_LAST]; | 844 Uint8 new_kstate[SDLK_LAST]; |
845 Uint8 *kstate = SDL_GetKeyState(NULL); | 845 Uint8 *kstate = SDL_GetKeyState(NULL); |
846 SDLMod modstate; | |
847 Window junk_window; | |
848 int x, y; | |
849 unsigned int mask; | |
846 | 850 |
847 /* The first time the window is mapped, we initialize key state */ | 851 /* The first time the window is mapped, we initialize key state */ |
848 if ( ! key_vec ) { | 852 if ( ! key_vec ) { |
849 key_vec = keys_return; | 853 key_vec = keys_return; |
850 XQueryKeymap(display, keys_return); | 854 XQueryKeymap(display, keys_return); |
851 gen_event = 0; | 855 gen_event = 0; |
852 } else { | 856 } else { |
857 #if 1 /* We no longer generate key down events, just update state */ | |
858 gen_event = 0; | |
859 #else | |
853 gen_event = 1; | 860 gen_event = 1; |
854 } | 861 #endif |
855 | 862 } |
856 /* Zero the new state and generate it */ | 863 |
857 memset(new_kstate, 0, sizeof(new_kstate)); | 864 /* Get the keyboard modifier state */ |
865 modstate = 0; | |
866 get_modifier_masks(display); | |
867 if ( XQueryPointer(display, DefaultRootWindow(display), | |
868 &junk_window, &junk_window, &x, &y, &x, &y, &mask) ) { | |
869 if ( mask & LockMask ) { | |
870 modstate |= KMOD_CAPS; | |
871 } | |
872 if ( mask & mode_switch_mask ) { | |
873 modstate |= KMOD_MODE; | |
874 } | |
875 if ( mask & num_mask ) { | |
876 modstate |= KMOD_NUM; | |
877 } | |
878 } | |
879 | |
880 /* Zero the new keyboard state and generate it */ | |
881 memset(new_kstate, SDL_RELEASED, sizeof(new_kstate)); | |
858 /* | 882 /* |
859 * An obvious optimisation is to check entire longwords at a time in | 883 * An obvious optimisation is to check entire longwords at a time in |
860 * both loops, but we can't be sure the arrays are aligned so it's not | 884 * both loops, but we can't be sure the arrays are aligned so it's not |
861 * worth the extra complexity | 885 * worth the extra complexity |
862 */ | 886 */ |
867 for(j = 0; j < 8; j++) { | 891 for(j = 0; j < 8; j++) { |
868 if(key_vec[i] & (1 << j)) { | 892 if(key_vec[i] & (1 << j)) { |
869 SDL_keysym sk; | 893 SDL_keysym sk; |
870 KeyCode kc = i << 3 | j; | 894 KeyCode kc = i << 3 | j; |
871 X11_TranslateKey(display, NULL, kc, &sk); | 895 X11_TranslateKey(display, NULL, kc, &sk); |
872 new_kstate[sk.sym] = 1; | 896 new_kstate[sk.sym] = SDL_PRESSED; |
873 xcode[sk.sym] = kc; | 897 xcode[sk.sym] = kc; |
874 } | 898 } |
875 } | 899 } |
876 } | 900 } |
877 for(i = SDLK_FIRST+1; i < SDLK_LAST; i++) { | 901 for(i = SDLK_FIRST+1; i < SDLK_LAST; i++) { |
878 int st; | 902 int state = new_kstate[i]; |
879 SDL_keysym sk; | 903 |
880 | 904 if ( state == SDL_PRESSED ) { |
881 if(kstate[i] == new_kstate[i]) | 905 switch (i) { |
906 case SDLK_LSHIFT: | |
907 modstate |= KMOD_LSHIFT; | |
908 break; | |
909 case SDLK_RSHIFT: | |
910 modstate |= KMOD_RSHIFT; | |
911 break; | |
912 case SDLK_LCTRL: | |
913 modstate |= KMOD_LCTRL; | |
914 break; | |
915 case SDLK_RCTRL: | |
916 modstate |= KMOD_RCTRL; | |
917 break; | |
918 case SDLK_LALT: | |
919 modstate |= KMOD_LALT; | |
920 break; | |
921 case SDLK_RALT: | |
922 modstate |= KMOD_RALT; | |
923 break; | |
924 case SDLK_LMETA: | |
925 modstate |= KMOD_LMETA; | |
926 break; | |
927 case SDLK_RMETA: | |
928 modstate |= KMOD_RMETA; | |
929 break; | |
930 default: | |
931 break; | |
932 } | |
933 } | |
934 if ( kstate[i] == state ) | |
882 continue; | 935 continue; |
936 | |
883 /* | 937 /* |
884 * Send a fake keyboard event correcting the difference between | 938 * Send a fake keyboard event correcting the difference between |
885 * SDL's keyboard state and the actual. Note that there is no | 939 * SDL's keyboard state and the actual. Note that there is no |
886 * way to find out the scancode for key releases, but since all | 940 * way to find out the scancode for key releases, but since all |
887 * keys are released when focus is lost only keypresses should | 941 * keys are released when focus is lost only keypresses should |
888 * be sent here | 942 * be sent here |
889 */ | 943 */ |
890 st = new_kstate[i] ? SDL_PRESSED : SDL_RELEASED; | |
891 memset(&sk, 0, sizeof(sk)); | |
892 sk.sym = i; | |
893 sk.scancode = xcode[i]; /* only valid for key press */ | |
894 if ( gen_event ) { | 944 if ( gen_event ) { |
895 SDL_PrivateKeyboard(st, &sk); | 945 SDL_keysym sk; |
946 memset(&sk, 0, sizeof(sk)); | |
947 sk.sym = i; | |
948 sk.scancode = xcode[i]; /* only valid for key press */ | |
949 SDL_PrivateKeyboard(state, &sk); | |
896 } else { | 950 } else { |
897 kstate[i] = new_kstate[i]; | 951 kstate[i] = state; |
898 } | 952 } |
899 } | 953 } |
954 | |
955 /* Hack - set toggle key state */ | |
956 if ( modstate & KMOD_CAPS ) { | |
957 kstate[SDLK_CAPSLOCK] = SDL_PRESSED; | |
958 } else { | |
959 kstate[SDLK_CAPSLOCK] = SDL_RELEASED; | |
960 } | |
961 if ( modstate & KMOD_NUM ) { | |
962 kstate[SDLK_NUMLOCK] = SDL_PRESSED; | |
963 } else { | |
964 kstate[SDLK_NUMLOCK] = SDL_RELEASED; | |
965 } | |
966 | |
967 /* Set the final modifier state */ | |
968 SDL_SetModState(modstate); | |
900 } | 969 } |
901 | 970 |
902 void X11_InitOSKeymap(_THIS) | 971 void X11_InitOSKeymap(_THIS) |
903 { | 972 { |
904 X11_InitKeymap(); | 973 X11_InitKeymap(); |