Files
kaizen/test/testime.c
SimoneN64 3a7f96fd99 Squashed 'external/SDL/' changes from f317581c919..95c3ee77c10
95c3ee77c10 Removed testing code
ea640894d32 Fixed SDL_DEFINE_STDBOOL logic if __bool_true_false_are_defined is defined
46fb2c6f1bf Added a script to help with the bool rename
23f855a9703 render: Integrate fix-shaders.sh into build-shaders.sh
d658ef66279 Don't guess about the size of an int, assert it!
32fd1878a09 Provide a definition of bool if needed for your environment
1834c83d5a2 testplatform: make sure bool is 1 byte
026d3c2306f Sync SDL3 wiki -> header
95f7e532331 Added the examples directory to the API renaming tool
a90ad3b0e28 Removed SDL_bool in favor of plain bool
9dd88592407 Sync SDL3 wiki -> header
32cf23d392f Added video/x11/edid.h to third party code list
407ccadfd72 Added copyright header to edid.h
bd04459cde1 Fix typos (thanks @qndel!)
e4f987f2996 tests: Fix testdropfile on high-DPI displays
8caf25f7b30 testautomation_stdlib.c (stdlib_strtox): fix warning from gcc-4.9:
03ae792df35 Fixed the recording field in audio device events
c68e9e157cd Fixed typo
bc9780299c0 Don't define backends if SDL_GPU_DISABLED is set
8481d1a12b0 Sync SDL3 wiki -> header
807b8a9d4de Allow initializing hints and properties from any thread
d0edf687741 Added Uint32 versions of the atomic functions
8d223b3037e Renamed atomic functions to match SDL 3.0 naming convention
f3e419596b1 Removed SDL_INIT_TIMER
9275c533ca2 Added thread-safe initialization/cleanup support
7edd43c276e Don't define render_drivers if SDL_RENDER_DISABLED is set
98cd8b34a76 Close the other file descriptors after redirection has taken place
4b3058d58db render: Don't try to rebuild D3D11, D3D12, Metal shaders without spirv-cross
13500bbf508 render: Fix detection of fxc, dxc in build-shaders.sh
11129761e7e v4l2: return success if the device is ready
ab9c68d9ffc pipewire: Always destroy hotplug loop during deinitialization
1ffb9f22aa5 build-release.py fixes from SDL2 port
231ea07617d Added SDL_LogTrace()
6180da00e1d Skip a lock/unlock when logging known categories
c499f79fe93 GPU: Fix multisample texture usage validation
926d1439660 Sync SDL3 wiki -> header
1bbc897e48e Updated documentation for bool return type
4a2683e2c10 Sync SDL3 wiki -> header
dc639956baa Improve logging performance and make log priorities thread-safe
f006d61bd12 Sync SDL3 wiki -> header
fd1fcfc951b Sync SDL3 wiki -> header
9416917353c GPU: Rework MSAA (#10859)
e59344ad469 Update proguard-rules.pro in-line with commit 9ff3446
0f85fdefc84 Fixed warning: "SDL_VIDEO_RENDER_D3D12" is not defined, evaluates to 0
4f722d372ae GPU: Metal vertex buffer indices should grow upward (#10837)
0548050fc5a Verify that the subsystems were successfully initialized
5b5b1a89267 Fixed cleaning up dependent subsystems if initialization fails
97d40b92180 Removed SDL_CleanupEnvironment()
a7f1deae8d1 Sync SDL3 wiki -> header
28b94c47585 Re-added SDL_getenv() as a thread-safe getenv() implementation
718845a33d0 Fixed warning C4701: potentially uninitialized local variable 'startup_info' used
19243092c67 Renamed SDL_GpuGraphicsPipelineTargetInfo to SDL_GPUGraphicsPipelineTargetInfo
604d0c519ec If posix_spawn() is available, so is sigaction
ec5d280c900 Make sure we ignore SIGPIPE so we don't crash if we write when the pipe is closed
34b2f4ffcab Use non-blocking pipes for process I/O on Windows
dd07cc89910 SDL_Process: WAIT_TIMEOUT means the process has not exited yet
1f3fd65c4c7 cpuinfo: Rename SDL_GetCPUCount to SDL_GetNumLogicalCPUCores
93bf5342684 testgpu_simple_clear: Fixed AppInit return values.
e5bd3bbcd6e add missing includes
8569bde053f Fixed typo
706aaaf2802 Sync SDL3 wiki -> header
3bc03477618 Revert "Sync SDL3 wiki -> header"
c2e4b14a828 Revert "stdinc: Added missing `\param` to SDL_CreateEnvironment."
11c9975caa9 Revert "Sync SDL3 wiki -> header"
ba65767afa2 Sync SDL3 wiki -> header
919f8d3e2b9 stdinc: Added missing `\param` to SDL_CreateEnvironment.
35427ff9ec7 Sync SDL3 wiki -> header
dcb2a8c2fe2 Use CreateProcessW() to handle non-ASCII file paths and arguments
e97f6365906 SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER is an SDL_Environment pointer
56fc4b790c4 Reduce strcmp() calls in hashtable lookup
e6734794491 Removed SDL_WriteProcess()
a833ee2e63e SDL_gpu.c: fixed build when SDL_GPU_DISABLED is defined.
f6f49de1342 Add convenience functions to get process IOStreams
7ff015ceaee Close file descriptors not used by the child process
fed0149172e Switched wayland messageboxes to use the new process API
aa7357a14d7 SDL_CreateEnvironment() fills the environment with a non-zero parameter
76c469910ee Added SDL_PROP_PROCESS_CREATE_BACKGROUND_BOOLEAN
44c6cfda053 Switched zenity dialogs to use the new process API
3166a05c18b cmake: don't prepend absolute CMAKE_INSTALL_{INCLUDE,LIB}DIR with ${prefix}
27862907c65 Update src/process/windows/SDL_windowsprocess.c
82b33a7d3f6 Update src/process/windows/SDL_windowsprocess.c
3cf54675bb0 Windows process: escape backslashes before quotes
90e01040c52 Added thread-safe environment functions
16ff7503b7b gpu: Update GPU support queries to better match naming conventions
d21c1de6518 Sync SDL3 wiki -> header
9eea8234e67 Add SDL_Process subsystem
6c83491116e Added SDL_FlushIO()
93caf1cd21a Fixed typo
1c5b063ade7 Sync SDL3 wiki -> header
f1d0a716292 Fixed the documentation for SDL_SetError()
d483886ec57 Sync SDL3 wiki -> header
84361bcf0ab stdlib: Remove test cases with impl-defined results
eb199176e68 stdlib: Conditionally undef some SDL_strtox tests
59ec0344128 stdlib: Use macros to define SDL_strtox tests
7d94bf528db stdlib: Document SDL_strtox functions
8092e35287e stdlib: Improve SDL_strtod
a78f612d4b2 stdlib: Add some tests for SDL_strtod
56a48763cd7 stdlib: Bring SDL_wcstol in line with SDL_strtol
61bc856b043 stdlib: Use new parser for scanf %p specifier
e109aa09aa3 stdlib: Rewrite SDL_strto(ll?|ul) impl
e326540a45d stdlib: Add failing tests for SDL_strto(ll?|ul)
fb82772fb31 stdlib: Rewrite SDL_strtoull impl
5331f36789d stdlib: Add failing tests for SDL_strtoull
88d3cb4f900 Remove unnecessary include from SDL_pen_c.h
4efbe1ca285 stdlib: Assert that signed integers use two's complement arithmetic
fd53b3e112b stdlib: Rewrite SDL_wcstol impl
5d30980df46 stdlib: Add failing tests for SDL_wcstol
ee377793fe0 Spell "unknown" correctly
ee65176eec3 SDL_test: add SDLTest_LogEscapedString
6a305e1532e SDL_test: fix SDLTest_CommonQuit with NULL state
0160e9eac64 gpu: Add SDL_QueryGPUSupport
262ffa27e7f Sync SDL3 wiki -> header
96e147b2b95 gpu: Rework driver name queries, add GetGPUShaderFormats
6d92de5d3a6 wayland: Ensure that a NULL internal structure isn't dereferenced when destroying a window
9d9721cd4c8 wayland: Fix some incorrect buffer scale calculations
94436a938d0 GPU: Remove unused and incorrect D16 format checks from Vulkan driver
5771b502f84 GPU: Fix D3D11/D3D12 vertex strides for multiple vertex buffers (#10818)
371cfaf8fe4 gpu: Implement support for SDL_GPU_DISABLED
d3932b1ba2e Sync SDL3 wiki -> header
a45a2caf493 GPU: Rename VertexBinding to VertexBufferDescription (#10811)
446ee3e7c54 Fix bug with SDL_ShowMessageBox not working with wayland (#10810)
b7dc30ca246 pulseaudio: Hotplug thread fixes.
c36982d45c2 vulkan: Avoid redundant commands for dynamic states
66489f91bb4 GPU: Cube Arrays (#10800)
9ea0a837aeb vulkan: Use a spinlock instead of a mutex for descriptor pools
f8d4c492641 Sync SDL3 wiki -> header
d5e1385c91d Fixed typos in SDL_gpu.h
6e2f2428bac Sync SDL3 wiki -> header
ddd5723e2ee GPU: Remove pitch parameters from indirect draw calls (#10803)
3d7e8c9bb7b Add missing SDL_WPRINTF_VARARG_FUNCV in gendynapi.py
d03c7cdf17b Removing extra semicolons
fd2a266549f Adding void to empty function prototype parenthesis
b15fab02070 Fixed typo in SDL_dialog.h
d62e3c3791e Fix SDL_GPUShaderFormat flag values
4d135fb696d Fixed color scale when rendering to an sRGB render target
3f7f6f624b0 testcolorspace: don't tonemap from HDR to SDR when reading pixels
4e43da684c7 Added texture colorspace testing
86e393b5235 Clarify that INPUTTYPE_UNSPECIFIED is correct for RGB textures
92255d36d4b We already have functions for converting between sRGB and linear space
667a3e40e9a Use the correct namespace for structures
77c569496df SDL_GPUShaderFormat isn't an enum anymore
37c9fb490ef Changed enums to use XXX_COUNT for the count or number of values
64f12bea4a9 GPU: Remove unnecessary texture format swizzles in Vulkan (#10693)
4123023d3ed Renamed sdlgpu to gpu
c77e51c0844 Use SDL_unsetenv()
3166f912826 GPU: Defer setting blend state on D3D11
6fca867ffe3 Fix testoffscreen.c: bool literal returned from 'main'
eeb465e2fcb Sync SDL3 wiki -> header
2b8a349b262 Add SDL_BindGPUComputeSamplers (#10778)
323b60abf1a SDL_test_harness.c: Fix warning -Wcast-function-type-strict
0b6f993deac GPU: Zero-init handling (#10786)
b8dbc7086d4 Disable GameInput mouse and keyboard support
76ce83801ad Removed pipewire client version check
9c8c6da4760 pipewire: If hotplug initialization fails, clean up any partial success
817f1b3da81 Revert "Changed SDL_GPU_TEXTUREFORMAT_INVALID to 0"
b51c6551d6e Changed SDL_GPU_TEXTUREFORMAT_INVALID to 0
1e9ff723adb GPU: fix SDL_GetGPUSwapchainTextureFormat error return value
640559eba68 Added migration documentation for SDL_strtoll(), SDL_strtoull(), SDL_lltoa(), and SDL_ulltoa().
4eb43705000 SDL_strtoll(), SDL_strtoull(), SDL_lltoa(), and SDL_ulltoa() use long long values
68c61ff3a8f Sync SDL3 wiki -> header
6fc6e3dc7eb Use SDL_bool where appropriate in SDL events
7d1bbae6b25 Sync SDL3 wiki -> header
af37056c0d0 stdinc: Fix typos and reword comments about aliasing
8eb194b6f86 stdinc: Document number parsing APIs
eda459ac49e stdinc: Fix up some printf definitions
d6caf2abe78 Remove SDL_RESTRICT in favor of doc comments
731939fca16 stdinc: Document memcpy APIs
8cea4bd3c94 stdinc: Document memory allocation APIs
f4c2c09f782 examples: Link to SDL integer types in code examples, too.
7713a7eec7d x11vulkan: Use the correct SONAME of libX11-xcb.so.1 on Linux, etc.
412a8244b66 audio: Make system-specific audio format types part of SDL_AudioFormat.
d3091b95389 Remove SDL_GPUDepthStencilValue struct
80e541d1fcb Fixed calling SDL_utf8strlcpy() with dst_bytes of 0
668e2f82d21 Add load op and clear color to SDL_BlitGPUTexture (#10767)
47283250442 pipewire: Give hotplug threads a more indicative name
c797ae16192 Fix -Wtype-limits warning
6e885d96193 Sync SDL3 wiki -> header
301f3ffa9dd Sync SDL3 wiki -> header
050aa4d084a include: Filling in more documentation gaps.
a57f6c4af30 Fix additional calloc-transposed-args warnings
d2ef15d8e6a Fix warnings: calloc-transposed-args
0da2bd49c8c Remove unused includes. This only removes includes that aren't used at all, not even in comments.
aa6a0be7a87 Fix warning: redefinition of typedef 'SDLTest_TestSuiteRunner'
7df1caba7cf Clarified that the contents of new textures aren't defined.
ac0b1b207f6 SDL_hints: fix gpu direct3d11 driver name in documentation
257385277d3 SDL_realloc() with size 0 now always defaults to size 1
4c0cb94b961 vulkan: VULKAN_CreateTexture returns a boolean
3eb49ac8627 vulken: VULKAN_CreateTexture returns a boolean
1ba99c53d48 Sync SDL3 wiki -> header
2b77b2e4ef0 Sync SDL3 wiki -> header
4f6e81cf622 include: Fixed up some minor documentation gaps.
7ec998819d1 Sync SDL3 wiki -> header
090e30dbca3 docs: Remove README-winrt.md, again.
fd6056307f0 fix trailing doc comments
1a13acac092 Always blit to the swapchain in GPU_RenderPresent
fa892d15b7d include files with required types directly
e9c7b36fbd7 Sync SDL3 wiki -> header
68a9991ec99 GPU: Document structs (#10742)
cf9613fe632 The GPU log category ate one of the SDL reserved categories
a01e8a5e105 The GPU log category ate one of the SDL reserved categories
18492057331 Fix GDK builds after renames done in #10730
0a44ea1b0b3 Amend use of non-standard compiler syntax
0a8bf663b7d Fix VisualC project filter
1d59be67fc7 Fixed crash if PIPEWIRE_PREFERRED_Init() fails and then pipewire is used anyway
f42da35ef96 Sync SDL3 wiki -> header
9730f62e8cb GPU: Rename struct members and parameters for SDL3 naming conventions (#10730)
e21f70c5936 pipewire : check minimum client library version early
845855d6573 Revert "stdinc: Document more symbols."
c93f76436fc stdinc: Document more symbols.
73b294cb1cd SDL_platform_defines.h: Added documentation, and a little whitespace.
4c8357a37da Clarified why the WINDOWS driver is lower than the RAWINPUT driver
66b4c080c06 Clarified why the WGI driver is lower than the WINDOWS driver
8f46cb771c7 SDL_XINPUT_Enabled() returns false until XInput DLL is successfully loaded
6da97c63da9 Sync SDL3 wiki -> header
ac08dde1b22 windows: Clean out things that should be using `defined(SDL_PLATFORM_WINDOWS)`.
154452a7263 winrt: Removed WinRT/Windows Phone/UWP support.
6d7c211fafd Fixed race condition at startup that could cause a crash in the XInput driver
6e5bd58c4cc Include the Steam controller driver on Android
1381cc85543 Add a step to fix generated shader code
104642ffe79 Sorted API symbols
4c382aafcdc Fixed building on Raspberry Pi
360bc667a82 SDL_Log: restore SDL2's SDL_LogCategory order
26728c321fa SDL_test: add include for SDLTest_CommonState
b4c3df1189c SDL_test: whitespace header fixes
db96ddca344 SDL_test: use SDLCALL calling convention
379aea5c2df Fixed warnings building with Visual Studio
8edb901724a Fixed crashes in Metal renderer due to ARC releasing references on random memory in newly allocated structs
e7969553f88 Sync SDL3 wiki -> header
702ed83f72a Initialize interface structures so they can be extended in the future
434193d1534 testmultiaudio: initialize audio through SDL_test framework
102b3b480bb SDL_test: move argument parsing into SDL_test
09af4a80866 SDL_test: make argument parsing extendable
396dd16471d SDL_test: Remove unused common_usage_* static char pointers
872608b8af2 Don't allocate in SDLTest_GenerateRunSeed
2f4b2df5959 SDL_test: pass data pointer to unit tests
741c04b339f Sync SDL3 wiki -> header
baa1a5e2f47 Add SDL_strpbrk
55934bc85e4 include: add SDL_RESTRICT for restricted pointer aliasing
2d4eb29c379 Add SDL_SetGPUBlendConstants, SDL_SetGPUStencilReference (#10704)
04a732881a3 Add a test to verify structure alignment
9e850bdcfaa docs: Update the migration doc with SDL_SetWindowModalFor() changes
c0b15e57f46 cmake: disable GPU backends for winrt
f494658f9c4 SDL_gpu.c: remove BOM.
cb2919ac55f [GPU] MinGW build fixes:
22aeb1bbba2 Sync SDL3 wiki -> header
a46e7027ce7 video: Allow setting the parents of toplevel windows
af4c6682ce6 Sync SDL3 wiki -> header
cba5ccb9cf3 GPU: Fix documentation formatting
42a0df91bfe Add padding for non-32-bit-aligned types in structs (#10701)
387774ab8a3 Build config flags are either defined or undefined, never 0
e3fd581acac GPU: vulkan: Respect swapchain minImageCount
b00bb215074 Added -Wl,--no-undefined-version to Android.mk
5db00cef96c Android.mk: allow to compile with storage functions
a0f36fb85b4 Use atomic variables for thread communication
41810c823d6 Compilation without vulkan: SDL_GPU_VULKAN is always defined for some plateform (at android, macos), test against 0 or 1
defbf673069 SDL_migration.cocci: add SDL_TTF/mixer/image function names to be upgraded from int returned type to bool.
f61d4db39af GPU: `colorAttachmentDescriptions` should be `const`
249a74e4363 Sync SDL3 wiki -> header
1c8c0144811 GPU: Document opaque handles and enums (#10700)
20d0a1a3d1a Added a defensive check against XINPUTGETCAPABILITIES being NULL
ff1e2524134 testautomation: remove SDLTest_TestCaseFp casts
abd2ac5992c Set the desired channel count when opening audio on Android
e2c9a0a4273 Updated Android audio log messages
1788be85477 GPU: Add const to some function parameters (#10694)

git-subtree-dir: external/SDL
git-subtree-split: 95c3ee77c101c60aab69663be2bc4eee3d4302b2
2024-09-18 20:42:08 +02:00

1276 lines
42 KiB
C

/*
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
/* A simple program to test the Input Method support in the SDL library (2.0+)
If you build without SDL_ttf, you can use the GNU Unifont hex file instead.
Download at http://unifoundry.com/unifont.html */
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <SDL3/SDL_test_font.h>
#ifdef HAVE_SDL_TTF
#include "SDL_ttf.h"
#endif
#include <SDL3/SDL_test_common.h>
#include "testutils.h"
#ifdef HAVE_SDL_TTF
#define DEFAULT_PTSIZE 30
#endif
#ifdef HAVE_SDL_TTF
#ifdef SDL_PLATFORM_MACOS
#define DEFAULT_FONT "/System/Library/Fonts/华文细黑.ttf"
#elif defined(SDL_PLATFORM_WIN32)
/* Some japanese font present on at least Windows 8.1. */
#define DEFAULT_FONT "C:\\Windows\\Fonts\\yugothic.ttf"
#else
#define DEFAULT_FONT "NoDefaultFont.ttf"
#endif
#else
#define DEFAULT_FONT "unifont-15.1.05.hex"
#endif
#define MAX_TEXT_LENGTH 256
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
#define MARGIN 32.0f
#define LINE_HEIGHT (FONT_CHARACTER_SIZE + 4.0f)
#define CURSOR_BLINK_INTERVAL_MS 500
typedef struct
{
SDL_Window *window;
SDL_Renderer *renderer;
int rendererID;
bool settings_visible;
SDL_Texture *settings_icon;
SDL_FRect settings_rect;
SDL_PropertiesID text_settings;
SDL_FRect textRect;
SDL_FRect markedRect;
char text[MAX_TEXT_LENGTH];
char markedText[MAX_TEXT_LENGTH];
int cursor;
int cursor_length;
bool cursor_visible;
Uint64 last_cursor_change;
char **candidates;
int num_candidates;
int selected_candidate;
bool horizontal_candidates;
} WindowState;
static SDLTest_CommonState *state;
static WindowState *windowstate;
static const SDL_Color lineColor = { 0, 0, 0, 255 };
static const SDL_Color backColor = { 255, 255, 255, 255 };
static const SDL_Color textColor = { 0, 0, 0, 255 };
static SDL_BlendMode highlight_mode;
static const struct
{
const char *label;
const char *setting;
int value;
} settings[] = {
{ "Text", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT },
{ "Name", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT_NAME },
{ "E-mail", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT_EMAIL },
{ "Username", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT_USERNAME },
{ "Password (hidden)", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT_PASSWORD_HIDDEN },
{ "Password (visible)", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT_PASSWORD_VISIBLE },
{ "Number", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_NUMBER },
{ "Numeric PIN (hidden)", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_NUMBER_PASSWORD_HIDDEN },
{ "Numeric PIN (visible)", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_NUMBER_PASSWORD_VISIBLE },
{ "", NULL },
{ "No capitalization", SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER, SDL_CAPITALIZE_NONE },
{ "Capitalize sentences", SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER, SDL_CAPITALIZE_SENTENCES },
{ "Capitalize words", SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER, SDL_CAPITALIZE_WORDS },
{ "All caps", SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER, SDL_CAPITALIZE_LETTERS },
{ "", NULL },
{ "Auto-correct OFF", SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN, false },
{ "Auto-correct ON", SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN, true },
{ "Multiline OFF", SDL_PROP_TEXTINPUT_MULTILINE_BOOLEAN, false },
{ "Multiline ON", SDL_PROP_TEXTINPUT_MULTILINE_BOOLEAN, true }
};
#ifdef HAVE_SDL_TTF
static TTF_Font *font;
#else
#define UNIFONT_MAX_CODEPOINT 0x1ffff
#define UNIFONT_NUM_GLYPHS 0x20000
#define UNIFONT_REPLACEMENT 0xFFFD
/* Using 512x512 textures that are supported everywhere. */
#define UNIFONT_TEXTURE_WIDTH 512
#define UNIFONT_GLYPH_SIZE 16
#define UNIFONT_GLYPH_BORDER 1
#define UNIFONT_GLYPH_AREA (UNIFONT_GLYPH_BORDER + UNIFONT_GLYPH_SIZE + UNIFONT_GLYPH_BORDER)
#define UNIFONT_GLYPHS_IN_ROW (UNIFONT_TEXTURE_WIDTH / UNIFONT_GLYPH_AREA)
#define UNIFONT_GLYPHS_IN_TEXTURE (UNIFONT_GLYPHS_IN_ROW * UNIFONT_GLYPHS_IN_ROW)
#define UNIFONT_NUM_TEXTURES ((UNIFONT_NUM_GLYPHS + UNIFONT_GLYPHS_IN_TEXTURE - 1) / UNIFONT_GLYPHS_IN_TEXTURE)
#define UNIFONT_TEXTURE_SIZE (UNIFONT_TEXTURE_WIDTH * UNIFONT_TEXTURE_WIDTH * 4)
#define UNIFONT_TEXTURE_PITCH (UNIFONT_TEXTURE_WIDTH * 4)
#define UNIFONT_DRAW_SCALE 2.0f
static struct UnifontGlyph
{
Uint8 width;
Uint8 data[32];
} * unifontGlyph;
static SDL_Texture **unifontTexture;
static Uint8 unifontTextureLoaded[UNIFONT_NUM_TEXTURES] = { 0 };
/* Unifont loading code start */
static Uint8 dehex(char c)
{
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
} else if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
}
return 255;
}
static Uint8 dehex2(char c1, char c2)
{
return (dehex(c1) << 4) | dehex(c2);
}
static Uint8 validate_hex(const char *cp, size_t len, Uint32 *np)
{
Uint32 n = 0;
for (; len > 0; cp++, len--) {
Uint8 c = dehex(*cp);
if (c == 255) {
return 0;
}
n = (n << 4) | c;
}
if (np) {
*np = n;
}
return 1;
}
static int unifont_init(const char *fontname)
{
Uint8 hexBuffer[65];
Uint32 numGlyphs = 0;
int lineNumber = 1;
size_t bytesRead;
SDL_IOStream *hexFile;
const size_t unifontGlyphSize = UNIFONT_NUM_GLYPHS * sizeof(struct UnifontGlyph);
const size_t unifontTextureSize = UNIFONT_NUM_TEXTURES * state->num_windows * sizeof(void *);
char *filename;
/* Allocate memory for the glyph data so the file can be closed after initialization. */
unifontGlyph = (struct UnifontGlyph *)SDL_malloc(unifontGlyphSize);
if (!unifontGlyph) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d KiB for glyph data.\n", (int)(unifontGlyphSize + 1023) / 1024);
return -1;
}
SDL_memset(unifontGlyph, 0, unifontGlyphSize);
/* Allocate memory for texture pointers for all renderers. */
unifontTexture = (SDL_Texture **)SDL_malloc(unifontTextureSize);
if (!unifontTexture) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d KiB for texture pointer data.\n", (int)(unifontTextureSize + 1023) / 1024);
return -1;
}
SDL_memset(unifontTexture, 0, unifontTextureSize);
filename = GetResourceFilename(NULL, fontname);
if (!filename) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory\n");
return -1;
}
hexFile = SDL_IOFromFile(filename, "rb");
SDL_free(filename);
if (!hexFile) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to open font file: %s\n", fontname);
return -1;
}
/* Read all the glyph data into memory to make it accessible later when textures are created. */
do {
int i, codepointHexSize;
size_t bytesOverread;
Uint8 glyphWidth;
Uint32 codepoint;
bytesRead = SDL_ReadIO(hexFile, hexBuffer, 9);
if (numGlyphs > 0 && bytesRead == 0) {
break; /* EOF */
}
if ((numGlyphs == 0 && bytesRead == 0) || (numGlyphs > 0 && bytesRead < 9)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
return -1;
}
/* Looking for the colon that separates the codepoint and glyph data at position 2, 4, 6 and 8. */
if (hexBuffer[2] == ':') {
codepointHexSize = 2;
} else if (hexBuffer[4] == ':') {
codepointHexSize = 4;
} else if (hexBuffer[6] == ':') {
codepointHexSize = 6;
} else if (hexBuffer[8] == ':') {
codepointHexSize = 8;
} else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Could not find codepoint and glyph data separator symbol in hex file on line %d.\n", lineNumber);
return -1;
}
if (!validate_hex((const char *)hexBuffer, codepointHexSize, &codepoint)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Malformed hexadecimal number in hex file on line %d.\n", lineNumber);
return -1;
}
if (codepoint > UNIFONT_MAX_CODEPOINT) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "unifont: Codepoint on line %d exceeded limit of 0x%x.\n", lineNumber, UNIFONT_MAX_CODEPOINT);
}
/* If there was glyph data read in the last file read, move it to the front of the buffer. */
bytesOverread = 8 - codepointHexSize;
if (codepointHexSize < 8) {
SDL_memmove(hexBuffer, hexBuffer + codepointHexSize + 1, bytesOverread);
}
bytesRead = SDL_ReadIO(hexFile, hexBuffer + bytesOverread, 33 - bytesOverread);
if (bytesRead < (33 - bytesOverread)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
return -1;
}
if (hexBuffer[32] == '\n') {
glyphWidth = 8;
} else {
glyphWidth = 16;
bytesRead = SDL_ReadIO(hexFile, hexBuffer + 33, 32);
if (bytesRead < 32) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
return -1;
}
}
if (!validate_hex((const char *)hexBuffer, glyphWidth * 4, NULL)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Malformed hexadecimal glyph data in hex file on line %d.\n", lineNumber);
return -1;
}
if (codepoint <= UNIFONT_MAX_CODEPOINT) {
if (unifontGlyph[codepoint].width > 0) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "unifont: Ignoring duplicate codepoint 0x%08" SDL_PRIx32 " in hex file on line %d.\n", codepoint, lineNumber);
} else {
unifontGlyph[codepoint].width = glyphWidth;
/* Pack the hex data into a more compact form. */
for (i = 0; i < glyphWidth * 2; i++) {
unifontGlyph[codepoint].data[i] = dehex2(hexBuffer[i * 2], hexBuffer[i * 2 + 1]);
}
numGlyphs++;
}
}
lineNumber++;
} while (bytesRead > 0);
SDL_CloseIO(hexFile);
SDL_Log("unifont: Loaded %" SDL_PRIu32 " glyphs.\n", numGlyphs);
return 0;
}
static void unifont_make_rgba(const Uint8 *src, Uint8 *dst, Uint8 width)
{
int i, j;
Uint8 *row = dst;
for (i = 0; i < width * 2; i++) {
Uint8 data = src[i];
for (j = 0; j < 8; j++) {
if (data & 0x80) {
row[0] = textColor.r;
row[1] = textColor.g;
row[2] = textColor.b;
row[3] = textColor.a;
} else {
row[0] = 0;
row[1] = 0;
row[2] = 0;
row[3] = 0;
}
data <<= 1;
row += 4;
}
if (width == 8 || (width == 16 && i % 2 == 1)) {
dst += UNIFONT_TEXTURE_PITCH;
row = dst;
}
}
}
static int unifont_load_texture(Uint32 textureID)
{
int i;
Uint8 *textureRGBA;
if (textureID >= UNIFONT_NUM_TEXTURES) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Tried to load out of range texture %" SDL_PRIu32 "\n", textureID);
return -1;
}
textureRGBA = (Uint8 *)SDL_malloc(UNIFONT_TEXTURE_SIZE);
if (!textureRGBA) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d MiB for a texture.\n", UNIFONT_TEXTURE_SIZE / 1024 / 1024);
return -1;
}
SDL_memset(textureRGBA, 0, UNIFONT_TEXTURE_SIZE);
/* Copy the glyphs into memory in RGBA format. */
for (i = 0; i < UNIFONT_GLYPHS_IN_TEXTURE; i++) {
Uint32 codepoint = UNIFONT_GLYPHS_IN_TEXTURE * textureID + i;
if (unifontGlyph[codepoint].width > 0) {
const Uint32 cInTex = codepoint % UNIFONT_GLYPHS_IN_TEXTURE;
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;
unifont_make_rgba(unifontGlyph[codepoint].data, textureRGBA + offset, unifontGlyph[codepoint].width);
}
}
/* Create textures and upload the RGBA data from above. */
for (i = 0; i < state->num_windows; ++i) {
SDL_Renderer *renderer = state->renderers[i];
SDL_Texture *tex = unifontTexture[UNIFONT_NUM_TEXTURES * i + textureID];
if (state->windows[i] == NULL || renderer == NULL || tex != NULL) {
continue;
}
tex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, UNIFONT_TEXTURE_WIDTH, UNIFONT_TEXTURE_WIDTH);
if (tex == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to create texture %" SDL_PRIu32 " for renderer %d.\n", textureID, i);
return -1;
}
unifontTexture[UNIFONT_NUM_TEXTURES * i + textureID] = tex;
SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND);
if (!SDL_UpdateTexture(tex, NULL, textureRGBA, UNIFONT_TEXTURE_PITCH)) {
SDL_Log("unifont error: Failed to update texture %" SDL_PRIu32 " data for renderer %d.\n", textureID, i);
}
}
SDL_free(textureRGBA);
unifontTextureLoaded[textureID] = 1;
return -1;
}
static int unifont_glyph_width(Uint32 codepoint)
{
if (codepoint > UNIFONT_MAX_CODEPOINT ||
unifontGlyph[codepoint].width == 0) {
codepoint = UNIFONT_REPLACEMENT;
}
return unifontGlyph[codepoint].width;
}
static int unifont_draw_glyph(Uint32 codepoint, int rendererID, SDL_FRect *dst)
{
SDL_Texture *texture;
Uint32 textureID;
SDL_FRect srcrect;
srcrect.w = srcrect.h = (float)UNIFONT_GLYPH_SIZE;
if (codepoint > UNIFONT_MAX_CODEPOINT ||
unifontGlyph[codepoint].width == 0) {
codepoint = UNIFONT_REPLACEMENT;
}
textureID = codepoint / UNIFONT_GLYPHS_IN_TEXTURE;
if (!unifontTextureLoaded[textureID]) {
if (unifont_load_texture(textureID) < 0) {
return 0;
}
}
texture = unifontTexture[UNIFONT_NUM_TEXTURES * rendererID + textureID];
if (texture) {
const Uint32 cInTex = codepoint % UNIFONT_GLYPHS_IN_TEXTURE;
srcrect.x = (float)(cInTex % UNIFONT_GLYPHS_IN_ROW * UNIFONT_GLYPH_AREA);
srcrect.y = (float)(cInTex / UNIFONT_GLYPHS_IN_ROW * UNIFONT_GLYPH_AREA);
SDL_RenderTexture(state->renderers[rendererID], texture, &srcrect, dst);
}
return unifontGlyph[codepoint].width;
}
static void unifont_cleanup(void)
{
int i, j;
for (i = 0; i < state->num_windows; ++i) {
SDL_Renderer *renderer = state->renderers[i];
if (state->windows[i] == NULL || !renderer) {
continue;
}
for (j = 0; j < UNIFONT_NUM_TEXTURES; j++) {
SDL_Texture *tex = unifontTexture[UNIFONT_NUM_TEXTURES * i + j];
if (tex) {
SDL_DestroyTexture(tex);
}
}
}
for (j = 0; j < UNIFONT_NUM_TEXTURES; j++) {
unifontTextureLoaded[j] = 0;
}
SDL_free(unifontTexture);
SDL_free(unifontGlyph);
}
/* Unifont code end */
#endif
static size_t utf8_length(unsigned char c)
{
c = (unsigned char)(0xff & c);
if (c < 0x80) {
return 1;
} else if ((c >> 5) == 0x6) {
return 2;
} else if ((c >> 4) == 0xe) {
return 3;
} else if ((c >> 3) == 0x1e) {
return 4;
}
return 0;
}
#ifdef HAVE_SDL_TTF
static char *utf8_next(char *p)
{
size_t len = utf8_length(*p);
size_t i = 0;
if (!len) {
return 0;
}
for (; i < len; ++i) {
++p;
if (!*p) {
return 0;
}
}
return p;
}
static char *utf8_advance(char *p, size_t distance)
{
size_t i = 0;
for (; i < distance && p; ++i) {
p = utf8_next(p);
}
return p;
}
#endif
static Uint32 utf8_decode(const char *p, size_t len)
{
Uint32 codepoint = 0;
size_t i = 0;
if (!len) {
return 0;
}
for (; i < len; ++i) {
if (i == 0) {
codepoint = (0xff >> len) & *p;
} else {
codepoint <<= 6;
codepoint |= 0x3f & *p;
}
if (!*p) {
return 0;
}
p++;
}
return codepoint;
}
static WindowState *GetWindowStateForWindowID(SDL_WindowID windowID)
{
int i;
SDL_Window *window = SDL_GetWindowFromID(windowID);
for (i = 0; i < state->num_windows; ++i) {
if (windowstate[i].window == window) {
return &windowstate[i];
}
}
return NULL;
}
static void InitInput(WindowState *ctx)
{
/* Prepare a rect for text input */
ctx->textRect.x = ctx->textRect.y = 100.0f;
ctx->textRect.w = DEFAULT_WINDOW_WIDTH - 2 * ctx->textRect.x;
ctx->textRect.h = 50.0f;
ctx->markedRect = ctx->textRect;
ctx->text_settings = SDL_CreateProperties();
SDL_StartTextInputWithProperties(ctx->window, ctx->text_settings);
}
static void ClearCandidates(WindowState *ctx)
{
int i;
for (i = 0; i < ctx->num_candidates; ++i) {
SDL_free(ctx->candidates[i]);
}
SDL_free(ctx->candidates);
ctx->candidates = NULL;
ctx->num_candidates = 0;
}
static void SaveCandidates(WindowState *ctx, SDL_Event *event)
{
int i;
ClearCandidates(ctx);
ctx->num_candidates = event->edit_candidates.num_candidates;
if (ctx->num_candidates > 0) {
ctx->candidates = (char **)SDL_malloc(ctx->num_candidates * sizeof(*ctx->candidates));
if (!ctx->candidates) {
ctx->num_candidates = 0;
return;
}
for (i = 0; i < ctx->num_candidates; ++i) {
ctx->candidates[i] = SDL_strdup(event->edit_candidates.candidates[i]);
}
ctx->selected_candidate = event->edit_candidates.selected_candidate;
ctx->horizontal_candidates = event->edit_candidates.horizontal;
}
}
static void DrawCandidates(WindowState *ctx, SDL_FRect *cursorRect)
{
SDL_Renderer *renderer = ctx->renderer;
int rendererID = ctx->rendererID;
int i;
int output_w = 0, output_h = 0;
float w = 0.0f, h = 0.0f;
SDL_FRect candidatesRect, dstRect, underlineRect;
if (ctx->num_candidates == 0) {
return;
}
/* Calculate the size of the candidate list */
for (i = 0; i < ctx->num_candidates; ++i) {
if (!ctx->candidates[i]) {
continue;
}
#ifdef HAVE_SDL_TTF
/* FIXME */
#else
if (ctx->horizontal_candidates) {
const char *utext = ctx->candidates[i];
Uint32 codepoint;
size_t len;
float advance = 0.0f;
if (i > 0) {
advance += unifont_glyph_width(' ') * UNIFONT_DRAW_SCALE;
}
while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) {
advance += unifont_glyph_width(codepoint) * UNIFONT_DRAW_SCALE;
utext += len;
}
w += advance;
h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
} else {
const char *utext = ctx->candidates[i];
Uint32 codepoint;
size_t len;
float advance = 0.0f;
while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) {
advance += unifont_glyph_width(codepoint) * UNIFONT_DRAW_SCALE;
utext += len;
}
w = SDL_max(w, advance);
if (i > 0) {
h += 2.0f;
}
h += UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
}
#endif
}
/* Position the candidate window */
SDL_GetCurrentRenderOutputSize(renderer, &output_w, &output_h);
candidatesRect.x = cursorRect->x;
candidatesRect.y = cursorRect->y + cursorRect->h + 2.0f;
candidatesRect.w = 1.0f + 2.0f + w + 2.0f + 1.0f;
candidatesRect.h = 1.0f + 2.0f + h + 2.0f + 1.0f;
if ((candidatesRect.x + candidatesRect.w) > output_w) {
candidatesRect.x = (output_w - candidatesRect.w);
if (candidatesRect.x < 0.0f) {
candidatesRect.x = 0.0f;
}
}
/* Draw the candidate background */
SDL_SetRenderDrawColor(renderer, 0xAA, 0xAA, 0xAA, 0xFF);
SDL_RenderFillRect(renderer, &candidatesRect);
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderRect(renderer, &candidatesRect);
/* Draw the candidates */
dstRect.x = candidatesRect.x + 3.0f;
dstRect.y = candidatesRect.y + 3.0f;
for (i = 0; i < ctx->num_candidates; ++i) {
if (!ctx->candidates[i]) {
continue;
}
#ifdef HAVE_SDL_TTF
/* FIXME */
#else
dstRect.w = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
dstRect.h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
if (ctx->horizontal_candidates) {
const char *utext = ctx->candidates[i];
Uint32 codepoint;
size_t len;
float start;
if (i > 0) {
dstRect.x += unifont_draw_glyph(' ', rendererID, &dstRect) * UNIFONT_DRAW_SCALE;
}
start = dstRect.x + 2 * unifont_glyph_width(' ') * UNIFONT_DRAW_SCALE;
while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) {
dstRect.x += unifont_draw_glyph(codepoint, rendererID, &dstRect) * UNIFONT_DRAW_SCALE;
utext += len;
}
if (i == ctx->selected_candidate) {
underlineRect.x = start;
underlineRect.y = dstRect.y + dstRect.h - 2;
underlineRect.h = 2;
underlineRect.w = dstRect.x - start;
SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a);
SDL_RenderFillRect(renderer, &underlineRect);
}
} else {
const char *utext = ctx->candidates[i];
Uint32 codepoint;
size_t len;
float start;
dstRect.x = candidatesRect.x + 3.0f;
start = dstRect.x + 2 * unifont_glyph_width(' ') * UNIFONT_DRAW_SCALE;
while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) {
dstRect.x += unifont_draw_glyph(codepoint, rendererID, &dstRect) * UNIFONT_DRAW_SCALE;
utext += len;
}
if (i == ctx->selected_candidate) {
underlineRect.x = start;
underlineRect.y = dstRect.y + dstRect.h - 2;
underlineRect.h = 2;
underlineRect.w = dstRect.x - start;
SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a);
SDL_RenderFillRect(renderer, &underlineRect);
}
if (i > 0) {
dstRect.y += 2.0f;
}
dstRect.y += dstRect.h;
}
#endif
}
}
static void UpdateTextInputArea(WindowState *ctx, const SDL_FRect *cursorRect)
{
SDL_Rect rect;
int cursor_offset = (int)(cursorRect->x - ctx->textRect.x);
rect.x = (int)ctx->textRect.x;
rect.y = (int)ctx->textRect.y;
rect.w = (int)ctx->textRect.w;
rect.h = (int)ctx->textRect.h;
SDL_SetTextInputArea(ctx->window, &rect, cursor_offset);
}
static void CleanupVideo(void)
{
int i;
for (i = 0; i < state->num_windows; ++i) {
WindowState *ctx = &windowstate[i];
SDL_StopTextInput(ctx->window);
ClearCandidates(ctx);
SDL_DestroyProperties(ctx->text_settings);
}
#ifdef HAVE_SDL_TTF
TTF_CloseFont(font);
TTF_Quit();
#else
unifont_cleanup();
#endif
}
static void DrawSettingsButton(WindowState *ctx)
{
SDL_Renderer *renderer = ctx->renderer;
SDL_RenderTexture(renderer, ctx->settings_icon, NULL, &ctx->settings_rect);
}
static void ToggleSettings(WindowState *ctx)
{
if (ctx->settings_visible) {
ctx->settings_visible = false;
SDL_StartTextInputWithProperties(ctx->window, ctx->text_settings);
} else {
SDL_StopTextInput(ctx->window);
ctx->settings_visible = true;
}
}
static int GetDefaultSetting(SDL_PropertiesID props, const char *setting)
{
if (SDL_strcmp(setting, SDL_PROP_TEXTINPUT_TYPE_NUMBER) == 0) {
return SDL_TEXTINPUT_TYPE_TEXT;
}
if (SDL_strcmp(setting, SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER) == 0) {
switch (SDL_GetNumberProperty(props, SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT)) {
case SDL_TEXTINPUT_TYPE_TEXT:
return SDL_CAPITALIZE_SENTENCES;
case SDL_TEXTINPUT_TYPE_TEXT_NAME:
return SDL_CAPITALIZE_WORDS;
default:
return SDL_CAPITALIZE_NONE;
}
}
if (SDL_strcmp(setting, SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN) == 0) {
return true;
}
if (SDL_strcmp(setting, SDL_PROP_TEXTINPUT_MULTILINE_BOOLEAN) == 0) {
return true;
}
SDL_assert(!"Unknown setting");
return 0;
}
static void DrawSettings(WindowState *ctx)
{
SDL_Renderer *renderer = ctx->renderer;
SDL_FRect checkbox;
int i;
checkbox.x = MARGIN;
checkbox.y = MARGIN;
checkbox.w = (float)FONT_CHARACTER_SIZE;
checkbox.h = (float)FONT_CHARACTER_SIZE;
for (i = 0; i < SDL_arraysize(settings); ++i) {
if (settings[i].setting) {
int value = (int)SDL_GetNumberProperty(ctx->text_settings, settings[i].setting, GetDefaultSetting(ctx->text_settings, settings[i].setting));
if (value == settings[i].value) {
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
SDL_RenderFillRect(renderer, &checkbox);
}
SDL_SetRenderDrawColor(renderer, backColor.r, backColor.g, backColor.b, backColor.a);
SDL_RenderRect(renderer, &checkbox);
SDLTest_DrawString(renderer, checkbox.x + checkbox.w + 8.0f, checkbox.y, settings[i].label);
}
checkbox.y += LINE_HEIGHT;
}
}
static void ClickSettings(WindowState *ctx, float x, float y)
{
int setting = (int)SDL_floorf((y - MARGIN) / LINE_HEIGHT);
if (setting >= 0 && setting < SDL_arraysize(settings)) {
SDL_SetNumberProperty(ctx->text_settings, settings[setting].setting, settings[setting].value);
}
}
static void RedrawWindow(WindowState *ctx)
{
SDL_Renderer *renderer = ctx->renderer;
int rendererID = ctx->rendererID;
SDL_FRect drawnTextRect, cursorRect, underlineRect;
char text[MAX_TEXT_LENGTH];
DrawSettingsButton(ctx);
if (ctx->settings_visible) {
DrawSettings(ctx);
return;
}
/* Hide the text if it's a password */
switch ((SDL_TextInputType)SDL_GetNumberProperty(ctx->text_settings, SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT)) {
case SDL_TEXTINPUT_TYPE_TEXT_PASSWORD_HIDDEN:
case SDL_TEXTINPUT_TYPE_NUMBER_PASSWORD_HIDDEN: {
size_t len = SDL_utf8strlen(ctx->text);
SDL_memset(text, '*', len);
text[len] = '\0';
break;
}
default:
SDL_strlcpy(text, ctx->text, sizeof(text));
break;
}
SDL_SetRenderDrawColor(renderer, backColor.r, backColor.g, backColor.b, backColor.a);
SDL_RenderFillRect(renderer, &ctx->textRect);
/* Initialize the drawn text rectangle for the cursor */
drawnTextRect.x = ctx->textRect.x;
drawnTextRect.y = ctx->textRect.y + (ctx->textRect.h - UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE) / 2;
drawnTextRect.w = 0.0f;
drawnTextRect.h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
if (text[0]) {
#ifdef HAVE_SDL_TTF
SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, textColor);
SDL_Texture *texture;
/* Vertically center text */
drawnTextRect.y = ctx->textRect.y + (ctx->textRect.h - textSur->h) / 2;
drawnTextRect.w = textSur->w;
drawnTextRect.h = textSur->h;
texture = SDL_CreateTextureFromSurface(renderer, textSur);
SDL_DestroySurface(textSur);
SDL_RenderTexture(renderer, texture, NULL, &drawnTextRect);
SDL_DestroyTexture(texture);
#else
char *utext = text;
Uint32 codepoint;
size_t len;
SDL_FRect dstrect;
dstrect.x = ctx->textRect.x;
dstrect.y = ctx->textRect.y + (ctx->textRect.h - UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE) / 2;
dstrect.w = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
dstrect.h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
drawnTextRect.y = dstrect.y;
drawnTextRect.h = dstrect.h;
while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) {
float advance = unifont_draw_glyph(codepoint, rendererID, &dstrect) * UNIFONT_DRAW_SCALE;
dstrect.x += advance;
drawnTextRect.w += advance;
utext += len;
}
#endif
}
/* The marked text rectangle is the text area that hasn't been filled by committed text */
ctx->markedRect.x = ctx->textRect.x + drawnTextRect.w;
ctx->markedRect.w = ctx->textRect.w - drawnTextRect.w;
/* Update the drawn text rectangle for composition text, after the committed text */
drawnTextRect.x += drawnTextRect.w;
drawnTextRect.w = 0;
/* Set the cursor to the new location, we'll update it as we go, below */
cursorRect = drawnTextRect;
cursorRect.w = 2;
cursorRect.h = drawnTextRect.h;
if (ctx->markedText[0]) {
#ifdef HAVE_SDL_TTF
SDL_Surface *textSur;
SDL_Texture *texture;
if (ctx->cursor) {
char *p = utf8_advance(ctx->markedText, ctx->cursor);
char c = 0;
if (!p) {
p = &ctx->markedText[SDL_strlen(ctx->markedText)];
}
c = *p;
*p = 0;
TTF_SizeUTF8(font, ctx->markedText, &drawnTextRect.w, NULL);
cursorRect.x += drawnTextRect.w;
*p = c;
}
textSur = TTF_RenderUTF8_Blended(font, ctx->markedText, textColor);
/* Vertically center text */
drawnTextRect.y = ctx->textRect.y + (ctx->textRect.h - textSur->h) / 2;
drawnTextRect.w = textSur->w;
drawnTextRect.h = textSur->h;
texture = SDL_CreateTextureFromSurface(renderer, textSur);
SDL_DestroySurface(textSur);
SDL_RenderTexture(renderer, texture, NULL, &drawnTextRect);
SDL_DestroyTexture(texture);
if (ctx->cursor_length > 0) {
/* FIXME: Need to measure text extents */
cursorRect.w = ctx->cursor_length * UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
}
#else
int i = 0;
char *utext = ctx->markedText;
Uint32 codepoint;
size_t len;
SDL_FRect dstrect;
dstrect.x = drawnTextRect.x;
dstrect.y = ctx->textRect.y + (ctx->textRect.h - UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE) / 2;
dstrect.w = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
dstrect.h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
drawnTextRect.y = dstrect.y;
drawnTextRect.h = dstrect.h;
while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) {
float advance = unifont_draw_glyph(codepoint, rendererID, &dstrect) * UNIFONT_DRAW_SCALE;
dstrect.x += advance;
drawnTextRect.w += advance;
if (i < ctx->cursor) {
cursorRect.x += advance;
}
i++;
utext += len;
}
if (ctx->cursor_length > 0) {
cursorRect.w = ctx->cursor_length * UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
}
#endif
cursorRect.y = drawnTextRect.y;
cursorRect.h = drawnTextRect.h;
underlineRect = ctx->markedRect;
underlineRect.y = drawnTextRect.y + drawnTextRect.h - 2;
underlineRect.h = 2;
underlineRect.w = drawnTextRect.w;
SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a);
SDL_RenderFillRect(renderer, &underlineRect);
}
/* Draw the cursor */
Uint64 now = SDL_GetTicks();
if ((now - ctx->last_cursor_change) >= CURSOR_BLINK_INTERVAL_MS) {
ctx->cursor_visible = !ctx->cursor_visible;
ctx->last_cursor_change = now;
}
if (ctx->cursor_length > 0) {
/* We'll show a highlight */
SDL_SetRenderDrawBlendMode(renderer, highlight_mode);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &cursorRect);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
} else if (ctx->cursor_visible) {
SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a);
SDL_RenderFillRect(renderer, &cursorRect);
}
/* Draw the candidates */
DrawCandidates(ctx, &cursorRect);
/* Update the area used to draw composition UI */
UpdateTextInputArea(ctx, &cursorRect);
}
static void Redraw(void)
{
int i;
for (i = 0; i < state->num_windows; ++i) {
SDL_Renderer *renderer = state->renderers[i];
if (state->windows[i] == NULL) {
continue;
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
RedrawWindow(&windowstate[i]);
SDL_RenderPresent(renderer);
}
}
int main(int argc, char *argv[])
{
bool render_composition = false;
bool render_candidates = false;
int i, done;
SDL_Event event;
char *fontname = NULL;
/* Initialize test framework */
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
if (!state) {
return 1;
}
/* Parse commandline */
for (i = 1; i < argc;) {
int consumed;
consumed = SDLTest_CommonArg(state, i);
if (SDL_strcmp(argv[i], "--font") == 0) {
if (*argv[i + 1]) {
fontname = argv[i + 1];
consumed = 2;
}
} else if (SDL_strcmp(argv[i], "--render-composition") == 0) {
render_composition = true;
consumed = 1;
} else if (SDL_strcmp(argv[i], "--render-candidates") == 0) {
render_candidates = true;
consumed = 1;
}
if (consumed <= 0) {
static const char *options[] = { "[--font fontfile] [--render-composition] [--render-candidates]", NULL };
SDLTest_CommonLogUsage(state, argv[0], options);
return 1;
}
i += consumed;
}
if (render_composition && render_candidates) {
SDL_SetHint(SDL_HINT_IME_IMPLEMENTED_UI, "composition,candidates");
} else if (render_composition) {
SDL_SetHint(SDL_HINT_IME_IMPLEMENTED_UI, "composition");
} else if (render_candidates) {
SDL_SetHint(SDL_HINT_IME_IMPLEMENTED_UI, "candidates");
}
if (!SDLTest_CommonInit(state)) {
return 2;
}
windowstate = (WindowState *)SDL_calloc(state->num_windows, sizeof(*windowstate));
if (!windowstate) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't allocate window state: %s\n", SDL_GetError());
return -1;
}
fontname = GetResourceFilename(fontname, DEFAULT_FONT);
#ifdef HAVE_SDL_TTF
/* Initialize fonts */
TTF_Init();
font = TTF_OpenFont(fontname, DEFAULT_PTSIZE);
if (!font) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to find font: %s\n", SDL_GetError());
return -1;
}
#else
if (unifont_init(fontname) < 0) {
return -1;
}
#endif
SDL_Log("Using font: %s\n", fontname);
/* Initialize window state */
for (i = 0; i < state->num_windows; ++i) {
WindowState *ctx = &windowstate[i];
SDL_Window *window = state->windows[i];
SDL_Renderer *renderer = state->renderers[i];
int icon_w = 0, icon_h = 0;
SDL_SetRenderLogicalPresentation(renderer, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_LOGICAL_PRESENTATION_LETTERBOX, SDL_SCALEMODE_LINEAR);
ctx->window = window;
ctx->renderer = renderer;
ctx->rendererID = i;
ctx->settings_icon = LoadTexture(renderer, "icon.bmp", true, &icon_w, &icon_h);
ctx->settings_rect.x = (float)WINDOW_WIDTH - icon_w - MARGIN;
ctx->settings_rect.y = MARGIN;
ctx->settings_rect.w = (float)icon_w;
ctx->settings_rect.h = (float)icon_h;
InitInput(ctx);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
SDL_RenderClear(renderer);
}
highlight_mode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE_MINUS_DST_COLOR,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_ADD);
/* Main render loop */
done = 0;
while (!done) {
/* Check for events */
while (SDL_PollEvent(&event)) {
SDLTest_CommonEvent(state, &event, &done);
switch (event.type) {
case SDL_EVENT_MOUSE_BUTTON_UP: {
SDL_FPoint point;
WindowState *ctx = GetWindowStateForWindowID(event.button.windowID);
if (!ctx) {
break;
}
SDL_ConvertEventToRenderCoordinates(ctx->renderer, &event);
point.x = event.button.x;
point.y = event.button.y;
if (SDL_PointInRectFloat(&point, &ctx->settings_rect)) {
ToggleSettings(ctx);
} else if (ctx->settings_visible) {
ClickSettings(ctx, point.x, point.y);
}
break;
}
case SDL_EVENT_KEY_DOWN: {
WindowState *ctx = GetWindowStateForWindowID(event.key.windowID);
if (!ctx) {
break;
}
switch (event.key.key) {
case SDLK_RETURN:
ctx->text[0] = 0x00;
break;
case SDLK_BACKSPACE:
/* Only delete text if not in editing mode. */
if (!ctx->markedText[0]) {
size_t textlen = SDL_strlen(ctx->text);
do {
if (textlen == 0) {
break;
}
if (!(ctx->text[textlen - 1] & 0x80)) {
/* One byte */
ctx->text[textlen - 1] = 0x00;
break;
}
if ((ctx->text[textlen - 1] & 0xC0) == 0x80) {
/* Byte from the multibyte sequence */
ctx->text[textlen - 1] = 0x00;
textlen--;
}
if ((ctx->text[textlen - 1] & 0xC0) == 0xC0) {
/* First byte of multibyte sequence */
ctx->text[textlen - 1] = 0x00;
break;
}
} while (1);
}
break;
default:
break;
}
if (done) {
break;
}
SDL_Log("Keyboard: scancode 0x%08X = %s, keycode 0x%08" SDL_PRIX32 " = %s\n",
event.key.scancode,
SDL_GetScancodeName(event.key.scancode),
SDL_static_cast(Uint32, event.key.key),
SDL_GetKeyName(event.key.key));
break;
}
case SDL_EVENT_TEXT_INPUT: {
WindowState *ctx = GetWindowStateForWindowID(event.text.windowID);
if (!ctx) {
break;
}
if (event.text.text[0] == '\0' || event.text.text[0] == '\n' || ctx->markedRect.w < 0) {
break;
}
SDL_Log("Keyboard: text input \"%s\"\n", event.text.text);
if (SDL_strlen(ctx->text) + SDL_strlen(event.text.text) < sizeof(ctx->text)) {
SDL_strlcat(ctx->text, event.text.text, sizeof(ctx->text));
}
SDL_Log("text inputted: %s\n", ctx->text);
/* After text inputted, we can clear up markedText because it */
/* is committed */
ctx->markedText[0] = 0;
break;
}
case SDL_EVENT_TEXT_EDITING: {
WindowState *ctx = GetWindowStateForWindowID(event.edit.windowID);
if (!ctx) {
break;
}
SDL_Log("text editing \"%s\", selected range (%" SDL_PRIs32 ", %" SDL_PRIs32 ")\n",
event.edit.text, event.edit.start, event.edit.length);
SDL_strlcpy(ctx->markedText, event.edit.text, sizeof(ctx->markedText));
ctx->cursor = event.edit.start;
ctx->cursor_length = event.edit.length;
break;
}
case SDL_EVENT_TEXT_EDITING_CANDIDATES: {
WindowState *ctx = GetWindowStateForWindowID(event.edit.windowID);
if (!ctx) {
break;
}
SDL_Log("text candidates:\n");
for (i = 0; i < event.edit_candidates.num_candidates; ++i) {
SDL_Log("%c%s\n", i == event.edit_candidates.selected_candidate ? '>' : ' ', event.edit_candidates.candidates[i]);
}
ClearCandidates(ctx);
SaveCandidates(ctx, &event);
break;
}
default:
break;
}
}
Redraw();
}
SDL_free(fontname);
CleanupVideo();
SDLTest_CommonQuit(state);
return 0;
}