monitors.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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, widthMM, heightMM, i;
  82. const GLFWvidmode* mode = glfwGetVideoMode(monitor);
  83. const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count);
  84. glfwGetMonitorPos(monitor, &x, &y);
  85. glfwGetMonitorPhysicalSize(monitor, &widthMM, &heightMM);
  86. printf("Name: %s (%s)\n",
  87. glfwGetMonitorName(monitor),
  88. glfwGetPrimaryMonitor() == monitor ? "primary" : "secondary");
  89. printf("Current mode: %s\n", format_mode(mode));
  90. printf("Virtual position: %i %i\n", x, y);
  91. printf("Physical size: %i x %i mm (%0.2f dpi)\n",
  92. widthMM, heightMM, mode->width * 25.4f / widthMM);
  93. printf("Modes:\n");
  94. for (i = 0; i < count; i++)
  95. {
  96. printf("%3u: %s", (unsigned int) i, format_mode(modes + i));
  97. if (memcmp(mode, modes + i, sizeof(GLFWvidmode)) == 0)
  98. printf(" (current mode)");
  99. putchar('\n');
  100. }
  101. }
  102. static void test_modes(GLFWmonitor* monitor)
  103. {
  104. int i, count;
  105. GLFWwindow* window;
  106. const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count);
  107. for (i = 0; i < count; i++)
  108. {
  109. const GLFWvidmode* mode = modes + i;
  110. GLFWvidmode current;
  111. glfwWindowHint(GLFW_RED_BITS, mode->redBits);
  112. glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
  113. glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
  114. glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
  115. printf("Testing mode %u on monitor %s: %s\n",
  116. (unsigned int) i,
  117. glfwGetMonitorName(monitor),
  118. format_mode(mode));
  119. window = glfwCreateWindow(mode->width, mode->height,
  120. "Video Mode Test",
  121. glfwGetPrimaryMonitor(),
  122. NULL);
  123. if (!window)
  124. {
  125. printf("Failed to enter mode %u: %s\n",
  126. (unsigned int) i,
  127. format_mode(mode));
  128. continue;
  129. }
  130. glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  131. glfwSetKeyCallback(window, key_callback);
  132. glfwMakeContextCurrent(window);
  133. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  134. glfwSwapInterval(1);
  135. glfwSetTime(0.0);
  136. while (glfwGetTime() < 5.0)
  137. {
  138. glClear(GL_COLOR_BUFFER_BIT);
  139. glfwSwapBuffers(window);
  140. glfwPollEvents();
  141. if (glfwWindowShouldClose(window))
  142. {
  143. printf("User terminated program\n");
  144. glfwTerminate();
  145. exit(EXIT_SUCCESS);
  146. }
  147. }
  148. glGetIntegerv(GL_RED_BITS, &current.redBits);
  149. glGetIntegerv(GL_GREEN_BITS, &current.greenBits);
  150. glGetIntegerv(GL_BLUE_BITS, &current.blueBits);
  151. glfwGetWindowSize(window, &current.width, &current.height);
  152. if (current.redBits != mode->redBits ||
  153. current.greenBits != mode->greenBits ||
  154. current.blueBits != mode->blueBits)
  155. {
  156. printf("*** Color bit mismatch: (%i %i %i) instead of (%i %i %i)\n",
  157. current.redBits, current.greenBits, current.blueBits,
  158. mode->redBits, mode->greenBits, mode->blueBits);
  159. }
  160. if (current.width != mode->width || current.height != mode->height)
  161. {
  162. printf("*** Size mismatch: %ix%i instead of %ix%i\n",
  163. current.width, current.height,
  164. mode->width, mode->height);
  165. }
  166. printf("Closing window\n");
  167. glfwDestroyWindow(window);
  168. window = NULL;
  169. glfwPollEvents();
  170. }
  171. }
  172. int main(int argc, char** argv)
  173. {
  174. int ch, i, count, mode = LIST_MODE;
  175. GLFWmonitor** monitors;
  176. while ((ch = getopt(argc, argv, "th")) != -1)
  177. {
  178. switch (ch)
  179. {
  180. case 'h':
  181. usage();
  182. exit(EXIT_SUCCESS);
  183. case 't':
  184. mode = TEST_MODE;
  185. break;
  186. default:
  187. usage();
  188. exit(EXIT_FAILURE);
  189. }
  190. }
  191. glfwSetErrorCallback(error_callback);
  192. if (!glfwInit())
  193. exit(EXIT_FAILURE);
  194. monitors = glfwGetMonitors(&count);
  195. for (i = 0; i < count; i++)
  196. {
  197. if (mode == LIST_MODE)
  198. list_modes(monitors[i]);
  199. else if (mode == TEST_MODE)
  200. test_modes(monitors[i]);
  201. }
  202. glfwTerminate();
  203. exit(EXIT_SUCCESS);
  204. }