From e756ae1d854107fb911ed866f519fa95a6ac9b5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Thu, 13 May 2021 17:32:31 +0200 Subject: [PATCH] Make monitor and joystick names static strings Related to #478. (cherry picked from commit da236ba807b73fb8050e52f1f1450266e7529659) --- src/cocoa_monitor.m | 8 ++++---- src/input.c | 3 +-- src/internal.h | 4 ++-- src/monitor.c | 4 +--- src/wl_monitor.c | 6 ++---- 5 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/cocoa_monitor.m b/src/cocoa_monitor.m index 55638cf0..6271828e 100644 --- a/src/cocoa_monitor.m +++ b/src/cocoa_monitor.m @@ -63,7 +63,7 @@ static char* getMonitorName(CGDirectDisplayID displayID, NSScreen* screen) &it) != 0) { // This may happen if a desktop Mac is running headless - return NULL; + return _glfw_strdup("Display"); } while ((service = IOIteratorNext(it)) != 0) @@ -101,7 +101,7 @@ static char* getMonitorName(CGDirectDisplayID displayID, NSScreen* screen) { _glfwInputError(GLFW_PLATFORM_ERROR, "Cocoa: Failed to find service port for display"); - return NULL; + return _glfw_strdup("Display"); } CFDictionaryRef names = @@ -114,7 +114,7 @@ static char* getMonitorName(CGDirectDisplayID displayID, NSScreen* screen) { // This may happen if a desktop Mac is running headless CFRelease(info); - return NULL; + return _glfw_strdup("Display"); } const CFIndex size = @@ -356,7 +356,7 @@ void _glfwPollMonitorsNS(void) const CGSize size = CGDisplayScreenSize(displays[i]); char* name = getMonitorName(displays[i], screen); if (!name) - name = _glfw_strdup("Unknown"); + continue; _GLFWmonitor* monitor = _glfwAllocMonitor(name, size.width, size.height); monitor->ns.displayID = displays[i]; diff --git a/src/input.c b/src/input.c index 337d5cf0..56277ca4 100644 --- a/src/input.c +++ b/src/input.c @@ -430,7 +430,6 @@ _GLFWjoystick* _glfwAllocJoystick(const char* name, js = _glfw.joysticks + jid; js->present = GLFW_TRUE; - js->name = _glfw_strdup(name); js->axes = calloc(axisCount, sizeof(float)); js->buttons = calloc(buttonCount + (size_t) hatCount * 4, 1); js->hats = calloc(hatCount, 1); @@ -438,6 +437,7 @@ _GLFWjoystick* _glfwAllocJoystick(const char* name, js->buttonCount = buttonCount; js->hatCount = hatCount; + strncpy(js->name, name, sizeof(js->name) - 1); strncpy(js->guid, guid, sizeof(js->guid) - 1); js->mapping = findValidMapping(js); @@ -448,7 +448,6 @@ _GLFWjoystick* _glfwAllocJoystick(const char* name, // void _glfwFreeJoystick(_GLFWjoystick* js) { - free(js->name); free(js->axes); free(js->buttons); free(js->hats); diff --git a/src/internal.h b/src/internal.h index 91631c06..2a5668af 100644 --- a/src/internal.h +++ b/src/internal.h @@ -423,7 +423,7 @@ struct _GLFWwindow // struct _GLFWmonitor { - char* name; + char name[128]; void* userPointer; // Physical dimensions in millimeters. @@ -484,7 +484,7 @@ struct _GLFWjoystick int buttonCount; unsigned char* hats; int hatCount; - char* name; + char name[128]; void* userPointer; char guid[33]; _GLFWmapping* mapping; diff --git a/src/monitor.c b/src/monitor.c index c6bcfd39..73411412 100644 --- a/src/monitor.c +++ b/src/monitor.c @@ -170,8 +170,7 @@ _GLFWmonitor* _glfwAllocMonitor(const char* name, int widthMM, int heightMM) monitor->widthMM = widthMM; monitor->heightMM = heightMM; - if (name) - monitor->name = _glfw_strdup(name); + strncpy(monitor->name, name, sizeof(monitor->name) - 1); return monitor; } @@ -189,7 +188,6 @@ void _glfwFreeMonitor(_GLFWmonitor* monitor) _glfwFreeGammaArrays(&monitor->currentRamp); free(monitor->modes); - free(monitor->name); free(monitor); } diff --git a/src/wl_monitor.c b/src/wl_monitor.c index a7b05c60..d1448a24 100644 --- a/src/wl_monitor.c +++ b/src/wl_monitor.c @@ -47,15 +47,13 @@ static void outputHandleGeometry(void* data, int32_t transform) { struct _GLFWmonitor *monitor = data; - char name[1024]; monitor->wl.x = x; monitor->wl.y = y; monitor->widthMM = physicalWidth; monitor->heightMM = physicalHeight; - snprintf(name, sizeof(name), "%s %s", make, model); - monitor->name = _glfw_strdup(name); + snprintf(monitor->name, sizeof(monitor->name), "%s %s", make, model); } static void outputHandleMode(void* data, @@ -133,7 +131,7 @@ void _glfwAddOutputWayland(uint32_t name, uint32_t version) } // The actual name of this output will be set in the geometry handler. - monitor = _glfwAllocMonitor(NULL, 0, 0); + monitor = _glfwAllocMonitor("", 0, 0); output = wl_registry_bind(_glfw.wl.registry, name,