8000 Add macros to set calling conventions for callbacks. by seanmiddleditch · Pull Request #904 · ocornut/imgui · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add macros to set calling conventions for callbacks. #904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,9 +712,9 @@ static bool DataTypeApplyOpFromText(const char* buf, const char* ini
// Platform dependent default implementations
//-----------------------------------------------------------------------------

static const char* GetClipboardTextFn_DefaultImpl(void* user_data);
static void SetClipboardTextFn_DefaultImpl(void* user_data, const char* text);
static void ImeSetInputScreenPosFn_DefaultImpl(int x, int y);
static const char* IMGUI_CALLBACK GetClipboardTextFn_DefaultImpl(void* user_data);
static void IMGUI_CALLBACK SetClipboardTextFn_DefaultImpl(void* user_data, const char* text);
static void IMGUI_CALLBACK ImeSetInputScreenPosFn_DefaultImpl(int x, int y);

//-----------------------------------------------------------------------------
// Context
Expand Down Expand Up @@ -835,6 +835,7 @@ ImGuiIO::ImGuiIO()

// User functions
RenderDrawListsFn = NULL;
UserData = NULL;
MemAllocFn = malloc;
MemFreeFn = free;
GetClipboardTextFn = GetClipboardTextFn_DefaultImpl; // Platform dependent default implementations
Expand Down Expand Up @@ -2047,7 +2048,7 @@ void ImGui::SetCurrentContext(ImGuiContext* ctx)
GImGui = ctx;
}

ImGuiContext* ImGui::CreateContext(void* (*malloc_fn)(size_t), void (*free_fn)(void*))
ImGuiContext* ImGui::CreateContext(void* (IMGUI_CALLBACK *malloc_fn)(size_t), void (IMGUI_CALLBACK *free_fn)(void*))
{
if (!malloc_fn) malloc_fn = malloc;
ImGuiContext* ctx = (ImGuiContext*)malloc_fn(sizeof(ImGuiContext));
Expand All @@ -2059,7 +2060,7 @@ ImGuiContext* ImGui::CreateContext(void* (*malloc_fn)(size_t), void (*free_fn)(v

void ImGui::DestroyContext(ImGuiContext* ctx)
{
void (*free_fn)(void*) = ctx->IO.MemFreeFn;
void (IMGUI_CALLBACK *free_fn)(void*) = ctx->IO.MemFreeFn;
ctx->~ImGuiContext();
free_fn(ctx);
if (GImGui == ctx)
Expand Down Expand Up @@ -2521,7 +2522,7 @@ static void MarkSettingsDirty()
}

// FIXME: Add a more explicit sort order in the window structure.
static int ChildWindowComparer(const void* lhs, const void* rhs)
static int IMGUI_CALLBACK ChildWindowComparer(const void* lhs, const void* rhs)
{
const ImGuiWindow* a = *(const ImGuiWindow**)lhs;
const ImGuiWindow* b = *(const ImGuiWindow**)rhs;
Expand Down Expand Up @@ -9588,7 +9589,7 @@ void ImGui::ValueColor(const char* prefix, ImU32 v)
#pragma comment(lib, "user32")
#endif

static const char* GetClipboardTextFn_DefaultImpl(void*)
static const char* IMGUI_CALLBACK GetClipboardTextFn_DefaultImpl(void*)
{
static ImVector<char> buf_local;
buf_local.clear();
Expand All @@ -9608,7 +9609,7 @@ static const char* GetClipboardTextFn_DefaultImpl(void*)
return buf_local.Data;
}

static void SetClipboardTextFn_DefaultImpl(void*, const char* text)
static void IMGUI_CALLBACK SetClipboardTextFn_DefaultImpl(void*, const char* text)
{
if (!OpenClipboard(NULL))
return;
Expand Down Expand Up @@ -9657,7 +9658,7 @@ static void SetClipboardTextFn_DefaultImpl(void*, const char* text)
#pragma comment(lib, "imm32")
#endif

static void ImeSetInputScreenPosFn_DefaultImpl(int x, int y)
static void IMGUI_CALLBACK ImeSetInputScreenPosFn_DefaultImpl(int x, int y)
{
// Notify OS Input Method Editor of text input position
if (HWND hwnd = (HWND)GImGui->IO.ImeWindowHandle)
Expand All @@ -9673,7 +9674,7 @@ static void ImeSetInputScreenPosFn_DefaultImpl(int x, int y)

#else

static void ImeSetInputScreenPosFn_DefaultImpl(int, int) {}
static void IMGUI_CALLBACK ImeSetInputScreenPosFn_DefaultImpl(int, int) {}

#endif

Expand Down
21 changes: 14 additions & 7 deletions imgui.h
< 8000 tr data-hunk="93ce4934e2cf0ed1cec939db5efdbf211c79c37a196be235631a281264f917a4" class="show-top-border">
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
#define IMGUI_API
#endif

// Calling conventions
#if !defined(IMGUI_CALLBACK) && defined(_WIN32)
#define IMGUI_CALLBACK __cdecl
#else
#define IMGUI_CALLBACK
#endif

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confused about how this blocks work, wouldn't statement in the else override whatever the user has specified?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty define result in replacement with nothing.

aka static const char* IMGUI_CALLBACK GetClipboardTextFn_DefaultImpl(void* user_data); turns into static const char* GetClipboardTextFn_DefaultImpl(void* user_data); letting the default calling conv take over.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but isn't the point that the user might want to define IMGUI_CALLBACK to something that's not your default?

Shouldn't the correct test should just be:

#ifndef IMGUI_CALLBACK
#define IMGUI_CALLBACK
#endif

Let the user do what they want and not involve _WIN32 there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're on _WIN32, __cdecl is the correct default. It's the Windows standard callback ABI that is used by all callbacks in Microsoft's headers (they have a CALLBACK macro that does the same thing here). If you decide to use a different ABI for callbacks on Windows, several of the default callbacks in imgui will become illegal.

This whole thing is basically a Windows-ism, so it really does make since to only do this for _WIN32, and even supporting non-default ABIs like we do is more of a super advanced need that could probably just be removed.

A large and ever better fix here would be for imgui to follow the standard Windows practice of using a macro to declare the ABI for every non-inline function so that imgui is not affected by default ABI settings. That would allow imgui and the host app to be compiled with different defaults and still have everything link and run. That is not essential to fixing the bug this PR is address, though.

// Define assertion handler.
#ifndef IM_ASSERT
#include <assert.h>
Expand Down Expand Up @@ -457,7 +464,7 @@ namespace ImGui
// Internal context access - if you want to use multiple context, share context between modules (e.g. DLL). There is a default context created and active by default.
// All contexts share a same ImFontAtlas by default. If you want different font atlas, you can new() them and overwrite the GetIO().Fonts variable of an ImGui context.
IMGUI_API const char* GetVersion();
IMGUI_API ImGuiContext* CreateContext(void* (*malloc_fn)(size_t) = NULL, void (*free_fn)(void*) = NULL);
IMGUI_API ImGuiContext* CreateContext(void* (IMGUI_CALLBACK *malloc_fn)(size_t) = NULL, void (IMGUI_CALLBACK *free_fn)(void*) = NULL);
IMGUI_API void DestroyContext(ImGuiContext* ctx);
IMGUI_API ImGuiContext* GetCurrentContext();
IMGUI_API void SetCurrentContext(ImGuiContext* ctx);
Expand Down Expand Up @@ -754,22 +761,22 @@ struct ImGuiIO
// Rendering function, will be called in Render().
// Alternatively you can keep this to NULL and call GetDrawData() after Render() to get the same pointer.
// See example applications if you are unsure of how to implement this.
void (*RenderDrawListsFn)(ImDrawData* data);
void (IMGUI_CALLBACK *RenderDrawListsFn)(ImDrawData* data);

// Optional: access OS clipboard
// (default to use native Win32 clipboard on Windows, otherwise uses a private clipboard. Override to access OS clipboard on other architectures)
const char* (*GetClipboardTextFn)(void* user_data);
void (*SetClipboardTextFn)(void* user_data, const char* text);
const char* (IMGUI_CALLBACK *GetClipboardTextFn)(void* user_data);
void (IMGUI_CALLBACK *SetClipboardTextFn)(void* user_data, const char* text);
void* ClipboardUserData;

// Optional: override memory allocations. MemFreeFn() may be called with a NULL pointer.
// (default to posix malloc/free)
void* (*MemAllocFn)(size_t sz);
void (*MemFreeFn)(void* ptr);
void* (IMGUI_CALLBACK *MemAllocFn)(size_t sz);
void (IMGUI_CALLBACK *MemFreeFn)(void* ptr);

// Optional: notify OS Input Method Editor of the screen position of your cursor for text input position (e.g. when using Japanese/Chinese IME in Windows)
// (default to use native imm32 api on Windows)
void (*ImeSetInputScreenPosFn)(int x, int y);
void (IMGUI_CALLBACK *ImeSetInputScreenPosFn)(int x, int y);
void* ImeWindowHandle; // (Windows) Set this to your HWND to get automatic IME cursor positioning.

//------------------------------------------------------------------
Expand Down
12 changes: 9 additions & 3 deletions stb_rect_pack.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
#define STBRP_DEF extern
#endif

#if !defined(STBRP_QSORT_CALLBACK) && defined(_WIN32)
#define STBRP_QSORT_CALLBACK __cdecl
#else
#define STBRP_QSORT_CALLBACK
#endif

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -509,7 +515,7 @@ static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context *context, i
return res;
}

static int rect_height_compare(const void *a, const void *b)
static int STBRP_QSORT_CALLBACK rect_height_compare(const void *a, const void *b)
{
const stbrp_rect *p = (const stbrp_rect *) a;
const stbrp_rect *q = (const stbrp_rect *) b;
Expand All @@ -520,7 +526,7 @@ static int rect_height_compare(const void *a, const void *b)
return (p->w > q->w) ? -1 : (p->w < q->w);
}

static int rect_width_compare(const void *a, const void *b)
static int STBRP_QSORT_CALLBACK rect_width_compare(const void *a, const void *b)
{
const stbrp_rect *p = (const stbrp_rect *) a;
const stbrp_rect *q = (const stbrp_rect *) b;
Expand All @@ -531,7 +537,7 @@ static int rect_width_compare(const void *a, const void *b)
return (p->h > q->h) ? -1 : (p->h < q->h);
}

static int rect_original_order(const void *a, const void *b)
static int STBRP_QSORT_CALLBACK rect_original_order(const void *a, const void *b)
{
const stbrp_rect *p = (const stbrp_rect *) a;
const stbrp_rect *q = (const stbrp_rect *) b;
Expand Down
0