null_window.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. //========================================================================
  2. // GLFW 3.4 - www.glfw.org
  3. //------------------------------------------------------------------------
  4. // Copyright (c) 2016 Google Inc.
  5. // Copyright (c) 2016-2019 Camilla Löwy <elmindreda@glfw.org>
  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. // It is fine to use C99 in this file because it will not be built with VS
  28. //========================================================================
  29. #include "internal.h"
  30. #include "../kitty/monotonic.h"
  31. #include <stdlib.h>
  32. static void applySizeLimits(_GLFWwindow* window, int* width, int* height)
  33. {
  34. if (window->numer != GLFW_DONT_CARE && window->denom != GLFW_DONT_CARE)
  35. {
  36. const float ratio = (float) window->numer / (float) window->denom;
  37. *height = (int) (*width / ratio);
  38. }
  39. if (window->minwidth != GLFW_DONT_CARE && *width < window->minwidth)
  40. *width = window->minwidth;
  41. else if (window->maxwidth != GLFW_DONT_CARE && *width > window->maxwidth)
  42. *width = window->maxwidth;
  43. if (window->minheight != GLFW_DONT_CARE && *height < window->minheight)
  44. *height = window->minheight;
  45. else if (window->maxheight != GLFW_DONT_CARE && *height > window->maxheight)
  46. *height = window->maxheight;
  47. }
  48. static void fitToMonitor(_GLFWwindow* window)
  49. {
  50. GLFWvidmode mode;
  51. _glfwPlatformGetVideoMode(window->monitor, &mode);
  52. _glfwPlatformGetMonitorPos(window->monitor,
  53. &window->null.xpos,
  54. &window->null.ypos);
  55. window->null.width = mode.width;
  56. window->null.height = mode.height;
  57. }
  58. static void acquireMonitor(_GLFWwindow* window)
  59. {
  60. _glfwInputMonitorWindow(window->monitor, window);
  61. }
  62. static void releaseMonitor(_GLFWwindow* window)
  63. {
  64. if (window->monitor->window != window)
  65. return;
  66. _glfwInputMonitorWindow(window->monitor, NULL);
  67. }
  68. static int createNativeWindow(_GLFWwindow* window,
  69. const _GLFWwndconfig* wndconfig,
  70. const _GLFWfbconfig* fbconfig)
  71. {
  72. if (window->monitor)
  73. fitToMonitor(window);
  74. else
  75. {
  76. window->null.xpos = 17;
  77. window->null.ypos = 17;
  78. window->null.width = wndconfig->width;
  79. window->null.height = wndconfig->height;
  80. }
  81. window->null.visible = wndconfig->visible;
  82. window->null.decorated = wndconfig->decorated;
  83. window->null.maximized = wndconfig->maximized;
  84. window->null.floating = wndconfig->floating;
  85. window->null.transparent = fbconfig->transparent;
  86. window->null.opacity = 1.f;
  87. return true;
  88. }
  89. //////////////////////////////////////////////////////////////////////////
  90. ////// GLFW platform API //////
  91. //////////////////////////////////////////////////////////////////////////
  92. int _glfwPlatformCreateWindow(_GLFWwindow* window,
  93. const _GLFWwndconfig* wndconfig,
  94. const _GLFWctxconfig* ctxconfig,
  95. const _GLFWfbconfig* fbconfig)
  96. {
  97. if (!createNativeWindow(window, wndconfig, fbconfig))
  98. return false;
  99. if (ctxconfig->client != GLFW_NO_API)
  100. {
  101. if (ctxconfig->source == GLFW_NATIVE_CONTEXT_API ||
  102. ctxconfig->source == GLFW_OSMESA_CONTEXT_API)
  103. {
  104. if (!_glfwInitOSMesa())
  105. return false;
  106. if (!_glfwCreateContextOSMesa(window, ctxconfig, fbconfig))
  107. return false;
  108. }
  109. else
  110. {
  111. _glfwInputError(GLFW_API_UNAVAILABLE, "Null: EGL not available");
  112. return false;
  113. }
  114. }
  115. if (window->monitor)
  116. {
  117. _glfwPlatformShowWindow(window);
  118. _glfwPlatformFocusWindow(window);
  119. acquireMonitor(window);
  120. }
  121. return true;
  122. }
  123. void _glfwPlatformDestroyWindow(_GLFWwindow* window)
  124. {
  125. if (window->monitor)
  126. releaseMonitor(window);
  127. if (_glfw.null.focusedWindow == window)
  128. _glfw.null.focusedWindow = NULL;
  129. if (window->context.destroy)
  130. window->context.destroy(window);
  131. }
  132. void _glfwPlatformSetWindowTitle(_GLFWwindow* window UNUSED, const char* title UNUSED)
  133. {
  134. }
  135. void _glfwPlatformSetWindowIcon(_GLFWwindow* window UNUSED, int count UNUSED,
  136. const GLFWimage* images UNUSED)
  137. {
  138. }
  139. void _glfwPlatformSetWindowMonitor(_GLFWwindow* window,
  140. _GLFWmonitor* monitor,
  141. int xpos, int ypos,
  142. int width, int height,
  143. int refreshRate UNUSED)
  144. {
  145. if (window->monitor == monitor)
  146. {
  147. if (!monitor)
  148. {
  149. _glfwPlatformSetWindowPos(window, xpos, ypos);
  150. _glfwPlatformSetWindowSize(window, width, height);
  151. }
  152. return;
  153. }
  154. if (window->monitor)
  155. releaseMonitor(window);
  156. _glfwInputWindowMonitor(window, monitor);
  157. if (window->monitor)
  158. {
  159. window->null.visible = true;
  160. acquireMonitor(window);
  161. fitToMonitor(window);
  162. }
  163. else
  164. {
  165. _glfwPlatformSetWindowPos(window, xpos, ypos);
  166. _glfwPlatformSetWindowSize(window, width, height);
  167. }
  168. }
  169. void _glfwPlatformGetWindowPos(_GLFWwindow* window, int* xpos, int* ypos)
  170. {
  171. if (xpos)
  172. *xpos = window->null.xpos;
  173. if (ypos)
  174. *ypos = window->null.ypos;
  175. }
  176. void _glfwPlatformSetWindowPos(_GLFWwindow* window, int xpos, int ypos)
  177. {
  178. if (window->monitor)
  179. return;
  180. if (window->null.xpos != xpos || window->null.ypos != ypos)
  181. {
  182. window->null.xpos = xpos;
  183. window->null.ypos = ypos;
  184. _glfwInputWindowPos(window, xpos, ypos);
  185. }
  186. }
  187. void _glfwPlatformGetWindowSize(_GLFWwindow* window, int* width, int* height)
  188. {
  189. if (width)
  190. *width = window->null.width;
  191. if (height)
  192. *height = window->null.height;
  193. }
  194. void _glfwPlatformSetWindowSize(_GLFWwindow* window, int width, int height)
  195. {
  196. if (window->monitor)
  197. return;
  198. if (window->null.width != width || window->null.height != height)
  199. {
  200. window->null.width = width;
  201. window->null.height = height;
  202. _glfwInputWindowSize(window, width, height);
  203. _glfwInputFramebufferSize(window, width, height);
  204. }
  205. }
  206. void _glfwPlatformSetWindowSizeLimits(_GLFWwindow* window,
  207. int minwidth UNUSED, int minheight UNUSED,
  208. int maxwidth UNUSED, int maxheight UNUSED)
  209. {
  210. int width = window->null.width;
  211. int height = window->null.height;
  212. applySizeLimits(window, &width, &height);
  213. _glfwPlatformSetWindowSize(window, width, height);
  214. }
  215. void _glfwPlatformSetWindowAspectRatio(_GLFWwindow* window, int n UNUSED, int d UNUSED)
  216. {
  217. int width = window->null.width;
  218. int height = window->null.height;
  219. applySizeLimits(window, &width, &height);
  220. _glfwPlatformSetWindowSize(window, width, height);
  221. }
  222. void _glfwPlatformSetWindowSizeIncrements(_GLFWwindow* window UNUSED, int widthincr UNUSED, int heightincr UNUSED)
  223. {
  224. }
  225. void _glfwPlatformGetFramebufferSize(_GLFWwindow* window, int* width, int* height)
  226. {
  227. if (width)
  228. *width = window->null.width;
  229. if (height)
  230. *height = window->null.height;
  231. }
  232. void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window,
  233. int* left, int* top,
  234. int* right, int* bottom)
  235. {
  236. if (window->null.decorated && !window->monitor)
  237. {
  238. if (left)
  239. *left = 1;
  240. if (top)
  241. *top = 10;
  242. if (right)
  243. *right = 1;
  244. if (bottom)
  245. *bottom = 1;
  246. }
  247. else
  248. {
  249. if (left)
  250. *left = 0;
  251. if (top)
  252. *top = 0;
  253. if (right)
  254. *right = 0;
  255. if (bottom)
  256. *bottom = 0;
  257. }
  258. }
  259. void _glfwPlatformGetWindowContentScale(_GLFWwindow* window UNUSED,
  260. float* xscale, float* yscale)
  261. {
  262. if (xscale)
  263. *xscale = 1.f;
  264. if (yscale)
  265. *yscale = 1.f;
  266. }
  267. monotonic_t _glfwPlatformGetDoubleClickInterval(_GLFWwindow* window UNUSED)
  268. {
  269. return ms_to_monotonic_t(500ll);
  270. }
  271. void _glfwPlatformIconifyWindow(_GLFWwindow* window)
  272. {
  273. if (_glfw.null.focusedWindow == window)
  274. {
  275. _glfw.null.focusedWindow = NULL;
  276. _glfwInputWindowFocus(window, false);
  277. }
  278. if (!window->null.iconified)
  279. {
  280. window->null.iconified = true;
  281. _glfwInputWindowIconify(window, true);
  282. if (window->monitor)
  283. releaseMonitor(window);
  284. }
  285. }
  286. void _glfwPlatformRestoreWindow(_GLFWwindow* window)
  287. {
  288. if (window->null.iconified)
  289. {
  290. window->null.iconified = false;
  291. _glfwInputWindowIconify(window, false);
  292. if (window->monitor)
  293. acquireMonitor(window);
  294. }
  295. else if (window->null.maximized)
  296. {
  297. window->null.maximized = false;
  298. _glfwInputWindowMaximize(window, false);
  299. }
  300. }
  301. void _glfwPlatformMaximizeWindow(_GLFWwindow* window)
  302. {
  303. if (!window->null.maximized)
  304. {
  305. window->null.maximized = true;
  306. _glfwInputWindowMaximize(window, true);
  307. }
  308. }
  309. int _glfwPlatformWindowMaximized(_GLFWwindow* window)
  310. {
  311. return window->null.maximized;
  312. }
  313. int _glfwPlatformWindowHovered(_GLFWwindow* window)
  314. {
  315. return _glfw.null.xcursor >= window->null.xpos &&
  316. _glfw.null.ycursor >= window->null.ypos &&
  317. _glfw.null.xcursor <= window->null.xpos + window->null.width - 1 &&
  318. _glfw.null.ycursor <= window->null.ypos + window->null.height - 1;
  319. }
  320. int _glfwPlatformFramebufferTransparent(_GLFWwindow* window)
  321. {
  322. return window->null.transparent;
  323. }
  324. void _glfwPlatformSetWindowResizable(_GLFWwindow* window, bool enabled)
  325. {
  326. window->null.resizable = enabled;
  327. }
  328. void _glfwPlatformSetWindowDecorated(_GLFWwindow* window, bool enabled)
  329. {
  330. window->null.decorated = enabled;
  331. }
  332. void _glfwPlatformSetWindowFloating(_GLFWwindow* window, bool enabled)
  333. {
  334. window->null.floating = enabled;
  335. }
  336. void _glfwPlatformSetWindowMousePassthrough(_GLFWwindow* window UNUSED, bool enabled UNUSED)
  337. {
  338. }
  339. float _glfwPlatformGetWindowOpacity(_GLFWwindow* window)
  340. {
  341. return window->null.opacity;
  342. }
  343. void _glfwPlatformSetWindowOpacity(_GLFWwindow* window, float opacity)
  344. {
  345. window->null.opacity = opacity;
  346. }
  347. void _glfwPlatformSetRawMouseMotion(_GLFWwindow *window UNUSED, bool enabled UNUSED)
  348. {
  349. }
  350. bool _glfwPlatformRawMouseMotionSupported(void)
  351. {
  352. return true;
  353. }
  354. void _glfwPlatformShowWindow(_GLFWwindow* window)
  355. {
  356. window->null.visible = true;
  357. }
  358. void _glfwPlatformRequestWindowAttention(_GLFWwindow* window UNUSED)
  359. {
  360. }
  361. int _glfwPlatformWindowBell(_GLFWwindow* window UNUSED)
  362. {
  363. return false;
  364. }
  365. void _glfwPlatformHideWindow(_GLFWwindow* window)
  366. {
  367. if (_glfw.null.focusedWindow == window)
  368. {
  369. _glfw.null.focusedWindow = NULL;
  370. _glfwInputWindowFocus(window, false);
  371. }
  372. window->null.visible = false;
  373. }
  374. void _glfwPlatformFocusWindow(_GLFWwindow* window)
  375. {
  376. if (_glfw.null.focusedWindow == window)
  377. return;
  378. if (!window->null.visible)
  379. return;
  380. _GLFWwindow* previous = _glfw.null.focusedWindow;
  381. _glfw.null.focusedWindow = window;
  382. if (previous)
  383. {
  384. _glfwInputWindowFocus(previous, false);
  385. if (previous->monitor && previous->autoIconify)
  386. _glfwPlatformIconifyWindow(previous);
  387. }
  388. _glfwInputWindowFocus(window, true);
  389. }
  390. int _glfwPlatformWindowFocused(_GLFWwindow* window)
  391. {
  392. return _glfw.null.focusedWindow == window;
  393. }
  394. int _glfwPlatformWindowOccluded(_GLFWwindow* window UNUSED)
  395. {
  396. return false;
  397. }
  398. int _glfwPlatformWindowIconified(_GLFWwindow* window)
  399. {
  400. return window->null.iconified;
  401. }
  402. int _glfwPlatformWindowVisible(_GLFWwindow* window)
  403. {
  404. return window->null.visible;
  405. }
  406. void _glfwPlatformPollEvents(void)
  407. {
  408. }
  409. void _glfwPlatformWaitEvents(void)
  410. {
  411. }
  412. void _glfwPlatformWaitEventsTimeout(monotonic_t timeout UNUSED)
  413. {
  414. }
  415. void _glfwPlatformPostEmptyEvent(void)
  416. {
  417. }
  418. void _glfwPlatformGetCursorPos(_GLFWwindow* window, double* xpos, double* ypos)
  419. {
  420. if (xpos)
  421. *xpos = _glfw.null.xcursor - window->null.xpos;
  422. if (ypos)
  423. *ypos = _glfw.null.ycursor - window->null.ypos;
  424. }
  425. void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y)
  426. {
  427. _glfw.null.xcursor = window->null.xpos + (int) x;
  428. _glfw.null.ycursor = window->null.ypos + (int) y;
  429. }
  430. void _glfwPlatformSetCursorMode(_GLFWwindow* window UNUSED, int mode UNUSED)
  431. {
  432. }
  433. int _glfwPlatformCreateCursor(_GLFWcursor* cursor UNUSED,
  434. const GLFWimage* image UNUSED,
  435. int xhot UNUSED, int yhot UNUSED, int count UNUSED)
  436. {
  437. return true;
  438. }
  439. int _glfwPlatformCreateStandardCursor(_GLFWcursor* cursor UNUSED, int shape UNUSED)
  440. {
  441. return true;
  442. }
  443. void _glfwPlatformDestroyCursor(_GLFWcursor* cursor UNUSED)
  444. {
  445. }
  446. void _glfwPlatformSetCursor(_GLFWwindow* window UNUSED, _GLFWcursor* cursor UNUSED)
  447. {
  448. }
  449. void _glfwPlatformSetClipboardString(const char* string)
  450. {
  451. char* copy = _glfw_strdup(string);
  452. free(_glfw.null.clipboardString);
  453. _glfw.null.clipboardString = copy;
  454. }
  455. const char* _glfwPlatformGetClipboardString(void)
  456. {
  457. return _glfw.null.clipboardString;
  458. }
  459. const char* _glfwPlatformGetNativeKeyName(int native_key)
  460. {
  461. switch (scancode)
  462. {
  463. case GLFW_KEY_APOSTROPHE:
  464. return "'";
  465. case GLFW_KEY_COMMA:
  466. return ",";
  467. case GLFW_KEY_MINUS:
  468. case GLFW_KEY_KP_SUBTRACT:
  469. return "-";
  470. case GLFW_KEY_PERIOD:
  471. case GLFW_KEY_KP_DECIMAL:
  472. return ".";
  473. case GLFW_KEY_SLASH:
  474. case GLFW_KEY_KP_DIVIDE:
  475. return "/";
  476. case GLFW_KEY_SEMICOLON:
  477. return ";";
  478. case GLFW_KEY_EQUAL:
  479. case GLFW_KEY_KP_EQUAL:
  480. return "=";
  481. case GLFW_KEY_LEFT_BRACKET:
  482. return "[";
  483. case GLFW_KEY_RIGHT_BRACKET:
  484. return "]";
  485. case GLFW_KEY_KP_MULTIPLY:
  486. return "*";
  487. case GLFW_KEY_KP_ADD:
  488. return "+";
  489. case GLFW_KEY_BACKSLASH:
  490. case GLFW_KEY_WORLD_1:
  491. case GLFW_KEY_WORLD_2:
  492. return "\\";
  493. case GLFW_KEY_0:
  494. case GLFW_KEY_KP_0:
  495. return "0";
  496. case GLFW_KEY_1:
  497. case GLFW_KEY_KP_1:
  498. return "1";
  499. case GLFW_KEY_2:
  500. case GLFW_KEY_KP_2:
  501. return "2";
  502. case GLFW_KEY_3:
  503. case GLFW_KEY_KP_3:
  504. return "3";
  505. case GLFW_KEY_4:
  506. case GLFW_KEY_KP_4:
  507. return "4";
  508. case GLFW_KEY_5:
  509. case GLFW_KEY_KP_5:
  510. return "5";
  511. case GLFW_KEY_6:
  512. case GLFW_KEY_KP_6:
  513. return "6";
  514. case GLFW_KEY_7:
  515. case GLFW_KEY_KP_7:
  516. return "7";
  517. case GLFW_KEY_8:
  518. case GLFW_KEY_KP_8:
  519. return "8";
  520. case GLFW_KEY_9:
  521. case GLFW_KEY_KP_9:
  522. return "9";
  523. case GLFW_KEY_A:
  524. return "a";
  525. case GLFW_KEY_B:
  526. return "b";
  527. case GLFW_KEY_C:
  528. return "c";
  529. case GLFW_KEY_D:
  530. return "d";
  531. case GLFW_KEY_E:
  532. return "e";
  533. case GLFW_KEY_F:
  534. return "f";
  535. case GLFW_KEY_G:
  536. return "g";
  537. case GLFW_KEY_H:
  538. return "h";
  539. case GLFW_KEY_I:
  540. return "i";
  541. case GLFW_KEY_J:
  542. return "j";
  543. case GLFW_KEY_K:
  544. return "k";
  545. case GLFW_KEY_L:
  546. return "l";
  547. case GLFW_KEY_M:
  548. return "m";
  549. case GLFW_KEY_N:
  550. return "n";
  551. case GLFW_KEY_O:
  552. return "o";
  553. case GLFW_KEY_P:
  554. return "p";
  555. case GLFW_KEY_Q:
  556. return "q";
  557. case GLFW_KEY_R:
  558. return "r";
  559. case GLFW_KEY_S:
  560. return "s";
  561. case GLFW_KEY_T:
  562. return "t";
  563. case GLFW_KEY_U:
  564. return "u";
  565. case GLFW_KEY_V:
  566. return "v";
  567. case GLFW_KEY_W:
  568. return "w";
  569. case GLFW_KEY_X:
  570. return "x";
  571. case GLFW_KEY_Y:
  572. return "y";
  573. case GLFW_KEY_Z:
  574. return "z";
  575. }
  576. return NULL;
  577. }
  578. int _glfwPlatformGetNativeKeyForKey(int key)
  579. {
  580. return key;
  581. }
  582. void _glfwPlatformGetRequiredInstanceExtensions(char** extensions UNUSED)
  583. {
  584. }
  585. int _glfwPlatformGetPhysicalDevicePresentationSupport(VkInstance instance UNUSED,
  586. VkPhysicalDevice device UNUSED,
  587. uint32_t queuefamily UNUSED)
  588. {
  589. return false;
  590. }
  591. VkResult _glfwPlatformCreateWindowSurface(VkInstance instance UNUSED,
  592. _GLFWwindow* window UNUSED,
  593. const VkAllocationCallbacks* allocator UNUSED,
  594. VkSurfaceKHR* surface UNUSED)
  595. {
  596. // This seems like the most appropriate error to return here
  597. return VK_ERROR_EXTENSION_NOT_PRESENT;
  598. }