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 
31 #include <glad/gl.h>
32 #define GLFW_INCLUDE_NONE
33 #include <GLFW/glfw3.h>
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 
38 #include "getopt.h"
39 
40 static int windowed_xpos, windowed_ypos, windowed_width, windowed_height;
41 
usage(void)42 static void usage(void)
43 {
44     printf("Usage: iconify [-h] [-f [-a] [-n]]\n");
45     printf("Options:\n");
46     printf("  -a create windows for all monitors\n");
47     printf("  -f create full screen window(s)\n");
48     printf("  -h show this help\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_A:
80             glfwSetWindowAttrib(window, GLFW_AUTO_ICONIFY, !glfwGetWindowAttrib(window, GLFW_AUTO_ICONIFY));
81             break;
82         case GLFW_KEY_B:
83             glfwSetWindowAttrib(window, GLFW_RESIZABLE, !glfwGetWindowAttrib(window, GLFW_RESIZABLE));
84             break;
85         case GLFW_KEY_D:
86             glfwSetWindowAttrib(window, GLFW_DECORATED, !glfwGetWindowAttrib(window, GLFW_DECORATED));
87             break;
88         case GLFW_KEY_F:
89             glfwSetWindowAttrib(window, GLFW_FLOATING, !glfwGetWindowAttrib(window, GLFW_FLOATING));
90             break;
91         case GLFW_KEY_F11:
92         case GLFW_KEY_ENTER:
93         {
94             if (mods != GLFW_MOD_ALT)
95                 return;
96 
97             if (glfwGetWindowMonitor(window))
98             {
99                 glfwSetWindowMonitor(window, NULL,
100                                      windowed_xpos, windowed_ypos,
101                                      windowed_width, windowed_height,
102                                      0);
103             }
104             else
105             {
106                 GLFWmonitor* monitor = glfwGetPrimaryMonitor();
107                 if (monitor)
108                 {
109                     const GLFWvidmode* mode = glfwGetVideoMode(monitor);
110                     glfwGetWindowPos(window, &windowed_xpos, &windowed_ypos);
111                     glfwGetWindowSize(window, &windowed_width, &windowed_height);
112                     glfwSetWindowMonitor(window, monitor,
113                                          0, 0, mode->width, mode->height,
114                                          mode->refreshRate);
115                 }
116             }
117 
118             break;
119         }
120     }
121 }
122 
window_size_callback(GLFWwindow * window,int width,int height)123 static void window_size_callback(GLFWwindow* window, int width, int height)
124 {
125     printf("%0.2f Window resized to %ix%i\n", glfwGetTime(), width, height);
126 }
127 
framebuffer_size_callback(GLFWwindow * window,int width,int height)128 static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
129 {
130     printf("%0.2f Framebuffer resized to %ix%i\n", glfwGetTime(), width, height);
131 }
132 
window_focus_callback(GLFWwindow * window,int focused)133 static void window_focus_callback(GLFWwindow* window, int focused)
134 {
135     printf("%0.2f Window %s\n",
136            glfwGetTime(),
137            focused ? "focused" : "defocused");
138 }
139 
window_iconify_callback(GLFWwindow * window,int iconified)140 static void window_iconify_callback(GLFWwindow* window, int iconified)
141 {
142     printf("%0.2f Window %s\n",
143            glfwGetTime(),
144            iconified ? "iconified" : "uniconified");
145 }
146 
window_maximize_callback(GLFWwindow * window,int maximized)147 static void window_maximize_callback(GLFWwindow* window, int maximized)
148 {
149     printf("%0.2f Window %s\n",
150            glfwGetTime(),
151            maximized ? "maximized" : "unmaximized");
152 }
153 
window_refresh_callback(GLFWwindow * window)154 static void window_refresh_callback(GLFWwindow* window)
155 {
156     printf("%0.2f Window refresh\n", glfwGetTime());
157 
158     glfwMakeContextCurrent(window);
159 
160     glClear(GL_COLOR_BUFFER_BIT);
161     glfwSwapBuffers(window);
162 }
163 
create_window(GLFWmonitor * monitor)164 static GLFWwindow* create_window(GLFWmonitor* monitor)
165 {
166     int width, height;
167     GLFWwindow* window;
168 
169     if (monitor)
170     {
171         const GLFWvidmode* mode = glfwGetVideoMode(monitor);
172 
173         glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
174         glfwWindowHint(GLFW_RED_BITS, mode->redBits);
175         glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
176         glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
177 
178         width = mode->width;
179         height = mode->height;
180     }
181     else
182     {
183         width = 640;
184         height = 480;
185     }
186 
187     window = glfwCreateWindow(width, height, "Iconify", monitor, NULL);
188     if (!window)
189     {
190         glfwTerminate();
191         exit(EXIT_FAILURE);
192     }
193 
194     glfwMakeContextCurrent(window);
195     gladLoadGL(glfwGetProcAddress);
196 
197     return window;
198 }
199 
main(int argc,char ** argv)200 int main(int argc, char** argv)
201 {
202     int ch, i, window_count;
203     int fullscreen = GLFW_FALSE, all_monitors = GLFW_FALSE;
204     GLFWwindow** windows;
205 
206     while ((ch = getopt(argc, argv, "afhn")) != -1)
207     {
208         switch (ch)
209         {
210             case 'a':
211                 all_monitors = GLFW_TRUE;
212                 break;
213 
214             case 'h':
215                 usage();
216                 exit(EXIT_SUCCESS);
217 
218             case 'f':
219                 fullscreen = GLFW_TRUE;
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     if (fullscreen && all_monitors)
234     {
235         int monitor_count;
236         GLFWmonitor** monitors = glfwGetMonitors(&monitor_count);
237 
238         window_count = monitor_count;
239         windows = calloc(window_count, sizeof(GLFWwindow*));
240 
241         for (i = 0;  i < monitor_count;  i++)
242         {
243             windows[i] = create_window(monitors[i]);
244             if (!windows[i])
245                 break;
246         }
247     }
248     else
249     {
250         GLFWmonitor* monitor = NULL;
251 
252         if (fullscreen)
253             monitor = glfwGetPrimaryMonitor();
254 
255         window_count = 1;
256         windows = calloc(window_count, sizeof(GLFWwindow*));
257         windows[0] = create_window(monitor);
258     }
259 
260     for (i = 0;  i < window_count;  i++)
261     {
262         glfwSetKeyCallback(windows[i], key_callback);
263         glfwSetFramebufferSizeCallback(windows[i], framebuffer_size_callback);
264         glfwSetWindowSizeCallback(windows[i], window_size_callback);
265         glfwSetWindowFocusCallback(windows[i], window_focus_callback);
266         glfwSetWindowIconifyCallback(windows[i], window_iconify_callback);
267         glfwSetWindowMaximizeCallback(windows[i], window_maximize_callback);
268         glfwSetWindowRefreshCallback(windows[i], window_refresh_callback);
269 
270         window_refresh_callback(windows[i]);
271 
272         printf("Window is %s and %s\n",
273             glfwGetWindowAttrib(windows[i], GLFW_ICONIFIED) ? "iconified" : "restored",
274             glfwGetWindowAttrib(windows[i], GLFW_FOCUSED) ? "focused" : "defocused");
275     }
276 
277     for (;;)
278     {
279         glfwWaitEvents();
280 
281         for (i = 0;  i < window_count;  i++)
282         {
283             if (glfwWindowShouldClose(windows[i]))
284                 break;
285         }
286 
287         if (i < window_count)
288             break;
289 
290         // Workaround for an issue with msvcrt and mintty
291         fflush(stdout);
292     }
293 
294     glfwTerminate();
295     exit(EXIT_SUCCESS);
296 }
297 
298