1 //========================================================================
2 // Iconify/restore test program
3 // Copyright (c) Camilla Berglund <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
31 #include <glad/glad.h>
32 #include <GLFW/glfw3.h>
33
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #include "getopt.h"
38
39 static int windowed_xpos, windowed_ypos, windowed_width, windowed_height;
40
usage(void)41 static void usage(void)
42 {
43 printf("Usage: iconify [-h] [-f [-a] [-n]]\n");
44 printf("Options:\n");
45 printf(" -a create windows for all monitors\n");
46 printf(" -f create full screen window(s)\n");
47 printf(" -h show this help\n");
48 printf(" -n no automatic iconification of full screen windows\n");
49 }
50
error_callback(int error,const char * description)51 static void error_callback(int error, const char* description)
52 {
53 fprintf(stderr, "Error: %s\n", description);
54 }
55
key_callback(GLFWwindow * window,int key,int scancode,int action,int mods)56 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
57 {
58 printf("%0.2f Key %s\n",
59 glfwGetTime(),
60 action == GLFW_PRESS ? "pressed" : "released");
61
62 if (action != GLFW_PRESS)
63 return;
64
65 switch (key)
66 {
67 case GLFW_KEY_I:
68 glfwIconifyWindow(window);
69 break;
70 case GLFW_KEY_M:
71 glfwMaximizeWindow(window);
72 break;
73 case GLFW_KEY_R:
74 glfwRestoreWindow(window);
75 break;
76 case GLFW_KEY_ESCAPE:
77 glfwSetWindowShouldClose(window, GLFW_TRUE);
78 break;
79 case GLFW_KEY_F11:
80 case GLFW_KEY_ENTER:
81 {
82 if (mods != GLFW_MOD_ALT)
83 return;
84
85 if (glfwGetWindowMonitor(window))
86 {
87 glfwSetWindowMonitor(window, NULL,
88 windowed_xpos, windowed_ypos,
89 windowed_width, windowed_height,
90 0);
91 }
92 else
93 {
94 GLFWmonitor* monitor = glfwGetPrimaryMonitor();
95 if (monitor)
96 {
97 const GLFWvidmode* mode = glfwGetVideoMode(monitor);
98 glfwGetWindowPos(window, &windowed_xpos, &windowed_ypos);
99 glfwGetWindowSize(window, &windowed_width, &windowed_height);
100 glfwSetWindowMonitor(window, monitor,
101 0, 0, mode->width, mode->height,
102 mode->refreshRate);
103 }
104 }
105
106 break;
107 }
108 }
109 }
110
window_size_callback(GLFWwindow * window,int width,int height)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
framebuffer_size_callback(GLFWwindow * window,int width,int height)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 glViewport(0, 0, width, height);
121 }
122
window_focus_callback(GLFWwindow * window,int focused)123 static void window_focus_callback(GLFWwindow* window, int focused)
124 {
125 printf("%0.2f Window %s\n",
126 glfwGetTime(),
127 focused ? "focused" : "defocused");
128 }
129
window_iconify_callback(GLFWwindow * window,int iconified)130 static void window_iconify_callback(GLFWwindow* window, int iconified)
131 {
132 printf("%0.2f Window %s\n",
133 glfwGetTime(),
134 iconified ? "iconified" : "restored");
135 }
136
window_refresh_callback(GLFWwindow * window)137 static void window_refresh_callback(GLFWwindow* window)
138 {
139 int width, height;
140
141 printf("%0.2f Window refresh\n", glfwGetTime());
142
143 glfwGetFramebufferSize(window, &width, &height);
144
145 glfwMakeContextCurrent(window);
146
147 glEnable(GL_SCISSOR_TEST);
148
149 glScissor(0, 0, width, height);
150 glClearColor(0, 0, 0, 0);
151 glClear(GL_COLOR_BUFFER_BIT);
152
153 glScissor(0, 0, 640, 480);
154 glClearColor(1, 1, 1, 0);
155 glClear(GL_COLOR_BUFFER_BIT);
156
157 glfwSwapBuffers(window);
158 }
159
create_window(GLFWmonitor * monitor)160 static GLFWwindow* create_window(GLFWmonitor* monitor)
161 {
162 int width, height;
163 GLFWwindow* window;
164
165 if (monitor)
166 {
167 const GLFWvidmode* mode = glfwGetVideoMode(monitor);
168
169 glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
170 glfwWindowHint(GLFW_RED_BITS, mode->redBits);
171 glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
172 glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
173
174 width = mode->width;
175 height = mode->height;
176 }
177 else
178 {
179 width = 640;
180 height = 480;
181 }
182
183 window = glfwCreateWindow(width, height, "Iconify", monitor, NULL);
184 if (!window)
185 {
186 glfwTerminate();
187 exit(EXIT_FAILURE);
188 }
189
190 glfwMakeContextCurrent(window);
191 gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
192
193 return window;
194 }
195
main(int argc,char ** argv)196 int main(int argc, char** argv)
197 {
198 int ch, i, window_count;
199 int auto_iconify = GLFW_TRUE, fullscreen = GLFW_FALSE, all_monitors = GLFW_FALSE;
200 GLFWwindow** windows;
201
202 while ((ch = getopt(argc, argv, "afhn")) != -1)
203 {
204 switch (ch)
205 {
206 case 'a':
207 all_monitors = GLFW_TRUE;
208 break;
209
210 case 'h':
211 usage();
212 exit(EXIT_SUCCESS);
213
214 case 'f':
215 fullscreen = GLFW_TRUE;
216 break;
217
218 case 'n':
219 auto_iconify = GLFW_FALSE;
220 break;
221
222 default:
223 usage();
224 exit(EXIT_FAILURE);
225 }
226 }
227
228 glfwSetErrorCallback(error_callback);
229
230 if (!glfwInit())
231 exit(EXIT_FAILURE);
232
233 glfwWindowHint(GLFW_AUTO_ICONIFY, auto_iconify);
234
235 if (fullscreen && all_monitors)
236 {
237 int monitor_count;
238 GLFWmonitor** monitors = glfwGetMonitors(&monitor_count);
239
240 window_count = monitor_count;
241 windows = calloc(window_count, sizeof(GLFWwindow*));
242
243 for (i = 0; i < monitor_count; i++)
244 {
245 windows[i] = create_window(monitors[i]);
246 if (!windows[i])
247 break;
248 }
249 }
250 else
251 {
252 GLFWmonitor* monitor = NULL;
253
254 if (fullscreen)
255 monitor = glfwGetPrimaryMonitor();
256
257 window_count = 1;
258 windows = calloc(window_count, sizeof(GLFWwindow*));
259 windows[0] = create_window(monitor);
260 }
261
262 for (i = 0; i < window_count; i++)
263 {
264 glfwSetKeyCallback(windows[i], key_callback);
265 glfwSetFramebufferSizeCallback(windows[i], framebuffer_size_callback);
266 glfwSetWindowSizeCallback(windows[i], window_size_callback);
267 glfwSetWindowFocusCallback(windows[i], window_focus_callback);
268 glfwSetWindowIconifyCallback(windows[i], window_iconify_callback);
269 glfwSetWindowRefreshCallback(windows[i], window_refresh_callback);
270
271 window_refresh_callback(windows[i]);
272
273 printf("Window is %s and %s\n",
274 glfwGetWindowAttrib(windows[i], GLFW_ICONIFIED) ? "iconified" : "restored",
275 glfwGetWindowAttrib(windows[i], GLFW_FOCUSED) ? "focused" : "defocused");
276 }
277
278 for (;;)
279 {
280 glfwWaitEvents();
281
282 for (i = 0; i < window_count; i++)
283 {
284 if (glfwWindowShouldClose(windows[i]))
285 break;
286 }
287
288 if (i < window_count)
289 break;
290
291 // Workaround for an issue with msvcrt and mintty
292 fflush(stdout);
293 }
294
295 glfwTerminate();
296 exit(EXIT_SUCCESS);
297 }
298
299