Add basic assertions for event API arguments

This commit is contained in:
Camilla Löwy 2021-12-22 14:14:24 +01:00
parent 05f6c13d11
commit ad3dbeb65f
3 changed files with 101 additions and 3 deletions

View File

@ -44,6 +44,13 @@
#define _GLFW_JOYSTICK_BUTTON 2
#define _GLFW_JOYSTICK_HATBIT 3
#define GLFW_MOD_MASK (GLFW_MOD_SHIFT | \
GLFW_MOD_CONTROL | \
GLFW_MOD_ALT | \
GLFW_MOD_SUPER | \
GLFW_MOD_CAPS_LOCK | \
GLFW_MOD_NUM_LOCK)
// Initializes the platform joystick API if it has not been already
//
static GLFWbool initJoysticks(void)
@ -266,6 +273,12 @@ static GLFWbool parseMapping(_GLFWmapping* mapping, const char* string)
//
void _glfwInputKey(_GLFWwindow* window, int key, int scancode, int action, int mods)
{
assert(window != NULL);
assert(key >= 0 || key == GLFW_KEY_UNKNOWN);
assert(key <= GLFW_KEY_LAST);
assert(action == GLFW_PRESS || action == GLFW_RELEASE);
assert(mods == (mods & GLFW_MOD_MASK));
if (key >= 0 && key <= GLFW_KEY_LAST)
{
GLFWbool repeated = GLFW_FALSE;
@ -297,6 +310,10 @@ void _glfwInputKey(_GLFWwindow* window, int key, int scancode, int action, int m
//
void _glfwInputChar(_GLFWwindow* window, uint32_t codepoint, int mods, GLFWbool plain)
{
assert(window != NULL);
assert(mods == (mods & GLFW_MOD_MASK));
assert(plain == GLFW_TRUE || plain == GLFW_FALSE);
if (codepoint < 32 || (codepoint > 126 && codepoint < 160))
return;
@ -317,6 +334,12 @@ void _glfwInputChar(_GLFWwindow* window, uint32_t codepoint, int mods, GLFWbool
//
void _glfwInputScroll(_GLFWwindow* window, double xoffset, double yoffset)
{
assert(window != NULL);
assert(xoffset > -FLT_MAX);
assert(xoffset < FLT_MAX);
assert(yoffset > -FLT_MAX);
assert(yoffset < FLT_MAX);
if (window->callbacks.scroll)
window->callbacks.scroll((GLFWwindow*) window, xoffset, yoffset);
}
@ -325,6 +348,12 @@ void _glfwInputScroll(_GLFWwindow* window, double xoffset, double yoffset)
//
void _glfwInputMouseClick(_GLFWwindow* window, int button, int action, int mods)
{
assert(window != NULL);
assert(button >= 0);
assert(button <= GLFW_MOUSE_BUTTON_LAST);
assert(action == GLFW_PRESS || action == GLFW_RELEASE);
assert(mods == (mods & GLFW_MOD_MASK));
if (button < 0 || button > GLFW_MOUSE_BUTTON_LAST)
return;
@ -345,6 +374,12 @@ void _glfwInputMouseClick(_GLFWwindow* window, int button, int action, int mods)
//
void _glfwInputCursorPos(_GLFWwindow* window, double xpos, double ypos)
{
assert(window != NULL);
assert(xpos > -FLT_MAX);
assert(xpos < FLT_MAX);
assert(ypos > -FLT_MAX);
assert(ypos < FLT_MAX);
if (window->virtualCursorPosX == xpos && window->virtualCursorPosY == ypos)
return;
@ -359,6 +394,9 @@ void _glfwInputCursorPos(_GLFWwindow* window, double xpos, double ypos)
//
void _glfwInputCursorEnter(_GLFWwindow* window, GLFWbool entered)
{
assert(window != NULL);
assert(entered == GLFW_TRUE || entered == GLFW_FALSE);
if (window->callbacks.cursorEnter)
window->callbacks.cursorEnter((GLFWwindow*) window, entered);
}
@ -367,6 +405,10 @@ void _glfwInputCursorEnter(_GLFWwindow* window, GLFWbool entered)
//
void _glfwInputDrop(_GLFWwindow* window, int count, const char** paths)
{
assert(window != NULL);
assert(count > 0);
assert(paths != NULL);
if (window->callbacks.drop)
window->callbacks.drop((GLFWwindow*) window, count, paths);
}
@ -375,7 +417,8 @@ void _glfwInputDrop(_GLFWwindow* window, int count, const char** paths)
//
void _glfwInputJoystick(_GLFWjoystick* js, int event)
{
const int jid = (int) (js - _glfw.joysticks);
assert(js != NULL);
assert(event == GLFW_CONNECTED || event == GLFW_DISCONNECTED);
if (event == GLFW_CONNECTED)
js->connected = GLFW_TRUE;
@ -383,13 +426,17 @@ void _glfwInputJoystick(_GLFWjoystick* js, int event)
js->connected = GLFW_FALSE;
if (_glfw.callbacks.joystick)
_glfw.callbacks.joystick(jid, event);
_glfw.callbacks.joystick((int) (js - _glfw.joysticks), event);
}
// Notifies shared code of the new value of a joystick axis
//
void _glfwInputJoystickAxis(_GLFWjoystick* js, int axis, float value)
{
assert(js != NULL);
assert(axis >= 0);
assert(axis < js->axisCount);
js->axes[axis] = value;
}
@ -397,6 +444,11 @@ void _glfwInputJoystickAxis(_GLFWjoystick* js, int axis, float value)
//
void _glfwInputJoystickButton(_GLFWjoystick* js, int button, char value)
{
assert(js != NULL);
assert(button >= 0);
assert(button < js->buttonCount);
assert(value == GLFW_PRESS || value == GLFW_RELEASE);
js->buttons[button] = value;
}
@ -404,7 +456,18 @@ void _glfwInputJoystickButton(_GLFWjoystick* js, int button, char value)
//
void _glfwInputJoystickHat(_GLFWjoystick* js, int hat, char value)
{
const int base = js->buttonCount + hat * 4;
int base;
assert(js != NULL);
assert(hat >= 0);
assert(hat < js->hatCount);
// Valid hat values only use the least significant nibble and have at most two bits
// set, which can be considered adjacent plus an arbitrary rotation within the nibble
assert((value & 0xf0) == 0);
assert((value & ((value << 2) | (value >> 2))) == 0);
base = js->buttonCount + hat * 4;
js->buttons[base + 0] = (value & 0x01) ? GLFW_PRESS : GLFW_RELEASE;
js->buttons[base + 1] = (value & 0x02) ? GLFW_PRESS : GLFW_RELEASE;

View File

@ -96,6 +96,10 @@ static GLFWbool refreshVideoModes(_GLFWmonitor* monitor)
//
void _glfwInputMonitor(_GLFWmonitor* monitor, int action, int placement)
{
assert(monitor != NULL);
assert(action == GLFW_CONNECTED || action == GLFW_DISCONNECTED);
assert(placement == _GLFW_INSERT_FIRST || placement == _GLFW_INSERT_LAST);
if (action == GLFW_CONNECTED)
{
_glfw.monitorCount++;
@ -155,6 +159,7 @@ void _glfwInputMonitor(_GLFWmonitor* monitor, int action, int placement)
//
void _glfwInputMonitorWindow(_GLFWmonitor* monitor, _GLFWwindow* window)
{
assert(monitor != NULL);
monitor->window = window;
}

View File

@ -44,6 +44,9 @@
//
void _glfwInputWindowFocus(_GLFWwindow* window, GLFWbool focused)
{
assert(window != NULL);
assert(focused == GLFW_TRUE || focused == GLFW_FALSE);
if (window->callbacks.focus)
window->callbacks.focus((GLFWwindow*) window, focused);
@ -73,6 +76,8 @@ void _glfwInputWindowFocus(_GLFWwindow* window, GLFWbool focused)
//
void _glfwInputWindowPos(_GLFWwindow* window, int x, int y)
{
assert(window != NULL);
if (window->callbacks.pos)
window->callbacks.pos((GLFWwindow*) window, x, y);
}
@ -82,6 +87,10 @@ void _glfwInputWindowPos(_GLFWwindow* window, int x, int y)
//
void _glfwInputWindowSize(_GLFWwindow* window, int width, int height)
{
assert(window != NULL);
assert(width >= 0);
assert(height >= 0);
if (window->callbacks.size)
window->callbacks.size((GLFWwindow*) window, width, height);
}
@ -90,6 +99,9 @@ void _glfwInputWindowSize(_GLFWwindow* window, int width, int height)
//
void _glfwInputWindowIconify(_GLFWwindow* window, GLFWbool iconified)
{
assert(window != NULL);
assert(iconified == GLFW_TRUE || iconified == GLFW_FALSE);
if (window->callbacks.iconify)
window->callbacks.iconify((GLFWwindow*) window, iconified);
}
@ -98,6 +110,9 @@ void _glfwInputWindowIconify(_GLFWwindow* window, GLFWbool iconified)
//
void _glfwInputWindowMaximize(_GLFWwindow* window, GLFWbool maximized)
{
assert(window != NULL);
assert(maximized == GLFW_TRUE || maximized == GLFW_FALSE);
if (window->callbacks.maximize)
window->callbacks.maximize((GLFWwindow*) window, maximized);
}
@ -107,6 +122,10 @@ void _glfwInputWindowMaximize(_GLFWwindow* window, GLFWbool maximized)
//
void _glfwInputFramebufferSize(_GLFWwindow* window, int width, int height)
{
assert(window != NULL);
assert(width >= 0);
assert(height >= 0);
if (window->callbacks.fbsize)
window->callbacks.fbsize((GLFWwindow*) window, width, height);
}
@ -116,6 +135,12 @@ void _glfwInputFramebufferSize(_GLFWwindow* window, int width, int height)
//
void _glfwInputWindowContentScale(_GLFWwindow* window, float xscale, float yscale)
{
assert(window != NULL);
assert(xscale > 0.f);
assert(xscale < FLT_MAX);
assert(yscale > 0.f);
assert(yscale < FLT_MAX);
if (window->callbacks.scale)
window->callbacks.scale((GLFWwindow*) window, xscale, yscale);
}
@ -124,6 +149,8 @@ void _glfwInputWindowContentScale(_GLFWwindow* window, float xscale, float yscal
//
void _glfwInputWindowDamage(_GLFWwindow* window)
{
assert(window != NULL);
if (window->callbacks.refresh)
window->callbacks.refresh((GLFWwindow*) window);
}
@ -132,6 +159,8 @@ void _glfwInputWindowDamage(_GLFWwindow* window)
//
void _glfwInputWindowCloseRequest(_GLFWwindow* window)
{
assert(window != NULL);
window->shouldClose = GLFW_TRUE;
if (window->callbacks.close)
@ -142,6 +171,7 @@ void _glfwInputWindowCloseRequest(_GLFWwindow* window)
//
void _glfwInputWindowMonitor(_GLFWwindow* window, _GLFWmonitor* monitor)
{
assert(window != NULL);
window->monitor = monitor;
}