1b877906bSopenharmony_ci# Input guide {#input_guide}
2b877906bSopenharmony_ci
3b877906bSopenharmony_ci[TOC]
4b877906bSopenharmony_ci
5b877906bSopenharmony_ciThis guide introduces the input related functions of GLFW.  For details on
6b877906bSopenharmony_cia specific function in this category, see the @ref input.  There are also guides
7b877906bSopenharmony_cifor the other areas of GLFW.
8b877906bSopenharmony_ci
9b877906bSopenharmony_ci - @ref intro_guide
10b877906bSopenharmony_ci - @ref window_guide
11b877906bSopenharmony_ci - @ref context_guide
12b877906bSopenharmony_ci - @ref vulkan_guide
13b877906bSopenharmony_ci - @ref monitor_guide
14b877906bSopenharmony_ci
15b877906bSopenharmony_ciGLFW provides many kinds of input.  While some can only be polled, like time, or
16b877906bSopenharmony_cionly received via callbacks, like scrolling, many provide both callbacks and
17b877906bSopenharmony_cipolling.  Callbacks are more work to use than polling but is less CPU intensive
18b877906bSopenharmony_ciand guarantees that you do not miss state changes.
19b877906bSopenharmony_ci
20b877906bSopenharmony_ciAll input callbacks receive a window handle.  By using the
21b877906bSopenharmony_ci[window user pointer](@ref window_userptr), you can access non-global structures
22b877906bSopenharmony_cior objects from your callbacks.
23b877906bSopenharmony_ci
24b877906bSopenharmony_ciTo get a better feel for how the various events callbacks behave, run the
25b877906bSopenharmony_ci`events` test program.  It registers every callback supported by GLFW and prints
26b877906bSopenharmony_ciout all arguments provided for every event, along with time and sequence
27b877906bSopenharmony_ciinformation.
28b877906bSopenharmony_ci
29b877906bSopenharmony_ci
30b877906bSopenharmony_ci## Event processing {#events}
31b877906bSopenharmony_ci
32b877906bSopenharmony_ciGLFW needs to poll the window system for events both to provide input to the
33b877906bSopenharmony_ciapplication and to prove to the window system that the application hasn't locked
34b877906bSopenharmony_ciup.  Event processing is normally done each frame after
35b877906bSopenharmony_ci[buffer swapping](@ref buffer_swap).  Even when you have no windows, event
36b877906bSopenharmony_cipolling needs to be done in order to receive monitor and joystick connection
37b877906bSopenharmony_cievents.
38b877906bSopenharmony_ci
39b877906bSopenharmony_ciThere are three functions for processing pending events.  @ref glfwPollEvents,
40b877906bSopenharmony_ciprocesses only those events that have already been received and then returns
41b877906bSopenharmony_ciimmediately.
42b877906bSopenharmony_ci
43b877906bSopenharmony_ci```c
44b877906bSopenharmony_ciglfwPollEvents();
45b877906bSopenharmony_ci```
46b877906bSopenharmony_ci
47b877906bSopenharmony_ciThis is the best choice when rendering continuously, like most games do.
48b877906bSopenharmony_ci
49b877906bSopenharmony_ciIf you only need to update the contents of the window when you receive new
50b877906bSopenharmony_ciinput, @ref glfwWaitEvents is a better choice.
51b877906bSopenharmony_ci
52b877906bSopenharmony_ci```c
53b877906bSopenharmony_ciglfwWaitEvents();
54b877906bSopenharmony_ci```
55b877906bSopenharmony_ci
56b877906bSopenharmony_ciIt puts the thread to sleep until at least one event has been received and then
57b877906bSopenharmony_ciprocesses all received events.  This saves a great deal of CPU cycles and is
58b877906bSopenharmony_ciuseful for, for example, editing tools.
59b877906bSopenharmony_ci
60b877906bSopenharmony_ciIf you want to wait for events but have UI elements or other tasks that need
61b877906bSopenharmony_ciperiodic updates, @ref glfwWaitEventsTimeout lets you specify a timeout.
62b877906bSopenharmony_ci
63b877906bSopenharmony_ci```c
64b877906bSopenharmony_ciglfwWaitEventsTimeout(0.7);
65b877906bSopenharmony_ci```
66b877906bSopenharmony_ci
67b877906bSopenharmony_ciIt puts the thread to sleep until at least one event has been received, or until
68b877906bSopenharmony_cithe specified number of seconds have elapsed.  It then processes any received
69b877906bSopenharmony_cievents.
70b877906bSopenharmony_ci
71b877906bSopenharmony_ciIf the main thread is sleeping in @ref glfwWaitEvents, you can wake it from
72b877906bSopenharmony_cianother thread by posting an empty event to the event queue with @ref
73b877906bSopenharmony_ciglfwPostEmptyEvent.
74b877906bSopenharmony_ci
75b877906bSopenharmony_ci```c
76b877906bSopenharmony_ciglfwPostEmptyEvent();
77b877906bSopenharmony_ci```
78b877906bSopenharmony_ci
79b877906bSopenharmony_ciDo not assume that callbacks will _only_ be called in response to the above
80b877906bSopenharmony_cifunctions.  While it is necessary to process events in one or more of the ways
81b877906bSopenharmony_ciabove, window systems that require GLFW to register callbacks of its own can
82b877906bSopenharmony_cipass events to GLFW in response to many window system function calls.  GLFW will
83b877906bSopenharmony_cipass those events on to the application callbacks before returning.
84b877906bSopenharmony_ci
85b877906bSopenharmony_ciFor example, on Windows the system function that @ref glfwSetWindowSize is
86b877906bSopenharmony_ciimplemented with will send window size events directly to the event callback
87b877906bSopenharmony_cithat every window has and that GLFW implements for its windows.  If you have set
88b877906bSopenharmony_cia [window size callback](@ref window_size) GLFW will call it in turn with the
89b877906bSopenharmony_cinew size before everything returns back out of the @ref glfwSetWindowSize call.
90b877906bSopenharmony_ci
91b877906bSopenharmony_ci
92b877906bSopenharmony_ci## Keyboard input {#input_keyboard}
93b877906bSopenharmony_ci
94b877906bSopenharmony_ciGLFW divides keyboard input into two categories; key events and character
95b877906bSopenharmony_cievents.  Key events relate to actual physical keyboard keys, whereas character
96b877906bSopenharmony_cievents relate to the text that is generated by pressing some of them.
97b877906bSopenharmony_ci
98b877906bSopenharmony_ciKeys and characters do not map 1:1.  A single key press may produce several
99b877906bSopenharmony_cicharacters, and a single character may require several keys to produce.  This
100b877906bSopenharmony_cimay not be the case on your machine, but your users are likely not all using the
101b877906bSopenharmony_cisame keyboard layout, input method or even operating system as you.
102b877906bSopenharmony_ci
103b877906bSopenharmony_ci
104b877906bSopenharmony_ci### Key input {#input_key}
105b877906bSopenharmony_ci
106b877906bSopenharmony_ciIf you wish to be notified when a physical key is pressed or released or when it
107b877906bSopenharmony_cirepeats, set a key callback.
108b877906bSopenharmony_ci
109b877906bSopenharmony_ci```c
110b877906bSopenharmony_ciglfwSetKeyCallback(window, key_callback);
111b877906bSopenharmony_ci```
112b877906bSopenharmony_ci
113b877906bSopenharmony_ciThe callback function receives the [keyboard key](@ref keys), platform-specific
114b877906bSopenharmony_ciscancode, key action and [modifier bits](@ref mods).
115b877906bSopenharmony_ci
116b877906bSopenharmony_ci```c
117b877906bSopenharmony_civoid key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
118b877906bSopenharmony_ci{
119b877906bSopenharmony_ci    if (key == GLFW_KEY_E && action == GLFW_PRESS)
120b877906bSopenharmony_ci        activate_airship();
121b877906bSopenharmony_ci}
122b877906bSopenharmony_ci```
123b877906bSopenharmony_ci
124b877906bSopenharmony_ciThe action is one of `GLFW_PRESS`, `GLFW_REPEAT` or `GLFW_RELEASE`.  Events with
125b877906bSopenharmony_ci`GLFW_PRESS` and `GLFW_RELEASE` actions are emitted for every key press.  Most
126b877906bSopenharmony_cikeys will also emit events with `GLFW_REPEAT` actions while a key is held down.
127b877906bSopenharmony_ci
128b877906bSopenharmony_ciNote that many keyboards have a limit on how many keys being simultaneous held
129b877906bSopenharmony_cidown that they can detect.  This limit is called
130b877906bSopenharmony_ci[key rollover](https://en.wikipedia.org/wiki/Key_rollover).
131b877906bSopenharmony_ci
132b877906bSopenharmony_ciKey events with `GLFW_REPEAT` actions are intended for text input.  They are
133b877906bSopenharmony_ciemitted at the rate set in the user's keyboard settings.  At most one key is
134b877906bSopenharmony_cirepeated even if several keys are held down.  `GLFW_REPEAT` actions should not
135b877906bSopenharmony_cibe relied on to know which keys are being held down or to drive animation.
136b877906bSopenharmony_ciInstead you should either save the state of relevant keys based on `GLFW_PRESS`
137b877906bSopenharmony_ciand `GLFW_RELEASE` actions, or call @ref glfwGetKey, which provides basic cached
138b877906bSopenharmony_cikey state.
139b877906bSopenharmony_ci
140b877906bSopenharmony_ciThe key will be one of the existing [key tokens](@ref keys), or
141b877906bSopenharmony_ci`GLFW_KEY_UNKNOWN` if GLFW lacks a token for it, for example _E-mail_ and _Play_
142b877906bSopenharmony_cikeys.
143b877906bSopenharmony_ci
144b877906bSopenharmony_ciThe scancode is unique for every key, regardless of whether it has a key token.
145b877906bSopenharmony_ciScancodes are platform-specific but consistent over time, so keys will have
146b877906bSopenharmony_cidifferent scancodes depending on the platform but they are safe to save to disk.
147b877906bSopenharmony_ciYou can query the scancode for any [key token](@ref keys) supported on the
148b877906bSopenharmony_cicurrent platform with @ref glfwGetKeyScancode.
149b877906bSopenharmony_ci
150b877906bSopenharmony_ci```c
151b877906bSopenharmony_ciconst int scancode = glfwGetKeyScancode(GLFW_KEY_X);
152b877906bSopenharmony_ciset_key_mapping(scancode, swap_weapons);
153b877906bSopenharmony_ci```
154b877906bSopenharmony_ci
155b877906bSopenharmony_ciThe last reported state for every physical key with a [key token](@ref keys) is
156b877906bSopenharmony_cialso saved in per-window state arrays that can be polled with @ref glfwGetKey.
157b877906bSopenharmony_ci
158b877906bSopenharmony_ci```c
159b877906bSopenharmony_ciint state = glfwGetKey(window, GLFW_KEY_E);
160b877906bSopenharmony_ciif (state == GLFW_PRESS)
161b877906bSopenharmony_ci{
162b877906bSopenharmony_ci    activate_airship();
163b877906bSopenharmony_ci}
164b877906bSopenharmony_ci```
165b877906bSopenharmony_ci
166b877906bSopenharmony_ciThe returned state is one of `GLFW_PRESS` or `GLFW_RELEASE`.
167b877906bSopenharmony_ci
168b877906bSopenharmony_ciThis function only returns cached key event state.  It does not poll the
169b877906bSopenharmony_cisystem for the current state of the physical key.  It also does not provide any
170b877906bSopenharmony_cikey repeat information.
171b877906bSopenharmony_ci
172b877906bSopenharmony_ci@anchor GLFW_STICKY_KEYS
173b877906bSopenharmony_ciWhenever you poll state, you risk missing the state change you are looking for.
174b877906bSopenharmony_ciIf a pressed key is released again before you poll its state, you will have
175b877906bSopenharmony_cimissed the key press.  The recommended solution for this is to use a
176b877906bSopenharmony_cikey callback, but there is also the `GLFW_STICKY_KEYS` input mode.
177b877906bSopenharmony_ci
178b877906bSopenharmony_ci```c
179b877906bSopenharmony_ciglfwSetInputMode(window, GLFW_STICKY_KEYS, GLFW_TRUE);
180b877906bSopenharmony_ci```
181b877906bSopenharmony_ci
182b877906bSopenharmony_ciWhen sticky keys mode is enabled, the pollable state of a key will remain
183b877906bSopenharmony_ci`GLFW_PRESS` until the state of that key is polled with @ref glfwGetKey.  Once
184b877906bSopenharmony_ciit has been polled, if a key release event had been processed in the meantime,
185b877906bSopenharmony_cithe state will reset to `GLFW_RELEASE`, otherwise it will remain `GLFW_PRESS`.
186b877906bSopenharmony_ci
187b877906bSopenharmony_ci@anchor GLFW_LOCK_KEY_MODS
188b877906bSopenharmony_ciIf you wish to know what the state of the Caps Lock and Num Lock keys was when
189b877906bSopenharmony_ciinput events were generated, set the `GLFW_LOCK_KEY_MODS` input mode.
190b877906bSopenharmony_ci
191b877906bSopenharmony_ci```c
192b877906bSopenharmony_ciglfwSetInputMode(window, GLFW_LOCK_KEY_MODS, GLFW_TRUE);
193b877906bSopenharmony_ci```
194b877906bSopenharmony_ci
195b877906bSopenharmony_ciWhen this input mode is enabled, any callback that receives
196b877906bSopenharmony_ci[modifier bits](@ref mods) will have the @ref GLFW_MOD_CAPS_LOCK bit set if Caps
197b877906bSopenharmony_ciLock was on when the event occurred and the @ref GLFW_MOD_NUM_LOCK bit set if
198b877906bSopenharmony_ciNum Lock was on.
199b877906bSopenharmony_ci
200b877906bSopenharmony_ciThe `GLFW_KEY_LAST` constant holds the highest value of any
201b877906bSopenharmony_ci[key token](@ref keys).
202b877906bSopenharmony_ci
203b877906bSopenharmony_ci
204b877906bSopenharmony_ci### Text input {#input_char}
205b877906bSopenharmony_ci
206b877906bSopenharmony_ciGLFW supports text input in the form of a stream of
207b877906bSopenharmony_ci[Unicode code points](https://en.wikipedia.org/wiki/Unicode), as produced by the
208b877906bSopenharmony_cioperating system text input system.  Unlike key input, text input is affected by
209b877906bSopenharmony_cikeyboard layouts and modifier keys and supports composing characters using
210b877906bSopenharmony_ci[dead keys](https://en.wikipedia.org/wiki/Dead_key).  Once received, you can
211b877906bSopenharmony_ciencode the code points into UTF-8 or any other encoding you prefer.
212b877906bSopenharmony_ci
213b877906bSopenharmony_ciBecause an `unsigned int` is 32 bits long on all platforms supported by GLFW,
214b877906bSopenharmony_ciyou can treat the code point argument as native endian UTF-32.
215b877906bSopenharmony_ci
216b877906bSopenharmony_ciIf you wish to offer regular text input, set a character callback.
217b877906bSopenharmony_ci
218b877906bSopenharmony_ci```c
219b877906bSopenharmony_ciglfwSetCharCallback(window, character_callback);
220b877906bSopenharmony_ci```
221b877906bSopenharmony_ci
222b877906bSopenharmony_ciThe callback function receives Unicode code points for key events that would
223b877906bSopenharmony_cihave led to regular text input and generally behaves as a standard text field on
224b877906bSopenharmony_cithat platform.
225b877906bSopenharmony_ci
226b877906bSopenharmony_ci```c
227b877906bSopenharmony_civoid character_callback(GLFWwindow* window, unsigned int codepoint)
228b877906bSopenharmony_ci{
229b877906bSopenharmony_ci}
230b877906bSopenharmony_ci```
231b877906bSopenharmony_ci
232b877906bSopenharmony_ci
233b877906bSopenharmony_ci### Key names {#input_key_name}
234b877906bSopenharmony_ci
235b877906bSopenharmony_ciIf you wish to refer to keys by name, you can query the keyboard layout
236b877906bSopenharmony_cidependent name of printable keys with @ref glfwGetKeyName.
237b877906bSopenharmony_ci
238b877906bSopenharmony_ci```c
239b877906bSopenharmony_ciconst char* key_name = glfwGetKeyName(GLFW_KEY_W, 0);
240b877906bSopenharmony_cishow_tutorial_hint("Press %s to move forward", key_name);
241b877906bSopenharmony_ci```
242b877906bSopenharmony_ci
243b877906bSopenharmony_ciThis function can handle both [keys and scancodes](@ref input_key).  If the
244b877906bSopenharmony_cispecified key is `GLFW_KEY_UNKNOWN` then the scancode is used, otherwise it is
245b877906bSopenharmony_ciignored.  This matches the behavior of the key callback, meaning the callback
246b877906bSopenharmony_ciarguments can always be passed unmodified to this function.
247b877906bSopenharmony_ci
248b877906bSopenharmony_ci
249b877906bSopenharmony_ci## Mouse input {#input_mouse}
250b877906bSopenharmony_ci
251b877906bSopenharmony_ciMouse input comes in many forms, including mouse motion, button presses and
252b877906bSopenharmony_ciscrolling offsets.  The cursor appearance can also be changed, either to
253b877906bSopenharmony_cia custom image or a standard cursor shape from the system theme.
254b877906bSopenharmony_ci
255b877906bSopenharmony_ci
256b877906bSopenharmony_ci### Cursor position {#cursor_pos}
257b877906bSopenharmony_ci
258b877906bSopenharmony_ciIf you wish to be notified when the cursor moves over the window, set a cursor
259b877906bSopenharmony_ciposition callback.
260b877906bSopenharmony_ci
261b877906bSopenharmony_ci```c
262b877906bSopenharmony_ciglfwSetCursorPosCallback(window, cursor_position_callback);
263b877906bSopenharmony_ci```
264b877906bSopenharmony_ci
265b877906bSopenharmony_ciThe callback functions receives the cursor position, measured in screen
266b877906bSopenharmony_cicoordinates but relative to the top-left corner of the window content area.  On
267b877906bSopenharmony_ciplatforms that provide it, the full sub-pixel cursor position is passed on.
268b877906bSopenharmony_ci
269b877906bSopenharmony_ci```c
270b877906bSopenharmony_cistatic void cursor_position_callback(GLFWwindow* window, double xpos, double ypos)
271b877906bSopenharmony_ci{
272b877906bSopenharmony_ci}
273b877906bSopenharmony_ci```
274b877906bSopenharmony_ci
275b877906bSopenharmony_ciThe cursor position is also saved per-window and can be polled with @ref
276b877906bSopenharmony_ciglfwGetCursorPos.
277b877906bSopenharmony_ci
278b877906bSopenharmony_ci```c
279b877906bSopenharmony_cidouble xpos, ypos;
280b877906bSopenharmony_ciglfwGetCursorPos(window, &xpos, &ypos);
281b877906bSopenharmony_ci```
282b877906bSopenharmony_ci
283b877906bSopenharmony_ci
284b877906bSopenharmony_ci### Cursor mode {#cursor_mode}
285b877906bSopenharmony_ci
286b877906bSopenharmony_ci@anchor GLFW_CURSOR
287b877906bSopenharmony_ciThe `GLFW_CURSOR` input mode provides several cursor modes for special forms of
288b877906bSopenharmony_cimouse motion input.  By default, the cursor mode is `GLFW_CURSOR_NORMAL`,
289b877906bSopenharmony_cimeaning the regular arrow cursor (or another cursor set with @ref glfwSetCursor)
290b877906bSopenharmony_ciis used and cursor motion is not limited.
291b877906bSopenharmony_ci
292b877906bSopenharmony_ciIf you wish to implement mouse motion based camera controls or other input
293b877906bSopenharmony_cischemes that require unlimited mouse movement, set the cursor mode to
294b877906bSopenharmony_ci`GLFW_CURSOR_DISABLED`.
295b877906bSopenharmony_ci
296b877906bSopenharmony_ci```c
297b877906bSopenharmony_ciglfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
298b877906bSopenharmony_ci```
299b877906bSopenharmony_ci
300b877906bSopenharmony_ciThis will hide the cursor and lock it to the specified window.  GLFW will then
301b877906bSopenharmony_citake care of all the details of cursor re-centering and offset calculation and
302b877906bSopenharmony_ciproviding the application with a virtual cursor position.  This virtual position
303b877906bSopenharmony_ciis provided normally via both the cursor position callback and through polling.
304b877906bSopenharmony_ci
305b877906bSopenharmony_ci@note You should not implement your own version of this functionality using
306b877906bSopenharmony_ciother features of GLFW.  It is not supported and will not work as robustly as
307b877906bSopenharmony_ci`GLFW_CURSOR_DISABLED`.
308b877906bSopenharmony_ci
309b877906bSopenharmony_ciIf you only wish the cursor to become hidden when it is over a window but still
310b877906bSopenharmony_ciwant it to behave normally, set the cursor mode to `GLFW_CURSOR_HIDDEN`.
311b877906bSopenharmony_ci
312b877906bSopenharmony_ci```c
313b877906bSopenharmony_ciglfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
314b877906bSopenharmony_ci```
315b877906bSopenharmony_ci
316b877906bSopenharmony_ciThis mode puts no limit on the motion of the cursor.
317b877906bSopenharmony_ci
318b877906bSopenharmony_ciIf you wish the cursor to be visible but confined to the content area of the
319b877906bSopenharmony_ciwindow, set the cursor mode to `GLFW_CURSOR_CAPTURED`.
320b877906bSopenharmony_ci
321b877906bSopenharmony_ci```c
322b877906bSopenharmony_ciglfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_CAPTURED);
323b877906bSopenharmony_ci```
324b877906bSopenharmony_ci
325b877906bSopenharmony_ciThe cursor will behave normally inside the content area but will not be able to
326b877906bSopenharmony_cileave unless the window loses focus.
327b877906bSopenharmony_ci
328b877906bSopenharmony_ciTo exit out of either of these special modes, restore the `GLFW_CURSOR_NORMAL`
329b877906bSopenharmony_cicursor mode.
330b877906bSopenharmony_ci
331b877906bSopenharmony_ci```c
332b877906bSopenharmony_ciglfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
333b877906bSopenharmony_ci```
334b877906bSopenharmony_ci
335b877906bSopenharmony_ciIf the cursor was disabled, this will move it back to its last visible position.
336b877906bSopenharmony_ci
337b877906bSopenharmony_ci
338b877906bSopenharmony_ci@anchor GLFW_RAW_MOUSE_MOTION
339b877906bSopenharmony_ci### Raw mouse motion {#raw_mouse_motion}
340b877906bSopenharmony_ci
341b877906bSopenharmony_ciWhen the cursor is disabled, raw (unscaled and unaccelerated) mouse motion can
342b877906bSopenharmony_cibe enabled if available.
343b877906bSopenharmony_ci
344b877906bSopenharmony_ciRaw mouse motion is closer to the actual motion of the mouse across a surface.
345b877906bSopenharmony_ciIt is not affected by the scaling and acceleration applied to the motion of the
346b877906bSopenharmony_cidesktop cursor.  That processing is suitable for a cursor while raw motion is
347b877906bSopenharmony_cibetter for controlling for example a 3D camera.  Because of this, raw mouse
348b877906bSopenharmony_cimotion is only provided when the cursor is disabled.
349b877906bSopenharmony_ci
350b877906bSopenharmony_ciCall @ref glfwRawMouseMotionSupported to check if the current machine provides
351b877906bSopenharmony_ciraw motion and set the `GLFW_RAW_MOUSE_MOTION` input mode to enable it.  It is
352b877906bSopenharmony_cidisabled by default.
353b877906bSopenharmony_ci
354b877906bSopenharmony_ci```c
355b877906bSopenharmony_ciif (glfwRawMouseMotionSupported())
356b877906bSopenharmony_ci    glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
357b877906bSopenharmony_ci```
358b877906bSopenharmony_ci
359b877906bSopenharmony_ciIf supported, raw mouse motion can be enabled or disabled per-window and at any
360b877906bSopenharmony_citime but it will only be provided when the cursor is disabled.
361b877906bSopenharmony_ci
362b877906bSopenharmony_ci
363b877906bSopenharmony_ci### Cursor objects {#cursor_object}
364b877906bSopenharmony_ci
365b877906bSopenharmony_ciGLFW supports creating both custom and system theme cursor images, encapsulated
366b877906bSopenharmony_cias @ref GLFWcursor objects.  They are created with @ref glfwCreateCursor or @ref
367b877906bSopenharmony_ciglfwCreateStandardCursor and destroyed with @ref glfwDestroyCursor, or @ref
368b877906bSopenharmony_ciglfwTerminate, if any remain.
369b877906bSopenharmony_ci
370b877906bSopenharmony_ci
371b877906bSopenharmony_ci#### Custom cursor creation {#cursor_custom}
372b877906bSopenharmony_ci
373b877906bSopenharmony_ciA custom cursor is created with @ref glfwCreateCursor, which returns a handle to
374b877906bSopenharmony_cithe created cursor object.  For example, this creates a 16x16 white square
375b877906bSopenharmony_cicursor with the hot-spot in the upper-left corner:
376b877906bSopenharmony_ci
377b877906bSopenharmony_ci```c
378b877906bSopenharmony_ciunsigned char pixels[16 * 16 * 4];
379b877906bSopenharmony_cimemset(pixels, 0xff, sizeof(pixels));
380b877906bSopenharmony_ci
381b877906bSopenharmony_ciGLFWimage image;
382b877906bSopenharmony_ciimage.width = 16;
383b877906bSopenharmony_ciimage.height = 16;
384b877906bSopenharmony_ciimage.pixels = pixels;
385b877906bSopenharmony_ci
386b877906bSopenharmony_ciGLFWcursor* cursor = glfwCreateCursor(&image, 0, 0);
387b877906bSopenharmony_ci```
388b877906bSopenharmony_ci
389b877906bSopenharmony_ciIf cursor creation fails, `NULL` will be returned, so it is necessary to check
390b877906bSopenharmony_cithe return value.
391b877906bSopenharmony_ci
392b877906bSopenharmony_ciThe image data is 32-bit, little-endian, non-premultiplied RGBA, i.e. eight bits
393b877906bSopenharmony_ciper channel with the red channel first.  The pixels are arranged canonically as
394b877906bSopenharmony_cisequential rows, starting from the top-left corner.
395b877906bSopenharmony_ci
396b877906bSopenharmony_ci
397b877906bSopenharmony_ci#### Standard cursor creation {#cursor_standard}
398b877906bSopenharmony_ci
399b877906bSopenharmony_ciA cursor with a [standard shape](@ref shapes) from the current system cursor
400b877906bSopenharmony_citheme can be created with @ref glfwCreateStandardCursor.
401b877906bSopenharmony_ci
402b877906bSopenharmony_ci```c
403b877906bSopenharmony_ciGLFWcursor* url_cursor = glfwCreateStandardCursor(GLFW_POINTING_HAND_CURSOR);
404b877906bSopenharmony_ci```
405b877906bSopenharmony_ci
406b877906bSopenharmony_ciThese cursor objects behave in the exact same way as those created with @ref
407b877906bSopenharmony_ciglfwCreateCursor except that the system cursor theme provides the actual image.
408b877906bSopenharmony_ci
409b877906bSopenharmony_ciA few of these shapes are not available everywhere.  If a shape is unavailable,
410b877906bSopenharmony_ci`NULL` is returned.  See @ref glfwCreateStandardCursor for details.
411b877906bSopenharmony_ci
412b877906bSopenharmony_ci
413b877906bSopenharmony_ci#### Cursor destruction {#cursor_destruction}
414b877906bSopenharmony_ci
415b877906bSopenharmony_ciWhen a cursor is no longer needed, destroy it with @ref glfwDestroyCursor.
416b877906bSopenharmony_ci
417b877906bSopenharmony_ci```c
418b877906bSopenharmony_ciglfwDestroyCursor(cursor);
419b877906bSopenharmony_ci```
420b877906bSopenharmony_ci
421b877906bSopenharmony_ciCursor destruction always succeeds.  If the cursor is current for any window,
422b877906bSopenharmony_cithat window will revert to the default cursor.  This does not affect the cursor
423b877906bSopenharmony_cimode.  All remaining cursors are destroyed when @ref glfwTerminate is called.
424b877906bSopenharmony_ci
425b877906bSopenharmony_ci
426b877906bSopenharmony_ci#### Cursor setting {#cursor_set}
427b877906bSopenharmony_ci
428b877906bSopenharmony_ciA cursor can be set as current for a window with @ref glfwSetCursor.
429b877906bSopenharmony_ci
430b877906bSopenharmony_ci```c
431b877906bSopenharmony_ciglfwSetCursor(window, cursor);
432b877906bSopenharmony_ci```
433b877906bSopenharmony_ci
434b877906bSopenharmony_ciOnce set, the cursor image will be used as long as the system cursor is over the
435b877906bSopenharmony_cicontent area of the window and the [cursor mode](@ref cursor_mode) is set
436b877906bSopenharmony_cito `GLFW_CURSOR_NORMAL`.
437b877906bSopenharmony_ci
438b877906bSopenharmony_ciA single cursor may be set for any number of windows.
439b877906bSopenharmony_ci
440b877906bSopenharmony_ciTo revert to the default cursor, set the cursor of that window to `NULL`.
441b877906bSopenharmony_ci
442b877906bSopenharmony_ci```c
443b877906bSopenharmony_ciglfwSetCursor(window, NULL);
444b877906bSopenharmony_ci```
445b877906bSopenharmony_ci
446b877906bSopenharmony_ciWhen a cursor is destroyed, any window that has it set will revert to the
447b877906bSopenharmony_cidefault cursor.  This does not affect the cursor mode.
448b877906bSopenharmony_ci
449b877906bSopenharmony_ci
450b877906bSopenharmony_ci### Cursor enter/leave events {#cursor_enter}
451b877906bSopenharmony_ci
452b877906bSopenharmony_ciIf you wish to be notified when the cursor enters or leaves the content area of
453b877906bSopenharmony_cia window, set a cursor enter/leave callback.
454b877906bSopenharmony_ci
455b877906bSopenharmony_ci```c
456b877906bSopenharmony_ciglfwSetCursorEnterCallback(window, cursor_enter_callback);
457b877906bSopenharmony_ci```
458b877906bSopenharmony_ci
459b877906bSopenharmony_ciThe callback function receives the new classification of the cursor.
460b877906bSopenharmony_ci
461b877906bSopenharmony_ci```c
462b877906bSopenharmony_civoid cursor_enter_callback(GLFWwindow* window, int entered)
463b877906bSopenharmony_ci{
464b877906bSopenharmony_ci    if (entered)
465b877906bSopenharmony_ci    {
466b877906bSopenharmony_ci        // The cursor entered the content area of the window
467b877906bSopenharmony_ci    }
468b877906bSopenharmony_ci    else
469b877906bSopenharmony_ci    {
470b877906bSopenharmony_ci        // The cursor left the content area of the window
471b877906bSopenharmony_ci    }
472b877906bSopenharmony_ci}
473b877906bSopenharmony_ci```
474b877906bSopenharmony_ci
475b877906bSopenharmony_ciYou can query whether the cursor is currently inside the content area of the
476b877906bSopenharmony_ciwindow with the [GLFW_HOVERED](@ref GLFW_HOVERED_attrib) window attribute.
477b877906bSopenharmony_ci
478b877906bSopenharmony_ci```c
479b877906bSopenharmony_ciif (glfwGetWindowAttrib(window, GLFW_HOVERED))
480b877906bSopenharmony_ci{
481b877906bSopenharmony_ci    highlight_interface();
482b877906bSopenharmony_ci}
483b877906bSopenharmony_ci```
484b877906bSopenharmony_ci
485b877906bSopenharmony_ci
486b877906bSopenharmony_ci### Mouse button input {#input_mouse_button}
487b877906bSopenharmony_ci
488b877906bSopenharmony_ciIf you wish to be notified when a mouse button is pressed or released, set
489b877906bSopenharmony_cia mouse button callback.
490b877906bSopenharmony_ci
491b877906bSopenharmony_ci```c
492b877906bSopenharmony_ciglfwSetMouseButtonCallback(window, mouse_button_callback);
493b877906bSopenharmony_ci```
494b877906bSopenharmony_ci
495b877906bSopenharmony_ci@anchor GLFW_UNLIMITED_MOUSE_BUTTONS
496b877906bSopenharmony_ciTo handle all mouse buttons in the callback, instead of only ones with associated
497b877906bSopenharmony_ci[button tokens](@ref buttons), set the @ref GLFW_UNLIMITED_MOUSE_BUTTONS
498b877906bSopenharmony_ciinput mode.
499b877906bSopenharmony_ci
500b877906bSopenharmony_ci```c
501b877906bSopenharmony_ciglfwSetInputMode(window, GLFW_UNLIMITED_MOUSE_BUTTONS, GLFW_TRUE);
502b877906bSopenharmony_ci```
503b877906bSopenharmony_ci
504b877906bSopenharmony_ciWhen this input mode is enabled, GLFW doesn't limit the reported mouse buttons
505b877906bSopenharmony_cito only those that have an associated button token, for compatibility with
506b877906bSopenharmony_ciearlier versions of GLFW, which never reported any buttons over
507b877906bSopenharmony_ci@ref GLFW_MOUSE_BUTTON_LAST, on which users could have relied on.
508b877906bSopenharmony_ci
509b877906bSopenharmony_ciThe callback function receives the [mouse button](@ref buttons), button action
510b877906bSopenharmony_ciand [modifier bits](@ref mods).
511b877906bSopenharmony_ci
512b877906bSopenharmony_ci```c
513b877906bSopenharmony_civoid mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
514b877906bSopenharmony_ci{
515b877906bSopenharmony_ci    if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS)
516b877906bSopenharmony_ci        popup_menu();
517b877906bSopenharmony_ci}
518b877906bSopenharmony_ci```
519b877906bSopenharmony_ci
520b877906bSopenharmony_ciThe mouse button is an integer that can be one of the
521b877906bSopenharmony_ci[mouse button tokens](@ref buttons) or, if the
522b877906bSopenharmony_ci@ref GLFW_UNLIMITED_MOUSE_BUTTONS input mode is set, any other positive value.
523b877906bSopenharmony_ci
524b877906bSopenharmony_ciThe action is one of `GLFW_PRESS` or `GLFW_RELEASE`.
525b877906bSopenharmony_ci
526b877906bSopenharmony_ciThe last reported state for every [mouse button token](@ref buttons) is also
527b877906bSopenharmony_cisaved in per-window state arrays that can be polled with @ref
528b877906bSopenharmony_ciglfwGetMouseButton. This is not effected by the @ref GLFW_UNLIMITED_MOUSE_BUTTONS
529b877906bSopenharmony_ciinput mode.
530b877906bSopenharmony_ci
531b877906bSopenharmony_ci```c
532b877906bSopenharmony_ciint state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT);
533b877906bSopenharmony_ciif (state == GLFW_PRESS)
534b877906bSopenharmony_ci{
535b877906bSopenharmony_ci    upgrade_cow();
536b877906bSopenharmony_ci}
537b877906bSopenharmony_ci```
538b877906bSopenharmony_ci
539b877906bSopenharmony_ciThe returned state is one of `GLFW_PRESS` or `GLFW_RELEASE`.
540b877906bSopenharmony_ci
541b877906bSopenharmony_ciThis function only returns cached mouse button event state.  It does not poll
542b877906bSopenharmony_cithe system for the current state of the mouse button.
543b877906bSopenharmony_ci
544b877906bSopenharmony_ci@anchor GLFW_STICKY_MOUSE_BUTTONS
545b877906bSopenharmony_ciWhenever you poll state, you risk missing the state change you are looking for.
546b877906bSopenharmony_ciIf a pressed mouse button is released again before you poll its state, you will have
547b877906bSopenharmony_cimissed the button press.  The recommended solution for this is to use a
548b877906bSopenharmony_cimouse button callback, but there is also the `GLFW_STICKY_MOUSE_BUTTONS`
549b877906bSopenharmony_ciinput mode.
550b877906bSopenharmony_ci
551b877906bSopenharmony_ci```c
552b877906bSopenharmony_ciglfwSetInputMode(window, GLFW_STICKY_MOUSE_BUTTONS, GLFW_TRUE);
553b877906bSopenharmony_ci```
554b877906bSopenharmony_ci
555b877906bSopenharmony_ciWhen sticky mouse buttons mode is enabled, the pollable state of a mouse button
556b877906bSopenharmony_ciwill remain `GLFW_PRESS` until the state of that button is polled with @ref
557b877906bSopenharmony_ciglfwGetMouseButton.  Once it has been polled, if a mouse button release event
558b877906bSopenharmony_cihad been processed in the meantime, the state will reset to `GLFW_RELEASE`,
559b877906bSopenharmony_ciotherwise it will remain `GLFW_PRESS`.
560b877906bSopenharmony_ci
561b877906bSopenharmony_ciThe `GLFW_MOUSE_BUTTON_LAST` constant holds the highest value of any
562b877906bSopenharmony_ci[mouse button token](@ref buttons).
563b877906bSopenharmony_ci
564b877906bSopenharmony_ci
565b877906bSopenharmony_ci### Scroll input {#scrolling}
566b877906bSopenharmony_ci
567b877906bSopenharmony_ciIf you wish to be notified when the user scrolls, whether with a mouse wheel or
568b877906bSopenharmony_citouchpad gesture, set a scroll callback.
569b877906bSopenharmony_ci
570b877906bSopenharmony_ci```c
571b877906bSopenharmony_ciglfwSetScrollCallback(window, scroll_callback);
572b877906bSopenharmony_ci```
573b877906bSopenharmony_ci
574b877906bSopenharmony_ciThe callback function receives two-dimensional scroll offsets.
575b877906bSopenharmony_ci
576b877906bSopenharmony_ci```c
577b877906bSopenharmony_civoid scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
578b877906bSopenharmony_ci{
579b877906bSopenharmony_ci}
580b877906bSopenharmony_ci```
581b877906bSopenharmony_ci
582b877906bSopenharmony_ciA normal mouse wheel, being vertical, provides offsets along the Y-axis.
583b877906bSopenharmony_ci
584b877906bSopenharmony_ci
585b877906bSopenharmony_ci## Joystick input {#joystick}
586b877906bSopenharmony_ci
587b877906bSopenharmony_ciThe joystick functions expose connected joysticks and controllers, with both
588b877906bSopenharmony_cireferred to as joysticks.  It supports up to sixteen joysticks, ranging from
589b877906bSopenharmony_ci`GLFW_JOYSTICK_1`, `GLFW_JOYSTICK_2` up to and including `GLFW_JOYSTICK_16` or
590b877906bSopenharmony_ci`GLFW_JOYSTICK_LAST`.  You can test whether a [joystick](@ref joysticks) is
591b877906bSopenharmony_cipresent with @ref glfwJoystickPresent.
592b877906bSopenharmony_ci
593b877906bSopenharmony_ci```c
594b877906bSopenharmony_ciint present = glfwJoystickPresent(GLFW_JOYSTICK_1);
595b877906bSopenharmony_ci```
596b877906bSopenharmony_ci
597b877906bSopenharmony_ciEach joystick has zero or more axes, zero or more buttons, zero or more hats,
598b877906bSopenharmony_cia human-readable name, a user pointer and an SDL compatible GUID.
599b877906bSopenharmony_ci
600b877906bSopenharmony_ciDetected joysticks are added to the beginning of the array.  Once a joystick is
601b877906bSopenharmony_cidetected, it keeps its assigned ID until it is disconnected or the library is
602b877906bSopenharmony_citerminated, so as joysticks are connected and disconnected, there may appear
603b877906bSopenharmony_cigaps in the IDs.
604b877906bSopenharmony_ci
605b877906bSopenharmony_ciJoystick axis, button and hat state is updated when polled and does not require
606b877906bSopenharmony_cia window to be created or events to be processed.  However, if you want joystick
607b877906bSopenharmony_ciconnection and disconnection events reliably delivered to the
608b877906bSopenharmony_ci[joystick callback](@ref joystick_event) then you must
609b877906bSopenharmony_ci[process events](@ref events).
610b877906bSopenharmony_ci
611b877906bSopenharmony_ciTo see all the properties of all connected joysticks in real-time, run the
612b877906bSopenharmony_ci`joysticks` test program.
613b877906bSopenharmony_ci
614b877906bSopenharmony_ci
615b877906bSopenharmony_ci### Joystick axis states {#joystick_axis}
616b877906bSopenharmony_ci
617b877906bSopenharmony_ciThe positions of all axes of a joystick are returned by @ref
618b877906bSopenharmony_ciglfwGetJoystickAxes.  See the reference documentation for the lifetime of the
619b877906bSopenharmony_cireturned array.
620b877906bSopenharmony_ci
621b877906bSopenharmony_ci```c
622b877906bSopenharmony_ciint count;
623b877906bSopenharmony_ciconst float* axes = glfwGetJoystickAxes(GLFW_JOYSTICK_5, &count);
624b877906bSopenharmony_ci```
625b877906bSopenharmony_ci
626b877906bSopenharmony_ciEach element in the returned array is a value between -1.0 and 1.0.
627b877906bSopenharmony_ci
628b877906bSopenharmony_ci
629b877906bSopenharmony_ci### Joystick button states {#joystick_button}
630b877906bSopenharmony_ci
631b877906bSopenharmony_ciThe states of all buttons of a joystick are returned by @ref
632b877906bSopenharmony_ciglfwGetJoystickButtons.  See the reference documentation for the lifetime of the
633b877906bSopenharmony_cireturned array.
634b877906bSopenharmony_ci
635b877906bSopenharmony_ci```c
636b877906bSopenharmony_ciint count;
637b877906bSopenharmony_ciconst unsigned char* buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_3, &count);
638b877906bSopenharmony_ci```
639b877906bSopenharmony_ci
640b877906bSopenharmony_ciEach element in the returned array is either `GLFW_PRESS` or `GLFW_RELEASE`.
641b877906bSopenharmony_ci
642b877906bSopenharmony_ciFor backward compatibility with earlier versions that did not have @ref
643b877906bSopenharmony_ciglfwGetJoystickHats, the button array by default also includes all hats.  See
644b877906bSopenharmony_cithe reference documentation for @ref glfwGetJoystickButtons for details.
645b877906bSopenharmony_ci
646b877906bSopenharmony_ci
647b877906bSopenharmony_ci### Joystick hat states {#joystick_hat}
648b877906bSopenharmony_ci
649b877906bSopenharmony_ciThe states of all hats are returned by @ref glfwGetJoystickHats.  See the
650b877906bSopenharmony_cireference documentation for the lifetime of the returned array.
651b877906bSopenharmony_ci
652b877906bSopenharmony_ci```c
653b877906bSopenharmony_ciint count;
654b877906bSopenharmony_ciconst unsigned char* hats = glfwGetJoystickHats(GLFW_JOYSTICK_7, &count);
655b877906bSopenharmony_ci```
656b877906bSopenharmony_ci
657b877906bSopenharmony_ciEach element in the returned array is one of the following:
658b877906bSopenharmony_ci
659b877906bSopenharmony_ciName                  | Value
660b877906bSopenharmony_ci----                  | -----
661b877906bSopenharmony_ci`GLFW_HAT_CENTERED`   | 0
662b877906bSopenharmony_ci`GLFW_HAT_UP`         | 1
663b877906bSopenharmony_ci`GLFW_HAT_RIGHT`      | 2
664b877906bSopenharmony_ci`GLFW_HAT_DOWN`       | 4
665b877906bSopenharmony_ci`GLFW_HAT_LEFT`       | 8
666b877906bSopenharmony_ci`GLFW_HAT_RIGHT_UP`   | `GLFW_HAT_RIGHT` \| `GLFW_HAT_UP`
667b877906bSopenharmony_ci`GLFW_HAT_RIGHT_DOWN` | `GLFW_HAT_RIGHT` \| `GLFW_HAT_DOWN`
668b877906bSopenharmony_ci`GLFW_HAT_LEFT_UP`    | `GLFW_HAT_LEFT` \| `GLFW_HAT_UP`
669b877906bSopenharmony_ci`GLFW_HAT_LEFT_DOWN`  | `GLFW_HAT_LEFT` \| `GLFW_HAT_DOWN`
670b877906bSopenharmony_ci
671b877906bSopenharmony_ciThe diagonal directions are bitwise combinations of the primary (up, right, down
672b877906bSopenharmony_ciand left) directions and you can test for these individually by ANDing it with
673b877906bSopenharmony_cithe corresponding direction.
674b877906bSopenharmony_ci
675b877906bSopenharmony_ci```c
676b877906bSopenharmony_ciif (hats[2] & GLFW_HAT_RIGHT)
677b877906bSopenharmony_ci{
678b877906bSopenharmony_ci    // State of hat 2 could be right-up, right or right-down
679b877906bSopenharmony_ci}
680b877906bSopenharmony_ci```
681b877906bSopenharmony_ci
682b877906bSopenharmony_ciFor backward compatibility with earlier versions that did not have @ref
683b877906bSopenharmony_ciglfwGetJoystickHats, all hats are by default also included in the button array.
684b877906bSopenharmony_ciSee the reference documentation for @ref glfwGetJoystickButtons for details.
685b877906bSopenharmony_ci
686b877906bSopenharmony_ci
687b877906bSopenharmony_ci### Joystick name {#joystick_name}
688b877906bSopenharmony_ci
689b877906bSopenharmony_ciThe human-readable, UTF-8 encoded name of a joystick is returned by @ref
690b877906bSopenharmony_ciglfwGetJoystickName.  See the reference documentation for the lifetime of the
691b877906bSopenharmony_cireturned string.
692b877906bSopenharmony_ci
693b877906bSopenharmony_ci```c
694b877906bSopenharmony_ciconst char* name = glfwGetJoystickName(GLFW_JOYSTICK_4);
695b877906bSopenharmony_ci```
696b877906bSopenharmony_ci
697b877906bSopenharmony_ciJoystick names are not guaranteed to be unique.  Two joysticks of the same model
698b877906bSopenharmony_ciand make may have the same name.  Only the [joystick ID](@ref joysticks) is
699b877906bSopenharmony_ciguaranteed to be unique, and only until that joystick is disconnected.
700b877906bSopenharmony_ci
701b877906bSopenharmony_ci
702b877906bSopenharmony_ci### Joystick user pointer {#joystick_userptr}
703b877906bSopenharmony_ci
704b877906bSopenharmony_ciEach joystick has a user pointer that can be set with @ref
705b877906bSopenharmony_ciglfwSetJoystickUserPointer and queried with @ref glfwGetJoystickUserPointer.
706b877906bSopenharmony_ciThis can be used for any purpose you need and will not be modified by GLFW.  The
707b877906bSopenharmony_civalue will be kept until the joystick is disconnected or until the library is
708b877906bSopenharmony_citerminated.
709b877906bSopenharmony_ci
710b877906bSopenharmony_ciThe initial value of the pointer is `NULL`.
711b877906bSopenharmony_ci
712b877906bSopenharmony_ci
713b877906bSopenharmony_ci### Joystick configuration changes {#joystick_event}
714b877906bSopenharmony_ci
715b877906bSopenharmony_ciIf you wish to be notified when a joystick is connected or disconnected, set
716b877906bSopenharmony_cia joystick callback.
717b877906bSopenharmony_ci
718b877906bSopenharmony_ci```c
719b877906bSopenharmony_ciglfwSetJoystickCallback(joystick_callback);
720b877906bSopenharmony_ci```
721b877906bSopenharmony_ci
722b877906bSopenharmony_ciThe callback function receives the ID of the joystick that has been connected
723b877906bSopenharmony_ciand disconnected and the event that occurred.
724b877906bSopenharmony_ci
725b877906bSopenharmony_ci```c
726b877906bSopenharmony_civoid joystick_callback(int jid, int event)
727b877906bSopenharmony_ci{
728b877906bSopenharmony_ci    if (event == GLFW_CONNECTED)
729b877906bSopenharmony_ci    {
730b877906bSopenharmony_ci        // The joystick was connected
731b877906bSopenharmony_ci    }
732b877906bSopenharmony_ci    else if (event == GLFW_DISCONNECTED)
733b877906bSopenharmony_ci    {
734b877906bSopenharmony_ci        // The joystick was disconnected
735b877906bSopenharmony_ci    }
736b877906bSopenharmony_ci}
737b877906bSopenharmony_ci```
738b877906bSopenharmony_ci
739b877906bSopenharmony_ciFor joystick connection and disconnection events to be delivered on all
740b877906bSopenharmony_ciplatforms, you need to call one of the [event processing](@ref events)
741b877906bSopenharmony_cifunctions.  Joystick disconnection may also be detected and the callback
742b877906bSopenharmony_cicalled by joystick functions.  The function will then return whatever it
743b877906bSopenharmony_cireturns for a disconnected joystick.
744b877906bSopenharmony_ci
745b877906bSopenharmony_ciOnly @ref glfwGetJoystickName and @ref glfwGetJoystickUserPointer will return
746b877906bSopenharmony_ciuseful values for a disconnected joystick and only before the monitor callback
747b877906bSopenharmony_cireturns.
748b877906bSopenharmony_ci
749b877906bSopenharmony_ci
750b877906bSopenharmony_ci### Gamepad input {#gamepad}
751b877906bSopenharmony_ci
752b877906bSopenharmony_ciThe joystick functions provide unlabeled axes, buttons and hats, with no
753b877906bSopenharmony_ciindication of where they are located on the device.  Their order may also vary
754b877906bSopenharmony_cibetween platforms even with the same device.
755b877906bSopenharmony_ci
756b877906bSopenharmony_ciTo solve this problem the SDL community crowdsourced the
757b877906bSopenharmony_ci[SDL_GameControllerDB][] project, a database of mappings from many different
758b877906bSopenharmony_cidevices to an Xbox-like gamepad.
759b877906bSopenharmony_ci
760b877906bSopenharmony_ci[SDL_GameControllerDB]: https://github.com/gabomdq/SDL_GameControllerDB
761b877906bSopenharmony_ci
762b877906bSopenharmony_ciGLFW supports this mapping format and contains a copy of the mappings
763b877906bSopenharmony_ciavailable at the time of release.  See @ref gamepad_mapping for how to update
764b877906bSopenharmony_cithis at runtime.  Mappings will be assigned to joysticks automatically any time
765b877906bSopenharmony_cia joystick is connected or the mappings are updated.
766b877906bSopenharmony_ci
767b877906bSopenharmony_ciYou can check whether a joystick is both present and has a gamepad mapping with
768b877906bSopenharmony_ci@ref glfwJoystickIsGamepad.
769b877906bSopenharmony_ci
770b877906bSopenharmony_ci```c
771b877906bSopenharmony_ciif (glfwJoystickIsGamepad(GLFW_JOYSTICK_2))
772b877906bSopenharmony_ci{
773b877906bSopenharmony_ci    // Use as gamepad
774b877906bSopenharmony_ci}
775b877906bSopenharmony_ci```
776b877906bSopenharmony_ci
777b877906bSopenharmony_ciIf you are only interested in gamepad input you can use this function instead of
778b877906bSopenharmony_ci@ref glfwJoystickPresent.
779b877906bSopenharmony_ci
780b877906bSopenharmony_ciYou can query the human-readable name provided by the gamepad mapping with @ref
781b877906bSopenharmony_ciglfwGetGamepadName.  This may or may not be the same as the
782b877906bSopenharmony_ci[joystick name](@ref joystick_name).
783b877906bSopenharmony_ci
784b877906bSopenharmony_ci```c
785b877906bSopenharmony_ciconst char* name = glfwGetGamepadName(GLFW_JOYSTICK_7);
786b877906bSopenharmony_ci```
787b877906bSopenharmony_ci
788b877906bSopenharmony_ciTo retrieve the gamepad state of a joystick, call @ref glfwGetGamepadState.
789b877906bSopenharmony_ci
790b877906bSopenharmony_ci```c
791b877906bSopenharmony_ciGLFWgamepadstate state;
792b877906bSopenharmony_ci
793b877906bSopenharmony_ciif (glfwGetGamepadState(GLFW_JOYSTICK_3, &state))
794b877906bSopenharmony_ci{
795b877906bSopenharmony_ci    if (state.buttons[GLFW_GAMEPAD_BUTTON_A])
796b877906bSopenharmony_ci    {
797b877906bSopenharmony_ci        input_jump();
798b877906bSopenharmony_ci    }
799b877906bSopenharmony_ci
800b877906bSopenharmony_ci    input_speed(state.axes[GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER]);
801b877906bSopenharmony_ci}
802b877906bSopenharmony_ci```
803b877906bSopenharmony_ci
804b877906bSopenharmony_ciThe @ref GLFWgamepadstate struct has two arrays; one for button states and one
805b877906bSopenharmony_cifor axis states.  The values for each button and axis are the same as for the
806b877906bSopenharmony_ci@ref glfwGetJoystickButtons and @ref glfwGetJoystickAxes functions, i.e.
807b877906bSopenharmony_ci`GLFW_PRESS` or `GLFW_RELEASE` for buttons and -1.0 to 1.0 inclusive for axes.
808b877906bSopenharmony_ci
809b877906bSopenharmony_ciThe sizes of the arrays and the positions within each array are fixed.
810b877906bSopenharmony_ci
811b877906bSopenharmony_ciThe [button indices](@ref gamepad_buttons) are `GLFW_GAMEPAD_BUTTON_A`,
812b877906bSopenharmony_ci`GLFW_GAMEPAD_BUTTON_B`, `GLFW_GAMEPAD_BUTTON_X`, `GLFW_GAMEPAD_BUTTON_Y`,
813b877906bSopenharmony_ci`GLFW_GAMEPAD_BUTTON_LEFT_BUMPER`, `GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER`,
814b877906bSopenharmony_ci`GLFW_GAMEPAD_BUTTON_BACK`, `GLFW_GAMEPAD_BUTTON_START`,
815b877906bSopenharmony_ci`GLFW_GAMEPAD_BUTTON_GUIDE`, `GLFW_GAMEPAD_BUTTON_LEFT_THUMB`,
816b877906bSopenharmony_ci`GLFW_GAMEPAD_BUTTON_RIGHT_THUMB`, `GLFW_GAMEPAD_BUTTON_DPAD_UP`,
817b877906bSopenharmony_ci`GLFW_GAMEPAD_BUTTON_DPAD_RIGHT`, `GLFW_GAMEPAD_BUTTON_DPAD_DOWN` and
818b877906bSopenharmony_ci`GLFW_GAMEPAD_BUTTON_DPAD_LEFT`.
819b877906bSopenharmony_ci
820b877906bSopenharmony_ciFor those who prefer, there are also the `GLFW_GAMEPAD_BUTTON_CROSS`,
821b877906bSopenharmony_ci`GLFW_GAMEPAD_BUTTON_CIRCLE`, `GLFW_GAMEPAD_BUTTON_SQUARE` and
822b877906bSopenharmony_ci`GLFW_GAMEPAD_BUTTON_TRIANGLE` aliases for the A, B, X and Y button indices.
823b877906bSopenharmony_ci
824b877906bSopenharmony_ciThe [axis indices](@ref gamepad_axes) are `GLFW_GAMEPAD_AXIS_LEFT_X`,
825b877906bSopenharmony_ci`GLFW_GAMEPAD_AXIS_LEFT_Y`, `GLFW_GAMEPAD_AXIS_RIGHT_X`,
826b877906bSopenharmony_ci`GLFW_GAMEPAD_AXIS_RIGHT_Y`, `GLFW_GAMEPAD_AXIS_LEFT_TRIGGER` and
827b877906bSopenharmony_ci`GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER`.
828b877906bSopenharmony_ci
829b877906bSopenharmony_ciThe `GLFW_GAMEPAD_BUTTON_LAST` and `GLFW_GAMEPAD_AXIS_LAST` constants equal
830b877906bSopenharmony_cithe largest available index for each array.
831b877906bSopenharmony_ci
832b877906bSopenharmony_ci
833b877906bSopenharmony_ci### Gamepad mappings {#gamepad_mapping}
834b877906bSopenharmony_ci
835b877906bSopenharmony_ciGLFW contains a copy of the mappings available in [SDL_GameControllerDB][] at
836b877906bSopenharmony_cithe time of release.  Newer ones can be added at runtime with @ref
837b877906bSopenharmony_ciglfwUpdateGamepadMappings.
838b877906bSopenharmony_ci
839b877906bSopenharmony_ci```c
840b877906bSopenharmony_ciconst char* mappings = load_file_contents("game/data/gamecontrollerdb.txt");
841b877906bSopenharmony_ci
842b877906bSopenharmony_ciglfwUpdateGamepadMappings(mappings);
843b877906bSopenharmony_ci```
844b877906bSopenharmony_ci
845b877906bSopenharmony_ciThis function supports everything from single lines up to and including the
846b877906bSopenharmony_ciunmodified contents of the whole `gamecontrollerdb.txt` file.
847b877906bSopenharmony_ci
848b877906bSopenharmony_ciIf you are compiling GLFW from source with CMake you can update the built-in mappings by
849b877906bSopenharmony_cibuilding the _update_mappings_ target.  This runs the `GenerateMappings.cmake` CMake
850b877906bSopenharmony_ciscript, which downloads `gamecontrollerdb.txt` and regenerates the `mappings.h` header
851b877906bSopenharmony_cifile.
852b877906bSopenharmony_ci
853b877906bSopenharmony_ciBelow is a description of the mapping format.  Please keep in mind that __this
854b877906bSopenharmony_cidescription is not authoritative__.  The format is defined by the SDL and
855b877906bSopenharmony_ciSDL_GameControllerDB projects and their documentation and code takes precedence.
856b877906bSopenharmony_ci
857b877906bSopenharmony_ciEach mapping is a single line of comma-separated values describing the GUID,
858b877906bSopenharmony_ciname and layout of the gamepad.  Lines that do not begin with a hexadecimal
859b877906bSopenharmony_cidigit are ignored.
860b877906bSopenharmony_ci
861b877906bSopenharmony_ciThe first value is always the gamepad GUID, a 32 character long hexadecimal
862b877906bSopenharmony_cistring that typically identifies its make, model, revision and the type of
863b877906bSopenharmony_ciconnection to the computer.  When this information is not available, the GUID is
864b877906bSopenharmony_cigenerated using the gamepad name.  GLFW uses the SDL 2.0.5+ GUID format but can
865b877906bSopenharmony_ciconvert from the older formats.
866b877906bSopenharmony_ci
867b877906bSopenharmony_ciThe second value is always the human-readable name of the gamepad.
868b877906bSopenharmony_ci
869b877906bSopenharmony_ciAll subsequent values are in the form `<field>:<value>` and describe the layout
870b877906bSopenharmony_ciof the mapping.  These fields may not all be present and may occur in any order.
871b877906bSopenharmony_ci
872b877906bSopenharmony_ciThe button fields are `a`, `b`, `x`, `y`, `back`, `start`, `guide`, `dpup`,
873b877906bSopenharmony_ci`dpright`, `dpdown`, `dpleft`, `leftshoulder`, `rightshoulder`, `leftstick` and
874b877906bSopenharmony_ci`rightstick`.
875b877906bSopenharmony_ci
876b877906bSopenharmony_ciThe axis fields are `leftx`, `lefty`, `rightx`, `righty`, `lefttrigger` and
877b877906bSopenharmony_ci`righttrigger`.
878b877906bSopenharmony_ci
879b877906bSopenharmony_ciThe value of an axis or button field can be a joystick button, a joystick axis,
880b877906bSopenharmony_cia hat bitmask or empty.  Joystick buttons are specified as `bN`, for example
881b877906bSopenharmony_ci`b2` for the third button.  Joystick axes are specified as `aN`, for example
882b877906bSopenharmony_ci`a7` for the eighth button.  Joystick hat bit masks are specified as `hN.N`, for
883b877906bSopenharmony_ciexample `h0.8` for left on the first hat.  More than one bit may be set in the
884b877906bSopenharmony_cimask.
885b877906bSopenharmony_ci
886b877906bSopenharmony_ciBefore an axis there may be a `+` or `-` range modifier, for example `+a3` for
887b877906bSopenharmony_cithe positive half of the fourth axis.  This restricts input to only the positive
888b877906bSopenharmony_cior negative halves of the joystick axis.  After an axis or half-axis there may
889b877906bSopenharmony_cibe the `~` inversion modifier, for example `a2~` or `-a7~`.  This negates the
890b877906bSopenharmony_civalues of the gamepad axis.
891b877906bSopenharmony_ci
892b877906bSopenharmony_ciThe hat bit mask match the [hat states](@ref hat_state) in the joystick
893b877906bSopenharmony_cifunctions.
894b877906bSopenharmony_ci
895b877906bSopenharmony_ciThere is also the special `platform` field that specifies which platform the
896b877906bSopenharmony_cimapping is valid for.  Possible values are `Windows`, `Mac OS X` and `Linux`.
897b877906bSopenharmony_ci
898b877906bSopenharmony_ciBelow is an example of what a gamepad mapping might look like.  It is the
899b877906bSopenharmony_cione built into GLFW for Xbox controllers accessed via the XInput API on Windows.
900b877906bSopenharmony_ciThis example has been broken into several lines to fit on the page, but real
901b877906bSopenharmony_cigamepad mappings must be a single line.
902b877906bSopenharmony_ci
903b877906bSopenharmony_ci```
904b877906bSopenharmony_ci78696e70757401000000000000000000,XInput Gamepad (GLFW),platform:Windows,a:b0,
905b877906bSopenharmony_cib:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,
906b877906bSopenharmony_cirightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,
907b877906bSopenharmony_cirighttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,
908b877906bSopenharmony_ci```
909b877906bSopenharmony_ci
910b877906bSopenharmony_ci@note GLFW does not yet support the output range and modifiers `+` and `-` that
911b877906bSopenharmony_ciwere recently added to SDL.  The input modifiers `+`, `-` and `~` are supported
912b877906bSopenharmony_ciand described above.
913b877906bSopenharmony_ci
914b877906bSopenharmony_ci
915b877906bSopenharmony_ci## Time input {#time}
916b877906bSopenharmony_ci
917b877906bSopenharmony_ciGLFW provides high-resolution time input, in seconds, with @ref glfwGetTime.
918b877906bSopenharmony_ci
919b877906bSopenharmony_ci```c
920b877906bSopenharmony_cidouble seconds = glfwGetTime();
921b877906bSopenharmony_ci```
922b877906bSopenharmony_ci
923b877906bSopenharmony_ciIt returns the number of seconds since the library was initialized with @ref
924b877906bSopenharmony_ciglfwInit.  The platform-specific time sources used typically have micro- or
925b877906bSopenharmony_cinanosecond resolution.
926b877906bSopenharmony_ci
927b877906bSopenharmony_ciYou can modify the base time with @ref glfwSetTime.
928b877906bSopenharmony_ci
929b877906bSopenharmony_ci```c
930b877906bSopenharmony_ciglfwSetTime(4.0);
931b877906bSopenharmony_ci```
932b877906bSopenharmony_ci
933b877906bSopenharmony_ciThis sets the time to the specified time, in seconds, and it continues to count
934b877906bSopenharmony_cifrom there.
935b877906bSopenharmony_ci
936b877906bSopenharmony_ciYou can also access the raw timer used to implement the functions above,
937b877906bSopenharmony_ciwith @ref glfwGetTimerValue.
938b877906bSopenharmony_ci
939b877906bSopenharmony_ci```c
940b877906bSopenharmony_ciuint64_t value = glfwGetTimerValue();
941b877906bSopenharmony_ci```
942b877906bSopenharmony_ci
943b877906bSopenharmony_ciThis value is in 1&nbsp;/&nbsp;frequency seconds.  The frequency of the raw
944b877906bSopenharmony_citimer varies depending on the operating system and hardware.  You can query the
945b877906bSopenharmony_cifrequency, in Hz, with @ref glfwGetTimerFrequency.
946b877906bSopenharmony_ci
947b877906bSopenharmony_ci```c
948b877906bSopenharmony_ciuint64_t frequency = glfwGetTimerFrequency();
949b877906bSopenharmony_ci```
950b877906bSopenharmony_ci
951b877906bSopenharmony_ci
952b877906bSopenharmony_ci## Clipboard input and output {#clipboard}
953b877906bSopenharmony_ci
954b877906bSopenharmony_ciIf the system clipboard contains a UTF-8 encoded string or if it can be
955b877906bSopenharmony_ciconverted to one, you can retrieve it with @ref glfwGetClipboardString.  See the
956b877906bSopenharmony_cireference documentation for the lifetime of the returned string.
957b877906bSopenharmony_ci
958b877906bSopenharmony_ci```c
959b877906bSopenharmony_ciconst char* text = glfwGetClipboardString(NULL);
960b877906bSopenharmony_ciif (text)
961b877906bSopenharmony_ci{
962b877906bSopenharmony_ci    insert_text(text);
963b877906bSopenharmony_ci}
964b877906bSopenharmony_ci```
965b877906bSopenharmony_ci
966b877906bSopenharmony_ciIf the clipboard is empty or if its contents could not be converted, `NULL` is
967b877906bSopenharmony_cireturned.
968b877906bSopenharmony_ci
969b877906bSopenharmony_ciThe contents of the system clipboard can be set to a UTF-8 encoded string with
970b877906bSopenharmony_ci@ref glfwSetClipboardString.
971b877906bSopenharmony_ci
972b877906bSopenharmony_ci```c
973b877906bSopenharmony_ciglfwSetClipboardString(NULL, "A string with words in it");
974b877906bSopenharmony_ci```
975b877906bSopenharmony_ci
976b877906bSopenharmony_ci
977b877906bSopenharmony_ci## Path drop input {#path_drop}
978b877906bSopenharmony_ci
979b877906bSopenharmony_ciIf you wish to receive the paths of files and/or directories dropped on
980b877906bSopenharmony_cia window, set a file drop callback.
981b877906bSopenharmony_ci
982b877906bSopenharmony_ci```c
983b877906bSopenharmony_ciglfwSetDropCallback(window, drop_callback);
984b877906bSopenharmony_ci```
985b877906bSopenharmony_ci
986b877906bSopenharmony_ciThe callback function receives an array of paths encoded as UTF-8.
987b877906bSopenharmony_ci
988b877906bSopenharmony_ci```c
989b877906bSopenharmony_civoid drop_callback(GLFWwindow* window, int count, const char** paths)
990b877906bSopenharmony_ci{
991b877906bSopenharmony_ci    int i;
992b877906bSopenharmony_ci    for (i = 0;  i < count;  i++)
993b877906bSopenharmony_ci        handle_dropped_file(paths[i]);
994b877906bSopenharmony_ci}
995b877906bSopenharmony_ci```
996b877906bSopenharmony_ci
997b877906bSopenharmony_ciThe path array and its strings are only valid until the file drop callback
998b877906bSopenharmony_cireturns, as they may have been generated specifically for that event.  You need
999b877906bSopenharmony_cito make a deep copy of the array if you want to keep the paths.
1000b877906bSopenharmony_ci
1001