Atlas - testime.c

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