From e4e2c1232ff6980a54f3f3bf5a3395c21079f5ef Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Thu, 23 Apr 2015 22:32:58 -0400 Subject: [PATCH] Started readding the Windows backend. Still not too sure about uiOSContainer... --- new/windows/alloc.c | 49 ++++++++++++++++++++++++++++++++++++++ new/windows/text.c | 57 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 new/windows/alloc.c create mode 100644 new/windows/text.c diff --git a/new/windows/alloc.c b/new/windows/alloc.c new file mode 100644 index 00000000..147c90d2 --- /dev/null +++ b/new/windows/alloc.c @@ -0,0 +1,49 @@ +// 4 december 2014 +#include "uipriv_windows.h" + +// wrappers for allocator of choice +// panics on memory exhausted, undefined on heap corruption or other unreliably-detected malady (see http://stackoverflow.com/questions/28761680/is-there-a-windows-api-memory-allocator-deallocator-i-can-use-that-will-just-giv) +// new memory is set to zero +// passing NULL to tableRealloc() acts like tableAlloc() +// passing NULL to tableFree() is a no-op + +void *uiAlloc(size_t size, const char *type) +{ + void *out; + + out = malloc(size); + if (out == NULL) { + fprintf(stderr, "memory exhausted in uiAlloc() allocating %s\n", type); + abort(); + } + ZeroMemory(out, size); + if (options.debugLogAllocations) + fprintf(stderr, "%p alloc %s\n", out, type); + return out; +} + +void *uiRealloc(void *p, size_t size, const char *type) +{ + void *out; + + if (p == NULL) + return uiAlloc(size, type); + out = realloc(p, size); + if (out == NULL) { + fprintf(stderr, "memory exhausted in uiRealloc() reallocating %s\n", type); + abort(); + } + // TODO zero the extra memory + if (options.debugLogAllocations) + fprintf(stderr, "%p realloc %p\n", p, out); + return out; +} + +void uiFree(void *p) +{ + if (p == NULL) + return; + free(p); + if (options.debugLogAllocations) + fprintf(stderr, "%p free\n", p); +} diff --git a/new/windows/text.c b/new/windows/text.c new file mode 100644 index 00000000..9594b79c --- /dev/null +++ b/new/windows/text.c @@ -0,0 +1,57 @@ +// 9 april 2015 +#include "uipriv_windows.h" + +// TODO figure out how to integreate HRESULT into this + +// see http://stackoverflow.com/a/29556509/3408572 + +#define MBTWC(str, wstr, bufsiz) MultiByteToWideChar(CP_UTF8, 0, str, -1, wstr, bufsiz) + +WCHAR *toUTF16(const char *str) +{ + WCHAR *wstr; + int n; + + n = MBTWC(str, NULL, 0); + if (n == 0) + logLastError("error figuring out number of characters to convert to in toUTF16()"); + wstr = (WCHAR *) uiAlloc(n * sizeof (WCHAR), "WCHAR[]"); + if (MBTWC(str, wstr, n) != n) + logLastError("error converting from UTF-8 to UTF-16 in toUTF16()"); + return wstr; +} + +#define WCTMB(wstr, str, bufsiz) WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, bufsiz, NULL, NULL) + +char *toUTF8(const WCHAR *wstr) +{ + char *str; + int n; + + n = WCTMB(wstr, NULL, 0); + if (n == 0) + logLastError("error figuring out number of characters to convert to in toUTF8()"); + str = (char *) uiAlloc(n * sizeof (char), "char[]"); + if (WCTMB(wstr, str, n) != n) + logLastError("error converting from UTF-16 to UTF-8 in toUTFF8()"); + return str; +} + +WCHAR *windowText(HWND hwnd) +{ + LRESULT n; + WCHAR *text; + + n = SendMessageW(hwnd, WM_GETTEXTLENGTH, 0, 0); + // WM_GETTEXTLENGTH does not include the null terminator + text = (WCHAR *) uiAlloc((n + 1) * sizeof (WCHAR), "WCHAR[]"); + // note the comparison: the size includes the null terminator, but the return does not + if (GetWindowTextW(hwnd, text, n + 1) != n) + logLastError("error getting window text in windowText()"); + return text; +} + +void uiFreeText(char *text) +{ + uiFree(text); +}