iconify.c 8.5 KB

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