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