1 //========================================================================
2 // Event linter (event spewer)
3 // Copyright (c) Camilla Berglund <elmindreda@glfw.org>
4 //
5 // This software is provided 'as-is', without any express or implied
6 // warranty. In no event will the authors be held liable for any damages
7 // arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it
11 // freely, subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented; you must not
14 // claim that you wrote the original software. If you use this software
15 // in a product, an acknowledgment in the product documentation would
16 // be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such, and must not
19 // be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source
22 // distribution.
23 //
24 //========================================================================
25 //
26 // This test hooks every available callback and outputs their arguments
27 //
28 // Log messages go to stdout, error messages to stderr
29 //
30 // Every event also gets a (sequential) number to aid discussion of logs
31 //
32 //========================================================================
33
34 #include <glad/glad.h>
35 #include <GLFW/glfw3.h>
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <ctype.h>
40 #include <string.h>
41 #include <locale.h>
42
43 #include "getopt.h"
44
45 // Event index
46 static unsigned int counter = 0;
47
48 typedef struct
49 {
50 GLFWwindow* window;
51 int number;
52 int closeable;
53 } Slot;
54
usage(void)55 static void usage(void)
56 {
57 printf("Usage: events [-f] [-h] [-n WINDOWS]\n");
58 printf("Options:\n");
59 printf(" -f use full screen\n");
60 printf(" -h show this help\n");
61 printf(" -n the number of windows to create\n");
62 }
63
get_key_name(int key)64 static const char* get_key_name(int key)
65 {
66 switch (key)
67 {
68 // Printable keys
69 case GLFW_KEY_A: return "A";
70 case GLFW_KEY_B: return "B";
71 case GLFW_KEY_C: return "C";
72 case GLFW_KEY_D: return "D";
73 case GLFW_KEY_E: return "E";
74 case GLFW_KEY_F: return "F";
75 case GLFW_KEY_G: return "G";
76 case GLFW_KEY_H: return "H";
77 case GLFW_KEY_I: return "I";
78 case GLFW_KEY_J: return "J";
79 case GLFW_KEY_K: return "K";
80 case GLFW_KEY_L: return "L";
81 case GLFW_KEY_M: return "M";
82 case GLFW_KEY_N: return "N";
83 case GLFW_KEY_O: return "O";
84 case GLFW_KEY_P: return "P";
85 case GLFW_KEY_Q: return "Q";
86 case GLFW_KEY_R: return "R";
87 case GLFW_KEY_S: return "S";
88 case GLFW_KEY_T: return "T";
89 case GLFW_KEY_U: return "U";
90 case GLFW_KEY_V: return "V";
91 case GLFW_KEY_W: return "W";
92 case GLFW_KEY_X: return "X";
93 case GLFW_KEY_Y: return "Y";
94 case GLFW_KEY_Z: return "Z";
95 case GLFW_KEY_1: return "1";
96 case GLFW_KEY_2: return "2";
97 case GLFW_KEY_3: return "3";
98 case GLFW_KEY_4: return "4";
99 case GLFW_KEY_5: return "5";
100 case GLFW_KEY_6: return "6";
101 case GLFW_KEY_7: return "7";
102 case GLFW_KEY_8: return "8";
103 case GLFW_KEY_9: return "9";
104 case GLFW_KEY_0: return "0";
105 case GLFW_KEY_SPACE: return "SPACE";
106 case GLFW_KEY_MINUS: return "MINUS";
107 case GLFW_KEY_EQUAL: return "EQUAL";
108 case GLFW_KEY_LEFT_BRACKET: return "LEFT BRACKET";
109 case GLFW_KEY_RIGHT_BRACKET: return "RIGHT BRACKET";
110 case GLFW_KEY_BACKSLASH: return "BACKSLASH";
111 case GLFW_KEY_SEMICOLON: return "SEMICOLON";
112 case GLFW_KEY_APOSTROPHE: return "APOSTROPHE";
113 case GLFW_KEY_GRAVE_ACCENT: return "GRAVE ACCENT";
114 case GLFW_KEY_COMMA: return "COMMA";
115 case GLFW_KEY_PERIOD: return "PERIOD";
116 case GLFW_KEY_SLASH: return "SLASH";
117 case GLFW_KEY_WORLD_1: return "WORLD 1";
118 case GLFW_KEY_WORLD_2: return "WORLD 2";
119
120 // Function keys
121 case GLFW_KEY_ESCAPE: return "ESCAPE";
122 case GLFW_KEY_F1: return "F1";
123 case GLFW_KEY_F2: return "F2";
124 case GLFW_KEY_F3: return "F3";
125 case GLFW_KEY_F4: return "F4";
126 case GLFW_KEY_F5: return "F5";
127 case GLFW_KEY_F6: return "F6";
128 case GLFW_KEY_F7: return "F7";
129 case GLFW_KEY_F8: return "F8";
130 case GLFW_KEY_F9: return "F9";
131 case GLFW_KEY_F10: return "F10";
132 case GLFW_KEY_F11: return "F11";
133 case GLFW_KEY_F12: return "F12";
134 case GLFW_KEY_F13: return "F13";
135 case GLFW_KEY_F14: return "F14";
136 case GLFW_KEY_F15: return "F15";
137 case GLFW_KEY_F16: return "F16";
138 case GLFW_KEY_F17: return "F17";
139 case GLFW_KEY_F18: return "F18";
140 case GLFW_KEY_F19: return "F19";
141 case GLFW_KEY_F20: return "F20";
142 case GLFW_KEY_F21: return "F21";
143 case GLFW_KEY_F22: return "F22";
144 case GLFW_KEY_F23: return "F23";
145 case GLFW_KEY_F24: return "F24";
146 case GLFW_KEY_F25: return "F25";
147 case GLFW_KEY_UP: return "UP";
148 case GLFW_KEY_DOWN: return "DOWN";
149 case GLFW_KEY_LEFT: return "LEFT";
150 case GLFW_KEY_RIGHT: return "RIGHT";
151 case GLFW_KEY_LEFT_SHIFT: return "LEFT SHIFT";
152 case GLFW_KEY_RIGHT_SHIFT: return "RIGHT SHIFT";
153 case GLFW_KEY_LEFT_CONTROL: return "LEFT CONTROL";
154 case GLFW_KEY_RIGHT_CONTROL: return "RIGHT CONTROL";
155 case GLFW_KEY_LEFT_ALT: return "LEFT ALT";
156 case GLFW_KEY_RIGHT_ALT: return "RIGHT ALT";
157 case GLFW_KEY_TAB: return "TAB";
158 case GLFW_KEY_ENTER: return "ENTER";
159 case GLFW_KEY_BACKSPACE: return "BACKSPACE";
160 case GLFW_KEY_INSERT: return "INSERT";
161 case GLFW_KEY_DELETE: return "DELETE";
162 case GLFW_KEY_PAGE_UP: return "PAGE UP";
163 case GLFW_KEY_PAGE_DOWN: return "PAGE DOWN";
164 case GLFW_KEY_HOME: return "HOME";
165 case GLFW_KEY_END: return "END";
166 case GLFW_KEY_KP_0: return "KEYPAD 0";
167 case GLFW_KEY_KP_1: return "KEYPAD 1";
168 case GLFW_KEY_KP_2: return "KEYPAD 2";
169 case GLFW_KEY_KP_3: return "KEYPAD 3";
170 case GLFW_KEY_KP_4: return "KEYPAD 4";
171 case GLFW_KEY_KP_5: return "KEYPAD 5";
172 case GLFW_KEY_KP_6: return "KEYPAD 6";
173 case GLFW_KEY_KP_7: return "KEYPAD 7";
174 case GLFW_KEY_KP_8: return "KEYPAD 8";
175 case GLFW_KEY_KP_9: return "KEYPAD 9";
176 case GLFW_KEY_KP_DIVIDE: return "KEYPAD DIVIDE";
177 case GLFW_KEY_KP_MULTIPLY: return "KEYPAD MULTPLY";
178 case GLFW_KEY_KP_SUBTRACT: return "KEYPAD SUBTRACT";
179 case GLFW_KEY_KP_ADD: return "KEYPAD ADD";
180 case GLFW_KEY_KP_DECIMAL: return "KEYPAD DECIMAL";
181 case GLFW_KEY_KP_EQUAL: return "KEYPAD EQUAL";
182 case GLFW_KEY_KP_ENTER: return "KEYPAD ENTER";
183 case GLFW_KEY_PRINT_SCREEN: return "PRINT SCREEN";
184 case GLFW_KEY_NUM_LOCK: return "NUM LOCK";
185 case GLFW_KEY_CAPS_LOCK: return "CAPS LOCK";
186 case GLFW_KEY_SCROLL_LOCK: return "SCROLL LOCK";
187 case GLFW_KEY_PAUSE: return "PAUSE";
188 case GLFW_KEY_LEFT_SUPER: return "LEFT SUPER";
189 case GLFW_KEY_RIGHT_SUPER: return "RIGHT SUPER";
190 case GLFW_KEY_MENU: return "MENU";
191
192 default: return "UNKNOWN";
193 }
194 }
195
get_action_name(int action)196 static const char* get_action_name(int action)
197 {
198 switch (action)
199 {
200 case GLFW_PRESS:
201 return "pressed";
202 case GLFW_RELEASE:
203 return "released";
204 case GLFW_REPEAT:
205 return "repeated";
206 }
207
208 return "caused unknown action";
209 }
210
get_button_name(int button)211 static const char* get_button_name(int button)
212 {
213 switch (button)
214 {
215 case GLFW_MOUSE_BUTTON_LEFT:
216 return "left";
217 case GLFW_MOUSE_BUTTON_RIGHT:
218 return "right";
219 case GLFW_MOUSE_BUTTON_MIDDLE:
220 return "middle";
221 default:
222 {
223 static char name[16];
224 sprintf(name, "%i", button);
225 return name;
226 }
227 }
228 }
229
get_mods_name(int mods)230 static const char* get_mods_name(int mods)
231 {
232 static char name[512];
233
234 if (mods == 0)
235 return " no mods";
236
237 name[0] = '\0';
238
239 if (mods & GLFW_MOD_SHIFT)
240 strcat(name, " shift");
241 if (mods & GLFW_MOD_CONTROL)
242 strcat(name, " control");
243 if (mods & GLFW_MOD_ALT)
244 strcat(name, " alt");
245 if (mods & GLFW_MOD_SUPER)
246 strcat(name, " super");
247
248 return name;
249 }
250
get_character_string(int codepoint)251 static const char* get_character_string(int codepoint)
252 {
253 // This assumes UTF-8, which is stupid
254 static char result[6 + 1];
255
256 int length = wctomb(result, codepoint);
257 if (length == -1)
258 length = 0;
259
260 result[length] = '\0';
261 return result;
262 }
263
error_callback(int error,const char * description)264 static void error_callback(int error, const char* description)
265 {
266 fprintf(stderr, "Error: %s\n", description);
267 }
268
window_pos_callback(GLFWwindow * window,int x,int y)269 static void window_pos_callback(GLFWwindow* window, int x, int y)
270 {
271 Slot* slot = glfwGetWindowUserPointer(window);
272 printf("%08x to %i at %0.3f: Window position: %i %i\n",
273 counter++, slot->number, glfwGetTime(), x, y);
274 }
275
window_size_callback(GLFWwindow * window,int width,int height)276 static void window_size_callback(GLFWwindow* window, int width, int height)
277 {
278 Slot* slot = glfwGetWindowUserPointer(window);
279 printf("%08x to %i at %0.3f: Window size: %i %i\n",
280 counter++, slot->number, glfwGetTime(), width, height);
281 }
282
framebuffer_size_callback(GLFWwindow * window,int width,int height)283 static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
284 {
285 Slot* slot = glfwGetWindowUserPointer(window);
286 printf("%08x to %i at %0.3f: Framebuffer size: %i %i\n",
287 counter++, slot->number, glfwGetTime(), width, height);
288
289 glViewport(0, 0, width, height);
290 }
291
window_close_callback(GLFWwindow * window)292 static void window_close_callback(GLFWwindow* window)
293 {
294 Slot* slot = glfwGetWindowUserPointer(window);
295 printf("%08x to %i at %0.3f: Window close\n",
296 counter++, slot->number, glfwGetTime());
297
298 glfwSetWindowShouldClose(window, slot->closeable);
299 }
300
window_refresh_callback(GLFWwindow * window)301 static void window_refresh_callback(GLFWwindow* window)
302 {
303 Slot* slot = glfwGetWindowUserPointer(window);
304 printf("%08x to %i at %0.3f: Window refresh\n",
305 counter++, slot->number, glfwGetTime());
306
307 glfwMakeContextCurrent(window);
308 glClear(GL_COLOR_BUFFER_BIT);
309 glfwSwapBuffers(window);
310 }
311
window_focus_callback(GLFWwindow * window,int focused)312 static void window_focus_callback(GLFWwindow* window, int focused)
313 {
314 Slot* slot = glfwGetWindowUserPointer(window);
315 printf("%08x to %i at %0.3f: Window %s\n",
316 counter++, slot->number, glfwGetTime(),
317 focused ? "focused" : "defocused");
318 }
319
window_iconify_callback(GLFWwindow * window,int iconified)320 static void window_iconify_callback(GLFWwindow* window, int iconified)
321 {
322 Slot* slot = glfwGetWindowUserPointer(window);
323 printf("%08x to %i at %0.3f: Window was %s\n",
324 counter++, slot->number, glfwGetTime(),
325 iconified ? "iconified" : "restored");
326 }
327
mouse_button_callback(GLFWwindow * window,int button,int action,int mods)328 static void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
329 {
330 Slot* slot = glfwGetWindowUserPointer(window);
331 printf("%08x to %i at %0.3f: Mouse button %i (%s) (with%s) was %s\n",
332 counter++, slot->number, glfwGetTime(), button,
333 get_button_name(button),
334 get_mods_name(mods),
335 get_action_name(action));
336 }
337
cursor_position_callback(GLFWwindow * window,double x,double y)338 static void cursor_position_callback(GLFWwindow* window, double x, double y)
339 {
340 Slot* slot = glfwGetWindowUserPointer(window);
341 printf("%08x to %i at %0.3f: Cursor position: %f %f\n",
342 counter++, slot->number, glfwGetTime(), x, y);
343 }
344
cursor_enter_callback(GLFWwindow * window,int entered)345 static void cursor_enter_callback(GLFWwindow* window, int entered)
346 {
347 Slot* slot = glfwGetWindowUserPointer(window);
348 printf("%08x to %i at %0.3f: Cursor %s window\n",
349 counter++, slot->number, glfwGetTime(),
350 entered ? "entered" : "left");
351 }
352
scroll_callback(GLFWwindow * window,double x,double y)353 static void scroll_callback(GLFWwindow* window, double x, double y)
354 {
355 Slot* slot = glfwGetWindowUserPointer(window);
356 printf("%08x to %i at %0.3f: Scroll: %0.3f %0.3f\n",
357 counter++, slot->number, glfwGetTime(), x, y);
358 }
359
key_callback(GLFWwindow * window,int key,int scancode,int action,int mods)360 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
361 {
362 Slot* slot = glfwGetWindowUserPointer(window);
363 const char* name = glfwGetKeyName(key, scancode);
364
365 if (name)
366 {
367 printf("%08x to %i at %0.3f: Key 0x%04x Scancode 0x%04x (%s) (%s) (with%s) was %s\n",
368 counter++, slot->number, glfwGetTime(), key, scancode,
369 get_key_name(key),
370 name,
371 get_mods_name(mods),
372 get_action_name(action));
373 }
374 else
375 {
376 printf("%08x to %i at %0.3f: Key 0x%04x Scancode 0x%04x (%s) (with%s) was %s\n",
377 counter++, slot->number, glfwGetTime(), key, scancode,
378 get_key_name(key),
379 get_mods_name(mods),
380 get_action_name(action));
381 }
382
383 if (action != GLFW_PRESS)
384 return;
385
386 switch (key)
387 {
388 case GLFW_KEY_C:
389 {
390 slot->closeable = !slot->closeable;
391
392 printf("(( closing %s ))\n", slot->closeable ? "enabled" : "disabled");
393 break;
394 }
395 }
396 }
397
char_callback(GLFWwindow * window,unsigned int codepoint)398 static void char_callback(GLFWwindow* window, unsigned int codepoint)
399 {
400 Slot* slot = glfwGetWindowUserPointer(window);
401 printf("%08x to %i at %0.3f: Character 0x%08x (%s) input\n",
402 counter++, slot->number, glfwGetTime(), codepoint,
403 get_character_string(codepoint));
404 }
405
char_mods_callback(GLFWwindow * window,unsigned int codepoint,int mods)406 static void char_mods_callback(GLFWwindow* window, unsigned int codepoint, int mods)
407 {
408 Slot* slot = glfwGetWindowUserPointer(window);
409 printf("%08x to %i at %0.3f: Character 0x%08x (%s) with modifiers (with%s) input\n",
410 counter++, slot->number, glfwGetTime(), codepoint,
411 get_character_string(codepoint),
412 get_mods_name(mods));
413 }
414
drop_callback(GLFWwindow * window,int count,const char ** paths)415 static void drop_callback(GLFWwindow* window, int count, const char** paths)
416 {
417 int i;
418 Slot* slot = glfwGetWindowUserPointer(window);
419
420 printf("%08x to %i at %0.3f: Drop input\n",
421 counter++, slot->number, glfwGetTime());
422
423 for (i = 0; i < count; i++)
424 printf(" %i: \"%s\"\n", i, paths[i]);
425 }
426
monitor_callback(GLFWmonitor * monitor,int event)427 static void monitor_callback(GLFWmonitor* monitor, int event)
428 {
429 if (event == GLFW_CONNECTED)
430 {
431 int x, y, widthMM, heightMM;
432 const GLFWvidmode* mode = glfwGetVideoMode(monitor);
433
434 glfwGetMonitorPos(monitor, &x, &y);
435 glfwGetMonitorPhysicalSize(monitor, &widthMM, &heightMM);
436
437 printf("%08x at %0.3f: Monitor %s (%ix%i at %ix%i, %ix%i mm) was connected\n",
438 counter++,
439 glfwGetTime(),
440 glfwGetMonitorName(monitor),
441 mode->width, mode->height,
442 x, y,
443 widthMM, heightMM);
444 }
445 else if (event == GLFW_DISCONNECTED)
446 {
447 printf("%08x at %0.3f: Monitor %s was disconnected\n",
448 counter++,
449 glfwGetTime(),
450 glfwGetMonitorName(monitor));
451 }
452 }
453
joystick_callback(int joy,int event)454 static void joystick_callback(int joy, int event)
455 {
456 if (event == GLFW_CONNECTED)
457 {
458 int axisCount, buttonCount;
459
460 glfwGetJoystickAxes(joy, &axisCount);
461 glfwGetJoystickButtons(joy, &buttonCount);
462
463 printf("%08x at %0.3f: Joystick %i (%s) was connected with %i axes and %i buttons\n",
464 counter++, glfwGetTime(),
465 joy,
466 glfwGetJoystickName(joy),
467 axisCount,
468 buttonCount);
469 }
470 else
471 {
472 printf("%08x at %0.3f: Joystick %i was disconnected\n",
473 counter++, glfwGetTime(), joy);
474 }
475 }
476
main(int argc,char ** argv)477 int main(int argc, char** argv)
478 {
479 Slot* slots;
480 GLFWmonitor* monitor = NULL;
481 int ch, i, width, height, count = 1;
482
483 setlocale(LC_ALL, "");
484
485 glfwSetErrorCallback(error_callback);
486
487 if (!glfwInit())
488 exit(EXIT_FAILURE);
489
490 printf("Library initialized\n");
491
492 glfwSetMonitorCallback(monitor_callback);
493 glfwSetJoystickCallback(joystick_callback);
494
495 while ((ch = getopt(argc, argv, "hfn:")) != -1)
496 {
497 switch (ch)
498 {
499 case 'h':
500 usage();
501 exit(EXIT_SUCCESS);
502
503 case 'f':
504 monitor = glfwGetPrimaryMonitor();
505 break;
506
507 case 'n':
508 count = (int) strtol(optarg, NULL, 10);
509 break;
510
511 default:
512 usage();
513 exit(EXIT_FAILURE);
514 }
515 }
516
517 if (monitor)
518 {
519 const GLFWvidmode* mode = glfwGetVideoMode(monitor);
520
521 glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
522 glfwWindowHint(GLFW_RED_BITS, mode->redBits);
523 glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
524 glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
525
526 width = mode->width;
527 height = mode->height;
528 }
529 else
530 {
531 width = 640;
532 height = 480;
533 }
534
535 if (!count)
536 {
537 fprintf(stderr, "Invalid user\n");
538 exit(EXIT_FAILURE);
539 }
540
541 slots = calloc(count, sizeof(Slot));
542
543 for (i = 0; i < count; i++)
544 {
545 char title[128];
546
547 slots[i].closeable = GLFW_TRUE;
548 slots[i].number = i + 1;
549
550 sprintf(title, "Event Linter (Window %i)", slots[i].number);
551
552 if (monitor)
553 {
554 printf("Creating full screen window %i (%ix%i on %s)\n",
555 slots[i].number,
556 width, height,
557 glfwGetMonitorName(monitor));
558 }
559 else
560 {
561 printf("Creating windowed mode window %i (%ix%i)\n",
562 slots[i].number,
563 width, height);
564 }
565
566 slots[i].window = glfwCreateWindow(width, height, title, monitor, NULL);
567 if (!slots[i].window)
568 {
569 free(slots);
570 glfwTerminate();
571 exit(EXIT_FAILURE);
572 }
573
574 glfwSetWindowUserPointer(slots[i].window, slots + i);
575
576 glfwSetWindowPosCallback(slots[i].window, window_pos_callback);
577 glfwSetWindowSizeCallback(slots[i].window, window_size_callback);
578 glfwSetFramebufferSizeCallback(slots[i].window, framebuffer_size_callback);
579 glfwSetWindowCloseCallback(slots[i].window, window_close_callback);
580 glfwSetWindowRefreshCallback(slots[i].window, window_refresh_callback);
581 glfwSetWindowFocusCallback(slots[i].window, window_focus_callback);
582 glfwSetWindowIconifyCallback(slots[i].window, window_iconify_callback);
583 glfwSetMouseButtonCallback(slots[i].window, mouse_button_callback);
584 glfwSetCursorPosCallback(slots[i].window, cursor_position_callback);
585 glfwSetCursorEnterCallback(slots[i].window, cursor_enter_callback);
586 glfwSetScrollCallback(slots[i].window, scroll_callback);
587 glfwSetKeyCallback(slots[i].window, key_callback);
588 glfwSetCharCallback(slots[i].window, char_callback);
589 glfwSetCharModsCallback(slots[i].window, char_mods_callback);
590 glfwSetDropCallback(slots[i].window, drop_callback);
591
592 glfwMakeContextCurrent(slots[i].window);
593 gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
594 glfwSwapInterval(1);
595 }
596
597 printf("Main loop starting\n");
598
599 for (;;)
600 {
601 for (i = 0; i < count; i++)
602 {
603 if (glfwWindowShouldClose(slots[i].window))
604 break;
605 }
606
607 if (i < count)
608 break;
609
610 glfwWaitEvents();
611
612 // Workaround for an issue with msvcrt and mintty
613 fflush(stdout);
614 }
615
616 free(slots);
617 glfwTerminate();
618 exit(EXIT_SUCCESS);
619 }
620
621