joysticks.c
Go to the documentation of this file.
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 
31 #include <glad/glad.h>
32 #include <GLFW/glfw3.h>
33 
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 
44 #define NK_GLFW_GL2_IMPLEMENTATION
45 #include <nuklear_glfw_gl2.h>
46 
47 #include <stdio.h>
48 #include <string.h>
49 #include <stdlib.h>
50 
51 #ifdef _MSC_VER
52 #define strdup(x) _strdup(x)
53 #endif
54 
56 static int joysticks[GLFW_JOYSTICK_LAST + 1];
57 static int joystick_count = 0;
58 
59 static void error_callback(int error, const char* description)
60 {
61  fprintf(stderr, "Error: %s\n", description);
62 }
63 
64 static void joystick_callback(int jid, int event)
65 {
66  if (event == GLFW_CONNECTED)
67  joysticks[joystick_count++] = jid;
68  else if (event == GLFW_DISCONNECTED)
69  {
70  int i;
71 
72  for (i = 0; i < joystick_count; i++)
73  {
74  if (joysticks[i] == jid)
75  break;
76  }
77 
78  for (i = i + 1; i < joystick_count; i++)
79  joysticks[i - 1] = joysticks[i];
80 
81  joystick_count--;
82  }
83 
86 }
87 
88 static void drop_callback(GLFWwindow* window, int count, const char** paths)
89 {
90  int i;
91 
92  for (i = 0; i < count; i++)
93  {
94  long size;
95  char* text;
96  FILE* stream = fopen(paths[i], "rb");
97  if (!stream)
98  continue;
99 
100  fseek(stream, 0, SEEK_END);
101  size = ftell(stream);
102  fseek(stream, 0, SEEK_SET);
103 
104  text = malloc(size + 1);
105  text[size] = '\0';
106  if (fread(text, 1, size, stream) == size)
108 
109  free(text);
110  fclose(stream);
111  }
112 }
113 
114 static const char* joystick_label(int jid)
115 {
116  static char label[1024];
117  snprintf(label, sizeof(label), "%i: %s", jid + 1, glfwGetJoystickName(jid));
118  return label;
119 }
120 
121 static void hat_widget(struct nk_context* nk, unsigned char state)
122 {
123  float radius;
124  struct nk_rect area;
125  struct nk_vec2 center;
126 
127  if (nk_widget(&area, nk) == NK_WIDGET_INVALID)
128  return;
129 
130  center = nk_vec2(area.x + area.w / 2.f, area.y + area.h / 2.f);
131  radius = NK_MIN(area.w, area.h) / 2.f;
132 
134  nk_rect(center.x - radius,
135  center.y - radius,
136  radius * 2.f,
137  radius * 2.f),
138  1.f,
139  nk_rgb(175, 175, 175));
140 
141  if (state)
142  {
143  const float angles[] =
144  {
145  0.f, 0.f,
146  NK_PI * 1.5f, NK_PI * 1.75f,
147  NK_PI, 0.f,
148  NK_PI * 1.25f, 0.f,
149  NK_PI * 0.5f, NK_PI * 0.25f,
150  0.f, 0.f,
151  NK_PI * 0.75f, 0.f,
152  };
153  const float cosa = nk_cos(angles[state]);
154  const float sina = nk_sin(angles[state]);
155  const struct nk_vec2 p0 = nk_vec2(0.f, -radius);
156  const struct nk_vec2 p1 = nk_vec2( radius / 2.f, -radius / 3.f);
157  const struct nk_vec2 p2 = nk_vec2(-radius / 2.f, -radius / 3.f);
158 
160  center.x + cosa * p0.x + sina * p0.y,
161  center.y + cosa * p0.y - sina * p0.x,
162  center.x + cosa * p1.x + sina * p1.y,
163  center.y + cosa * p1.y - sina * p1.x,
164  center.x + cosa * p2.x + sina * p2.y,
165  center.y + cosa * p2.y - sina * p2.x,
166  nk_rgb(175, 175, 175));
167  }
168 }
169 
170 int main(void)
171 {
172  int jid, hat_buttons = GLFW_FALSE;
173  struct nk_context* nk;
174  struct nk_font_atlas* atlas;
175 
176  memset(joysticks, 0, sizeof(joysticks));
177 
179 
180  if (!glfwInit())
181  exit(EXIT_FAILURE);
182 
184 
185  window = glfwCreateWindow(800, 600, "Joystick Test", NULL, NULL);
186  if (!window)
187  {
188  glfwTerminate();
189  exit(EXIT_FAILURE);
190  }
191 
194  glfwSwapInterval(1);
195 
199 
200  for (jid = GLFW_JOYSTICK_1; jid <= GLFW_JOYSTICK_LAST; jid++)
201  {
202  if (glfwJoystickPresent(jid))
203  joysticks[joystick_count++] = jid;
204  }
205 
208 
209  while (!glfwWindowShouldClose(window))
210  {
211  int i, width, height;
212 
213  glfwGetWindowSize(window, &width, &height);
214 
217 
218  if (nk_begin(nk,
219  "Joysticks",
220  nk_rect(width - 200.f, 0.f, 200.f, (float) height),
223  {
224  nk_layout_row_dynamic(nk, 30, 1);
225 
226  nk_checkbox_label(nk, "Hat buttons", &hat_buttons);
227 
228  if (joystick_count)
229  {
230  for (i = 0; i < joystick_count; i++)
231  {
234  }
235  }
236  else
237  nk_label(nk, "No joysticks connected", NK_TEXT_LEFT);
238  }
239 
240  nk_end(nk);
241 
242  for (i = 0; i < joystick_count; i++)
243  {
244  if (nk_begin(nk,
246  nk_rect(i * 20.f, i * 20.f, 550.f, 570.f),
252  {
253  int j, axis_count, button_count, hat_count;
254  const float* axes;
255  const unsigned char* buttons;
256  const unsigned char* hats;
258 
259  nk_layout_row_dynamic(nk, 30, 1);
260  nk_labelf(nk, NK_TEXT_LEFT, "Hardware GUID %s",
262  nk_label(nk, "Joystick state", NK_TEXT_LEFT);
263 
264  axes = glfwGetJoystickAxes(joysticks[i], &axis_count);
265  buttons = glfwGetJoystickButtons(joysticks[i], &button_count);
266  hats = glfwGetJoystickHats(joysticks[i], &hat_count);
267 
268  if (!hat_buttons)
269  button_count -= hat_count * 4;
270 
271  for (j = 0; j < axis_count; j++)
272  nk_slide_float(nk, -1.f, axes[j], 1.f, 0.1f);
273 
274  nk_layout_row_dynamic(nk, 30, 12);
275 
276  for (j = 0; j < button_count; j++)
277  {
278  char name[16];
279  snprintf(name, sizeof(name), "%i", j + 1);
280  nk_select_label(nk, name, NK_TEXT_CENTERED, buttons[j]);
281  }
282 
283  nk_layout_row_dynamic(nk, 30, 8);
284 
285  for (j = 0; j < hat_count; j++)
286  hat_widget(nk, hats[j]);
287 
288  nk_layout_row_dynamic(nk, 30, 1);
289 
290  if (glfwGetGamepadState(joysticks[i], &state))
291  {
292  int hat = 0;
293  const char* names[GLFW_GAMEPAD_BUTTON_LAST + 1 - 4] =
294  {
295  "A", "B", "X", "Y",
296  "LB", "RB",
297  "Back", "Start", "Guide",
298  "LT", "RT",
299  };
300 
301  nk_labelf(nk, NK_TEXT_LEFT,
302  "Gamepad state: %s",
304 
305  nk_layout_row_dynamic(nk, 30, 2);
306 
307  for (j = 0; j <= GLFW_GAMEPAD_AXIS_LAST; j++)
308  nk_slide_float(nk, -1.f, state.axes[j], 1.f, 0.1f);
309 
311 
312  for (j = 0; j <= GLFW_GAMEPAD_BUTTON_LAST - 4; j++)
313  nk_select_label(nk, names[j], NK_TEXT_CENTERED, state.buttons[j]);
314 
316  hat |= GLFW_HAT_UP;
318  hat |= GLFW_HAT_RIGHT;
320  hat |= GLFW_HAT_DOWN;
322  hat |= GLFW_HAT_LEFT;
323 
324  nk_layout_row_dynamic(nk, 30, 8);
325  hat_widget(nk, hat);
326  }
327  else
328  nk_label(nk, "Joystick has no gamepad mapping", NK_TEXT_LEFT);
329  }
330 
331  nk_end(nk);
332  }
333 
335 
337  glfwPollEvents();
338  }
339 
340  glfwTerminate();
341  exit(EXIT_SUCCESS);
342 }
343 
Gamepad input state.
Definition: glfw3.h:1612
static void error_callback(int error, const char *description)
Definition: joysticks.c:59
static void joystick_callback(int jid, int event)
Definition: joysticks.c:64
static void drop_callback(GLFWwindow *window, int count, const char **paths)
Definition: joysticks.c:88
#define GLFW_HAT_LEFT
Definition: glfw3.h:325
NK_API int nk_begin(struct nk_context *ctx, const char *title, struct nk_rect bounds, nk_flags flags)
NK_API void nk_stroke_circle(struct nk_command_buffer *, struct nk_rect, float line_thickness, struct nk_color)
static void hat_widget(struct nk_context *nk, unsigned char state)
Definition: joysticks.c:121
GLFWAPI void glfwGetWindowSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the client area of the specified window.
Definition: window.c:544
GLuint const GLchar * name
GLFWAPI int glfwGetWindowAttrib(GLFWwindow *window, int attrib)
Returns an attribute of the specified window.
Definition: window.c:804
#define GLFW_HAT_UP
Definition: glfw3.h:322
NK_API int nk_select_label(struct nk_context *, const char *, nk_flags align, int value)
The header of the GLFW 3 API.
GLFWAPI GLFWglproc glfwGetProcAddress(const char *procname)
Returns the address of the specified function for the current context.
Definition: context.c:741
void *(* GLADloadproc)(const char *name)
GLFWAPI int glfwJoystickPresent(int jid)
Returns whether the specified joystick is present.
Definition: input.c:875
static GLFWwindow * window
Definition: joysticks.c:55
def axes(out, pos, rotation=np.eye(3), size=0.075, thickness=2)
#define GLFW_GAMEPAD_BUTTON_LAST
Definition: glfw3.h:599
GLFWAPI const unsigned char * glfwGetJoystickButtons(int jid, int *count)
Returns the state of all buttons of the specified joystick.
Definition: input.c:926
GLFWAPI GLFWjoystickfun glfwSetJoystickCallback(GLFWjoystickfun cbfun)
Sets the joystick configuration callback.
Definition: input.c:1070
NK_API void nk_window_set_focus(struct nk_context *, const char *name)
GLsizei const GLuint * paths
Definition: glext.h:10532
GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun)
Sets the error callback.
Definition: init.c:309
#define GLFW_FALSE
Zero.
Definition: glfw3.h:287
NK_API void nk_label(struct nk_context *, const char *, nk_flags align)
GLuint GLuint stream
Definition: glext.h:1790
NK_API void nk_glfw3_render(enum nk_anti_aliasing)
GLuint GLuint * names
Definition: glext.h:5648
NK_API struct nk_color nk_rgb(int r, int g, int b)
#define GLFW_SCALE_TO_MONITOR
Window content area scaling window window hint.
Definition: glfw3.h:979
#define GLFW_DISCONNECTED
Definition: glfw3.h:1059
NK_API struct nk_vec2 nk_vec2(float x, float y)
#define GLFW_HAT_RIGHT
Definition: glfw3.h:323
#define GLFW_CONNECTED
Definition: glfw3.h:1058
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
Definition: init.c:198
GLdouble f
NK_API void nk_layout_row_dynamic(struct nk_context *ctx, float height, int cols)
GLFWAPI const char * glfwGetGamepadName(int jid)
Returns the human-readable gamepad name for the specified joystick.
Definition: input.c:1162
GLsizeiptr size
#define GL_COLOR_BUFFER_BIT
NK_API void nk_glfw3_new_frame(void)
float x
Definition: nuklear.h:459
NK_API struct nk_rect nk_rect(float x, float y, float w, float h)
NK_API struct nk_context * nk_glfw3_init(GLFWwindow *win, enum nk_glfw_init_state)
GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate *state)
Retrieves the state of the specified joystick remapped as a gamepad.
Definition: input.c:1190
GLFWAPI void glfwSwapInterval(int interval)
Sets the swap interval for the current context.
Definition: context.c:658
GLFWAPI int glfwUpdateGamepadMappings(const char *string)
Adds the specified SDL_GameControllerDB gamepad mappings.
Definition: input.c:1077
#define glClear
#define GLFW_JOYSTICK_1
Definition: glfw3.h:558
GLint GLsizei GLsizei height
GLFWAPI void glfwSwapBuffers(GLFWwindow *window)
Swaps the front and back buffers of the specified window.
Definition: context.c:641
#define NK_PI
Definition: nuklear.h:5535
GLFWAPI void glfwMakeContextCurrent(GLFWwindow *window)
Makes the context of the specified window current for the calling thread.
Definition: context.c:611
#define GLFW_JOYSTICK_LAST
Definition: glfw3.h:574
NK_API enum nk_widget_layout_states nk_widget(struct nk_rect *, const struct nk_context *)
#define NK_MIN(a, b)
Definition: nuklear.h:299
unsigned char buttons[15]
Definition: glfw3.h:1617
#define GLFW_GAMEPAD_BUTTON_DPAD_LEFT
Definition: glfw3.h:598
GLint j
float y
Definition: nuklear.h:457
#define GLFW_HAT_DOWN
Definition: glfw3.h:324
float x
Definition: nuklear.h:457
GLAPI int gladLoadGLLoader(GLADloadproc)
Definition: glad/glad.c:1697
static const textual_icon exit
Definition: model-views.h:254
#define GLFW_GAMEPAD_BUTTON_DPAD_RIGHT
Definition: glfw3.h:596
#define GLFW_FOCUSED
Input focus window hint and attribute.
Definition: glfw3.h:763
static int joystick_count
Definition: joysticks.c:57
NK_API void nk_fill_triangle(struct nk_command_buffer *, float x0, float y0, float x1, float y1, float x2, float y2, struct nk_color)
struct _cl_event * event
Definition: glext.h:2991
NK_API float nk_slide_float(struct nk_context *, float min, float val, float max, float step)
GLFWAPI GLFWwindow * glfwCreateWindow(int width, int height, const char *title, GLFWmonitor *monitor, GLFWwindow *share)
Creates a window and its associated context.
Definition: window.c:151
#define GLFW_TRUE
One.
Definition: glfw3.h:279
GLFWAPI const unsigned char * glfwGetJoystickHats(int jid, int *count)
Returns the state of all hats of the specified joystick.
Definition: input.c:959
float y
Definition: nuklear.h:459
static int joysticks[GLFW_JOYSTICK_LAST+1]
Definition: joysticks.c:56
#define GLFW_GAMEPAD_AXIS_LAST
Definition: glfw3.h:620
GLFWAPI void glfwRequestWindowAttention(GLFWwindow *window)
Requests user attention to the specified window.
Definition: window.c:771
NK_API struct nk_command_buffer * nk_window_get_canvas(struct nk_context *)
#define GLFW_GAMEPAD_BUTTON_DPAD_DOWN
Definition: glfw3.h:597
GLuint GLsizei const GLchar * label
GLFWAPI GLFWdropfun glfwSetDropCallback(GLFWwindow *window, GLFWdropfun cbfun)
Sets the file drop callback.
Definition: input.c:865
NK_API int nk_checkbox_label(struct nk_context *, const char *, int *active)
GLint GLsizei count
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
Definition: init.c:243
float w
Definition: nuklear.h:459
GLFWAPI void glfwPollEvents(void)
Processes all pending events.
Definition: window.c:1072
int main(void)
Definition: joysticks.c:170
NK_API void nk_glfw3_font_stash_end(void)
float h
Definition: nuklear.h:459
#define NULL
Definition: tinycthread.c:47
NK_API void nk_glfw3_font_stash_begin(struct nk_font_atlas **atlas)
int i
#define GLFW_GAMEPAD_BUTTON_DPAD_UP
Definition: glfw3.h:595
static const char * joystick_label(int jid)
Definition: joysticks.c:114
NK_API void nk_end(struct nk_context *ctx)
float axes[6]
Definition: glfw3.h:1621
NK_API int nk_button_label(struct nk_context *, const char *title)
GLFWAPI const float * glfwGetJoystickAxes(int jid, int *count)
Returns the values of all axes of the specified joystick.
Definition: input.c:897
struct GLFWwindow GLFWwindow
GLFWAPI void glfwWindowHint(int hint, int value)
Sets the specified window hint to the desired value.
Definition: window.c:291
GLint GLsizei width
GLFWAPI const char * glfwGetJoystickGUID(int jid)
Returns the SDL comaptible GUID of the specified joystick.
Definition: input.c:1013
GLFWAPI const char * glfwGetJoystickName(int jid)
Returns the name of the specified joystick.
Definition: input.c:988
GLFWAPI int glfwWindowShouldClose(GLFWwindow *window)
Checks the close flag of the specified window.
Definition: window.c:477


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:20