• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //========================================================================
2 // Monitor information tool
3 // Copyright (c) Camilla Berglund <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 {
42     LIST_MODE,
43     TEST_MODE
44 };
45 
usage(void)46 static void usage(void)
47 {
48     printf("Usage: monitors [-t]\n");
49     printf("       monitors -h\n");
50 }
51 
format_mode(const GLFWvidmode * mode)52 static const char* format_mode(const GLFWvidmode* mode)
53 {
54     static char buffer[512];
55 
56     sprintf(buffer,
57             "%i x %i x %i (%i %i %i) %i Hz",
58             mode->width, mode->height,
59             mode->redBits + mode->greenBits + mode->blueBits,
60             mode->redBits, mode->greenBits, mode->blueBits,
61             mode->refreshRate);
62 
63     buffer[sizeof(buffer) - 1] = '\0';
64     return buffer;
65 }
66 
error_callback(int error,const char * description)67 static void error_callback(int error, const char* description)
68 {
69     fprintf(stderr, "Error: %s\n", description);
70 }
71 
framebuffer_size_callback(GLFWwindow * window,int width,int height)72 static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
73 {
74     printf("Framebuffer resized to %ix%i\n", width, height);
75 
76     glViewport(0, 0, width, height);
77 }
78 
key_callback(GLFWwindow * window,int key,int scancode,int action,int mods)79 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
80 {
81     if (key == GLFW_KEY_ESCAPE)
82         glfwSetWindowShouldClose(window, GLFW_TRUE);
83 }
84 
list_modes(GLFWmonitor * monitor)85 static void list_modes(GLFWmonitor* monitor)
86 {
87     int count, x, y, widthMM, heightMM, i;
88     const GLFWvidmode* mode = glfwGetVideoMode(monitor);
89     const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count);
90 
91     glfwGetMonitorPos(monitor, &x, &y);
92     glfwGetMonitorPhysicalSize(monitor, &widthMM, &heightMM);
93 
94     printf("Name: %s (%s)\n",
95            glfwGetMonitorName(monitor),
96            glfwGetPrimaryMonitor() == monitor ? "primary" : "secondary");
97     printf("Current mode: %s\n", format_mode(mode));
98     printf("Virtual position: %i %i\n", x, y);
99 
100     printf("Physical size: %i x %i mm (%0.2f dpi)\n",
101            widthMM, heightMM, mode->width * 25.4f / widthMM);
102 
103     printf("Modes:\n");
104 
105     for (i = 0;  i < count;  i++)
106     {
107         printf("%3u: %s", (unsigned int) i, format_mode(modes + i));
108 
109         if (memcmp(mode, modes + i, sizeof(GLFWvidmode)) == 0)
110             printf(" (current mode)");
111 
112         putchar('\n');
113     }
114 }
115 
test_modes(GLFWmonitor * monitor)116 static void test_modes(GLFWmonitor* monitor)
117 {
118     int i, count;
119     GLFWwindow* window;
120     const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count);
121 
122     for (i = 0;  i < count;  i++)
123     {
124         const GLFWvidmode* mode = modes + i;
125         GLFWvidmode current;
126 
127         glfwWindowHint(GLFW_RED_BITS, mode->redBits);
128         glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
129         glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
130         glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
131 
132         printf("Testing mode %u on monitor %s: %s\n",
133                (unsigned int) i,
134                glfwGetMonitorName(monitor),
135                format_mode(mode));
136 
137         window = glfwCreateWindow(mode->width, mode->height,
138                                   "Video Mode Test",
139                                   glfwGetPrimaryMonitor(),
140                                   NULL);
141         if (!window)
142         {
143             printf("Failed to enter mode %u: %s\n",
144                    (unsigned int) i,
145                    format_mode(mode));
146             continue;
147         }
148 
149         glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
150         glfwSetKeyCallback(window, key_callback);
151 
152         glfwMakeContextCurrent(window);
153         gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
154         glfwSwapInterval(1);
155 
156         glfwSetTime(0.0);
157 
158         while (glfwGetTime() < 5.0)
159         {
160             glClear(GL_COLOR_BUFFER_BIT);
161             glfwSwapBuffers(window);
162             glfwPollEvents();
163 
164             if (glfwWindowShouldClose(window))
165             {
166                 printf("User terminated program\n");
167 
168                 glfwTerminate();
169                 exit(EXIT_SUCCESS);
170             }
171         }
172 
173         glGetIntegerv(GL_RED_BITS, &current.redBits);
174         glGetIntegerv(GL_GREEN_BITS, &current.greenBits);
175         glGetIntegerv(GL_BLUE_BITS, &current.blueBits);
176 
177         glfwGetWindowSize(window, &current.width, &current.height);
178 
179         if (current.redBits != mode->redBits ||
180             current.greenBits != mode->greenBits ||
181             current.blueBits != mode->blueBits)
182         {
183             printf("*** Color bit mismatch: (%i %i %i) instead of (%i %i %i)\n",
184                    current.redBits, current.greenBits, current.blueBits,
185                    mode->redBits, mode->greenBits, mode->blueBits);
186         }
187 
188         if (current.width != mode->width || current.height != mode->height)
189         {
190             printf("*** Size mismatch: %ix%i instead of %ix%i\n",
191                    current.width, current.height,
192                    mode->width, mode->height);
193         }
194 
195         printf("Closing window\n");
196 
197         glfwDestroyWindow(window);
198         window = NULL;
199 
200         glfwPollEvents();
201     }
202 }
203 
main(int argc,char ** argv)204 int main(int argc, char** argv)
205 {
206     int ch, i, count, mode = LIST_MODE;
207     GLFWmonitor** monitors;
208 
209     while ((ch = getopt(argc, argv, "th")) != -1)
210     {
211         switch (ch)
212         {
213             case 'h':
214                 usage();
215                 exit(EXIT_SUCCESS);
216             case 't':
217                 mode = TEST_MODE;
218                 break;
219             default:
220                 usage();
221                 exit(EXIT_FAILURE);
222         }
223     }
224 
225     glfwSetErrorCallback(error_callback);
226 
227     if (!glfwInit())
228         exit(EXIT_FAILURE);
229 
230     monitors = glfwGetMonitors(&count);
231 
232     for (i = 0;  i < count;  i++)
233     {
234         if (mode == LIST_MODE)
235             list_modes(monitors[i]);
236         else if (mode == TEST_MODE)
237             test_modes(monitors[i]);
238     }
239 
240     glfwTerminate();
241     exit(EXIT_SUCCESS);
242 }
243 
244