⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdl_x11events.c

📁 linux下面的一个开源的多媒体中间件
💻 C
📖 第 1 页 / 共 3 页
字号:
/*    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;}#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);	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		if ( SDL_IC != NULL ) {			XUnsetICFocus(SDL_IC);		}#endif		/* Queue leaving fullscreen mode */		switch_waiting = 0x01;		switch_time = SDL_GetTicks() + 200;	    }	    break;	    /* Generated upon EnterWindow and FocusIn */	    case KeymapNotify: {#ifdef DEBUG_XEVENTSprintf("KeymapNotify!\n");#endif		X11_SetKeyboardState(SDL_Display,  xevent.xkeymap.key_vector);	    }	    break;	    /* Mouse motion? */	    case MotionNotify: {		if ( SDL_VideoSurface ) {			if ( mouse_relative ) {				if ( using_dga & DGA_MOUSE ) {#ifdef DEBUG_MOTION  printf("DGA motion: %d,%d\n", xevent.xmotion.x_root, xevent.xmotion.y_root);#endif					posted = SDL_PrivateMouseMotion(0, 1,							xevent.xmotion.x_root,							xevent.xmotion.y_root);				} else {					posted = X11_WarpedMotion(this,&xevent);				}			} else {#ifdef DEBUG_MOTION  printf("X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);#endif				posted = SDL_PrivateMouseMotion(0, 0,						xevent.xmotion.x,						xevent.xmotion.y);			}		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -