wl_window.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754
  1. //========================================================================
  2. // GLFW 3.4 Wayland - www.glfw.org
  3. //------------------------------------------------------------------------
  4. // Copyright (c) 2014 Jonas Ådahl <[email protected]>
  5. //
  6. // This software is provided 'as-is', without any express or implied
  7. // warranty. In no event will the authors be held liable for any damages
  8. // arising from the use of this software.
  9. //
  10. // Permission is granted to anyone to use this software for any purpose,
  11. // including commercial applications, and to alter it and redistribute it
  12. // freely, subject to the following restrictions:
  13. //
  14. // 1. The origin of this software must not be misrepresented; you must not
  15. // claim that you wrote the original software. If you use this software
  16. // in a product, an acknowledgment in the product documentation would
  17. // be appreciated but is not required.
  18. //
  19. // 2. Altered source versions must be plainly marked as such, and must not
  20. // be misrepresented as being the original software.
  21. //
  22. // 3. This notice may not be removed or altered from any source
  23. // distribution.
  24. //
  25. //========================================================================
  26. // It is fine to use C99 in this file because it will not be built with VS
  27. //========================================================================
  28. #define _GNU_SOURCE
  29. #include "internal.h"
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <errno.h>
  33. #include <unistd.h>
  34. #include <string.h>
  35. #include <fcntl.h>
  36. #include <sys/mman.h>
  37. #include <sys/timerfd.h>
  38. #include <poll.h>
  39. static int createTmpfileCloexec(char* tmpname)
  40. {
  41. int fd;
  42. fd = mkostemp(tmpname, O_CLOEXEC);
  43. if (fd >= 0)
  44. unlink(tmpname);
  45. return fd;
  46. }
  47. /*
  48. * Create a new, unique, anonymous file of the given size, and
  49. * return the file descriptor for it. The file descriptor is set
  50. * CLOEXEC. The file is immediately suitable for mmap()'ing
  51. * the given size at offset zero.
  52. *
  53. * The file should not have a permanent backing store like a disk,
  54. * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
  55. *
  56. * The file name is deleted from the file system.
  57. *
  58. * The file is suitable for buffer sharing between processes by
  59. * transmitting the file descriptor over Unix sockets using the
  60. * SCM_RIGHTS methods.
  61. *
  62. * posix_fallocate() is used to guarantee that disk space is available
  63. * for the file at the given size. If disk space is insufficient, errno
  64. * is set to ENOSPC. If posix_fallocate() is not supported, program may
  65. * receive SIGBUS on accessing mmap()'ed file contents instead.
  66. */
  67. static int createAnonymousFile(off_t size)
  68. {
  69. static const char template[] = "/glfw-shared-XXXXXX";
  70. const char* path;
  71. char* name;
  72. int fd;
  73. int ret;
  74. #ifdef HAVE_MEMFD_CREATE
  75. fd = memfd_create("glfw-shared", MFD_CLOEXEC | MFD_ALLOW_SEALING);
  76. if (fd >= 0)
  77. {
  78. // We can add this seal before calling posix_fallocate(), as the file
  79. // is currently zero-sized anyway.
  80. //
  81. // There is also no need to check for the return value, we couldn’t do
  82. // anything with it anyway.
  83. fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_SEAL);
  84. }
  85. else
  86. #elif defined(SHM_ANON)
  87. fd = shm_open(SHM_ANON, O_RDWR | O_CLOEXEC, 0600);
  88. if (fd < 0)
  89. #endif
  90. {
  91. path = getenv("XDG_RUNTIME_DIR");
  92. if (!path)
  93. {
  94. errno = ENOENT;
  95. return -1;
  96. }
  97. name = calloc(strlen(path) + sizeof(template), 1);
  98. strcpy(name, path);
  99. strcat(name, template);
  100. fd = createTmpfileCloexec(name);
  101. free(name);
  102. if (fd < 0)
  103. return -1;
  104. }
  105. #if defined(SHM_ANON)
  106. // posix_fallocate does not work on SHM descriptors
  107. ret = ftruncate(fd, size);
  108. #else
  109. ret = posix_fallocate(fd, 0, size);
  110. #endif
  111. if (ret != 0)
  112. {
  113. close(fd);
  114. errno = ret;
  115. return -1;
  116. }
  117. return fd;
  118. }
  119. static struct wl_buffer* createShmBuffer(const GLFWimage* image)
  120. {
  121. struct wl_shm_pool* pool;
  122. struct wl_buffer* buffer;
  123. int stride = image->width * 4;
  124. int length = image->width * image->height * 4;
  125. void* data;
  126. int fd, i;
  127. fd = createAnonymousFile(length);
  128. if (fd < 0)
  129. {
  130. _glfwInputError(GLFW_PLATFORM_ERROR,
  131. "Wayland: Creating a buffer file for %d B failed: %m",
  132. length);
  133. return NULL;
  134. }
  135. data = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  136. if (data == MAP_FAILED)
  137. {
  138. _glfwInputError(GLFW_PLATFORM_ERROR,
  139. "Wayland: mmap failed: %m");
  140. close(fd);
  141. return NULL;
  142. }
  143. pool = wl_shm_create_pool(_glfw.wl.shm, fd, length);
  144. close(fd);
  145. unsigned char* source = (unsigned char*) image->pixels;
  146. unsigned char* target = data;
  147. for (i = 0; i < image->width * image->height; i++, source += 4)
  148. {
  149. unsigned int alpha = source[3];
  150. *target++ = (unsigned char) ((source[2] * alpha) / 255);
  151. *target++ = (unsigned char) ((source[1] * alpha) / 255);
  152. *target++ = (unsigned char) ((source[0] * alpha) / 255);
  153. *target++ = (unsigned char) alpha;
  154. }
  155. buffer =
  156. wl_shm_pool_create_buffer(pool, 0,
  157. image->width,
  158. image->height,
  159. stride, WL_SHM_FORMAT_ARGB8888);
  160. munmap(data, length);
  161. wl_shm_pool_destroy(pool);
  162. return buffer;
  163. }
  164. static void createDecoration(_GLFWdecorationWayland* decoration,
  165. struct wl_surface* parent,
  166. struct wl_buffer* buffer, GLFWbool opaque,
  167. int x, int y,
  168. int width, int height)
  169. {
  170. struct wl_region* region;
  171. decoration->surface = wl_compositor_create_surface(_glfw.wl.compositor);
  172. decoration->subsurface =
  173. wl_subcompositor_get_subsurface(_glfw.wl.subcompositor,
  174. decoration->surface, parent);
  175. wl_subsurface_set_position(decoration->subsurface, x, y);
  176. decoration->viewport = wp_viewporter_get_viewport(_glfw.wl.viewporter,
  177. decoration->surface);
  178. wp_viewport_set_destination(decoration->viewport, width, height);
  179. wl_surface_attach(decoration->surface, buffer, 0, 0);
  180. if (opaque)
  181. {
  182. region = wl_compositor_create_region(_glfw.wl.compositor);
  183. wl_region_add(region, 0, 0, width, height);
  184. wl_surface_set_opaque_region(decoration->surface, region);
  185. wl_surface_commit(decoration->surface);
  186. wl_region_destroy(region);
  187. }
  188. else
  189. wl_surface_commit(decoration->surface);
  190. }
  191. static void createDecorations(_GLFWwindow* window)
  192. {
  193. unsigned char data[] = { 224, 224, 224, 255 };
  194. const GLFWimage image = { 1, 1, data };
  195. GLFWbool opaque = (data[3] == 255);
  196. if (!_glfw.wl.viewporter || !window->decorated || window->wl.decorations.serverSide)
  197. return;
  198. if (!window->wl.decorations.buffer)
  199. window->wl.decorations.buffer = createShmBuffer(&image);
  200. if (!window->wl.decorations.buffer)
  201. return;
  202. createDecoration(&window->wl.decorations.top, window->wl.surface,
  203. window->wl.decorations.buffer, opaque,
  204. 0, -_GLFW_DECORATION_TOP,
  205. window->wl.width, _GLFW_DECORATION_TOP);
  206. createDecoration(&window->wl.decorations.left, window->wl.surface,
  207. window->wl.decorations.buffer, opaque,
  208. -_GLFW_DECORATION_WIDTH, -_GLFW_DECORATION_TOP,
  209. _GLFW_DECORATION_WIDTH, window->wl.height + _GLFW_DECORATION_TOP);
  210. createDecoration(&window->wl.decorations.right, window->wl.surface,
  211. window->wl.decorations.buffer, opaque,
  212. window->wl.width, -_GLFW_DECORATION_TOP,
  213. _GLFW_DECORATION_WIDTH, window->wl.height + _GLFW_DECORATION_TOP);
  214. createDecoration(&window->wl.decorations.bottom, window->wl.surface,
  215. window->wl.decorations.buffer, opaque,
  216. -_GLFW_DECORATION_WIDTH, window->wl.height,
  217. window->wl.width + _GLFW_DECORATION_HORIZONTAL, _GLFW_DECORATION_WIDTH);
  218. }
  219. static void destroyDecoration(_GLFWdecorationWayland* decoration)
  220. {
  221. if (decoration->surface)
  222. wl_surface_destroy(decoration->surface);
  223. if (decoration->subsurface)
  224. wl_subsurface_destroy(decoration->subsurface);
  225. if (decoration->viewport)
  226. wp_viewport_destroy(decoration->viewport);
  227. decoration->surface = NULL;
  228. decoration->subsurface = NULL;
  229. decoration->viewport = NULL;
  230. }
  231. static void destroyDecorations(_GLFWwindow* window)
  232. {
  233. destroyDecoration(&window->wl.decorations.top);
  234. destroyDecoration(&window->wl.decorations.left);
  235. destroyDecoration(&window->wl.decorations.right);
  236. destroyDecoration(&window->wl.decorations.bottom);
  237. }
  238. static void xdgDecorationHandleConfigure(void* data,
  239. struct zxdg_toplevel_decoration_v1* decoration,
  240. uint32_t mode)
  241. {
  242. _GLFWwindow* window = data;
  243. window->wl.decorations.serverSide = (mode == ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
  244. if (!window->wl.decorations.serverSide)
  245. createDecorations(window);
  246. }
  247. static const struct zxdg_toplevel_decoration_v1_listener xdgDecorationListener = {
  248. xdgDecorationHandleConfigure,
  249. };
  250. // Makes the surface considered as XRGB instead of ARGB.
  251. static void setOpaqueRegion(_GLFWwindow* window)
  252. {
  253. struct wl_region* region;
  254. region = wl_compositor_create_region(_glfw.wl.compositor);
  255. if (!region)
  256. return;
  257. wl_region_add(region, 0, 0, window->wl.width, window->wl.height);
  258. wl_surface_set_opaque_region(window->wl.surface, region);
  259. wl_surface_commit(window->wl.surface);
  260. wl_region_destroy(region);
  261. }
  262. static void resizeWindow(_GLFWwindow* window)
  263. {
  264. int scale = window->wl.scale;
  265. int scaledWidth = window->wl.width * scale;
  266. int scaledHeight = window->wl.height * scale;
  267. wl_egl_window_resize(window->wl.native, scaledWidth, scaledHeight, 0, 0);
  268. if (!window->wl.transparent)
  269. setOpaqueRegion(window);
  270. _glfwInputFramebufferSize(window, scaledWidth, scaledHeight);
  271. _glfwInputWindowContentScale(window, scale, scale);
  272. if (!window->wl.decorations.top.surface)
  273. return;
  274. // Top decoration.
  275. wp_viewport_set_destination(window->wl.decorations.top.viewport,
  276. window->wl.width, _GLFW_DECORATION_TOP);
  277. wl_surface_commit(window->wl.decorations.top.surface);
  278. // Left decoration.
  279. wp_viewport_set_destination(window->wl.decorations.left.viewport,
  280. _GLFW_DECORATION_WIDTH, window->wl.height + _GLFW_DECORATION_TOP);
  281. wl_surface_commit(window->wl.decorations.left.surface);
  282. // Right decoration.
  283. wl_subsurface_set_position(window->wl.decorations.right.subsurface,
  284. window->wl.width, -_GLFW_DECORATION_TOP);
  285. wp_viewport_set_destination(window->wl.decorations.right.viewport,
  286. _GLFW_DECORATION_WIDTH, window->wl.height + _GLFW_DECORATION_TOP);
  287. wl_surface_commit(window->wl.decorations.right.surface);
  288. // Bottom decoration.
  289. wl_subsurface_set_position(window->wl.decorations.bottom.subsurface,
  290. -_GLFW_DECORATION_WIDTH, window->wl.height);
  291. wp_viewport_set_destination(window->wl.decorations.bottom.viewport,
  292. window->wl.width + _GLFW_DECORATION_HORIZONTAL, _GLFW_DECORATION_WIDTH);
  293. wl_surface_commit(window->wl.decorations.bottom.surface);
  294. }
  295. static void checkScaleChange(_GLFWwindow* window)
  296. {
  297. int scale = 1;
  298. int i;
  299. int monitorScale;
  300. // Check if we will be able to set the buffer scale or not.
  301. if (_glfw.wl.compositorVersion < 3)
  302. return;
  303. // Get the scale factor from the highest scale monitor.
  304. for (i = 0; i < window->wl.monitorsCount; ++i)
  305. {
  306. monitorScale = window->wl.monitors[i]->wl.scale;
  307. if (scale < monitorScale)
  308. scale = monitorScale;
  309. }
  310. // Only change the framebuffer size if the scale changed.
  311. if (scale != window->wl.scale)
  312. {
  313. window->wl.scale = scale;
  314. wl_surface_set_buffer_scale(window->wl.surface, scale);
  315. resizeWindow(window);
  316. }
  317. }
  318. static void surfaceHandleEnter(void *data,
  319. struct wl_surface *surface,
  320. struct wl_output *output)
  321. {
  322. _GLFWwindow* window = data;
  323. _GLFWmonitor* monitor = wl_output_get_user_data(output);
  324. if (window->wl.monitorsCount + 1 > window->wl.monitorsSize)
  325. {
  326. ++window->wl.monitorsSize;
  327. window->wl.monitors =
  328. realloc(window->wl.monitors,
  329. window->wl.monitorsSize * sizeof(_GLFWmonitor*));
  330. }
  331. window->wl.monitors[window->wl.monitorsCount++] = monitor;
  332. checkScaleChange(window);
  333. }
  334. static void surfaceHandleLeave(void *data,
  335. struct wl_surface *surface,
  336. struct wl_output *output)
  337. {
  338. _GLFWwindow* window = data;
  339. _GLFWmonitor* monitor = wl_output_get_user_data(output);
  340. GLFWbool found;
  341. int i;
  342. for (i = 0, found = GLFW_FALSE; i < window->wl.monitorsCount - 1; ++i)
  343. {
  344. if (monitor == window->wl.monitors[i])
  345. found = GLFW_TRUE;
  346. if (found)
  347. window->wl.monitors[i] = window->wl.monitors[i + 1];
  348. }
  349. window->wl.monitors[--window->wl.monitorsCount] = NULL;
  350. checkScaleChange(window);
  351. }
  352. static const struct wl_surface_listener surfaceListener = {
  353. surfaceHandleEnter,
  354. surfaceHandleLeave
  355. };
  356. static void setIdleInhibitor(_GLFWwindow* window, GLFWbool enable)
  357. {
  358. if (enable && !window->wl.idleInhibitor && _glfw.wl.idleInhibitManager)
  359. {
  360. window->wl.idleInhibitor =
  361. zwp_idle_inhibit_manager_v1_create_inhibitor(
  362. _glfw.wl.idleInhibitManager, window->wl.surface);
  363. if (!window->wl.idleInhibitor)
  364. _glfwInputError(GLFW_PLATFORM_ERROR,
  365. "Wayland: Idle inhibitor creation failed");
  366. }
  367. else if (!enable && window->wl.idleInhibitor)
  368. {
  369. zwp_idle_inhibitor_v1_destroy(window->wl.idleInhibitor);
  370. window->wl.idleInhibitor = NULL;
  371. }
  372. }
  373. static GLFWbool createSurface(_GLFWwindow* window,
  374. const _GLFWwndconfig* wndconfig)
  375. {
  376. window->wl.surface = wl_compositor_create_surface(_glfw.wl.compositor);
  377. if (!window->wl.surface)
  378. return GLFW_FALSE;
  379. wl_surface_add_listener(window->wl.surface,
  380. &surfaceListener,
  381. window);
  382. wl_surface_set_user_data(window->wl.surface, window);
  383. window->wl.native = wl_egl_window_create(window->wl.surface,
  384. wndconfig->width,
  385. wndconfig->height);
  386. if (!window->wl.native)
  387. return GLFW_FALSE;
  388. window->wl.width = wndconfig->width;
  389. window->wl.height = wndconfig->height;
  390. window->wl.scale = 1;
  391. if (!window->wl.transparent)
  392. setOpaqueRegion(window);
  393. return GLFW_TRUE;
  394. }
  395. static void setFullscreen(_GLFWwindow* window, _GLFWmonitor* monitor,
  396. int refreshRate)
  397. {
  398. if (window->wl.xdg.toplevel)
  399. {
  400. xdg_toplevel_set_fullscreen(
  401. window->wl.xdg.toplevel,
  402. monitor->wl.output);
  403. }
  404. setIdleInhibitor(window, GLFW_TRUE);
  405. if (!window->wl.decorations.serverSide)
  406. destroyDecorations(window);
  407. }
  408. static void xdgToplevelHandleConfigure(void* data,
  409. struct xdg_toplevel* toplevel,
  410. int32_t width,
  411. int32_t height,
  412. struct wl_array* states)
  413. {
  414. _GLFWwindow* window = data;
  415. float aspectRatio;
  416. float targetRatio;
  417. uint32_t* state;
  418. GLFWbool maximized = GLFW_FALSE;
  419. GLFWbool fullscreen = GLFW_FALSE;
  420. GLFWbool activated = GLFW_FALSE;
  421. wl_array_for_each(state, states)
  422. {
  423. switch (*state)
  424. {
  425. case XDG_TOPLEVEL_STATE_MAXIMIZED:
  426. maximized = GLFW_TRUE;
  427. break;
  428. case XDG_TOPLEVEL_STATE_FULLSCREEN:
  429. fullscreen = GLFW_TRUE;
  430. break;
  431. case XDG_TOPLEVEL_STATE_RESIZING:
  432. break;
  433. case XDG_TOPLEVEL_STATE_ACTIVATED:
  434. activated = GLFW_TRUE;
  435. break;
  436. }
  437. }
  438. if (width != 0 && height != 0)
  439. {
  440. if (!maximized && !fullscreen)
  441. {
  442. if (window->numer != GLFW_DONT_CARE && window->denom != GLFW_DONT_CARE)
  443. {
  444. aspectRatio = (float)width / (float)height;
  445. targetRatio = (float)window->numer / (float)window->denom;
  446. if (aspectRatio < targetRatio)
  447. height = width / targetRatio;
  448. else if (aspectRatio > targetRatio)
  449. width = height * targetRatio;
  450. }
  451. }
  452. _glfwInputWindowSize(window, width, height);
  453. _glfwPlatformSetWindowSize(window, width, height);
  454. _glfwInputWindowDamage(window);
  455. }
  456. if (window->wl.wasFullscreen && window->autoIconify)
  457. {
  458. if (!activated || !fullscreen)
  459. {
  460. _glfwPlatformIconifyWindow(window);
  461. window->wl.wasFullscreen = GLFW_FALSE;
  462. }
  463. }
  464. if (fullscreen && activated)
  465. window->wl.wasFullscreen = GLFW_TRUE;
  466. _glfwInputWindowFocus(window, activated);
  467. }
  468. static void xdgToplevelHandleClose(void* data,
  469. struct xdg_toplevel* toplevel)
  470. {
  471. _GLFWwindow* window = data;
  472. _glfwInputWindowCloseRequest(window);
  473. }
  474. static const struct xdg_toplevel_listener xdgToplevelListener = {
  475. xdgToplevelHandleConfigure,
  476. xdgToplevelHandleClose
  477. };
  478. static void xdgSurfaceHandleConfigure(void* data,
  479. struct xdg_surface* surface,
  480. uint32_t serial)
  481. {
  482. xdg_surface_ack_configure(surface, serial);
  483. }
  484. static const struct xdg_surface_listener xdgSurfaceListener = {
  485. xdgSurfaceHandleConfigure
  486. };
  487. static void setXdgDecorations(_GLFWwindow* window)
  488. {
  489. if (_glfw.wl.decorationManager)
  490. {
  491. window->wl.xdg.decoration =
  492. zxdg_decoration_manager_v1_get_toplevel_decoration(
  493. _glfw.wl.decorationManager, window->wl.xdg.toplevel);
  494. zxdg_toplevel_decoration_v1_add_listener(window->wl.xdg.decoration,
  495. &xdgDecorationListener,
  496. window);
  497. zxdg_toplevel_decoration_v1_set_mode(
  498. window->wl.xdg.decoration,
  499. ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
  500. }
  501. else
  502. {
  503. window->wl.decorations.serverSide = GLFW_FALSE;
  504. createDecorations(window);
  505. }
  506. }
  507. static GLFWbool createXdgSurface(_GLFWwindow* window)
  508. {
  509. window->wl.xdg.surface = xdg_wm_base_get_xdg_surface(_glfw.wl.wmBase,
  510. window->wl.surface);
  511. if (!window->wl.xdg.surface)
  512. {
  513. _glfwInputError(GLFW_PLATFORM_ERROR,
  514. "Wayland: xdg-surface creation failed");
  515. return GLFW_FALSE;
  516. }
  517. xdg_surface_add_listener(window->wl.xdg.surface,
  518. &xdgSurfaceListener,
  519. window);
  520. window->wl.xdg.toplevel = xdg_surface_get_toplevel(window->wl.xdg.surface);
  521. if (!window->wl.xdg.toplevel)
  522. {
  523. _glfwInputError(GLFW_PLATFORM_ERROR,
  524. "Wayland: xdg-toplevel creation failed");
  525. return GLFW_FALSE;
  526. }
  527. xdg_toplevel_add_listener(window->wl.xdg.toplevel,
  528. &xdgToplevelListener,
  529. window);
  530. if (window->wl.title)
  531. xdg_toplevel_set_title(window->wl.xdg.toplevel, window->wl.title);
  532. if (window->minwidth != GLFW_DONT_CARE && window->minheight != GLFW_DONT_CARE)
  533. xdg_toplevel_set_min_size(window->wl.xdg.toplevel,
  534. window->minwidth, window->minheight);
  535. if (window->maxwidth != GLFW_DONT_CARE && window->maxheight != GLFW_DONT_CARE)
  536. xdg_toplevel_set_max_size(window->wl.xdg.toplevel,
  537. window->maxwidth, window->maxheight);
  538. if (window->monitor)
  539. {
  540. xdg_toplevel_set_fullscreen(window->wl.xdg.toplevel,
  541. window->monitor->wl.output);
  542. setIdleInhibitor(window, GLFW_TRUE);
  543. }
  544. else if (window->wl.maximized)
  545. {
  546. xdg_toplevel_set_maximized(window->wl.xdg.toplevel);
  547. setIdleInhibitor(window, GLFW_FALSE);
  548. setXdgDecorations(window);
  549. }
  550. else
  551. {
  552. setIdleInhibitor(window, GLFW_FALSE);
  553. setXdgDecorations(window);
  554. }
  555. wl_surface_commit(window->wl.surface);
  556. wl_display_roundtrip(_glfw.wl.display);
  557. return GLFW_TRUE;
  558. }
  559. static void setCursorImage(_GLFWwindow* window,
  560. _GLFWcursorWayland* cursorWayland)
  561. {
  562. struct itimerspec timer = {};
  563. struct wl_cursor* wlCursor = cursorWayland->cursor;
  564. struct wl_cursor_image* image;
  565. struct wl_buffer* buffer;
  566. struct wl_surface* surface = _glfw.wl.cursorSurface;
  567. int scale = 1;
  568. if (!wlCursor)
  569. buffer = cursorWayland->buffer;
  570. else
  571. {
  572. if (window->wl.scale > 1 && cursorWayland->cursorHiDPI)
  573. {
  574. wlCursor = cursorWayland->cursorHiDPI;
  575. scale = 2;
  576. }
  577. image = wlCursor->images[cursorWayland->currentImage];
  578. buffer = wl_cursor_image_get_buffer(image);
  579. if (!buffer)
  580. return;
  581. timer.it_value.tv_sec = image->delay / 1000;
  582. timer.it_value.tv_nsec = (image->delay % 1000) * 1000000;
  583. timerfd_settime(_glfw.wl.cursorTimerfd, 0, &timer, NULL);
  584. cursorWayland->width = image->width;
  585. cursorWayland->height = image->height;
  586. cursorWayland->xhot = image->hotspot_x;
  587. cursorWayland->yhot = image->hotspot_y;
  588. }
  589. wl_pointer_set_cursor(_glfw.wl.pointer, _glfw.wl.serial,
  590. surface,
  591. cursorWayland->xhot / scale,
  592. cursorWayland->yhot / scale);
  593. wl_surface_set_buffer_scale(surface, scale);
  594. wl_surface_attach(surface, buffer, 0, 0);
  595. wl_surface_damage(surface, 0, 0,
  596. cursorWayland->width, cursorWayland->height);
  597. wl_surface_commit(surface);
  598. }
  599. static void incrementCursorImage(_GLFWwindow* window)
  600. {
  601. _GLFWcursor* cursor;
  602. if (!window || window->wl.decorations.focus != mainWindow)
  603. return;
  604. cursor = window->wl.currentCursor;
  605. if (cursor && cursor->wl.cursor)
  606. {
  607. cursor->wl.currentImage += 1;
  608. cursor->wl.currentImage %= cursor->wl.cursor->image_count;
  609. setCursorImage(window, &cursor->wl);
  610. }
  611. }
  612. static void handleEvents(int timeout)
  613. {
  614. struct wl_display* display = _glfw.wl.display;
  615. struct pollfd fds[] = {
  616. { wl_display_get_fd(display), POLLIN },
  617. { _glfw.wl.timerfd, POLLIN },
  618. { _glfw.wl.cursorTimerfd, POLLIN },
  619. };
  620. ssize_t read_ret;
  621. uint64_t repeats, i;
  622. while (wl_display_prepare_read(display) != 0)
  623. wl_display_dispatch_pending(display);
  624. // If an error different from EAGAIN happens, we have likely been
  625. // disconnected from the Wayland session, try to handle that the best we
  626. // can.
  627. if (wl_display_flush(display) < 0 && errno != EAGAIN)
  628. {
  629. _GLFWwindow* window = _glfw.windowListHead;
  630. while (window)
  631. {
  632. _glfwInputWindowCloseRequest(window);
  633. window = window->next;
  634. }
  635. wl_display_cancel_read(display);
  636. return;
  637. }
  638. if (poll(fds, 3, timeout) > 0)
  639. {
  640. if (fds[0].revents & POLLIN)
  641. {
  642. wl_display_read_events(display);
  643. wl_display_dispatch_pending(display);
  644. }
  645. else
  646. {
  647. wl_display_cancel_read(display);
  648. }
  649. if (fds[1].revents & POLLIN)
  650. {
  651. read_ret = read(_glfw.wl.timerfd, &repeats, sizeof(repeats));
  652. if (read_ret != 8)
  653. return;
  654. for (i = 0; i < repeats; ++i)
  655. _glfwInputKey(_glfw.wl.keyboardFocus, _glfw.wl.keyboardLastKey,
  656. _glfw.wl.keyboardLastScancode, GLFW_REPEAT,
  657. _glfw.wl.xkb.modifiers);
  658. }
  659. if (fds[2].revents & POLLIN)
  660. {
  661. read_ret = read(_glfw.wl.cursorTimerfd, &repeats, sizeof(repeats));
  662. if (read_ret != 8)
  663. return;
  664. incrementCursorImage(_glfw.wl.pointerFocus);
  665. }
  666. }
  667. else
  668. {
  669. wl_display_cancel_read(display);
  670. }
  671. }
  672. //////////////////////////////////////////////////////////////////////////
  673. ////// GLFW platform API //////
  674. //////////////////////////////////////////////////////////////////////////
  675. int _glfwPlatformCreateWindow(_GLFWwindow* window,
  676. const _GLFWwndconfig* wndconfig,
  677. const _GLFWctxconfig* ctxconfig,
  678. const _GLFWfbconfig* fbconfig)
  679. {
  680. window->wl.transparent = fbconfig->transparent;
  681. if (!createSurface(window, wndconfig))
  682. return GLFW_FALSE;
  683. if (ctxconfig->client != GLFW_NO_API)
  684. {
  685. if (ctxconfig->source == GLFW_EGL_CONTEXT_API ||
  686. ctxconfig->source == GLFW_NATIVE_CONTEXT_API)
  687. {
  688. if (!_glfwInitEGL())
  689. return GLFW_FALSE;
  690. if (!_glfwCreateContextEGL(window, ctxconfig, fbconfig))
  691. return GLFW_FALSE;
  692. }
  693. else if (ctxconfig->source == GLFW_OSMESA_CONTEXT_API)
  694. {
  695. if (!_glfwInitOSMesa())
  696. return GLFW_FALSE;
  697. if (!_glfwCreateContextOSMesa(window, ctxconfig, fbconfig))
  698. return GLFW_FALSE;
  699. }
  700. }
  701. if (wndconfig->title)
  702. window->wl.title = _glfw_strdup(wndconfig->title);
  703. if (wndconfig->visible)
  704. {
  705. if (!createXdgSurface(window))
  706. return GLFW_FALSE;
  707. window->wl.visible = GLFW_TRUE;
  708. }
  709. else
  710. {
  711. window->wl.xdg.surface = NULL;
  712. window->wl.xdg.toplevel = NULL;
  713. window->wl.visible = GLFW_FALSE;
  714. }
  715. window->wl.currentCursor = NULL;
  716. window->wl.monitors = calloc(1, sizeof(_GLFWmonitor*));
  717. window->wl.monitorsCount = 0;
  718. window->wl.monitorsSize = 1;
  719. return GLFW_TRUE;
  720. }
  721. void _glfwPlatformDestroyWindow(_GLFWwindow* window)
  722. {
  723. if (window == _glfw.wl.pointerFocus)
  724. {
  725. _glfw.wl.pointerFocus = NULL;
  726. _glfwInputCursorEnter(window, GLFW_FALSE);
  727. }
  728. if (window == _glfw.wl.keyboardFocus)
  729. {
  730. _glfw.wl.keyboardFocus = NULL;
  731. _glfwInputWindowFocus(window, GLFW_FALSE);
  732. }
  733. if (window->wl.idleInhibitor)
  734. zwp_idle_inhibitor_v1_destroy(window->wl.idleInhibitor);
  735. if (window->context.destroy)
  736. window->context.destroy(window);
  737. destroyDecorations(window);
  738. if (window->wl.xdg.decoration)
  739. zxdg_toplevel_decoration_v1_destroy(window->wl.xdg.decoration);
  740. if (window->wl.decorations.buffer)
  741. wl_buffer_destroy(window->wl.decorations.buffer);
  742. if (window->wl.native)
  743. wl_egl_window_destroy(window->wl.native);
  744. if (window->wl.xdg.toplevel)
  745. xdg_toplevel_destroy(window->wl.xdg.toplevel);
  746. if (window->wl.xdg.surface)
  747. xdg_surface_destroy(window->wl.xdg.surface);
  748. if (window->wl.surface)
  749. wl_surface_destroy(window->wl.surface);
  750. free(window->wl.title);
  751. free(window->wl.monitors);
  752. }
  753. void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title)
  754. {
  755. if (window->wl.title)
  756. free(window->wl.title);
  757. window->wl.title = _glfw_strdup(title);
  758. if (window->wl.xdg.toplevel)
  759. xdg_toplevel_set_title(window->wl.xdg.toplevel, title);
  760. }
  761. void _glfwPlatformSetWindowIcon(_GLFWwindow* window,
  762. int count, const GLFWimage* images)
  763. {
  764. _glfwInputError(GLFW_PLATFORM_ERROR,
  765. "Wayland: Setting window icon not supported");
  766. }
  767. void _glfwPlatformGetWindowPos(_GLFWwindow* window, int* xpos, int* ypos)
  768. {
  769. // A Wayland client is not aware of its position, so just warn and leave it
  770. // as (0, 0)
  771. _glfwInputError(GLFW_PLATFORM_ERROR,
  772. "Wayland: Window position retrieval not supported");
  773. }
  774. void _glfwPlatformSetWindowPos(_GLFWwindow* window, int xpos, int ypos)
  775. {
  776. // A Wayland client can not set its position, so just warn
  777. _glfwInputError(GLFW_PLATFORM_ERROR,
  778. "Wayland: Window position setting not supported");
  779. }
  780. void _glfwPlatformGetWindowSize(_GLFWwindow* window, int* width, int* height)
  781. {
  782. if (width)
  783. *width = window->wl.width;
  784. if (height)
  785. *height = window->wl.height;
  786. }
  787. void _glfwPlatformSetWindowSize(_GLFWwindow* window, int width, int height)
  788. {
  789. window->wl.width = width;
  790. window->wl.height = height;
  791. resizeWindow(window);
  792. }
  793. void _glfwPlatformSetWindowSizeLimits(_GLFWwindow* window,
  794. int minwidth, int minheight,
  795. int maxwidth, int maxheight)
  796. {
  797. if (window->wl.xdg.toplevel)
  798. {
  799. if (minwidth == GLFW_DONT_CARE || minheight == GLFW_DONT_CARE)
  800. minwidth = minheight = 0;
  801. if (maxwidth == GLFW_DONT_CARE || maxheight == GLFW_DONT_CARE)
  802. maxwidth = maxheight = 0;
  803. xdg_toplevel_set_min_size(window->wl.xdg.toplevel, minwidth, minheight);
  804. xdg_toplevel_set_max_size(window->wl.xdg.toplevel, maxwidth, maxheight);
  805. wl_surface_commit(window->wl.surface);
  806. }
  807. }
  808. void _glfwPlatformSetWindowAspectRatio(_GLFWwindow* window,
  809. int numer, int denom)
  810. {
  811. // TODO: find out how to trigger a resize.
  812. // The actual limits are checked in the xdg_toplevel::configure handler.
  813. }
  814. void _glfwPlatformGetFramebufferSize(_GLFWwindow* window,
  815. int* width, int* height)
  816. {
  817. _glfwPlatformGetWindowSize(window, width, height);
  818. *width *= window->wl.scale;
  819. *height *= window->wl.scale;
  820. }
  821. void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window,
  822. int* left, int* top,
  823. int* right, int* bottom)
  824. {
  825. if (window->decorated && !window->monitor && !window->wl.decorations.serverSide)
  826. {
  827. if (top)
  828. *top = _GLFW_DECORATION_TOP;
  829. if (left)
  830. *left = _GLFW_DECORATION_WIDTH;
  831. if (right)
  832. *right = _GLFW_DECORATION_WIDTH;
  833. if (bottom)
  834. *bottom = _GLFW_DECORATION_WIDTH;
  835. }
  836. }
  837. void _glfwPlatformGetWindowContentScale(_GLFWwindow* window,
  838. float* xscale, float* yscale)
  839. {
  840. if (xscale)
  841. *xscale = (float) window->wl.scale;
  842. if (yscale)
  843. *yscale = (float) window->wl.scale;
  844. }
  845. void _glfwPlatformIconifyWindow(_GLFWwindow* window)
  846. {
  847. if (window->wl.xdg.toplevel)
  848. xdg_toplevel_set_minimized(window->wl.xdg.toplevel);
  849. }
  850. void _glfwPlatformRestoreWindow(_GLFWwindow* window)
  851. {
  852. if (window->wl.xdg.toplevel)
  853. {
  854. if (window->monitor)
  855. xdg_toplevel_unset_fullscreen(window->wl.xdg.toplevel);
  856. if (window->wl.maximized)
  857. xdg_toplevel_unset_maximized(window->wl.xdg.toplevel);
  858. // There is no way to unset minimized, or even to know if we are
  859. // minimized, so there is nothing to do in this case.
  860. }
  861. _glfwInputWindowMonitor(window, NULL);
  862. window->wl.maximized = GLFW_FALSE;
  863. }
  864. void _glfwPlatformMaximizeWindow(_GLFWwindow* window)
  865. {
  866. if (window->wl.xdg.toplevel)
  867. {
  868. xdg_toplevel_set_maximized(window->wl.xdg.toplevel);
  869. }
  870. window->wl.maximized = GLFW_TRUE;
  871. }
  872. void _glfwPlatformShowWindow(_GLFWwindow* window)
  873. {
  874. if (!window->wl.visible)
  875. {
  876. createXdgSurface(window);
  877. window->wl.visible = GLFW_TRUE;
  878. }
  879. }
  880. void _glfwPlatformHideWindow(_GLFWwindow* window)
  881. {
  882. if (window->wl.xdg.toplevel)
  883. {
  884. xdg_toplevel_destroy(window->wl.xdg.toplevel);
  885. xdg_surface_destroy(window->wl.xdg.surface);
  886. window->wl.xdg.toplevel = NULL;
  887. window->wl.xdg.surface = NULL;
  888. }
  889. window->wl.visible = GLFW_FALSE;
  890. }
  891. void _glfwPlatformRequestWindowAttention(_GLFWwindow* window)
  892. {
  893. // TODO
  894. _glfwInputError(GLFW_PLATFORM_ERROR,
  895. "Wayland: Window attention request not implemented yet");
  896. }
  897. void _glfwPlatformFocusWindow(_GLFWwindow* window)
  898. {
  899. _glfwInputError(GLFW_PLATFORM_ERROR,
  900. "Wayland: Focusing a window requires user interaction");
  901. }
  902. void _glfwPlatformSetWindowMonitor(_GLFWwindow* window,
  903. _GLFWmonitor* monitor,
  904. int xpos, int ypos,
  905. int width, int height,
  906. int refreshRate)
  907. {
  908. if (monitor)
  909. {
  910. setFullscreen(window, monitor, refreshRate);
  911. }
  912. else
  913. {
  914. if (window->wl.xdg.toplevel)
  915. xdg_toplevel_unset_fullscreen(window->wl.xdg.toplevel);
  916. setIdleInhibitor(window, GLFW_FALSE);
  917. if (!_glfw.wl.decorationManager)
  918. createDecorations(window);
  919. }
  920. _glfwInputWindowMonitor(window, monitor);
  921. }
  922. int _glfwPlatformWindowFocused(_GLFWwindow* window)
  923. {
  924. return _glfw.wl.keyboardFocus == window;
  925. }
  926. int _glfwPlatformWindowIconified(_GLFWwindow* window)
  927. {
  928. // xdg-shell doesn’t give any way to request whether a surface is
  929. // iconified.
  930. return GLFW_FALSE;
  931. }
  932. int _glfwPlatformWindowVisible(_GLFWwindow* window)
  933. {
  934. return window->wl.visible;
  935. }
  936. int _glfwPlatformWindowMaximized(_GLFWwindow* window)
  937. {
  938. return window->wl.maximized;
  939. }
  940. int _glfwPlatformWindowHovered(_GLFWwindow* window)
  941. {
  942. return window->wl.hovered;
  943. }
  944. int _glfwPlatformFramebufferTransparent(_GLFWwindow* window)
  945. {
  946. return window->wl.transparent;
  947. }
  948. void _glfwPlatformSetWindowResizable(_GLFWwindow* window, GLFWbool enabled)
  949. {
  950. // TODO
  951. _glfwInputError(GLFW_PLATFORM_ERROR,
  952. "Wayland: Window attribute setting not implemented yet");
  953. }
  954. void _glfwPlatformSetWindowDecorated(_GLFWwindow* window, GLFWbool enabled)
  955. {
  956. if (!window->monitor)
  957. {
  958. if (enabled)
  959. createDecorations(window);
  960. else
  961. destroyDecorations(window);
  962. }
  963. }
  964. void _glfwPlatformSetWindowFloating(_GLFWwindow* window, GLFWbool enabled)
  965. {
  966. // TODO
  967. _glfwInputError(GLFW_PLATFORM_ERROR,
  968. "Wayland: Window attribute setting not implemented yet");
  969. }
  970. float _glfwPlatformGetWindowOpacity(_GLFWwindow* window)
  971. {
  972. return 1.f;
  973. }
  974. void _glfwPlatformSetWindowOpacity(_GLFWwindow* window, float opacity)
  975. {
  976. }
  977. void _glfwPlatformSetRawMouseMotion(_GLFWwindow *window, GLFWbool enabled)
  978. {
  979. // This is handled in relativePointerHandleRelativeMotion
  980. }
  981. GLFWbool _glfwPlatformRawMouseMotionSupported(void)
  982. {
  983. return GLFW_TRUE;
  984. }
  985. void _glfwPlatformPollEvents(void)
  986. {
  987. handleEvents(0);
  988. }
  989. void _glfwPlatformWaitEvents(void)
  990. {
  991. handleEvents(-1);
  992. }
  993. void _glfwPlatformWaitEventsTimeout(double timeout)
  994. {
  995. handleEvents((int) (timeout * 1e3));
  996. }
  997. void _glfwPlatformPostEmptyEvent(void)
  998. {
  999. wl_display_sync(_glfw.wl.display);
  1000. }
  1001. void _glfwPlatformGetCursorPos(_GLFWwindow* window, double* xpos, double* ypos)
  1002. {
  1003. if (xpos)
  1004. *xpos = window->wl.cursorPosX;
  1005. if (ypos)
  1006. *ypos = window->wl.cursorPosY;
  1007. }
  1008. static GLFWbool isPointerLocked(_GLFWwindow* window);
  1009. void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y)
  1010. {
  1011. if (isPointerLocked(window))
  1012. {
  1013. zwp_locked_pointer_v1_set_cursor_position_hint(
  1014. window->wl.pointerLock.lockedPointer,
  1015. wl_fixed_from_double(x), wl_fixed_from_double(y));
  1016. wl_surface_commit(window->wl.surface);
  1017. }
  1018. }
  1019. void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode)
  1020. {
  1021. _glfwPlatformSetCursor(window, window->wl.currentCursor);
  1022. }
  1023. const char* _glfwPlatformGetScancodeName(int scancode)
  1024. {
  1025. // TODO
  1026. return NULL;
  1027. }
  1028. int _glfwPlatformGetKeyScancode(int key)
  1029. {
  1030. return _glfw.wl.scancodes[key];
  1031. }
  1032. int _glfwPlatformCreateCursor(_GLFWcursor* cursor,
  1033. const GLFWimage* image,
  1034. int xhot, int yhot)
  1035. {
  1036. cursor->wl.buffer = createShmBuffer(image);
  1037. if (!cursor->wl.buffer)
  1038. return GLFW_FALSE;
  1039. cursor->wl.width = image->width;
  1040. cursor->wl.height = image->height;
  1041. cursor->wl.xhot = xhot;
  1042. cursor->wl.yhot = yhot;
  1043. return GLFW_TRUE;
  1044. }
  1045. int _glfwPlatformCreateStandardCursor(_GLFWcursor* cursor, int shape)
  1046. {
  1047. const char* name = NULL;
  1048. // Try the XDG names first
  1049. if (shape == GLFW_ARROW_CURSOR)
  1050. name = "default";
  1051. else if (shape == GLFW_IBEAM_CURSOR)
  1052. name = "text";
  1053. else if (shape == GLFW_CROSSHAIR_CURSOR)
  1054. name = "crosshair";
  1055. else if (shape == GLFW_POINTING_HAND_CURSOR)
  1056. name = "pointer";
  1057. else if (shape == GLFW_RESIZE_EW_CURSOR)
  1058. name = "ew-resize";
  1059. else if (shape == GLFW_RESIZE_NS_CURSOR)
  1060. name = "ns-resize";
  1061. else if (shape == GLFW_RESIZE_NWSE_CURSOR)
  1062. name = "nwse-resize";
  1063. else if (shape == GLFW_RESIZE_NESW_CURSOR)
  1064. name = "nesw-resize";
  1065. else if (shape == GLFW_RESIZE_ALL_CURSOR)
  1066. name = "all-scroll";
  1067. else if (shape == GLFW_NOT_ALLOWED_CURSOR)
  1068. name = "not-allowed";
  1069. cursor->wl.cursor = wl_cursor_theme_get_cursor(_glfw.wl.cursorTheme, name);
  1070. if (_glfw.wl.cursorThemeHiDPI)
  1071. {
  1072. cursor->wl.cursorHiDPI =
  1073. wl_cursor_theme_get_cursor(_glfw.wl.cursorThemeHiDPI, name);
  1074. }
  1075. if (!cursor->wl.cursor)
  1076. {
  1077. // Fall back to the core X11 names
  1078. if (shape == GLFW_ARROW_CURSOR)
  1079. name = "left_ptr";
  1080. else if (shape == GLFW_IBEAM_CURSOR)
  1081. name = "xterm";
  1082. else if (shape == GLFW_CROSSHAIR_CURSOR)
  1083. name = "crosshair";
  1084. else if (shape == GLFW_POINTING_HAND_CURSOR)
  1085. name = "hand2";
  1086. else if (shape == GLFW_RESIZE_EW_CURSOR)
  1087. name = "sb_h_double_arrow";
  1088. else if (shape == GLFW_RESIZE_NS_CURSOR)
  1089. name = "sb_v_double_arrow";
  1090. else if (shape == GLFW_RESIZE_ALL_CURSOR)
  1091. name = "fleur";
  1092. else
  1093. {
  1094. _glfwInputError(GLFW_CURSOR_UNAVAILABLE,
  1095. "Wayland: Standard cursor shape unavailable");
  1096. return GLFW_FALSE;
  1097. }
  1098. cursor->wl.cursor = wl_cursor_theme_get_cursor(_glfw.wl.cursorTheme, name);
  1099. if (!cursor->wl.cursor)
  1100. {
  1101. _glfwInputError(GLFW_PLATFORM_ERROR,
  1102. "Wayland: Failed to create standard cursor \"%s\"",
  1103. name);
  1104. return GLFW_FALSE;
  1105. }
  1106. if (_glfw.wl.cursorThemeHiDPI)
  1107. {
  1108. if (!cursor->wl.cursorHiDPI)
  1109. {
  1110. cursor->wl.cursorHiDPI =
  1111. wl_cursor_theme_get_cursor(_glfw.wl.cursorThemeHiDPI, name);
  1112. }
  1113. }
  1114. }
  1115. return GLFW_TRUE;
  1116. }
  1117. void _glfwPlatformDestroyCursor(_GLFWcursor* cursor)
  1118. {
  1119. // If it's a standard cursor we don't need to do anything here
  1120. if (cursor->wl.cursor)
  1121. return;
  1122. if (cursor->wl.buffer)
  1123. wl_buffer_destroy(cursor->wl.buffer);
  1124. }
  1125. static void relativePointerHandleRelativeMotion(void* data,
  1126. struct zwp_relative_pointer_v1* pointer,
  1127. uint32_t timeHi,
  1128. uint32_t timeLo,
  1129. wl_fixed_t dx,
  1130. wl_fixed_t dy,
  1131. wl_fixed_t dxUnaccel,
  1132. wl_fixed_t dyUnaccel)
  1133. {
  1134. _GLFWwindow* window = data;
  1135. double xpos = window->virtualCursorPosX;
  1136. double ypos = window->virtualCursorPosY;
  1137. if (window->cursorMode != GLFW_CURSOR_DISABLED)
  1138. return;
  1139. if (window->rawMouseMotion)
  1140. {
  1141. xpos += wl_fixed_to_double(dxUnaccel);
  1142. ypos += wl_fixed_to_double(dyUnaccel);
  1143. }
  1144. else
  1145. {
  1146. xpos += wl_fixed_to_double(dx);
  1147. ypos += wl_fixed_to_double(dy);
  1148. }
  1149. _glfwInputCursorPos(window, xpos, ypos);
  1150. }
  1151. static const struct zwp_relative_pointer_v1_listener relativePointerListener = {
  1152. relativePointerHandleRelativeMotion
  1153. };
  1154. static void lockedPointerHandleLocked(void* data,
  1155. struct zwp_locked_pointer_v1* lockedPointer)
  1156. {
  1157. }
  1158. static void unlockPointer(_GLFWwindow* window)
  1159. {
  1160. struct zwp_relative_pointer_v1* relativePointer =
  1161. window->wl.pointerLock.relativePointer;
  1162. struct zwp_locked_pointer_v1* lockedPointer =
  1163. window->wl.pointerLock.lockedPointer;
  1164. zwp_relative_pointer_v1_destroy(relativePointer);
  1165. zwp_locked_pointer_v1_destroy(lockedPointer);
  1166. window->wl.pointerLock.relativePointer = NULL;
  1167. window->wl.pointerLock.lockedPointer = NULL;
  1168. }
  1169. static void lockPointer(_GLFWwindow* window);
  1170. static void lockedPointerHandleUnlocked(void* data,
  1171. struct zwp_locked_pointer_v1* lockedPointer)
  1172. {
  1173. }
  1174. static const struct zwp_locked_pointer_v1_listener lockedPointerListener = {
  1175. lockedPointerHandleLocked,
  1176. lockedPointerHandleUnlocked
  1177. };
  1178. static void lockPointer(_GLFWwindow* window)
  1179. {
  1180. struct zwp_relative_pointer_v1* relativePointer;
  1181. struct zwp_locked_pointer_v1* lockedPointer;
  1182. if (!_glfw.wl.relativePointerManager)
  1183. {
  1184. _glfwInputError(GLFW_PLATFORM_ERROR,
  1185. "Wayland: no relative pointer manager");
  1186. return;
  1187. }
  1188. relativePointer =
  1189. zwp_relative_pointer_manager_v1_get_relative_pointer(
  1190. _glfw.wl.relativePointerManager,
  1191. _glfw.wl.pointer);
  1192. zwp_relative_pointer_v1_add_listener(relativePointer,
  1193. &relativePointerListener,
  1194. window);
  1195. lockedPointer =
  1196. zwp_pointer_constraints_v1_lock_pointer(
  1197. _glfw.wl.pointerConstraints,
  1198. window->wl.surface,
  1199. _glfw.wl.pointer,
  1200. NULL,
  1201. ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
  1202. zwp_locked_pointer_v1_add_listener(lockedPointer,
  1203. &lockedPointerListener,
  1204. window);
  1205. window->wl.pointerLock.relativePointer = relativePointer;
  1206. window->wl.pointerLock.lockedPointer = lockedPointer;
  1207. wl_pointer_set_cursor(_glfw.wl.pointer, _glfw.wl.serial,
  1208. NULL, 0, 0);
  1209. }
  1210. static GLFWbool isPointerLocked(_GLFWwindow* window)
  1211. {
  1212. return window->wl.pointerLock.lockedPointer != NULL;
  1213. }
  1214. void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor)
  1215. {
  1216. struct wl_cursor* defaultCursor;
  1217. struct wl_cursor* defaultCursorHiDPI = NULL;
  1218. if (!_glfw.wl.pointer)
  1219. return;
  1220. window->wl.currentCursor = cursor;
  1221. // If we're not in the correct window just save the cursor
  1222. // the next time the pointer enters the window the cursor will change
  1223. if (window != _glfw.wl.pointerFocus || window->wl.decorations.focus != mainWindow)
  1224. return;
  1225. // Unlock possible pointer lock if no longer disabled.
  1226. if (window->cursorMode != GLFW_CURSOR_DISABLED && isPointerLocked(window))
  1227. unlockPointer(window);
  1228. if (window->cursorMode == GLFW_CURSOR_NORMAL)
  1229. {
  1230. if (cursor)
  1231. setCursorImage(window, &cursor->wl);
  1232. else
  1233. {
  1234. defaultCursor = wl_cursor_theme_get_cursor(_glfw.wl.cursorTheme,
  1235. "left_ptr");
  1236. if (!defaultCursor)
  1237. {
  1238. _glfwInputError(GLFW_PLATFORM_ERROR,
  1239. "Wayland: Standard cursor not found");
  1240. return;
  1241. }
  1242. if (_glfw.wl.cursorThemeHiDPI)
  1243. defaultCursorHiDPI =
  1244. wl_cursor_theme_get_cursor(_glfw.wl.cursorThemeHiDPI,
  1245. "left_ptr");
  1246. _GLFWcursorWayland cursorWayland = {
  1247. defaultCursor,
  1248. defaultCursorHiDPI,
  1249. NULL,
  1250. 0, 0,
  1251. 0, 0,
  1252. 0
  1253. };
  1254. setCursorImage(window, &cursorWayland);
  1255. }
  1256. }
  1257. else if (window->cursorMode == GLFW_CURSOR_DISABLED)
  1258. {
  1259. if (!isPointerLocked(window))
  1260. lockPointer(window);
  1261. }
  1262. else if (window->cursorMode == GLFW_CURSOR_HIDDEN)
  1263. {
  1264. wl_pointer_set_cursor(_glfw.wl.pointer, _glfw.wl.serial, NULL, 0, 0);
  1265. }
  1266. }
  1267. static void dataSourceHandleTarget(void* data,
  1268. struct wl_data_source* dataSource,
  1269. const char* mimeType)
  1270. {
  1271. if (_glfw.wl.dataSource != dataSource)
  1272. {
  1273. _glfwInputError(GLFW_PLATFORM_ERROR,
  1274. "Wayland: Unknown clipboard data source");
  1275. return;
  1276. }
  1277. }
  1278. static void dataSourceHandleSend(void* data,
  1279. struct wl_data_source* dataSource,
  1280. const char* mimeType,
  1281. int fd)
  1282. {
  1283. const char* string = _glfw.wl.clipboardSendString;
  1284. size_t len = _glfw.wl.clipboardSendSize;
  1285. int ret;
  1286. if (_glfw.wl.dataSource != dataSource)
  1287. {
  1288. _glfwInputError(GLFW_PLATFORM_ERROR,
  1289. "Wayland: Unknown clipboard data source");
  1290. return;
  1291. }
  1292. if (!string)
  1293. {
  1294. _glfwInputError(GLFW_PLATFORM_ERROR,
  1295. "Wayland: Copy requested from an invalid string");
  1296. return;
  1297. }
  1298. if (strcmp(mimeType, "text/plain;charset=utf-8") != 0)
  1299. {
  1300. _glfwInputError(GLFW_PLATFORM_ERROR,
  1301. "Wayland: Wrong MIME type asked from clipboard");
  1302. close(fd);
  1303. return;
  1304. }
  1305. while (len > 0)
  1306. {
  1307. ret = write(fd, string, len);
  1308. if (ret == -1 && errno == EINTR)
  1309. continue;
  1310. if (ret == -1)
  1311. {
  1312. // TODO: also report errno maybe.
  1313. _glfwInputError(GLFW_PLATFORM_ERROR,
  1314. "Wayland: Error while writing the clipboard");
  1315. close(fd);
  1316. return;
  1317. }
  1318. len -= ret;
  1319. }
  1320. close(fd);
  1321. }
  1322. static void dataSourceHandleCancelled(void* data,
  1323. struct wl_data_source* dataSource)
  1324. {
  1325. wl_data_source_destroy(dataSource);
  1326. if (_glfw.wl.dataSource != dataSource)
  1327. {
  1328. _glfwInputError(GLFW_PLATFORM_ERROR,
  1329. "Wayland: Unknown clipboard data source");
  1330. return;
  1331. }
  1332. _glfw.wl.dataSource = NULL;
  1333. }
  1334. static const struct wl_data_source_listener dataSourceListener = {
  1335. dataSourceHandleTarget,
  1336. dataSourceHandleSend,
  1337. dataSourceHandleCancelled,
  1338. };
  1339. void _glfwPlatformSetClipboardString(const char* string)
  1340. {
  1341. if (_glfw.wl.dataSource)
  1342. {
  1343. wl_data_source_destroy(_glfw.wl.dataSource);
  1344. _glfw.wl.dataSource = NULL;
  1345. }
  1346. if (_glfw.wl.clipboardSendString)
  1347. {
  1348. free(_glfw.wl.clipboardSendString);
  1349. _glfw.wl.clipboardSendString = NULL;
  1350. }
  1351. _glfw.wl.clipboardSendString = strdup(string);
  1352. if (!_glfw.wl.clipboardSendString)
  1353. {
  1354. _glfwInputError(GLFW_PLATFORM_ERROR,
  1355. "Wayland: Impossible to allocate clipboard string");
  1356. return;
  1357. }
  1358. _glfw.wl.clipboardSendSize = strlen(string);
  1359. _glfw.wl.dataSource =
  1360. wl_data_device_manager_create_data_source(_glfw.wl.dataDeviceManager);
  1361. if (!_glfw.wl.dataSource)
  1362. {
  1363. _glfwInputError(GLFW_PLATFORM_ERROR,
  1364. "Wayland: Impossible to create clipboard source");
  1365. free(_glfw.wl.clipboardSendString);
  1366. return;
  1367. }
  1368. wl_data_source_add_listener(_glfw.wl.dataSource,
  1369. &dataSourceListener,
  1370. NULL);
  1371. wl_data_source_offer(_glfw.wl.dataSource, "text/plain;charset=utf-8");
  1372. wl_data_device_set_selection(_glfw.wl.dataDevice,
  1373. _glfw.wl.dataSource,
  1374. _glfw.wl.serial);
  1375. }
  1376. static GLFWbool growClipboardString(void)
  1377. {
  1378. char* clipboard = _glfw.wl.clipboardString;
  1379. clipboard = realloc(clipboard, _glfw.wl.clipboardSize * 2);
  1380. if (!clipboard)
  1381. {
  1382. _glfwInputError(GLFW_PLATFORM_ERROR,
  1383. "Wayland: Impossible to grow clipboard string");
  1384. return GLFW_FALSE;
  1385. }
  1386. _glfw.wl.clipboardString = clipboard;
  1387. _glfw.wl.clipboardSize = _glfw.wl.clipboardSize * 2;
  1388. return GLFW_TRUE;
  1389. }
  1390. const char* _glfwPlatformGetClipboardString(void)
  1391. {
  1392. int fds[2];
  1393. int ret;
  1394. size_t len = 0;
  1395. if (!_glfw.wl.dataOffer)
  1396. {
  1397. _glfwInputError(GLFW_FORMAT_UNAVAILABLE,
  1398. "No clipboard data has been sent yet");
  1399. return NULL;
  1400. }
  1401. ret = pipe2(fds, O_CLOEXEC);
  1402. if (ret < 0)
  1403. {
  1404. // TODO: also report errno maybe?
  1405. _glfwInputError(GLFW_PLATFORM_ERROR,
  1406. "Wayland: Impossible to create clipboard pipe fds");
  1407. return NULL;
  1408. }
  1409. wl_data_offer_receive(_glfw.wl.dataOffer, "text/plain;charset=utf-8", fds[1]);
  1410. close(fds[1]);
  1411. // XXX: this is a huge hack, this function shouldn’t be synchronous!
  1412. handleEvents(-1);
  1413. while (1)
  1414. {
  1415. // Grow the clipboard if we need to paste something bigger, there is no
  1416. // shrink operation yet.
  1417. if (len + 4096 > _glfw.wl.clipboardSize)
  1418. {
  1419. if (!growClipboardString())
  1420. {
  1421. close(fds[0]);
  1422. return NULL;
  1423. }
  1424. }
  1425. // Then read from the fd to the clipboard, handling all known errors.
  1426. ret = read(fds[0], _glfw.wl.clipboardString + len, 4096);
  1427. if (ret == 0)
  1428. break;
  1429. if (ret == -1 && errno == EINTR)
  1430. continue;
  1431. if (ret == -1)
  1432. {
  1433. // TODO: also report errno maybe.
  1434. _glfwInputError(GLFW_PLATFORM_ERROR,
  1435. "Wayland: Impossible to read from clipboard fd");
  1436. close(fds[0]);
  1437. return NULL;
  1438. }
  1439. len += ret;
  1440. }
  1441. close(fds[0]);
  1442. if (len + 1 > _glfw.wl.clipboardSize)
  1443. {
  1444. if (!growClipboardString())
  1445. return NULL;
  1446. }
  1447. _glfw.wl.clipboardString[len] = '\0';
  1448. return _glfw.wl.clipboardString;
  1449. }
  1450. void _glfwPlatformGetRequiredInstanceExtensions(char** extensions)
  1451. {
  1452. if (!_glfw.vk.KHR_surface || !_glfw.vk.KHR_wayland_surface)
  1453. return;
  1454. extensions[0] = "VK_KHR_surface";
  1455. extensions[1] = "VK_KHR_wayland_surface";
  1456. }
  1457. int _glfwPlatformGetPhysicalDevicePresentationSupport(VkInstance instance,
  1458. VkPhysicalDevice device,
  1459. uint32_t queuefamily)
  1460. {
  1461. PFN_vkGetPhysicalDeviceWaylandPresentationSupportKHR
  1462. vkGetPhysicalDeviceWaylandPresentationSupportKHR =
  1463. (PFN_vkGetPhysicalDeviceWaylandPresentationSupportKHR)
  1464. vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceWaylandPresentationSupportKHR");
  1465. if (!vkGetPhysicalDeviceWaylandPresentationSupportKHR)
  1466. {
  1467. _glfwInputError(GLFW_API_UNAVAILABLE,
  1468. "Wayland: Vulkan instance missing VK_KHR_wayland_surface extension");
  1469. return VK_NULL_HANDLE;
  1470. }
  1471. return vkGetPhysicalDeviceWaylandPresentationSupportKHR(device,
  1472. queuefamily,
  1473. _glfw.wl.display);
  1474. }
  1475. VkResult _glfwPlatformCreateWindowSurface(VkInstance instance,
  1476. _GLFWwindow* window,
  1477. const VkAllocationCallbacks* allocator,
  1478. VkSurfaceKHR* surface)
  1479. {
  1480. VkResult err;
  1481. VkWaylandSurfaceCreateInfoKHR sci;
  1482. PFN_vkCreateWaylandSurfaceKHR vkCreateWaylandSurfaceKHR;
  1483. vkCreateWaylandSurfaceKHR = (PFN_vkCreateWaylandSurfaceKHR)
  1484. vkGetInstanceProcAddr(instance, "vkCreateWaylandSurfaceKHR");
  1485. if (!vkCreateWaylandSurfaceKHR)
  1486. {
  1487. _glfwInputError(GLFW_API_UNAVAILABLE,
  1488. "Wayland: Vulkan instance missing VK_KHR_wayland_surface extension");
  1489. return VK_ERROR_EXTENSION_NOT_PRESENT;
  1490. }
  1491. memset(&sci, 0, sizeof(sci));
  1492. sci.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
  1493. sci.display = _glfw.wl.display;
  1494. sci.surface = window->wl.surface;
  1495. err = vkCreateWaylandSurfaceKHR(instance, &sci, allocator, surface);
  1496. if (err)
  1497. {
  1498. _glfwInputError(GLFW_PLATFORM_ERROR,
  1499. "Wayland: Failed to create Vulkan surface: %s",
  1500. _glfwGetVulkanResultString(err));
  1501. }
  1502. return err;
  1503. }
  1504. //////////////////////////////////////////////////////////////////////////
  1505. ////// GLFW native API //////
  1506. //////////////////////////////////////////////////////////////////////////
  1507. GLFWAPI struct wl_display* glfwGetWaylandDisplay(void)
  1508. {
  1509. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  1510. return _glfw.wl.display;
  1511. }
  1512. GLFWAPI struct wl_surface* glfwGetWaylandWindow(GLFWwindow* handle)
  1513. {
  1514. _GLFWwindow* window = (_GLFWwindow*) handle;
  1515. _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
  1516. return window->wl.surface;
  1517. }