Wise&mystical  1.0
Project about Europe
Loading...
Searching...
No Matches
monitor.c
Go to the documentation of this file.
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//
42static 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//
69static GLFWbool refreshVideoModes(_GLFWmonitor* monitor)
70{
71 int modeCount;
72 GLFWvidmode* modes;
73
74 if (monitor->modes)
75 return GLFW_TRUE;
76
77 modes = _glfwPlatformGetVideoModes(monitor, &modeCount);
78 if (!modes)
79 return GLFW_FALSE;
80
81 qsort(modes, modeCount, sizeof(GLFWvidmode), compareVideoModes);
82
83 free(monitor->modes);
84 monitor->modes = modes;
85 monitor->modeCount = modeCount;
86
87 return GLFW_TRUE;
88}
89
90
94
95// Notifies shared code of a monitor connection or disconnection
96//
97void _glfwInputMonitor(_GLFWmonitor* monitor, int action, int placement)
98{
99 if (action == GLFW_CONNECTED)
100 {
103 realloc(_glfw.monitors, sizeof(_GLFWmonitor*) * _glfw.monitorCount);
104
105 if (placement == _GLFW_INSERT_FIRST)
106 {
107 memmove(_glfw.monitors + 1,
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 {
137 memmove(_glfw.monitors + i,
138 _glfw.monitors + i + 1,
139 ((size_t) _glfw.monitorCount - i) * sizeof(_GLFWmonitor*));
140 break;
141 }
142 }
143 }
144
146 _glfw.callbacks.monitor((GLFWmonitor*) monitor, action);
147
148 if (action == GLFW_DISCONNECTED)
149 _glfwFreeMonitor(monitor);
150}
151
152// Notifies shared code that a full screen window has acquired or released
153// a monitor
154//
156{
157 monitor->window = window;
158}
159
160
164
165// Allocates and returns a monitor object with the specified name and dimensions
166//
167_GLFWmonitor* _glfwAllocMonitor(const char* name, int widthMM, int heightMM)
168{
169 _GLFWmonitor* monitor = calloc(1, sizeof(_GLFWmonitor));
170 monitor->widthMM = widthMM;
171 monitor->heightMM = heightMM;
172
173 strncpy(monitor->name, name, sizeof(monitor->name) - 1);
174
175 return monitor;
176}
177
178// Frees a monitor object and any data associated with it
179//
181{
182 if (monitor == NULL)
183 return;
184
186
189
190 free(monitor->modes);
191 free(monitor);
192}
193
194// Allocates red, green and blue value arrays of the specified size
195//
196void _glfwAllocGammaArrays(GLFWgammaramp* ramp, unsigned int size)
197{
198 ramp->red = calloc(size, sizeof(unsigned short));
199 ramp->green = calloc(size, sizeof(unsigned short));
200 ramp->blue = calloc(size, sizeof(unsigned short));
201 ramp->size = size;
202}
203
204// Frees the red, green and blue value arrays and clears the struct
205//
207{
208 free(ramp->red);
209 free(ramp->green);
210 free(ramp->blue);
211
212 memset(ramp, 0, sizeof(GLFWgammaramp));
213}
214
215// Chooses the video mode most closely matching the desired one
216//
218 const GLFWvidmode* desired)
219{
220 int i;
221 unsigned int sizeDiff, leastSizeDiff = UINT_MAX;
222 unsigned int rateDiff, leastRateDiff = UINT_MAX;
223 unsigned int colorDiff, leastColorDiff = UINT_MAX;
224 const GLFWvidmode* current;
225 const GLFWvidmode* closest = NULL;
226
227 if (!refreshVideoModes(monitor))
228 return NULL;
229
230 for (i = 0; i < monitor->modeCount; i++)
231 {
232 current = monitor->modes + i;
233
234 colorDiff = 0;
235
236 if (desired->redBits != GLFW_DONT_CARE)
237 colorDiff += abs(current->redBits - desired->redBits);
238 if (desired->greenBits != GLFW_DONT_CARE)
239 colorDiff += abs(current->greenBits - desired->greenBits);
240 if (desired->blueBits != GLFW_DONT_CARE)
241 colorDiff += abs(current->blueBits - desired->blueBits);
242
243 sizeDiff = abs((current->width - desired->width) *
244 (current->width - desired->width) +
245 (current->height - desired->height) *
246 (current->height - desired->height));
247
248 if (desired->refreshRate != GLFW_DONT_CARE)
249 rateDiff = abs(current->refreshRate - desired->refreshRate);
250 else
251 rateDiff = UINT_MAX - current->refreshRate;
252
253 if ((colorDiff < leastColorDiff) ||
254 (colorDiff == leastColorDiff && sizeDiff < leastSizeDiff) ||
255 (colorDiff == leastColorDiff && sizeDiff == leastSizeDiff && rateDiff < leastRateDiff))
256 {
257 closest = current;
258 leastSizeDiff = sizeDiff;
259 leastRateDiff = rateDiff;
260 leastColorDiff = colorDiff;
261 }
262 }
263
264 return closest;
265}
266
267// Performs lexical comparison between two @ref GLFWvidmode structures
268//
270{
271 return compareVideoModes(fm, sm);
272}
273
274// Splits a color depth into red, green and blue bit depths
275//
276void _glfwSplitBPP(int bpp, int* red, int* green, int* blue)
277{
278 int delta;
279
280 // We assume that by 32 the user really meant 24
281 if (bpp == 32)
282 bpp = 24;
283
284 // Convert "bits per pixel" to red, green & blue sizes
285
286 *red = *green = *blue = bpp / 3;
287 delta = bpp - (*red * 3);
288 if (delta >= 1)
289 *green = *green + 1;
290
291 if (delta == 2)
292 *red = *red + 1;
293}
294
295
299
301{
302 assert(count != NULL);
303
304 *count = 0;
305
307
308 *count = _glfw.monitorCount;
309 return (GLFWmonitor**) _glfw.monitors;
310}
311
313{
315
316 if (!_glfw.monitorCount)
317 return NULL;
318
319 return (GLFWmonitor*) _glfw.monitors[0];
320}
321
322GLFWAPI void glfwGetMonitorPos(GLFWmonitor* handle, int* xpos, int* ypos)
323{
324 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
325 assert(monitor != NULL);
326
327 if (xpos)
328 *xpos = 0;
329 if (ypos)
330 *ypos = 0;
331
333
334 _glfwPlatformGetMonitorPos(monitor, xpos, ypos);
335}
336
338 int* xpos, int* ypos,
339 int* width, int* height)
340{
341 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
342 assert(monitor != NULL);
343
344 if (xpos)
345 *xpos = 0;
346 if (ypos)
347 *ypos = 0;
348 if (width)
349 *width = 0;
350 if (height)
351 *height = 0;
352
354
355 _glfwPlatformGetMonitorWorkarea(monitor, xpos, ypos, width, height);
356}
357
358GLFWAPI void glfwGetMonitorPhysicalSize(GLFWmonitor* handle, int* widthMM, int* heightMM)
359{
360 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
361 assert(monitor != NULL);
362
363 if (widthMM)
364 *widthMM = 0;
365 if (heightMM)
366 *heightMM = 0;
367
369
370 if (widthMM)
371 *widthMM = monitor->widthMM;
372 if (heightMM)
373 *heightMM = monitor->heightMM;
374}
375
377 float* xscale, float* yscale)
378{
379 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
380 assert(monitor != NULL);
381
382 if (xscale)
383 *xscale = 0.f;
384 if (yscale)
385 *yscale = 0.f;
386
388 _glfwPlatformGetMonitorContentScale(monitor, xscale, yscale);
389}
390
392{
393 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
394 assert(monitor != NULL);
395
397 return monitor->name;
398}
399
401{
402 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
403 assert(monitor != NULL);
404
406 monitor->userPointer = pointer;
407}
408
410{
411 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
412 assert(monitor != NULL);
413
415 return monitor->userPointer;
416}
417
419{
422 return cbfun;
423}
424
426{
427 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
428 assert(monitor != NULL);
429 assert(count != NULL);
430
431 *count = 0;
432
434
435 if (!refreshVideoModes(monitor))
436 return NULL;
437
438 *count = monitor->modeCount;
439 return monitor->modes;
440}
441
443{
444 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
445 assert(monitor != NULL);
446
448
449 _glfwPlatformGetVideoMode(monitor, &monitor->currentMode);
450 return &monitor->currentMode;
451}
452
453GLFWAPI void glfwSetGamma(GLFWmonitor* handle, float gamma)
454{
455 unsigned int i;
456 unsigned short* values;
457 GLFWgammaramp ramp;
458 const GLFWgammaramp* original;
459 assert(handle != NULL);
460 assert(gamma > 0.f);
461 assert(gamma <= FLT_MAX);
462
464
465 if (gamma != gamma || gamma <= 0.f || gamma > FLT_MAX)
466 {
467 _glfwInputError(GLFW_INVALID_VALUE, "Invalid gamma value %f", gamma);
468 return;
469 }
470
471 original = glfwGetGammaRamp(handle);
472 if (!original)
473 return;
474
475 values = calloc(original->size, sizeof(unsigned short));
476
477 for (i = 0; i < original->size; i++)
478 {
479 float value;
480
481 // Calculate intensity
482 value = i / (float) (original->size - 1);
483 // Apply gamma curve
484 value = powf(value, 1.f / gamma) * 65535.f + 0.5f;
485 // Clamp to value range
486 value = _glfw_fminf(value, 65535.f);
487
488 values[i] = (unsigned short) value;
489 }
490
491 ramp.red = values;
492 ramp.green = values;
493 ramp.blue = values;
494 ramp.size = original->size;
495
496 glfwSetGammaRamp(handle, &ramp);
497 free(values);
498}
499
501{
502 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
503 assert(monitor != NULL);
504
506
508 if (!_glfwPlatformGetGammaRamp(monitor, &monitor->currentRamp))
509 return NULL;
510
511 return &monitor->currentRamp;
512}
513
515{
516 _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
517 assert(monitor != NULL);
518 assert(ramp != NULL);
519 assert(ramp->size > 0);
520 assert(ramp->red != NULL);
521 assert(ramp->green != NULL);
522 assert(ramp->blue != NULL);
523
524 if (ramp->size <= 0)
525 {
527 "Invalid gamma ramp size %i",
528 ramp->size);
529 return;
530 }
531
533
534 if (!monitor->originalRamp.size)
535 {
536 if (!_glfwPlatformGetGammaRamp(monitor, &monitor->originalRamp))
537 return;
538 }
539
540 _glfwPlatformSetGammaRamp(monitor, ramp);
541}
542
void _glfwPlatformFreeMonitor(_GLFWmonitor *monitor)
GLFWvidmode * _glfwPlatformGetVideoModes(_GLFWmonitor *monitor, int *count)
void _glfwPlatformGetVideoMode(_GLFWmonitor *monitor, GLFWvidmode *mode)
void _glfwPlatformSetGammaRamp(_GLFWmonitor *monitor, const GLFWgammaramp *ramp)
void _glfwPlatformGetMonitorPos(_GLFWmonitor *monitor, int *xpos, int *ypos)
GLFWbool _glfwPlatformGetGammaRamp(_GLFWmonitor *monitor, GLFWgammaramp *ramp)
void _glfwPlatformGetMonitorContentScale(_GLFWmonitor *monitor, float *xscale, float *yscale)
void _glfwPlatformGetMonitorWorkarea(_GLFWmonitor *monitor, int *xpos, int *ypos, int *width, int *height)
void _glfwPlatformSetWindowMonitor(_GLFWwindow *window, _GLFWmonitor *monitor, int xpos, int ypos, int width, int height, int refreshRate)
void _glfwPlatformGetWindowSize(_GLFWwindow *window, int *width, int *height)
void _glfwPlatformSetWindowPos(_GLFWwindow *window, int x, int y)
void _glfwPlatformGetWindowFrameSize(_GLFWwindow *window, int *left, int *top, int *right, int *bottom)
#define GLFWAPI
Definition: glfw3.h:269
#define GLFW_DONT_CARE
Definition: glfw3.h:1262
#define GLFW_DISCONNECTED
Definition: glfw3.h:1231
#define GLFW_CONNECTED
Definition: glfw3.h:1230
#define GLFW_INVALID_VALUE
One of the arguments to the function was an invalid value.
Definition: glfw3.h:706
#define GLFW_TRUE
One.
Definition: glfw3.h:310
#define GLFW_FALSE
Zero.
Definition: glfw3.h:319
GLFWAPI void * glfwGetMonitorUserPointer(GLFWmonitor *handle)
Returns the user pointer of the specified monitor.
Definition: monitor.c:409
GLFWAPI const GLFWvidmode * glfwGetVideoMode(GLFWmonitor *handle)
Returns the current mode of the specified monitor.
Definition: monitor.c:442
GLFWAPI void glfwGetMonitorContentScale(GLFWmonitor *handle, float *xscale, float *yscale)
Retrieves the content scale for the specified monitor.
Definition: monitor.c:376
GLFWAPI GLFWmonitor * glfwGetPrimaryMonitor(void)
Returns the primary monitor.
Definition: monitor.c:312
GLFWAPI void glfwSetGamma(GLFWmonitor *handle, float gamma)
Generates a gamma ramp and sets it for the specified monitor.
Definition: monitor.c:453
GLFWAPI void glfwGetMonitorPos(GLFWmonitor *handle, int *xpos, int *ypos)
Returns the position of the monitor's viewport on the virtual screen.
Definition: monitor.c:322
GLFWAPI void glfwGetMonitorWorkarea(GLFWmonitor *handle, int *xpos, int *ypos, int *width, int *height)
Retrieves the work area of the monitor.
Definition: monitor.c:337
GLFWAPI const char * glfwGetMonitorName(GLFWmonitor *handle)
Returns the name of the specified monitor.
Definition: monitor.c:391
GLFWAPI void glfwSetMonitorUserPointer(GLFWmonitor *handle, void *pointer)
Sets the user pointer of the specified monitor.
Definition: monitor.c:400
GLFWAPI const GLFWvidmode * glfwGetVideoModes(GLFWmonitor *handle, int *count)
Returns the available video modes for the specified monitor.
Definition: monitor.c:425
GLFWAPI GLFWmonitor ** glfwGetMonitors(int *count)
Returns the currently connected monitors.
Definition: monitor.c:300
struct GLFWmonitor GLFWmonitor
Opaque monitor object.
Definition: glfw3.h:1307
GLFWAPI GLFWmonitorfun glfwSetMonitorCallback(GLFWmonitorfun cbfun)
Sets the monitor configuration callback.
Definition: monitor.c:418
GLFWAPI void glfwSetGammaRamp(GLFWmonitor *handle, const GLFWgammaramp *ramp)
Sets the current gamma ramp for the specified monitor.
Definition: monitor.c:514
void(* GLFWmonitorfun)(GLFWmonitor *, int)
The function pointer type for monitor configuration callbacks.
Definition: glfw3.h:1755
GLFWAPI void glfwGetMonitorPhysicalSize(GLFWmonitor *handle, int *widthMM, int *heightMM)
Returns the physical size of the monitor.
Definition: monitor.c:358
GLFWAPI const GLFWgammaramp * glfwGetGammaRamp(GLFWmonitor *handle)
Returns the current gamma ramp for the specified monitor.
Definition: monitor.c:500
_GLFWlibrary _glfw
Definition: init.c:46
void _glfwInputError(int code, const char *format,...)
Definition: init.c:160
float _glfw_fminf(float a, float b)
Definition: init.c:129
#define _GLFW_REQUIRE_INIT_OR_RETURN(x)
Definition: internal.h:214
#define _GLFW_SWAP_POINTERS(x, y)
Definition: internal.h:222
#define _GLFW_INSERT_FIRST
Definition: internal.h:51
int GLFWbool
Definition: internal.h:61
#define _GLFW_REQUIRE_INIT()
Definition: internal.h:208
#define NULL
Definition: miniaudio.h:3718
void _glfwInputMonitorWindow(_GLFWmonitor *monitor, _GLFWwindow *window)
Definition: monitor.c:155
void _glfwInputMonitor(_GLFWmonitor *monitor, int action, int placement)
Definition: monitor.c:97
void _glfwSplitBPP(int bpp, int *red, int *green, int *blue)
Definition: monitor.c:276
const GLFWvidmode * _glfwChooseVideoMode(_GLFWmonitor *monitor, const GLFWvidmode *desired)
Definition: monitor.c:217
void _glfwFreeMonitor(_GLFWmonitor *monitor)
Definition: monitor.c:180
void _glfwFreeGammaArrays(GLFWgammaramp *ramp)
Definition: monitor.c:206
int _glfwCompareVideoModes(const GLFWvidmode *fm, const GLFWvidmode *sm)
Definition: monitor.c:269
_GLFWmonitor * _glfwAllocMonitor(const char *name, int widthMM, int heightMM)
Definition: monitor.c:167
void _glfwAllocGammaArrays(GLFWgammaramp *ramp, unsigned int size)
Definition: monitor.c:196
struct _GLFWlibrary::@26 callbacks
_GLFWwindow * windowListHead
Definition: internal.h:541
_GLFWmonitor ** monitors
Definition: internal.h:543
GLFWmonitorfun monitor
Definition: internal.h:584
int monitorCount
Definition: internal.h:544
GLFWvidmode * modes
Definition: internal.h:448
int modeCount
Definition: internal.h:449
GLFWgammaramp originalRamp
Definition: internal.h:452
GLFWgammaramp currentRamp
Definition: internal.h:453
void * userPointer
Definition: internal.h:440
_GLFWwindow * window
Definition: internal.h:446
GLFWvidmode currentMode
Definition: internal.h:450
char name[128]
Definition: internal.h:439
int heightMM
Definition: internal.h:443
_GLFWmonitor * monitor
Definition: internal.h:392
struct _GLFWwindow * next
Definition: internal.h:379
Gamma ramp.
Definition: glfw3.h:1826
unsigned short * red
Definition: glfw3.h:1829
unsigned short * blue
Definition: glfw3.h:1835
unsigned int size
Definition: glfw3.h:1838
unsigned short * green
Definition: glfw3.h:1832
Video mode type.
Definition: glfw3.h:1792
int greenBits
Definition: glfw3.h:1804
int redBits
Definition: glfw3.h:1801
int width
Definition: glfw3.h:1795
int refreshRate
Definition: glfw3.h:1810
int height
Definition: glfw3.h:1798
int blueBits
Definition: glfw3.h:1807