1 //========================================================================
2 // GLFW 3.4 - www.glfw.org
3 //------------------------------------------------------------------------
4 // Copyright (c) 2002-2006 Marcus Geelnard
5 // Copyright (c) 2006-2019 Camilla Löwy <elmindreda@glfw.org>
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 // Please use C89 style variable declarations in this file because VS 2010
28 //========================================================================
40 // Lexically compare video modes, used by qsort
42 static int compareVideoModes(const void* fp, const void* sp)
44 const GLFWvidmode* fm = fp;
45 const GLFWvidmode* sm = sp;
46 const int fbpp = fm->redBits + fm->greenBits + fm->blueBits;
47 const int sbpp = sm->redBits + sm->greenBits + sm->blueBits;
48 const int farea = fm->width * fm->height;
49 const int sarea = sm->width * sm->height;
51 // First sort on color bits per pixel
55 // Then sort on screen area
60 if (fm->width != sm->width)
61 return fm->width - sm->width;
63 // Lastly sort on refresh rate
64 return fm->refreshRate - sm->refreshRate;
67 // Retrieves the available modes for the specified monitor
69 static GLFWbool refreshVideoModes(_GLFWmonitor* monitor)
77 modes = _glfwPlatformGetVideoModes(monitor, &modeCount);
81 qsort(modes, modeCount, sizeof(GLFWvidmode), compareVideoModes);
84 monitor->modes = modes;
85 monitor->modeCount = modeCount;
91 //////////////////////////////////////////////////////////////////////////
92 ////// GLFW event API //////
93 //////////////////////////////////////////////////////////////////////////
95 // Notifies shared code of a monitor connection or disconnection
97 void _glfwInputMonitor(_GLFWmonitor* monitor, int action, int placement)
99 if (action == GLFW_CONNECTED)
101 _glfw.monitorCount++;
103 realloc(_glfw.monitors, sizeof(_GLFWmonitor*) * _glfw.monitorCount);
105 if (placement == _GLFW_INSERT_FIRST)
107 memmove(_glfw.monitors + 1,
109 ((size_t) _glfw.monitorCount - 1) * sizeof(_GLFWmonitor*));
110 _glfw.monitors[0] = monitor;
113 _glfw.monitors[_glfw.monitorCount - 1] = monitor;
115 else if (action == GLFW_DISCONNECTED)
120 for (window = _glfw.windowListHead; window; window = window->next)
122 if (window->monitor == monitor)
124 int width, height, xoff, yoff;
125 _glfwPlatformGetWindowSize(window, &width, &height);
126 _glfwPlatformSetWindowMonitor(window, NULL, 0, 0, width, height, 0);
127 _glfwPlatformGetWindowFrameSize(window, &xoff, &yoff, NULL, NULL);
128 _glfwPlatformSetWindowPos(window, xoff, yoff);
132 for (i = 0; i < _glfw.monitorCount; i++)
134 if (_glfw.monitors[i] == monitor)
136 _glfw.monitorCount--;
137 memmove(_glfw.monitors + i,
138 _glfw.monitors + i + 1,
139 ((size_t) _glfw.monitorCount - i) * sizeof(_GLFWmonitor*));
145 if (_glfw.callbacks.monitor)
146 _glfw.callbacks.monitor((GLFWmonitor*) monitor, action);
148 if (action == GLFW_DISCONNECTED)
149 _glfwFreeMonitor(monitor);
152 // Notifies shared code that a full screen window has acquired or released
155 void _glfwInputMonitorWindow(_GLFWmonitor* monitor, _GLFWwindow* window)
157 monitor->window = window;
161 //////////////////////////////////////////////////////////////////////////
162 ////// GLFW internal API //////
163 //////////////////////////////////////////////////////////////////////////
165 // Allocates and returns a monitor object with the specified name and dimensions
167 _GLFWmonitor* _glfwAllocMonitor(const char* name, int widthMM, int heightMM)
169 _GLFWmonitor* monitor = calloc(1, sizeof(_GLFWmonitor));
170 monitor->widthMM = widthMM;
171 monitor->heightMM = heightMM;
174 monitor->name = _glfw_strdup(name);
179 // Frees a monitor object and any data associated with it
181 void _glfwFreeMonitor(_GLFWmonitor* monitor)
186 _glfwPlatformFreeMonitor(monitor);
188 _glfwFreeGammaArrays(&monitor->originalRamp);
189 _glfwFreeGammaArrays(&monitor->currentRamp);
191 free(monitor->modes);
196 // Allocates red, green and blue value arrays of the specified size
198 void _glfwAllocGammaArrays(GLFWgammaramp* ramp, unsigned int size)
200 ramp->red = calloc(size, sizeof(unsigned short));
201 ramp->green = calloc(size, sizeof(unsigned short));
202 ramp->blue = calloc(size, sizeof(unsigned short));
206 // Frees the red, green and blue value arrays and clears the struct
208 void _glfwFreeGammaArrays(GLFWgammaramp* ramp)
214 memset(ramp, 0, sizeof(GLFWgammaramp));
217 // Chooses the video mode most closely matching the desired one
219 const GLFWvidmode* _glfwChooseVideoMode(_GLFWmonitor* monitor,
220 const GLFWvidmode* desired)
223 unsigned int sizeDiff, leastSizeDiff = UINT_MAX;
224 unsigned int rateDiff, leastRateDiff = UINT_MAX;
225 unsigned int colorDiff, leastColorDiff = UINT_MAX;
226 const GLFWvidmode* current;
227 const GLFWvidmode* closest = NULL;
229 if (!refreshVideoModes(monitor))
232 for (i = 0; i < monitor->modeCount; i++)
234 current = monitor->modes + i;
238 if (desired->redBits != GLFW_DONT_CARE)
239 colorDiff += abs(current->redBits - desired->redBits);
240 if (desired->greenBits != GLFW_DONT_CARE)
241 colorDiff += abs(current->greenBits - desired->greenBits);
242 if (desired->blueBits != GLFW_DONT_CARE)
243 colorDiff += abs(current->blueBits - desired->blueBits);
245 sizeDiff = abs((current->width - desired->width) *
246 (current->width - desired->width) +
247 (current->height - desired->height) *
248 (current->height - desired->height));
250 if (desired->refreshRate != GLFW_DONT_CARE)
251 rateDiff = abs(current->refreshRate - desired->refreshRate);
253 rateDiff = UINT_MAX - current->refreshRate;
255 if ((colorDiff < leastColorDiff) ||
256 (colorDiff == leastColorDiff && sizeDiff < leastSizeDiff) ||
257 (colorDiff == leastColorDiff && sizeDiff == leastSizeDiff && rateDiff < leastRateDiff))
260 leastSizeDiff = sizeDiff;
261 leastRateDiff = rateDiff;
262 leastColorDiff = colorDiff;
269 // Performs lexical comparison between two @ref GLFWvidmode structures
271 int _glfwCompareVideoModes(const GLFWvidmode* fm, const GLFWvidmode* sm)
273 return compareVideoModes(fm, sm);
276 // Splits a color depth into red, green and blue bit depths
278 void _glfwSplitBPP(int bpp, int* red, int* green, int* blue)
282 // We assume that by 32 the user really meant 24
286 // Convert "bits per pixel" to red, green & blue sizes
288 *red = *green = *blue = bpp / 3;
289 delta = bpp - (*red * 3);
298 //////////////////////////////////////////////////////////////////////////
299 ////// GLFW public API //////
300 //////////////////////////////////////////////////////////////////////////
302 GLFWAPI GLFWmonitor** glfwGetMonitors(int* count)
304 assert(count != NULL);
308 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
310 *count = _glfw.monitorCount;
311 return (GLFWmonitor**) _glfw.monitors;
314 GLFWAPI GLFWmonitor* glfwGetPrimaryMonitor(void)
316 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
318 if (!_glfw.monitorCount)
321 return (GLFWmonitor*) _glfw.monitors[0];
324 GLFWAPI void glfwGetMonitorPos(GLFWmonitor* handle, int* xpos, int* ypos)
326 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
327 assert(monitor != NULL);
334 _GLFW_REQUIRE_INIT();
336 _glfwPlatformGetMonitorPos(monitor, xpos, ypos);
339 GLFWAPI void glfwGetMonitorWorkarea(GLFWmonitor* handle,
340 int* xpos, int* ypos,
341 int* width, int* height)
343 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
344 assert(monitor != NULL);
355 _GLFW_REQUIRE_INIT();
357 _glfwPlatformGetMonitorWorkarea(monitor, xpos, ypos, width, height);
360 GLFWAPI void glfwGetMonitorPhysicalSize(GLFWmonitor* handle, int* widthMM, int* heightMM)
362 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
363 assert(monitor != NULL);
370 _GLFW_REQUIRE_INIT();
373 *widthMM = monitor->widthMM;
375 *heightMM = monitor->heightMM;
378 GLFWAPI void glfwGetMonitorContentScale(GLFWmonitor* handle,
379 float* xscale, float* yscale)
381 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
382 assert(monitor != NULL);
389 _GLFW_REQUIRE_INIT();
390 _glfwPlatformGetMonitorContentScale(monitor, xscale, yscale);
393 GLFWAPI const char* glfwGetMonitorName(GLFWmonitor* handle)
395 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
396 assert(monitor != NULL);
398 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
399 return monitor->name;
402 GLFWAPI void glfwSetMonitorUserPointer(GLFWmonitor* handle, void* pointer)
404 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
405 assert(monitor != NULL);
407 _GLFW_REQUIRE_INIT();
408 monitor->userPointer = pointer;
411 GLFWAPI void* glfwGetMonitorUserPointer(GLFWmonitor* handle)
413 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
414 assert(monitor != NULL);
416 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
417 return monitor->userPointer;
420 GLFWAPI GLFWmonitorfun glfwSetMonitorCallback(GLFWmonitorfun cbfun)
422 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
423 _GLFW_SWAP_POINTERS(_glfw.callbacks.monitor, cbfun);
427 GLFWAPI const GLFWvidmode* glfwGetVideoModes(GLFWmonitor* handle, int* count)
429 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
430 assert(monitor != NULL);
431 assert(count != NULL);
435 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
437 if (!refreshVideoModes(monitor))
440 *count = monitor->modeCount;
441 return monitor->modes;
444 GLFWAPI const GLFWvidmode* glfwGetVideoMode(GLFWmonitor* handle)
446 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
447 assert(monitor != NULL);
449 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
451 _glfwPlatformGetVideoMode(monitor, &monitor->currentMode);
452 return &monitor->currentMode;
455 GLFWAPI void glfwSetGamma(GLFWmonitor* handle, float gamma)
458 unsigned short* values;
460 const GLFWgammaramp* original;
461 assert(handle != NULL);
463 assert(gamma <= FLT_MAX);
465 _GLFW_REQUIRE_INIT();
467 if (gamma != gamma || gamma <= 0.f || gamma > FLT_MAX)
469 _glfwInputError(GLFW_INVALID_VALUE, "Invalid gamma value %f", gamma);
473 original = glfwGetGammaRamp(handle);
477 values = calloc(original->size, sizeof(unsigned short));
479 for (i = 0; i < original->size; i++)
483 // Calculate intensity
484 value = i / (float) (original->size - 1);
486 value = powf(value, 1.f / gamma) * 65535.f + 0.5f;
487 // Clamp to value range
488 value = _glfw_fminf(value, 65535.f);
490 values[i] = (unsigned short) value;
496 ramp.size = original->size;
498 glfwSetGammaRamp(handle, &ramp);
502 GLFWAPI const GLFWgammaramp* glfwGetGammaRamp(GLFWmonitor* handle)
504 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
505 assert(monitor != NULL);
507 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
509 _glfwFreeGammaArrays(&monitor->currentRamp);
510 if (!_glfwPlatformGetGammaRamp(monitor, &monitor->currentRamp))
513 return &monitor->currentRamp;
516 GLFWAPI void glfwSetGammaRamp(GLFWmonitor* handle, const GLFWgammaramp* ramp)
518 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
519 assert(monitor != NULL);
520 assert(ramp != NULL);
521 assert(ramp->size > 0);
522 assert(ramp->red != NULL);
523 assert(ramp->green != NULL);
524 assert(ramp->blue != NULL);
528 _glfwInputError(GLFW_INVALID_VALUE,
529 "Invalid gamma ramp size %i",
534 _GLFW_REQUIRE_INIT();
536 if (!monitor->originalRamp.size)
538 if (!_glfwPlatformGetGammaRamp(monitor, &monitor->originalRamp))
542 _glfwPlatformSetGammaRamp(monitor, ramp);