input.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  1. //========================================================================
  2. // GLFW 3.4 - www.glfw.org
  3. //------------------------------------------------------------------------
  4. // Copyright (c) 2002-2006 Marcus Geelnard
  5. // Copyright (c) 2006-2019 Camilla Löwy <[email protected]>
  6. //
  7. // This software is provided 'as-is', without any express or implied
  8. // warranty. In no event will the authors be held liable for any damages
  9. // arising from the use of this software.
  10. //
  11. // Permission is granted to anyone to use this software for any purpose,
  12. // including commercial applications, and to alter it and redistribute it
  13. // freely, subject to the following restrictions:
  14. //
  15. // 1. The origin of this software must not be misrepresented; you must not
  16. // claim that you wrote the original software. If you use this software
  17. // in a product, an acknowledgment in the product documentation would
  18. // be appreciated but is not required.
  19. //
  20. // 2. Altered source versions must be plainly marked as such, and must not
  21. // be misrepresented as being the original software.
  22. //
  23. // 3. This notice may not be removed or altered from any source
  24. // distribution.
  25. //
  26. //========================================================================
  27. // Please use C89 style variable declarations in this file because VS 2010
  28. //========================================================================
  29. #include "internal.h"
  30. #include <assert.h>
  31. #include <float.h>
  32. #include <math.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. // Internal key state used for sticky keys
  36. #define _GLFW_STICK 3
  37. // Internal constants for gamepad mapping source types
  38. #define _GLFW_JOYSTICK_AXIS 1
  39. #define _GLFW_JOYSTICK_BUTTON 2
  40. #define _GLFW_JOYSTICK_HATBIT 3
  41. // Finds a mapping based on joystick GUID
  42. //
  43. static _GLFWmapping* findMapping(const char* guid)
  44. {
  45. int i;
  46. for (i = 0; i < _glfw.mappingCount; i++)
  47. {
  48. if (strcmp(_glfw.mappings[i].guid, guid) == 0)
  49. return _glfw.mappings + i;
  50. }
  51. return NULL;
  52. }
  53. // Checks whether a gamepad mapping element is present in the hardware
  54. //
  55. static GLFWbool isValidElementForJoystick(const _GLFWmapelement* e,
  56. const _GLFWjoystick* js)
  57. {
  58. if (e->type == _GLFW_JOYSTICK_HATBIT && (e->index >> 4) >= js->hatCount)
  59. return GLFW_FALSE;
  60. else if (e->type == _GLFW_JOYSTICK_BUTTON && e->index >= js->buttonCount)
  61. return GLFW_FALSE;
  62. else if (e->type == _GLFW_JOYSTICK_AXIS && e->index >= js->axisCount)
  63. return GLFW_FALSE;
  64. return GLFW_TRUE;
  65. }
  66. // Finds a mapping based on joystick GUID and verifies element indices
  67. //
  68. static _GLFWmapping* findValidMapping(const _GLFWjoystick* js)
  69. {
  70. _GLFWmapping* mapping = findMapping(js->guid);
  71. if (mapping)
  72. {
  73. int i;
  74. for (i = 0; i <= GLFW_GAMEPAD_BUTTON_LAST; i++)
  75. {
  76. if (!isValidElementForJoystick(mapping->buttons + i, js))
  77. {
  78. _glfwInputError(GLFW_INVALID_VALUE,
  79. "Invalid button in gamepad mapping %s (%s)",
  80. mapping->guid,
  81. mapping->name);
  82. return NULL;
  83. }
  84. }
  85. for (i = 0; i <= GLFW_GAMEPAD_AXIS_LAST; i++)
  86. {
  87. if (!isValidElementForJoystick(mapping->axes + i, js))
  88. {
  89. _glfwInputError(GLFW_INVALID_VALUE,
  90. "Invalid axis in gamepad mapping %s (%s)",
  91. mapping->guid,
  92. mapping->name);
  93. return NULL;
  94. }
  95. }
  96. }
  97. return mapping;
  98. }
  99. // Parses an SDL_GameControllerDB line and adds it to the mapping list
  100. //
  101. static GLFWbool parseMapping(_GLFWmapping* mapping, const char* string)
  102. {
  103. const char* c = string;
  104. size_t i, length;
  105. struct
  106. {
  107. const char* name;
  108. _GLFWmapelement* element;
  109. } fields[] =
  110. {
  111. { "platform", NULL },
  112. { "a", mapping->buttons + GLFW_GAMEPAD_BUTTON_A },
  113. { "b", mapping->buttons + GLFW_GAMEPAD_BUTTON_B },
  114. { "x", mapping->buttons + GLFW_GAMEPAD_BUTTON_X },
  115. { "y", mapping->buttons + GLFW_GAMEPAD_BUTTON_Y },
  116. { "back", mapping->buttons + GLFW_GAMEPAD_BUTTON_BACK },
  117. { "start", mapping->buttons + GLFW_GAMEPAD_BUTTON_START },
  118. { "guide", mapping->buttons + GLFW_GAMEPAD_BUTTON_GUIDE },
  119. { "leftshoulder", mapping->buttons + GLFW_GAMEPAD_BUTTON_LEFT_BUMPER },
  120. { "rightshoulder", mapping->buttons + GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER },
  121. { "leftstick", mapping->buttons + GLFW_GAMEPAD_BUTTON_LEFT_THUMB },
  122. { "rightstick", mapping->buttons + GLFW_GAMEPAD_BUTTON_RIGHT_THUMB },
  123. { "dpup", mapping->buttons + GLFW_GAMEPAD_BUTTON_DPAD_UP },
  124. { "dpright", mapping->buttons + GLFW_GAMEPAD_BUTTON_DPAD_RIGHT },
  125. { "dpdown", mapping->buttons + GLFW_GAMEPAD_BUTTON_DPAD_DOWN },
  126. { "dpleft", mapping->buttons + GLFW_GAMEPAD_BUTTON_DPAD_LEFT },
  127. { "lefttrigger", mapping->axes + GLFW_GAMEPAD_AXIS_LEFT_TRIGGER },
  128. { "righttrigger", mapping->axes + GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER },
  129. { "leftx", mapping->axes + GLFW_GAMEPAD_AXIS_LEFT_X },
  130. { "lefty", mapping->axes + GLFW_GAMEPAD_AXIS_LEFT_Y },
  131. { "rightx", mapping->axes + GLFW_GAMEPAD_AXIS_RIGHT_X },
  132. { "righty", mapping->axes + GLFW_GAMEPAD_AXIS_RIGHT_Y }
  133. };
  134. length = strcspn(c, ",");
  135. if (length != 32 || c[length] != ',')
  136. {
  137. _glfwInputError(GLFW_INVALID_VALUE, NULL);
  138. return GLFW_FALSE;
  139. }
  140. memcpy(mapping->guid, c, length);
  141. c += length + 1;
  142. length = strcspn(c, ",");
  143. if (length >= sizeof(mapping->name) || c[length] != ',')
  144. {
  145. _glfwInputError(GLFW_INVALID_VALUE, NULL);
  146. return GLFW_FALSE;
  147. }
  148. memcpy(mapping->name, c, length);
  149. c += length + 1;
  150. while (*c)
  151. {
  152. // TODO: Implement output modifiers
  153. if (*c == '+' || *c == '-')
  154. return GLFW_FALSE;
  155. for (i = 0; i < sizeof(fields) / sizeof(fields[0]); i++)
  156. {
  157. length = strlen(fields[i].name);
  158. if (strncmp(c, fields[i].name, length) != 0 || c[length] != ':')
  159. continue;
  160. c += length + 1;
  161. if (fields[i].element)
  162. {
  163. _GLFWmapelement* e = fields[i].element;
  164. int8_t minimum = -1;
  165. int8_t maximum = 1;
  166. if (*c == '+')
  167. {
  168. minimum = 0;
  169. c += 1;
  170. }
  171. else if (*c == '-')
  172. {
  173. maximum = 0;
  174. c += 1;
  175. }
  176. if (*c == 'a')
  177. e->type = _GLFW_JOYSTICK_AXIS;
  178. else if (*c == 'b')
  179. e->type = _GLFW_JOYSTICK_BUTTON;
  180. else if (*c == 'h')
  181. e->type = _GLFW_JOYSTICK_HATBIT;
  182. else
  183. break;
  184. if (e->type == _GLFW_JOYSTICK_HATBIT)
  185. {
  186. const unsigned long hat = strtoul(c + 1, (char**) &c, 10);
  187. const unsigned long bit = strtoul(c + 1, (char**) &c, 10);
  188. e->index = (uint8_t) ((hat << 4) | bit);
  189. }
  190. else
  191. e->index = (uint8_t) strtoul(c + 1, (char**) &c, 10);
  192. if (e->type == _GLFW_JOYSTICK_AXIS)
  193. {
  194. e->axisScale = 2 / (maximum - minimum);
  195. e->axisOffset = -(maximum + minimum);
  196. if (*c == '~')
  197. {
  198. e->axisScale = -e->axisScale;
  199. e->axisOffset = -e->axisOffset;
  200. }
  201. }
  202. }
  203. else
  204. {
  205. length = strlen(_GLFW_PLATFORM_MAPPING_NAME);
  206. if (strncmp(c, _GLFW_PLATFORM_MAPPING_NAME, length) != 0)
  207. return GLFW_FALSE;
  208. }
  209. break;
  210. }
  211. c += strcspn(c, ",");
  212. c += strspn(c, ",");
  213. }
  214. for (i = 0; i < 32; i++)
  215. {
  216. if (mapping->guid[i] >= 'A' && mapping->guid[i] <= 'F')
  217. mapping->guid[i] += 'a' - 'A';
  218. }
  219. _glfwPlatformUpdateGamepadGUID(mapping->guid);
  220. return GLFW_TRUE;
  221. }
  222. //////////////////////////////////////////////////////////////////////////
  223. ////// GLFW event API //////
  224. //////////////////////////////////////////////////////////////////////////
  225. // Notifies shared code of a physical key event
  226. //
  227. void _glfwInputKey(_GLFWwindow* window, int key, int scancode, int action, int mods)
  228. {
  229. if (key >= 0 && key <= GLFW_KEY_LAST)
  230. {
  231. GLFWbool repeated = GLFW_FALSE;
  232. if (action == GLFW_RELEASE && window->keys[key] == GLFW_RELEASE)
  233. return;
  234. if (action == GLFW_PRESS && window->keys[key] == GLFW_PRESS)
  235. repeated = GLFW_TRUE;
  236. if (action == GLFW_RELEASE && window->stickyKeys)
  237. window->keys[key] = _GLFW_STICK;
  238. else
  239. window->keys[key] = (char) action;
  240. if (repeated)
  241. action = GLFW_REPEAT;
  242. }
  243. if (!window->lockKeyMods)
  244. mods &= ~(GLFW_MOD_CAPS_LOCK | GLFW_MOD_NUM_LOCK);
  245. if (window->callbacks.key)
  246. window->callbacks.key((GLFWwindow*) window, key, scancode, action, mods);
  247. }
  248. // Notifies shared code of a Unicode codepoint input event
  249. // The 'plain' parameter determines whether to emit a regular character event
  250. //
  251. void _glfwInputChar(_GLFWwindow* window, unsigned int codepoint, int mods, GLFWbool plain)
  252. {
  253. if (codepoint < 32 || (codepoint > 126 && codepoint < 160))
  254. return;
  255. if (!window->lockKeyMods)
  256. mods &= ~(GLFW_MOD_CAPS_LOCK | GLFW_MOD_NUM_LOCK);
  257. if (window->callbacks.charmods)
  258. window->callbacks.charmods((GLFWwindow*) window, codepoint, mods);
  259. if (plain)
  260. {
  261. if (window->callbacks.character)
  262. window->callbacks.character((GLFWwindow*) window, codepoint);
  263. }
  264. }
  265. // Notifies shared code of a scroll event
  266. //
  267. void _glfwInputScroll(_GLFWwindow* window, double xoffset, double yoffset)
  268. {
  269. if (window->callbacks.scroll)
  270. window->callbacks.scroll((GLFWwindow*) window, xoffset, yoffset);
  271. }
  272. // Notifies shared code of a mouse button click event
  273. //
  274. void _glfwInputMouseClick(_GLFWwindow* window, int button, int action, int mods)
  275. {
  276. if (button < 0 || button > GLFW_MOUSE_BUTTON_LAST)
  277. return;
  278. if (!window->lockKeyMods)
  279. mods &= ~(GLFW_MOD_CAPS_LOCK | GLFW_MOD_NUM_LOCK);
  280. if (action == GLFW_RELEASE && window->stickyMouseButtons)
  281. window->mouseButtons[button] = _GLFW_STICK;
  282. else
  283. window->mouseButtons[button] = (char) action;
  284. if (window->callbacks.mouseButton)
  285. window->callbacks.mouseButton((GLFWwindow*) window, button, action, mods);
  286. }
  287. // Notifies shared code of a cursor motion event
  288. // The position is specified in content area relative screen coordinates
  289. //
  290. void _glfwInputCursorPos(_GLFWwindow* window, double xpos, double ypos)
  291. {
  292. if (window->virtualCursorPosX == xpos && window->virtualCursorPosY == ypos)
  293. return;
  294. window->virtualCursorPosX = xpos;
  295. window->virtualCursorPosY = ypos;
  296. if (window->callbacks.cursorPos)
  297. window->callbacks.cursorPos((GLFWwindow*) window, xpos, ypos);
  298. }
  299. // Notifies shared code of a cursor enter/leave event
  300. //
  301. void _glfwInputCursorEnter(_GLFWwindow* window, GLFWbool entered)
  302. {
  303. if (window->callbacks.cursorEnter)
  304. window->callbacks.cursorEnter((GLFWwindow*) window, entered);
  305. }
  306. // Notifies shared code of files or directories dropped on a window
  307. //
  308. void _glfwInputDrop(_GLFWwindow* window, int count, const char** paths)
  309. {
  310. if (window->callbacks.drop)
  311. window->callbacks.drop((GLFWwindow*) window, count, paths);
  312. }
  313. // Notifies shared code of a joystick connection or disconnection
  314. //
  315. void _glfwInputJoystick(_GLFWjoystick* js, int event)
  316. {
  317. const int jid = (int) (js - _glfw.joysticks);
  318. if (_glfw.callbacks.joystick)
  319. _glfw.callbacks.joystick(jid, event);
  320. }
  321. // Notifies shared code of the new value of a joystick axis
  322. //
  323. void _glfwInputJoystickAxis(_GLFWjoystick* js, int axis, float value)
  324. {
  325. js->axes[axis] = value;
  326. }
  327. // Notifies shared code of the new value of a joystick button
  328. //
  329. void _glfwInputJoystickButton(_GLFWjoystick* js, int button, char value)
  330. {
  331. js->buttons[button] = value;
  332. }
  333. // Notifies shared code of the new value of a joystick hat
  334. //
  335. void _glfwInputJoystickHat(_GLFWjoystick* js, int hat, char value)
  336. {
  337. const int base = js->buttonCount + hat * 4;
  338. js->buttons[base + 0] = (value & 0x01) ? GLFW_PRESS : GLFW_RELEASE;
  339. js->buttons[base + 1] = (value & 0x02) ? GLFW_PRESS : GLFW_RELEASE;
  340. js->buttons[base + 2] = (value & 0x04) ? GLFW_PRESS : GLFW_RELEASE;
  341. js->buttons[base + 3] = (value & 0x08) ? GLFW_PRESS : GLFW_RELEASE;
  342. js->hats[hat] = value;
  343. }
  344. //////////////////////////////////////////////////////////////////////////
  345. ////// GLFW internal API //////
  346. //////////////////////////////////////////////////////////////////////////
  347. // Returns an available joystick object with arrays and name allocated
  348. //
  349. _GLFWjoystick* _glfwAllocJoystick(const char* name,
  350. const char* guid,
  351. int axisCount,
  352. int buttonCount,
  353. int hatCount)
  354. {
  355. int jid;
  356. _GLFWjoystick* js;
  357. for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
  358. {
  359. if (!_glfw.joysticks[jid].present)
  360. break;
  361. }
  362. if (jid > GLFW_JOYSTICK_LAST)
  363. return NULL;
  364. js = _glfw.joysticks + jid;
  365. js->present = GLFW_TRUE;
  366. js->name = _glfw_strdup(name);
  367. js->axes = (float*)calloc(axisCount, sizeof(float));
  368. js->buttons = (unsigned char*)calloc(buttonCount + (size_t) hatCount * 4, 1);
  369. js->hats = (unsigned char*)calloc(hatCount, 1);
  370. js->axisCount = axisCount;
  371. js->buttonCount = buttonCount;
  372. js->hatCount = hatCount;
  373. strncpy(js->guid, guid, sizeof(js->guid) - 1);
  374. js->mapping = findValidMapping(js);
  375. return js;
  376. }
  377. // Frees arrays and name and flags the joystick object as unused
  378. //
  379. void _glfwFreeJoystick(_GLFWjoystick* js)
  380. {
  381. free(js->name);
  382. free(js->axes);
  383. free(js->buttons);
  384. free(js->hats);
  385. memset(js, 0, sizeof(_GLFWjoystick));
  386. }
  387. // Center the cursor in the content area of the specified window
  388. //
  389. void _glfwCenterCursorInContentArea(_GLFWwindow* window)
  390. {
  391. int width, height;
  392. _glfwPlatformGetWindowSize(window, &width, &height);
  393. _glfwPlatformSetCursorPos(window, width / 2.0, height / 2.0);
  394. }
  395. //////////////////////////////////////////////////////////////////////////
  396. ////// GLFW public API //////
  397. //////////////////////////////////////////////////////////////////////////
  398. GLFWAPI int glfwGetInputMode(GLFWwindow* handle, int mode)
  399. {
  400. _GLFWwindow* window = (_GLFWwindow*) handle;
  401. assert(window != NULL);
  402. _GLFW_REQUIRE_INIT_OR_RETURN(0);
  403. switch (mode)
  404. {
  405. case GLFW_CURSOR:
  406. return window->cursorMode;
  407. case GLFW_STICKY_KEYS:
  408. return window->stickyKeys;
  409. case GLFW_STICKY_MOUSE_BUTTONS:
  410. return window->stickyMouseButtons;
  411. case GLFW_LOCK_KEY_MODS:
  412. return window->lockKeyMods;
  413. case GLFW_RAW_MOUSE_MOTION:
  414. return window->rawMouseMotion;
  415. }
  416. _glfwInputError(GLFW_INVALID_ENUM, "Invalid input mode 0x%08X", mode);
  417. return 0;
  418. }
  419. GLFWAPI void glfwSetInputMode(GLFWwindow* handle, int mode, int value)
  420. {
  421. _GLFWwindow* window = (_GLFWwindow*) handle;
  422. assert(window != NULL);
  423. _GLFW_REQUIRE_INIT();
  424. if (mode == GLFW_CURSOR)
  425. {
  426. if (value != GLFW_CURSOR_NORMAL &&
  427. value != GLFW_CURSOR_HIDDEN &&
  428. value != GLFW_CURSOR_DISABLED)
  429. {
  430. _glfwInputError(GLFW_INVALID_ENUM,
  431. "Invalid cursor mode 0x%08X",
  432. value);
  433. return;
  434. }
  435. if (window->cursorMode == value)
  436. return;
  437. window->cursorMode = value;
  438. _glfwPlatformGetCursorPos(window,
  439. &window->virtualCursorPosX,
  440. &window->virtualCursorPosY);
  441. _glfwPlatformSetCursorMode(window, value);
  442. }
  443. else if (mode == GLFW_STICKY_KEYS)
  444. {
  445. value = value ? GLFW_TRUE : GLFW_FALSE;
  446. if (window->stickyKeys == value)
  447. return;
  448. if (!value)
  449. {
  450. int i;
  451. // Release all sticky keys
  452. for (i = 0; i <= GLFW_KEY_LAST; i++)
  453. {
  454. if (window->keys[i] == _GLFW_STICK)
  455. window->keys[i] = GLFW_RELEASE;
  456. }
  457. }
  458. window->stickyKeys = value;
  459. }
  460. else if (mode == GLFW_STICKY_MOUSE_BUTTONS)
  461. {
  462. value = value ? GLFW_TRUE : GLFW_FALSE;
  463. if (window->stickyMouseButtons == value)
  464. return;
  465. if (!value)
  466. {
  467. int i;
  468. // Release all sticky mouse buttons
  469. for (i = 0; i <= GLFW_MOUSE_BUTTON_LAST; i++)
  470. {
  471. if (window->mouseButtons[i] == _GLFW_STICK)
  472. window->mouseButtons[i] = GLFW_RELEASE;
  473. }
  474. }
  475. window->stickyMouseButtons = value;
  476. }
  477. else if (mode == GLFW_LOCK_KEY_MODS)
  478. {
  479. window->lockKeyMods = value ? GLFW_TRUE : GLFW_FALSE;
  480. }
  481. else if (mode == GLFW_RAW_MOUSE_MOTION)
  482. {
  483. if (!_glfwPlatformRawMouseMotionSupported())
  484. {
  485. _glfwInputError(GLFW_PLATFORM_ERROR,
  486. "Raw mouse motion is not supported on this system");
  487. return;
  488. }
  489. value = value ? GLFW_TRUE : GLFW_FALSE;
  490. if (window->rawMouseMotion == value)
  491. return;
  492. window->rawMouseMotion = value;
  493. _glfwPlatformSetRawMouseMotion(window, value);
  494. }
  495. else
  496. _glfwInputError(GLFW_INVALID_ENUM, "Invalid input mode 0x%08X", mode);
  497. }
  498. GLFWAPI int glfwRawMouseMotionSupported(void)
  499. {
  500. _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_FALSE);
  501. return _glfwPlatformRawMouseMotionSupported();
  502. }
  503. GLFWAPI const char* glfwGetKeyName(int key, int scancode)
  504. {
  505. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  506. if (key != GLFW_KEY_UNKNOWN)
  507. {
  508. if (key != GLFW_KEY_KP_EQUAL &&
  509. (key < GLFW_KEY_KP_0 || key > GLFW_KEY_KP_ADD) &&
  510. (key < GLFW_KEY_APOSTROPHE || key > GLFW_KEY_WORLD_2))
  511. {
  512. return NULL;
  513. }
  514. scancode = _glfwPlatformGetKeyScancode(key);
  515. }
  516. return _glfwPlatformGetScancodeName(scancode);
  517. }
  518. GLFWAPI int glfwGetKeyScancode(int key)
  519. {
  520. _GLFW_REQUIRE_INIT_OR_RETURN(-1);
  521. if (key < GLFW_KEY_SPACE || key > GLFW_KEY_LAST)
  522. {
  523. _glfwInputError(GLFW_INVALID_ENUM, "Invalid key %i", key);
  524. return GLFW_RELEASE;
  525. }
  526. return _glfwPlatformGetKeyScancode(key);
  527. }
  528. GLFWAPI int glfwGetKey(GLFWwindow* handle, int key)
  529. {
  530. _GLFWwindow* window = (_GLFWwindow*) handle;
  531. assert(window != NULL);
  532. _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_RELEASE);
  533. if (key < GLFW_KEY_SPACE || key > GLFW_KEY_LAST)
  534. {
  535. _glfwInputError(GLFW_INVALID_ENUM, "Invalid key %i", key);
  536. return GLFW_RELEASE;
  537. }
  538. if (window->keys[key] == _GLFW_STICK)
  539. {
  540. // Sticky mode: release key now
  541. window->keys[key] = GLFW_RELEASE;
  542. return GLFW_PRESS;
  543. }
  544. return (int) window->keys[key];
  545. }
  546. GLFWAPI int glfwGetMouseButton(GLFWwindow* handle, int button)
  547. {
  548. _GLFWwindow* window = (_GLFWwindow*) handle;
  549. assert(window != NULL);
  550. _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_RELEASE);
  551. if (button < GLFW_MOUSE_BUTTON_1 || button > GLFW_MOUSE_BUTTON_LAST)
  552. {
  553. _glfwInputError(GLFW_INVALID_ENUM, "Invalid mouse button %i", button);
  554. return GLFW_RELEASE;
  555. }
  556. if (window->mouseButtons[button] == _GLFW_STICK)
  557. {
  558. // Sticky mode: release mouse button now
  559. window->mouseButtons[button] = GLFW_RELEASE;
  560. return GLFW_PRESS;
  561. }
  562. return (int) window->mouseButtons[button];
  563. }
  564. GLFWAPI void glfwGetCursorPos(GLFWwindow* handle, double* xpos, double* ypos)
  565. {
  566. _GLFWwindow* window = (_GLFWwindow*) handle;
  567. assert(window != NULL);
  568. if (xpos)
  569. *xpos = 0;
  570. if (ypos)
  571. *ypos = 0;
  572. _GLFW_REQUIRE_INIT();
  573. if (window->cursorMode == GLFW_CURSOR_DISABLED)
  574. {
  575. if (xpos)
  576. *xpos = window->virtualCursorPosX;
  577. if (ypos)
  578. *ypos = window->virtualCursorPosY;
  579. }
  580. else
  581. _glfwPlatformGetCursorPos(window, xpos, ypos);
  582. }
  583. GLFWAPI void glfwSetCursorPos(GLFWwindow* handle, double xpos, double ypos)
  584. {
  585. _GLFWwindow* window = (_GLFWwindow*) handle;
  586. assert(window != NULL);
  587. _GLFW_REQUIRE_INIT();
  588. if (xpos != xpos || xpos < -DBL_MAX || xpos > DBL_MAX ||
  589. ypos != ypos || ypos < -DBL_MAX || ypos > DBL_MAX)
  590. {
  591. _glfwInputError(GLFW_INVALID_VALUE,
  592. "Invalid cursor position %f %f",
  593. xpos, ypos);
  594. return;
  595. }
  596. if (!_glfwPlatformWindowFocused(window))
  597. return;
  598. if (window->cursorMode == GLFW_CURSOR_DISABLED)
  599. {
  600. // Only update the accumulated position if the cursor is disabled
  601. window->virtualCursorPosX = xpos;
  602. window->virtualCursorPosY = ypos;
  603. }
  604. else
  605. {
  606. // Update system cursor position
  607. _glfwPlatformSetCursorPos(window, xpos, ypos);
  608. }
  609. }
  610. GLFWAPI GLFWcursor* glfwCreateCursor(const GLFWimage* image, int xhot, int yhot)
  611. {
  612. _GLFWcursor* cursor;
  613. assert(image != NULL);
  614. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  615. cursor = (_GLFWcursor*)calloc(1, sizeof(_GLFWcursor));
  616. cursor->next = _glfw.cursorListHead;
  617. _glfw.cursorListHead = cursor;
  618. if (!_glfwPlatformCreateCursor(cursor, image, xhot, yhot))
  619. {
  620. glfwDestroyCursor((GLFWcursor*) cursor);
  621. return NULL;
  622. }
  623. return (GLFWcursor*) cursor;
  624. }
  625. GLFWAPI GLFWcursor* glfwCreateStandardCursor(int shape)
  626. {
  627. _GLFWcursor* cursor;
  628. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  629. if (shape != GLFW_ARROW_CURSOR &&
  630. shape != GLFW_IBEAM_CURSOR &&
  631. shape != GLFW_CROSSHAIR_CURSOR &&
  632. shape != GLFW_POINTING_HAND_CURSOR &&
  633. shape != GLFW_RESIZE_EW_CURSOR &&
  634. shape != GLFW_RESIZE_NS_CURSOR &&
  635. shape != GLFW_RESIZE_NWSE_CURSOR &&
  636. shape != GLFW_RESIZE_NESW_CURSOR &&
  637. shape != GLFW_RESIZE_ALL_CURSOR &&
  638. shape != GLFW_NOT_ALLOWED_CURSOR)
  639. {
  640. _glfwInputError(GLFW_INVALID_ENUM, "Invalid standard cursor 0x%08X", shape);
  641. return NULL;
  642. }
  643. cursor = (_GLFWcursor*)calloc(1, sizeof(_GLFWcursor));
  644. cursor->next = _glfw.cursorListHead;
  645. _glfw.cursorListHead = cursor;
  646. if (!_glfwPlatformCreateStandardCursor(cursor, shape))
  647. {
  648. glfwDestroyCursor((GLFWcursor*) cursor);
  649. return NULL;
  650. }
  651. return (GLFWcursor*) cursor;
  652. }
  653. GLFWAPI void glfwDestroyCursor(GLFWcursor* handle)
  654. {
  655. _GLFWcursor* cursor = (_GLFWcursor*) handle;
  656. _GLFW_REQUIRE_INIT();
  657. if (cursor == NULL)
  658. return;
  659. // Make sure the cursor is not being used by any window
  660. {
  661. _GLFWwindow* window;
  662. for (window = _glfw.windowListHead; window; window = window->next)
  663. {
  664. if (window->cursor == cursor)
  665. glfwSetCursor((GLFWwindow*) window, NULL);
  666. }
  667. }
  668. _glfwPlatformDestroyCursor(cursor);
  669. // Unlink cursor from global linked list
  670. {
  671. _GLFWcursor** prev = &_glfw.cursorListHead;
  672. while (*prev != cursor)
  673. prev = &((*prev)->next);
  674. *prev = cursor->next;
  675. }
  676. free(cursor);
  677. }
  678. GLFWAPI void glfwSetCursor(GLFWwindow* windowHandle, GLFWcursor* cursorHandle)
  679. {
  680. _GLFWwindow* window = (_GLFWwindow*) windowHandle;
  681. _GLFWcursor* cursor = (_GLFWcursor*) cursorHandle;
  682. assert(window != NULL);
  683. _GLFW_REQUIRE_INIT();
  684. window->cursor = cursor;
  685. _glfwPlatformSetCursor(window, cursor);
  686. }
  687. GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow* handle, GLFWkeyfun cbfun)
  688. {
  689. _GLFWwindow* window = (_GLFWwindow*) handle;
  690. assert(window != NULL);
  691. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  692. _GLFW_SWAP_POINTERS(GLFWkeyfun, window->callbacks.key, cbfun);
  693. return cbfun;
  694. }
  695. GLFWAPI GLFWcharfun glfwSetCharCallback(GLFWwindow* handle, GLFWcharfun cbfun)
  696. {
  697. _GLFWwindow* window = (_GLFWwindow*) handle;
  698. assert(window != NULL);
  699. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  700. _GLFW_SWAP_POINTERS(GLFWcharfun, window->callbacks.character, cbfun);
  701. return cbfun;
  702. }
  703. GLFWAPI GLFWcharmodsfun glfwSetCharModsCallback(GLFWwindow* handle, GLFWcharmodsfun cbfun)
  704. {
  705. _GLFWwindow* window = (_GLFWwindow*) handle;
  706. assert(window != NULL);
  707. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  708. _GLFW_SWAP_POINTERS(GLFWcharmodsfun, window->callbacks.charmods, cbfun);
  709. return cbfun;
  710. }
  711. GLFWAPI GLFWmousebuttonfun glfwSetMouseButtonCallback(GLFWwindow* handle,
  712. GLFWmousebuttonfun cbfun)
  713. {
  714. _GLFWwindow* window = (_GLFWwindow*) handle;
  715. assert(window != NULL);
  716. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  717. _GLFW_SWAP_POINTERS(GLFWmousebuttonfun, window->callbacks.mouseButton, cbfun);
  718. return cbfun;
  719. }
  720. GLFWAPI GLFWcursorposfun glfwSetCursorPosCallback(GLFWwindow* handle,
  721. GLFWcursorposfun cbfun)
  722. {
  723. _GLFWwindow* window = (_GLFWwindow*) handle;
  724. assert(window != NULL);
  725. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  726. _GLFW_SWAP_POINTERS(GLFWcursorposfun, window->callbacks.cursorPos, cbfun);
  727. return cbfun;
  728. }
  729. GLFWAPI GLFWcursorenterfun glfwSetCursorEnterCallback(GLFWwindow* handle,
  730. GLFWcursorenterfun cbfun)
  731. {
  732. _GLFWwindow* window = (_GLFWwindow*) handle;
  733. assert(window != NULL);
  734. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  735. _GLFW_SWAP_POINTERS(GLFWcursorenterfun, window->callbacks.cursorEnter, cbfun);
  736. return cbfun;
  737. }
  738. GLFWAPI GLFWscrollfun glfwSetScrollCallback(GLFWwindow* handle,
  739. GLFWscrollfun cbfun)
  740. {
  741. _GLFWwindow* window = (_GLFWwindow*) handle;
  742. assert(window != NULL);
  743. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  744. _GLFW_SWAP_POINTERS(GLFWscrollfun, window->callbacks.scroll, cbfun);
  745. return cbfun;
  746. }
  747. GLFWAPI GLFWdropfun glfwSetDropCallback(GLFWwindow* handle, GLFWdropfun cbfun)
  748. {
  749. _GLFWwindow* window = (_GLFWwindow*) handle;
  750. assert(window != NULL);
  751. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  752. _GLFW_SWAP_POINTERS(GLFWdropfun, window->callbacks.drop, cbfun);
  753. return cbfun;
  754. }
  755. GLFWAPI int glfwJoystickPresent(int jid)
  756. {
  757. _GLFWjoystick* js;
  758. assert(jid >= GLFW_JOYSTICK_1);
  759. assert(jid <= GLFW_JOYSTICK_LAST);
  760. _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_FALSE);
  761. if (jid < 0 || jid > GLFW_JOYSTICK_LAST)
  762. {
  763. _glfwInputError(GLFW_INVALID_ENUM, "Invalid joystick ID %i", jid);
  764. return GLFW_FALSE;
  765. }
  766. js = _glfw.joysticks + jid;
  767. if (!js->present)
  768. return GLFW_FALSE;
  769. return _glfwPlatformPollJoystick(js, _GLFW_POLL_PRESENCE);
  770. }
  771. GLFWAPI const float* glfwGetJoystickAxes(int jid, int* count)
  772. {
  773. _GLFWjoystick* js;
  774. assert(jid >= GLFW_JOYSTICK_1);
  775. assert(jid <= GLFW_JOYSTICK_LAST);
  776. assert(count != NULL);
  777. *count = 0;
  778. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  779. if (jid < 0 || jid > GLFW_JOYSTICK_LAST)
  780. {
  781. _glfwInputError(GLFW_INVALID_ENUM, "Invalid joystick ID %i", jid);
  782. return NULL;
  783. }
  784. js = _glfw.joysticks + jid;
  785. if (!js->present)
  786. return NULL;
  787. if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_AXES))
  788. return NULL;
  789. *count = js->axisCount;
  790. return js->axes;
  791. }
  792. GLFWAPI const unsigned char* glfwGetJoystickButtons(int jid, int* count)
  793. {
  794. _GLFWjoystick* js;
  795. assert(jid >= GLFW_JOYSTICK_1);
  796. assert(jid <= GLFW_JOYSTICK_LAST);
  797. assert(count != NULL);
  798. *count = 0;
  799. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  800. if (jid < 0 || jid > GLFW_JOYSTICK_LAST)
  801. {
  802. _glfwInputError(GLFW_INVALID_ENUM, "Invalid joystick ID %i", jid);
  803. return NULL;
  804. }
  805. js = _glfw.joysticks + jid;
  806. if (!js->present)
  807. return NULL;
  808. if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_BUTTONS))
  809. return NULL;
  810. if (_glfw.hints.init.hatButtons)
  811. *count = js->buttonCount + js->hatCount * 4;
  812. else
  813. *count = js->buttonCount;
  814. return js->buttons;
  815. }
  816. GLFWAPI const unsigned char* glfwGetJoystickHats(int jid, int* count)
  817. {
  818. _GLFWjoystick* js;
  819. assert(jid >= GLFW_JOYSTICK_1);
  820. assert(jid <= GLFW_JOYSTICK_LAST);
  821. assert(count != NULL);
  822. *count = 0;
  823. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  824. if (jid < 0 || jid > GLFW_JOYSTICK_LAST)
  825. {
  826. _glfwInputError(GLFW_INVALID_ENUM, "Invalid joystick ID %i", jid);
  827. return NULL;
  828. }
  829. js = _glfw.joysticks + jid;
  830. if (!js->present)
  831. return NULL;
  832. if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_BUTTONS))
  833. return NULL;
  834. *count = js->hatCount;
  835. return js->hats;
  836. }
  837. GLFWAPI const char* glfwGetJoystickName(int jid)
  838. {
  839. _GLFWjoystick* js;
  840. assert(jid >= GLFW_JOYSTICK_1);
  841. assert(jid <= GLFW_JOYSTICK_LAST);
  842. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  843. if (jid < 0 || jid > GLFW_JOYSTICK_LAST)
  844. {
  845. _glfwInputError(GLFW_INVALID_ENUM, "Invalid joystick ID %i", jid);
  846. return NULL;
  847. }
  848. js = _glfw.joysticks + jid;
  849. if (!js->present)
  850. return NULL;
  851. if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_PRESENCE))
  852. return NULL;
  853. return js->name;
  854. }
  855. GLFWAPI const char* glfwGetJoystickGUID(int jid)
  856. {
  857. _GLFWjoystick* js;
  858. assert(jid >= GLFW_JOYSTICK_1);
  859. assert(jid <= GLFW_JOYSTICK_LAST);
  860. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  861. if (jid < 0 || jid > GLFW_JOYSTICK_LAST)
  862. {
  863. _glfwInputError(GLFW_INVALID_ENUM, "Invalid joystick ID %i", jid);
  864. return NULL;
  865. }
  866. js = _glfw.joysticks + jid;
  867. if (!js->present)
  868. return NULL;
  869. if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_PRESENCE))
  870. return NULL;
  871. return js->guid;
  872. }
  873. GLFWAPI void glfwSetJoystickUserPointer(int jid, void* pointer)
  874. {
  875. _GLFWjoystick* js;
  876. assert(jid >= GLFW_JOYSTICK_1);
  877. assert(jid <= GLFW_JOYSTICK_LAST);
  878. _GLFW_REQUIRE_INIT();
  879. js = _glfw.joysticks + jid;
  880. if (!js->present)
  881. return;
  882. js->userPointer = pointer;
  883. }
  884. GLFWAPI void* glfwGetJoystickUserPointer(int jid)
  885. {
  886. _GLFWjoystick* js;
  887. assert(jid >= GLFW_JOYSTICK_1);
  888. assert(jid <= GLFW_JOYSTICK_LAST);
  889. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  890. js = _glfw.joysticks + jid;
  891. if (!js->present)
  892. return NULL;
  893. return js->userPointer;
  894. }
  895. GLFWAPI GLFWjoystickfun glfwSetJoystickCallback(GLFWjoystickfun cbfun)
  896. {
  897. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  898. _GLFW_SWAP_POINTERS(GLFWjoystickfun, _glfw.callbacks.joystick, cbfun);
  899. return cbfun;
  900. }
  901. GLFWAPI int glfwUpdateGamepadMappings(const char* string)
  902. {
  903. int jid;
  904. const char* c = string;
  905. assert(string != NULL);
  906. _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_FALSE);
  907. while (*c)
  908. {
  909. if ((*c >= '0' && *c <= '9') ||
  910. (*c >= 'a' && *c <= 'f') ||
  911. (*c >= 'A' && *c <= 'F'))
  912. {
  913. char line[1024];
  914. const size_t length = strcspn(c, "\r\n");
  915. if (length < sizeof(line))
  916. {
  917. _GLFWmapping mapping = {{0}};
  918. memcpy(line, c, length);
  919. line[length] = '\0';
  920. if (parseMapping(&mapping, line))
  921. {
  922. _GLFWmapping* previous = findMapping(mapping.guid);
  923. if (previous)
  924. *previous = mapping;
  925. else
  926. {
  927. _glfw.mappingCount++;
  928. _glfw.mappings =
  929. (_GLFWmapping*)realloc(_glfw.mappings,
  930. sizeof(_GLFWmapping) * _glfw.mappingCount);
  931. _glfw.mappings[_glfw.mappingCount - 1] = mapping;
  932. }
  933. }
  934. }
  935. c += length;
  936. }
  937. else
  938. {
  939. c += strcspn(c, "\r\n");
  940. c += strspn(c, "\r\n");
  941. }
  942. }
  943. for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
  944. {
  945. _GLFWjoystick* js = _glfw.joysticks + jid;
  946. if (js->present)
  947. js->mapping = findValidMapping(js);
  948. }
  949. return GLFW_TRUE;
  950. }
  951. GLFWAPI int glfwJoystickIsGamepad(int jid)
  952. {
  953. _GLFWjoystick* js;
  954. assert(jid >= GLFW_JOYSTICK_1);
  955. assert(jid <= GLFW_JOYSTICK_LAST);
  956. _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_FALSE);
  957. if (jid < 0 || jid > GLFW_JOYSTICK_LAST)
  958. {
  959. _glfwInputError(GLFW_INVALID_ENUM, "Invalid joystick ID %i", jid);
  960. return GLFW_FALSE;
  961. }
  962. js = _glfw.joysticks + jid;
  963. if (!js->present)
  964. return GLFW_FALSE;
  965. if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_PRESENCE))
  966. return GLFW_FALSE;
  967. return js->mapping != NULL;
  968. }
  969. GLFWAPI const char* glfwGetGamepadName(int jid)
  970. {
  971. _GLFWjoystick* js;
  972. assert(jid >= GLFW_JOYSTICK_1);
  973. assert(jid <= GLFW_JOYSTICK_LAST);
  974. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  975. if (jid < 0 || jid > GLFW_JOYSTICK_LAST)
  976. {
  977. _glfwInputError(GLFW_INVALID_ENUM, "Invalid joystick ID %i", jid);
  978. return NULL;
  979. }
  980. js = _glfw.joysticks + jid;
  981. if (!js->present)
  982. return NULL;
  983. if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_PRESENCE))
  984. return NULL;
  985. if (!js->mapping)
  986. return NULL;
  987. return js->mapping->name;
  988. }
  989. GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state)
  990. {
  991. int i;
  992. _GLFWjoystick* js;
  993. assert(jid >= GLFW_JOYSTICK_1);
  994. assert(jid <= GLFW_JOYSTICK_LAST);
  995. assert(state != NULL);
  996. memset(state, 0, sizeof(GLFWgamepadstate));
  997. _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_FALSE);
  998. if (jid < 0 || jid > GLFW_JOYSTICK_LAST)
  999. {
  1000. _glfwInputError(GLFW_INVALID_ENUM, "Invalid joystick ID %i", jid);
  1001. return GLFW_FALSE;
  1002. }
  1003. js = _glfw.joysticks + jid;
  1004. if (!js->present)
  1005. return GLFW_FALSE;
  1006. if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_ALL))
  1007. return GLFW_FALSE;
  1008. if (!js->mapping)
  1009. return GLFW_FALSE;
  1010. for (i = 0; i <= GLFW_GAMEPAD_BUTTON_LAST; i++)
  1011. {
  1012. const _GLFWmapelement* e = js->mapping->buttons + i;
  1013. if (e->type == _GLFW_JOYSTICK_AXIS)
  1014. {
  1015. const float value = js->axes[e->index] * e->axisScale + e->axisOffset;
  1016. // HACK: This should be baked into the value transform
  1017. // TODO: Bake into transform when implementing output modifiers
  1018. if (e->axisOffset < 0 || (e->axisOffset == 0 && e->axisScale > 0))
  1019. {
  1020. if (value >= 0.f)
  1021. state->buttons[i] = GLFW_PRESS;
  1022. }
  1023. else
  1024. {
  1025. if (value <= 0.f)
  1026. state->buttons[i] = GLFW_PRESS;
  1027. }
  1028. }
  1029. else if (e->type == _GLFW_JOYSTICK_HATBIT)
  1030. {
  1031. const unsigned int hat = e->index >> 4;
  1032. const unsigned int bit = e->index & 0xf;
  1033. if (js->hats[hat] & bit)
  1034. state->buttons[i] = GLFW_PRESS;
  1035. }
  1036. else if (e->type == _GLFW_JOYSTICK_BUTTON)
  1037. state->buttons[i] = js->buttons[e->index];
  1038. }
  1039. for (i = 0; i <= GLFW_GAMEPAD_AXIS_LAST; i++)
  1040. {
  1041. const _GLFWmapelement* e = js->mapping->axes + i;
  1042. if (e->type == _GLFW_JOYSTICK_AXIS)
  1043. {
  1044. const float value = js->axes[e->index] * e->axisScale + e->axisOffset;
  1045. state->axes[i] = _glfw_fminf(_glfw_fmaxf(value, -1.f), 1.f);
  1046. }
  1047. else if (e->type == _GLFW_JOYSTICK_HATBIT)
  1048. {
  1049. const unsigned int hat = e->index >> 4;
  1050. const unsigned int bit = e->index & 0xf;
  1051. if (js->hats[hat] & bit)
  1052. state->axes[i] = 1.f;
  1053. else
  1054. state->axes[i] = -1.f;
  1055. }
  1056. else if (e->type == _GLFW_JOYSTICK_BUTTON)
  1057. state->axes[i] = js->buttons[e->index] * 2.f - 1.f;
  1058. }
  1059. return GLFW_TRUE;
  1060. }
  1061. GLFWAPI void glfwSetClipboardString(GLFWwindow* handle, const char* string)
  1062. {
  1063. assert(string != NULL);
  1064. _GLFW_REQUIRE_INIT();
  1065. _glfwPlatformSetClipboardString(string);
  1066. }
  1067. GLFWAPI const char* glfwGetClipboardString(GLFWwindow* handle)
  1068. {
  1069. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  1070. return _glfwPlatformGetClipboardString();
  1071. }
  1072. GLFWAPI double glfwGetTime(void)
  1073. {
  1074. _GLFW_REQUIRE_INIT_OR_RETURN(0.0);
  1075. return (double) (_glfwPlatformGetTimerValue() - _glfw.timer.offset) /
  1076. _glfwPlatformGetTimerFrequency();
  1077. }
  1078. GLFWAPI void glfwSetTime(double time)
  1079. {
  1080. _GLFW_REQUIRE_INIT();
  1081. if (time != time || time < 0.0 || time > 18446744073.0)
  1082. {
  1083. _glfwInputError(GLFW_INVALID_VALUE, "Invalid time %f", time);
  1084. return;
  1085. }
  1086. _glfw.timer.offset = _glfwPlatformGetTimerValue() -
  1087. (uint64_t) (time * _glfwPlatformGetTimerFrequency());
  1088. }
  1089. GLFWAPI uint64_t glfwGetTimerValue(void)
  1090. {
  1091. _GLFW_REQUIRE_INIT_OR_RETURN(0);
  1092. return _glfwPlatformGetTimerValue();
  1093. }
  1094. GLFWAPI uint64_t glfwGetTimerFrequency(void)
  1095. {
  1096. _GLFW_REQUIRE_INIT_OR_RETURN(0);
  1097. return _glfwPlatformGetTimerFrequency();
  1098. }