monitors.c
Go to the documentation of this file.
1 //========================================================================
2 // Monitor information tool
3 // Copyright (c) Camilla Löwy <elmindreda@glfw.org>
4 //
5 // This software is provided 'as-is', without any express or implied
6 // warranty. In no event will the authors be held liable for any damages
7 // arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it
11 // freely, subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented; you must not
14 // claim that you wrote the original software. If you use this software
15 // in a product, an acknowledgment in the product documentation would
16 // be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such, and must not
19 // be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source
22 // distribution.
23 //
24 //========================================================================
25 //
26 // This test prints monitor and video mode information or verifies video
27 // modes
28 //
29 //========================================================================
30 
31 #include <glad/glad.h>
32 #include <GLFW/glfw3.h>
33 
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37 
38 #include "getopt.h"
39 
40 enum Mode
41 {
44 };
45 
46 static void usage(void)
47 {
48  printf("Usage: monitors [-t]\n");
49  printf(" monitors -h\n");
50 }
51 
52 static int euclid(int a, int b)
53 {
54  return b ? euclid(b, a % b) : a;
55 }
56 
57 static const char* format_mode(const GLFWvidmode* mode)
58 {
59  static char buffer[512];
60  const int gcd = euclid(mode->width, mode->height);
61 
62  snprintf(buffer,
63  sizeof(buffer),
64  "%i x %i x %i (%i:%i) (%i %i %i) %i Hz",
65  mode->width, mode->height,
66  mode->redBits + mode->greenBits + mode->blueBits,
67  mode->width / gcd, mode->height / gcd,
68  mode->redBits, mode->greenBits, mode->blueBits,
69  mode->refreshRate);
70 
71  buffer[sizeof(buffer) - 1] = '\0';
72  return buffer;
73 }
74 
75 static void error_callback(int error, const char* description)
76 {
77  fprintf(stderr, "Error: %s\n", description);
78 }
79 
81 {
82  printf("Framebuffer resized to %ix%i\n", width, height);
83 
84  glViewport(0, 0, width, height);
85 }
86 
87 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
88 {
89  if (key == GLFW_KEY_ESCAPE)
91 }
92 
93 static void list_modes(GLFWmonitor* monitor)
94 {
95  int count, x, y, width_mm, height_mm, i;
96  float xscale, yscale;
97  const GLFWvidmode* mode = glfwGetVideoMode(monitor);
98  const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count);
99 
100  glfwGetMonitorPos(monitor, &x, &y);
101  glfwGetMonitorPhysicalSize(monitor, &width_mm, &height_mm);
102  glfwGetMonitorContentScale(monitor, &xscale, &yscale);
103 
104  printf("Name: %s (%s)\n",
105  glfwGetMonitorName(monitor),
106  glfwGetPrimaryMonitor() == monitor ? "primary" : "secondary");
107  printf("Current mode: %s\n", format_mode(mode));
108  printf("Virtual position: %i %i\n", x, y);
109  printf("Content scale: %f %f\n", xscale, yscale);
110 
111  printf("Physical size: %i x %i mm (%0.2f dpi)\n",
112  width_mm, height_mm, mode->width * 25.4f / width_mm);
113 
114  printf("Modes:\n");
115 
116  for (i = 0; i < count; i++)
117  {
118  printf("%3u: %s", (unsigned int) i, format_mode(modes + i));
119 
120  if (memcmp(mode, modes + i, sizeof(GLFWvidmode)) == 0)
121  printf(" (current mode)");
122 
123  putchar('\n');
124  }
125 }
126 
127 static void test_modes(GLFWmonitor* monitor)
128 {
129  int i, count;
131  const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count);
132 
133  for (i = 0; i < count; i++)
134  {
135  const GLFWvidmode* mode = modes + i;
136  GLFWvidmode current;
137 
142 
143  printf("Testing mode %u on monitor %s: %s\n",
144  (unsigned int) i,
145  glfwGetMonitorName(monitor),
146  format_mode(mode));
147 
148  window = glfwCreateWindow(mode->width, mode->height,
149  "Video Mode Test",
151  NULL);
152  if (!window)
153  {
154  printf("Failed to enter mode %u: %s\n",
155  (unsigned int) i,
156  format_mode(mode));
157  continue;
158  }
159 
162 
163  glfwMakeContextCurrent(window);
165  glfwSwapInterval(1);
166 
167  glfwSetTime(0.0);
168 
169  while (glfwGetTime() < 5.0)
170  {
172  glfwSwapBuffers(window);
173  glfwPollEvents();
174 
175  if (glfwWindowShouldClose(window))
176  {
177  printf("User terminated program\n");
178 
179  glfwTerminate();
180  exit(EXIT_SUCCESS);
181  }
182  }
183 
184  glGetIntegerv(GL_RED_BITS, &current.redBits);
187 
188  glfwGetWindowSize(window, &current.width, &current.height);
189 
190  if (current.redBits != mode->redBits ||
191  current.greenBits != mode->greenBits ||
192  current.blueBits != mode->blueBits)
193  {
194  printf("*** Color bit mismatch: (%i %i %i) instead of (%i %i %i)\n",
195  current.redBits, current.greenBits, current.blueBits,
196  mode->redBits, mode->greenBits, mode->blueBits);
197  }
198 
199  if (current.width != mode->width || current.height != mode->height)
200  {
201  printf("*** Size mismatch: %ix%i instead of %ix%i\n",
202  current.width, current.height,
203  mode->width, mode->height);
204  }
205 
206  printf("Closing window\n");
207 
208  glfwDestroyWindow(window);
209  window = NULL;
210 
211  glfwPollEvents();
212  }
213 }
214 
215 int main(int argc, char** argv)
216 {
217  int ch, i, count, mode = LIST_MODE;
219 
220  while ((ch = getopt(argc, argv, "th")) != -1)
221  {
222  switch (ch)
223  {
224  case 'h':
225  usage();
226  exit(EXIT_SUCCESS);
227  case 't':
228  mode = TEST_MODE;
229  break;
230  default:
231  usage();
232  exit(EXIT_FAILURE);
233  }
234  }
235 
237 
238  if (!glfwInit())
239  exit(EXIT_FAILURE);
240 
241  monitors = glfwGetMonitors(&count);
242 
243  for (i = 0; i < count; i++)
244  {
245  if (mode == LIST_MODE)
246  list_modes(monitors[i]);
247  else if (mode == TEST_MODE)
248  test_modes(monitors[i]);
249  }
250 
251  glfwTerminate();
252  exit(EXIT_SUCCESS);
253 }
254 
int redBits
Definition: glfw3.h:1535
int height
Definition: glfw3.h:1532
GLboolean GLboolean GLboolean b
GLint y
GLFWAPI void glfwGetWindowSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the client area of the specified window.
Definition: window.c:544
static void error_callback(int error, const char *description)
Definition: monitors.c:75
int blueBits
Definition: glfw3.h:1541
The header of the GLFW 3 API.
GLFWAPI GLFWglproc glfwGetProcAddress(const char *procname)
Returns the address of the specified function for the current context.
Definition: context.c:741
void *(* GLADloadproc)(const char *name)
static void list_modes(GLFWmonitor *monitor)
Definition: monitors.c:93
static GLFWwindow * window
Definition: joysticks.c:55
static void usage(void)
Definition: monitors.c:46
#define glGetIntegerv
int width
Definition: glfw3.h:1529
GLFWAPI const GLFWvidmode * glfwGetVideoMode(GLFWmonitor *monitor)
Returns the current mode of the specified monitor.
Definition: monitor.c:417
struct GLFWmonitor GLFWmonitor
GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun)
Sets the error callback.
Definition: init.c:309
#define GLFW_REFRESH_RATE
Monitor refresh rate hint.
Definition: glfw3.h:903
int refreshRate
Definition: glfw3.h:1544
Mode
Definition: monitors.c:40
GLFWAPI GLFWframebuffersizefun glfwSetFramebufferSizeCallback(GLFWwindow *window, GLFWframebuffersizefun cbfun)
Sets the framebuffer resize callback for the specified window.
Definition: window.c:1050
int getopt(int argc, char *const argv[], const char *optstring)
Definition: getopt.c:52
int gcd(int a, int b)
GLenum GLfloat * buffer
GLFWAPI void glfwGetMonitorContentScale(GLFWmonitor *monitor, float *xscale, float *yscale)
Retrieves the content scale for the specified monitor.
Definition: monitor.c:351
int greenBits
Definition: glfw3.h:1538
GLboolean GLboolean GLboolean GLboolean a
GLuint64 key
Definition: glext.h:8966
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
Definition: init.c:198
#define GLFW_RED_BITS
Framebuffer bit depth hint.
Definition: glfw3.h:833
GLenum mode
GLFWAPI GLFWmonitor ** glfwGetMonitors(int *count)
Returns the currently connected monitors.
Definition: monitor.c:296
GLFWAPI const char * glfwGetMonitorName(GLFWmonitor *monitor)
Returns the name of the specified monitor.
Definition: monitor.c:366
#define GL_COLOR_BUFFER_BIT
GLdouble x
GLFWAPI void glfwSwapInterval(int interval)
Sets the swap interval for the current context.
Definition: context.c:658
#define GLFW_KEY_ESCAPE
Definition: glfw3.h:412
#define glClear
int main(int argc, char **argv)
Definition: monitors.c:215
GLint GLsizei GLsizei height
GLFWAPI void glfwSwapBuffers(GLFWwindow *window)
Swaps the front and back buffers of the specified window.
Definition: context.c:641
GLFWAPI void glfwMakeContextCurrent(GLFWwindow *window)
Makes the context of the specified window current for the calling thread.
Definition: context.c:611
#define GLFW_GREEN_BITS
Framebuffer bit depth hint.
Definition: glfw3.h:838
GLuint * monitors
Definition: glext.h:5686
action
Definition: enums.py:62
GLAPI int gladLoadGLLoader(GLADloadproc)
Definition: glad/glad.c:1697
#define glViewport
static const textual_icon exit
Definition: model-views.h:254
GLFWAPI void glfwSetWindowShouldClose(GLFWwindow *window, int value)
Sets the close flag of the specified window.
Definition: window.c:486
#define GL_GREEN_BITS
GLFWAPI void glfwSetTime(double time)
Sets the GLFW timer.
Definition: input.c:1282
GLFWAPI GLFWwindow * glfwCreateWindow(int width, int height, const char *title, GLFWmonitor *monitor, GLFWwindow *share)
Creates a window and its associated context.
Definition: window.c:151
static int euclid(int a, int b)
Definition: monitors.c:52
#define GLFW_TRUE
One.
Definition: glfw3.h:279
#define GL_BLUE_BITS
GLFWAPI void glfwGetMonitorPos(GLFWmonitor *monitor, int *xpos, int *ypos)
Returns the position of the monitor&#39;s viewport on the virtual screen.
Definition: monitor.c:318
GLFWAPI double glfwGetTime(void)
Returns the value of the GLFW timer.
Definition: input.c:1275
Video mode type.
Definition: glfw3.h:1525
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
Definition: monitors.c:87
GLFWAPI const GLFWvidmode * glfwGetVideoModes(GLFWmonitor *monitor, int *count)
Returns the available video modes for the specified monitor.
Definition: monitor.c:400
GLFWAPI GLFWmonitor * glfwGetPrimaryMonitor(void)
Returns the primary monitor.
Definition: monitor.c:308
GLFWAPI void glfwDestroyWindow(GLFWwindow *window)
Destroys the specified window and its context.
Definition: window.c:444
GLint GLsizei count
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
Definition: init.c:243
static void framebuffer_size_callback(GLFWwindow *window, int width, int height)
Definition: monitors.c:80
GLFWAPI void glfwGetMonitorPhysicalSize(GLFWmonitor *monitor, int *widthMM, int *heightMM)
Returns the physical size of the monitor.
Definition: monitor.c:333
GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow *window, GLFWkeyfun cbfun)
Sets the key callback.
Definition: input.c:791
GLFWAPI void glfwPollEvents(void)
Processes all pending events.
Definition: window.c:1072
#define GLFW_BLUE_BITS
Framebuffer bit depth hint.
Definition: glfw3.h:843
#define NULL
Definition: tinycthread.c:47
int i
static void test_modes(GLFWmonitor *monitor)
Definition: monitors.c:127
static const char * format_mode(const GLFWvidmode *mode)
Definition: monitors.c:57
#define GL_RED_BITS
struct GLFWwindow GLFWwindow
GLFWAPI void glfwWindowHint(int hint, int value)
Sets the specified window hint to the desired value.
Definition: window.c:291
GLint GLsizei width
GLFWAPI int glfwWindowShouldClose(GLFWwindow *window)
Checks the close flag of the specified window.
Definition: window.c:477


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