input.c 41 KB

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