joysticks.c 11 KB

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