monitors.c 7.6 KB

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