monitor.c
Go to the documentation of this file.
1 //========================================================================
2 // GLFW 3.1 - www.glfw.org
3 //------------------------------------------------------------------------
4 // Copyright (c) 2002-2006 Marcus Geelnard
5 // Copyright (c) 2006-2010 Camilla Berglund <elmindreda@elmindreda.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 
28 #include "internal.h"
29 
30 #include <math.h>
31 #include <float.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <limits.h>
35 
36 
37 // Lexical comparison function for GLFW video modes, used by qsort
38 //
39 static int compareVideoModes(const void* firstPtr, const void* secondPtr)
40 {
41  int firstBPP, secondBPP, firstSize, secondSize;
42  const GLFWvidmode* first = firstPtr;
43  const GLFWvidmode* second = secondPtr;
44 
45  // First sort on color bits per pixel
46  firstBPP = first->redBits + first->greenBits + first->blueBits;
47  secondBPP = second->redBits + second->greenBits + second->blueBits;
48  if (firstBPP != secondBPP)
49  return firstBPP - secondBPP;
50 
51  // Then sort on screen area, in pixels
52  firstSize = first->width * first->height;
53  secondSize = second->width * second->height;
54  if (firstSize != secondSize)
55  return firstSize - secondSize;
56 
57  // Lastly sort on refresh rate
58  return first->refreshRate - second->refreshRate;
59 }
60 
61 // Retrieves the available modes for the specified monitor
62 //
63 static int refreshVideoModes(_GLFWmonitor* monitor)
64 {
65  int modeCount;
66  GLFWvidmode* modes;
67 
68  if (monitor->modes)
69  return GL_TRUE;
70 
71  modes = _glfwPlatformGetVideoModes(monitor, &modeCount);
72  if (!modes)
73  return GL_FALSE;
74 
75  qsort(modes, modeCount, sizeof(GLFWvidmode), compareVideoModes);
76 
77  free(monitor->modes);
78  monitor->modes = modes;
79  monitor->modeCount = modeCount;
80 
81  return GL_TRUE;
82 }
83 
84 
88 
90 {
91  int i, j, monitorCount = _glfw.monitorCount;
93 
95 
96  // Re-use still connected monitor objects
97 
98  for (i = 0; i < _glfw.monitorCount; i++)
99  {
100  for (j = 0; j < monitorCount; j++)
101  {
102  if (_glfwPlatformIsSameMonitor(_glfw.monitors[i], monitors[j]))
103  {
105  _glfw.monitors[i] = monitors[j];
106  break;
107  }
108  }
109  }
110 
111  // Find and report disconnected monitors (not in the new list)
112 
113  for (i = 0; i < monitorCount; i++)
114  {
115  _GLFWwindow* window;
116 
117  for (j = 0; j < _glfw.monitorCount; j++)
118  {
119  if (monitors[i] == _glfw.monitors[j])
120  break;
121  }
122 
123  if (j < _glfw.monitorCount)
124  continue;
125 
126  for (window = _glfw.windowListHead; window; window = window->next)
127  {
128  if (window->monitor == monitors[i])
129  window->monitor = NULL;
130  }
131 
132  if (_glfw.callbacks.monitor)
134  }
135 
136  // Find and report newly connected monitors (not in the old list)
137  // Re-used monitor objects are then removed from the old list to avoid
138  // having them destroyed at the end of this function
139 
140  for (i = 0; i < _glfw.monitorCount; i++)
141  {
142  for (j = 0; j < monitorCount; j++)
143  {
144  if (_glfw.monitors[i] == monitors[j])
145  {
146  monitors[j] = NULL;
147  break;
148  }
149  }
150 
151  if (j < monitorCount)
152  continue;
153 
154  if (_glfw.callbacks.monitor)
156  }
157 
158  _glfwFreeMonitors(monitors, monitorCount);
159 }
160 
161 
165 
166 _GLFWmonitor* _glfwAllocMonitor(const char* name, int widthMM, int heightMM)
167 {
168  _GLFWmonitor* monitor = calloc(1, sizeof(_GLFWmonitor));
169  monitor->name = strdup(name);
170  monitor->widthMM = widthMM;
171  monitor->heightMM = heightMM;
172 
173  return monitor;
174 }
175 
177 {
178  if (monitor == NULL)
179  return;
180 
183 
184  free(monitor->modes);
185  free(monitor->name);
186  free(monitor);
187 }
188 
189 void _glfwAllocGammaArrays(GLFWgammaramp* ramp, unsigned int size)
190 {
191  ramp->red = calloc(size, sizeof(unsigned short));
192  ramp->green = calloc(size, sizeof(unsigned short));
193  ramp->blue = calloc(size, sizeof(unsigned short));
194  ramp->size = size;
195 }
196 
198 {
199  free(ramp->red);
200  free(ramp->green);
201  free(ramp->blue);
202 
203  memset(ramp, 0, sizeof(GLFWgammaramp));
204 }
205 
207 {
208  int i;
209 
210  for (i = 0; i < count; i++)
211  _glfwFreeMonitor(monitors[i]);
212 
213  free(monitors);
214 }
215 
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 
267 {
268  return compareVideoModes(first, second);
269 }
270 
271 void _glfwSplitBPP(int bpp, int* red, int* green, int* blue)
272 {
273  int delta;
274 
275  // We assume that by 32 the user really meant 24
276  if (bpp == 32)
277  bpp = 24;
278 
279  // Convert "bits per pixel" to red, green & blue sizes
280 
281  *red = *green = *blue = bpp / 3;
282  delta = bpp - (*red * 3);
283  if (delta >= 1)
284  *green = *green + 1;
285 
286  if (delta == 2)
287  *red = *red + 1;
288 }
289 
290 
294 
296 {
297  *count = 0;
298 
300 
301  *count = _glfw.monitorCount;
302  return (GLFWmonitor**) _glfw.monitors;
303 }
304 
306 {
308 
309  if (!_glfw.monitorCount)
310  return NULL;
311 
312  return (GLFWmonitor*) _glfw.monitors[0];
313 }
314 
315 GLFWAPI void glfwGetMonitorPos(GLFWmonitor* handle, int* xpos, int* ypos)
316 {
317  _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
318 
319  if (xpos)
320  *xpos = 0;
321  if (ypos)
322  *ypos = 0;
323 
325 
326  _glfwPlatformGetMonitorPos(monitor, xpos, ypos);
327 }
328 
329 GLFWAPI void glfwGetMonitorPhysicalSize(GLFWmonitor* handle, int* widthMM, int* heightMM)
330 {
331  _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
332 
333  if (widthMM)
334  *widthMM = 0;
335  if (heightMM)
336  *heightMM = 0;
337 
339 
340  if (widthMM)
341  *widthMM = monitor->widthMM;
342  if (heightMM)
343  *heightMM = monitor->heightMM;
344 }
345 
347 {
348  _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
350  return monitor->name;
351 }
352 
354 {
357  return cbfun;
358 }
359 
361 {
362  _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
363 
364  *count = 0;
365 
367 
368  if (!refreshVideoModes(monitor))
369  return NULL;
370 
371  *count = monitor->modeCount;
372  return monitor->modes;
373 }
374 
376 {
377  _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
378 
380 
381  _glfwPlatformGetVideoMode(monitor, &monitor->currentMode);
382  return &monitor->currentMode;
383 }
384 
385 GLFWAPI void glfwSetGamma(GLFWmonitor* handle, float gamma)
386 {
387  int i;
388  unsigned short values[256];
389  GLFWgammaramp ramp;
390 
392 
393  if (gamma != gamma || gamma <= 0.f || gamma > FLT_MAX)
394  {
395  _glfwInputError(GLFW_INVALID_VALUE, "Invalid gamma value");
396  return;
397  }
398 
399  for (i = 0; i < 256; i++)
400  {
401  double value;
402 
403  // Calculate intensity
404  value = i / 255.0;
405  // Apply gamma curve
406  value = pow(value, 1.0 / gamma) * 65535.0 + 0.5;
407 
408  // Clamp to value range
409  if (value > 65535.0)
410  value = 65535.0;
411 
412  values[i] = (unsigned short) value;
413  }
414 
415  ramp.red = values;
416  ramp.green = values;
417  ramp.blue = values;
418  ramp.size = 256;
419 
420  glfwSetGammaRamp(handle, &ramp);
421 }
422 
424 {
425  _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
426 
428 
430  _glfwPlatformGetGammaRamp(monitor, &monitor->currentRamp);
431 
432  return &monitor->currentRamp;
433 }
434 
436 {
437  _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
438 
440 
441  if (!monitor->originalRamp.size)
442  _glfwPlatformGetGammaRamp(monitor, &monitor->originalRamp);
443 
444  _glfwPlatformSetGammaRamp(monitor, ramp);
445 }
446 
static int refreshVideoModes(_GLFWmonitor *monitor)
Definition: monitor.c:63
int redBits
Definition: glfw3.h:993
void _glfwFreeGammaArrays(GLFWgammaramp *ramp)
Definition: monitor.c:197
int height
Definition: glfw3.h:990
const GLint * first
Definition: glext.h:368
static int compareVideoModes(const void *firstPtr, const void *secondPtr)
Definition: monitor.c:39
GLFWgammaramp currentRamp
Definition: internal.h:311
void _glfwFreeMonitor(_GLFWmonitor *monitor)
Frees a monitor object and any data associated with it.
Definition: monitor.c:176
void _glfwPlatformGetVideoMode(_GLFWmonitor *monitor, GLFWvidmode *mode)
int blueBits
Definition: glfw3.h:999
GLFWAPI GLFWmonitorfun glfwSetMonitorCallback(GLFWmonitorfun cbfun)
Sets the monitor configuration callback.
Definition: monitor.c:353
GLFWAPI void glfwSetGamma(GLFWmonitor *handle, float gamma)
Generates a gamma ramp and sets it for the specified monitor.
Definition: monitor.c:385
void(* GLFWmonitorfun)(GLFWmonitor *, int)
The function signature for monitor configuration callbacks.
Definition: glfw3.h:975
#define GLFWAPI
Definition: glfw3.h:185
GLboolean _glfwPlatformIsSameMonitor(_GLFWmonitor *first, _GLFWmonitor *second)
Checks whether two monitor objects represent the same monitor.
#define _GLFW_REQUIRE_INIT()
Definition: internal.h:131
struct _GLFWlibrary::@3 callbacks
_GLFWmonitor ** _glfwPlatformGetMonitors(int *count)
Returns the currently connected monitors.
int width
Definition: glfw3.h:987
GLFWAPI const GLFWvidmode * glfwGetVideoMode(GLFWmonitor *handle)
Returns the current mode of the specified monitor.
Definition: monitor.c:375
GLFWvidmode * modes
Definition: internal.h:306
int heightMM
Definition: internal.h:304
void _glfwPlatformSetGammaRamp(_GLFWmonitor *monitor, const GLFWgammaramp *ramp)
Sets the current gamma ramp for the specified monitor.
int refreshRate
Definition: glfw3.h:1002
unsigned short * red
Definition: glfw3.h:1017
GLbyte GLbyte blue
Definition: glext.h:379
_GLFWwindow * windowListHead
Definition: internal.h:343
_GLFWlibrary _glfw
All global data protected by _glfwInitialized. This should only be touched after a call to glfwInit t...
unsigned short * green
Definition: glfw3.h:1020
int modeCount
Definition: internal.h:307
struct GLFWmonitor GLFWmonitor
Opaque monitor object.
Definition: glfw3.h:714
#define GLFW_DONT_CARE
Definition: glfw3.h:692
int greenBits
Definition: glfw3.h:996
Monitor structure.
Definition: internal.h:299
char * name
Definition: internal.h:301
#define GLFW_DISCONNECTED
Definition: glfw3.h:690
#define _GLFW_SWAP_POINTERS(x, y)
Definition: internal.h:145
#define GLFW_CONNECTED
Definition: glfw3.h:689
#define _GLFW_REQUIRE_INIT_OR_RETURN(x)
Definition: internal.h:137
GLFWAPI GLFWmonitor ** glfwGetMonitors(int *count)
Returns the currently connected monitors.
Definition: monitor.c:295
GLFWAPI const char * glfwGetMonitorName(GLFWmonitor *handle)
Returns the name of the specified monitor.
Definition: monitor.c:346
void _glfwPlatformGetGammaRamp(_GLFWmonitor *monitor, GLFWgammaramp *ramp)
Returns the current gamma ramp for the specified monitor.
GLuint GLuint GLsizei count
Definition: glext.h:111
int monitorCount
Definition: internal.h:347
struct _GLFWwindow * next
Definition: internal.h:234
void _glfwFreeMonitors(_GLFWmonitor **monitors, int count)
Definition: monitor.c:206
unsigned int size
Definition: glfw3.h:1026
GLFWAPI const GLFWgammaramp * glfwGetGammaRamp(GLFWmonitor *handle)
Returns the current gamma ramp for the specified monitor.
Definition: monitor.c:423
void _glfwSplitBPP(int bpp, int *red, int *green, int *blue)
Splits a color depth into red, green and blue bit depths.
Definition: monitor.c:271
GLFWvidmode currentMode
Definition: internal.h:308
GLFWvidmode * _glfwPlatformGetVideoModes(_GLFWmonitor *monitor, int *count)
Returns the available video modes for the specified monitor.
_GLFWmonitor * _glfwAllocMonitor(const char *name, int widthMM, int heightMM)
Allocates and returns a monitor object with the specified name and dimensions.
Definition: monitor.c:166
void _glfwPlatformGetMonitorPos(_GLFWmonitor *monitor, int *xpos, int *ypos)
Returns the position of the monitor&#39;s viewport on the virtual screen.
GLuint * monitors
Definition: glext.h:5363
GLsizei const GLfloat * value
Definition: glext.h:693
void _glfwAllocGammaArrays(GLFWgammaramp *ramp, unsigned int size)
Definition: monitor.c:189
GLFWgammaramp originalRamp
Definition: internal.h:310
GLbyte green
Definition: glext.h:379
GLFWAPI void glfwGetMonitorPos(GLFWmonitor *handle, int *xpos, int *ypos)
Returns the position of the monitor&#39;s viewport on the virtual screen.
Definition: monitor.c:315
#define GLFW_INVALID_VALUE
One of the arguments to the function was an invalid value.
Definition: glfw3.h:510
Gamma ramp.
Definition: glfw3.h:1013
GLenum GLsizei GLsizei GLint * values
Definition: glext.h:1484
unsigned short * blue
Definition: glfw3.h:1023
Video mode type.
Definition: glfw3.h:983
void _glfwInputMonitorChange(void)
Definition: monitor.c:89
GLuint const GLchar * name
Definition: glext.h:655
GLFWmonitorfun monitor
Definition: internal.h:350
GLFWAPI const GLFWvidmode * glfwGetVideoModes(GLFWmonitor *handle, int *count)
Returns the available video modes for the specified monitor.
Definition: monitor.c:360
GLFWAPI GLFWmonitor * glfwGetPrimaryMonitor(void)
Returns the primary monitor.
Definition: monitor.c:305
GLsizeiptr size
Definition: glext.h:532
int _glfwCompareVideoModes(const GLFWvidmode *first, const GLFWvidmode *second)
Performs lexical comparison between two GLFWvidmode structures.
Definition: monitor.c:266
GLFWAPI void glfwGetMonitorPhysicalSize(GLFWmonitor *handle, int *widthMM, int *heightMM)
Returns the physical size of the monitor.
Definition: monitor.c:329
void _glfwInputError(int error, const char *format,...)
Notifies shared code of an error.
const GLFWvidmode * _glfwChooseVideoMode(_GLFWmonitor *monitor, const GLFWvidmode *desired)
Definition: monitor.c:216
_GLFWmonitor ** monitors
Definition: internal.h:346
Window and context structure.
Definition: internal.h:232
_GLFWmonitor * monitor
Definition: internal.h:244
GLFWAPI void glfwSetGammaRamp(GLFWmonitor *handle, const GLFWgammaramp *ramp)
Sets the current gamma ramp for the specified monitor.
Definition: monitor.c:435


librealsense
Author(s): Sergey Dorodnicov , Mark Horn , Reagan Lopez
autogenerated on Fri Mar 13 2020 03:16:17