x11_monitor.c
Go to the documentation of this file.
1 //========================================================================
2 // GLFW 3.3 X11 - www.glfw.org
3 //------------------------------------------------------------------------
4 // Copyright (c) 2002-2006 Marcus Geelnard
5 // Copyright (c) 2006-2016 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 
28 #include "internal.h"
29 
30 #include <limits.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 
35 // Check whether the display mode should be included in enumeration
36 //
37 static GLFWbool modeIsGood(const XRRModeInfo* mi)
38 {
39  return (mi->modeFlags & RR_Interlace) == 0;
40 }
41 
42 // Calculates the refresh rate, in Hz, from the specified RandR mode info
43 //
44 static int calculateRefreshRate(const XRRModeInfo* mi)
45 {
46  if (mi->hTotal && mi->vTotal)
47  return (int) ((double) mi->dotClock / ((double) mi->hTotal * (double) mi->vTotal));
48  else
49  return 0;
50 }
51 
52 // Returns the mode info for a RandR mode XID
53 //
54 static const XRRModeInfo* getModeInfo(const XRRScreenResources* sr, RRMode id)
55 {
56  int i;
57 
58  for (i = 0; i < sr->nmode; i++)
59  {
60  if (sr->modes[i].id == id)
61  return sr->modes + i;
62  }
63 
64  return NULL;
65 }
66 
67 // Convert RandR mode info to GLFW video mode
68 //
69 static GLFWvidmode vidmodeFromModeInfo(const XRRModeInfo* mi,
70  const XRRCrtcInfo* ci)
71 {
73 
74  if (ci->rotation == RR_Rotate_90 || ci->rotation == RR_Rotate_270)
75  {
76  mode.width = mi->height;
77  mode.height = mi->width;
78  }
79  else
80  {
81  mode.width = mi->width;
82  mode.height = mi->height;
83  }
84 
86 
87  _glfwSplitBPP(DefaultDepth(_glfw.x11.display, _glfw.x11.screen),
88  &mode.redBits, &mode.greenBits, &mode.blueBits);
89 
90  return mode;
91 }
92 
93 
97 
98 // Poll for changes in the set of connected monitors
99 //
101 {
102  if (_glfw.x11.randr.available && !_glfw.x11.randr.monitorBroken)
103  {
104  int i, j, disconnectedCount, screenCount = 0;
105  _GLFWmonitor** disconnected = NULL;
106  XineramaScreenInfo* screens = NULL;
107  XRRScreenResources* sr = XRRGetScreenResourcesCurrent(_glfw.x11.display,
108  _glfw.x11.root);
109  RROutput primary = XRRGetOutputPrimary(_glfw.x11.display,
110  _glfw.x11.root);
111 
112  if (_glfw.x11.xinerama.available)
113  screens = XineramaQueryScreens(_glfw.x11.display, &screenCount);
114 
115  disconnectedCount = _glfw.monitorCount;
116  if (disconnectedCount)
117  {
118  disconnected = calloc(_glfw.monitorCount, sizeof(_GLFWmonitor*));
119  memcpy(disconnected,
120  _glfw.monitors,
121  _glfw.monitorCount * sizeof(_GLFWmonitor*));
122  }
123 
124  for (i = 0; i < sr->noutput; i++)
125  {
126  int type, widthMM, heightMM;
127  XRROutputInfo* oi;
128  XRRCrtcInfo* ci;
129  _GLFWmonitor* monitor;
130 
131  oi = XRRGetOutputInfo(_glfw.x11.display, sr, sr->outputs[i]);
132  if (oi->connection != RR_Connected || oi->crtc == None)
133  {
134  XRRFreeOutputInfo(oi);
135  continue;
136  }
137 
138  for (j = 0; j < disconnectedCount; j++)
139  {
140  if (disconnected[j] &&
141  disconnected[j]->x11.output == sr->outputs[i])
142  {
143  disconnected[j] = NULL;
144  break;
145  }
146  }
147 
148  if (j < disconnectedCount)
149  {
150  XRRFreeOutputInfo(oi);
151  continue;
152  }
153 
154  ci = XRRGetCrtcInfo(_glfw.x11.display, sr, oi->crtc);
155  if (ci->rotation == RR_Rotate_90 || ci->rotation == RR_Rotate_270)
156  {
157  widthMM = oi->mm_height;
158  heightMM = oi->mm_width;
159  }
160  else
161  {
162  widthMM = oi->mm_width;
163  heightMM = oi->mm_height;
164  }
165 
166  monitor = _glfwAllocMonitor(oi->name, widthMM, heightMM);
167  monitor->x11.output = sr->outputs[i];
168  monitor->x11.crtc = oi->crtc;
169 
170  for (j = 0; j < screenCount; j++)
171  {
172  if (screens[j].x_org == ci->x &&
173  screens[j].y_org == ci->y &&
174  screens[j].width == ci->width &&
175  screens[j].height == ci->height)
176  {
177  monitor->x11.index = j;
178  break;
179  }
180  }
181 
182  if (monitor->x11.output == primary)
183  type = _GLFW_INSERT_FIRST;
184  else
185  type = _GLFW_INSERT_LAST;
186 
187  _glfwInputMonitor(monitor, GLFW_CONNECTED, type);
188 
189  XRRFreeOutputInfo(oi);
190  XRRFreeCrtcInfo(ci);
191  }
192 
194 
195  if (screens)
196  XFree(screens);
197 
198  for (i = 0; i < disconnectedCount; i++)
199  {
200  if (disconnected[i])
201  _glfwInputMonitor(disconnected[i], GLFW_DISCONNECTED, 0);
202  }
203 
204  free(disconnected);
205  }
206  else
207  {
208  const int widthMM = DisplayWidthMM(_glfw.x11.display, _glfw.x11.screen);
209  const int heightMM = DisplayHeightMM(_glfw.x11.display, _glfw.x11.screen);
210 
211  _glfwInputMonitor(_glfwAllocMonitor("Display", widthMM, heightMM),
214  }
215 }
216 
217 // Set the current video mode for the specified monitor
218 //
219 void _glfwSetVideoModeX11(_GLFWmonitor* monitor, const GLFWvidmode* desired)
220 {
221  if (_glfw.x11.randr.available && !_glfw.x11.randr.monitorBroken)
222  {
223  XRRScreenResources* sr;
224  XRRCrtcInfo* ci;
225  XRROutputInfo* oi;
226  GLFWvidmode current;
227  const GLFWvidmode* best;
228  RRMode native = None;
229  int i;
230 
231  best = _glfwChooseVideoMode(monitor, desired);
232  _glfwPlatformGetVideoMode(monitor, &current);
233  if (_glfwCompareVideoModes(&current, best) == 0)
234  return;
235 
236  sr = XRRGetScreenResourcesCurrent(_glfw.x11.display, _glfw.x11.root);
237  ci = XRRGetCrtcInfo(_glfw.x11.display, sr, monitor->x11.crtc);
238  oi = XRRGetOutputInfo(_glfw.x11.display, sr, monitor->x11.output);
239 
240  for (i = 0; i < oi->nmode; i++)
241  {
242  const XRRModeInfo* mi = getModeInfo(sr, oi->modes[i]);
243  if (!modeIsGood(mi))
244  continue;
245 
246  const GLFWvidmode mode = vidmodeFromModeInfo(mi, ci);
247  if (_glfwCompareVideoModes(best, &mode) == 0)
248  {
249  native = mi->id;
250  break;
251  }
252  }
253 
254  if (native)
255  {
256  if (monitor->x11.oldMode == None)
257  monitor->x11.oldMode = ci->mode;
258 
259  XRRSetCrtcConfig(_glfw.x11.display,
260  sr, monitor->x11.crtc,
261  CurrentTime,
262  ci->x, ci->y,
263  native,
264  ci->rotation,
265  ci->outputs,
266  ci->noutput);
267  }
268 
269  XRRFreeOutputInfo(oi);
270  XRRFreeCrtcInfo(ci);
272  }
273 }
274 
275 // Restore the saved (original) video mode for the specified monitor
276 //
278 {
279  if (_glfw.x11.randr.available && !_glfw.x11.randr.monitorBroken)
280  {
281  XRRScreenResources* sr;
282  XRRCrtcInfo* ci;
283 
284  if (monitor->x11.oldMode == None)
285  return;
286 
287  sr = XRRGetScreenResourcesCurrent(_glfw.x11.display, _glfw.x11.root);
288  ci = XRRGetCrtcInfo(_glfw.x11.display, sr, monitor->x11.crtc);
289 
290  XRRSetCrtcConfig(_glfw.x11.display,
291  sr, monitor->x11.crtc,
292  CurrentTime,
293  ci->x, ci->y,
294  monitor->x11.oldMode,
295  ci->rotation,
296  ci->outputs,
297  ci->noutput);
298 
299  XRRFreeCrtcInfo(ci);
301 
302  monitor->x11.oldMode = None;
303  }
304 }
305 
306 
310 
312 {
313 }
314 
316 {
317  if (_glfw.x11.randr.available && !_glfw.x11.randr.monitorBroken)
318  {
319  XRRScreenResources* sr;
320  XRRCrtcInfo* ci;
321 
322  sr = XRRGetScreenResourcesCurrent(_glfw.x11.display, _glfw.x11.root);
323  ci = XRRGetCrtcInfo(_glfw.x11.display, sr, monitor->x11.crtc);
324 
325  if (xpos)
326  *xpos = ci->x;
327  if (ypos)
328  *ypos = ci->y;
329 
330  XRRFreeCrtcInfo(ci);
332  }
333 }
334 
336  float* xscale, float* yscale)
337 {
338  if (xscale)
339  *xscale = _glfw.x11.contentScaleX;
340  if (yscale)
341  *yscale = _glfw.x11.contentScaleY;
342 }
343 
345 {
347 
348  *count = 0;
349 
350  if (_glfw.x11.randr.available && !_glfw.x11.randr.monitorBroken)
351  {
352  int i, j;
353  XRRScreenResources* sr;
354  XRRCrtcInfo* ci;
355  XRROutputInfo* oi;
356 
357  sr = XRRGetScreenResourcesCurrent(_glfw.x11.display, _glfw.x11.root);
358  ci = XRRGetCrtcInfo(_glfw.x11.display, sr, monitor->x11.crtc);
359  oi = XRRGetOutputInfo(_glfw.x11.display, sr, monitor->x11.output);
360 
361  result = calloc(oi->nmode, sizeof(GLFWvidmode));
362 
363  for (i = 0; i < oi->nmode; i++)
364  {
365  const XRRModeInfo* mi = getModeInfo(sr, oi->modes[i]);
366  if (!modeIsGood(mi))
367  continue;
368 
369  const GLFWvidmode mode = vidmodeFromModeInfo(mi, ci);
370 
371  for (j = 0; j < *count; j++)
372  {
373  if (_glfwCompareVideoModes(result + j, &mode) == 0)
374  break;
375  }
376 
377  // Skip duplicate modes
378  if (j < *count)
379  continue;
380 
381  (*count)++;
382  result[*count - 1] = mode;
383  }
384 
385  XRRFreeOutputInfo(oi);
386  XRRFreeCrtcInfo(ci);
388  }
389  else
390  {
391  *count = 1;
392  result = calloc(1, sizeof(GLFWvidmode));
393  _glfwPlatformGetVideoMode(monitor, result);
394  }
395 
396  return result;
397 }
398 
400 {
401  if (_glfw.x11.randr.available && !_glfw.x11.randr.monitorBroken)
402  {
403  XRRScreenResources* sr;
404  XRRCrtcInfo* ci;
405 
406  sr = XRRGetScreenResourcesCurrent(_glfw.x11.display, _glfw.x11.root);
407  ci = XRRGetCrtcInfo(_glfw.x11.display, sr, monitor->x11.crtc);
408 
409  *mode = vidmodeFromModeInfo(getModeInfo(sr, ci->mode), ci);
410 
411  XRRFreeCrtcInfo(ci);
413  }
414  else
415  {
416  mode->width = DisplayWidth(_glfw.x11.display, _glfw.x11.screen);
417  mode->height = DisplayHeight(_glfw.x11.display, _glfw.x11.screen);
418  mode->refreshRate = 0;
419 
420  _glfwSplitBPP(DefaultDepth(_glfw.x11.display, _glfw.x11.screen),
421  &mode->redBits, &mode->greenBits, &mode->blueBits);
422  }
423 }
424 
426 {
427  if (_glfw.x11.randr.available && !_glfw.x11.randr.gammaBroken)
428  {
429  const size_t size = XRRGetCrtcGammaSize(_glfw.x11.display,
430  monitor->x11.crtc);
431  XRRCrtcGamma* gamma = XRRGetCrtcGamma(_glfw.x11.display,
432  monitor->x11.crtc);
433 
434  _glfwAllocGammaArrays(ramp, size);
435 
436  memcpy(ramp->red, gamma->red, size * sizeof(unsigned short));
437  memcpy(ramp->green, gamma->green, size * sizeof(unsigned short));
438  memcpy(ramp->blue, gamma->blue, size * sizeof(unsigned short));
439 
440  XRRFreeGamma(gamma);
441  }
442  else if (_glfw.x11.vidmode.available)
443  {
444  int size;
445  XF86VidModeGetGammaRampSize(_glfw.x11.display, _glfw.x11.screen, &size);
446 
447  _glfwAllocGammaArrays(ramp, size);
448 
449  XF86VidModeGetGammaRamp(_glfw.x11.display,
450  _glfw.x11.screen,
451  ramp->size, ramp->red, ramp->green, ramp->blue);
452  }
453 }
454 
456 {
457  if (_glfw.x11.randr.available && !_glfw.x11.randr.gammaBroken)
458  {
459  if (XRRGetCrtcGammaSize(_glfw.x11.display, monitor->x11.crtc) != ramp->size)
460  {
462  "X11: Gamma ramp size must match current ramp size");
463  return;
464  }
465 
466  XRRCrtcGamma* gamma = XRRAllocGamma(ramp->size);
467 
468  memcpy(gamma->red, ramp->red, ramp->size * sizeof(unsigned short));
469  memcpy(gamma->green, ramp->green, ramp->size * sizeof(unsigned short));
470  memcpy(gamma->blue, ramp->blue, ramp->size * sizeof(unsigned short));
471 
472  XRRSetCrtcGamma(_glfw.x11.display, monitor->x11.crtc, gamma);
473  XRRFreeGamma(gamma);
474  }
475  else if (_glfw.x11.vidmode.available)
476  {
477  XF86VidModeSetGammaRamp(_glfw.x11.display,
478  _glfw.x11.screen,
479  ramp->size,
480  (unsigned short*) ramp->red,
481  (unsigned short*) ramp->green,
482  (unsigned short*) ramp->blue);
483  }
484 }
485 
486 
490 
492 {
493  _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
495  return monitor->x11.crtc;
496 }
497 
499 {
500  _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
502  return monitor->x11.output;
503 }
504 
int redBits
Definition: glfw3.h:1535
#define XRRGetCrtcGammaSize
Definition: x11_platform.h:73
int height
Definition: glfw3.h:1532
int blueBits
Definition: glfw3.h:1541
#define GLFWAPI
Definition: glfw3.h:240
void _glfwPlatformSetGammaRamp(_GLFWmonitor *monitor, const GLFWgammaramp *ramp)
Definition: x11_monitor.c:455
void _glfwAllocGammaArrays(GLFWgammaramp *ramp, unsigned int size)
Definition: monitor.c:192
#define XRRSetCrtcConfig
Definition: x11_platform.h:81
int width
Definition: glfw3.h:1529
GLuint64 GLenum void * handle
Definition: glext.h:7785
void _glfwSetVideoModeX11(_GLFWmonitor *monitor, const GLFWvidmode *desired)
Definition: x11_monitor.c:219
void _glfwSplitBPP(int bpp, int *red, int *green, int *blue)
Definition: monitor.c:272
static GLFWbool modeIsGood(const XRRModeInfo *mi)
Definition: x11_monitor.c:37
struct GLFWmonitor GLFWmonitor
_GLFWmonitor * _glfwAllocMonitor(const char *name, int widthMM, int heightMM)
Definition: monitor.c:161
void _glfwPlatformGetMonitorPos(_GLFWmonitor *monitor, int *xpos, int *ypos)
Definition: x11_monitor.c:315
int refreshRate
Definition: glfw3.h:1544
unsigned short * red
Definition: glfw3.h:1563
int GLFWbool
Definition: internal.h:61
unsigned short * green
Definition: glfw3.h:1566
int greenBits
Definition: glfw3.h:1538
#define XRRGetScreenResourcesCurrent
Definition: x11_platform.h:77
#define GLFW_DISCONNECTED
Definition: glfw3.h:1059
GLFWAPI RRCrtc glfwGetX11Adapter(GLFWmonitor *handle)
Definition: x11_monitor.c:491
#define GLFW_CONNECTED
Definition: glfw3.h:1058
#define XF86VidModeSetGammaRamp
Definition: x11_platform.h:111
#define XF86VidModeGetGammaRampSize
Definition: x11_platform.h:112
#define _GLFW_REQUIRE_INIT_OR_RETURN(x)
Definition: internal.h:210
GLenum mode
static const XRRModeInfo * getModeInfo(const XRRScreenResources *sr, RRMode id)
Definition: x11_monitor.c:54
GLsizeiptr size
#define GLFW_PLATFORM_ERROR
A platform-specific error occurred that does not match any of the more specific categories.
Definition: glfw3.h:726
_GLFWlibrary _glfw
Definition: init.c:44
int monitorCount
Definition: internal.h:529
#define XRRSetCrtcGamma
Definition: x11_platform.h:82
GLFWbool available
Definition: internal.h:546
#define XRRGetOutputInfo
Definition: x11_platform.h:75
#define XRRGetOutputPrimary
Definition: x11_platform.h:76
unsigned int size
Definition: glfw3.h:1572
GLFWAPI RROutput glfwGetX11Monitor(GLFWmonitor *handle)
Definition: x11_monitor.c:498
GLint j
#define XF86VidModeGetGammaRamp
Definition: x11_platform.h:110
#define XRRAllocGamma
Definition: x11_platform.h:67
#define XRRGetCrtcGamma
Definition: x11_platform.h:72
void _glfwPlatformGetMonitorContentScale(_GLFWmonitor *monitor, float *xscale, float *yscale)
Definition: x11_monitor.c:335
void _glfwInputError(int code, const char *format,...)
Definition: init.c:129
void _glfwInputMonitor(_GLFWmonitor *monitor, int action, int placement)
Definition: monitor.c:91
#define XineramaQueryScreens
Definition: x11_platform.h:97
void _glfwPollMonitorsX11(void)
Definition: x11_monitor.c:100
void _glfwPlatformFreeMonitor(_GLFWmonitor *monitor)
Definition: x11_monitor.c:311
int _glfwCompareVideoModes(const GLFWvidmode *first, const GLFWvidmode *second)
Definition: monitor.c:265
static GLFWvidmode vidmodeFromModeInfo(const XRRModeInfo *mi, const XRRCrtcInfo *ci)
Definition: x11_monitor.c:69
#define _GLFW_INSERT_FIRST
Definition: internal.h:51
Gamma ramp.
Definition: glfw3.h:1559
void _glfwRestoreVideoModeX11(_GLFWmonitor *monitor)
Definition: x11_monitor.c:277
unsigned short * blue
Definition: glfw3.h:1569
Video mode type.
Definition: glfw3.h:1525
void _glfwPlatformGetVideoMode(_GLFWmonitor *monitor, GLFWvidmode *mode)
Definition: x11_monitor.c:399
GLenum type
static int calculateRefreshRate(const XRRModeInfo *mi)
Definition: x11_monitor.c:44
static double xpos
Definition: splitview.c:33
void _glfwPlatformGetGammaRamp(_GLFWmonitor *monitor, GLFWgammaramp *ramp)
Definition: x11_monitor.c:425
GLint GLsizei count
#define XRRFreeGamma
Definition: x11_platform.h:69
#define NULL
Definition: tinycthread.c:47
int i
_GLFWmonitor ** monitors
Definition: internal.h:528
#define XRRFreeOutputInfo
Definition: x11_platform.h:70
#define XRRFreeScreenResources
Definition: x11_platform.h:71
GLFWvidmode * _glfwPlatformGetVideoModes(_GLFWmonitor *monitor, int *count)
Definition: x11_monitor.c:344
const GLFWvidmode * _glfwChooseVideoMode(_GLFWmonitor *monitor, const GLFWvidmode *desired)
Definition: monitor.c:213
GLuint64EXT * result
Definition: glext.h:10921
#define XRRFreeCrtcInfo
Definition: x11_platform.h:68
#define XRRGetCrtcInfo
Definition: x11_platform.h:74
static double ypos
Definition: splitview.c:33
#define _GLFW_INSERT_LAST
Definition: internal.h:52


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:23