📄 sdl_x11events.c
字号:
/* SDL - Simple DirectMedia Layer Copyright (C) 1997-2006 Sam Lantinga This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Sam Lantinga slouken@libsdl.org*/#include "SDL_config.h"/* Handle the event stream, converting X11 events into SDL events */#include <setjmp.h>#include <X11/Xlib.h>#include <X11/Xutil.h>#include <X11/keysym.h>#ifdef __SVR4#include <X11/Sunkeysym.h>#endif#include <sys/types.h>#include <sys/time.h>#include <unistd.h>#include "SDL_timer.h"#include "SDL_syswm.h"#include "../SDL_sysvideo.h"#include "../../events/SDL_sysevents.h"#include "../../events/SDL_events_c.h"#include "SDL_x11video.h"#include "SDL_x11dga_c.h"#include "SDL_x11modes_c.h"#include "SDL_x11image_c.h"#include "SDL_x11gamma_c.h"#include "SDL_x11wm_c.h"#include "SDL_x11mouse_c.h"#include "SDL_x11events_c.h"/* Define this if you want to debug X11 events *//*#define DEBUG_XEVENTS*//* The translation tables from an X11 keysym to a SDL keysym */static SDLKey ODD_keymap[256];static SDLKey MISC_keymap[256];SDLKey X11_TranslateKeycode(Display *display, KeyCode kc);#ifdef X_HAVE_UTF8_STRINGUint32 Utf8ToUcs4(const Uint8 *utf8){ Uint32 c; int i = 1; int noOctets = 0; int firstOctetMask = 0; unsigned char firstOctet = utf8[0]; if (firstOctet < 0x80) { /* Characters in the range: 00000000 to 01111111 (ASCII Range) are stored in one octet: 0xxxxxxx (The same as its ASCII representation) The least 6 significant bits of the first octet is the most 6 significant nonzero bits of the UCS4 representation. */ noOctets = 1; firstOctetMask = 0x7F; /* 0(1111111) - The most significant bit is ignored */ } else if ((firstOctet & 0xE0) /* get the most 3 significant bits by AND'ing with 11100000 */ == 0xC0 ) { /* see if those 3 bits are 110. If so, the char is in this range */ /* Characters in the range: 00000000 10000000 to 00000111 11111111 are stored in two octets: 110xxxxx 10xxxxxx The least 5 significant bits of the first octet is the most 5 significant nonzero bits of the UCS4 representation. */ noOctets = 2; firstOctetMask = 0x1F; /* 000(11111) - The most 3 significant bits are ignored */ } else if ((firstOctet & 0xF0) /* get the most 4 significant bits by AND'ing with 11110000 */ == 0xE0) { /* see if those 4 bits are 1110. If so, the char is in this range */ /* Characters in the range: 00001000 00000000 to 11111111 11111111 are stored in three octets: 1110xxxx 10xxxxxx 10xxxxxx The least 4 significant bits of the first octet is the most 4 significant nonzero bits of the UCS4 representation. */ noOctets = 3; firstOctetMask = 0x0F; /* 0000(1111) - The most 4 significant bits are ignored */ } else if ((firstOctet & 0xF8) /* get the most 5 significant bits by AND'ing with 11111000 */ == 0xF0) { /* see if those 5 bits are 11110. If so, the char is in this range */ /* Characters in the range: 00000001 00000000 00000000 to 00011111 11111111 11111111 are stored in four octets: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx The least 3 significant bits of the first octet is the most 3 significant nonzero bits of the UCS4 representation. */ noOctets = 4; firstOctetMask = 0x07; /* 11110(111) - The most 5 significant bits are ignored */ } else if ((firstOctet & 0xFC) /* get the most 6 significant bits by AND'ing with 11111100 */ == 0xF8) { /* see if those 6 bits are 111110. If so, the char is in this range */ /* Characters in the range: 00000000 00100000 00000000 00000000 to 00000011 11111111 11111111 11111111 are stored in five octets: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx The least 2 significant bits of the first octet is the most 2 significant nonzero bits of the UCS4 representation. */ noOctets = 5; firstOctetMask = 0x03; /* 111110(11) - The most 6 significant bits are ignored */ } else if ((firstOctet & 0xFE) /* get the most 7 significant bits by AND'ing with 11111110 */ == 0xFC) { /* see if those 7 bits are 1111110. If so, the char is in this range */ /* Characters in the range: 00000100 00000000 00000000 00000000 to 01111111 11111111 11111111 11111111 are stored in six octets: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx The least significant bit of the first octet is the most significant nonzero bit of the UCS4 representation. */ noOctets = 6; firstOctetMask = 0x01; /* 1111110(1) - The most 7 significant bits are ignored */ } else return 0; /* The given chunk is not a valid UTF-8 encoded Unicode character */ /* The least noOctets significant bits of the first octet is the most 2 significant nonzero bits of the UCS4 representation. The first 6 bits of the UCS4 representation is the least 8-noOctets-1 significant bits of firstOctet if the character is not ASCII. If so, it's the least 7 significant bits of firstOctet. This done by AND'ing firstOctet with its mask to trim the bits used for identifying the number of continuing octets (if any) and leave only the free bits (the x's) Sample: 1-octet: 0xxxxxxx & 01111111 = 0xxxxxxx 2-octets: 110xxxxx & 00011111 = 000xxxxx */ c = firstOctet & firstOctetMask; /* Now, start filling c.ucs4 with the bits from the continuing octets from utf8. */ for (i = 1; i < noOctets; i++) { /* A valid continuing octet is of the form 10xxxxxx */ if ((utf8[i] & 0xC0) /* get the most 2 significant bits by AND'ing with 11000000 */ != 0x80) /* see if those 2 bits are 10. If not, the is a malformed sequence. */ /*The given chunk is a partial sequence at the end of a string that could begin a valid character */ return 0; /* Make room for the next 6-bits */ c <<= 6; /* Take only the least 6 significance bits of the current octet (utf8[i]) and fill the created room of c.ucs4 with them. This done by AND'ing utf8[i] with 00111111 and the OR'ing the result with c.ucs4. */ c |= utf8[i] & 0x3F; } return c;}/* Given a UTF-8 encoded string pointed to by utf8 of length length in bytes, returns the corresponding UTF-16 encoded string in the buffer pointed to by utf16. The maximum number of UTF-16 encoding units (i.e., Unit16s) allowed in the buffer is specified in utf16_max_length. The return value is the number of UTF-16 encoding units placed in the output buffer pointed to by utf16. In case of an error, -1 is returned, leaving some unusable partial results in the output buffer. The caller must estimate the size of utf16 buffer by itself before calling this function. Insufficient output buffer is considered as an error, and once an error occured, this function doesn't give any clue how large the result will be. The error cases include following: - Invalid byte sequences were in the input UTF-8 bytes. The caller has no way to know what point in the input buffer was the errornous byte. - The input contained a character (a valid UTF-8 byte sequence) whose scalar value exceeded the range that UTF-16 can represent (i.e., characters whose Unicode scalar value above 0x110000). - The output buffer has no enough space to hold entire utf16 data. Please note: - '\0'-termination is not assumed both on the input UTF-8 string and on the output UTF-16 string; any legal zero byte in the input UTF-8 string will be converted to a 16-bit zero in output. As a side effect, the last UTF-16 encoding unit stored in the output buffer will have a non-zero value if the input UTF-8 was not '\0'-terminated. - UTF-8 aliases are *not* considered as an error. They are converted to UTF-16. For example, 0xC0 0xA0, 0xE0 0x80 0xA0, and 0xF0 0x80 0x80 0xA0 are all mapped to a single UTF-16 encoding unit 0x0020. - Three byte UTF-8 sequences whose value corresponds to a surrogate code or other reserved scalar value are not considered as an error either. They may cause an invalid UTF-16 data (e.g., those containing unpaired surrogates).*/static int Utf8ToUtf16(const Uint8 *utf8, const int utf8_length, Uint16 *utf16, const int utf16_max_length) { /* p moves over the output buffer. max_ptr points to the next to the last slot of the buffer. */ Uint16 *p = utf16; Uint16 const *const max_ptr = utf16 + utf16_max_length; /* end_of_input points to the last byte of input as opposed to the next to the last byte. */ Uint8 const *const end_of_input = utf8 + utf8_length - 1; while (utf8 <= end_of_input) { Uint8 const c = *utf8; if (p >= max_ptr) { /* No more output space. */ return -1; } if (c < 0x80) { /* One byte ASCII. */ *p++ = c; utf8 += 1; } else if (c < 0xC0) { /* Follower byte without preceeding leader bytes. */ return -1; } else if (c < 0xE0) { /* Two byte sequence. We need one follower byte. */ if (end_of_input - utf8 < 1 || (((utf8[1] ^ 0x80)) & 0xC0)) { return -1; } *p++ = (Uint16)(0xCF80 + (c << 6) + utf8[1]); utf8 += 2; } else if (c < 0xF0) { /* Three byte sequence. We need two follower byte. */ if (end_of_input - utf8 < 2 || (((utf8[1] ^ 0x80) | (utf8[2] ^ 0x80)) & 0xC0)) { return -1; } *p++ = (Uint16)(0xDF80 + (c << 12) + (utf8[1] << 6) + utf8[2]); utf8 += 3; } else if (c < 0xF8) { int plane; /* Four byte sequence. We need three follower bytes. */ if (end_of_input - utf8 < 3 || (((utf8[1] ^ 0x80) | (utf8[2] ^0x80) | (utf8[3] ^ 0x80)) & 0xC0)) { return -1; } plane = (-0xC8 + (c << 2) + (utf8[1] >> 4)); if (plane == 0) { /* This four byte sequence is an alias that corresponds to a Unicode scalar value in BMP. It fits in an UTF-16 encoding unit. */ *p++ = (Uint16)(0xDF80 + (utf8[1] << 12) + (utf8[2] << 6) + utf8[3]); } else if (plane <= 16) { /* This is a legal four byte sequence that corresponds to a surrogate pair. */ if (p + 1 >= max_ptr) { /* No enough space on the output buffer for the pair. */ return -1; } *p++ = (Uint16)(0xE5B8 + (c << 8) + (utf8[1] << 2) + (utf8[2] >> 4)); *p++ = (Uint16)(0xDB80 + ((utf8[2] & 0x0F) << 6) + utf8[3]); } else { /* This four byte sequence is out of UTF-16 code space. */ return -1; } utf8 += 4; } else { /* Longer sequence or unused byte. */ return -1; } } return p - utf16;}#endif/* Check to see if this is a repeated key. (idea shamelessly lifted from GII -- thanks guys! :) */static int X11_KeyRepeat(Display *display, XEvent *event){ XEvent peekevent; int repeated; repeated = 0; if ( XPending(display) ) { XPeekEvent(display, &peekevent); if ( (peekevent.type == KeyPress) && (peekevent.xkey.keycode == event->xkey.keycode) && ((peekevent.xkey.time-event->xkey.time) < 2) ) { repeated = 1; XNextEvent(display, &peekevent); } } return(repeated);}/* Note: The X server buffers and accumulates mouse motion events, so the motion event generated by the warp may not appear exactly as we expect it to. We work around this (and improve performance) by only warping the pointer when it reaches the edge, and then wait for it.*/#define MOUSE_FUDGE_FACTOR 8static __inline__ int X11_WarpedMotion(_THIS, XEvent *xevent){ int w, h, i; int deltax, deltay; int posted; w = SDL_VideoSurface->w; h = SDL_VideoSurface->h; deltax = xevent->xmotion.x - mouse_last.x; deltay = xevent->xmotion.y - mouse_last.y;#ifdef DEBUG_MOTION printf("Warped mouse motion: %d,%d\n", deltax, deltay);#endif mouse_last.x = xevent->xmotion.x; mouse_last.y = xevent->xmotion.y; posted = SDL_PrivateMouseMotion(0, 1, deltax, deltay); if ( (xevent->xmotion.x < MOUSE_FUDGE_FACTOR) || (xevent->xmotion.x > (w-MOUSE_FUDGE_FACTOR)) || (xevent->xmotion.y < MOUSE_FUDGE_FACTOR) || (xevent->xmotion.y > (h-MOUSE_FUDGE_FACTOR)) ) { /* Get the events that have accumulated */ while ( XCheckTypedEvent(SDL_Display, MotionNotify, xevent) ) { deltax = xevent->xmotion.x - mouse_last.x; deltay = xevent->xmotion.y - mouse_last.y;#ifdef DEBUG_MOTION printf("Extra mouse motion: %d,%d\n", deltax, deltay);#endif mouse_last.x = xevent->xmotion.x; mouse_last.y = xevent->xmotion.y; posted += SDL_PrivateMouseMotion(0, 1, deltax, deltay); } mouse_last.x = w/2; mouse_last.y = h/2; XWarpPointer(SDL_Display, None, SDL_Window, 0, 0, 0, 0, mouse_last.x, mouse_last.y); for ( i=0; i<10; ++i ) { XMaskEvent(SDL_Display, PointerMotionMask, xevent); if ( (xevent->xmotion.x > (mouse_last.x-MOUSE_FUDGE_FACTOR)) && (xevent->xmotion.x < (mouse_last.x+MOUSE_FUDGE_FACTOR)) && (xevent->xmotion.y > (mouse_last.y-MOUSE_FUDGE_FACTOR)) && (xevent->xmotion.y < (mouse_last.y+MOUSE_FUDGE_FACTOR)) ) { break; }#ifdef DEBUG_XEVENTS printf("Lost mouse motion: %d,%d\n", xevent->xmotion.x, xevent->xmotion.y);#endif }#ifdef DEBUG_XEVENTS if ( i == 10 ) { printf("Warning: didn't detect mouse warp motion\n"); }#endif } return(posted);}static int X11_DispatchEvent(_THIS){ int posted; XEvent xevent; SDL_memset(&xevent, '\0', sizeof (XEvent)); /* valgrind fix. --ryan. */ XNextEvent(SDL_Display, &xevent); /* Discard KeyRelease and KeyPress events generated by auto-repeat. We need to do it before passing event to XFilterEvent. Otherwise, KeyRelease aware IMs are confused... */ if ( xevent.type == KeyRelease && X11_KeyRepeat(SDL_Display, &xevent) ) { return 0; }#ifdef X_HAVE_UTF8_STRING /* If we are translating with IM, we need to pass all events to XFilterEvent, and discard those filtered events immediately. */ if ( SDL_TranslateUNICODE && SDL_IM != NULL && XFilterEvent(&xevent, None) ) { return 0; }#endif posted = 0; switch (xevent.type) { /* Gaining mouse coverage? */ case EnterNotify: {#ifdef DEBUG_XEVENTSprintf("EnterNotify! (%d,%d)\n", xevent.xcrossing.x, xevent.xcrossing.y);if ( xevent.xcrossing.mode == NotifyGrab )printf("Mode: NotifyGrab\n");if ( xevent.xcrossing.mode == NotifyUngrab )printf("Mode: NotifyUngrab\n");#endif if ( (xevent.xcrossing.mode != NotifyGrab) && (xevent.xcrossing.mode != NotifyUngrab) ) { if ( this->input_grab == SDL_GRAB_OFF ) { posted = SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS); } posted = SDL_PrivateMouseMotion(0, 0, xevent.xcrossing.x, xevent.xcrossing.y); } } break; /* Losing mouse coverage? */ case LeaveNotify: {#ifdef DEBUG_XEVENTSprintf("LeaveNotify! (%d,%d)\n", xevent.xcrossing.x, xevent.xcrossing.y);if ( xevent.xcrossing.mode == NotifyGrab )printf("Mode: NotifyGrab\n");if ( xevent.xcrossing.mode == NotifyUngrab )printf("Mode: NotifyUngrab\n");#endif if ( (xevent.xcrossing.mode != NotifyGrab) && (xevent.xcrossing.mode != NotifyUngrab) && (xevent.xcrossing.detail != NotifyInferior) ) { if ( this->input_grab == SDL_GRAB_OFF ) { posted = SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS); } else { posted = SDL_PrivateMouseMotion(0, 0, xevent.xcrossing.x, xevent.xcrossing.y); } } } break; /* Gaining input focus? */ case FocusIn: {#ifdef DEBUG_XEVENTSprintf("FocusIn!\n");#endif posted = SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);#ifdef X_HAVE_UTF8_STRING if ( SDL_IC != NULL ) { XSetICFocus(SDL_IC); }#endif /* Queue entry into fullscreen mode */ switch_waiting = 0x01 | SDL_FULLSCREEN; switch_time = SDL_GetTicks() + 1500; } break; /* Losing input focus? */ case FocusOut: {#ifdef DEBUG_XEVENTSprintf("FocusOut!\n");#endif posted = SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);#ifdef X_HAVE_UTF8_STRING
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -