1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
//========================================================================
// Monitor information tool
// Copyright (c) Camilla Löwy <elmindreda@glfw.org>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
//
// This test prints monitor and video mode information or verifies video
// modes
//
//========================================================================
#include <glad/gl.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "getopt.h"
enum Mode
{
LIST_MODE,
TEST_MODE
};
static void usage(void)
{
printf("Usage: monitors [-t]\n");
printf(" monitors -h\n");
}
static int euclid(int a, int b)
{
return b ? euclid(b, a % b) : a;
}
static const char* format_mode(const GLFWvidmode* mode)
{
static char buffer[512];
const int gcd = euclid(mode->width, mode->height);
snprintf(buffer,
sizeof(buffer),
"%i x %i x %i (%i:%i) (%i %i %i) %i Hz",
mode->width, mode->height,
mode->redBits + mode->greenBits + mode->blueBits,
mode->width / gcd, mode->height / gcd,
mode->redBits, mode->greenBits, mode->blueBits,
mode->refreshRate);
buffer[sizeof(buffer) - 1] = '\0';
return buffer;
}
static void error_callback(int error, const char* description)
{
fprintf(stderr, "Error: %s\n", description);
}
static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
printf("Framebuffer resized to %ix%i\n", width, height);
glViewport(0, 0, width, height);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE)
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
static void list_modes(GLFWmonitor* monitor)
{
int count, x, y, width_mm, height_mm, i;
int workarea_x, workarea_y, workarea_width, workarea_height;
float xscale, yscale;
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count);
glfwGetMonitorPos(monitor, &x, &y);
glfwGetMonitorPhysicalSize(monitor, &width_mm, &height_mm);
glfwGetMonitorContentScale(monitor, &xscale, &yscale);
glfwGetMonitorWorkarea(monitor, &workarea_x, &workarea_y, &workarea_width, &workarea_height);
printf("Name: %s (%s)\n",
glfwGetMonitorName(monitor),
glfwGetPrimaryMonitor() == monitor ? "primary" : "secondary");
printf("Current mode: %s\n", format_mode(mode));
printf("Virtual position: %i, %i\n", x, y);
printf("Content scale: %f x %f\n", xscale, yscale);
printf("Physical size: %i x %i mm (%0.2f dpi at %i x %i)\n",
width_mm, height_mm, mode->width * 25.4f / width_mm, mode->width, mode->height);
printf("Monitor work area: %i x %i starting at %i, %i\n",
workarea_width, workarea_height, workarea_x, workarea_y);
printf("Modes:\n");
for (i = 0; i < count; i++)
{
printf("%3u: %s", (unsigned int) i, format_mode(modes + i));
if (memcmp(mode, modes + i, sizeof(GLFWvidmode)) == 0)
printf(" (current mode)");
putchar('\n');
}
}
static void test_modes(GLFWmonitor* monitor)
{
int i, count;
GLFWwindow* window;
const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count);
for (i = 0; i < count; i++)
{
const GLFWvidmode* mode = modes + i;
GLFWvidmode current;
glfwWindowHint(GLFW_RED_BITS, mode->redBits);
glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
printf("Testing mode %u on monitor %s: %s\n",
(unsigned int) i,
glfwGetMonitorName(monitor),
format_mode(mode));
window = glfwCreateWindow(mode->width, mode->height,
"Video Mode Test",
glfwGetPrimaryMonitor(),
NULL);
if (!window)
{
printf("Failed to enter mode %u: %s\n",
(unsigned int) i,
format_mode(mode));
continue;
}
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetKeyCallback(window, key_callback);
glfwMakeContextCurrent(window);
gladLoadGL(glfwGetProcAddress);
glfwSwapInterval(1);
glfwSetTime(0.0);
while (glfwGetTime() < 5.0)
{
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
if (glfwWindowShouldClose(window))
{
printf("User terminated program\n");
glfwTerminate();
exit(EXIT_SUCCESS);
}
}
glGetIntegerv(GL_RED_BITS, ¤t.redBits);
glGetIntegerv(GL_GREEN_BITS, ¤t.greenBits);
glGetIntegerv(GL_BLUE_BITS, ¤t.blueBits);
glfwGetWindowSize(window, ¤t.width, ¤t.height);
if (current.redBits != mode->redBits ||
current.greenBits != mode->greenBits ||
current.blueBits != mode->blueBits)
{
printf("*** Color bit mismatch: (%i %i %i) instead of (%i %i %i)\n",
current.redBits, current.greenBits, current.blueBits,
mode->redBits, mode->greenBits, mode->blueBits);
}
if (current.width != mode->width || current.height != mode->height)
{
printf("*** Size mismatch: %ix%i instead of %ix%i\n",
current.width, current.height,
mode->width, mode->height);
}
printf("Closing window\n");
glfwDestroyWindow(window);
window = NULL;
glfwPollEvents();
}
}
int main(int argc, char** argv)
{
int ch, i, count, mode = LIST_MODE;
GLFWmonitor** monitors;
while ((ch = getopt(argc, argv, "th")) != -1)
{
switch (ch)
{
case 'h':
usage();
exit(EXIT_SUCCESS);
case 't':
mode = TEST_MODE;
break;
default:
usage();
exit(EXIT_FAILURE);
}
}
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
monitors = glfwGetMonitors(&count);
for (i = 0; i < count; i++)
{
if (mode == LIST_MODE)
list_modes(monitors[i]);
else if (mode == TEST_MODE)
test_modes(monitors[i]);
}
glfwTerminate();
exit(EXIT_SUCCESS);
}
|