Significantly improved XIM support. SDL-1.2
authorRyan C. Gordon <icculus@icculus.org>
Mon, 25 Jun 2007 19:58:32 +0000
branchSDL-1.2
changeset 3978b966761fef6c
parent 3977 722db1bd733c
child 3979 1683d807c4fa
Significantly improved XIM support.

Fixes Bugzilla #429.


Selected notes from the patch's README:

= FIXES =

This patch fixes the above issues as follows.

== X11 events ==

Moved XFilterEvent just after XNextEvent so that all events are passed
to it. Also, XFilterEvent will receive masks indicated by IM through
XNFilterEvents IC value as well as masks surpplied by SDL.

X11_KeyRepeat is called between XNextEvent and XFilterEvent, after
testing an event is a KeyRelease. I'm not 100% comfortable to do so,
but I couldn't find a better timing to call it, and use of the
function is inevitable.

== Xutf8LookupString ==

Used a longer buffer to receive UTF-8 string. If it is insufficient,
a dynamic storage of the requested size will be allocated. The
initial size of the buffer is set to 32, because the Japanese text
converted from the most widely used benchmark key sequence for
Japanese IM, "WATASHINONAMAEHANAKANODESU." has ten Japanese characters
in it, that occupies 30 bytes when encoded in UTF-8.

== SDL_keysym.unicode ==

On Windows version of SDL implementation, SDL_keysym.unicode stores
UTF-16 encoded unicode characters, one UTF-16 encoding unit per an SDL
event. A Unicode supplementary characters are sent to an application
as two events. (One with a high surrogate and another with a low
surrogate.) The behavior seems reasonable since it is upward
compatible with existing handling of BMP characters.

I wrote a UTF-8 to UTF-16 conversion function for the purpose. It is
designed with the execution speed in mind, having a minimum set of
features that my patch requires.
src/video/x11/SDL_x11dyn.c
src/video/x11/SDL_x11dyn.h
src/video/x11/SDL_x11events.c
src/video/x11/SDL_x11sym.h
src/video/x11/SDL_x11video.c
     1.1 --- a/src/video/x11/SDL_x11dyn.c	Mon Jun 25 14:58:22 2007 +0000
     1.2 +++ b/src/video/x11/SDL_x11dyn.c	Mon Jun 25 19:58:32 2007 +0000
     1.3 @@ -98,6 +98,7 @@
     1.4  /* Annoying varargs entry point... */
     1.5  #ifdef X_HAVE_UTF8_STRING
     1.6  XIC (*pXCreateIC)(XIM,...) = NULL;
     1.7 +char *(*pXGetICValues)(XIC, ...) = NULL;
     1.8  #endif
     1.9  
    1.10  /* These SDL_X11_HAVE_* flags are here whether you have dynamic X11 or not. */
    1.11 @@ -127,6 +128,7 @@
    1.12  
    1.13  			#ifdef X_HAVE_UTF8_STRING
    1.14  			pXCreateIC = NULL;
    1.15 +			pXGetICValues = NULL;
    1.16  			#endif
    1.17  
    1.18  			for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
    1.19 @@ -163,6 +165,7 @@
    1.20  
    1.21  		#ifdef X_HAVE_UTF8_STRING
    1.22  		X11_GetSym("XCreateIC",&SDL_X11_HAVE_UTF8,(void **)&pXCreateIC);
    1.23 +		X11_GetSym("XGetICValues",&SDL_X11_HAVE_UTF8,(void **)&pXGetICValues);
    1.24  		#endif
    1.25  
    1.26  		if (SDL_X11_HAVE_BASEXLIB) {  /* all required symbols loaded. */
    1.27 @@ -178,6 +181,7 @@
    1.28  		#endif
    1.29  		#ifdef X_HAVE_UTF8_STRING
    1.30  		pXCreateIC = XCreateIC;
    1.31 +		pXGetICValues = XGetICValues;
    1.32  		#endif
    1.33  	#endif
    1.34  
     2.1 --- a/src/video/x11/SDL_x11dyn.h	Mon Jun 25 14:58:22 2007 +0000
     2.2 +++ b/src/video/x11/SDL_x11dyn.h	Mon Jun 25 19:58:32 2007 +0000
     2.3 @@ -68,6 +68,7 @@
     2.4  /* That's really annoying...make this a function pointer no matter what. */
     2.5  #ifdef X_HAVE_UTF8_STRING
     2.6  extern XIC (*pXCreateIC)(XIM,...);
     2.7 +extern char *(*pXGetICValues)(XIC, ...);
     2.8  #endif
     2.9  
    2.10  /* These SDL_X11_HAVE_* flags are here whether you have dynamic X11 or not. */
     3.1 --- a/src/video/x11/SDL_x11events.c	Mon Jun 25 14:58:22 2007 +0000
     3.2 +++ b/src/video/x11/SDL_x11events.c	Mon Jun 25 19:58:32 2007 +0000
     3.3 @@ -176,6 +176,124 @@
     3.4  	}
     3.5  	return c;
     3.6  }
     3.7 +
     3.8 +/* Given a UTF-8 encoded string pointed to by utf8 of length length in
     3.9 +   bytes, returns the corresponding UTF-16 encoded string in the
    3.10 +   buffer pointed to by utf16.  The maximum number of UTF-16 encoding
    3.11 +   units (i.e., Unit16s) allowed in the buffer is specified in
    3.12 +   utf16_max_length.  The return value is the number of UTF-16
    3.13 +   encoding units placed in the output buffer pointed to by utf16.
    3.14 +
    3.15 +   In case of an error, -1 is returned, leaving some unusable partial
    3.16 +   results in the output buffer.
    3.17 +
    3.18 +   The caller must estimate the size of utf16 buffer by itself before
    3.19 +   calling this function.  Insufficient output buffer is considered as
    3.20 +   an error, and once an error occured, this function doesn't give any
    3.21 +   clue how large the result will be.
    3.22 +
    3.23 +   The error cases include following:
    3.24 +
    3.25 +   - Invalid byte sequences were in the input UTF-8 bytes.  The caller
    3.26 +     has no way to know what point in the input buffer was the
    3.27 +     errornous byte.
    3.28 +
    3.29 +   - The input contained a character (a valid UTF-8 byte sequence)
    3.30 +     whose scalar value exceeded the range that UTF-16 can represent
    3.31 +     (i.e., characters whose Unicode scalar value above 0x110000).
    3.32 +
    3.33 +   - The output buffer has no enough space to hold entire utf16 data.
    3.34 +
    3.35 +   Please note:
    3.36 +
    3.37 +   - '\0'-termination is not assumed both on the input UTF-8 string
    3.38 +     and on the output UTF-16 string; any legal zero byte in the input
    3.39 +     UTF-8 string will be converted to a 16-bit zero in output.  As a
    3.40 +     side effect, the last UTF-16 encoding unit stored in the output
    3.41 +     buffer will have a non-zero value if the input UTF-8 was not
    3.42 +     '\0'-terminated.
    3.43 +
    3.44 +   - UTF-8 aliases are *not* considered as an error.  They are
    3.45 +     converted to UTF-16.  For example, 0xC0 0xA0, 0xE0 0x80 0xA0, 
    3.46 +     and 0xF0 0x80 0x80 0xA0 are all mapped to a single UTF-16
    3.47 +     encoding unit 0x0020.
    3.48 +
    3.49 +   - Three byte UTF-8 sequences whose value corresponds to a surrogate
    3.50 +     code or other reserved scalar value are not considered as an
    3.51 +     error either.  They may cause an invalid UTF-16 data (e.g., those
    3.52 +     containing unpaired surrogates).
    3.53 +
    3.54 +*/
    3.55 +
    3.56 +static int Utf8ToUtf16(const Uint8 *utf8, const int utf8_length, Uint16 *utf16, const int utf16_max_length) {
    3.57 +
    3.58 +    /* p moves over the output buffer.  max_ptr points to the next to the last slot of the buffer.  */
    3.59 +    Uint16 *p = utf16;
    3.60 +    Uint16 const *const max_ptr = utf16 + utf16_max_length;
    3.61 +
    3.62 +    /* end_of_input points to the last byte of input as opposed to the next to the last byte.  */
    3.63 +    Uint8 const *const end_of_input = utf8 + utf8_length - 1;
    3.64 +
    3.65 +    while (utf8 <= end_of_input) {
    3.66 +	if (p >= max_ptr) {
    3.67 +	    /* No more output space.  */
    3.68 +	    return -1;
    3.69 +	}
    3.70 +	Uint8 const c = *utf8;
    3.71 +	if (c < 0x80) {
    3.72 +	    /* One byte ASCII.  */
    3.73 +	    *p++ = c;
    3.74 +	    utf8 += 1;
    3.75 +	} else if (c < 0xC0) {
    3.76 +	    /* Follower byte without preceeding leader bytes.  */
    3.77 +	    return -1;
    3.78 +	} else if (c < 0xE0) {
    3.79 +	    /* Two byte sequence.  We need one follower byte.  */
    3.80 +	    if (end_of_input - utf8 < 1 || (((utf8[1] ^ 0x80)) & 0xC0)) {
    3.81 +		return -1;
    3.82 +	    }
    3.83 +	    *p++ = (Uint16)(0xCF80 + (c << 6) + utf8[1]);
    3.84 +	    utf8 += 2;
    3.85 +	} else if (c < 0xF0) {
    3.86 +	    /* Three byte sequence.  We need two follower byte.  */
    3.87 +	    if (end_of_input - utf8 < 2 || (((utf8[1] ^ 0x80) | (utf8[2] ^ 0x80)) & 0xC0)) {
    3.88 +		return -1;
    3.89 +	    }
    3.90 +	    *p++ = (Uint16)(0xDF80 + (c << 12) + (utf8[1] << 6) + utf8[2]);
    3.91 +	    utf8 += 3;
    3.92 +	} else if (c < 0xF8) {
    3.93 +	    int plane;
    3.94 +	    /* Four byte sequence.  We need three follower bytes.  */
    3.95 +	    if (end_of_input - utf8 < 3 || (((utf8[1] ^ 0x80) | (utf8[2] ^0x80) | (utf8[3] ^ 0x80)) & 0xC0)) {
    3.96 +		return -1;
    3.97 +	    }
    3.98 +	    plane = (-0xC8 + (c << 2) + (utf8[1] >> 4));
    3.99 +	    if (plane == 0) {
   3.100 +		/* This four byte sequence is an alias that
   3.101 +                   corresponds to a Unicode scalar value in BMP.
   3.102 +		   It fits in an UTF-16 encoding unit.  */
   3.103 +		*p++ = (Uint16)(0xDF80 + (utf8[1] << 12) + (utf8[2] << 6) + utf8[3]);
   3.104 +	    } else if (plane <= 16) {
   3.105 +		/* This is a legal four byte sequence that corresponds to a surrogate pair.  */
   3.106 +		if (p + 1 >= max_ptr) {
   3.107 +		    /* No enough space on the output buffer for the pair.  */
   3.108 +		    return -1;
   3.109 +		}
   3.110 +		*p++ = (Uint16)(0xE5B8 + (c << 8) + (utf8[1] << 2) + (utf8[2] >> 4));
   3.111 +		*p++ = (Uint16)(0xDB80 + ((utf8[2] & 0x0F) << 6) + utf8[3]);
   3.112 +	    } else {
   3.113 +		/* This four byte sequence is out of UTF-16 code space.  */
   3.114 +		return -1;
   3.115 +	    }
   3.116 +	    utf8 += 4;
   3.117 +	} else {
   3.118 +	    /* Longer sequence or unused byte.  */
   3.119 +	    return -1;
   3.120 +	}
   3.121 +    }
   3.122 +    return p - utf16;
   3.123 +}
   3.124 +
   3.125  #endif
   3.126  
   3.127  /* Check to see if this is a repeated key.
   3.128 @@ -275,6 +393,24 @@
   3.129  	SDL_memset(&xevent, '\0', sizeof (XEvent));  /* valgrind fix. --ryan. */
   3.130  	XNextEvent(SDL_Display, &xevent);
   3.131  
   3.132 +	/* Discard KeyRelease and KeyPress events generated by auto-repeat.
   3.133 +	   We need to do it before passing event to XFilterEvent.  Otherwise,
   3.134 +	   KeyRelease aware IMs are confused...  */
   3.135 +	if ( xevent.type == KeyRelease
   3.136 +	     && X11_KeyRepeat(SDL_Display, &xevent) ) {
   3.137 +		return 0;
   3.138 +	}
   3.139 +
   3.140 +#ifdef X_HAVE_UTF8_STRING
   3.141 +	/* If we are translating with IM, we need to pass all events
   3.142 +	   to XFilterEvent, and discard those filtered events immediately.  */
   3.143 +	if ( SDL_TranslateUNICODE
   3.144 +	     && SDL_IM != NULL
   3.145 +	     && XFilterEvent(&xevent, None) ) {
   3.146 +		return 0;
   3.147 +	}
   3.148 +#endif
   3.149 +
   3.150  	posted = 0;
   3.151  	switch (xevent.type) {
   3.152  
   3.153 @@ -358,6 +494,13 @@
   3.154  	    }
   3.155  	    break;
   3.156  
   3.157 +	    /* Some IM requires MappingNotify to be passed to
   3.158 +	       XRefreshKeyboardMapping by the app.  */
   3.159 +	    case MappingNotify: {
   3.160 +		XRefreshKeyboardMapping(&xevent.xmapping);
   3.161 +	    }
   3.162 +	    break;
   3.163 +
   3.164  	    /* Generated upon EnterWindow and FocusIn */
   3.165  	    case KeymapNotify: {
   3.166  #ifdef DEBUG_XEVENTS
   3.167 @@ -409,50 +552,168 @@
   3.168  
   3.169  	    /* Key press? */
   3.170  	    case KeyPress: {
   3.171 -		static SDL_keysym saved_keysym;
   3.172  		SDL_keysym keysym;
   3.173  		KeyCode keycode = xevent.xkey.keycode;
   3.174  
   3.175  #ifdef DEBUG_XEVENTS
   3.176  printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
   3.177  #endif
   3.178 -		/* Get the translated SDL virtual keysym */
   3.179 -		if ( keycode ) {
   3.180 +		/* If we're not doing translation, we're done! */
   3.181 +		if ( !SDL_TranslateUNICODE ) {
   3.182 +			/* Get the translated SDL virtual keysym and put it on the queue.*/
   3.183  			keysym.scancode = keycode;
   3.184  			keysym.sym = X11_TranslateKeycode(SDL_Display, keycode);
   3.185  			keysym.mod = KMOD_NONE;
   3.186  			keysym.unicode = 0;
   3.187 -		} else {
   3.188 -			keysym = saved_keysym;
   3.189 -		}
   3.190 -
   3.191 -		/* If we're not doing translation, we're done! */
   3.192 -		if ( !SDL_TranslateUNICODE ) {
   3.193  			posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
   3.194  			break;
   3.195  		}
   3.196  
   3.197 -		if ( XFilterEvent(&xevent, None) ) {
   3.198 -			if ( xevent.xkey.keycode ) {
   3.199 -				posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
   3.200 -			} else {
   3.201 -				/* Save event to be associated with IM text
   3.202 -				   In 1.3 we'll have a text event instead.. */
   3.203 -				saved_keysym = keysym;
   3.204 -			}
   3.205 -			break;
   3.206 -		}
   3.207 -
   3.208  		/* Look up the translated value for the key event */
   3.209  #ifdef X_HAVE_UTF8_STRING
   3.210  		if ( SDL_IC != NULL ) {
   3.211 -			static Status state;
   3.212 +			Status status;
   3.213 +			KeySym xkeysym;
   3.214 +			int i;
   3.215  			/* A UTF-8 character can be at most 6 bytes */
   3.216 -			char keybuf[6];
   3.217 -			if ( Xutf8LookupString(SDL_IC, &xevent.xkey,
   3.218 -			                        keybuf, sizeof(keybuf),
   3.219 -			                        NULL, &state) ) {
   3.220 -				keysym.unicode = Utf8ToUcs4((Uint8*)keybuf);
   3.221 +			/* ... It's true, but Xutf8LookupString can
   3.222 +			   return more than one characters.  Moreover,
   3.223 +			   the spec. put no upper bound, so we should
   3.224 +			   be ready for longer strings.  */
   3.225 +			char keybuf[32];
   3.226 +			char *keydata = keybuf;
   3.227 +			int count;
   3.228 +			Uint16 utf16buf[32];
   3.229 +			Uint16 *utf16data = utf16buf;
   3.230 +			int utf16size;
   3.231 +			int utf16length;
   3.232 +
   3.233 +			count = Xutf8LookupString(SDL_IC, &xevent.xkey, keydata, sizeof(keybuf), &xkeysym, &status);
   3.234 +			if (XBufferOverflow == status) {
   3.235 +			  /* The IM has just generated somewhat long
   3.236 +			     string.  We need a longer buffer in this
   3.237 +			     case.  */
   3.238 +			  keydata = SDL_malloc(count);
   3.239 +			  if ( keydata == NULL ) {
   3.240 +			    SDL_OutOfMemory();
   3.241 +			    break;
   3.242 +			  }
   3.243 +			  count = Xutf8LookupString(SDL_IC, &xevent.xkey, keydata, count, &xkeysym, &status);
   3.244 +			}
   3.245 +
   3.246 +			switch (status) {
   3.247 +
   3.248 +			case XBufferOverflow: {
   3.249 +			  /* Oops!  We have allocated the bytes as
   3.250 +			     requested by Xutf8LookupString, so the
   3.251 +			     length of the buffer must be
   3.252 +			     sufficient.  This case should never
   3.253 +			     happen! */
   3.254 +			  SDL_SetError("Xutf8LookupString indicated a double buffer overflow!");
   3.255 +			  break;
   3.256 +			}
   3.257 +
   3.258 +			case XLookupChars:
   3.259 +			case XLookupBoth: {
   3.260 +			  if (0 == count) {
   3.261 +			    break;
   3.262 +			  }
   3.263 +
   3.264 +			  /* We got a converted string from IM.  Make
   3.265 +			     sure to deliver all characters to the
   3.266 +			     application as SDL events.  Note that
   3.267 +			     an SDL event can only carry one UTF-16
   3.268 +			     encoding unit, and a surrogate pair is
   3.269 +			     delivered as two SDL events.  I guess
   3.270 +			     this behaviour is probably _imported_
   3.271 +			     from Windows or MacOS.  To do so, we need
   3.272 +			     to convert the UTF-8 data into UTF-16
   3.273 +			     data (not UCS4/UTF-32!).  We need an
   3.274 +			     estimate of the number of UTF-16 encoding
   3.275 +			     units here.  The worst case is pure ASCII
   3.276 +			     string.  Assume so. */
   3.277 +			  /* In 1.3 SDL may have a text event instead, that
   3.278 +			     carries the whole UTF-8 string with it. */
   3.279 +			  utf16size = count * sizeof(Uint16);
   3.280 +			  if (utf16size > sizeof(utf16buf)) {
   3.281 +			    utf16data = (Uint16 *) SDL_malloc(utf16size);
   3.282 +			    if (utf16data == NULL) {
   3.283 +			      SDL_OutOfMemory();
   3.284 +			      break;
   3.285 +			    }
   3.286 +			  }
   3.287 +			  utf16length = Utf8ToUtf16((Uint8 *)keydata, count, utf16data, utf16size);
   3.288 +			  if (utf16length < 0) {
   3.289 +			    /* The keydata contained an invalid byte
   3.290 +			       sequence.  It should be a bug of the IM
   3.291 +			       or Xlib... */
   3.292 +			    SDL_SetError("Oops! Xutf8LookupString returned an invalid UTF-8 sequence!");
   3.293 +			    break;
   3.294 +			  }
   3.295 +
   3.296 +			  /* Deliver all UTF-16 encoding units.  At
   3.297 +			     this moment, SDL event queue has a
   3.298 +			     fixed size (128 events), and an SDL
   3.299 +			     event can hold just one UTF-16 encoding
   3.300 +			     unit.  So, if we receive more than 128
   3.301 +			     UTF-16 encoding units from a commit,
   3.302 +			     exceeded characters will be lost.  */
   3.303 +			  for (i = 0; i < utf16length - 1; i++) {
   3.304 +			    keysym.scancode = 0;
   3.305 +			    keysym.sym = SDLK_UNKNOWN;
   3.306 +			    keysym.mod = KMOD_NONE;
   3.307 +			    keysym.unicode = utf16data[i];
   3.308 +			    posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
   3.309 +			  }
   3.310 +			  /* The keysym for the last character carries the
   3.311 +			     scancode and symbol that corresponds to the X11
   3.312 +			     keycode.  */
   3.313 +			  if (utf16length > 0) {			       
   3.314 +			    keysym.scancode = keycode;
   3.315 +			    keysym.sym = (keycode ? X11_TranslateKeycode(SDL_Display, keycode) : 0);
   3.316 +			    keysym.mod = KMOD_NONE;
   3.317 +			    keysym.unicode = utf16data[utf16length - 1];
   3.318 +			    posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
   3.319 +			  }
   3.320 +			  break;
   3.321 +			}
   3.322 +
   3.323 +			case XLookupKeySym: {
   3.324 +			  /* I'm not sure whether it is possible that
   3.325 +			     a zero keycode makes XLookupKeySym
   3.326 +			     status.  What I'm sure is that a
   3.327 +			     combination of a zero scan code and a non
   3.328 +			     zero sym makes SDL_PrivateKeyboard
   3.329 +			     strange state...  So, just discard it.
   3.330 +			     If this doesn't work, I'm receiving bug
   3.331 +			     reports, and I can know under what
   3.332 +			     condition this case happens.  */
   3.333 +			  if (keycode) {
   3.334 +			    keysym.scancode = keycode;
   3.335 +			    keysym.sym = X11_TranslateKeycode(SDL_Display, keycode);
   3.336 +			    keysym.mod = KMOD_NONE;
   3.337 +			    keysym.unicode = 0;
   3.338 +			    posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
   3.339 +			  }
   3.340 +			  break;
   3.341 +			}
   3.342 +
   3.343 +			case XLookupNone: {
   3.344 +			  /* IM has eaten the event.  */
   3.345 +			  break;
   3.346 +			}
   3.347 +
   3.348 +			default:
   3.349 +			  /* An unknown status from Xutf8LookupString.  */
   3.350 +			  SDL_SetError("Oops! Xutf8LookupStringreturned an unknown status");
   3.351 +			}
   3.352 +
   3.353 +			/* Release dynamic buffers if allocated.  */
   3.354 +			if (keydata != NULL && keybuf != keydata) {
   3.355 +			  SDL_free(keydata);
   3.356 +			}
   3.357 +			if (utf16data != NULL && utf16buf != utf16data) {
   3.358 +			  SDL_free(utf16data);
   3.359  			}
   3.360  		}
   3.361  		else
   3.362 @@ -472,8 +733,9 @@
   3.363  				*/
   3.364  				keysym.unicode = (Uint8)keybuf[0];
   3.365  			}
   3.366 +
   3.367 +			posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
   3.368  		}
   3.369 -		posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
   3.370  	    }
   3.371  	    break;
   3.372  
   3.373 @@ -482,13 +744,17 @@
   3.374  		SDL_keysym keysym;
   3.375  		KeyCode keycode = xevent.xkey.keycode;
   3.376  
   3.377 +		if (keycode == 0) {
   3.378 +		  /* There should be no KeyRelease for keycode == 0,
   3.379 +		     since it is a notification from IM but a real
   3.380 +		     keystroke.  */
   3.381 +		  /* We need to emit some diagnostic message here.  */
   3.382 +		  break;
   3.383 +		}
   3.384 +
   3.385  #ifdef DEBUG_XEVENTS
   3.386  printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
   3.387  #endif
   3.388 -		/* Check to see if this is a repeated key */
   3.389 -		if ( X11_KeyRepeat(SDL_Display, &xevent) ) {
   3.390 -			break;
   3.391 -		}
   3.392  
   3.393  		/* Get the translated SDL virtual keysym */
   3.394  		keysym.scancode = keycode;
     4.1 --- a/src/video/x11/SDL_x11sym.h	Mon Jun 25 14:58:22 2007 +0000
     4.2 +++ b/src/video/x11/SDL_x11sym.h	Mon Jun 25 19:58:32 2007 +0000
     4.3 @@ -143,8 +143,10 @@
     4.4  SDL_X11_SYM(void,XDestroyIC,(XIC a),(a),)
     4.5  SDL_X11_SYM(void,XSetICFocus,(XIC a),(a),)
     4.6  SDL_X11_SYM(void,XUnsetICFocus,(XIC a),(a),)
     4.7 +/*SDL_X11_SYM(char*,XGetICValues,(XIC a, ...),return)*/
     4.8  SDL_X11_SYM(XIM,XOpenIM,(Display* a,struct _XrmHashBucketRec* b,char* c,char* d),(a,b,c,d),return)
     4.9  SDL_X11_SYM(Status,XCloseIM,(XIM a),(a),return)
    4.10 +SDL_X11_SYM(char*,XSetLocaleModifiers,(_Xconst char* a),(a),return)
    4.11  #endif
    4.12  
    4.13  #ifndef NO_SHARED_MEMORY
     5.1 --- a/src/video/x11/SDL_x11video.c	Mon Jun 25 14:58:22 2007 +0000
     5.2 +++ b/src/video/x11/SDL_x11video.c	Mon Jun 25 19:58:32 2007 +0000
     5.3 @@ -54,6 +54,10 @@
     5.4  #include "SDL_x11gamma_c.h"
     5.5  #include "../blank_cursor.h"
     5.6  
     5.7 +#ifdef X_HAVE_UTF8_STRING
     5.8 +#include <locale.h>
     5.9 +#endif
    5.10 +
    5.11  /* Initialization/Query functions */
    5.12  static int X11_VideoInit(_THIS, SDL_PixelFormat *vformat);
    5.13  static SDL_Surface *X11_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
    5.14 @@ -105,8 +109,10 @@
    5.15  			SDL_memset(device, 0, (sizeof *device));
    5.16  			device->hidden = (struct SDL_PrivateVideoData *)
    5.17  					SDL_malloc((sizeof *device->hidden));
    5.18 +			SDL_memset(device->hidden, 0, (sizeof *device->hidden));
    5.19  			device->gl_data = (struct SDL_PrivateGLData *)
    5.20  					SDL_malloc((sizeof *device->gl_data));
    5.21 +			SDL_memset(device->gl_data, 0, (sizeof *device->gl_data));
    5.22  		}
    5.23  		if ( (device == NULL) || (device->hidden == NULL) ||
    5.24  		                         (device->gl_data == NULL) ) {
    5.25 @@ -314,6 +320,7 @@
    5.26      char classname[1024];
    5.27      XSetWindowAttributes xattr;
    5.28      XWMHints *hints;
    5.29 +    unsigned long app_event_mask;
    5.30      int def_vis = (SDL_Visual == DefaultVisual(SDL_Display, SDL_Screen));
    5.31  
    5.32      /* Look up some useful Atoms */
    5.33 @@ -391,9 +398,9 @@
    5.34      XFree(hints);
    5.35      X11_SetCaptionNoLock(this, this->wm_title, this->wm_icon);
    5.36  
    5.37 -    XSelectInput(SDL_Display, WMwindow,
    5.38 -		 FocusChangeMask | KeyPressMask | KeyReleaseMask
    5.39 -		 | PropertyChangeMask | StructureNotifyMask | KeymapStateMask);
    5.40 +    app_event_mask = FocusChangeMask | KeyPressMask | KeyReleaseMask
    5.41 +	| PropertyChangeMask | StructureNotifyMask | KeymapStateMask;
    5.42 +    XSelectInput(SDL_Display, WMwindow, app_event_mask);
    5.43  
    5.44      /* Set the class hints so we can get an icon (AfterStep) */
    5.45      get_classname(classname, sizeof(classname));
    5.46 @@ -409,19 +416,111 @@
    5.47      }
    5.48  
    5.49  	/* Setup the communication with the IM server */
    5.50 -	SDL_IM = NULL;
    5.51 -	SDL_IC = NULL;
    5.52 +	/* create_aux_windows may be called several times against the same
    5.53 +	   Display.  We should reuse the SDL_IM if one has been opened for
    5.54 +	   the Display, so we should not simply reset SDL_IM here.  */
    5.55  
    5.56  	#ifdef X_HAVE_UTF8_STRING
    5.57  	if (SDL_X11_HAVE_UTF8) {
    5.58 +		/* Discard obsolete resources if any.  */
    5.59 +		if (SDL_IM != NULL && SDL_Display != XDisplayOfIM(SDL_IM)) {
    5.60 +			/* Just a double check. I don't think this
    5.61 +		           code is ever executed. */
    5.62 +			SDL_SetError("display has changed while an IM is kept");
    5.63 +			if (SDL_IC) {
    5.64 +				XUnsetICFocus(SDL_IC);
    5.65 +				XDestroyIC(SDL_IC);
    5.66 +				SDL_IC = NULL;
    5.67 +			}
    5.68 +			XCloseIM(SDL_IM);
    5.69 +			SDL_IM = NULL;
    5.70 +		}
    5.71 +
    5.72 +		/* Open an input method.  */
    5.73 +		if (SDL_IM == NULL) {
    5.74 +			char *old_locale, *old_modifiers;
    5.75 +			/* I'm not comfortable to do locale setup
    5.76 +			   here.  However, we need C library locale
    5.77 +			   (and xlib modifiers) to be set based on the
    5.78 +			   user's preference to use XIM, and many
    5.79 +			   existing game programs doesn't take care of
    5.80 +			   users' locale preferences, so someone other
    5.81 +			   than the game program should do it.
    5.82 +			   Moreover, ones say that some game programs
    5.83 +			   heavily rely on the C locale behaviour,
    5.84 +			   e.g., strcol()'s, and we can't change the C
    5.85 +			   library locale.  Given the situation, I
    5.86 +			   couldn't find better place to do the
    5.87 +			   job... */
    5.88 +
    5.89 +			/* Save the current (application program's)
    5.90 +			   locale settings.  */
    5.91 +			old_locale = setlocale(LC_ALL, NULL);
    5.92 +			old_modifiers = XSetLocaleModifiers(NULL);
    5.93 +			if (old_locale == NULL || old_modifiers == NULL) {
    5.94 +				/* The specs guarantee that the query
    5.95 +				   calls to above functions never
    5.96 +				   fail, so we should never come
    5.97 +				   here.  */
    5.98 +				SDL_SetError("failed to retreive current locale settings");
    5.99 +				old_locale = NULL;
   5.100 +				old_modifiers = NULL;
   5.101 +			} else {
   5.102 +				/* Save retreived values in our own
   5.103 +				   storage, since they may be
   5.104 +				   overwritten by the successive calls
   5.105 +				   to
   5.106 +				   setlocale/XSetLocaleModifiers.  */
   5.107 +				char const *p;
   5.108 +				p = old_locale;
   5.109 +				old_locale = SDL_malloc(strlen(p) + 1);
   5.110 +				strcpy(old_locale, p);
   5.111 +				p = old_modifiers;
   5.112 +				old_modifiers = SDL_malloc(strlen(p) + 1);
   5.113 +				strcpy(old_modifiers, p);
   5.114 +			}
   5.115 +
   5.116 +			/* Fetch the user's preferences and open the
   5.117 +			   input method with them.  */
   5.118 +			setlocale(LC_ALL, "");
   5.119 +			XSetLocaleModifiers("");
   5.120 +			SDL_IM = XOpenIM(SDL_Display, NULL, classname, classname);
   5.121 +
   5.122 +			/* Restore the application's locale settings
   5.123 +			   so that we don't break the application's
   5.124 +			   expected behaviour.  */
   5.125 +			if (old_locale != NULL && old_modifiers != NULL) {
   5.126 +				/* We need to restore the C library
   5.127 +				   locale first, since the
   5.128 +				   interpretation of the X modifier
   5.129 +				   may depend on it.  */
   5.130 +				setlocale(LC_ALL, old_locale);
   5.131 +				SDL_free(old_locale);
   5.132 +				XSetLocaleModifiers(old_modifiers);
   5.133 +				SDL_free(old_modifiers);
   5.134 +			}
   5.135 +		}
   5.136 +
   5.137 +		/* Create a new input context for the new window just created.  */
   5.138  		SDL_IM = XOpenIM(SDL_Display, NULL, classname, classname);
   5.139  		if (SDL_IM == NULL) {
   5.140  			SDL_SetError("no input method could be opened");
   5.141  		} else {
   5.142 +			if (SDL_IC != NULL) {
   5.143 +				/* Discard the old IC before creating new one.  */
   5.144 +			    XUnsetICFocus(SDL_IC);
   5.145 +			    XDestroyIC(SDL_IC);
   5.146 +			}
   5.147 +			/* Theoretically we should check the current IM supports
   5.148 +			   PreeditNothing+StatusNothing style (i.e., root window method)
   5.149 +			   before creating the IC.  However, it is the bottom line method,
   5.150 +			   and we supports any other options.  If the IM didn't support
   5.151 +			   root window method, the following call fails, and SDL falls
   5.152 +			   back to pre-XIM keyboard handling.  */
   5.153  			SDL_IC = pXCreateIC(SDL_IM,
   5.154  					XNClientWindow, WMwindow,
   5.155  					XNFocusWindow, WMwindow,
   5.156 -					XNInputStyle, XIMPreeditNothing  | XIMStatusNothing,
   5.157 +					XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
   5.158  					XNResourceName, classname,
   5.159  					XNResourceClass, classname,
   5.160  					NULL);
   5.161 @@ -430,6 +529,19 @@
   5.162  				SDL_SetError("no input context could be created");
   5.163  				XCloseIM(SDL_IM);
   5.164  				SDL_IM = NULL;
   5.165 +			} else {
   5.166 +				/* We need to receive X events that an IM wants and to pass
   5.167 +				   them to the IM through XFilterEvent. The set of events may
   5.168 +				   vary depending on the IM implementation and the options
   5.169 +				   specified through various routes. Although unlikely, the
   5.170 +				   xlib specification allows IM to change the event requirement
   5.171 +				   with its own circumstances, it is safe to call SelectInput
   5.172 +				   whenever we re-create an IC.  */
   5.173 +				unsigned long mask = 0;
   5.174 +				char *ret = pXGetICValues(SDL_IC, XNFilterEvents, &mask, NULL);
   5.175 +				                          &ic_event_mask, NULL);
   5.176 +				XSelectInput(SDL_Display, WMwindow, app_event_mask | mask);
   5.177 +				XSetICFocus(SDL_IC);
   5.178  			}
   5.179  		}
   5.180  	}
   5.181 @@ -1350,6 +1462,7 @@
   5.182  		/* Close the connection with the IM server */
   5.183  		#ifdef X_HAVE_UTF8_STRING
   5.184  		if (SDL_IC != NULL) {
   5.185 +			XUnsetICFocus(SDL_IC);
   5.186  			XDestroyIC(SDL_IC);
   5.187  			SDL_IC = NULL;
   5.188  		}