1 //========================================================================
2 // GLFW 3.4 - www.glfw.org
3 //------------------------------------------------------------------------
4 // Copyright (c) 2002-2006 Marcus Geelnard
5 // Copyright (c) 2006-2019 Camilla Löwy <elmindreda@glfw.org>
6 //
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 // Please use C89 style variable declarations in this file because VS 2010
28 //========================================================================
29
30 #include "internal.h"
31
32 #include <assert.h>
33 #include <math.h>
34 #include <float.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <limits.h>
38
39
40 // Lexically compare video modes, used by qsort
41 //
compareVideoModes(const void * fp,const void * sp)42 static int compareVideoModes(const void* fp, const void* sp)
43 {
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;
50
51 // First sort on color bits per pixel
52 if (fbpp != sbpp)
53 return fbpp - sbpp;
54
55 // Then sort on screen area
56 if (farea != sarea)
57 return farea - sarea;
58
59 // Then sort on width
60 if (fm->width != sm->width)
61 return fm->width - sm->width;
62
63 // Lastly sort on refresh rate
64 return fm->refreshRate - sm->refreshRate;
65 }
66
67 // Retrieves the available modes for the specified monitor
68 //
refreshVideoModes(_GLFWmonitor * monitor)69 static bool refreshVideoModes(_GLFWmonitor* monitor)
70 {
71 int modeCount;
72 GLFWvidmode* modes;
73
74 if (monitor->modes)
75 return true;
76
77 modes = _glfwPlatformGetVideoModes(monitor, &modeCount);
78 if (!modes)
79 return false;
80
81 qsort(modes, modeCount, sizeof(modes[0]), compareVideoModes);
82
83 free(monitor->modes);
84 monitor->modes = modes;
85 monitor->modeCount = modeCount;
86
87 return true;
88 }
89
90
91 //////////////////////////////////////////////////////////////////////////
92 ////// GLFW event API //////
93 //////////////////////////////////////////////////////////////////////////
94
95 // Notifies shared code of a monitor connection or disconnection
96 //
_glfwInputMonitor(_GLFWmonitor * monitor,int action,int placement)97 void _glfwInputMonitor(_GLFWmonitor* monitor, int action, int placement)
98 {
99 if (action == GLFW_CONNECTED)
100 {
101 _glfw.monitorCount++;
102 _glfw.monitors =
103 realloc(_glfw.monitors, sizeof(_GLFWmonitor*) * _glfw.monitorCount);
104
105 if (placement == _GLFW_INSERT_FIRST)
106 {
107 memmove(_glfw.monitors + 1,
108 _glfw.monitors,
109 ((size_t) _glfw.monitorCount - 1) * sizeof(_GLFWmonitor*));
110 _glfw.monitors[0] = monitor;
111 }
112 else
113 _glfw.monitors[_glfw.monitorCount - 1] = monitor;
114 }
115 else if (action == GLFW_DISCONNECTED)
116 {
117 int i;
118 _GLFWwindow* window;
119
120 for (window = _glfw.windowListHead; window; window = window->next)
121 {
122 if (window->monitor == monitor)
123 {
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);
129 }
130 }
131
132 for (i = 0; i < _glfw.monitorCount; i++)
133 {
134 if (_glfw.monitors[i] == monitor)
135 {
136 remove_i_from_array(_glfw.monitors, i, _glfw.monitorCount);
137 break;
138 }
139 }
140 }
141
142 if (_glfw.callbacks.monitor)
143 _glfw.callbacks.monitor((GLFWmonitor*) monitor, action);
144
145 if (action == GLFW_DISCONNECTED)
146 _glfwFreeMonitor(monitor);
147 }
148
149 // Notifies shared code that a full screen window has acquired or released
150 // a monitor
151 //
_glfwInputMonitorWindow(_GLFWmonitor * monitor,_GLFWwindow * window)152 void _glfwInputMonitorWindow(_GLFWmonitor* monitor, _GLFWwindow* window)
153 {
154 monitor->window = window;
155 }
156
157
158 //////////////////////////////////////////////////////////////////////////
159 ////// GLFW internal API //////
160 //////////////////////////////////////////////////////////////////////////
161
162 // Allocates and returns a monitor object with the specified name and dimensions
163 //
_glfwAllocMonitor(const char * name,int widthMM,int heightMM)164 _GLFWmonitor* _glfwAllocMonitor(const char* name, int widthMM, int heightMM)
165 {
166 _GLFWmonitor* monitor = calloc(1, sizeof(_GLFWmonitor));
167 monitor->widthMM = widthMM;
168 monitor->heightMM = heightMM;
169
170 if (name)
171 monitor->name = _glfw_strdup(name);
172
173 return monitor;
174 }
175
176 // Frees a monitor object and any data associated with it
177 //
_glfwFreeMonitor(_GLFWmonitor * monitor)178 void _glfwFreeMonitor(_GLFWmonitor* monitor)
179 {
180 if (monitor == NULL)
181 return;
182
183 _glfwPlatformFreeMonitor(monitor);
184
185 _glfwFreeGammaArrays(&monitor->originalRamp);
186 _glfwFreeGammaArrays(&monitor->currentRamp);
187
188 free(monitor->modes);
189 free(monitor->name);
190 free(monitor);
191 }
192
193 // Allocates red, green and blue value arrays of the specified size
194 //
_glfwAllocGammaArrays(GLFWgammaramp * ramp,unsigned int size)195 void _glfwAllocGammaArrays(GLFWgammaramp* ramp, unsigned int size)
196 {
197 ramp->red = calloc(size, sizeof(unsigned short));
198 ramp->green = calloc(size, sizeof(unsigned short));
199 ramp->blue = calloc(size, sizeof(unsigned short));
200 ramp->size = size;
201 }
202
203 // Frees the red, green and blue value arrays and clears the struct
204 //
_glfwFreeGammaArrays(GLFWgammaramp * ramp)205 void _glfwFreeGammaArrays(GLFWgammaramp* ramp)
206 {
207 free(ramp->red);
208 free(ramp->green);
209 free(ramp->blue);
210
211 memset(ramp, 0, sizeof(GLFWgammaramp));
212 }
213
214 // Chooses the video mode most closely matching the desired one
215 //
_glfwChooseVideoMode(_GLFWmonitor * monitor,const GLFWvidmode * desired)216 const GLFWvidmode* _glfwChooseVideoMode(_GLFWmonitor* monitor,
217 const GLFWvidmode* desired)
218 {
219 int i;
220 unsigned int sizeDiff, leastSizeDiff = UINT_MAX;
221 unsigned int rateDiff, leastRateDiff = UINT_MAX;
222 unsigned int colorDiff, leastColorDiff = UINT_MAX;
223 const GLFWvidmode* current;
224 const GLFWvidmode* closest = NULL;
225
226 if (!refreshVideoModes(monitor))
227 return NULL;
228
229 for (i = 0; i < monitor->modeCount; i++)
230 {
231 current = monitor->modes + i;
232
233 colorDiff = 0;
234
235 if (desired->redBits != GLFW_DONT_CARE)
236 colorDiff += abs(current->redBits - desired->redBits);
237 if (desired->greenBits != GLFW_DONT_CARE)
238 colorDiff += abs(current->greenBits - desired->greenBits);
239 if (desired->blueBits != GLFW_DONT_CARE)
240 colorDiff += abs(current->blueBits - desired->blueBits);
241
242 sizeDiff = abs((current->width - desired->width) *
243 (current->width - desired->width) +
244 (current->height - desired->height) *
245 (current->height - desired->height));
246
247 if (desired->refreshRate != GLFW_DONT_CARE)
248 rateDiff = abs(current->refreshRate - desired->refreshRate);
249 else
250 rateDiff = UINT_MAX - current->refreshRate;
251
252 if ((colorDiff < leastColorDiff) ||
253 (colorDiff == leastColorDiff && sizeDiff < leastSizeDiff) ||
254 (colorDiff == leastColorDiff && sizeDiff == leastSizeDiff && rateDiff < leastRateDiff))
255 {
256 closest = current;
257 leastSizeDiff = sizeDiff;
258 leastRateDiff = rateDiff;
259 leastColorDiff = colorDiff;
260 }
261 }
262
263 return closest;
264 }
265
266 // Performs lexical comparison between two @ref GLFWvidmode structures
267 //
_glfwCompareVideoModes(const GLFWvidmode * fm,const GLFWvidmode * sm)268 int _glfwCompareVideoModes(const GLFWvidmode* fm, const GLFWvidmode* sm)
269 {
270 return compareVideoModes(fm, sm);
271 }
272
273 // Splits a color depth into red, green and blue bit depths
274 //
_glfwSplitBPP(int bpp,int * red,int * green,int * blue)275 void _glfwSplitBPP(int bpp, int* red, int* green, int* blue)
276 {
277 int delta;
278
279 // We assume that by 32 the user really meant 24
280 if (bpp == 32)
281 bpp = 24;
282
283 // Convert "bits per pixel" to red, green & blue sizes
284
285 *red = *green = *blue = bpp / 3;
286 delta = bpp - (*red * 3);
287 if (delta >= 1)
288 *green = *green + 1;
289
290 if (delta == 2)
291 *red = *red + 1;
292 }
293
294
295 //////////////////////////////////////////////////////////////////////////
296 ////// GLFW public API //////
297 //////////////////////////////////////////////////////////////////////////
298
glfwGetMonitors(int * count)299 GLFWAPI GLFWmonitor** glfwGetMonitors(int* count)
300 {
301 assert(count != NULL);
302
303 *count = 0;
304
305 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
306
307 *count = _glfw.monitorCount;
308 return (GLFWmonitor**) _glfw.monitors;
309 }
310
glfwGetPrimaryMonitor(void)311 GLFWAPI GLFWmonitor* glfwGetPrimaryMonitor(void)
312 {
313 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
314
315 if (!_glfw.monitorCount)
316 return NULL;
317
318 return (GLFWmonitor*) _glfw.monitors[0];
319 }
320
glfwGetMonitorPos(GLFWmonitor * handle,int * xpos,int * ypos)321 GLFWAPI void glfwGetMonitorPos(GLFWmonitor* handle, int* xpos, int* ypos)
322 {
323 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
324 assert(monitor != NULL);
325
326 if (xpos)
327 *xpos = 0;
328 if (ypos)
329 *ypos = 0;
330
331 _GLFW_REQUIRE_INIT();
332
333 _glfwPlatformGetMonitorPos(monitor, xpos, ypos);
334 }
335
glfwGetMonitorWorkarea(GLFWmonitor * handle,int * xpos,int * ypos,int * width,int * height)336 GLFWAPI void glfwGetMonitorWorkarea(GLFWmonitor* handle,
337 int* xpos, int* ypos,
338 int* width, int* height)
339 {
340 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
341 assert(monitor != NULL);
342
343 if (xpos)
344 *xpos = 0;
345 if (ypos)
346 *ypos = 0;
347 if (width)
348 *width = 0;
349 if (height)
350 *height = 0;
351
352 _GLFW_REQUIRE_INIT();
353
354 _glfwPlatformGetMonitorWorkarea(monitor, xpos, ypos, width, height);
355 }
356
glfwGetMonitorPhysicalSize(GLFWmonitor * handle,int * widthMM,int * heightMM)357 GLFWAPI void glfwGetMonitorPhysicalSize(GLFWmonitor* handle, int* widthMM, int* heightMM)
358 {
359 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
360 assert(monitor != NULL);
361
362 if (widthMM)
363 *widthMM = 0;
364 if (heightMM)
365 *heightMM = 0;
366
367 _GLFW_REQUIRE_INIT();
368
369 if (widthMM)
370 *widthMM = monitor->widthMM;
371 if (heightMM)
372 *heightMM = monitor->heightMM;
373 }
374
glfwGetMonitorContentScale(GLFWmonitor * handle,float * xscale,float * yscale)375 GLFWAPI void glfwGetMonitorContentScale(GLFWmonitor* handle,
376 float* xscale, float* yscale)
377 {
378 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
379 assert(monitor != NULL);
380
381 if (xscale)
382 *xscale = 0.f;
383 if (yscale)
384 *yscale = 0.f;
385
386 _GLFW_REQUIRE_INIT();
387 _glfwPlatformGetMonitorContentScale(monitor, xscale, yscale);
388 }
389
glfwGetMonitorName(GLFWmonitor * handle)390 GLFWAPI const char* glfwGetMonitorName(GLFWmonitor* handle)
391 {
392 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
393 assert(monitor != NULL);
394
395 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
396 return monitor->name;
397 }
398
glfwSetMonitorUserPointer(GLFWmonitor * handle,void * pointer)399 GLFWAPI void glfwSetMonitorUserPointer(GLFWmonitor* handle, void* pointer)
400 {
401 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
402 assert(monitor != NULL);
403
404 _GLFW_REQUIRE_INIT();
405 monitor->userPointer = pointer;
406 }
407
glfwGetMonitorUserPointer(GLFWmonitor * handle)408 GLFWAPI void* glfwGetMonitorUserPointer(GLFWmonitor* handle)
409 {
410 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
411 assert(monitor != NULL);
412
413 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
414 return monitor->userPointer;
415 }
416
glfwSetMonitorCallback(GLFWmonitorfun cbfun)417 GLFWAPI GLFWmonitorfun glfwSetMonitorCallback(GLFWmonitorfun cbfun)
418 {
419 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
420 _GLFW_SWAP_POINTERS(_glfw.callbacks.monitor, cbfun);
421 return cbfun;
422 }
423
glfwGetVideoModes(GLFWmonitor * handle,int * count)424 GLFWAPI const GLFWvidmode* glfwGetVideoModes(GLFWmonitor* handle, int* count)
425 {
426 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
427 assert(monitor != NULL);
428 assert(count != NULL);
429
430 *count = 0;
431
432 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
433
434 if (!refreshVideoModes(monitor))
435 return NULL;
436
437 *count = monitor->modeCount;
438 return monitor->modes;
439 }
440
glfwGetVideoMode(GLFWmonitor * handle)441 GLFWAPI const GLFWvidmode* glfwGetVideoMode(GLFWmonitor* handle)
442 {
443 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
444 assert(monitor != NULL);
445
446 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
447
448 _glfwPlatformGetVideoMode(monitor, &monitor->currentMode);
449 return &monitor->currentMode;
450 }
451
glfwSetGamma(GLFWmonitor * handle,float gamma)452 GLFWAPI void glfwSetGamma(GLFWmonitor* handle, float gamma)
453 {
454 unsigned int i;
455 unsigned short* values;
456 GLFWgammaramp ramp;
457 const GLFWgammaramp* original;
458 assert(handle != NULL);
459 assert(gamma > 0.f);
460 assert(gamma <= FLT_MAX);
461
462 _GLFW_REQUIRE_INIT();
463
464 if (gamma != gamma || gamma <= 0.f || gamma > FLT_MAX)
465 {
466 _glfwInputError(GLFW_INVALID_VALUE, "Invalid gamma value %f", gamma);
467 return;
468 }
469
470 original = glfwGetGammaRamp(handle);
471 if (!original)
472 return;
473
474 values = calloc(original->size, sizeof(unsigned short));
475
476 for (i = 0; i < original->size; i++)
477 {
478 float value;
479
480 // Calculate intensity
481 value = i / (float) (original->size - 1);
482 // Apply gamma curve
483 value = powf(value, 1.f / gamma) * 65535.f + 0.5f;
484 // Clamp to value range
485 value = fminf(value, 65535.f);
486
487 values[i] = (unsigned short) value;
488 }
489
490 ramp.red = values;
491 ramp.green = values;
492 ramp.blue = values;
493 ramp.size = original->size;
494
495 glfwSetGammaRamp(handle, &ramp);
496 free(values);
497 }
498
glfwGetGammaRamp(GLFWmonitor * handle)499 GLFWAPI const GLFWgammaramp* glfwGetGammaRamp(GLFWmonitor* handle)
500 {
501 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
502 assert(monitor != NULL);
503
504 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
505
506 _glfwFreeGammaArrays(&monitor->currentRamp);
507 if (!_glfwPlatformGetGammaRamp(monitor, &monitor->currentRamp))
508 return NULL;
509
510 return &monitor->currentRamp;
511 }
512
glfwSetGammaRamp(GLFWmonitor * handle,const GLFWgammaramp * ramp)513 GLFWAPI void glfwSetGammaRamp(GLFWmonitor* handle, const GLFWgammaramp* ramp)
514 {
515 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
516 assert(monitor != NULL);
517 assert(ramp != NULL);
518 assert(ramp->size > 0);
519 assert(ramp->red != NULL);
520 assert(ramp->green != NULL);
521 assert(ramp->blue != NULL);
522
523 if (ramp->size <= 0)
524 {
525 _glfwInputError(GLFW_INVALID_VALUE,
526 "Invalid gamma ramp size %i",
527 ramp->size);
528 return;
529 }
530
531 _GLFW_REQUIRE_INIT();
532
533 if (!monitor->originalRamp.size)
534 {
535 if (!_glfwPlatformGetGammaRamp(monitor, &monitor->originalRamp))
536 return;
537 }
538
539 _glfwPlatformSetGammaRamp(monitor, ramp);
540 }
541