| 1 | /* |
|---|
| 2 | SDL_textmanager |
|---|
| 3 | Copyright (C) 2004 Kazunori Itoyanagi |
|---|
| 4 | |
|---|
| 5 | This library is free software; you can redistribute it and/or |
|---|
| 6 | modify it under the terms of the GNU Library General Public |
|---|
| 7 | License as published by the Free Software Foundation; either |
|---|
| 8 | version 2 of the License, or (at your option) any later version. |
|---|
| 9 | |
|---|
| 10 | This library is distributed in the hope that it will be useful, |
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 13 | Library General Public License for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU Library General Public |
|---|
| 16 | License along with this library; if not, write to the Free |
|---|
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 18 | |
|---|
| 19 | ITOYANAGI Kazunori |
|---|
| 20 | itkz@users.sourceforge.jp |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #include <stdio.h> |
|---|
| 24 | #include <stdlib.h> |
|---|
| 25 | #include <string.h> |
|---|
| 26 | #include <ctype.h> |
|---|
| 27 | |
|---|
| 28 | #include "SDL_version.h" |
|---|
| 29 | #include "SDL_textmanager.h" |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | static SDL_TextManager *TextManager_BootStrap[] = |
|---|
| 33 | { |
|---|
| 34 | #ifdef ENABLE_WIN32 |
|---|
| 35 | &TextManager_win32, |
|---|
| 36 | #endif |
|---|
| 37 | #ifdef ENABLE_XIM |
|---|
| 38 | &TextManager_xim, |
|---|
| 39 | #endif |
|---|
| 40 | NULL |
|---|
| 41 | }; |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | typedef struct _TextManager_Event |
|---|
| 45 | { |
|---|
| 46 | TextManager_Message message; |
|---|
| 47 | Uint16 *editingString; |
|---|
| 48 | int cursorPosition; |
|---|
| 49 | struct _TextManager_Event *next; |
|---|
| 50 | int compositionPosition; |
|---|
| 51 | int compositionLength; |
|---|
| 52 | Uint16 character; |
|---|
| 53 | } TextManager_Event; |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | static SDL_TextManager *TextManager_Use = NULL; |
|---|
| 57 | SDL_EventFilter OriginalFilterSDL = NULL; |
|---|
| 58 | |
|---|
| 59 | static TextManager_Event *TextManager_EventQueue = NULL; |
|---|
| 60 | static int TextManager_EventQueueNumber = 0; |
|---|
| 61 | static SDL_bool IsOriginalTextManager = SDL_FALSE; |
|---|
| 62 | static SDL_bool IsInited = SDL_FALSE; |
|---|
| 63 | static SDL_bool IsValid = SDL_TRUE; |
|---|
| 64 | static SDL_bool IsEnableUNICODEOld; |
|---|
| 65 | static TextManager_DeadChar PreviousDeadChar; |
|---|
| 66 | static int BootStrapNum = -1; |
|---|
| 67 | |
|---|
| 68 | static int TextManager_GetUnicodeStringLength(Uint16 *unicodeString); |
|---|
| 69 | static int TextManager_SDLEventFilter(const SDL_Event *e); |
|---|
| 70 | static void TextManager_SetEventFilter(void); |
|---|
| 71 | static void TextManager_RestoreEventFilter(void); |
|---|
| 72 | static void TextManager_AddEventToQueue(TextManager_Event *event); |
|---|
| 73 | static Uint16 TextManager_GetDeadCharCode(TextManager_DeadChar deadChar); |
|---|
| 74 | static Uint16 TextManager_CombineDeadKey( |
|---|
| 75 | TextManager_DeadChar deadChar, Uint16 character); |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | static void TextManager_SetEventFilter(void) |
|---|
| 79 | { |
|---|
| 80 | IsEnableUNICODEOld = SDL_EnableUNICODE(SDL_TRUE); |
|---|
| 81 | OriginalFilterSDL = SDL_GetEventFilter(); |
|---|
| 82 | SDL_SetEventFilter(TextManager_SDLEventFilter); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | static void TextManager_RestoreEventFilter(void) |
|---|
| 87 | { |
|---|
| 88 | SDL_EnableUNICODE(IsEnableUNICODEOld); |
|---|
| 89 | SDL_SetEventFilter(OriginalFilterSDL); |
|---|
| 90 | OriginalFilterSDL = NULL; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | TextManager_Result TextManager_Init(void) |
|---|
| 95 | { |
|---|
| 96 | int i; |
|---|
| 97 | TextManager_Result result; |
|---|
| 98 | |
|---|
| 99 | if (IsInited == SDL_TRUE) { |
|---|
| 100 | return TEXT_MANAGER_ERROR_ALREADY_INIT; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | for (i = 0; TextManager_BootStrap[i]; i++) { |
|---|
| 104 | if (TextManager_BootStrap[i]->available()) { |
|---|
| 105 | TextManager_Use = TextManager_BootStrap[i]; |
|---|
| 106 | break; |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | TextManager_SetEventFilter(); |
|---|
| 111 | if (TextManager_Use != NULL) { |
|---|
| 112 | result = TextManager_Use->init(); |
|---|
| 113 | if (result != TEXT_MANAGER_SUCCESS) { |
|---|
| 114 | TextManager_Use->quit(); |
|---|
| 115 | TextManager_Use = NULL; |
|---|
| 116 | TextManager_RestoreEventFilter(); |
|---|
| 117 | return result; |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | TextManager_EventQueue = NULL; |
|---|
| 121 | TextManager_EventQueueNumber = 0; |
|---|
| 122 | IsOriginalTextManager = SDL_FALSE; |
|---|
| 123 | IsValid = SDL_TRUE; |
|---|
| 124 | IsInited = SDL_TRUE; |
|---|
| 125 | PreviousDeadChar = DEAD_CHAR_NON; |
|---|
| 126 | |
|---|
| 127 | return TEXT_MANAGER_SUCCESS; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | void TextManager_Quit(void) |
|---|
| 132 | { |
|---|
| 133 | TextManager_Event *event; |
|---|
| 134 | TextManager_Event *next; |
|---|
| 135 | |
|---|
| 136 | if (TextManager_Use != NULL) { |
|---|
| 137 | if (TextManager_Use->quit != NULL) { |
|---|
| 138 | TextManager_Use->quit(); |
|---|
| 139 | } |
|---|
| 140 | if (IsOriginalTextManager) { |
|---|
| 141 | free(TextManager_Use); |
|---|
| 142 | } |
|---|
| 143 | TextManager_Use = NULL; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | event = TextManager_EventQueue; |
|---|
| 147 | while (event != NULL) { |
|---|
| 148 | next = event->next; |
|---|
| 149 | if (event->editingString != NULL) { |
|---|
| 150 | free(event->editingString); |
|---|
| 151 | } |
|---|
| 152 | free(event); |
|---|
| 153 | event = next; |
|---|
| 154 | } |
|---|
| 155 | TextManager_EventQueue = NULL; |
|---|
| 156 | TextManager_EventQueueNumber = 0; |
|---|
| 157 | IsOriginalTextManager = SDL_FALSE; |
|---|
| 158 | IsInited = SDL_FALSE; |
|---|
| 159 | TextManager_RestoreEventFilter(); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | int TextManager_BootstrapNum(void) |
|---|
| 164 | { |
|---|
| 165 | int count; |
|---|
| 166 | |
|---|
| 167 | if (BootStrapNum > -1) { |
|---|
| 168 | return BootStrapNum; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | count = 0; |
|---|
| 172 | while (TextManager_BootStrap[count]) { |
|---|
| 173 | count++; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | BootStrapNum = count; |
|---|
| 177 | |
|---|
| 178 | return BootStrapNum; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | SDL_bool TextManager_BootstrapIsAvailable(int num) { |
|---|
| 183 | if (0 <= num && num < TextManager_BootstrapNum()) { |
|---|
| 184 | return TextManager_BootStrap[num]->available(); |
|---|
| 185 | } else { |
|---|
| 186 | return SDL_FALSE; |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | Uint16 *TextManager_BootstrapGetTextManagerName(int num) { |
|---|
| 192 | if (0 <= num && num < TextManager_BootstrapNum()) { |
|---|
| 193 | if (TextManager_BootStrap[num]->available()) { |
|---|
| 194 | return TextManager_BootStrap[num]->get_name(); |
|---|
| 195 | } |
|---|
| 196 | } |
|---|
| 197 | return NULL; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | TextManager_Result TextManager_InitFromBootstrap(int num) |
|---|
| 202 | { |
|---|
| 203 | TextManager_Result result; |
|---|
| 204 | |
|---|
| 205 | if (IsInited == SDL_TRUE) { |
|---|
| 206 | return TEXT_MANAGER_ERROR_ALREADY_INIT; |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | if (0 <= num && num < TextManager_BootstrapNum()) { |
|---|
| 210 | if (TextManager_BootStrap[num]->available()) { |
|---|
| 211 | TextManager_Use = TextManager_BootStrap[num]; |
|---|
| 212 | } else { |
|---|
| 213 | return TEXT_MANAGER_ERROR_NOT_AVAILABLE; |
|---|
| 214 | } |
|---|
| 215 | } else { |
|---|
| 216 | return TEXT_MANAGER_ERROR_INVALID_BOOTSTRAP; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | result = TextManager_Use->init(); |
|---|
| 220 | if (result != TEXT_MANAGER_SUCCESS) { |
|---|
| 221 | TextManager_Use->quit(); |
|---|
| 222 | TextManager_Use = NULL; |
|---|
| 223 | return result; |
|---|
| 224 | } |
|---|
| 225 | TextManager_EventQueue = NULL; |
|---|
| 226 | TextManager_EventQueueNumber = 0; |
|---|
| 227 | IsOriginalTextManager = SDL_FALSE; |
|---|
| 228 | IsValid = SDL_TRUE; |
|---|
| 229 | IsInited = SDL_TRUE; |
|---|
| 230 | |
|---|
| 231 | return TEXT_MANAGER_SUCCESS; |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | void TextManager_Reset(void) |
|---|
| 236 | { |
|---|
| 237 | TextManager_Event *event; |
|---|
| 238 | TextManager_Event *next; |
|---|
| 239 | |
|---|
| 240 | event = TextManager_EventQueue; |
|---|
| 241 | while (event != NULL) { |
|---|
| 242 | next = event->next; |
|---|
| 243 | if (event->editingString != NULL) { |
|---|
| 244 | free(event->editingString); |
|---|
| 245 | } |
|---|
| 246 | free(event); |
|---|
| 247 | event = next; |
|---|
| 248 | } |
|---|
| 249 | TextManager_EventQueue = NULL; |
|---|
| 250 | TextManager_EventQueueNumber = 0; |
|---|
| 251 | |
|---|
| 252 | if (TextManager_Use != NULL) { |
|---|
| 253 | if (TextManager_Use->reset != NULL) { |
|---|
| 254 | TextManager_Use->reset(); |
|---|
| 255 | } |
|---|
| 256 | } |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | TextManager_Result TextManager_Validate(void) |
|---|
| 261 | { |
|---|
| 262 | TextManager_Result result; |
|---|
| 263 | |
|---|
| 264 | if (TextManager_Use == NULL) { |
|---|
| 265 | SDL_SetError("Input method is no init"); |
|---|
| 266 | return TEXT_MANAGER_ERROR_NO_INIT; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | if (IsValid == SDL_TRUE) { |
|---|
| 270 | return TEXT_MANAGER_ERROR_ALREADY_VALIDATED; |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | if (TextManager_Use != NULL) { |
|---|
| 274 | if (TextManager_Use->validate != NULL) { |
|---|
| 275 | result = TextManager_Use->validate(); |
|---|
| 276 | if (result == TEXT_MANAGER_SUCCESS) { |
|---|
| 277 | IsValid = SDL_TRUE; |
|---|
| 278 | } |
|---|
| 279 | return result; |
|---|
| 280 | } |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | SDL_SetError("Input method is no init"); |
|---|
| 284 | return TEXT_MANAGER_ERROR_NO_INIT; |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | TextManager_Result TextManager_Invalidate(void) |
|---|
| 289 | { |
|---|
| 290 | TextManager_Result result; |
|---|
| 291 | |
|---|
| 292 | if (TextManager_Use == NULL) { |
|---|
| 293 | SDL_SetError("Input method is no init"); |
|---|
| 294 | return TEXT_MANAGER_ERROR_NO_INIT; |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | if (IsValid == SDL_FALSE) { |
|---|
| 298 | return TEXT_MANAGER_ERROR_ALREADY_INVALIDATED; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | if (TextManager_Use != NULL) { |
|---|
| 302 | if (TextManager_Use->invalidate !=NULL) { |
|---|
| 303 | result = TextManager_Use->invalidate(); |
|---|
| 304 | if (result == TEXT_MANAGER_SUCCESS) { |
|---|
| 305 | IsValid = SDL_FALSE; |
|---|
| 306 | } |
|---|
| 307 | return result; |
|---|
| 308 | } |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | SDL_SetError("Input method is no init"); |
|---|
| 312 | return TEXT_MANAGER_ERROR_NO_INIT; |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | int TextManager_GetEventNumber(void) |
|---|
| 317 | { |
|---|
| 318 | return TextManager_EventQueueNumber; |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | void TextManager_MoveNextEvent(void) |
|---|
| 323 | { |
|---|
| 324 | TextManager_Event *next; |
|---|
| 325 | |
|---|
| 326 | if (TextManager_EventQueueNumber > 0) { |
|---|
| 327 | if (TextManager_EventQueue->editingString != NULL) { |
|---|
| 328 | free(TextManager_EventQueue->editingString); |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | if (TextManager_EventQueue != NULL) { |
|---|
| 332 | next = TextManager_EventQueue->next; |
|---|
| 333 | free(TextManager_EventQueue); |
|---|
| 334 | TextManager_EventQueue = next; |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | TextManager_EventQueueNumber--; |
|---|
| 338 | } |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | TextManager_Message TextManager_GetCurrentMessage(void) |
|---|
| 343 | { |
|---|
| 344 | if (TextManager_EventQueue == NULL) { |
|---|
| 345 | SDL_SetError("Message is not found"); |
|---|
| 346 | return TEXT_MANAGER_MESSAGE_NO_EXIST; |
|---|
| 347 | } else { |
|---|
| 348 | return TextManager_EventQueue->message; |
|---|
| 349 | } |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | Uint16 *TextManager_GetCurrentEditingString(void) |
|---|
| 354 | { |
|---|
| 355 | if (TextManager_EventQueue == NULL) { |
|---|
| 356 | return NULL; |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | return TextManager_EventQueue->editingString; |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | Uint16 TextManager_GetCurrentChar(void) |
|---|
| 364 | { |
|---|
| 365 | if (TextManager_EventQueue == NULL) { |
|---|
| 366 | return 0x0000; |
|---|
| 367 | } |
|---|
| 368 | |
|---|
| 369 | return TextManager_EventQueue->character; |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | typedef struct dead_char_combination |
|---|
| 374 | { |
|---|
| 375 | TextManager_DeadChar deadChar; |
|---|
| 376 | Uint16 character; |
|---|
| 377 | Uint16 combinedChar; |
|---|
| 378 | } DEAD_CHAR_COMBINATION; |
|---|
| 379 | |
|---|
| 380 | |
|---|
| 381 | static DEAD_CHAR_COMBINATION DeadCharCombinationList[] = |
|---|
| 382 | { |
|---|
| 383 | /* ^ */ |
|---|
| 384 | {DEAD_CHAR_CIRCUMFLEX, 0x0041/* A */, 0x00C2}, |
|---|
| 385 | {DEAD_CHAR_CIRCUMFLEX, 0x0045/* E */, 0x00CA}, |
|---|
| 386 | {DEAD_CHAR_CIRCUMFLEX, 0x0049/* I */, 0x00CE}, |
|---|
| 387 | {DEAD_CHAR_CIRCUMFLEX, 0x004F/* O */, 0x00D4}, |
|---|
| 388 | {DEAD_CHAR_CIRCUMFLEX, 0x0055/* U */, 0x00DB}, |
|---|
| 389 | {DEAD_CHAR_CIRCUMFLEX, 0x0061/* a */, 0x00E2}, |
|---|
| 390 | {DEAD_CHAR_CIRCUMFLEX, 0x0065/* e */, 0x00EA}, |
|---|
| 391 | {DEAD_CHAR_CIRCUMFLEX, 0x0069/* i */, 0x00EE}, |
|---|
| 392 | {DEAD_CHAR_CIRCUMFLEX, 0x006F/* o */, 0x00F4}, |
|---|
| 393 | {DEAD_CHAR_CIRCUMFLEX, 0x0075/* u */, 0x00FB}, |
|---|
| 394 | /* �N */ |
|---|
| 395 | {DEAD_CHAR_DIAERESIS, 0x0041/* A */, 0x00C4}, |
|---|
| 396 | {DEAD_CHAR_DIAERESIS, 0x004F/* O */, 0x00D6}, |
|---|
| 397 | {DEAD_CHAR_DIAERESIS, 0x0055/* U */, 0x00DC}, |
|---|
| 398 | {DEAD_CHAR_DIAERESIS, 0x0061/* a */, 0x00E4}, |
|---|
| 399 | {DEAD_CHAR_DIAERESIS, 0x006F/* o */, 0x00F6}, |
|---|
| 400 | {DEAD_CHAR_DIAERESIS, 0x0075/* u */, 0x00FC}, |
|---|
| 401 | }; |
|---|
| 402 | |
|---|
| 403 | |
|---|
| 404 | static Uint16 TextManager_CombineDeadKey( |
|---|
| 405 | TextManager_DeadChar deadChar, Uint16 character) |
|---|
| 406 | { |
|---|
| 407 | int i; |
|---|
| 408 | int combinationListNum; |
|---|
| 409 | DEAD_CHAR_COMBINATION *combination; |
|---|
| 410 | |
|---|
| 411 | combinationListNum = |
|---|
| 412 | sizeof(DeadCharCombinationList) / sizeof(DEAD_CHAR_COMBINATION); |
|---|
| 413 | for (i = 0; i < combinationListNum; i++) { |
|---|
| 414 | combination = &DeadCharCombinationList[i]; |
|---|
| 415 | if ( |
|---|
| 416 | combination->deadChar == deadChar && |
|---|
| 417 | combination->character == character |
|---|
| 418 | ) { |
|---|
| 419 | return combination->combinedChar; |
|---|
| 420 | } |
|---|
| 421 | } |
|---|
| 422 | |
|---|
| 423 | return 0x0000; |
|---|
| 424 | } |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | int TextManager_GetCurrentCursorPosition(void) |
|---|
| 428 | { |
|---|
| 429 | if (TextManager_EventQueue == NULL) { |
|---|
| 430 | return 0; |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | return TextManager_EventQueue->cursorPosition; |
|---|
| 434 | } |
|---|
| 435 | |
|---|
| 436 | |
|---|
| 437 | int TextManager_GetCurrentCompositionPosition(void) |
|---|
| 438 | { |
|---|
| 439 | if (TextManager_EventQueue == NULL) { |
|---|
| 440 | return 0; |
|---|
| 441 | } |
|---|
| 442 | |
|---|
| 443 | return TextManager_EventQueue->compositionPosition; |
|---|
| 444 | } |
|---|
| 445 | |
|---|
| 446 | |
|---|
| 447 | int TextManager_GetCurrentCompositionLength(void) |
|---|
| 448 | { |
|---|
| 449 | if (TextManager_EventQueue == NULL) { |
|---|
| 450 | return 0; |
|---|
| 451 | } |
|---|
| 452 | |
|---|
| 453 | return TextManager_EventQueue->compositionLength; |
|---|
| 454 | } |
|---|
| 455 | |
|---|
| 456 | |
|---|
| 457 | Uint16 *TextManager_GetTextManagerName(void) |
|---|
| 458 | { |
|---|
| 459 | if (TextManager_Use != NULL) { |
|---|
| 460 | if (TextManager_Use->get_name != NULL) { |
|---|
| 461 | return TextManager_Use->get_name(); |
|---|
| 462 | } |
|---|
| 463 | } |
|---|
| 464 | return NULL; |
|---|
| 465 | } |
|---|
| 466 | |
|---|
| 467 | |
|---|
| 468 | static int TextManager_GetUnicodeStringLength(Uint16 *unicodeString) |
|---|
| 469 | { |
|---|
| 470 | int i; |
|---|
| 471 | |
|---|
| 472 | if (unicodeString == NULL) { |
|---|
| 473 | return 0; |
|---|
| 474 | } |
|---|
| 475 | |
|---|
| 476 | i = 0; |
|---|
| 477 | while (unicodeString[i]) { |
|---|
| 478 | i++; |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | return i; |
|---|
| 482 | } |
|---|
| 483 | |
|---|
| 484 | |
|---|
| 485 | static void TextManager_AddEventToQueue(TextManager_Event *event) |
|---|
| 486 | { |
|---|
| 487 | TextManager_Event *lastEvent; |
|---|
| 488 | |
|---|
| 489 | if (TextManager_EventQueue == NULL) { |
|---|
| 490 | TextManager_EventQueue = event; |
|---|
| 491 | } else { |
|---|
| 492 | lastEvent = TextManager_EventQueue; |
|---|
| 493 | while (lastEvent->next != NULL) { |
|---|
| 494 | lastEvent = lastEvent->next; |
|---|
| 495 | } |
|---|
| 496 | lastEvent->next = event; |
|---|
| 497 | } |
|---|
| 498 | PreviousDeadChar = DEAD_CHAR_NON; |
|---|
| 499 | TextManager_EventQueueNumber++; |
|---|
| 500 | } |
|---|
| 501 | |
|---|
| 502 | |
|---|
| 503 | TextManager_Result TextManager_PostIMChangeEvent( |
|---|
| 504 | Uint16 *editingString, |
|---|
| 505 | int cursorPosition, |
|---|
| 506 | int compositionPosition, |
|---|
| 507 | int compositionLength) |
|---|
| 508 | { |
|---|
| 509 | TextManager_Event *event; |
|---|
| 510 | int size; |
|---|
| 511 | |
|---|
| 512 | event = (TextManager_Event*)malloc(sizeof(TextManager_Event)); |
|---|
| 513 | if (event == NULL) { |
|---|
| 514 | SDL_SetError("Allocate memory failed"); |
|---|
| 515 | return TEXT_MANAGER_ERROR_ALLOCATED_MEMORY; |
|---|
| 516 | } |
|---|
| 517 | event->next = NULL; |
|---|
| 518 | |
|---|
| 519 | if (editingString == NULL) { |
|---|
| 520 | free(event); |
|---|
| 521 | SDL_SetError( |
|---|
| 522 | "post TEXT_MANAGER_MESSAGE_CHANGE: " |
|---|
| 523 | "Editing string is NULL"); |
|---|
| 524 | return TEXT_MANAGER_ERROR_NO_STRING; |
|---|
| 525 | } |
|---|
| 526 | size = |
|---|
| 527 | TextManager_GetUnicodeStringLength(editingString) * sizeof(Uint16) + |
|---|
| 528 | sizeof(Uint16); |
|---|
| 529 | event->editingString = (Uint16*)malloc(size); |
|---|
| 530 | if (event->editingString == NULL) { |
|---|
| 531 | free(event); |
|---|
| 532 | return TEXT_MANAGER_ERROR_ALLOCATED_MEMORY; |
|---|
| 533 | } |
|---|
| 534 | memcpy(event->editingString, editingString, size); |
|---|
| 535 | event->message = TEXT_MANAGER_MESSAGE_IM_CHANGE; |
|---|
| 536 | event->cursorPosition = cursorPosition; |
|---|
| 537 | event->compositionPosition = compositionPosition; |
|---|
| 538 | event->compositionLength = compositionLength; |
|---|
| 539 | event->character = 0x0000; |
|---|
| 540 | |
|---|
| 541 | TextManager_AddEventToQueue(event); |
|---|
| 542 | |
|---|
| 543 | return TEXT_MANAGER_SUCCESS; |
|---|
| 544 | } |
|---|
| 545 | |
|---|
| 546 | |
|---|
| 547 | TextManager_Result TextManager_PostIMResultEvent( |
|---|
| 548 | Uint16 *editingString) |
|---|
| 549 | { |
|---|
| 550 | TextManager_Event *event; |
|---|
| 551 | int size; |
|---|
| 552 | |
|---|
| 553 | event = (TextManager_Event*)malloc(sizeof(TextManager_Event)); |
|---|
| 554 | if (event == NULL) { |
|---|
| 555 | SDL_SetError("Allocate memory failed"); |
|---|
| 556 | return TEXT_MANAGER_ERROR_ALLOCATED_MEMORY; |
|---|
| 557 | } |
|---|
| 558 | event->next = NULL; |
|---|
| 559 | |
|---|
| 560 | if (editingString == NULL) { |
|---|
| 561 | free(event); |
|---|
| 562 | SDL_SetError( |
|---|
| 563 | "post TEXT_MANAGER_MESSAGE_RESULT: " |
|---|
| 564 | "Editing string is NULL"); |
|---|
| 565 | return TEXT_MANAGER_ERROR_NO_STRING; |
|---|
| 566 | } |
|---|
| 567 | size = |
|---|
| 568 | TextManager_GetUnicodeStringLength(editingString) * sizeof(Uint16) + |
|---|
| 569 | sizeof(Uint16); |
|---|
| 570 | event->editingString = (Uint16*)malloc(size); |
|---|
| 571 | if (event->editingString == NULL) { |
|---|
| 572 | free(event); |
|---|
| 573 | SDL_SetError("Allocate memory failed"); |
|---|
| 574 | return TEXT_MANAGER_ERROR_ALLOCATED_MEMORY; |
|---|
| 575 | } |
|---|
| 576 | memcpy(event->editingString, editingString, size); |
|---|
| 577 | event->message = TEXT_MANAGER_MESSAGE_IM_RESULT; |
|---|
| 578 | event->cursorPosition = 0; |
|---|
| 579 | event->compositionPosition = 0; |
|---|
| 580 | event->compositionLength = 0; |
|---|
| 581 | event->character = 0x0000; |
|---|
| 582 | |
|---|
| 583 | TextManager_AddEventToQueue(event); |
|---|
| 584 | |
|---|
| 585 | return TEXT_MANAGER_SUCCESS; |
|---|
| 586 | } |
|---|
| 587 | |
|---|
| 588 | |
|---|
| 589 | TextManager_Result TextManager_PostIMOnEvent(void) |
|---|
| 590 | { |
|---|
| 591 | TextManager_Event *event; |
|---|
| 592 | |
|---|
| 593 | event = (TextManager_Event*)malloc(sizeof(TextManager_Event)); |
|---|
| 594 | if (event == NULL) { |
|---|
| 595 | SDL_SetError("Allocate memory failed"); |
|---|
| 596 | return TEXT_MANAGER_ERROR_ALLOCATED_MEMORY; |
|---|
| 597 | } |
|---|
| 598 | event->next = NULL; |
|---|
| 599 | |
|---|
| 600 | event->message = TEXT_MANAGER_MESSAGE_IM_ON; |
|---|
| 601 | event->editingString = NULL; |
|---|
| 602 | event->cursorPosition = 0; |
|---|
| 603 | event->compositionPosition = 0; |
|---|
| 604 | event->compositionLength = 0; |
|---|
| 605 | event->character = 0x0000; |
|---|
| 606 | |
|---|
| 607 | TextManager_AddEventToQueue(event); |
|---|
| 608 | |
|---|
| 609 | return TEXT_MANAGER_SUCCESS; |
|---|
| 610 | } |
|---|
| 611 | |
|---|
| 612 | |
|---|
| 613 | TextManager_Result TextManager_PostIMOffEvent(void) |
|---|
| 614 | { |
|---|
| 615 | TextManager_Event *event; |
|---|
| 616 | |
|---|
| 617 | event = (TextManager_Event*)malloc(sizeof(TextManager_Event)); |
|---|
| 618 | if (event == NULL) { |
|---|
| 619 | SDL_SetError("Allocate memory failed"); |
|---|
| 620 | return TEXT_MANAGER_ERROR_ALLOCATED_MEMORY; |
|---|
| 621 | } |
|---|
| 622 | event->next = NULL; |
|---|
| 623 | |
|---|
| 624 | event->message = TEXT_MANAGER_MESSAGE_IM_OFF; |
|---|
| 625 | event->editingString = NULL; |
|---|
| 626 | event->cursorPosition = 0; |
|---|
| 627 | event->compositionPosition = 0; |
|---|
| 628 | event->compositionLength = 0; |
|---|
| 629 | event->character = 0x0000; |
|---|
| 630 | |
|---|
| 631 | TextManager_AddEventToQueue(event); |
|---|
| 632 | |
|---|
| 633 | return TEXT_MANAGER_SUCCESS; |
|---|
| 634 | } |
|---|
| 635 | |
|---|
| 636 | |
|---|
| 637 | TextManager_Result TextManager_PostCharEvent(Uint16 unicode) |
|---|
| 638 | { |
|---|
| 639 | TextManager_Event *event; |
|---|
| 640 | Uint16 deadCharCode; |
|---|
| 641 | Uint16 inputedChar; |
|---|
| 642 | |
|---|
| 643 | if (PreviousDeadChar != DEAD_CHAR_NON) { |
|---|
| 644 | inputedChar = TextManager_CombineDeadKey(PreviousDeadChar, unicode); |
|---|
| 645 | if (inputedChar == 0x0000) { |
|---|
| 646 | deadCharCode = TextManager_GetDeadCharCode(PreviousDeadChar); |
|---|
| 647 | if (deadCharCode != 0x0000) { |
|---|
| 648 | PreviousDeadChar = DEAD_CHAR_NON; |
|---|
| 649 | TextManager_PostCharEvent(deadCharCode); |
|---|
| 650 | } |
|---|
| 651 | inputedChar = unicode; |
|---|
| 652 | } |
|---|
| 653 | } else { |
|---|
| 654 | inputedChar = unicode; |
|---|
| 655 | } |
|---|
| 656 | |
|---|
| 657 | event = (TextManager_Event*)malloc(sizeof(TextManager_Event)); |
|---|
| 658 | if (event == NULL) { |
|---|
| 659 | SDL_SetError("Allocate memory failed"); |
|---|
| 660 | return TEXT_MANAGER_ERROR_ALLOCATED_MEMORY; |
|---|
| 661 | } |
|---|
| 662 | event->next = NULL; |
|---|
| 663 | |
|---|
| 664 | event->message = TEXT_MANAGER_MESSAGE_CHAR; |
|---|
| 665 | event->character = inputedChar; |
|---|
| 666 | event->editingString = NULL; |
|---|
| 667 | event->cursorPosition = 0; |
|---|
| 668 | event->compositionPosition = 0; |
|---|
| 669 | event->compositionLength = 0; |
|---|
| 670 | |
|---|
| 671 | TextManager_AddEventToQueue(event); |
|---|
| 672 | |
|---|
| 673 | return TEXT_MANAGER_SUCCESS; |
|---|
| 674 | } |
|---|
| 675 | |
|---|
| 676 | |
|---|
| 677 | typedef struct dead_char_code |
|---|
| 678 | { |
|---|
| 679 | TextManager_DeadChar deadChar; |
|---|
| 680 | Uint16 deadCharCode; |
|---|
| 681 | } DEAD_CHAR_CODE; |
|---|
| 682 | |
|---|
| 683 | |
|---|
| 684 | DEAD_CHAR_CODE DeadKeyConversionTable[] = |
|---|
| 685 | { |
|---|
| 686 | {DEAD_CHAR_GRAVE, 0x0060}, |
|---|
| 687 | {DEAD_CHAR_ACUTE, 0x00B4}, |
|---|
| 688 | {DEAD_CHAR_CIRCUMFLEX, 0x005E}, |
|---|
| 689 | {DEAD_CHAR_TILDE, 0x007E}, |
|---|
| 690 | // {DEAD_CHAR_MACRON,}, |
|---|
| 691 | // {DEAD_CHAR_BREVE,}, |
|---|
| 692 | // {DEAD_CHAR_ABOVE_DOT,}, |
|---|
| 693 | {DEAD_CHAR_DIAERESIS, 0x00A8}, |
|---|
| 694 | {DEAD_CHAR_ABOVE_RING, 0x00B0}, |
|---|
| 695 | // {DEAD_CHAR_DOUBLE_ACUTE,}, |
|---|
| 696 | // {DEAD_CHAR_CARON,}, |
|---|
| 697 | // {DEAD_CHAR_CEDILLA,}, |
|---|
| 698 | // {DEAD_CHAR_OGONEK,}, |
|---|
| 699 | // {DEAD_CHAR_IOTA,}, |
|---|
| 700 | // {DEAD_CHAR_VOICED_SOUND,}, |
|---|
| 701 | // {DEAD_CHAR_SEMIVOICED_SOUND,}, |
|---|
| 702 | // {DEAD_CHAR_BELOW_DOT,}, |
|---|
| 703 | // {DEAD_CHAR_HOOK,}, |
|---|
| 704 | // {DEAD_CHAR_HORN,}, |
|---|
| 705 | }; |
|---|
| 706 | |
|---|
| 707 | |
|---|
| 708 | static Uint16 TextManager_GetDeadCharCode(TextManager_DeadChar deadChar) |
|---|
| 709 | { |
|---|
| 710 | int i; |
|---|
| 711 | int tableNum; |
|---|
| 712 | DEAD_CHAR_CODE *deadCharCode; |
|---|
| 713 | |
|---|
| 714 | tableNum = sizeof(DeadKeyConversionTable) / sizeof(DEAD_CHAR_CODE); |
|---|
| 715 | for (i = 0; i < tableNum; i++) { |
|---|
| 716 | deadCharCode = &DeadKeyConversionTable[i]; |
|---|
| 717 | if (deadCharCode->deadChar == deadChar) { |
|---|
| 718 | return deadCharCode->deadCharCode; |
|---|
| 719 | } |
|---|
| 720 | } |
|---|
| 721 | return 0x0000; |
|---|
| 722 | } |
|---|
| 723 | |
|---|
| 724 | |
|---|
| 725 | TextManager_Result TextManager_SetDeadKey( |
|---|
| 726 | TextManager_DeadChar deadCharacter) |
|---|
| 727 | { |
|---|
| 728 | Uint16 deadCharCode; |
|---|
| 729 | TextManager_DeadChar deadChar; |
|---|
| 730 | |
|---|
| 731 | if (PreviousDeadChar != DEAD_CHAR_NON) { |
|---|
| 732 | deadChar = PreviousDeadChar; |
|---|
| 733 | PreviousDeadChar = DEAD_CHAR_NON; |
|---|
| 734 | deadCharCode = TextManager_GetDeadCharCode(deadChar); |
|---|
| 735 | if (deadCharCode != 0x0000) { |
|---|
| 736 | TextManager_PostCharEvent(deadCharCode); |
|---|
| 737 | } |
|---|
| 738 | deadCharCode = TextManager_GetDeadCharCode(deadCharacter); |
|---|
| 739 | if (deadCharCode != 0x0000) { |
|---|
| 740 | TextManager_PostCharEvent(deadCharCode); |
|---|
| 741 | } |
|---|
| 742 | } else { |
|---|
| 743 | PreviousDeadChar = deadCharacter; |
|---|
| 744 | } |
|---|
| 745 | |
|---|
| 746 | return TEXT_MANAGER_SUCCESS; |
|---|
| 747 | } |
|---|
| 748 | |
|---|
| 749 | |
|---|
| 750 | TextManager_Result TextManager_InitFromOther( |
|---|
| 751 | SDL_TextManager textManager) |
|---|
| 752 | { |
|---|
| 753 | TextManager_Result result; |
|---|
| 754 | |
|---|
| 755 | if (IsInited == SDL_TRUE) { |
|---|
| 756 | SDL_SetError("Input method is already inited"); |
|---|
| 757 | return TEXT_MANAGER_ERROR_ALREADY_INIT; |
|---|
| 758 | } |
|---|
| 759 | |
|---|
| 760 | if (textManager.available() == SDL_FALSE) { |
|---|
| 761 | SDL_SetError("Bootstrap is invalid"); |
|---|
| 762 | return TEXT_MANAGER_ERROR_INVALID_BOOTSTRAP; |
|---|
| 763 | } else { |
|---|
| 764 | TextManager_SetEventFilter(); |
|---|
| 765 | result = textManager.init(); |
|---|
| 766 | if (result != TEXT_MANAGER_SUCCESS) { |
|---|
| 767 | TextManager_RestoreEventFilter(); |
|---|
| 768 | return result; |
|---|
| 769 | } |
|---|
| 770 | } |
|---|
| 771 | |
|---|
| 772 | TextManager_Use = (SDL_TextManager*)malloc(sizeof(SDL_TextManager)); |
|---|
| 773 | if (TextManager_Use == NULL) { |
|---|
| 774 | textManager.quit(); |
|---|
| 775 | SDL_SetError("Allocate memory failed"); |
|---|
| 776 | return TEXT_MANAGER_ERROR_ALLOCATED_MEMORY; |
|---|
| 777 | } |
|---|
| 778 | |
|---|
| 779 | memcpy(TextManager_Use, &textManager, sizeof(SDL_TextManager)); |
|---|
| 780 | |
|---|
| 781 | TextManager_EventQueue = NULL; |
|---|
| 782 | TextManager_EventQueueNumber = 0; |
|---|
| 783 | IsOriginalTextManager = SDL_TRUE; |
|---|
| 784 | IsValid = SDL_TRUE; |
|---|
| 785 | IsInited = SDL_TRUE; |
|---|
| 786 | |
|---|
| 787 | return TEXT_MANAGER_SUCCESS; |
|---|
| 788 | } |
|---|
| 789 | |
|---|
| 790 | |
|---|
| 791 | static int TextManager_SDLEventFilter(const SDL_Event *e) |
|---|
| 792 | { |
|---|
| 793 | /* |
|---|
| 794 | Uint8 *keys; |
|---|
| 795 | SDL_keysym keysym; |
|---|
| 796 | char ch; |
|---|
| 797 | |
|---|
| 798 | if (IsValid == SDL_TRUE) { |
|---|
| 799 | keys = SDL_GetKeyState(NULL); |
|---|
| 800 | keys[SDLK_UNKNOWN] = SDL_RELEASED; |
|---|
| 801 | |
|---|
| 802 | switch (e->type) { |
|---|
| 803 | case SDL_KEYDOWN: |
|---|
| 804 | keysym = ((SDL_KeyboardEvent*)e)->keysym; |
|---|
| 805 | if ( (keysym.unicode & 0xFF80) == 0 ) { |
|---|
| 806 | ch = keysym.unicode & 0x007F; |
|---|
| 807 | if (isprint(ch)) { |
|---|
| 808 | TextManager_PostCharEvent(keysym.unicode); |
|---|
| 809 | keys[keysym.sym] = SDL_RELEASED; |
|---|
| 810 | return 0; |
|---|
| 811 | } |
|---|
| 812 | } |
|---|
| 813 | break; |
|---|
| 814 | default: |
|---|
| 815 | break; |
|---|
| 816 | } |
|---|
| 817 | } |
|---|
| 818 | */ |
|---|
| 819 | |
|---|
| 820 | if (OriginalFilterSDL != NULL) { |
|---|
| 821 | return (*OriginalFilterSDL)(e); |
|---|
| 822 | } |
|---|
| 823 | return 1; |
|---|
| 824 | } |
|---|