window.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  1. //========================================================================
  2. // GLFW 3.4 - www.glfw.org
  3. //------------------------------------------------------------------------
  4. // Copyright (c) 2002-2006 Marcus Geelnard
  5. // Copyright (c) 2006-2019 Camilla Löwy <elmindreda@glfw.org>
  6. // Copyright (c) 2012 Torsten Walluhn <tw@mad-cad.net>
  7. //
  8. // This software is provided 'as-is', without any express or implied
  9. // warranty. In no event will the authors be held liable for any damages
  10. // arising from the use of this software.
  11. //
  12. // Permission is granted to anyone to use this software for any purpose,
  13. // including commercial applications, and to alter it and redistribute it
  14. // freely, subject to the following restrictions:
  15. //
  16. // 1. The origin of this software must not be misrepresented; you must not
  17. // claim that you wrote the original software. If you use this software
  18. // in a product, an acknowledgment in the product documentation would
  19. // be appreciated but is not required.
  20. //
  21. // 2. Altered source versions must be plainly marked as such, and must not
  22. // be misrepresented as being the original software.
  23. //
  24. // 3. This notice may not be removed or altered from any source
  25. // distribution.
  26. //
  27. //========================================================================
  28. // Please use C89 style variable declarations in this file because VS 2010
  29. //========================================================================
  30. #include "internal.h"
  31. #include "../kitty/monotonic.h"
  32. #include <assert.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35. //////////////////////////////////////////////////////////////////////////
  36. ////// GLFW event API //////
  37. //////////////////////////////////////////////////////////////////////////
  38. // Notifies shared code that a window has lost or received input focus
  39. //
  40. void _glfwInputWindowFocus(_GLFWwindow* window, bool focused)
  41. {
  42. if (window->callbacks.focus)
  43. window->callbacks.focus((GLFWwindow*) window, focused);
  44. if (!focused)
  45. {
  46. _glfw.focusedWindowId = 0;
  47. for (unsigned i = 0; i < arraysz(window->activated_keys); i++)
  48. {
  49. if (window->activated_keys[i].key > 0 && window->activated_keys[i].action == GLFW_PRESS)
  50. {
  51. const int native_key = _glfwPlatformGetNativeKeyForKey(window->activated_keys[i].key);
  52. GLFWkeyevent ev = {.key = window->activated_keys[i].key, .native_key = native_key, .action = GLFW_RELEASE, .fake_event_on_focus_change = true};
  53. _glfwInputKeyboard(window, &ev);
  54. }
  55. }
  56. for (int button = 0; button <= GLFW_MOUSE_BUTTON_LAST; button++)
  57. {
  58. if (window->mouseButtons[button] == GLFW_PRESS)
  59. _glfwInputMouseClick(window, button, GLFW_RELEASE, 0);
  60. }
  61. } else
  62. _glfw.focusedWindowId = window->id;
  63. }
  64. _GLFWwindow* _glfwFocusedWindow(void) {
  65. if (_glfw.focusedWindowId) {
  66. _GLFWwindow *w = _glfw.windowListHead;
  67. while (w) {
  68. if (w->id == _glfw.focusedWindowId) return w;
  69. w = w->next;
  70. }
  71. }
  72. return NULL;
  73. }
  74. _GLFWwindow* _glfwWindowForId(GLFWid id) {
  75. _GLFWwindow *w = _glfw.windowListHead;
  76. while (w) {
  77. if (w->id == id) return w;
  78. w = w->next;
  79. }
  80. return NULL;
  81. }
  82. // Notifies shared code that a window's occlusion state has changed
  83. //
  84. void _glfwInputWindowOcclusion(_GLFWwindow* window, bool occluded)
  85. {
  86. if (window->callbacks.occlusion)
  87. window->callbacks.occlusion((GLFWwindow*) window, occluded);
  88. }
  89. // Notifies shared code that a window has moved
  90. // The position is specified in content area relative screen coordinates
  91. //
  92. void _glfwInputWindowPos(_GLFWwindow* window, int x, int y)
  93. {
  94. if (window->callbacks.pos)
  95. window->callbacks.pos((GLFWwindow*) window, x, y);
  96. }
  97. // Notifies shared code that a window has been resized
  98. // The size is specified in screen coordinates
  99. //
  100. void _glfwInputWindowSize(_GLFWwindow* window, int width, int height)
  101. {
  102. if (window->callbacks.size)
  103. window->callbacks.size((GLFWwindow*) window, width, height);
  104. }
  105. // Notifies shared code that a window has been iconified or restored
  106. //
  107. void _glfwInputWindowIconify(_GLFWwindow* window, bool iconified)
  108. {
  109. if (window->callbacks.iconify)
  110. window->callbacks.iconify((GLFWwindow*) window, iconified);
  111. }
  112. // Notifies shared code that a window has been maximized or restored
  113. //
  114. void _glfwInputWindowMaximize(_GLFWwindow* window, bool maximized)
  115. {
  116. if (window->callbacks.maximize)
  117. window->callbacks.maximize((GLFWwindow*) window, maximized);
  118. }
  119. // Notifies shared code that a window framebuffer has been resized
  120. // The size is specified in pixels
  121. //
  122. void _glfwInputFramebufferSize(_GLFWwindow* window, int width, int height)
  123. {
  124. if (window->callbacks.fbsize)
  125. window->callbacks.fbsize((GLFWwindow*) window, width, height);
  126. }
  127. // Notifies shared code that a window live resize is in progress
  128. //
  129. void _glfwInputLiveResize(_GLFWwindow* window, bool started)
  130. {
  131. if (window && window->callbacks.liveResize)
  132. window->callbacks.liveResize((GLFWwindow*) window, started);
  133. }
  134. // Notifies shared code that a window content scale has changed
  135. // The scale is specified as the ratio between the current and default DPI
  136. //
  137. void _glfwInputWindowContentScale(_GLFWwindow* window, float xscale, float yscale)
  138. {
  139. if (window->callbacks.scale)
  140. window->callbacks.scale((GLFWwindow*) window, xscale, yscale);
  141. }
  142. // Notifies shared code that the window contents needs updating
  143. //
  144. void _glfwInputWindowDamage(_GLFWwindow* window)
  145. {
  146. if (window->callbacks.refresh)
  147. window->callbacks.refresh((GLFWwindow*) window);
  148. }
  149. // Notifies shared code that the user wishes to close a window
  150. //
  151. void _glfwInputWindowCloseRequest(_GLFWwindow* window)
  152. {
  153. window->shouldClose = true;
  154. if (window->callbacks.close)
  155. window->callbacks.close((GLFWwindow*) window);
  156. }
  157. // Notifies shared code that a window has changed its desired monitor
  158. //
  159. void _glfwInputWindowMonitor(_GLFWwindow* window, _GLFWmonitor* monitor)
  160. {
  161. window->monitor = monitor;
  162. }
  163. //////////////////////////////////////////////////////////////////////////
  164. ////// GLFW public API //////
  165. //////////////////////////////////////////////////////////////////////////
  166. GLFWAPI GLFWwindow* glfwCreateWindow(int width, int height,
  167. const char* title,
  168. GLFWmonitor* monitor,
  169. GLFWwindow* share)
  170. {
  171. _GLFWfbconfig fbconfig;
  172. _GLFWctxconfig ctxconfig;
  173. _GLFWwndconfig wndconfig;
  174. _GLFWwindow* window;
  175. assert(title != NULL);
  176. assert(width >= 0);
  177. assert(height >= 0);
  178. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  179. if (width <= 0 || height <= 0)
  180. {
  181. _glfwInputError(GLFW_INVALID_VALUE,
  182. "Invalid window size %ix%i",
  183. width, height);
  184. return NULL;
  185. }
  186. fbconfig = _glfw.hints.framebuffer;
  187. ctxconfig = _glfw.hints.context;
  188. wndconfig = _glfw.hints.window;
  189. wndconfig.width = width;
  190. wndconfig.height = height;
  191. wndconfig.title = title;
  192. ctxconfig.share = (_GLFWwindow*) share;
  193. if (!_glfwIsValidContextConfig(&ctxconfig))
  194. return NULL;
  195. static GLFWid windowIdCounter = 0;
  196. window = calloc(1, sizeof(_GLFWwindow));
  197. window->next = _glfw.windowListHead;
  198. window->id = ++windowIdCounter;
  199. _glfw.windowListHead = window;
  200. window->videoMode.width = width;
  201. window->videoMode.height = height;
  202. window->videoMode.redBits = fbconfig.redBits;
  203. window->videoMode.greenBits = fbconfig.greenBits;
  204. window->videoMode.blueBits = fbconfig.blueBits;
  205. window->videoMode.refreshRate = _glfw.hints.refreshRate;
  206. window->monitor = (_GLFWmonitor*) monitor;
  207. window->resizable = wndconfig.resizable;
  208. window->decorated = wndconfig.decorated;
  209. window->autoIconify = wndconfig.autoIconify;
  210. window->floating = wndconfig.floating;
  211. window->focusOnShow = wndconfig.focusOnShow;
  212. window->mousePassthrough = wndconfig.mousePassthrough;
  213. window->cursorMode = GLFW_CURSOR_NORMAL;
  214. window->minwidth = GLFW_DONT_CARE;
  215. window->minheight = GLFW_DONT_CARE;
  216. window->maxwidth = GLFW_DONT_CARE;
  217. window->maxheight = GLFW_DONT_CARE;
  218. window->numer = GLFW_DONT_CARE;
  219. window->denom = GLFW_DONT_CARE;
  220. window->widthincr = GLFW_DONT_CARE;
  221. window->heightincr = GLFW_DONT_CARE;
  222. // Open the actual window and create its context
  223. if (!_glfwPlatformCreateWindow(window, &wndconfig, &ctxconfig, &fbconfig))
  224. {
  225. glfwDestroyWindow((GLFWwindow*) window);
  226. return NULL;
  227. }
  228. if (ctxconfig.client != GLFW_NO_API)
  229. {
  230. if (!_glfwRefreshContextAttribs(window, &ctxconfig))
  231. {
  232. glfwDestroyWindow((GLFWwindow*) window);
  233. return NULL;
  234. }
  235. }
  236. if (wndconfig.mousePassthrough)
  237. _glfwPlatformSetWindowMousePassthrough(window, true);
  238. if (window->monitor)
  239. {
  240. if (wndconfig.centerCursor)
  241. _glfwCenterCursorInContentArea(window);
  242. }
  243. else
  244. {
  245. if (wndconfig.visible)
  246. {
  247. _glfwPlatformShowWindow(window);
  248. #ifndef _GLFW_WAYLAND
  249. if (wndconfig.focused)
  250. _glfwPlatformFocusWindow(window);
  251. #endif
  252. }
  253. }
  254. return (GLFWwindow*) window;
  255. }
  256. void glfwDefaultWindowHints(void)
  257. {
  258. _GLFW_REQUIRE_INIT();
  259. // The default is OpenGL with minimum version 1.0
  260. memset(&_glfw.hints.context, 0, sizeof(_glfw.hints.context));
  261. _glfw.hints.context.client = GLFW_OPENGL_API;
  262. _glfw.hints.context.source = GLFW_NATIVE_CONTEXT_API;
  263. _glfw.hints.context.major = 1;
  264. _glfw.hints.context.minor = 0;
  265. // The default is a focused, visible, resizable window with decorations
  266. memset(&_glfw.hints.window, 0, sizeof(_glfw.hints.window));
  267. _glfw.hints.window.resizable = true;
  268. _glfw.hints.window.visible = true;
  269. _glfw.hints.window.decorated = true;
  270. _glfw.hints.window.focused = true;
  271. _glfw.hints.window.autoIconify = true;
  272. _glfw.hints.window.centerCursor = true;
  273. _glfw.hints.window.focusOnShow = true;
  274. _glfw.hints.window.blur_radius = 0;
  275. // The default is 24 bits of color, 24 bits of depth and 8 bits of stencil,
  276. // double buffered
  277. memset(&_glfw.hints.framebuffer, 0, sizeof(_glfw.hints.framebuffer));
  278. _glfw.hints.framebuffer.redBits = 8;
  279. _glfw.hints.framebuffer.greenBits = 8;
  280. _glfw.hints.framebuffer.blueBits = 8;
  281. _glfw.hints.framebuffer.alphaBits = 8;
  282. _glfw.hints.framebuffer.depthBits = 24;
  283. _glfw.hints.framebuffer.stencilBits = 8;
  284. _glfw.hints.framebuffer.doublebuffer = true;
  285. // The default is to select the highest available refresh rate
  286. _glfw.hints.refreshRate = GLFW_DONT_CARE;
  287. // The default is to use full Retina resolution framebuffers
  288. _glfw.hints.window.ns.retina = true;
  289. // use the default colorspace assigned by the system
  290. _glfw.hints.window.ns.color_space = 0;
  291. }
  292. GLFWAPI void glfwWindowHint(int hint, int value)
  293. {
  294. _GLFW_REQUIRE_INIT();
  295. switch (hint)
  296. {
  297. case GLFW_RED_BITS:
  298. _glfw.hints.framebuffer.redBits = value;
  299. return;
  300. case GLFW_GREEN_BITS:
  301. _glfw.hints.framebuffer.greenBits = value;
  302. return;
  303. case GLFW_BLUE_BITS:
  304. _glfw.hints.framebuffer.blueBits = value;
  305. return;
  306. case GLFW_ALPHA_BITS:
  307. _glfw.hints.framebuffer.alphaBits = value;
  308. return;
  309. case GLFW_DEPTH_BITS:
  310. _glfw.hints.framebuffer.depthBits = value;
  311. return;
  312. case GLFW_STENCIL_BITS:
  313. _glfw.hints.framebuffer.stencilBits = value;
  314. return;
  315. case GLFW_ACCUM_RED_BITS:
  316. _glfw.hints.framebuffer.accumRedBits = value;
  317. return;
  318. case GLFW_ACCUM_GREEN_BITS:
  319. _glfw.hints.framebuffer.accumGreenBits = value;
  320. return;
  321. case GLFW_ACCUM_BLUE_BITS:
  322. _glfw.hints.framebuffer.accumBlueBits = value;
  323. return;
  324. case GLFW_ACCUM_ALPHA_BITS:
  325. _glfw.hints.framebuffer.accumAlphaBits = value;
  326. return;
  327. case GLFW_AUX_BUFFERS:
  328. _glfw.hints.framebuffer.auxBuffers = value;
  329. return;
  330. case GLFW_STEREO:
  331. _glfw.hints.framebuffer.stereo = value ? true : false;
  332. return;
  333. case GLFW_DOUBLEBUFFER:
  334. _glfw.hints.framebuffer.doublebuffer = value ? true : false;
  335. return;
  336. case GLFW_TRANSPARENT_FRAMEBUFFER:
  337. _glfw.hints.framebuffer.transparent = value ? true : false;
  338. return;
  339. case GLFW_SAMPLES:
  340. _glfw.hints.framebuffer.samples = value;
  341. return;
  342. case GLFW_SRGB_CAPABLE:
  343. _glfw.hints.framebuffer.sRGB = value ? true : false;
  344. return;
  345. case GLFW_RESIZABLE:
  346. _glfw.hints.window.resizable = value ? true : false;
  347. return;
  348. case GLFW_DECORATED:
  349. _glfw.hints.window.decorated = value ? true : false;
  350. return;
  351. case GLFW_FOCUSED:
  352. _glfw.hints.window.focused = value ? true : false;
  353. return;
  354. case GLFW_AUTO_ICONIFY:
  355. _glfw.hints.window.autoIconify = value ? true : false;
  356. return;
  357. case GLFW_FLOATING:
  358. _glfw.hints.window.floating = value ? true : false;
  359. return;
  360. case GLFW_MAXIMIZED:
  361. _glfw.hints.window.maximized = value ? true : false;
  362. return;
  363. case GLFW_VISIBLE:
  364. _glfw.hints.window.visible = value ? true : false;
  365. return;
  366. case GLFW_COCOA_RETINA_FRAMEBUFFER:
  367. _glfw.hints.window.ns.retina = value ? true : false;
  368. return;
  369. case GLFW_COCOA_COLOR_SPACE:
  370. _glfw.hints.window.ns.color_space = value;
  371. return;
  372. case GLFW_BLUR_RADIUS:
  373. _glfw.hints.window.blur_radius = value;
  374. return;
  375. case GLFW_COCOA_GRAPHICS_SWITCHING:
  376. _glfw.hints.context.nsgl.offline = value ? true : false;
  377. return;
  378. case GLFW_SCALE_TO_MONITOR:
  379. _glfw.hints.window.scaleToMonitor = value ? true : false;
  380. return;
  381. case GLFW_CENTER_CURSOR:
  382. _glfw.hints.window.centerCursor = value ? true : false;
  383. return;
  384. case GLFW_FOCUS_ON_SHOW:
  385. _glfw.hints.window.focusOnShow = value ? true : false;
  386. return;
  387. case GLFW_MOUSE_PASSTHROUGH:
  388. _glfw.hints.window.mousePassthrough = value ? true : false;
  389. return;
  390. case GLFW_CLIENT_API:
  391. _glfw.hints.context.client = value;
  392. return;
  393. case GLFW_CONTEXT_CREATION_API:
  394. _glfw.hints.context.source = value;
  395. return;
  396. case GLFW_CONTEXT_VERSION_MAJOR:
  397. _glfw.hints.context.major = value;
  398. return;
  399. case GLFW_CONTEXT_VERSION_MINOR:
  400. _glfw.hints.context.minor = value;
  401. return;
  402. case GLFW_CONTEXT_ROBUSTNESS:
  403. _glfw.hints.context.robustness = value;
  404. return;
  405. case GLFW_OPENGL_FORWARD_COMPAT:
  406. _glfw.hints.context.forward = value ? true : false;
  407. return;
  408. case GLFW_CONTEXT_DEBUG:
  409. _glfw.hints.context.debug = value ? true : false;
  410. return;
  411. case GLFW_CONTEXT_NO_ERROR:
  412. _glfw.hints.context.noerror = value ? true : false;
  413. return;
  414. case GLFW_OPENGL_PROFILE:
  415. _glfw.hints.context.profile = value;
  416. return;
  417. case GLFW_CONTEXT_RELEASE_BEHAVIOR:
  418. _glfw.hints.context.release = value;
  419. return;
  420. case GLFW_REFRESH_RATE:
  421. _glfw.hints.refreshRate = value;
  422. return;
  423. case GLFW_WAYLAND_BGCOLOR:
  424. _glfw.hints.window.wl.bgcolor = value;
  425. return;
  426. }
  427. _glfwInputError(GLFW_INVALID_ENUM, "Invalid window hint 0x%08X", hint);
  428. }
  429. GLFWAPI void glfwWindowHintString(int hint, const char* value)
  430. {
  431. assert(value != NULL);
  432. _GLFW_REQUIRE_INIT();
  433. switch (hint)
  434. {
  435. case GLFW_COCOA_FRAME_NAME:
  436. strncpy(_glfw.hints.window.ns.frameName, value,
  437. sizeof(_glfw.hints.window.ns.frameName) - 1);
  438. return;
  439. case GLFW_X11_CLASS_NAME:
  440. strncpy(_glfw.hints.window.x11.className, value,
  441. sizeof(_glfw.hints.window.x11.className) - 1);
  442. return;
  443. case GLFW_X11_INSTANCE_NAME:
  444. strncpy(_glfw.hints.window.x11.instanceName, value,
  445. sizeof(_glfw.hints.window.x11.instanceName) - 1);
  446. return;
  447. case GLFW_WAYLAND_APP_ID:
  448. strncpy(_glfw.hints.window.wl.appId, value,
  449. sizeof(_glfw.hints.window.wl.appId) - 1);
  450. return;
  451. }
  452. _glfwInputError(GLFW_INVALID_ENUM, "Invalid window hint string 0x%08X", hint);
  453. }
  454. GLFWAPI void glfwDestroyWindow(GLFWwindow* handle)
  455. {
  456. _GLFWwindow* window = (_GLFWwindow*) handle;
  457. _GLFW_REQUIRE_INIT();
  458. // Allow closing of NULL (to match the behavior of free)
  459. if (window == NULL)
  460. return;
  461. // Clear all callbacks to avoid exposing a half torn-down window object
  462. memset(&window->callbacks, 0, sizeof(window->callbacks));
  463. // The window's context must not be current on another thread when the
  464. // window is destroyed
  465. if (window == _glfwPlatformGetTls(&_glfw.contextSlot))
  466. glfwMakeContextCurrent(NULL);
  467. _glfwPlatformDestroyWindow(window);
  468. // Unlink window from global linked list
  469. {
  470. _GLFWwindow** prev = &_glfw.windowListHead;
  471. while (*prev != window)
  472. prev = &((*prev)->next);
  473. *prev = window->next;
  474. }
  475. free(window);
  476. }
  477. GLFWAPI int glfwWindowShouldClose(GLFWwindow* handle)
  478. {
  479. _GLFWwindow* window = (_GLFWwindow*) handle;
  480. assert(window != NULL);
  481. _GLFW_REQUIRE_INIT_OR_RETURN(0);
  482. return window->shouldClose;
  483. }
  484. GLFWAPI void glfwSetWindowShouldClose(GLFWwindow* handle, int value)
  485. {
  486. _GLFWwindow* window = (_GLFWwindow*) handle;
  487. assert(window != NULL);
  488. _GLFW_REQUIRE_INIT();
  489. window->shouldClose = value;
  490. }
  491. GLFWAPI void glfwSetWindowTitle(GLFWwindow* handle, const char* title)
  492. {
  493. _GLFWwindow* window = (_GLFWwindow*) handle;
  494. assert(window != NULL);
  495. assert(title != NULL);
  496. _GLFW_REQUIRE_INIT();
  497. _glfwPlatformSetWindowTitle(window, title);
  498. }
  499. GLFWAPI void glfwSetWindowIcon(GLFWwindow* handle,
  500. int count, const GLFWimage* images)
  501. {
  502. _GLFWwindow* window = (_GLFWwindow*) handle;
  503. assert(window != NULL);
  504. assert(count >= 0);
  505. assert(count == 0 || images != NULL);
  506. _GLFW_REQUIRE_INIT();
  507. _glfwPlatformSetWindowIcon(window, count, images);
  508. }
  509. GLFWAPI void glfwGetWindowPos(GLFWwindow* handle, int* xpos, int* ypos)
  510. {
  511. _GLFWwindow* window = (_GLFWwindow*) handle;
  512. assert(window != NULL);
  513. if (xpos)
  514. *xpos = 0;
  515. if (ypos)
  516. *ypos = 0;
  517. _GLFW_REQUIRE_INIT();
  518. _glfwPlatformGetWindowPos(window, xpos, ypos);
  519. }
  520. GLFWAPI void glfwSetWindowPos(GLFWwindow* handle, int xpos, int ypos)
  521. {
  522. _GLFWwindow* window = (_GLFWwindow*) handle;
  523. assert(window != NULL);
  524. _GLFW_REQUIRE_INIT();
  525. if (window->monitor)
  526. return;
  527. _glfwPlatformSetWindowPos(window, xpos, ypos);
  528. }
  529. GLFWAPI void glfwGetWindowSize(GLFWwindow* handle, int* width, int* height)
  530. {
  531. _GLFWwindow* window = (_GLFWwindow*) handle;
  532. assert(window != NULL);
  533. if (width)
  534. *width = 0;
  535. if (height)
  536. *height = 0;
  537. _GLFW_REQUIRE_INIT();
  538. _glfwPlatformGetWindowSize(window, width, height);
  539. }
  540. GLFWAPI void glfwSetWindowSize(GLFWwindow* handle, int width, int height)
  541. {
  542. _GLFWwindow* window = (_GLFWwindow*) handle;
  543. assert(window != NULL);
  544. assert(width >= 0);
  545. assert(height >= 0);
  546. _GLFW_REQUIRE_INIT();
  547. window->videoMode.width = width;
  548. window->videoMode.height = height;
  549. _glfwPlatformSetWindowSize(window, width, height);
  550. }
  551. GLFWAPI void glfwSetWindowSizeLimits(GLFWwindow* handle,
  552. int minwidth, int minheight,
  553. int maxwidth, int maxheight)
  554. {
  555. _GLFWwindow* window = (_GLFWwindow*) handle;
  556. assert(window != NULL);
  557. _GLFW_REQUIRE_INIT();
  558. if (minwidth != GLFW_DONT_CARE && minheight != GLFW_DONT_CARE)
  559. {
  560. if (minwidth < 0 || minheight < 0)
  561. {
  562. _glfwInputError(GLFW_INVALID_VALUE,
  563. "Invalid window minimum size %ix%i",
  564. minwidth, minheight);
  565. return;
  566. }
  567. }
  568. if (maxwidth != GLFW_DONT_CARE && maxheight != GLFW_DONT_CARE)
  569. {
  570. if (maxwidth < 0 || maxheight < 0 ||
  571. maxwidth < minwidth || maxheight < minheight)
  572. {
  573. _glfwInputError(GLFW_INVALID_VALUE,
  574. "Invalid window maximum size %ix%i",
  575. maxwidth, maxheight);
  576. return;
  577. }
  578. }
  579. window->minwidth = minwidth;
  580. window->minheight = minheight;
  581. window->maxwidth = maxwidth;
  582. window->maxheight = maxheight;
  583. if (window->monitor || !window->resizable)
  584. return;
  585. _glfwPlatformSetWindowSizeLimits(window,
  586. minwidth, minheight,
  587. maxwidth, maxheight);
  588. }
  589. GLFWAPI void glfwSetWindowAspectRatio(GLFWwindow* handle, int numer, int denom)
  590. {
  591. _GLFWwindow* window = (_GLFWwindow*) handle;
  592. assert(window != NULL);
  593. assert(numer != 0);
  594. assert(denom != 0);
  595. _GLFW_REQUIRE_INIT();
  596. if (numer != GLFW_DONT_CARE && denom != GLFW_DONT_CARE)
  597. {
  598. if (numer <= 0 || denom <= 0)
  599. {
  600. _glfwInputError(GLFW_INVALID_VALUE,
  601. "Invalid window aspect ratio %i:%i",
  602. numer, denom);
  603. return;
  604. }
  605. }
  606. window->numer = numer;
  607. window->denom = denom;
  608. if (window->monitor || !window->resizable)
  609. return;
  610. _glfwPlatformSetWindowAspectRatio(window, numer, denom);
  611. }
  612. GLFWAPI void glfwSetWindowSizeIncrements(GLFWwindow* handle, int widthincr, int heightincr)
  613. {
  614. _GLFWwindow* window = (_GLFWwindow*) handle;
  615. assert(window != NULL);
  616. assert(widthincr >= 0 || widthincr == GLFW_DONT_CARE);
  617. assert(heightincr >= 0 || heightincr == GLFW_DONT_CARE);
  618. _GLFW_REQUIRE_INIT();
  619. window->widthincr = widthincr;
  620. window->heightincr = heightincr;
  621. _glfwPlatformSetWindowSizeIncrements(window, window->widthincr, window->heightincr);
  622. }
  623. GLFWAPI void glfwGetFramebufferSize(GLFWwindow* handle, int* width, int* height)
  624. {
  625. _GLFWwindow* window = (_GLFWwindow*) handle;
  626. assert(window != NULL);
  627. if (width)
  628. *width = 0;
  629. if (height)
  630. *height = 0;
  631. _GLFW_REQUIRE_INIT();
  632. _glfwPlatformGetFramebufferSize(window, width, height);
  633. }
  634. GLFWAPI void glfwGetWindowFrameSize(GLFWwindow* handle,
  635. int* left, int* top,
  636. int* right, int* bottom)
  637. {
  638. _GLFWwindow* window = (_GLFWwindow*) handle;
  639. assert(window != NULL);
  640. if (left)
  641. *left = 0;
  642. if (top)
  643. *top = 0;
  644. if (right)
  645. *right = 0;
  646. if (bottom)
  647. *bottom = 0;
  648. _GLFW_REQUIRE_INIT();
  649. _glfwPlatformGetWindowFrameSize(window, left, top, right, bottom);
  650. }
  651. GLFWAPI void glfwGetWindowContentScale(GLFWwindow* handle,
  652. float* xscale, float* yscale)
  653. {
  654. _GLFWwindow* window = (_GLFWwindow*) handle;
  655. assert(window != NULL);
  656. if (xscale)
  657. *xscale = 0.f;
  658. if (yscale)
  659. *yscale = 0.f;
  660. _GLFW_REQUIRE_INIT();
  661. _glfwPlatformGetWindowContentScale(window, xscale, yscale);
  662. }
  663. GLFWAPI monotonic_t glfwGetDoubleClickInterval(GLFWwindow* handle)
  664. {
  665. _GLFWwindow* window = (_GLFWwindow*) handle;
  666. assert(window != NULL);
  667. _GLFW_REQUIRE_INIT_OR_RETURN(ms_to_monotonic_t(500ll));
  668. return _glfwPlatformGetDoubleClickInterval(window);
  669. }
  670. GLFWAPI float glfwGetWindowOpacity(GLFWwindow* handle)
  671. {
  672. _GLFWwindow* window = (_GLFWwindow*) handle;
  673. assert(window != NULL);
  674. _GLFW_REQUIRE_INIT_OR_RETURN(1.f);
  675. return _glfwPlatformGetWindowOpacity(window);
  676. }
  677. GLFWAPI void glfwSetWindowOpacity(GLFWwindow* handle, float opacity)
  678. {
  679. _GLFWwindow* window = (_GLFWwindow*) handle;
  680. assert(window != NULL);
  681. assert(opacity == opacity);
  682. assert(opacity >= 0.f);
  683. assert(opacity <= 1.f);
  684. _GLFW_REQUIRE_INIT();
  685. if (opacity != opacity || opacity < 0.f || opacity > 1.f)
  686. {
  687. _glfwInputError(GLFW_INVALID_VALUE, "Invalid window opacity %f", opacity);
  688. return;
  689. }
  690. _glfwPlatformSetWindowOpacity(window, opacity);
  691. }
  692. GLFWAPI void glfwIconifyWindow(GLFWwindow* handle)
  693. {
  694. _GLFWwindow* window = (_GLFWwindow*) handle;
  695. assert(window != NULL);
  696. _GLFW_REQUIRE_INIT();
  697. _glfwPlatformIconifyWindow(window);
  698. }
  699. GLFWAPI void glfwRestoreWindow(GLFWwindow* handle)
  700. {
  701. _GLFWwindow* window = (_GLFWwindow*) handle;
  702. assert(window != NULL);
  703. _GLFW_REQUIRE_INIT();
  704. _glfwPlatformRestoreWindow(window);
  705. }
  706. GLFWAPI void glfwMaximizeWindow(GLFWwindow* handle)
  707. {
  708. _GLFWwindow* window = (_GLFWwindow*) handle;
  709. assert(window != NULL);
  710. _GLFW_REQUIRE_INIT();
  711. if (window->monitor)
  712. return;
  713. _glfwPlatformMaximizeWindow(window);
  714. }
  715. GLFWAPI void glfwShowWindow(GLFWwindow* handle)
  716. {
  717. _GLFWwindow* window = (_GLFWwindow*) handle;
  718. assert(window != NULL);
  719. _GLFW_REQUIRE_INIT();
  720. if (window->monitor)
  721. return;
  722. _glfwPlatformShowWindow(window);
  723. #ifndef _GLFW_WAYLAND
  724. if (window->focusOnShow)
  725. _glfwPlatformFocusWindow(window);
  726. #endif
  727. }
  728. GLFWAPI void glfwRequestWindowAttention(GLFWwindow* handle)
  729. {
  730. _GLFWwindow* window = (_GLFWwindow*) handle;
  731. assert(window != NULL);
  732. _GLFW_REQUIRE_INIT();
  733. _glfwPlatformRequestWindowAttention(window);
  734. }
  735. GLFWAPI int glfwWindowBell(GLFWwindow* handle)
  736. {
  737. _GLFWwindow* window = (_GLFWwindow*) handle;
  738. assert(window != NULL);
  739. _GLFW_REQUIRE_INIT_OR_RETURN(false);
  740. return _glfwPlatformWindowBell(window);
  741. }
  742. GLFWAPI void glfwHideWindow(GLFWwindow* handle)
  743. {
  744. _GLFWwindow* window = (_GLFWwindow*) handle;
  745. assert(window != NULL);
  746. _GLFW_REQUIRE_INIT();
  747. if (window->monitor)
  748. return;
  749. _glfwPlatformHideWindow(window);
  750. }
  751. GLFWAPI void glfwFocusWindow(GLFWwindow* handle)
  752. {
  753. _GLFWwindow* window = (_GLFWwindow*) handle;
  754. assert(window != NULL);
  755. _GLFW_REQUIRE_INIT();
  756. _glfwPlatformFocusWindow(window);
  757. }
  758. GLFWAPI int glfwGetWindowAttrib(GLFWwindow* handle, int attrib)
  759. {
  760. _GLFWwindow* window = (_GLFWwindow*) handle;
  761. assert(window != NULL);
  762. _GLFW_REQUIRE_INIT_OR_RETURN(0);
  763. switch (attrib)
  764. {
  765. case GLFW_FOCUSED:
  766. return _glfwPlatformWindowFocused(window);
  767. case GLFW_ICONIFIED:
  768. return _glfwPlatformWindowIconified(window);
  769. case GLFW_VISIBLE:
  770. return _glfwPlatformWindowVisible(window);
  771. case GLFW_MAXIMIZED:
  772. return _glfwPlatformWindowMaximized(window);
  773. case GLFW_HOVERED:
  774. return _glfwPlatformWindowHovered(window);
  775. case GLFW_FOCUS_ON_SHOW:
  776. return window->focusOnShow;
  777. case GLFW_MOUSE_PASSTHROUGH:
  778. return window->mousePassthrough;
  779. case GLFW_TRANSPARENT_FRAMEBUFFER:
  780. return _glfwPlatformFramebufferTransparent(window);
  781. case GLFW_OCCLUDED:
  782. return _glfwPlatformWindowOccluded(window);
  783. case GLFW_RESIZABLE:
  784. return window->resizable;
  785. case GLFW_DECORATED:
  786. return window->decorated;
  787. case GLFW_FLOATING:
  788. return window->floating;
  789. case GLFW_AUTO_ICONIFY:
  790. return window->autoIconify;
  791. case GLFW_CLIENT_API:
  792. return window->context.client;
  793. case GLFW_CONTEXT_CREATION_API:
  794. return window->context.source;
  795. case GLFW_CONTEXT_VERSION_MAJOR:
  796. return window->context.major;
  797. case GLFW_CONTEXT_VERSION_MINOR:
  798. return window->context.minor;
  799. case GLFW_CONTEXT_REVISION:
  800. return window->context.revision;
  801. case GLFW_CONTEXT_ROBUSTNESS:
  802. return window->context.robustness;
  803. case GLFW_OPENGL_FORWARD_COMPAT:
  804. return window->context.forward;
  805. case GLFW_CONTEXT_DEBUG:
  806. return window->context.debug;
  807. case GLFW_OPENGL_PROFILE:
  808. return window->context.profile;
  809. case GLFW_CONTEXT_RELEASE_BEHAVIOR:
  810. return window->context.release;
  811. case GLFW_CONTEXT_NO_ERROR:
  812. return window->context.noerror;
  813. }
  814. _glfwInputError(GLFW_INVALID_ENUM, "Invalid window attribute 0x%08X", attrib);
  815. return 0;
  816. }
  817. GLFWAPI void glfwSetWindowAttrib(GLFWwindow* handle, int attrib, int value)
  818. {
  819. _GLFWwindow* window = (_GLFWwindow*) handle;
  820. assert(window != NULL);
  821. _GLFW_REQUIRE_INIT();
  822. value = value ? true : false;
  823. if (attrib == GLFW_AUTO_ICONIFY)
  824. window->autoIconify = value;
  825. else if (attrib == GLFW_RESIZABLE)
  826. {
  827. if (window->resizable == value)
  828. return;
  829. window->resizable = value;
  830. if (!window->monitor)
  831. _glfwPlatformSetWindowResizable(window, value);
  832. }
  833. else if (attrib == GLFW_DECORATED)
  834. {
  835. if (window->decorated == value)
  836. return;
  837. window->decorated = value;
  838. if (!window->monitor)
  839. _glfwPlatformSetWindowDecorated(window, value);
  840. }
  841. else if (attrib == GLFW_FLOATING)
  842. {
  843. if (window->floating == value)
  844. return;
  845. window->floating = value;
  846. if (!window->monitor)
  847. _glfwPlatformSetWindowFloating(window, value);
  848. }
  849. else if (attrib == GLFW_FOCUS_ON_SHOW)
  850. window->focusOnShow = value;
  851. else if (attrib == GLFW_MOUSE_PASSTHROUGH)
  852. {
  853. if (window->mousePassthrough == value)
  854. return;
  855. window->mousePassthrough = value;
  856. _glfwPlatformSetWindowMousePassthrough(window, value);
  857. }
  858. else
  859. _glfwInputError(GLFW_INVALID_ENUM, "Invalid window attribute 0x%08X", attrib);
  860. }
  861. GLFWAPI int glfwSetWindowBlur(GLFWwindow* handle, int value)
  862. {
  863. _GLFWwindow* window = (_GLFWwindow*) handle;
  864. assert(window != NULL);
  865. _GLFW_REQUIRE_INIT_OR_RETURN(0);
  866. return _glfwPlatformSetWindowBlur(window, value);
  867. }
  868. GLFWAPI GLFWmonitor* glfwGetWindowMonitor(GLFWwindow* handle)
  869. {
  870. _GLFWwindow* window = (_GLFWwindow*) handle;
  871. assert(window != NULL);
  872. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  873. return (GLFWmonitor*) window->monitor;
  874. }
  875. GLFWAPI void glfwSetWindowMonitor(GLFWwindow* wh,
  876. GLFWmonitor* mh,
  877. int xpos, int ypos,
  878. int width, int height,
  879. int refreshRate)
  880. {
  881. _GLFWwindow* window = (_GLFWwindow*) wh;
  882. _GLFWmonitor* monitor = (_GLFWmonitor*) mh;
  883. assert(window != NULL);
  884. assert(width >= 0);
  885. assert(height >= 0);
  886. _GLFW_REQUIRE_INIT();
  887. if (width <= 0 || height <= 0)
  888. {
  889. _glfwInputError(GLFW_INVALID_VALUE,
  890. "Invalid window size %ix%i",
  891. width, height);
  892. return;
  893. }
  894. if (refreshRate < 0 && refreshRate != GLFW_DONT_CARE)
  895. {
  896. _glfwInputError(GLFW_INVALID_VALUE,
  897. "Invalid refresh rate %i",
  898. refreshRate);
  899. return;
  900. }
  901. window->videoMode.width = width;
  902. window->videoMode.height = height;
  903. window->videoMode.refreshRate = refreshRate;
  904. _glfwPlatformSetWindowMonitor(window, monitor,
  905. xpos, ypos, width, height,
  906. refreshRate);
  907. }
  908. GLFWAPI bool glfwToggleFullscreen(GLFWwindow* wh, unsigned int flags) {
  909. _GLFWwindow* window = (_GLFWwindow*) wh;
  910. if (window) return _glfwPlatformToggleFullscreen(window, flags);
  911. return false;
  912. }
  913. GLFWAPI bool glfwIsFullscreen(GLFWwindow* wh, unsigned int flags) {
  914. return _glfwPlatformIsFullscreen((_GLFWwindow*)wh, flags);
  915. }
  916. GLFWAPI bool glfwAreSwapsAllowed(const GLFWwindow* wh) {
  917. return !(((_GLFWwindow*)wh)->swaps_disallowed);
  918. }
  919. GLFWAPI void glfwSetWindowUserPointer(GLFWwindow* handle, void* pointer)
  920. {
  921. _GLFWwindow* window = (_GLFWwindow*) handle;
  922. assert(window != NULL);
  923. _GLFW_REQUIRE_INIT();
  924. window->userPointer = pointer;
  925. }
  926. GLFWAPI void* glfwGetWindowUserPointer(GLFWwindow* handle)
  927. {
  928. _GLFWwindow* window = (_GLFWwindow*) handle;
  929. assert(window != NULL);
  930. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  931. return window->userPointer;
  932. }
  933. GLFWAPI GLFWwindowposfun glfwSetWindowPosCallback(GLFWwindow* handle,
  934. GLFWwindowposfun cbfun)
  935. {
  936. _GLFWwindow* window = (_GLFWwindow*) handle;
  937. assert(window != NULL);
  938. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  939. _GLFW_SWAP_POINTERS(window->callbacks.pos, cbfun);
  940. return cbfun;
  941. }
  942. GLFWAPI GLFWwindowsizefun glfwSetWindowSizeCallback(GLFWwindow* handle,
  943. GLFWwindowsizefun cbfun)
  944. {
  945. _GLFWwindow* window = (_GLFWwindow*) handle;
  946. assert(window != NULL);
  947. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  948. _GLFW_SWAP_POINTERS(window->callbacks.size, cbfun);
  949. return cbfun;
  950. }
  951. GLFWAPI GLFWwindowclosefun glfwSetWindowCloseCallback(GLFWwindow* handle,
  952. GLFWwindowclosefun cbfun)
  953. {
  954. _GLFWwindow* window = (_GLFWwindow*) handle;
  955. assert(window != NULL);
  956. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  957. _GLFW_SWAP_POINTERS(window->callbacks.close, cbfun);
  958. return cbfun;
  959. }
  960. GLFWAPI GLFWwindowrefreshfun glfwSetWindowRefreshCallback(GLFWwindow* handle,
  961. GLFWwindowrefreshfun cbfun)
  962. {
  963. _GLFWwindow* window = (_GLFWwindow*) handle;
  964. assert(window != NULL);
  965. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  966. _GLFW_SWAP_POINTERS(window->callbacks.refresh, cbfun);
  967. return cbfun;
  968. }
  969. GLFWAPI GLFWwindowfocusfun glfwSetWindowFocusCallback(GLFWwindow* handle,
  970. GLFWwindowfocusfun cbfun)
  971. {
  972. _GLFWwindow* window = (_GLFWwindow*) handle;
  973. assert(window != NULL);
  974. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  975. _GLFW_SWAP_POINTERS(window->callbacks.focus, cbfun);
  976. return cbfun;
  977. }
  978. GLFWAPI GLFWwindowocclusionfun glfwSetWindowOcclusionCallback(GLFWwindow* handle,
  979. GLFWwindowocclusionfun cbfun)
  980. {
  981. _GLFWwindow* window = (_GLFWwindow*) handle;
  982. assert(window != NULL);
  983. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  984. _GLFW_SWAP_POINTERS(window->callbacks.occlusion, cbfun);
  985. return cbfun;
  986. }
  987. GLFWAPI GLFWwindowiconifyfun glfwSetWindowIconifyCallback(GLFWwindow* handle,
  988. GLFWwindowiconifyfun cbfun)
  989. {
  990. _GLFWwindow* window = (_GLFWwindow*) handle;
  991. assert(window != NULL);
  992. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  993. _GLFW_SWAP_POINTERS(window->callbacks.iconify, cbfun);
  994. return cbfun;
  995. }
  996. GLFWAPI GLFWwindowmaximizefun glfwSetWindowMaximizeCallback(GLFWwindow* handle,
  997. GLFWwindowmaximizefun cbfun)
  998. {
  999. _GLFWwindow* window = (_GLFWwindow*) handle;
  1000. assert(window != NULL);
  1001. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  1002. _GLFW_SWAP_POINTERS(window->callbacks.maximize, cbfun);
  1003. return cbfun;
  1004. }
  1005. GLFWAPI GLFWframebuffersizefun glfwSetFramebufferSizeCallback(GLFWwindow* handle,
  1006. GLFWframebuffersizefun cbfun)
  1007. {
  1008. _GLFWwindow* window = (_GLFWwindow*) handle;
  1009. assert(window != NULL);
  1010. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  1011. _GLFW_SWAP_POINTERS(window->callbacks.fbsize, cbfun);
  1012. return cbfun;
  1013. }
  1014. GLFWAPI GLFWliveresizefun glfwSetLiveResizeCallback(GLFWwindow* handle,
  1015. GLFWliveresizefun cbfun)
  1016. {
  1017. _GLFWwindow* window = (_GLFWwindow*) handle;
  1018. assert(window != NULL);
  1019. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  1020. _GLFW_SWAP_POINTERS(window->callbacks.liveResize, cbfun);
  1021. return cbfun;
  1022. }
  1023. GLFWAPI GLFWwindowcontentscalefun glfwSetWindowContentScaleCallback(GLFWwindow* handle,
  1024. GLFWwindowcontentscalefun cbfun)
  1025. {
  1026. _GLFWwindow* window = (_GLFWwindow*) handle;
  1027. assert(window != NULL);
  1028. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  1029. _GLFW_SWAP_POINTERS(window->callbacks.scale, cbfun);
  1030. return cbfun;
  1031. }
  1032. GLFWAPI void glfwPostEmptyEvent(void)
  1033. {
  1034. _GLFW_REQUIRE_INIT();
  1035. _glfwPlatformPostEmptyEvent();
  1036. }