glfw/tests/events.c

409 lines
14 KiB
C
Raw Normal View History

2010-09-07 15:34:51 +00:00
//========================================================================
// Event linter (event spewer)
// Copyright (c) Camilla Berglund <elmindreda@elmindreda.org>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
//
// This test hooks every available callback and outputs their arguments
//
// Log messages go to stdout, error messages to stderr
//
// Every event also gets a (sequential) number to aid discussion of logs
//
//========================================================================
2011-07-27 15:09:17 +00:00
#define _CRT_SECURE_NO_WARNINGS
#include <GL/glfw3.h>
2010-09-07 15:34:51 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <locale.h>
// These must match the input mode defaults
2012-02-07 16:19:38 +00:00
static GLboolean keyrepeat = GL_FALSE;
static GLboolean systemkeys = GL_TRUE;
static GLboolean closeable = GL_TRUE;
// Event index
2010-09-07 15:34:51 +00:00
static unsigned int counter = 0;
// Should we keep running?
static GLboolean running = GL_TRUE;
2010-09-07 15:34:51 +00:00
static const char* get_key_name(int key)
{
switch (key)
{
// Printable keys
case GLFW_KEY_A: return "A";
case GLFW_KEY_B: return "B";
case GLFW_KEY_C: return "C";
case GLFW_KEY_D: return "D";
case GLFW_KEY_E: return "E";
case GLFW_KEY_F: return "F";
case GLFW_KEY_G: return "G";
case GLFW_KEY_H: return "H";
case GLFW_KEY_I: return "I";
case GLFW_KEY_J: return "J";
case GLFW_KEY_K: return "K";
case GLFW_KEY_L: return "L";
case GLFW_KEY_M: return "M";
case GLFW_KEY_N: return "N";
case GLFW_KEY_O: return "O";
case GLFW_KEY_P: return "P";
case GLFW_KEY_Q: return "Q";
case GLFW_KEY_R: return "R";
case GLFW_KEY_S: return "S";
case GLFW_KEY_T: return "T";
case GLFW_KEY_U: return "U";
case GLFW_KEY_V: return "V";
case GLFW_KEY_W: return "W";
case GLFW_KEY_X: return "X";
case GLFW_KEY_Y: return "Y";
case GLFW_KEY_Z: return "Z";
case GLFW_KEY_1: return "1";
case GLFW_KEY_2: return "2";
case GLFW_KEY_3: return "3";
case GLFW_KEY_4: return "4";
case GLFW_KEY_5: return "5";
case GLFW_KEY_6: return "6";
case GLFW_KEY_7: return "7";
case GLFW_KEY_8: return "8";
case GLFW_KEY_9: return "9";
case GLFW_KEY_0: return "0";
case GLFW_KEY_SPACE: return "SPACE";
case GLFW_KEY_MINUS: return "MINUS";
case GLFW_KEY_EQUAL: return "EQUAL";
case GLFW_KEY_LEFT_BRACKET: return "LEFT BRACKET";
case GLFW_KEY_RIGHT_BRACKET: return "RIGHT BRACKET";
case GLFW_KEY_BACKSLASH: return "BACKSLASH";
case GLFW_KEY_SEMICOLON: return "SEMICOLON";
case GLFW_KEY_APOSTROPHE: return "APOSTROPHE";
case GLFW_KEY_GRAVE_ACCENT: return "GRAVE ACCENT";
case GLFW_KEY_COMMA: return "COMMA";
case GLFW_KEY_PERIOD: return "PERIOD";
case GLFW_KEY_SLASH: return "SLASH";
case GLFW_KEY_WORLD_1: return "WORLD 1";
case GLFW_KEY_WORLD_2: return "WORLD 2";
// Function keys
case GLFW_KEY_ESCAPE: return "ESCAPE";
2010-09-07 15:34:51 +00:00
case GLFW_KEY_F1: return "F1";
case GLFW_KEY_F2: return "F2";
case GLFW_KEY_F3: return "F3";
case GLFW_KEY_F4: return "F4";
case GLFW_KEY_F5: return "F5";
case GLFW_KEY_F6: return "F6";
case GLFW_KEY_F7: return "F7";
case GLFW_KEY_F8: return "F8";
case GLFW_KEY_F9: return "F9";
case GLFW_KEY_F10: return "F10";
case GLFW_KEY_F11: return "F11";
case GLFW_KEY_F12: return "F12";
case GLFW_KEY_F13: return "F13";
case GLFW_KEY_F14: return "F14";
case GLFW_KEY_F15: return "F15";
case GLFW_KEY_F16: return "F16";
case GLFW_KEY_F17: return "F17";
case GLFW_KEY_F18: return "F18";
case GLFW_KEY_F19: return "F19";
case GLFW_KEY_F20: return "F20";
case GLFW_KEY_F21: return "F21";
case GLFW_KEY_F22: return "F22";
case GLFW_KEY_F23: return "F23";
case GLFW_KEY_F24: return "F24";
case GLFW_KEY_F25: return "F25";
case GLFW_KEY_UP: return "UP";
case GLFW_KEY_DOWN: return "DOWN";
case GLFW_KEY_LEFT: return "LEFT";
case GLFW_KEY_RIGHT: return "RIGHT";
case GLFW_KEY_LEFT_SHIFT: return "LEFT SHIFT";
case GLFW_KEY_RIGHT_SHIFT: return "RIGHT SHIFT";
case GLFW_KEY_LEFT_CONTROL: return "LEFT CONTROL";
case GLFW_KEY_RIGHT_CONTROL: return "RIGHT CONTROL";
case GLFW_KEY_LEFT_ALT: return "LEFT ALT";
case GLFW_KEY_RIGHT_ALT: return "RIGHT ALT";
case GLFW_KEY_TAB: return "TAB";
case GLFW_KEY_ENTER: return "ENTER";
case GLFW_KEY_BACKSPACE: return "BACKSPACE";
case GLFW_KEY_INSERT: return "INSERT";
case GLFW_KEY_DELETE: return "DELETE";
case GLFW_KEY_PAGE_UP: return "PAGE UP";
case GLFW_KEY_PAGE_DOWN: return "PAGE DOWN";
case GLFW_KEY_HOME: return "HOME";
case GLFW_KEY_END: return "END";
case GLFW_KEY_KP_0: return "KEYPAD 0";
case GLFW_KEY_KP_1: return "KEYPAD 1";
case GLFW_KEY_KP_2: return "KEYPAD 2";
case GLFW_KEY_KP_3: return "KEYPAD 3";
case GLFW_KEY_KP_4: return "KEYPAD 4";
case GLFW_KEY_KP_5: return "KEYPAD 5";
case GLFW_KEY_KP_6: return "KEYPAD 6";
case GLFW_KEY_KP_7: return "KEYPAD 7";
case GLFW_KEY_KP_8: return "KEYPAD 8";
case GLFW_KEY_KP_9: return "KEYPAD 9";
case GLFW_KEY_KP_DIVIDE: return "KEYPAD DIVIDE";
case GLFW_KEY_KP_MULTIPLY: return "KEYPAD MULTPLY";
case GLFW_KEY_KP_SUBTRACT: return "KEYPAD SUBTRACT";
case GLFW_KEY_KP_ADD: return "KEYPAD ADD";
case GLFW_KEY_KP_DECIMAL: return "KEYPAD DECIMAL";
case GLFW_KEY_KP_EQUAL: return "KEYPAD EQUAL";
case GLFW_KEY_KP_ENTER: return "KEYPAD ENTER";
case GLFW_KEY_NUM_LOCK: return "NUM LOCK";
case GLFW_KEY_CAPS_LOCK: return "CAPS LOCK";
case GLFW_KEY_SCROLL_LOCK: return "SCROLL LOCK";
case GLFW_KEY_PAUSE: return "PAUSE";
case GLFW_KEY_LEFT_SUPER: return "LEFT SUPER";
case GLFW_KEY_RIGHT_SUPER: return "RIGHT SUPER";
case GLFW_KEY_MENU: return "MENU";
2010-09-07 15:34:51 +00:00
default: return NULL;
}
2010-09-07 15:34:51 +00:00
}
static const char* get_action_name(int action)
{
switch (action)
{
case GLFW_PRESS:
return "pressed";
2010-09-07 15:34:51 +00:00
case GLFW_RELEASE:
return "released";
2010-09-07 15:34:51 +00:00
}
return "caused unknown action";
}
static const char* get_button_name(int button)
{
switch (button)
{
case GLFW_MOUSE_BUTTON_LEFT:
return "left";
case GLFW_MOUSE_BUTTON_RIGHT:
return "right";
case GLFW_MOUSE_BUTTON_MIDDLE:
return "middle";
}
return NULL;
}
static const char* get_character_string(int character)
{
2010-09-09 17:36:40 +00:00
// This assumes UTF-8, which is stupid
static char result[6 + 1];
2010-09-07 15:34:51 +00:00
2010-09-09 17:36:40 +00:00
int length = wctomb(result, character);
if (length == -1)
length = 0;
2010-09-07 15:34:51 +00:00
2010-09-09 17:36:40 +00:00
result[length] = '\0';
return result;
2010-09-07 15:34:51 +00:00
}
static void window_size_callback(GLFWwindow window, int width, int height)
2010-09-07 15:34:51 +00:00
{
printf("%08x at %0.3f: Window size: %i %i\n",
counter++,
glfwGetTime(),
width,
height);
glViewport(0, 0, width, height);
}
static int window_close_callback(GLFWwindow window)
2010-09-07 15:34:51 +00:00
{
printf("%08x at %0.3f: Window close\n", counter++, glfwGetTime());
if (closeable)
running = GL_FALSE;
2012-02-07 15:40:26 +00:00
return closeable;
2010-09-07 15:34:51 +00:00
}
static void window_refresh_callback(GLFWwindow window)
2010-09-07 15:34:51 +00:00
{
printf("%08x at %0.3f: Window refresh\n", counter++, glfwGetTime());
2011-09-08 20:52:58 +00:00
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
2010-09-07 15:34:51 +00:00
}
2010-09-19 00:45:20 +00:00
static void window_focus_callback(GLFWwindow window, int activated)
{
printf("%08x at %0.3f: Window %s\n",
counter++,
glfwGetTime(),
activated ? "activated" : "deactivated");
}
2010-09-20 00:22:35 +00:00
static void window_iconify_callback(GLFWwindow window, int iconified)
{
printf("%08x at %0.3f: Window was %s\n",
counter++,
glfwGetTime(),
iconified ? "iconified" : "restored");
}
static void mouse_button_callback(GLFWwindow window, int button, int action)
2010-09-07 15:34:51 +00:00
{
const char* name = get_button_name(button);
printf("%08x at %0.3f: Mouse button %i", counter++, glfwGetTime(), button);
if (name)
printf(" (%s) was %s\n", name, get_action_name(action));
else
printf(" was %s\n", get_action_name(action));
}
static void cursor_position_callback(GLFWwindow window, int x, int y)
2010-09-07 15:34:51 +00:00
{
printf("%08x at %0.3f: Cursor position: %i %i\n", counter++, glfwGetTime(), x, y);
2010-09-07 15:34:51 +00:00
}
2012-01-30 21:59:38 +00:00
static void cursor_enter_callback(GLFWwindow window, int entered)
{
2012-01-30 21:59:38 +00:00
printf("%08x at %0.3f: Cursor %s window\n",
counter++,
glfwGetTime(),
entered ? "entered" : "left");
}
2012-03-28 19:54:09 +00:00
static void scroll_callback(GLFWwindow window, double x, double y)
2010-09-07 15:34:51 +00:00
{
2012-03-28 19:54:09 +00:00
printf("%08x at %0.3f: Scroll: %0.3f %0.3f\n", counter++, glfwGetTime(), x, y);
2010-09-07 15:34:51 +00:00
}
static void key_callback(GLFWwindow window, int key, int action)
2010-09-07 15:34:51 +00:00
{
const char* name = get_key_name(key);
printf("%08x at %0.3f: Key 0x%04x", counter++, glfwGetTime(), key);
if (name)
printf(" (%s) was %s\n", name, get_action_name(action));
else
printf(" was %s\n", get_action_name(action));
if (action != GLFW_PRESS)
return;
switch (key)
{
case GLFW_KEY_R:
2010-09-07 15:34:51 +00:00
{
keyrepeat = !keyrepeat;
glfwSetInputMode(window, GLFW_KEY_REPEAT, keyrepeat);
2010-09-07 15:34:51 +00:00
printf("(( key repeat %s ))\n", keyrepeat ? "enabled" : "disabled");
break;
}
case GLFW_KEY_S:
2010-09-07 15:34:51 +00:00
{
systemkeys = !systemkeys;
glfwSetInputMode(window, GLFW_SYSTEM_KEYS, systemkeys);
2010-09-07 15:34:51 +00:00
printf("(( system keys %s ))\n", systemkeys ? "enabled" : "disabled");
break;
}
2012-02-07 15:40:26 +00:00
case GLFW_KEY_C:
{
closeable = !closeable;
printf("(( closing %s ))\n", closeable ? "enabled" : "disabled");
break;
}
2010-09-07 15:34:51 +00:00
}
}
static void char_callback(GLFWwindow window, int character)
2010-09-07 15:34:51 +00:00
{
2010-09-11 00:59:50 +00:00
printf("%08x at %0.3f: Character 0x%04x (%s) input\n",
counter++,
glfwGetTime(),
character,
get_character_string(character));
2010-09-07 15:34:51 +00:00
}
int main(void)
{
GLFWwindow window;
2012-08-07 11:01:19 +00:00
int width, height;
2010-09-07 15:34:51 +00:00
setlocale(LC_ALL, "");
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW: %s\n", glfwErrorString(glfwGetError()));
2011-09-08 20:47:37 +00:00
exit(EXIT_FAILURE);
2010-09-07 15:34:51 +00:00
}
printf("Library initialized\n");
glfwSetWindowSizeCallback(window_size_callback);
glfwSetWindowCloseCallback(window_close_callback);
glfwSetWindowRefreshCallback(window_refresh_callback);
glfwSetWindowFocusCallback(window_focus_callback);
glfwSetWindowIconifyCallback(window_iconify_callback);
glfwSetMouseButtonCallback(mouse_button_callback);
glfwSetCursorPosCallback(cursor_position_callback);
glfwSetCursorEnterCallback(cursor_enter_callback);
glfwSetScrollCallback(scroll_callback);
glfwSetKeyCallback(key_callback);
glfwSetCharCallback(char_callback);
window = glfwCreateWindow(0, 0, GLFW_WINDOWED, "Event Linter", NULL);
if (!window)
2010-09-07 15:34:51 +00:00
{
glfwTerminate();
fprintf(stderr, "Failed to open GLFW window: %s\n", glfwErrorString(glfwGetError()));
2011-09-08 20:47:37 +00:00
exit(EXIT_FAILURE);
2010-09-07 15:34:51 +00:00
}
printf("Window opened\n");
glfwSwapInterval(1);
2012-08-07 11:01:19 +00:00
glfwGetWindowSize(window, &width, &height);
printf("Window size should be %ix%i\n", width, height);
2010-09-07 15:34:51 +00:00
printf("Key repeat should be %s\n", keyrepeat ? "enabled" : "disabled");
printf("System keys should be %s\n", systemkeys ? "enabled" : "disabled");
printf("Main loop starting\n");
while (running)
2011-09-08 20:45:35 +00:00
glfwWaitEvents();
2010-09-07 15:34:51 +00:00
glfwTerminate();
2011-09-08 20:47:37 +00:00
exit(EXIT_SUCCESS);
2010-09-07 15:34:51 +00:00
}