1 //========================================================================
2 // GLFW 3.4 X11 - www.glfw.org
3 //------------------------------------------------------------------------
4 // Copyright (c) 2002-2006 Marcus Geelnard
5 // Copyright (c) 2006-2019 Camilla Löwy <elmindreda@glfw.org>
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.
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:
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.
20 // 2. Altered source versions must be plainly marked as such, and must not
21 // be misrepresented as being the original software.
23 // 3. This notice may not be removed or altered from any source
26 //========================================================================
27 // It is fine to use C99 in this file because it will not be built with VS
28 //========================================================================
38 // Check whether the display mode should be included in enumeration
40 static GLFWbool modeIsGood(const XRRModeInfo* mi)
42 return (mi->modeFlags & RR_Interlace) == 0;
45 // Calculates the refresh rate, in Hz, from the specified RandR mode info
47 static int calculateRefreshRate(const XRRModeInfo* mi)
49 if (mi->hTotal && mi->vTotal)
50 return (int) round((double) mi->dotClock / ((double) mi->hTotal * (double) mi->vTotal));
55 // Returns the mode info for a RandR mode XID
57 static const XRRModeInfo* getModeInfo(const XRRScreenResources* sr, RRMode id)
59 for (int i = 0; i < sr->nmode; i++)
61 if (sr->modes[i].id == id)
68 // Convert RandR mode info to GLFW video mode
70 static GLFWvidmode vidmodeFromModeInfo(const XRRModeInfo* mi,
71 const XRRCrtcInfo* ci)
75 if (ci->rotation == RR_Rotate_90 || ci->rotation == RR_Rotate_270)
77 mode.width = mi->height;
78 mode.height = mi->width;
82 mode.width = mi->width;
83 mode.height = mi->height;
86 mode.refreshRate = calculateRefreshRate(mi);
88 _glfwSplitBPP(DefaultDepth(_glfw.x11.display, _glfw.x11.screen),
89 &mode.redBits, &mode.greenBits, &mode.blueBits);
95 //////////////////////////////////////////////////////////////////////////
96 ////// GLFW internal API //////
97 //////////////////////////////////////////////////////////////////////////
99 // Poll for changes in the set of connected monitors
101 void _glfwPollMonitorsX11(void)
103 if (_glfw.x11.randr.available && !_glfw.x11.randr.monitorBroken)
105 int disconnectedCount, screenCount = 0;
106 _GLFWmonitor** disconnected = NULL;
107 XineramaScreenInfo* screens = NULL;
108 XRRScreenResources* sr = XRRGetScreenResourcesCurrent(_glfw.x11.display,
110 RROutput primary = XRRGetOutputPrimary(_glfw.x11.display,
113 if (_glfw.x11.xinerama.available)
114 screens = XineramaQueryScreens(_glfw.x11.display, &screenCount);
116 disconnectedCount = _glfw.monitorCount;
117 if (disconnectedCount)
119 disconnected = calloc(_glfw.monitorCount, sizeof(_GLFWmonitor*));
122 _glfw.monitorCount * sizeof(_GLFWmonitor*));
125 for (int i = 0; i < sr->noutput; i++)
127 int j, type, widthMM, heightMM;
129 XRROutputInfo* oi = XRRGetOutputInfo(_glfw.x11.display, sr, sr->outputs[i]);
130 if (oi->connection != RR_Connected || oi->crtc == None)
132 XRRFreeOutputInfo(oi);
136 for (j = 0; j < disconnectedCount; j++)
138 if (disconnected[j] &&
139 disconnected[j]->x11.output == sr->outputs[i])
141 disconnected[j] = NULL;
146 if (j < disconnectedCount)
148 XRRFreeOutputInfo(oi);
152 XRRCrtcInfo* ci = XRRGetCrtcInfo(_glfw.x11.display, sr, oi->crtc);
153 if (ci->rotation == RR_Rotate_90 || ci->rotation == RR_Rotate_270)
155 widthMM = oi->mm_height;
156 heightMM = oi->mm_width;
160 widthMM = oi->mm_width;
161 heightMM = oi->mm_height;
164 if (widthMM <= 0 || heightMM <= 0)
166 // HACK: If RandR does not provide a physical size, assume the
167 // X11 default 96 DPI and calculate from the CRTC viewport
168 // NOTE: These members are affected by rotation, unlike the mode
169 // info and output info members
170 widthMM = (int) (ci->width * 25.4f / 96.f);
171 heightMM = (int) (ci->height * 25.4f / 96.f);
174 _GLFWmonitor* monitor = _glfwAllocMonitor(oi->name, widthMM, heightMM);
175 monitor->x11.output = sr->outputs[i];
176 monitor->x11.crtc = oi->crtc;
178 for (j = 0; j < screenCount; j++)
180 if (screens[j].x_org == ci->x &&
181 screens[j].y_org == ci->y &&
182 screens[j].width == ci->width &&
183 screens[j].height == ci->height)
185 monitor->x11.index = j;
190 if (monitor->x11.output == primary)
191 type = _GLFW_INSERT_FIRST;
193 type = _GLFW_INSERT_LAST;
195 _glfwInputMonitor(monitor, GLFW_CONNECTED, type);
197 XRRFreeOutputInfo(oi);
201 XRRFreeScreenResources(sr);
206 for (int i = 0; i < disconnectedCount; i++)
209 _glfwInputMonitor(disconnected[i], GLFW_DISCONNECTED, 0);
216 const int widthMM = DisplayWidthMM(_glfw.x11.display, _glfw.x11.screen);
217 const int heightMM = DisplayHeightMM(_glfw.x11.display, _glfw.x11.screen);
219 _glfwInputMonitor(_glfwAllocMonitor("Display", widthMM, heightMM),
225 // Set the current video mode for the specified monitor
227 void _glfwSetVideoModeX11(_GLFWmonitor* monitor, const GLFWvidmode* desired)
229 if (_glfw.x11.randr.available && !_glfw.x11.randr.monitorBroken)
232 RRMode native = None;
234 const GLFWvidmode* best = _glfwChooseVideoMode(monitor, desired);
235 _glfwPlatformGetVideoMode(monitor, ¤t);
236 if (_glfwCompareVideoModes(¤t, best) == 0)
239 XRRScreenResources* sr =
240 XRRGetScreenResourcesCurrent(_glfw.x11.display, _glfw.x11.root);
241 XRRCrtcInfo* ci = XRRGetCrtcInfo(_glfw.x11.display, sr, monitor->x11.crtc);
242 XRROutputInfo* oi = XRRGetOutputInfo(_glfw.x11.display, sr, monitor->x11.output);
244 for (int i = 0; i < oi->nmode; i++)
246 const XRRModeInfo* mi = getModeInfo(sr, oi->modes[i]);
250 const GLFWvidmode mode = vidmodeFromModeInfo(mi, ci);
251 if (_glfwCompareVideoModes(best, &mode) == 0)
260 if (monitor->x11.oldMode == None)
261 monitor->x11.oldMode = ci->mode;
263 XRRSetCrtcConfig(_glfw.x11.display,
264 sr, monitor->x11.crtc,
273 XRRFreeOutputInfo(oi);
275 XRRFreeScreenResources(sr);
279 // Restore the saved (original) video mode for the specified monitor
281 void _glfwRestoreVideoModeX11(_GLFWmonitor* monitor)
283 if (_glfw.x11.randr.available && !_glfw.x11.randr.monitorBroken)
285 if (monitor->x11.oldMode == None)
288 XRRScreenResources* sr =
289 XRRGetScreenResourcesCurrent(_glfw.x11.display, _glfw.x11.root);
290 XRRCrtcInfo* ci = XRRGetCrtcInfo(_glfw.x11.display, sr, monitor->x11.crtc);
292 XRRSetCrtcConfig(_glfw.x11.display,
293 sr, monitor->x11.crtc,
296 monitor->x11.oldMode,
302 XRRFreeScreenResources(sr);
304 monitor->x11.oldMode = None;
309 //////////////////////////////////////////////////////////////////////////
310 ////// GLFW platform API //////
311 //////////////////////////////////////////////////////////////////////////
313 void _glfwPlatformFreeMonitor(_GLFWmonitor* monitor)
317 void _glfwPlatformGetMonitorPos(_GLFWmonitor* monitor, int* xpos, int* ypos)
319 if (_glfw.x11.randr.available && !_glfw.x11.randr.monitorBroken)
321 XRRScreenResources* sr =
322 XRRGetScreenResourcesCurrent(_glfw.x11.display, _glfw.x11.root);
323 XRRCrtcInfo* ci = XRRGetCrtcInfo(_glfw.x11.display, sr, monitor->x11.crtc);
335 XRRFreeScreenResources(sr);
339 void _glfwPlatformGetMonitorContentScale(_GLFWmonitor* monitor,
340 float* xscale, float* yscale)
343 *xscale = _glfw.x11.contentScaleX;
345 *yscale = _glfw.x11.contentScaleY;
348 void _glfwPlatformGetMonitorWorkarea(_GLFWmonitor* monitor, int* xpos, int* ypos, int* width, int* height)
350 int areaX = 0, areaY = 0, areaWidth = 0, areaHeight = 0;
352 if (_glfw.x11.randr.available && !_glfw.x11.randr.monitorBroken)
354 XRRScreenResources* sr =
355 XRRGetScreenResourcesCurrent(_glfw.x11.display, _glfw.x11.root);
356 XRRCrtcInfo* ci = XRRGetCrtcInfo(_glfw.x11.display, sr, monitor->x11.crtc);
361 const XRRModeInfo* mi = getModeInfo(sr, ci->mode);
363 if (ci->rotation == RR_Rotate_90 || ci->rotation == RR_Rotate_270)
365 areaWidth = mi->height;
366 areaHeight = mi->width;
370 areaWidth = mi->width;
371 areaHeight = mi->height;
375 XRRFreeScreenResources(sr);
379 areaWidth = DisplayWidth(_glfw.x11.display, _glfw.x11.screen);
380 areaHeight = DisplayHeight(_glfw.x11.display, _glfw.x11.screen);
383 if (_glfw.x11.NET_WORKAREA && _glfw.x11.NET_CURRENT_DESKTOP)
385 Atom* extents = NULL;
386 Atom* desktop = NULL;
387 const unsigned long extentCount =
388 _glfwGetWindowPropertyX11(_glfw.x11.root,
389 _glfw.x11.NET_WORKAREA,
391 (unsigned char**) &extents);
393 if (_glfwGetWindowPropertyX11(_glfw.x11.root,
394 _glfw.x11.NET_CURRENT_DESKTOP,
396 (unsigned char**) &desktop) > 0)
398 if (extentCount >= 4 && *desktop < extentCount / 4)
400 const int globalX = extents[*desktop * 4 + 0];
401 const int globalY = extents[*desktop * 4 + 1];
402 const int globalWidth = extents[*desktop * 4 + 2];
403 const int globalHeight = extents[*desktop * 4 + 3];
407 areaWidth -= globalX - areaX;
413 areaHeight -= globalY - areaY;
417 if (areaX + areaWidth > globalX + globalWidth)
418 areaWidth = globalX - areaX + globalWidth;
419 if (areaY + areaHeight > globalY + globalHeight)
420 areaHeight = globalY - areaY + globalHeight;
437 *height = areaHeight;
440 GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* count)
446 if (_glfw.x11.randr.available && !_glfw.x11.randr.monitorBroken)
448 XRRScreenResources* sr =
449 XRRGetScreenResourcesCurrent(_glfw.x11.display, _glfw.x11.root);
450 XRRCrtcInfo* ci = XRRGetCrtcInfo(_glfw.x11.display, sr, monitor->x11.crtc);
451 XRROutputInfo* oi = XRRGetOutputInfo(_glfw.x11.display, sr, monitor->x11.output);
453 result = calloc(oi->nmode, sizeof(GLFWvidmode));
455 for (int i = 0; i < oi->nmode; i++)
457 const XRRModeInfo* mi = getModeInfo(sr, oi->modes[i]);
461 const GLFWvidmode mode = vidmodeFromModeInfo(mi, ci);
464 for (j = 0; j < *count; j++)
466 if (_glfwCompareVideoModes(result + j, &mode) == 0)
470 // Skip duplicate modes
475 result[*count - 1] = mode;
478 XRRFreeOutputInfo(oi);
480 XRRFreeScreenResources(sr);
485 result = calloc(1, sizeof(GLFWvidmode));
486 _glfwPlatformGetVideoMode(monitor, result);
492 void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode)
494 if (_glfw.x11.randr.available && !_glfw.x11.randr.monitorBroken)
496 XRRScreenResources* sr =
497 XRRGetScreenResourcesCurrent(_glfw.x11.display, _glfw.x11.root);
498 XRRCrtcInfo* ci = XRRGetCrtcInfo(_glfw.x11.display, sr, monitor->x11.crtc);
502 const XRRModeInfo* mi = getModeInfo(sr, ci->mode);
503 if (mi) // mi can be NULL if the monitor has been disconnected
504 *mode = vidmodeFromModeInfo(mi, ci);
509 XRRFreeScreenResources(sr);
513 mode->width = DisplayWidth(_glfw.x11.display, _glfw.x11.screen);
514 mode->height = DisplayHeight(_glfw.x11.display, _glfw.x11.screen);
515 mode->refreshRate = 0;
517 _glfwSplitBPP(DefaultDepth(_glfw.x11.display, _glfw.x11.screen),
518 &mode->redBits, &mode->greenBits, &mode->blueBits);
522 GLFWbool _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp)
524 if (_glfw.x11.randr.available && !_glfw.x11.randr.gammaBroken)
526 const size_t size = XRRGetCrtcGammaSize(_glfw.x11.display,
528 XRRCrtcGamma* gamma = XRRGetCrtcGamma(_glfw.x11.display,
531 _glfwAllocGammaArrays(ramp, size);
533 memcpy(ramp->red, gamma->red, size * sizeof(unsigned short));
534 memcpy(ramp->green, gamma->green, size * sizeof(unsigned short));
535 memcpy(ramp->blue, gamma->blue, size * sizeof(unsigned short));
540 else if (_glfw.x11.vidmode.available)
543 XF86VidModeGetGammaRampSize(_glfw.x11.display, _glfw.x11.screen, &size);
545 _glfwAllocGammaArrays(ramp, size);
547 XF86VidModeGetGammaRamp(_glfw.x11.display,
549 ramp->size, ramp->red, ramp->green, ramp->blue);
554 _glfwInputError(GLFW_PLATFORM_ERROR,
555 "X11: Gamma ramp access not supported by server");
560 void _glfwPlatformSetGammaRamp(_GLFWmonitor* monitor, const GLFWgammaramp* ramp)
562 if (_glfw.x11.randr.available && !_glfw.x11.randr.gammaBroken)
564 if (XRRGetCrtcGammaSize(_glfw.x11.display, monitor->x11.crtc) != ramp->size)
566 _glfwInputError(GLFW_PLATFORM_ERROR,
567 "X11: Gamma ramp size must match current ramp size");
571 XRRCrtcGamma* gamma = XRRAllocGamma(ramp->size);
573 memcpy(gamma->red, ramp->red, ramp->size * sizeof(unsigned short));
574 memcpy(gamma->green, ramp->green, ramp->size * sizeof(unsigned short));
575 memcpy(gamma->blue, ramp->blue, ramp->size * sizeof(unsigned short));
577 XRRSetCrtcGamma(_glfw.x11.display, monitor->x11.crtc, gamma);
580 else if (_glfw.x11.vidmode.available)
582 XF86VidModeSetGammaRamp(_glfw.x11.display,
585 (unsigned short*) ramp->red,
586 (unsigned short*) ramp->green,
587 (unsigned short*) ramp->blue);
591 _glfwInputError(GLFW_PLATFORM_ERROR,
592 "X11: Gamma ramp access not supported by server");
597 //////////////////////////////////////////////////////////////////////////
598 ////// GLFW native API //////
599 //////////////////////////////////////////////////////////////////////////
601 GLFWAPI RRCrtc glfwGetX11Adapter(GLFWmonitor* handle)
603 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
604 _GLFW_REQUIRE_INIT_OR_RETURN(None);
605 return monitor->x11.crtc;
608 GLFWAPI RROutput glfwGetX11Monitor(GLFWmonitor* handle)
610 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
611 _GLFW_REQUIRE_INIT_OR_RETURN(None);
612 return monitor->x11.output;