events.c 22 KB

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