From ee3ba7392c930367fd4e70d0af95d2fbb9b88b2a Mon Sep 17 00:00:00 2001 From: Hanspeter Portner Date: Tue, 13 Apr 2021 23:51:00 +0200 Subject: [PATCH] Squashed 'subprojects/nk_pugl/' changes from 383d2f42..2f43d058 2f43d058 redesign nk_pugl_get_scale API for standalone usage. b2593ae8 Use pugl's native Display handle. git-subtree-dir: subprojects/nk_pugl git-subtree-split: 2f43d0588f1930c70a832f7e00d15bfb1a5c8e94 --- VERSION | 2 +- example/example.c | 8 ++-- nk_pugl/nk_pugl.h | 117 +++++++++++++++++++++------------------------- 3 files changed, 58 insertions(+), 69 deletions(-) diff --git a/VERSION b/VERSION index ae51b49..d3bfa59 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.169 +0.1.173 diff --git a/example/example.c b/example/example.c index 1c505da..c55a2ca 100644 --- a/example/example.c +++ b/example/example.c @@ -53,8 +53,10 @@ main(int argc __attribute__((unused)), char **argv __attribute__((unused))) static nk_pugl_window_t win; nk_pugl_config_t *cfg = &win.cfg; - cfg->width = 1280; - cfg->height = 720; + const float scale = nk_pugl_get_scale(); + + cfg->width = 1280 * scale; + cfg->height = 720 * scale; cfg->resizable = true; cfg->parent = 0; cfg->threads = false; @@ -64,7 +66,7 @@ main(int argc __attribute__((unused)), char **argv __attribute__((unused))) cfg->expose = _expose; cfg->data = NULL; cfg->font.face = "./Cousine-Regular.ttf"; - cfg->font.size = 13; + cfg->font.size = 13 * scale; signal(SIGTERM, _sigint); signal(SIGINT, _sigint); diff --git a/nk_pugl/nk_pugl.h b/nk_pugl/nk_pugl.h index 43b124e..58fce93 100644 --- a/nk_pugl/nk_pugl.h +++ b/nk_pugl/nk_pugl.h @@ -107,7 +107,6 @@ struct _nk_pugl_config_t { struct _nk_pugl_window_t { nk_pugl_config_t cfg; char urn [46]; - float scale; PuglWorld *world; PuglView *view; @@ -134,7 +133,6 @@ struct _nk_pugl_window_t { PuglMod state; #if !defined(__APPLE__) && !defined(_WIN32) atomic_flag async; - Display *disp; #endif }; @@ -184,7 +182,7 @@ NK_PUGL_API const char * nk_pugl_paste_from_clipboard(nk_pugl_window_t *win, size_t *len); NK_PUGL_API float -nk_pugl_get_scale(nk_pugl_window_t *win); +nk_pugl_get_scale(); #ifdef __cplusplus } @@ -451,7 +449,7 @@ _nk_pugl_font_init(nk_pugl_window_t *win) { nk_pugl_config_t *cfg = &win->cfg; - const int font_size = cfg->font.size * win->scale; + const int font_size = cfg->font.size; // init nuklear font struct nk_font *ttf = NULL; @@ -969,57 +967,8 @@ nk_pugl_init(nk_pugl_window_t *win) nk_pugl_config_t *cfg = &win->cfg; struct nk_convert_config *conv = &win->conv; - const char *NK_SCALE = getenv("NK_SCALE"); - const float scale = NK_SCALE ? atof(NK_SCALE) : 1.f; - const float dpi0 = 96.f; // reference DPI we're designing for - -#if defined(__APPLE__) - const float dpi1 = dpi0; //TODO implement this -#elif defined(_WIN32) - // GetDpiForSystem/Monitor/Window is Win10 only - HDC screen = GetDC(NULL); - const float dpi1 = GetDeviceCaps(screen, LOGPIXELSX); - ReleaseDC(NULL, screen); -#else - win->async = (atomic_flag)ATOMIC_FLAG_INIT; - win->disp = XOpenDisplay(0); - // modern X actually lies here, but proprietary nvidia - float dpi1 = XDisplayWidth(win->disp, 0) * 25.4f / XDisplayWidthMM(win->disp, 0); - - // read DPI from users's ~/.Xresources - char *resource_string = XResourceManagerString(win->disp); - XrmInitialize(); - if(resource_string) - { - XrmDatabase db = XrmGetStringDatabase(resource_string); - if(db) - { - char *type = NULL; - XrmValue value; - - XrmGetResource(db, "Xft.dpi", "String", &type, &value); - if(value.addr) - { - dpi1 = atof(value.addr); - } - - XrmDestroyDatabase(db); - } - } -#endif - - win->scale = scale * dpi1 / dpi0; - if(win->scale < 0.5) - { - win->scale = 0.5; - } win->has_left = true; - cfg->width *= win->scale; - cfg->height *= win->scale; - cfg->min_width *= win->scale; - cfg->min_height *= win->scale; - // init pugl win->world = puglNewWorld(cfg->parent ? PUGL_MODULE : PUGL_PROGRAM, cfg->threads ? PUGL_WORLD_THREADS : 0); @@ -1166,13 +1115,6 @@ nk_pugl_shutdown(nk_pugl_window_t *win) puglFreeWorld(win->world); } - -#if !defined(__APPLE__) && !defined(_WIN32) - if(win->disp) - { - XCloseDisplay(win->disp); - } -#endif } NK_PUGL_API void @@ -1242,10 +1184,11 @@ nk_pugl_async_redisplay(nk_pugl_window_t *win) const int status = SendNotifyMessage(widget, WM_PAINT, 0, 0); (void)status; #else + Display *disp = puglGetNativeWorld(win->world); const Window widget = (Window)win->widget; XExposeEvent xevent = { .type = Expose, - .display = win->disp, + .display = disp, .window = widget }; @@ -1254,10 +1197,10 @@ nk_pugl_async_redisplay(nk_pugl_window_t *win) // spin } - const Status status = XSendEvent(win->disp, widget, false, ExposureMask, + const Status status = XSendEvent(disp, widget, false, ExposureMask, (XEvent *)&xevent); (void)status; - XFlush(win->disp); + XFlush(disp); atomic_flag_clear_explicit(&win->async, memory_order_release); #endif @@ -1377,9 +1320,53 @@ nk_pugl_paste_from_clipboard(nk_pugl_window_t *win, size_t *len) } NK_PUGL_API float -nk_pugl_get_scale(nk_pugl_window_t *win) +nk_pugl_get_scale() { - return win->scale; + const char *NK_SCALE = getenv("NK_SCALE"); + const float scale = NK_SCALE ? atof(NK_SCALE) : 1.f; + const float dpi0 = 96.f; // reference DPI we're designing for + float dpi1 = dpi0; + +#if defined(__APPLE__) + // FIXME +#elif defined(_WIN32) + // GetDpiForSystem/Monitor/Window is Win10 only + HDC screen = GetDC(NULL); + dpi1 = GetDeviceCaps(screen, LOGPIXELSX); + ReleaseDC(NULL, screen); +#else + Display *disp = XOpenDisplay(0); + if(disp) + { + // modern X actually lies here, but proprietary nvidia + dpi1 = XDisplayWidth(disp, 0) * 25.4f / XDisplayWidthMM(disp, 0); + + // read DPI from users's ~/.Xresources + char *resource_string = XResourceManagerString(disp); + XrmInitialize(); + if(resource_string) + { + XrmDatabase db = XrmGetStringDatabase(resource_string); + if(db) + { + char *type = NULL; + XrmValue value; + + XrmGetResource(db, "Xft.dpi", "String", &type, &value); + if(value.addr) + { + dpi1 = atof(value.addr); + } + + XrmDestroyDatabase(db); + } + } + + XCloseDisplay(disp); + } +#endif + + return scale * dpi1 / dpi0; } #ifdef __cplusplus -- 2.38.5