joysticks.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //========================================================================
  2. // Joystick input test
  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 displays the state of every button and axis of every connected
  27. // joystick and/or gamepad
  28. //
  29. //========================================================================
  30. #include <glad/glad.h>
  31. #include <GLFW/glfw3.h>
  32. #define NK_IMPLEMENTATION
  33. #define NK_INCLUDE_FIXED_TYPES
  34. #define NK_INCLUDE_FONT_BAKING
  35. #define NK_INCLUDE_DEFAULT_FONT
  36. #define NK_INCLUDE_DEFAULT_ALLOCATOR
  37. #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  38. #define NK_INCLUDE_STANDARD_VARARGS
  39. #define NK_BUTTON_TRIGGER_ON_RELEASE
  40. #include <nuklear.h>
  41. #define NK_GLFW_GL2_IMPLEMENTATION
  42. #include <nuklear_glfw_gl2.h>
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <stdlib.h>
  46. #ifdef _MSC_VER
  47. #define strdup(x) _strdup(x)
  48. #endif
  49. static GLFWwindow* window;
  50. static int joysticks[GLFW_JOYSTICK_LAST + 1];
  51. static int joystick_count = 0;
  52. static void error_callback(int error, const char* description)
  53. {
  54. fprintf(stderr, "Error: %s\n", description);
  55. }
  56. static void joystick_callback(int jid, int event)
  57. {
  58. if (event == GLFW_CONNECTED)
  59. joysticks[joystick_count++] = jid;
  60. else if (event == GLFW_DISCONNECTED)
  61. {
  62. int i;
  63. for (i = 0; i < joystick_count; i++)
  64. {
  65. if (joysticks[i] == jid)
  66. break;
  67. }
  68. for (i = i + 1; i < joystick_count; i++)
  69. joysticks[i - 1] = joysticks[i];
  70. joystick_count--;
  71. }
  72. if (!glfwGetWindowAttrib(window, GLFW_FOCUSED))
  73. glfwRequestWindowAttention(window);
  74. }
  75. static const char* joystick_label(int jid)
  76. {
  77. static char label[1024];
  78. snprintf(label, sizeof(label), "%i: %s", jid + 1, glfwGetJoystickName(jid));
  79. return label;
  80. }
  81. static void hat_widget(struct nk_context* nk, unsigned char state)
  82. {
  83. float radius;
  84. struct nk_rect area;
  85. struct nk_vec2 center;
  86. if (nk_widget(&area, nk) != NK_WIDGET_VALID)
  87. return;
  88. center = nk_vec2(area.x + area.w / 2.f, area.y + area.h / 2.f);
  89. radius = NK_MIN(area.w, area.h) / 2.f;
  90. nk_stroke_circle(nk_window_get_canvas(nk),
  91. nk_rect(center.x - radius,
  92. center.y - radius,
  93. radius * 2.f,
  94. radius * 2.f),
  95. 1.f,
  96. nk_rgb(175, 175, 175));
  97. if (state)
  98. {
  99. const float angles[] =
  100. {
  101. 0.f, 0.f,
  102. NK_PI * 1.5f, NK_PI * 1.75f,
  103. NK_PI, 0.f,
  104. NK_PI * 1.25f, 0.f,
  105. NK_PI * 0.5f, NK_PI * 0.25f,
  106. 0.f, 0.f,
  107. NK_PI * 0.75f, 0.f,
  108. };
  109. const float cosa = nk_cos(angles[state]);
  110. const float sina = nk_sin(angles[state]);
  111. const struct nk_vec2 p0 = nk_vec2(0.f, -radius);
  112. const struct nk_vec2 p1 = nk_vec2( radius / 2.f, -radius / 3.f);
  113. const struct nk_vec2 p2 = nk_vec2(-radius / 2.f, -radius / 3.f);
  114. nk_fill_triangle(nk_window_get_canvas(nk),
  115. center.x + cosa * p0.x + sina * p0.y,
  116. center.y + cosa * p0.y - sina * p0.x,
  117. center.x + cosa * p1.x + sina * p1.y,
  118. center.y + cosa * p1.y - sina * p1.x,
  119. center.x + cosa * p2.x + sina * p2.y,
  120. center.y + cosa * p2.y - sina * p2.x,
  121. nk_rgb(175, 175, 175));
  122. }
  123. }
  124. int main(void)
  125. {
  126. int jid, hat_buttons = GLFW_FALSE;
  127. struct nk_context* nk;
  128. struct nk_font_atlas* atlas;
  129. memset(joysticks, 0, sizeof(joysticks));
  130. glfwSetErrorCallback(error_callback);
  131. if (!glfwInit())
  132. exit(EXIT_FAILURE);
  133. window = glfwCreateWindow(800, 600, "Joystick Test", NULL, NULL);
  134. if (!window)
  135. {
  136. glfwTerminate();
  137. exit(EXIT_FAILURE);
  138. }
  139. glfwMakeContextCurrent(window);
  140. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  141. glfwSwapInterval(1);
  142. nk = nk_glfw3_init(window, NK_GLFW3_INSTALL_CALLBACKS);
  143. nk_glfw3_font_stash_begin(&atlas);
  144. nk_glfw3_font_stash_end();
  145. for (jid = GLFW_JOYSTICK_1; jid <= GLFW_JOYSTICK_LAST; jid++)
  146. {
  147. if (glfwJoystickPresent(jid))
  148. joysticks[joystick_count++] = jid;
  149. }
  150. glfwSetJoystickCallback(joystick_callback);
  151. while (!glfwWindowShouldClose(window))
  152. {
  153. int i, width, height;
  154. glfwGetWindowSize(window, &width, &height);
  155. glClear(GL_COLOR_BUFFER_BIT);
  156. nk_glfw3_new_frame();
  157. if (nk_begin(nk,
  158. "Joysticks",
  159. nk_rect(width - 200.f, 0.f, 200.f, (float) height),
  160. NK_WINDOW_MINIMIZABLE |
  161. NK_WINDOW_TITLE))
  162. {
  163. nk_layout_row_dynamic(nk, 30, 1);
  164. nk_checkbox_label(nk, "Hat buttons", &hat_buttons);
  165. if (joystick_count)
  166. {
  167. for (i = 0; i < joystick_count; i++)
  168. {
  169. if (nk_button_label(nk, joystick_label(joysticks[i])))
  170. nk_window_set_focus(nk, joystick_label(joysticks[i]));
  171. }
  172. }
  173. else
  174. nk_label(nk, "No joysticks connected", NK_TEXT_LEFT);
  175. }
  176. nk_end(nk);
  177. for (i = 0; i < joystick_count; i++)
  178. {
  179. if (nk_begin(nk,
  180. joystick_label(joysticks[i]),
  181. nk_rect(i * 20.f, i * 20.f, 550.f, 570.f),
  182. NK_WINDOW_BORDER |
  183. NK_WINDOW_MOVABLE |
  184. NK_WINDOW_SCALABLE |
  185. NK_WINDOW_MINIMIZABLE |
  186. NK_WINDOW_TITLE))
  187. {
  188. int j, axis_count, button_count, hat_count;
  189. const float* axes;
  190. const unsigned char* buttons;
  191. const unsigned char* hats;
  192. GLFWgamepadstate state;
  193. nk_layout_row_dynamic(nk, 30, 1);
  194. nk_labelf(nk, NK_TEXT_LEFT, "Hardware GUID %s",
  195. glfwGetJoystickGUID(joysticks[i]));
  196. nk_label(nk, "Joystick state", NK_TEXT_LEFT);
  197. axes = glfwGetJoystickAxes(joysticks[i], &axis_count);
  198. buttons = glfwGetJoystickButtons(joysticks[i], &button_count);
  199. hats = glfwGetJoystickHats(joysticks[i], &hat_count);
  200. if (!hat_buttons)
  201. button_count -= hat_count * 4;
  202. for (j = 0; j < axis_count; j++)
  203. nk_slide_float(nk, -1.f, axes[j], 1.f, 0.1f);
  204. nk_layout_row_dynamic(nk, 30, 12);
  205. for (j = 0; j < button_count; j++)
  206. {
  207. char name[16];
  208. snprintf(name, sizeof(name), "%i", j + 1);
  209. nk_select_label(nk, name, NK_TEXT_CENTERED, buttons[j]);
  210. }
  211. nk_layout_row_dynamic(nk, 30, 8);
  212. for (j = 0; j < hat_count; j++)
  213. hat_widget(nk, hats[j]);
  214. nk_layout_row_dynamic(nk, 30, 1);
  215. if (glfwGetGamepadState(joysticks[i], &state))
  216. {
  217. int hat = 0;
  218. const char* names[GLFW_GAMEPAD_BUTTON_LAST + 1 - 4] =
  219. {
  220. "A", "B", "X", "Y",
  221. "LB", "RB",
  222. "Back", "Start", "Guide",
  223. "LT", "RT",
  224. };
  225. nk_label(nk, "Gamepad state", NK_TEXT_LEFT);
  226. nk_layout_row_dynamic(nk, 30, 2);
  227. for (j = 0; j <= GLFW_GAMEPAD_AXIS_LAST; j++)
  228. nk_slide_float(nk, -1.f, state.axes[j], 1.f, 0.1f);
  229. nk_layout_row_dynamic(nk, 30, GLFW_GAMEPAD_BUTTON_LAST + 1 - 4);
  230. for (j = 0; j <= GLFW_GAMEPAD_BUTTON_LAST - 4; j++)
  231. nk_select_label(nk, names[j], NK_TEXT_CENTERED, state.buttons[j]);
  232. if (state.buttons[GLFW_GAMEPAD_BUTTON_DPAD_UP])
  233. hat |= GLFW_HAT_UP;
  234. if (state.buttons[GLFW_GAMEPAD_BUTTON_DPAD_RIGHT])
  235. hat |= GLFW_HAT_RIGHT;
  236. if (state.buttons[GLFW_GAMEPAD_BUTTON_DPAD_DOWN])
  237. hat |= GLFW_HAT_DOWN;
  238. if (state.buttons[GLFW_GAMEPAD_BUTTON_DPAD_LEFT])
  239. hat |= GLFW_HAT_LEFT;
  240. nk_layout_row_dynamic(nk, 30, 8);
  241. hat_widget(nk, hat);
  242. }
  243. else
  244. nk_label(nk, "Joystick has no gamepad mapping", NK_TEXT_LEFT);
  245. }
  246. nk_end(nk);
  247. }
  248. nk_glfw3_render(NK_ANTI_ALIASING_ON);
  249. glfwSwapBuffers(window);
  250. glfwPollEvents();
  251. }
  252. glfwTerminate();
  253. exit(EXIT_SUCCESS);
  254. }