Skip to content

Commit

Permalink
process configure event (resize) in lazy mode
Browse files Browse the repository at this point in the history
  • Loading branch information
fangyidong committed May 17, 2024
1 parent de4807e commit 5edc147
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/ant.hwi/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local default_renderer <const> = {
macos = "METAL",
ios = "METAL",
android = "VULKAN",
linux = "VULKAN",
}

local function check_renderer(renderer)
Expand Down
50 changes: 48 additions & 2 deletions pkg/ant.window/src/platform/linux/x11_window.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include <stdio.h>
#include <cstdio>
#include <thread>
#include <chrono>

#include <X11/keysymdef.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
Expand All @@ -9,6 +12,8 @@
#include "imgui.h"
#include "../../window.h"

#define CONFIG_EVENT_MAX_WAIT_TIME (5) // ms

struct WindowContext
{
Display *dpy{NULL};
Expand All @@ -31,9 +36,28 @@ struct Rect
int h;
};

struct ConfigEventState
{
bool received;
int64_t received_time;
int x;
int y;
int w;
int h;
};

static WindowContext s_win_ctx;
static ThreadContext s_thread_ctx;
static bee::thread_handle s_thread_h;
static ConfigEventState s_config_event = {};

static int64_t now_ms()
{
struct timeval now;
gettimeofday(&now, 0);
int64_t t = now.tv_sec * INT64_C(1000) + now.tv_usec / 1000;
return t;
}

static ImGuiKey ToImGuiKey(const KeySym &keysym)
{
Expand Down Expand Up @@ -353,13 +377,35 @@ static void x_run(void *_userData) noexcept

while (true)
{
// process configure event data in lazy mode to avoid frequently calling window_message_size when resizing
if (!XPending(ctx->dpy) && s_config_event.received)
{
auto event_wait_time = now_ms() - s_config_event.received_time;
if (event_wait_time > CONFIG_EVENT_MAX_WAIT_TIME)
{
s_config_event.received = false;
s_config_event.received_time = 0;
window_message_size(L, s_config_event.w, s_config_event.h);
}
else
{
std::this_thread::sleep_for(std::chrono::milliseconds(CONFIG_EVENT_MAX_WAIT_TIME));
continue;
}
}

XNextEvent(ctx->dpy, &event);

switch (event.type)
{
case ConfigureNotify:
{
window_message_size(L, event.xconfigure.width, event.xconfigure.height);
s_config_event.received = true;
s_config_event.received_time = now_ms();
s_config_event.w = event.xconfigure.width;
s_config_event.h = event.xconfigure.height;
s_config_event.x = event.xconfigure.x;
s_config_event.y = event.xconfigure.y;
}
break;

Expand Down

0 comments on commit 5edc147

Please sign in to comment.