diff options
author | David Robillard <d@drobilla.net> | 2019-08-04 20:19:01 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2019-09-03 08:34:39 +0200 |
commit | b0ac6dcb492b68404d800fe8ed0c7393d487fa4b (patch) | |
tree | 41d0050ef6d33bfe9f2186195168e36f8a8b8f3a | |
parent | 075c5c5927e511dd03d9608a285ed58ef395120b (diff) | |
download | pugl-b0ac6dcb492b68404d800fe8ed0c7393d487fa4b.tar.gz pugl-b0ac6dcb492b68404d800fe8ed0c7393d487fa4b.tar.bz2 pugl-b0ac6dcb492b68404d800fe8ed0c7393d487fa4b.zip |
Add puglSetClassName()
-rw-r--r-- | pugl/detail/implementation.c | 29 | ||||
-rw-r--r-- | pugl/detail/types.h | 2 | ||||
-rw-r--r-- | pugl/detail/win.c | 10 | ||||
-rw-r--r-- | pugl/detail/win.h | 2 | ||||
-rw-r--r-- | pugl/detail/x11.c | 3 | ||||
-rw-r--r-- | pugl/pugl.h | 27 | ||||
-rw-r--r-- | test/pugl_cairo_test.c | 2 | ||||
-rw-r--r-- | test/pugl_test.c | 4 |
8 files changed, 50 insertions, 29 deletions
diff --git a/pugl/detail/implementation.c b/pugl/detail/implementation.c index a64e6fd..1f027a9 100644 --- a/pugl/detail/implementation.c +++ b/pugl/detail/implementation.c @@ -26,6 +26,15 @@ #include <string.h> static void +puglSetString(char** dest, const char* string) +{ + const size_t len = strlen(string); + + *dest = (char*)realloc(*dest, len + 1); + strncpy(*dest, string, len + 1); +} + +static void puglSetDefaultHints(PuglHints hints) { hints[PUGL_USE_COMPAT_PROFILE] = PUGL_TRUE; @@ -54,6 +63,7 @@ puglNewWorld(void) } world->startTime = puglGetTime(world); + puglSetString(&world->className, "Pugl"); return world; } @@ -62,10 +72,18 @@ void puglFreeWorld(PuglWorld* const world) { puglFreeWorldInternals(world); + free(world->className); free(world->views); free(world); } +PuglStatus +puglSetClassName(PuglWorld* const world, const char* const name) +{ + puglSetString(&world->className, name); + return PUGL_SUCCESS; +} + PuglView* puglNewView(PuglWorld* const world) { @@ -109,7 +127,6 @@ puglFreeView(PuglView* view) } puglFreeViewInternals(view); - free(view->windowClass); free(view); } @@ -128,16 +145,6 @@ puglInitWindowHint(PuglView* view, PuglWindowHint hint, int value) } void -puglInitWindowClass(PuglView* view, const char* name) -{ - const size_t len = strlen(name); - - free(view->windowClass); - view->windowClass = (char*)calloc(1, len + 1); - memcpy(view->windowClass, name, len); -} - -void puglInitWindowParent(PuglView* view, PuglNativeWindow parent) { view->parent = parent; diff --git a/pugl/detail/types.h b/pugl/detail/types.h index 24cf219..a413848 100644 --- a/pugl/detail/types.h +++ b/pugl/detail/types.h @@ -52,7 +52,6 @@ struct PuglViewImpl { PuglInternals* impl; PuglHandle handle; PuglEventFunc eventFunc; - char* windowClass; PuglNativeWindow parent; uintptr_t transientParent; PuglHints hints; @@ -70,6 +69,7 @@ struct PuglViewImpl { /** Cross-platform world definition. */ struct PuglWorldImpl { PuglWorldInternals* impl; + char* className; double startTime; size_t numViews; PuglView** views; diff --git a/pugl/detail/win.c b/pugl/detail/win.c index 96642cc..efd6070 100644 --- a/pugl/detail/win.c +++ b/pugl/detail/win.c @@ -51,8 +51,6 @@ typedef BOOL (WINAPI *PFN_SetProcessDPIAware)(void); -static const TCHAR* DEFAULT_CLASSNAME = "Pugl"; - LRESULT CALLBACK wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); @@ -140,9 +138,7 @@ puglCreateWindow(PuglView* view, const char* title) { PuglInternals* impl = view->impl; - const char* className = view->windowClass ? view->windowClass : DEFAULT_CLASSNAME; - - title = title ? title : "Window"; + title = title ? title : view->world->className; // Get refresh rate for resize draw timer DEVMODEA devMode = {0}; @@ -150,7 +146,7 @@ puglCreateWindow(PuglView* view, const char* title) view->impl->refreshRate = devMode.dmDisplayFrequency; // Register window class if necessary - if (!puglRegisterWindowClass(className)) { + if (!puglRegisterWindowClass(view->world->className)) { return 1; } @@ -202,7 +198,6 @@ puglFreeViewInternals(PuglView* view) view->backend->destroy(view); ReleaseDC(view->impl->hwnd, view->impl->hdc); DestroyWindow(view->impl->hwnd); - UnregisterClass(view->windowClass ? view->windowClass : DEFAULT_CLASSNAME, NULL); free(view->impl); } } @@ -210,6 +205,7 @@ puglFreeViewInternals(PuglView* view) void puglFreeWorldInternals(PuglWorld* world) { + UnregisterClass(world->className, NULL); free(world->impl); } diff --git a/pugl/detail/win.h b/pugl/detail/win.h index 8d6ce12..b9e554c 100644 --- a/pugl/detail/win.h +++ b/pugl/detail/win.h @@ -90,7 +90,7 @@ puglWinCreateWindow(const PuglView* const view, HWND* const hwnd, HDC* const hdc) { - const char* className = view->windowClass ? view->windowClass : "Pugl"; + const char* className = (const char*)view->world->className; const unsigned winFlags = puglWinGetWindowFlags(view); const unsigned winExFlags = puglWinGetWindowExFlags(view); diff --git a/pugl/detail/x11.c b/pugl/detail/x11.c index ea8367c..f000b02 100644 --- a/pugl/detail/x11.c +++ b/pugl/detail/x11.c @@ -211,6 +211,9 @@ puglCreateWindow(PuglView* view, const char* title) XSizeHints sizeHints = getSizeHints(view); XSetNormalHints(display, win, &sizeHints); + XClassHint classHint = { world->className, world->className }; + XSetClassHint(display, win, &classHint); + if (title) { XStoreName(display, win, title); XChangeProperty(display, win, atoms->NET_WM_NAME, diff --git a/pugl/pugl.h b/pugl/pugl.h index 017338c..112b11b 100644 --- a/pugl/pugl.h +++ b/pugl/pugl.h @@ -454,6 +454,18 @@ PUGL_API void puglFreeWorld(PuglWorld* world); /** + Set the class name of the application. + + This is a stable identifier for the application, used as the window + class/instance name on X11 and Windows. It is not displayed to the user, + but can be used in scripts and by window managers, so it should be the same + for every instance of the application, but different from other + applications. +*/ +PUGL_API PuglStatus +puglSetClassName(PuglWorld* world, const char* name); + +/** Return the time in seconds. This is a monotonically increasing clock with high resolution. The returned @@ -523,12 +535,6 @@ PUGL_API void puglInitWindowHint(PuglView* view, PuglWindowHint hint, int value); /** - Set the window class name before creating a window. -*/ -PUGL_API void -puglInitWindowClass(PuglView* view, const char* name); - -/** Set the parent window before creating a window (for embedding). */ PUGL_API void @@ -797,6 +803,15 @@ puglDestroy(PuglView* view) } /** + Set the window class name before creating a window. +*/ +static inline PUGL_DEPRECATED_BY("puglSetClassName") void +puglInitWindowClass(PuglView* view, const char* name) +{ + puglSetClassName(puglGetWorld(view), name); +} + +/** Set the window size before creating a window. @deprecated Use puglSetFrame(). diff --git a/test/pugl_cairo_test.c b/test/pugl_cairo_test.c index c396dad..52cbbbd 100644 --- a/test/pugl_cairo_test.c +++ b/test/pugl_cairo_test.c @@ -206,10 +206,10 @@ main(int argc, char** argv) } world = puglNewWorld(); + puglSetClassName(world, "PuglCairoTest"); PuglRect frame = { 0, 0, 512, 512 }; PuglView* view = puglNewView(world); - puglInitWindowClass(view, "PuglCairoTest"); puglSetFrame(view, frame); puglSetMinSize(view, 256, 256); puglInitWindowHint(view, PUGL_RESIZABLE, resizable); diff --git a/test/pugl_test.c b/test/pugl_test.c index 285c5d9..e291df7 100644 --- a/test/pugl_test.c +++ b/test/pugl_test.c @@ -314,8 +314,9 @@ main(int argc, char** argv) app.parent = puglNewView(app.world); app.child = puglNewView(app.world); + puglSetClassName(app.world, "Pugl Test"); + const PuglRect parentFrame = { 0, 0, 512, 512 }; - puglInitWindowClass(app.parent, "PuglTest"); puglSetFrame(app.parent, parentFrame); puglSetMinSize(app.parent, borderWidth * 3, borderWidth * 3); puglSetAspectRatio(app.parent, 1, 1, 16, 9); @@ -336,7 +337,6 @@ main(int argc, char** argv) return 1; } - puglInitWindowClass(app.child, "PuglTest"); puglSetFrame(app.child, getChildFrame(parentFrame)); puglInitWindowParent(app.child, puglGetNativeWindow(app.parent)); |