monitors.c 7.2 KB

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