joysticks.c 11 KB

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