monitors.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #include <glad/gl.h>
  31. #define GLFW_INCLUDE_NONE
  32. #include <GLFW/glfw3.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <stdlib.h>
  36. #include "getopt.h"
  37. enum Mode
  38. {
  39. LIST_MODE,
  40. TEST_MODE
  41. };
  42. static void usage(void)
  43. {
  44. printf("Usage: monitors [-t]\n");
  45. printf(" monitors -h\n");
  46. }
  47. static int euclid(int a, int b)
  48. {
  49. return b ? euclid(b, a % b) : a;
  50. }
  51. static const char* format_mode(const GLFWvidmode* mode)
  52. {
  53. static char buffer[512];
  54. const int gcd = euclid(mode->width, mode->height);
  55. snprintf(buffer,
  56. sizeof(buffer),
  57. "%i x %i x %i (%i:%i) (%i %i %i) %i Hz",
  58. mode->width, mode->height,
  59. mode->redBits + mode->greenBits + mode->blueBits,
  60. mode->width / gcd, mode->height / gcd,
  61. mode->redBits, mode->greenBits, mode->blueBits,
  62. mode->refreshRate);
  63. buffer[sizeof(buffer) - 1] = '\0';
  64. return buffer;
  65. }
  66. static void error_callback(int error, const char* description)
  67. {
  68. fprintf(stderr, "Error: %s\n", description);
  69. }
  70. static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  71. {
  72. printf("Framebuffer resized to %ix%i\n", width, height);
  73. glViewport(0, 0, width, height);
  74. }
  75. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  76. {
  77. if (key == GLFW_KEY_ESCAPE)
  78. glfwSetWindowShouldClose(window, GLFW_TRUE);
  79. }
  80. static void list_modes(GLFWmonitor* monitor)
  81. {
  82. int count, x, y, width_mm, height_mm, i;
  83. int workarea_x, workarea_y, workarea_width, workarea_height;
  84. float xscale, yscale;
  85. const GLFWvidmode* mode = glfwGetVideoMode(monitor);
  86. const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count);
  87. glfwGetMonitorPos(monitor, &x, &y);
  88. glfwGetMonitorPhysicalSize(monitor, &width_mm, &height_mm);
  89. glfwGetMonitorContentScale(monitor, &xscale, &yscale);
  90. glfwGetMonitorWorkarea(monitor, &workarea_x, &workarea_y, &workarea_width, &workarea_height);
  91. printf("Name: %s (%s)\n",
  92. glfwGetMonitorName(monitor),
  93. glfwGetPrimaryMonitor() == monitor ? "primary" : "secondary");
  94. printf("Current mode: %s\n", format_mode(mode));
  95. printf("Virtual position: %i, %i\n", x, y);
  96. printf("Content scale: %f x %f\n", xscale, yscale);
  97. printf("Physical size: %i x %i mm (%0.2f dpi at %i x %i)\n",
  98. width_mm, height_mm, mode->width * 25.4f / width_mm, mode->width, mode->height);
  99. printf("Monitor work area: %i x %i starting at %i, %i\n",
  100. workarea_width, workarea_height, workarea_x, workarea_y);
  101. printf("Modes:\n");
  102. for (i = 0; i < count; i++)
  103. {
  104. printf("%3u: %s", (unsigned int) i, format_mode(modes + i));
  105. if (memcmp(mode, modes + i, sizeof(GLFWvidmode)) == 0)
  106. printf(" (current mode)");
  107. putchar('\n');
  108. }
  109. }
  110. static void test_modes(GLFWmonitor* monitor)
  111. {
  112. int i, count;
  113. GLFWwindow* window;
  114. const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count);
  115. for (i = 0; i < count; i++)
  116. {
  117. const GLFWvidmode* mode = modes + i;
  118. GLFWvidmode current;
  119. glfwWindowHint(GLFW_RED_BITS, mode->redBits);
  120. glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
  121. glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
  122. glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
  123. printf("Testing mode %u on monitor %s: %s\n",
  124. (unsigned int) i,
  125. glfwGetMonitorName(monitor),
  126. format_mode(mode));
  127. window = glfwCreateWindow(mode->width, mode->height,
  128. "Video Mode Test",
  129. glfwGetPrimaryMonitor(),
  130. NULL);
  131. if (!window)
  132. {
  133. printf("Failed to enter mode %u: %s\n",
  134. (unsigned int) i,
  135. format_mode(mode));
  136. continue;
  137. }
  138. glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  139. glfwSetKeyCallback(window, key_callback);
  140. glfwMakeContextCurrent(window);
  141. gladLoadGL(glfwGetProcAddress);
  142. glfwSwapInterval(1);
  143. glfwSetTime(0.0);
  144. while (glfwGetTime() < 5.0)
  145. {
  146. glClear(GL_COLOR_BUFFER_BIT);
  147. glfwSwapBuffers(window);
  148. glfwPollEvents();
  149. if (glfwWindowShouldClose(window))
  150. {
  151. printf("User terminated program\n");
  152. glfwTerminate();
  153. exit(EXIT_SUCCESS);
  154. }
  155. }
  156. glGetIntegerv(GL_RED_BITS, &current.redBits);
  157. glGetIntegerv(GL_GREEN_BITS, &current.greenBits);
  158. glGetIntegerv(GL_BLUE_BITS, &current.blueBits);
  159. glfwGetWindowSize(window, &current.width, &current.height);
  160. if (current.redBits != mode->redBits ||
  161. current.greenBits != mode->greenBits ||
  162. current.blueBits != mode->blueBits)
  163. {
  164. printf("*** Color bit mismatch: (%i %i %i) instead of (%i %i %i)\n",
  165. current.redBits, current.greenBits, current.blueBits,
  166. mode->redBits, mode->greenBits, mode->blueBits);
  167. }
  168. if (current.width != mode->width || current.height != mode->height)
  169. {
  170. printf("*** Size mismatch: %ix%i instead of %ix%i\n",
  171. current.width, current.height,
  172. mode->width, mode->height);
  173. }
  174. printf("Closing window\n");
  175. glfwDestroyWindow(window);
  176. window = NULL;
  177. glfwPollEvents();
  178. }
  179. }
  180. int main(int argc, char** argv)
  181. {
  182. int ch, i, count, mode = LIST_MODE;
  183. GLFWmonitor** monitors;
  184. while ((ch = getopt(argc, argv, "th")) != -1)
  185. {
  186. switch (ch)
  187. {
  188. case 'h':
  189. usage();
  190. exit(EXIT_SUCCESS);
  191. case 't':
  192. mode = TEST_MODE;
  193. break;
  194. default:
  195. usage();
  196. exit(EXIT_FAILURE);
  197. }
  198. }
  199. glfwSetErrorCallback(error_callback);
  200. if (!glfwInit())
  201. exit(EXIT_FAILURE);
  202. monitors = glfwGetMonitors(&count);
  203. for (i = 0; i < count; i++)
  204. {
  205. if (mode == LIST_MODE)
  206. list_modes(monitors[i]);
  207. else if (mode == TEST_MODE)
  208. test_modes(monitors[i]);
  209. }
  210. glfwTerminate();
  211. exit(EXIT_SUCCESS);
  212. }