iconify.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //========================================================================
  2. // Iconify/restore test program
  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 program is used to test the iconify/restore functionality for
  27. // both full screen and windowed mode windows
  28. //
  29. //========================================================================
  30. #include <glad/gl.h>
  31. #define GLFW_INCLUDE_NONE
  32. #include <GLFW/glfw3.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include "getopt.h"
  36. static int windowed_xpos, windowed_ypos, windowed_width, windowed_height;
  37. static void usage(void)
  38. {
  39. printf("Usage: iconify [-h] [-f [-a] [-n]]\n");
  40. printf("Options:\n");
  41. printf(" -a create windows for all monitors\n");
  42. printf(" -f create full screen window(s)\n");
  43. printf(" -h show this help\n");
  44. }
  45. static void error_callback(int error, const char* description)
  46. {
  47. fprintf(stderr, "Error: %s\n", description);
  48. }
  49. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  50. {
  51. printf("%0.2f Key %s\n",
  52. glfwGetTime(),
  53. action == GLFW_PRESS ? "pressed" : "released");
  54. if (action != GLFW_PRESS)
  55. return;
  56. switch (key)
  57. {
  58. case GLFW_KEY_I:
  59. glfwIconifyWindow(window);
  60. break;
  61. case GLFW_KEY_M:
  62. glfwMaximizeWindow(window);
  63. break;
  64. case GLFW_KEY_R:
  65. glfwRestoreWindow(window);
  66. break;
  67. case GLFW_KEY_ESCAPE:
  68. glfwSetWindowShouldClose(window, GLFW_TRUE);
  69. break;
  70. case GLFW_KEY_A:
  71. glfwSetWindowAttrib(window, GLFW_AUTO_ICONIFY, !glfwGetWindowAttrib(window, GLFW_AUTO_ICONIFY));
  72. break;
  73. case GLFW_KEY_B:
  74. glfwSetWindowAttrib(window, GLFW_RESIZABLE, !glfwGetWindowAttrib(window, GLFW_RESIZABLE));
  75. break;
  76. case GLFW_KEY_D:
  77. glfwSetWindowAttrib(window, GLFW_DECORATED, !glfwGetWindowAttrib(window, GLFW_DECORATED));
  78. break;
  79. case GLFW_KEY_F:
  80. glfwSetWindowAttrib(window, GLFW_FLOATING, !glfwGetWindowAttrib(window, GLFW_FLOATING));
  81. break;
  82. case GLFW_KEY_F11:
  83. case GLFW_KEY_ENTER:
  84. {
  85. if (mods != GLFW_MOD_ALT)
  86. return;
  87. if (glfwGetWindowMonitor(window))
  88. {
  89. glfwSetWindowMonitor(window, NULL,
  90. windowed_xpos, windowed_ypos,
  91. windowed_width, windowed_height,
  92. 0);
  93. }
  94. else
  95. {
  96. GLFWmonitor* monitor = glfwGetPrimaryMonitor();
  97. if (monitor)
  98. {
  99. const GLFWvidmode* mode = glfwGetVideoMode(monitor);
  100. glfwGetWindowPos(window, &windowed_xpos, &windowed_ypos);
  101. glfwGetWindowSize(window, &windowed_width, &windowed_height);
  102. glfwSetWindowMonitor(window, monitor,
  103. 0, 0, mode->width, mode->height,
  104. mode->refreshRate);
  105. }
  106. }
  107. break;
  108. }
  109. }
  110. }
  111. static void window_size_callback(GLFWwindow* window, int width, int height)
  112. {
  113. printf("%0.2f Window resized to %ix%i\n", glfwGetTime(), width, height);
  114. }
  115. static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  116. {
  117. printf("%0.2f Framebuffer resized to %ix%i\n", glfwGetTime(), width, height);
  118. }
  119. static void window_focus_callback(GLFWwindow* window, int focused)
  120. {
  121. printf("%0.2f Window %s\n",
  122. glfwGetTime(),
  123. focused ? "focused" : "defocused");
  124. }
  125. static void window_iconify_callback(GLFWwindow* window, int iconified)
  126. {
  127. printf("%0.2f Window %s\n",
  128. glfwGetTime(),
  129. iconified ? "iconified" : "uniconified");
  130. }
  131. static void window_maximize_callback(GLFWwindow* window, int maximized)
  132. {
  133. printf("%0.2f Window %s\n",
  134. glfwGetTime(),
  135. maximized ? "maximized" : "unmaximized");
  136. }
  137. static void window_refresh_callback(GLFWwindow* window)
  138. {
  139. printf("%0.2f Window refresh\n", glfwGetTime());
  140. glfwMakeContextCurrent(window);
  141. glClear(GL_COLOR_BUFFER_BIT);
  142. glfwSwapBuffers(window);
  143. }
  144. static GLFWwindow* create_window(GLFWmonitor* monitor)
  145. {
  146. int width, height;
  147. GLFWwindow* window;
  148. if (monitor)
  149. {
  150. const GLFWvidmode* mode = glfwGetVideoMode(monitor);
  151. glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
  152. glfwWindowHint(GLFW_RED_BITS, mode->redBits);
  153. glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
  154. glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
  155. width = mode->width;
  156. height = mode->height;
  157. }
  158. else
  159. {
  160. width = 640;
  161. height = 480;
  162. }
  163. window = glfwCreateWindow(width, height, "Iconify", monitor, NULL);
  164. if (!window)
  165. {
  166. glfwTerminate();
  167. exit(EXIT_FAILURE);
  168. }
  169. glfwMakeContextCurrent(window);
  170. gladLoadGL(glfwGetProcAddress);
  171. return window;
  172. }
  173. int main(int argc, char** argv)
  174. {
  175. int ch, i, window_count;
  176. int fullscreen = GLFW_FALSE, all_monitors = GLFW_FALSE;
  177. GLFWwindow** windows;
  178. while ((ch = getopt(argc, argv, "afhn")) != -1)
  179. {
  180. switch (ch)
  181. {
  182. case 'a':
  183. all_monitors = GLFW_TRUE;
  184. break;
  185. case 'h':
  186. usage();
  187. exit(EXIT_SUCCESS);
  188. case 'f':
  189. fullscreen = GLFW_TRUE;
  190. break;
  191. default:
  192. usage();
  193. exit(EXIT_FAILURE);
  194. }
  195. }
  196. glfwSetErrorCallback(error_callback);
  197. if (!glfwInit())
  198. exit(EXIT_FAILURE);
  199. if (fullscreen && all_monitors)
  200. {
  201. int monitor_count;
  202. GLFWmonitor** monitors = glfwGetMonitors(&monitor_count);
  203. window_count = monitor_count;
  204. windows = calloc(window_count, sizeof(GLFWwindow*));
  205. for (i = 0; i < monitor_count; i++)
  206. {
  207. windows[i] = create_window(monitors[i]);
  208. if (!windows[i])
  209. break;
  210. }
  211. }
  212. else
  213. {
  214. GLFWmonitor* monitor = NULL;
  215. if (fullscreen)
  216. monitor = glfwGetPrimaryMonitor();
  217. window_count = 1;
  218. windows = calloc(window_count, sizeof(GLFWwindow*));
  219. windows[0] = create_window(monitor);
  220. }
  221. for (i = 0; i < window_count; i++)
  222. {
  223. glfwSetKeyCallback(windows[i], key_callback);
  224. glfwSetFramebufferSizeCallback(windows[i], framebuffer_size_callback);
  225. glfwSetWindowSizeCallback(windows[i], window_size_callback);
  226. glfwSetWindowFocusCallback(windows[i], window_focus_callback);
  227. glfwSetWindowIconifyCallback(windows[i], window_iconify_callback);
  228. glfwSetWindowMaximizeCallback(windows[i], window_maximize_callback);
  229. glfwSetWindowRefreshCallback(windows[i], window_refresh_callback);
  230. window_refresh_callback(windows[i]);
  231. printf("Window is %s and %s\n",
  232. glfwGetWindowAttrib(windows[i], GLFW_ICONIFIED) ? "iconified" : "restored",
  233. glfwGetWindowAttrib(windows[i], GLFW_FOCUSED) ? "focused" : "defocused");
  234. }
  235. for (;;)
  236. {
  237. glfwWaitEvents();
  238. for (i = 0; i < window_count; i++)
  239. {
  240. if (glfwWindowShouldClose(windows[i]))
  241. break;
  242. }
  243. if (i < window_count)
  244. break;
  245. // Workaround for an issue with msvcrt and mintty
  246. fflush(stdout);
  247. }
  248. glfwTerminate();
  249. exit(EXIT_SUCCESS);
  250. }