Atlas - testime.c
Home / ext / SDL / test Lines: 7 | Size: 42286 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Copyright (C) 1997-2026 Sam Lantinga <[email protected]> 3 4 This software is provided 'as-is', without any express or implied 5 warranty. In no event will the authors be held liable for any damages 6 arising from the use of this software. 7 8 Permission is granted to anyone to use this software for any purpose, 9 including commercial applications, and to alter it and redistribute it 10 freely. 11*/ 12 13/* A simple program to test the Input Method support in SDL. 14 * 15 * This uses the GNU Unifont to display non-ASCII characters, available at: 16 * http://unifoundry.com/unifont.html 17 * 18 * An example of IME support with TrueType fonts is available in the SDL_ttf example code: 19 * https://github.com/libsdl-org/SDL_ttf/blob/main/examples/editbox.h 20 */ 21#include <SDL3/SDL.h> 22#include <SDL3/SDL_main.h> 23#include <SDL3/SDL_test_font.h> 24 25#include <SDL3/SDL_test_common.h> 26#include "testutils.h" 27 28#ifdef SDL_PLATFORM_DOS 29#define DEFAULT_FONT "UNIFONT.HEX" 30#else 31#define DEFAULT_FONT "unifont-15.1.05.hex" 32#endif 33#define MAX_TEXT_LENGTH 256 34 35#define WINDOW_WIDTH 640 36#define WINDOW_HEIGHT 480 37 38#define MARGIN 32.0f 39#define LINE_HEIGHT (FONT_CHARACTER_SIZE + 4.0f) 40#define CURSOR_BLINK_INTERVAL_MS 500 41 42typedef struct 43{ 44 SDL_Window *window; 45 SDL_Renderer *renderer; 46 int rendererID; 47 bool settings_visible; 48 SDL_Texture *settings_icon; 49 SDL_FRect settings_rect; 50 SDL_PropertiesID text_settings; 51 SDL_FRect textRect; 52 SDL_FRect markedRect; 53 char text[MAX_TEXT_LENGTH]; 54 char markedText[MAX_TEXT_LENGTH]; 55 int cursor; 56 int cursor_length; 57 bool cursor_visible; 58 Uint64 last_cursor_change; 59 char **candidates; 60 int num_candidates; 61 int selected_candidate; 62 bool horizontal_candidates; 63} WindowState; 64 65static SDLTest_CommonState *state; 66static WindowState *windowstate; 67static const SDL_Color lineColor = { 0, 0, 0, 255 }; 68static const SDL_Color backColor = { 255, 255, 255, 255 }; 69static const SDL_Color textColor = { 0, 0, 0, 255 }; 70static SDL_BlendMode highlight_mode; 71 72static const struct 73{ 74 const char *label; 75 const char *setting; 76 int value; 77} settings[] = { 78 { "Text", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT }, 79 { "Name", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT_NAME }, 80 { "E-mail", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT_EMAIL }, 81 { "Username", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT_USERNAME }, 82 { "Password (hidden)", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT_PASSWORD_HIDDEN }, 83 { "Password (visible)", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT_PASSWORD_VISIBLE }, 84 { "Number", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_NUMBER }, 85 { "Numeric PIN (hidden)", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_NUMBER_PASSWORD_HIDDEN }, 86 { "Numeric PIN (visible)", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_NUMBER_PASSWORD_VISIBLE }, 87 { "", NULL }, 88 { "No capitalization", SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER, SDL_CAPITALIZE_NONE }, 89 { "Capitalize sentences", SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER, SDL_CAPITALIZE_SENTENCES }, 90 { "Capitalize words", SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER, SDL_CAPITALIZE_WORDS }, 91 { "All caps", SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER, SDL_CAPITALIZE_LETTERS }, 92 { "", NULL }, 93 { "Auto-correct OFF", SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN, false }, 94 { "Auto-correct ON", SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN, true }, 95 { "Multiline OFF", SDL_PROP_TEXTINPUT_MULTILINE_BOOLEAN, false }, 96 { "Multiline ON", SDL_PROP_TEXTINPUT_MULTILINE_BOOLEAN, true } 97}; 98 99#define UNIFONT_MAX_CODEPOINT 0x1ffff 100#define UNIFONT_NUM_GLYPHS 0x20000 101#define UNIFONT_REPLACEMENT 0xFFFD 102/* Using 512x512 textures that are supported everywhere. */ 103#define UNIFONT_TEXTURE_WIDTH 512 104#define UNIFONT_GLYPH_SIZE 16 105#define UNIFONT_GLYPH_BORDER 1 106#define UNIFONT_GLYPH_AREA (UNIFONT_GLYPH_BORDER + UNIFONT_GLYPH_SIZE + UNIFONT_GLYPH_BORDER) 107#define UNIFONT_GLYPHS_IN_ROW (UNIFONT_TEXTURE_WIDTH / UNIFONT_GLYPH_AREA) 108#define UNIFONT_GLYPHS_IN_TEXTURE (UNIFONT_GLYPHS_IN_ROW * UNIFONT_GLYPHS_IN_ROW) 109#define UNIFONT_NUM_TEXTURES ((UNIFONT_NUM_GLYPHS + UNIFONT_GLYPHS_IN_TEXTURE - 1) / UNIFONT_GLYPHS_IN_TEXTURE) 110#define UNIFONT_TEXTURE_SIZE (UNIFONT_TEXTURE_WIDTH * UNIFONT_TEXTURE_WIDTH * 4) 111#define UNIFONT_TEXTURE_PITCH (UNIFONT_TEXTURE_WIDTH * 4) 112#define UNIFONT_DRAW_SCALE 2.0f 113static struct UnifontGlyph 114{ 115 Uint8 width; 116 Uint8 data[32]; 117} * unifontGlyph; 118static SDL_Texture **unifontTexture; 119static Uint8 unifontTextureLoaded[UNIFONT_NUM_TEXTURES] = { 0 }; 120 121/* Unifont loading code start */ 122 123static Uint8 dehex(char c) 124{ 125 if (c >= '0' && c <= '9') { 126 return c - '0'; 127 } else if (c >= 'a' && c <= 'f') { 128 return c - 'a' + 10; 129 } else if (c >= 'A' && c <= 'F') { 130 return c - 'A' + 10; 131 } 132 return 255; 133} 134 135static Uint8 dehex2(char c1, char c2) 136{ 137 return (dehex(c1) << 4) | dehex(c2); 138} 139 140static Uint8 validate_hex(const char *cp, size_t len, Uint32 *np) 141{ 142 Uint32 n = 0; 143 for (; len > 0; cp++, len--) { 144 Uint8 c = dehex(*cp); 145 if (c == 255) { 146 return 0; 147 } 148 n = (n << 4) | c; 149 } 150 if (np) { 151 *np = n; 152 } 153 return 1; 154} 155 156static int unifont_init(const char *fontname) 157{ 158 Uint8 hexBuffer[65]; 159 Uint32 numGlyphs = 0; 160 int lineNumber = 1; 161 size_t bytesRead; 162 SDL_IOStream *hexFile; 163 const size_t unifontGlyphSize = UNIFONT_NUM_GLYPHS * sizeof(struct UnifontGlyph); 164 const size_t unifontTextureSize = UNIFONT_NUM_TEXTURES * state->num_windows * sizeof(void *); 165 char *filename; 166 167 /* Allocate memory for the glyph data so the file can be closed after initialization. */ 168 unifontGlyph = (struct UnifontGlyph *)SDL_malloc(unifontGlyphSize); 169 if (!unifontGlyph) { 170 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d KiB for glyph data.", (int)(unifontGlyphSize + 1023) / 1024); 171 return -1; 172 } 173 SDL_memset(unifontGlyph, 0, unifontGlyphSize); 174 175 /* Allocate memory for texture pointers for all renderers. */ 176 unifontTexture = (SDL_Texture **)SDL_malloc(unifontTextureSize); 177 if (!unifontTexture) { 178 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d KiB for texture pointer data.", (int)(unifontTextureSize + 1023) / 1024); 179 return -1; 180 } 181 SDL_memset(unifontTexture, 0, unifontTextureSize); 182 183 filename = GetResourceFilename(NULL, fontname); 184 if (!filename) { 185 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory"); 186 return -1; 187 } 188 hexFile = SDL_IOFromFile(filename, "rb"); 189 SDL_free(filename); 190 if (!hexFile) { 191 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to open font file: %s", fontname); 192 return -1; 193 } 194 195 /* Read all the glyph data into memory to make it accessible later when textures are created. */ 196 do { 197 int i, codepointHexSize; 198 size_t bytesOverread; 199 Uint8 glyphWidth; 200 Uint32 codepoint; 201 202 bytesRead = SDL_ReadIO(hexFile, hexBuffer, 9); 203 if (numGlyphs > 0 && bytesRead == 0) { 204 break; /* EOF */ 205 } 206 if ((numGlyphs == 0 && bytesRead == 0) || (numGlyphs > 0 && bytesRead < 9)) { 207 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file."); 208 return -1; 209 } 210 211 /* Looking for the colon that separates the codepoint and glyph data at position 2, 4, 6 and 8. */ 212 if (hexBuffer[2] == ':') { 213 codepointHexSize = 2; 214 } else if (hexBuffer[4] == ':') { 215 codepointHexSize = 4; 216 } else if (hexBuffer[6] == ':') { 217 codepointHexSize = 6; 218 } else if (hexBuffer[8] == ':') { 219 codepointHexSize = 8; 220 } else { 221 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Could not find codepoint and glyph data separator symbol in hex file on line %d.", lineNumber); 222 return -1; 223 } 224 225 if (!validate_hex((const char *)hexBuffer, codepointHexSize, &codepoint)) { 226 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Malformed hexadecimal number in hex file on line %d.", lineNumber); 227 return -1; 228 } 229 if (codepoint > UNIFONT_MAX_CODEPOINT) { 230 SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "unifont: Codepoint on line %d exceeded limit of 0x%x.", lineNumber, UNIFONT_MAX_CODEPOINT); 231 } 232 233 /* If there was glyph data read in the last file read, move it to the front of the buffer. */ 234 bytesOverread = 8 - codepointHexSize; 235 if (codepointHexSize < 8) { 236 SDL_memmove(hexBuffer, hexBuffer + codepointHexSize + 1, bytesOverread); 237 } 238 bytesRead = SDL_ReadIO(hexFile, hexBuffer + bytesOverread, 33 - bytesOverread); 239 240 if (bytesRead < (33 - bytesOverread)) { 241 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file."); 242 return -1; 243 } 244 if (hexBuffer[32] == '\n') { 245 glyphWidth = 8; 246 } else { 247 glyphWidth = 16; 248 bytesRead = SDL_ReadIO(hexFile, hexBuffer + 33, 32); 249 if (bytesRead < 32) { 250 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file."); 251 return -1; 252 } 253 } 254 255 if (!validate_hex((const char *)hexBuffer, glyphWidth * 4, NULL)) { 256 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Malformed hexadecimal glyph data in hex file on line %d.", lineNumber); 257 return -1; 258 } 259 260 if (codepoint <= UNIFONT_MAX_CODEPOINT) { 261 if (unifontGlyph[codepoint].width > 0) { 262 SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "unifont: Ignoring duplicate codepoint 0x%08" SDL_PRIx32 " in hex file on line %d.", codepoint, lineNumber); 263 } else { 264 unifontGlyph[codepoint].width = glyphWidth; 265 /* Pack the hex data into a more compact form. */ 266 for (i = 0; i < glyphWidth * 2; i++) { 267 unifontGlyph[codepoint].data[i] = dehex2(hexBuffer[i * 2], hexBuffer[i * 2 + 1]); 268 } 269 numGlyphs++; 270 } 271 } 272 273 lineNumber++; 274 } while (bytesRead > 0); 275 276 SDL_CloseIO(hexFile); 277 SDL_Log("unifont: Loaded %" SDL_PRIu32 " glyphs.", numGlyphs); 278 return 0; 279} 280 281static void unifont_make_rgba(const Uint8 *src, Uint8 *dst, Uint8 width) 282{ 283 int i, j; 284 Uint8 *row = dst; 285 286 for (i = 0; i < width * 2; i++) { 287 Uint8 data = src[i]; 288 for (j = 0; j < 8; j++) { 289 if (data & 0x80) { 290 row[0] = textColor.r; 291 row[1] = textColor.g; 292 row[2] = textColor.b; 293 row[3] = textColor.a; 294 } else { 295 row[0] = 0; 296 row[1] = 0; 297 row[2] = 0; 298 row[3] = 0; 299 } 300 data <<= 1; 301 row += 4; 302 } 303 304 if (width == 8 || (width == 16 && i % 2 == 1)) { 305 dst += UNIFONT_TEXTURE_PITCH; 306 row = dst; 307 } 308 } 309} 310 311static int unifont_load_texture(Uint32 textureID) 312{ 313 int i; 314 Uint8 *textureRGBA; 315 316 if (textureID >= UNIFONT_NUM_TEXTURES) { 317 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Tried to load out of range texture %" SDL_PRIu32, textureID); 318 return -1; 319 } 320 321 textureRGBA = (Uint8 *)SDL_malloc(UNIFONT_TEXTURE_SIZE); 322 if (!textureRGBA) { 323 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d MiB for a texture.", UNIFONT_TEXTURE_SIZE / 1024 / 1024); 324 return -1; 325 } 326 SDL_memset(textureRGBA, 0, UNIFONT_TEXTURE_SIZE); 327 328 /* Copy the glyphs into memory in RGBA format. */ 329 for (i = 0; i < UNIFONT_GLYPHS_IN_TEXTURE; i++) { 330 Uint32 codepoint = UNIFONT_GLYPHS_IN_TEXTURE * textureID + i; 331 if (unifontGlyph[codepoint].width > 0) { 332 const Uint32 cInTex = codepoint % UNIFONT_GLYPHS_IN_TEXTURE; 333 const size_t offset = ((size_t)cInTex / UNIFONT_GLYPHS_IN_ROW) * UNIFONT_TEXTURE_PITCH * UNIFONT_GLYPH_AREA + (cInTex % UNIFONT_GLYPHS_IN_ROW) * UNIFONT_GLYPH_AREA * 4; 334 unifont_make_rgba(unifontGlyph[codepoint].data, textureRGBA + offset, unifontGlyph[codepoint].width); 335 } 336 } 337 338 /* Create textures and upload the RGBA data from above. */ 339 for (i = 0; i < state->num_windows; ++i) { 340 SDL_Renderer *renderer = state->renderers[i]; 341 SDL_Texture *tex = unifontTexture[UNIFONT_NUM_TEXTURES * i + textureID]; 342 if (state->windows[i] == NULL || renderer == NULL || tex != NULL) { 343 continue; 344 } 345 tex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, UNIFONT_TEXTURE_WIDTH, UNIFONT_TEXTURE_WIDTH); 346 if (tex == NULL) { 347 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to create texture %" SDL_PRIu32 " for renderer %d.", textureID, i); 348 return -1; 349 } 350 unifontTexture[UNIFONT_NUM_TEXTURES * i + textureID] = tex; 351 SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND); 352 if (!SDL_UpdateTexture(tex, NULL, textureRGBA, UNIFONT_TEXTURE_PITCH)) { 353 SDL_Log("unifont error: Failed to update texture %" SDL_PRIu32 " data for renderer %d.", textureID, i); 354 } 355 } 356 357 SDL_free(textureRGBA); 358 unifontTextureLoaded[textureID] = 1; 359 return -1; 360} 361 362static int unifont_glyph_width(Uint32 codepoint) 363{ 364 if (codepoint > UNIFONT_MAX_CODEPOINT || 365 unifontGlyph[codepoint].width == 0) { 366 codepoint = UNIFONT_REPLACEMENT; 367 } 368 return unifontGlyph[codepoint].width; 369} 370 371static int unifont_draw_glyph(Uint32 codepoint, int rendererID, SDL_FRect *dst) 372{ 373 SDL_Texture *texture; 374 Uint32 textureID; 375 SDL_FRect srcrect; 376 srcrect.w = srcrect.h = (float)UNIFONT_GLYPH_SIZE; 377 378 if (codepoint > UNIFONT_MAX_CODEPOINT || 379 unifontGlyph[codepoint].width == 0) { 380 codepoint = UNIFONT_REPLACEMENT; 381 } 382 383 textureID = codepoint / UNIFONT_GLYPHS_IN_TEXTURE; 384 if (!unifontTextureLoaded[textureID]) { 385 if (unifont_load_texture(textureID) < 0) { 386 return 0; 387 } 388 } 389 texture = unifontTexture[UNIFONT_NUM_TEXTURES * rendererID + textureID]; 390 if (texture) { 391 const Uint32 cInTex = codepoint % UNIFONT_GLYPHS_IN_TEXTURE; 392 srcrect.x = (float)(cInTex % UNIFONT_GLYPHS_IN_ROW * UNIFONT_GLYPH_AREA); 393 srcrect.y = (float)(cInTex / UNIFONT_GLYPHS_IN_ROW * UNIFONT_GLYPH_AREA); 394 SDL_RenderTexture(state->renderers[rendererID], texture, &srcrect, dst); 395 } 396 return unifontGlyph[codepoint].width; 397} 398 399static void unifont_cleanup(void) 400{ 401 int i, j; 402 for (i = 0; i < state->num_windows; ++i) { 403 SDL_Renderer *renderer = state->renderers[i]; 404 if (state->windows[i] == NULL || !renderer) { 405 continue; 406 } 407 for (j = 0; j < UNIFONT_NUM_TEXTURES; j++) { 408 SDL_Texture *tex = unifontTexture[UNIFONT_NUM_TEXTURES * i + j]; 409 if (tex) { 410 SDL_DestroyTexture(tex); 411 } 412 } 413 } 414 415 for (j = 0; j < UNIFONT_NUM_TEXTURES; j++) { 416 unifontTextureLoaded[j] = 0; 417 } 418 419 SDL_free(unifontTexture); 420 SDL_free(unifontGlyph); 421} 422 423/* Unifont code end */ 424 425static size_t utf8_length(unsigned char c) 426{ 427 c = (unsigned char)(0xff & c); 428 if (c < 0x80) { 429 return 1; 430 } else if ((c >> 5) == 0x6) { 431 return 2; 432 } else if ((c >> 4) == 0xe) { 433 return 3; 434 } else if ((c >> 3) == 0x1e) { 435 return 4; 436 } 437 return 0; 438} 439 440static Uint32 utf8_decode(const char *p, size_t len) 441{ 442 Uint32 codepoint = 0; 443 size_t i = 0; 444 if (!len) { 445 return 0; 446 } 447 448 for (; i < len; ++i) { 449 if (i == 0) { 450 codepoint = (0xff >> len) & *p; 451 } else { 452 codepoint <<= 6; 453 codepoint |= 0x3f & *p; 454 } 455 if (!*p) { 456 return 0; 457 } 458 p++; 459 } 460 461 return codepoint; 462} 463 464static WindowState *GetWindowStateForWindowID(SDL_WindowID windowID) 465{ 466 int i; 467 SDL_Window *window = SDL_GetWindowFromID(windowID); 468 469 for (i = 0; i < state->num_windows; ++i) { 470 if (windowstate[i].window == window) { 471 return &windowstate[i]; 472 } 473 } 474 return NULL; 475} 476 477static void InitInput(WindowState *ctx) 478{ 479 /* Prepare a rect for text input */ 480 ctx->textRect.x = 100.0f; 481 ctx->textRect.y = 250.0f; 482 ctx->textRect.w = DEFAULT_WINDOW_WIDTH - 2 * ctx->textRect.x; 483 ctx->textRect.h = 50.0f; 484 ctx->markedRect = ctx->textRect; 485 486 ctx->text_settings = SDL_CreateProperties(); 487 488 SDL_StartTextInputWithProperties(ctx->window, ctx->text_settings); 489} 490 491 492static void ClearCandidates(WindowState *ctx) 493{ 494 int i; 495 496 for (i = 0; i < ctx->num_candidates; ++i) { 497 SDL_free(ctx->candidates[i]); 498 } 499 SDL_free(ctx->candidates); 500 ctx->candidates = NULL; 501 ctx->num_candidates = 0; 502} 503 504static void SaveCandidates(WindowState *ctx, SDL_Event *event) 505{ 506 int i; 507 508 ClearCandidates(ctx); 509 510 ctx->num_candidates = event->edit_candidates.num_candidates; 511 if (ctx->num_candidates > 0) { 512 ctx->candidates = (char **)SDL_malloc(ctx->num_candidates * sizeof(*ctx->candidates)); 513 if (!ctx->candidates) { 514 ctx->num_candidates = 0; 515 return; 516 } 517 for (i = 0; i < ctx->num_candidates; ++i) { 518 ctx->candidates[i] = SDL_strdup(event->edit_candidates.candidates[i]); 519 } 520 ctx->selected_candidate = event->edit_candidates.selected_candidate; 521 ctx->horizontal_candidates = event->edit_candidates.horizontal; 522 } 523} 524 525static void DrawCandidates(WindowState *ctx, SDL_FRect *cursorRect) 526{ 527 SDL_Renderer *renderer = ctx->renderer; 528 int rendererID = ctx->rendererID; 529 int i; 530 int output_w = 0, output_h = 0; 531 float w = 0.0f, h = 0.0f; 532 SDL_FRect candidatesRect, dstRect, underlineRect; 533 534 if (ctx->num_candidates == 0) { 535 return; 536 } 537 538 /* Calculate the size of the candidate list */ 539 for (i = 0; i < ctx->num_candidates; ++i) { 540 if (!ctx->candidates[i]) { 541 continue; 542 } 543 544 if (ctx->horizontal_candidates) { 545 const char *utext = ctx->candidates[i]; 546 Uint32 codepoint; 547 size_t len; 548 float advance = 0.0f; 549 550 if (i > 0) { 551 advance += unifont_glyph_width(' ') * UNIFONT_DRAW_SCALE; 552 } 553 while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) { 554 advance += unifont_glyph_width(codepoint) * UNIFONT_DRAW_SCALE; 555 utext += len; 556 } 557 w += advance; 558 h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE; 559 } else { 560 const char *utext = ctx->candidates[i]; 561 Uint32 codepoint; 562 size_t len; 563 float advance = 0.0f; 564 565 while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) { 566 advance += unifont_glyph_width(codepoint) * UNIFONT_DRAW_SCALE; 567 utext += len; 568 } 569 w = SDL_max(w, advance); 570 if (i > 0) { 571 h += 2.0f; 572 } 573 h += UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE; 574 } 575 } 576 577 /* Position the candidate window */ 578 SDL_GetCurrentRenderOutputSize(renderer, &output_w, &output_h); 579 candidatesRect.x = cursorRect->x; 580 candidatesRect.y = cursorRect->y + cursorRect->h + 2.0f; 581 candidatesRect.w = 1.0f + 2.0f + w + 2.0f + 1.0f; 582 candidatesRect.h = 1.0f + 2.0f + h + 2.0f + 1.0f; 583 if ((candidatesRect.x + candidatesRect.w) > output_w) { 584 candidatesRect.x = (output_w - candidatesRect.w); 585 if (candidatesRect.x < 0.0f) { 586 candidatesRect.x = 0.0f; 587 } 588 } 589 590 /* Draw the candidate background */ 591 SDL_SetRenderDrawColor(renderer, 0xAA, 0xAA, 0xAA, 0xFF); 592 SDL_RenderFillRect(renderer, &candidatesRect); 593 SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF); 594 SDL_RenderRect(renderer, &candidatesRect); 595 596 /* Draw the candidates */ 597 dstRect.x = candidatesRect.x + 3.0f; 598 dstRect.y = candidatesRect.y + 3.0f; 599 for (i = 0; i < ctx->num_candidates; ++i) { 600 if (!ctx->candidates[i]) { 601 continue; 602 } 603 604 dstRect.w = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE; 605 dstRect.h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE; 606 607 if (ctx->horizontal_candidates) { 608 const char *utext = ctx->candidates[i]; 609 Uint32 codepoint; 610 size_t len; 611 float start; 612 613 if (i > 0) { 614 dstRect.x += unifont_draw_glyph(' ', rendererID, &dstRect) * UNIFONT_DRAW_SCALE; 615 } 616 617 start = dstRect.x + 2 * unifont_glyph_width(' ') * UNIFONT_DRAW_SCALE; 618 while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) { 619 dstRect.x += unifont_draw_glyph(codepoint, rendererID, &dstRect) * UNIFONT_DRAW_SCALE; 620 utext += len; 621 } 622 623 if (i == ctx->selected_candidate) { 624 underlineRect.x = start; 625 underlineRect.y = dstRect.y + dstRect.h - 2; 626 underlineRect.h = 2; 627 underlineRect.w = dstRect.x - start; 628 629 SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a); 630 SDL_RenderFillRect(renderer, &underlineRect); 631 } 632 } else { 633 const char *utext = ctx->candidates[i]; 634 Uint32 codepoint; 635 size_t len; 636 float start; 637 638 dstRect.x = candidatesRect.x + 3.0f; 639 640 start = dstRect.x + 2 * unifont_glyph_width(' ') * UNIFONT_DRAW_SCALE; 641 while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) { 642 dstRect.x += unifont_draw_glyph(codepoint, rendererID, &dstRect) * UNIFONT_DRAW_SCALE; 643 utext += len; 644 } 645 646 if (i == ctx->selected_candidate) { 647 underlineRect.x = start; 648 underlineRect.y = dstRect.y + dstRect.h - 2; 649 underlineRect.h = 2; 650 underlineRect.w = dstRect.x - start; 651 652 SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a); 653 SDL_RenderFillRect(renderer, &underlineRect); 654 } 655 656 if (i > 0) { 657 dstRect.y += 2.0f; 658 } 659 dstRect.y += dstRect.h; 660 } 661 } 662} 663 664static void UpdateTextInputArea(WindowState *ctx, const SDL_FRect *cursorRect) 665{ 666 SDL_Rect rect; 667 float x1, y1, x2, y2; 668 669 /* Convert render coordinates to window coordinates for SDL_SetTextInputArea */ 670 SDL_RenderCoordinatesToWindow(ctx->renderer, ctx->textRect.x, ctx->textRect.y, &x1, &y1); 671 SDL_RenderCoordinatesToWindow(ctx->renderer, ctx->textRect.x + ctx->textRect.w, ctx->textRect.y + ctx->textRect.h, &x2, &y2); 672 673 rect.x = (int)x1; 674 rect.y = (int)y1; 675 rect.w = (int)(x2 - x1); 676 rect.h = (int)(y2 - y1); 677 678 /* cursor_offset also needs to be in window coordinates */ 679 float cursor_x_render = cursorRect->x; 680 float cursor_x_window, dummy; 681 SDL_RenderCoordinatesToWindow(ctx->renderer, cursor_x_render, 0, &cursor_x_window, &dummy); 682 int cursor_offset = (int)(cursor_x_window - x1); 683 684 SDL_SetTextInputArea(ctx->window, &rect, cursor_offset); 685} 686 687static void CleanupVideo(void) 688{ 689 int i; 690 691 for (i = 0; i < state->num_windows; ++i) { 692 WindowState *ctx = &windowstate[i]; 693 694 SDL_StopTextInput(ctx->window); 695 ClearCandidates(ctx); 696 SDL_DestroyProperties(ctx->text_settings); 697 } 698 unifont_cleanup(); 699} 700 701static void DrawSettingsButton(WindowState *ctx) 702{ 703 SDL_Renderer *renderer = ctx->renderer; 704 705 SDL_RenderTexture(renderer, ctx->settings_icon, NULL, &ctx->settings_rect); 706} 707 708static void ToggleSettings(WindowState *ctx) 709{ 710 if (ctx->settings_visible) { 711 ctx->settings_visible = false; 712 SDL_StartTextInputWithProperties(ctx->window, ctx->text_settings); 713 } else { 714 SDL_StopTextInput(ctx->window); 715 ctx->settings_visible = true; 716 } 717} 718 719static int GetDefaultSetting(SDL_PropertiesID props, const char *setting) 720{ 721 if (SDL_strcmp(setting, SDL_PROP_TEXTINPUT_TYPE_NUMBER) == 0) { 722 return SDL_TEXTINPUT_TYPE_TEXT; 723 } 724 725 if (SDL_strcmp(setting, SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER) == 0) { 726 switch (SDL_GetNumberProperty(props, SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT)) { 727 case SDL_TEXTINPUT_TYPE_TEXT: 728 return SDL_CAPITALIZE_SENTENCES; 729 case SDL_TEXTINPUT_TYPE_TEXT_NAME: 730 return SDL_CAPITALIZE_WORDS; 731 default: 732 return SDL_CAPITALIZE_NONE; 733 } 734 } 735 736 if (SDL_strcmp(setting, SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN) == 0) { 737 return true; 738 } 739 740 if (SDL_strcmp(setting, SDL_PROP_TEXTINPUT_MULTILINE_BOOLEAN) == 0) { 741 return true; 742 } 743 744 SDL_assert(!"Unknown setting"); 745 return 0; 746} 747 748static void DrawSettings(WindowState *ctx) 749{ 750 SDL_Renderer *renderer = ctx->renderer; 751 SDL_FRect checkbox; 752 int i; 753 754 checkbox.x = MARGIN; 755 checkbox.y = MARGIN; 756 checkbox.w = (float)FONT_CHARACTER_SIZE; 757 checkbox.h = (float)FONT_CHARACTER_SIZE; 758 759 for (i = 0; i < SDL_arraysize(settings); ++i) { 760 if (settings[i].setting) { 761 int value = (int)SDL_GetNumberProperty(ctx->text_settings, settings[i].setting, GetDefaultSetting(ctx->text_settings, settings[i].setting)); 762 if (value == settings[i].value) { 763 SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); 764 SDL_RenderFillRect(renderer, &checkbox); 765 } 766 SDL_SetRenderDrawColor(renderer, backColor.r, backColor.g, backColor.b, backColor.a); 767 SDL_RenderRect(renderer, &checkbox); 768 SDLTest_DrawString(renderer, checkbox.x + checkbox.w + 8.0f, checkbox.y, settings[i].label); 769 } 770 checkbox.y += LINE_HEIGHT; 771 } 772} 773 774static void ClickSettings(WindowState *ctx, float x, float y) 775{ 776 int setting = (int)SDL_floorf((y - MARGIN) / LINE_HEIGHT); 777 if (setting >= 0 && setting < SDL_arraysize(settings)) { 778 SDL_SetNumberProperty(ctx->text_settings, settings[setting].setting, settings[setting].value); 779 } 780} 781 782static void RedrawWindow(WindowState *ctx) 783{ 784 SDL_Renderer *renderer = ctx->renderer; 785 int rendererID = ctx->rendererID; 786 SDL_FRect drawnTextRect, cursorRect, underlineRect; 787 char text[MAX_TEXT_LENGTH]; 788 789 DrawSettingsButton(ctx); 790 791 if (ctx->settings_visible) { 792 DrawSettings(ctx); 793 return; 794 } 795 796 /* Hide the text if it's a password */ 797 switch ((SDL_TextInputType)SDL_GetNumberProperty(ctx->text_settings, SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT)) { 798 case SDL_TEXTINPUT_TYPE_TEXT_PASSWORD_HIDDEN: 799 case SDL_TEXTINPUT_TYPE_NUMBER_PASSWORD_HIDDEN: { 800 size_t len = SDL_utf8strlen(ctx->text); 801 SDL_memset(text, '*', len); 802 text[len] = '\0'; 803 break; 804 } 805 default: 806 SDL_strlcpy(text, ctx->text, sizeof(text)); 807 break; 808 } 809 810 if (SDL_TextInputActive(ctx->window)) { 811 SDL_SetRenderDrawColor(renderer, backColor.r, backColor.g, backColor.b, backColor.a); 812 } else { 813 SDL_SetRenderDrawColor(renderer, 0x80, 0x80, 0x80, 0xFF); 814 } 815 SDL_RenderFillRect(renderer, &ctx->textRect); 816 817 /* Initialize the drawn text rectangle for the cursor */ 818 drawnTextRect.x = ctx->textRect.x; 819 drawnTextRect.y = ctx->textRect.y + (ctx->textRect.h - UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE) / 2; 820 drawnTextRect.w = 0.0f; 821 drawnTextRect.h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE; 822 823 if (text[0]) { 824 char *utext = text; 825 Uint32 codepoint; 826 size_t len; 827 SDL_FRect dstrect; 828 829 dstrect.x = ctx->textRect.x; 830 dstrect.y = ctx->textRect.y + (ctx->textRect.h - UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE) / 2; 831 dstrect.w = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE; 832 dstrect.h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE; 833 drawnTextRect.y = dstrect.y; 834 drawnTextRect.h = dstrect.h; 835 836 while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) { 837 float advance = unifont_draw_glyph(codepoint, rendererID, &dstrect) * UNIFONT_DRAW_SCALE; 838 dstrect.x += advance; 839 drawnTextRect.w += advance; 840 utext += len; 841 } 842 } 843 844 /* The marked text rectangle is the text area that hasn't been filled by committed text */ 845 ctx->markedRect.x = ctx->textRect.x + drawnTextRect.w; 846 ctx->markedRect.w = ctx->textRect.w - drawnTextRect.w; 847 848 /* Update the drawn text rectangle for composition text, after the committed text */ 849 drawnTextRect.x += drawnTextRect.w; 850 drawnTextRect.w = 0; 851 852 /* Set the cursor to the new location, we'll update it as we go, below */ 853 cursorRect = drawnTextRect; 854 cursorRect.w = 2; 855 cursorRect.h = drawnTextRect.h; 856 857 if (ctx->markedText[0]) { 858 int i = 0; 859 char *utext = ctx->markedText; 860 Uint32 codepoint; 861 size_t len; 862 SDL_FRect dstrect; 863 864 dstrect.x = drawnTextRect.x; 865 dstrect.y = ctx->textRect.y + (ctx->textRect.h - UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE) / 2; 866 dstrect.w = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE; 867 dstrect.h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE; 868 drawnTextRect.y = dstrect.y; 869 drawnTextRect.h = dstrect.h; 870 871 while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) { 872 float advance = unifont_draw_glyph(codepoint, rendererID, &dstrect) * UNIFONT_DRAW_SCALE; 873 dstrect.x += advance; 874 drawnTextRect.w += advance; 875 if (i < ctx->cursor) { 876 cursorRect.x += advance; 877 } 878 i++; 879 utext += len; 880 } 881 882 if (ctx->cursor_length > 0) { 883 cursorRect.w = ctx->cursor_length * UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE; 884 } 885 886 cursorRect.y = drawnTextRect.y; 887 cursorRect.h = drawnTextRect.h; 888 889 underlineRect = ctx->markedRect; 890 underlineRect.y = drawnTextRect.y + drawnTextRect.h - 2; 891 underlineRect.h = 2; 892 underlineRect.w = drawnTextRect.w; 893 894 SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a); 895 SDL_RenderFillRect(renderer, &underlineRect); 896 } 897 898 /* Draw the cursor */ 899 if (SDL_TextInputActive(ctx->window)) { 900 Uint64 now = SDL_GetTicks(); 901 if ((now - ctx->last_cursor_change) >= CURSOR_BLINK_INTERVAL_MS) { 902 ctx->cursor_visible = !ctx->cursor_visible; 903 ctx->last_cursor_change = now; 904 } 905 if (ctx->cursor_length > 0) { 906 /* We'll show a highlight */ 907 SDL_SetRenderDrawBlendMode(renderer, highlight_mode); 908 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 909 SDL_RenderFillRect(renderer, &cursorRect); 910 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE); 911 } else if (ctx->cursor_visible) { 912 SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a); 913 SDL_RenderFillRect(renderer, &cursorRect); 914 } 915 } 916 917 /* Draw the candidates */ 918 DrawCandidates(ctx, &cursorRect); 919 920 /* Update the area used to draw composition UI */ 921 UpdateTextInputArea(ctx, &cursorRect); 922} 923 924static void Redraw(void) 925{ 926 int i; 927 for (i = 0; i < state->num_windows; ++i) { 928 SDL_Renderer *renderer = state->renderers[i]; 929 if (state->windows[i] == NULL) { 930 continue; 931 } 932 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); 933 SDL_RenderClear(renderer); 934 935 RedrawWindow(&windowstate[i]); 936 937 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 938 SDL_RenderDebugTextFormat(renderer, 4, 4, "Window %d", 1 + i); 939 940 SDL_RenderPresent(renderer); 941 } 942} 943 944int main(int argc, char *argv[]) 945{ 946 bool render_composition = false; 947 bool render_candidates = false; 948 int i, done; 949 SDL_Event event; 950 char *fontname = NULL; 951 952 /* Initialize test framework */ 953 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); 954 if (!state) { 955 return 1; 956 } 957 958 /* Parse commandline */ 959 for (i = 1; i < argc;) { 960 int consumed; 961 962 consumed = SDLTest_CommonArg(state, i); 963 if (SDL_strcmp(argv[i], "--font") == 0) { 964 if (*argv[i + 1]) { 965 fontname = argv[i + 1]; 966 consumed = 2; 967 } 968 } else if (SDL_strcmp(argv[i], "--render-composition") == 0) { 969 render_composition = true; 970 consumed = 1; 971 } else if (SDL_strcmp(argv[i], "--render-candidates") == 0) { 972 render_candidates = true; 973 consumed = 1; 974 } 975 if (consumed <= 0) { 976 static const char *options[] = { "[--font fontfile] [--render-composition] [--render-candidates]", NULL }; 977 SDLTest_CommonLogUsage(state, argv[0], options); 978 return 1; 979 } 980 981 i += consumed; 982 } 983 984 if (render_composition && render_candidates) { 985 SDL_SetHint(SDL_HINT_IME_IMPLEMENTED_UI, "composition,candidates"); 986 } else if (render_composition) { 987 SDL_SetHint(SDL_HINT_IME_IMPLEMENTED_UI, "composition"); 988 } else if (render_candidates) { 989 SDL_SetHint(SDL_HINT_IME_IMPLEMENTED_UI, "candidates"); 990 } 991 992 if (!SDLTest_CommonInit(state)) { 993 return 2; 994 } 995 996 windowstate = (WindowState *)SDL_calloc(state->num_windows, sizeof(*windowstate)); 997 if (!windowstate) { 998 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't allocate window state: %s", SDL_GetError()); 999 return -1; 1000 } 1001 1002 fontname = GetResourceFilename(fontname, DEFAULT_FONT); 1003 1004 if (unifont_init(fontname) < 0) { 1005 return -1; 1006 } 1007 1008 SDL_Log("Using font: %s", fontname); 1009 1010 /* Initialize window state */ 1011 for (i = 0; i < state->num_windows; ++i) { 1012 WindowState *ctx = &windowstate[i]; 1013 SDL_Window *window = state->windows[i]; 1014 SDL_Renderer *renderer = state->renderers[i]; 1015 1016 SDL_SetRenderLogicalPresentation(renderer, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_LOGICAL_PRESENTATION_LETTERBOX); 1017 1018 ctx->window = window; 1019 ctx->renderer = renderer; 1020 ctx->rendererID = i; 1021 ctx->settings_icon = LoadTexture(renderer, "icon.png", true); 1022 if (ctx->settings_icon) { 1023 ctx->settings_rect.w = (float)ctx->settings_icon->w; 1024 ctx->settings_rect.h = (float)ctx->settings_icon->h; 1025 ctx->settings_rect.x = (float)WINDOW_WIDTH - ctx->settings_rect.w - MARGIN; 1026 ctx->settings_rect.y = MARGIN; 1027 } 1028 1029 InitInput(ctx); 1030 1031 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE); 1032 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); 1033 SDL_RenderClear(renderer); 1034 } 1035 highlight_mode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE_MINUS_DST_COLOR, 1036 SDL_BLENDFACTOR_ZERO, 1037 SDL_BLENDOPERATION_ADD, 1038 SDL_BLENDFACTOR_ZERO, 1039 SDL_BLENDFACTOR_ONE, 1040 SDL_BLENDOPERATION_ADD); 1041 1042 /* Main render loop */ 1043 done = 0; 1044 while (!done) { 1045 /* Check for events */ 1046 while (SDL_PollEvent(&event)) { 1047 SDLTest_CommonEvent(state, &event, &done); 1048 switch (event.type) { 1049 case SDL_EVENT_MOUSE_BUTTON_UP: { 1050 SDL_FPoint point; 1051 WindowState *ctx = GetWindowStateForWindowID(event.button.windowID); 1052 if (!ctx) { 1053 break; 1054 } 1055 1056 SDL_ConvertEventToRenderCoordinates(ctx->renderer, &event); 1057 point.x = event.button.x; 1058 point.y = event.button.y; 1059 if (SDL_PointInRectFloat(&point, &ctx->settings_rect)) { 1060 ToggleSettings(ctx); 1061 } else if (ctx->settings_visible) { 1062 ClickSettings(ctx, point.x, point.y); 1063 } else { 1064 if (SDL_TextInputActive(ctx->window)) { 1065 SDL_Log("Disabling text input\n"); 1066 SDL_StopTextInput(ctx->window); 1067 } else { 1068 SDL_Log("Enabling text input\n"); 1069 SDL_StartTextInput(ctx->window); 1070 } 1071 } 1072 break; 1073 } 1074 case SDL_EVENT_KEY_DOWN: { 1075 WindowState *ctx = GetWindowStateForWindowID(event.key.windowID); 1076 if (!ctx) { 1077 break; 1078 } 1079 1080 switch (event.key.key) { 1081 case SDLK_RETURN: 1082 ctx->text[0] = 0x00; 1083 break; 1084 case SDLK_BACKSPACE: 1085 /* Only delete text if not in editing mode. */ 1086 if (!ctx->markedText[0]) { 1087 size_t textlen = SDL_strlen(ctx->text); 1088 1089 do { 1090 if (textlen == 0) { 1091 break; 1092 } 1093 if (!(ctx->text[textlen - 1] & 0x80)) { 1094 /* One byte */ 1095 ctx->text[textlen - 1] = 0x00; 1096 break; 1097 } 1098 if ((ctx->text[textlen - 1] & 0xC0) == 0x80) { 1099 /* Byte from the multibyte sequence */ 1100 ctx->text[textlen - 1] = 0x00; 1101 textlen--; 1102 } 1103 if ((ctx->text[textlen - 1] & 0xC0) == 0xC0) { 1104 /* First byte of multibyte sequence */ 1105 ctx->text[textlen - 1] = 0x00; 1106 break; 1107 } 1108 } while (1); 1109 } 1110 break; 1111 default: 1112 if ((event.key.mod & SDL_KMOD_CTRL) && (event.key.key >= SDLK_KP_1 && event.key.key <= SDLK_KP_9)) { 1113 int index = (event.key.key - SDLK_KP_1); 1114 if (index < state->num_windows) { 1115 SDL_Window *window = state->windows[index]; 1116 if (SDL_TextInputActive(window)) { 1117 SDL_Log("Disabling text input for window %d\n", 1 + index); 1118 SDL_StopTextInput(window); 1119 } else { 1120 SDL_Log("Enabling text input for window %d\n", 1 + index); 1121 SDL_StartTextInput(window); 1122 } 1123 } 1124 } 1125 break; 1126 } 1127 1128 if (done) { 1129 break; 1130 } 1131 1132 SDL_Log("Keyboard: scancode 0x%08X = %s, keycode 0x%08" SDL_PRIX32 " = %s", 1133 event.key.scancode, 1134 SDL_GetScancodeName(event.key.scancode), 1135 SDL_static_cast(Uint32, event.key.key), 1136 SDL_GetKeyName(event.key.key)); 1137 break; 1138 } 1139 case SDL_EVENT_TEXT_INPUT: { 1140 WindowState *ctx = GetWindowStateForWindowID(event.text.windowID); 1141 if (!ctx) { 1142 break; 1143 } 1144 1145 if (event.text.text[0] == '\0' || event.text.text[0] == '\n' || ctx->markedRect.w < 0) { 1146 break; 1147 } 1148 1149 SDL_Log("Keyboard: text input \"%s\"", event.text.text); 1150 1151 if (SDL_strlen(ctx->text) + SDL_strlen(event.text.text) < sizeof(ctx->text)) { 1152 SDL_strlcat(ctx->text, event.text.text, sizeof(ctx->text)); 1153 } 1154 1155 SDL_Log("text inputted: %s", ctx->text); 1156 1157 /* After text inputted, we can clear up markedText because it */ 1158 /* is committed */ 1159 ctx->markedText[0] = 0; 1160 break; 1161 } 1162 case SDL_EVENT_TEXT_EDITING: { 1163 WindowState *ctx = GetWindowStateForWindowID(event.edit.windowID); 1164 if (!ctx) { 1165 break; 1166 } 1167 1168 SDL_Log("text editing \"%s\", selected range (%" SDL_PRIs32 ", %" SDL_PRIs32 ")", 1169 event.edit.text, event.edit.start, event.edit.length); 1170 1171 SDL_strlcpy(ctx->markedText, event.edit.text, sizeof(ctx->markedText)); 1172 ctx->cursor = event.edit.start; 1173 ctx->cursor_length = event.edit.length; 1174 break; 1175 } 1176 case SDL_EVENT_TEXT_EDITING_CANDIDATES: { 1177 WindowState *ctx = GetWindowStateForWindowID(event.edit.windowID); 1178 if (!ctx) { 1179 break; 1180 } 1181 1182 SDL_Log("text candidates:"); 1183 for (i = 0; i < event.edit_candidates.num_candidates; ++i) { 1184 SDL_Log("%c%s", i == event.edit_candidates.selected_candidate ? '>' : ' ', event.edit_candidates.candidates[i]); 1185 } 1186 1187 ClearCandidates(ctx); 1188 SaveCandidates(ctx, &event); 1189 break; 1190 } 1191 default: 1192 break; 1193 } 1194 } 1195 1196 Redraw(); 1197 } 1198 SDL_free(fontname); 1199 CleanupVideo(); 1200 SDLTest_CommonQuit(state); 1201 return 0; 1202} 1203[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.