cursor.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. //========================================================================
  2. // Cursor & input mode tests
  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 provides an interface to the cursor image and cursor mode
  27. // parts of the API.
  28. //
  29. // Custom cursor image generation by urraka.
  30. //
  31. //========================================================================
  32. #define GLAD_GL_IMPLEMENTATION
  33. #include <glad/gl.h>
  34. #define GLFW_INCLUDE_NONE
  35. #include <GLFW/glfw3.h>
  36. #if defined(_MSC_VER)
  37. // Make MS math.h define M_PI
  38. #define _USE_MATH_DEFINES
  39. #endif
  40. #include <math.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include "linmath.h"
  44. #define CURSOR_FRAME_COUNT 60
  45. static const char* vertex_shader_text =
  46. "#version 110\n"
  47. "uniform mat4 MVP;\n"
  48. "attribute vec2 vPos;\n"
  49. "void main()\n"
  50. "{\n"
  51. " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
  52. "}\n";
  53. static const char* fragment_shader_text =
  54. "#version 110\n"
  55. "void main()\n"
  56. "{\n"
  57. " gl_FragColor = vec4(1.0);\n"
  58. "}\n";
  59. static double cursor_x;
  60. static double cursor_y;
  61. static int swap_interval = 1;
  62. static int wait_events = GLFW_TRUE;
  63. static int animate_cursor = GLFW_FALSE;
  64. static int track_cursor = GLFW_FALSE;
  65. static GLFWcursor* standard_cursors[10];
  66. static GLFWcursor* tracking_cursor = NULL;
  67. static void error_callback(int error, const char* description)
  68. {
  69. fprintf(stderr, "Error: %s\n", description);
  70. }
  71. static float star(int x, int y, float t)
  72. {
  73. const float c = 64 / 2.f;
  74. const float i = (0.25f * (float) sin(2.f * M_PI * t) + 0.75f);
  75. const float k = 64 * 0.046875f * i;
  76. const float dist = (float) sqrt((x - c) * (x - c) + (y - c) * (y - c));
  77. const float salpha = 1.f - dist / c;
  78. const float xalpha = (float) x == c ? c : k / (float) fabs(x - c);
  79. const float yalpha = (float) y == c ? c : k / (float) fabs(y - c);
  80. return (float) fmax(0.f, fmin(1.f, i * salpha * 0.2f + salpha * xalpha * yalpha));
  81. }
  82. static GLFWcursor* create_cursor_frame(float t)
  83. {
  84. int i = 0, x, y;
  85. unsigned char buffer[64 * 64 * 4];
  86. const GLFWimage image = { 64, 64, buffer };
  87. for (y = 0; y < image.width; y++)
  88. {
  89. for (x = 0; x < image.height; x++)
  90. {
  91. buffer[i++] = 255;
  92. buffer[i++] = 255;
  93. buffer[i++] = 255;
  94. buffer[i++] = (unsigned char) (255 * star(x, y, t));
  95. }
  96. }
  97. return glfwCreateCursor(&image, image.width / 2, image.height / 2);
  98. }
  99. static GLFWcursor* create_tracking_cursor(void)
  100. {
  101. int i = 0, x, y;
  102. unsigned char buffer[32 * 32 * 4];
  103. const GLFWimage image = { 32, 32, buffer };
  104. for (y = 0; y < image.width; y++)
  105. {
  106. for (x = 0; x < image.height; x++)
  107. {
  108. if (x == 7 || y == 7)
  109. {
  110. buffer[i++] = 255;
  111. buffer[i++] = 0;
  112. buffer[i++] = 0;
  113. buffer[i++] = 255;
  114. }
  115. else
  116. {
  117. buffer[i++] = 0;
  118. buffer[i++] = 0;
  119. buffer[i++] = 0;
  120. buffer[i++] = 0;
  121. }
  122. }
  123. }
  124. return glfwCreateCursor(&image, 7, 7);
  125. }
  126. static void cursor_position_callback(GLFWwindow* window, double x, double y)
  127. {
  128. printf("%0.3f: Cursor position: %f %f (%+f %+f)\n",
  129. glfwGetTime(),
  130. x, y, x - cursor_x, y - cursor_y);
  131. cursor_x = x;
  132. cursor_y = y;
  133. }
  134. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  135. {
  136. if (action != GLFW_PRESS)
  137. return;
  138. switch (key)
  139. {
  140. case GLFW_KEY_A:
  141. {
  142. animate_cursor = !animate_cursor;
  143. if (!animate_cursor)
  144. glfwSetCursor(window, NULL);
  145. break;
  146. }
  147. case GLFW_KEY_ESCAPE:
  148. {
  149. const int mode = glfwGetInputMode(window, GLFW_CURSOR);
  150. if (mode != GLFW_CURSOR_DISABLED && mode != GLFW_CURSOR_CAPTURED)
  151. {
  152. glfwSetWindowShouldClose(window, GLFW_TRUE);
  153. break;
  154. }
  155. /* FALLTHROUGH */
  156. }
  157. case GLFW_KEY_N:
  158. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  159. glfwGetCursorPos(window, &cursor_x, &cursor_y);
  160. printf("(( cursor is normal ))\n");
  161. break;
  162. case GLFW_KEY_D:
  163. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  164. printf("(( cursor is disabled ))\n");
  165. break;
  166. case GLFW_KEY_H:
  167. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
  168. printf("(( cursor is hidden ))\n");
  169. break;
  170. case GLFW_KEY_C:
  171. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_CAPTURED);
  172. printf("(( cursor is captured ))\n");
  173. break;
  174. case GLFW_KEY_R:
  175. if (!glfwRawMouseMotionSupported())
  176. break;
  177. if (glfwGetInputMode(window, GLFW_RAW_MOUSE_MOTION))
  178. {
  179. glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_FALSE);
  180. printf("(( raw input is disabled ))\n");
  181. }
  182. else
  183. {
  184. glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
  185. printf("(( raw input is enabled ))\n");
  186. }
  187. break;
  188. case GLFW_KEY_SPACE:
  189. swap_interval = 1 - swap_interval;
  190. printf("(( swap interval: %i ))\n", swap_interval);
  191. glfwSwapInterval(swap_interval);
  192. break;
  193. case GLFW_KEY_W:
  194. wait_events = !wait_events;
  195. printf("(( %sing for events ))\n", wait_events ? "wait" : "poll");
  196. break;
  197. case GLFW_KEY_T:
  198. track_cursor = !track_cursor;
  199. if (track_cursor)
  200. glfwSetCursor(window, tracking_cursor);
  201. else
  202. glfwSetCursor(window, NULL);
  203. break;
  204. case GLFW_KEY_P:
  205. {
  206. double x, y;
  207. glfwGetCursorPos(window, &x, &y);
  208. printf("Query before set: %f %f (%+f %+f)\n",
  209. x, y, x - cursor_x, y - cursor_y);
  210. cursor_x = x;
  211. cursor_y = y;
  212. glfwSetCursorPos(window, cursor_x, cursor_y);
  213. glfwGetCursorPos(window, &x, &y);
  214. printf("Query after set: %f %f (%+f %+f)\n",
  215. x, y, x - cursor_x, y - cursor_y);
  216. cursor_x = x;
  217. cursor_y = y;
  218. break;
  219. }
  220. case GLFW_KEY_UP:
  221. glfwSetCursorPos(window, 0, 0);
  222. glfwGetCursorPos(window, &cursor_x, &cursor_y);
  223. break;
  224. case GLFW_KEY_DOWN:
  225. {
  226. int width, height;
  227. glfwGetWindowSize(window, &width, &height);
  228. glfwSetCursorPos(window, width - 1, height - 1);
  229. glfwGetCursorPos(window, &cursor_x, &cursor_y);
  230. break;
  231. }
  232. case GLFW_KEY_0:
  233. glfwSetCursor(window, NULL);
  234. break;
  235. case GLFW_KEY_1:
  236. case GLFW_KEY_2:
  237. case GLFW_KEY_3:
  238. case GLFW_KEY_4:
  239. case GLFW_KEY_5:
  240. case GLFW_KEY_6:
  241. case GLFW_KEY_7:
  242. case GLFW_KEY_8:
  243. case GLFW_KEY_9:
  244. {
  245. int index = key - GLFW_KEY_1;
  246. if (mods & GLFW_MOD_SHIFT)
  247. index += 9;
  248. if (index < sizeof(standard_cursors) / sizeof(standard_cursors[0]))
  249. glfwSetCursor(window, standard_cursors[index]);
  250. break;
  251. }
  252. case GLFW_KEY_F11:
  253. case GLFW_KEY_ENTER:
  254. {
  255. static int x, y, width, height;
  256. if (mods != GLFW_MOD_ALT)
  257. return;
  258. if (glfwGetWindowMonitor(window))
  259. glfwSetWindowMonitor(window, NULL, x, y, width, height, 0);
  260. else
  261. {
  262. GLFWmonitor* monitor = glfwGetPrimaryMonitor();
  263. const GLFWvidmode* mode = glfwGetVideoMode(monitor);
  264. glfwGetWindowPos(window, &x, &y);
  265. glfwGetWindowSize(window, &width, &height);
  266. glfwSetWindowMonitor(window, monitor,
  267. 0, 0, mode->width, mode->height,
  268. mode->refreshRate);
  269. }
  270. glfwGetCursorPos(window, &cursor_x, &cursor_y);
  271. break;
  272. }
  273. }
  274. }
  275. int main(void)
  276. {
  277. int i;
  278. GLFWwindow* window;
  279. GLFWcursor* star_cursors[CURSOR_FRAME_COUNT];
  280. GLFWcursor* current_frame = NULL;
  281. GLuint vertex_buffer, vertex_shader, fragment_shader, program;
  282. GLint mvp_location, vpos_location;
  283. glfwSetErrorCallback(error_callback);
  284. if (!glfwInit())
  285. exit(EXIT_FAILURE);
  286. tracking_cursor = create_tracking_cursor();
  287. if (!tracking_cursor)
  288. {
  289. glfwTerminate();
  290. exit(EXIT_FAILURE);
  291. }
  292. for (i = 0; i < CURSOR_FRAME_COUNT; i++)
  293. {
  294. star_cursors[i] = create_cursor_frame(i / (float) CURSOR_FRAME_COUNT);
  295. if (!star_cursors[i])
  296. {
  297. glfwTerminate();
  298. exit(EXIT_FAILURE);
  299. }
  300. }
  301. for (i = 0; i < sizeof(standard_cursors) / sizeof(standard_cursors[0]); i++)
  302. {
  303. const int shapes[] = {
  304. GLFW_ARROW_CURSOR,
  305. GLFW_IBEAM_CURSOR,
  306. GLFW_CROSSHAIR_CURSOR,
  307. GLFW_POINTING_HAND_CURSOR,
  308. GLFW_RESIZE_EW_CURSOR,
  309. GLFW_RESIZE_NS_CURSOR,
  310. GLFW_RESIZE_NWSE_CURSOR,
  311. GLFW_RESIZE_NESW_CURSOR,
  312. GLFW_RESIZE_ALL_CURSOR,
  313. GLFW_NOT_ALLOWED_CURSOR
  314. };
  315. standard_cursors[i] = glfwCreateStandardCursor(shapes[i]);
  316. }
  317. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  318. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  319. window = glfwCreateWindow(640, 480, "Cursor Test", NULL, NULL);
  320. if (!window)
  321. {
  322. glfwTerminate();
  323. exit(EXIT_FAILURE);
  324. }
  325. glfwMakeContextCurrent(window);
  326. gladLoadGL(glfwGetProcAddress);
  327. glGenBuffers(1, &vertex_buffer);
  328. glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
  329. vertex_shader = glCreateShader(GL_VERTEX_SHADER);
  330. glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
  331. glCompileShader(vertex_shader);
  332. fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
  333. glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
  334. glCompileShader(fragment_shader);
  335. program = glCreateProgram();
  336. glAttachShader(program, vertex_shader);
  337. glAttachShader(program, fragment_shader);
  338. glLinkProgram(program);
  339. mvp_location = glGetUniformLocation(program, "MVP");
  340. vpos_location = glGetAttribLocation(program, "vPos");
  341. glEnableVertexAttribArray(vpos_location);
  342. glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
  343. sizeof(vec2), (void*) 0);
  344. glUseProgram(program);
  345. glfwGetCursorPos(window, &cursor_x, &cursor_y);
  346. printf("Cursor position: %f %f\n", cursor_x, cursor_y);
  347. glfwSetCursorPosCallback(window, cursor_position_callback);
  348. glfwSetKeyCallback(window, key_callback);
  349. while (!glfwWindowShouldClose(window))
  350. {
  351. glClear(GL_COLOR_BUFFER_BIT);
  352. if (track_cursor)
  353. {
  354. int wnd_width, wnd_height, fb_width, fb_height;
  355. float scale;
  356. vec2 vertices[4];
  357. mat4x4 mvp;
  358. glfwGetWindowSize(window, &wnd_width, &wnd_height);
  359. glfwGetFramebufferSize(window, &fb_width, &fb_height);
  360. glViewport(0, 0, fb_width, fb_height);
  361. scale = (float) fb_width / (float) wnd_width;
  362. vertices[0][0] = 0.5f;
  363. vertices[0][1] = (float) (fb_height - floor(cursor_y * scale) - 1.f + 0.5f);
  364. vertices[1][0] = (float) fb_width + 0.5f;
  365. vertices[1][1] = (float) (fb_height - floor(cursor_y * scale) - 1.f + 0.5f);
  366. vertices[2][0] = (float) floor(cursor_x * scale) + 0.5f;
  367. vertices[2][1] = 0.5f;
  368. vertices[3][0] = (float) floor(cursor_x * scale) + 0.5f;
  369. vertices[3][1] = (float) fb_height + 0.5f;
  370. glBufferData(GL_ARRAY_BUFFER,
  371. sizeof(vertices),
  372. vertices,
  373. GL_STREAM_DRAW);
  374. mat4x4_ortho(mvp, 0.f, (float) fb_width, 0.f, (float) fb_height, 0.f, 1.f);
  375. glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
  376. glDrawArrays(GL_LINES, 0, 4);
  377. }
  378. glfwSwapBuffers(window);
  379. if (animate_cursor)
  380. {
  381. const int i = (int) (glfwGetTime() * 30.0) % CURSOR_FRAME_COUNT;
  382. if (current_frame != star_cursors[i])
  383. {
  384. glfwSetCursor(window, star_cursors[i]);
  385. current_frame = star_cursors[i];
  386. }
  387. }
  388. else
  389. current_frame = NULL;
  390. if (wait_events)
  391. {
  392. if (animate_cursor)
  393. glfwWaitEventsTimeout(1.0 / 30.0);
  394. else
  395. glfwWaitEvents();
  396. }
  397. else
  398. glfwPollEvents();
  399. // Workaround for an issue with msvcrt and mintty
  400. fflush(stdout);
  401. }
  402. glfwDestroyWindow(window);
  403. for (i = 0; i < CURSOR_FRAME_COUNT; i++)
  404. glfwDestroyCursor(star_cursors[i]);
  405. for (i = 0; i < sizeof(standard_cursors) / sizeof(standard_cursors[0]); i++)
  406. glfwDestroyCursor(standard_cursors[i]);
  407. glfwTerminate();
  408. exit(EXIT_SUCCESS);
  409. }