This commit is contained in:
Bayemite 2021-08-16 09:07:11 -04:00 committed by GitHub
commit 8222fc363b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 220 additions and 1 deletions

View File

@ -159,6 +159,10 @@ if (_GLFW_X11)
# Set up library and include paths
list(APPEND glfw_INCLUDE_DIRS "${X11_X11_INCLUDE_PATH}")
if (NOT X11_X11_INCLUDE_PATH)
message(FATAL_ERROR "X111 headers not found; install libx11 development package")
endif()
# Check for XRandR (modern resolution switching and gamma control)
if (NOT X11_Xrandr_INCLUDE_PATH)
message(FATAL_ERROR "RandR headers not found; install libxrandr development package")
@ -188,6 +192,8 @@ if (_GLFW_X11)
if (NOT X11_Xshape_INCLUDE_PATH)
message(FATAL_ERROR "X Shape headers not found; install libxext development package")
endif()
list(APPEND glfw_LIBRARIES ${X11_X11_LIB})
endif()
#--------------------------------------------------------------------

View File

@ -129,6 +129,7 @@ information on what to include when reporting a bug.
values to select ANGLE backend (#1380)
- Added `GLFW_X11_XCB_VULKAN_SURFACE` init hint for selecting X11 Vulkan
surface extension (#1793)
- Added `glfwGetWindowTitle` function for GLFWwindow for querying window titles
- Made joystick subsystem initialize at first use (#1284,#1646)
- Made `GLFW_DOUBLEBUFFER` a read-only window attribute
- Updated the minimum required CMake version to 3.1

View File

@ -2895,6 +2895,28 @@ GLFWAPI int glfwWindowShouldClose(GLFWwindow* window);
*/
GLFWAPI void glfwSetWindowShouldClose(GLFWwindow* window, int value);
/*! @brief Retrieves the title of the specified window.
*
* This function gets the window title, encoded as UTF-8, of the specified
* window.
*
* @param[in] window The window to query.
* @return A copy of the UTF-8 encoded window title, or NULL if an error has occurred.
*
* @errors Possible errors include @ref GLFW_NOT_INITIALIZED and @ref
* GLFW_PLATFORM_ERROR.
*
* @remark Do not forget to free the returned char* when you are done with it.
*
* @thread_safety This function must only be called from the main thread.
*
* @sa @ref window_title
* @sa @ref glfwSetWindowTitle
*
* @ingroup window
*/
GLFWAPI char* glfwGetWindowTitle(GLFWwindow* window);
/*! @brief Sets the title of the specified window.
*
* This function sets the window title, encoded as UTF-8, of the specified
@ -2912,6 +2934,7 @@ GLFWAPI void glfwSetWindowShouldClose(GLFWwindow* window, int value);
* @thread_safety This function must only be called from the main thread.
*
* @sa @ref window_title
* @sa @ref glfwGetWindowTitle
*
* @since Added in version 1.0.
* @glfw3 Added window handle parameter.

View File

@ -977,6 +977,11 @@ void _glfwPlatformDestroyWindow(_GLFWwindow* window)
} // autoreleasepool
}
char* _glfwPlatformGetWindowTitle(_GLFWwindow* window)
{
return NULL;
}
void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title)
{
@autoreleasepool {

View File

@ -650,6 +650,7 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window,
const _GLFWctxconfig* ctxconfig,
const _GLFWfbconfig* fbconfig);
void _glfwPlatformDestroyWindow(_GLFWwindow* window);
char* _glfwPlatformGetWindowTitle(_GLFWwindow* window);
void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title);
void _glfwPlatformSetWindowIcon(_GLFWwindow* window,
int count, const GLFWimage* images);

View File

@ -150,6 +150,11 @@ void _glfwPlatformDestroyWindow(_GLFWwindow* window)
window->context.destroy(window);
}
char* _glfwPlatformGetWindowTitle(_GLFWwindow* window)
{
return NULL;
}
void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title)
{
}

View File

@ -1462,6 +1462,44 @@ void _glfwPlatformDestroyWindow(_GLFWwindow* window)
DestroyIcon(window->win32.smallIcon);
}
char* _glfwPlatformGetWindowTitle(_GLFWwindow* window)
{
int count;
SetLastError(0);
count = GetWindowTextLengthW(window->win32.handle);
if(count == 0)
{
int error;
SetLastError(0);
error = GetLastError();
if(error != 0)
{
_glfwInputErrorWin32(GLFW_PLATFORM_ERROR, "Win32: Querying window title failed");
return NULL;
}
else
return calloc(1, sizeof(char)); // single \0
}
else
{
WCHAR* wideTitle;
char* title;
count += 1; // the \0
wideTitle = calloc(count, sizeof(WCHAR));
GetWindowTextW(window->win32.handle, wideTitle, count);
title = _glfwCreateUTF8FromWideStringWin32(wideTitle);
if(!title)
return calloc(1, sizeof(char)); // single \0
return title;
}
}
void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title)
{
WCHAR* wideTitle = _glfwCreateWideStringFromUTF8Win32(title);

View File

@ -501,6 +501,15 @@ GLFWAPI void glfwSetWindowShouldClose(GLFWwindow* handle, int value)
window->shouldClose = value;
}
GLFWAPI char* glfwGetWindowTitle(GLFWwindow* handle)
{
_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
return _glfwPlatformGetWindowTitle(window);
}
GLFWAPI void glfwSetWindowTitle(GLFWwindow* handle, const char* title)
{
_GLFWwindow* window = (_GLFWwindow*) handle;

View File

@ -886,6 +886,13 @@ void _glfwPlatformDestroyWindow(_GLFWwindow* window)
free(window->wl.monitors);
}
char* _glfwPlatformGetWindowTitle(_GLFWwindow* window)
{
if(window->wl.title)
return _glfw_strdup(window->wl.title);
return calloc(1, sizeof(char));
}
void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title)
{
if (window->wl.title)

View File

@ -31,7 +31,8 @@
#include <X11/cursorfont.h>
#include <X11/Xmd.h>
#include <X11/Xutil.h>
#include <X11/Xlib.h>
#include <sys/select.h>
#include <string.h>
@ -2074,6 +2075,60 @@ void _glfwPlatformDestroyWindow(_GLFWwindow* window)
XFlush(_glfw.x11.display);
}
char* _glfwPlatformGetWindowTitle(_GLFWwindow* window)
{
// Uses XGetWMName instead of XFetchName (which occasionally fails for some reason)
XTextProperty textProperty;
int len;
char** charList = NULL;
char* title;
_glfwGrabErrorHandlerX11();
if (XGetWMName(_glfw.x11.display, window->x11.handle, &textProperty) == 0)
{
_glfwInputErrorX11(GLFW_PLATFORM_ERROR, "X11: Could not get window title");
_glfwReleaseErrorHandlerX11();
return NULL;
}
int ret = Xutf8TextPropertyToTextList(_glfw.x11.display, &textProperty, &charList, &len);
if (ret != Success)
{
_glfwReleaseErrorHandlerX11();
if (ret == XNoMemory)
_glfwInputErrorX11(GLFW_PLATFORM_ERROR, "X11: No memory to convert window title to UTF-8");
else if (ret == XLocaleNotSupported || ret == XConverterNotFound)
_glfwInputErrorX11(GLFW_PLATFORM_ERROR, "X11: Cannot convert window title, unsupported locale");
if (textProperty.value != NULL)
XFree(textProperty.value);
return NULL;
}
if (len < 1) // empty title
{
_glfwReleaseErrorHandlerX11();
if (textProperty.value != NULL)
XFree(textProperty.value);
if (charList != NULL)
XFreeStringList(charList);
return calloc(1, sizeof(char));
}
title = _glfw_strdup(charList[0]);
_glfwReleaseErrorHandlerX11();
if (textProperty.value != NULL)
XFree(textProperty.value);
if (charList != NULL)
XFreeStringList(charList);
return title;
}
void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title)
{
if (_glfw.x11.xlib.utf8)

21
testing/CMakeLists.txt Normal file
View File

@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 3.18)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
project(test
VERSION 1.0
LANGUAGES CXX)
add_executable(test
test.cpp
)
find_package(OpenGL REQUIRED)
set(GLFW_BUILD_DOCS OFF)
set(USE_MSVC_RUNTIME_LIBRARY_DLL OFF)
add_subdirectory(D:/src/glfw ${CMAKE_BINARY_DIR}/glfw-build)
target_include_directories(test PUBLIC D:/src/glfw/include ${OPENGL_INCLUDE_DIRS})
target_link_libraries(test glfw ${OPENGL_LIBRARIES})

48
testing/test.cpp Normal file
View File

@ -0,0 +1,48 @@
#include <GLFW/glfw3.h>
#include <iostream>
void printWindowTitle(GLFWwindow *window)
{
std::cout << "The window title should be '" << glfwGetWindowTitle(window) << "'.\n";
}
void windowShow(GLFWwindow *window)
{
printWindowTitle(window);
while (!glfwWindowShouldClose(window))
{
glfwWaitEvents();
}
glfwSetWindowShouldClose(window, GLFW_FALSE);
}
int main()
{
if (!glfwInit())
{
std::cerr << "Could not initialise glfw.\n";
return -1;
}
GLFWwindow *window = glfwCreateWindow(800, 600, "Initial title", NULL, NULL);
windowShow(window);
glfwSetWindowTitle(window, "");
windowShow(window);
glfwSetWindowTitle(window, "Potato's are cool");
windowShow(window);
glfwSetWindowTitle(window, u8"😀 😃 😄 😁");
windowShow(window);
glfwDestroyWindow(window);
window = glfwCreateWindow(800, 600, "", NULL, NULL);
windowShow(window);
glfwTerminate();
return 0;
}