blob: 17ffc35f27fcc828106758f5572374186bbfe1dd [file] [log] [blame]
Camilla Berglund5ca21892010-10-04 18:19:04 +02001//========================================================================
2// Fullscreen window (un)focus test
3// Copyright (c) Camilla Berglund <elmindreda@elmindreda.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 test is used to test window activation and iconfication for
27// fullscreen windows with a video mode differing from the desktop mode
28//
29//========================================================================
30
31#include <GL/glfw3.h>
32
33#include <stdio.h>
34#include <stdlib.h>
35
36static GLboolean running = GL_TRUE;
37
38static void window_focus_callback(GLFWwindow window, int activated)
39{
40 printf("%0.3f: Window %s\n",
41 glfwGetTime(),
42 activated ? "activated" : "deactivated");
43}
44
45static void window_key_callback(GLFWwindow window, int key, int action)
46{
47 if (action != GLFW_PRESS)
48 return;
49
50 switch (key)
51 {
Marcuse3cb5632011-01-03 22:22:14 +010052 case GLFW_KEY_ESCAPE:
Camilla Berglund5ca21892010-10-04 18:19:04 +020053 {
54 printf("%0.3f: User pressed Escape\n", glfwGetTime());
55 running = GL_FALSE;
56 break;
57 }
58
59 case GLFW_KEY_SPACE:
60 {
61 printf("%0.3f: User pressed Space\n", glfwGetTime());
62 glfwIconifyWindow(window);
63 break;
64 }
65 }
66}
67
68static int window_close_callback(GLFWwindow window)
69{
70 printf("%0.3f: User closed window\n", glfwGetTime());
Camilla Berglund2972cdf2012-08-03 16:20:52 +020071 running = GL_FALSE;
Camilla Berglund5ca21892010-10-04 18:19:04 +020072 return GL_TRUE;
73}
74
75int main(void)
76{
77 GLFWwindow window;
78
Camilla Berglund0c3b1b52012-02-07 14:58:58 +010079 if (!glfwInit())
Camilla Berglund5ca21892010-10-04 18:19:04 +020080 {
81 fprintf(stderr, "Failed to initialize GLFW: %s\n", glfwErrorString(glfwGetError()));
82 exit(EXIT_FAILURE);
83 }
84
Camilla Berglundaff30d02012-08-06 17:56:41 +020085 window = glfwCreateWindow(640, 480, GLFW_FULLSCREEN, "Fullscreen focus", NULL);
Camilla Berglund5ca21892010-10-04 18:19:04 +020086 if (!window)
87 {
88 glfwTerminate();
89
90 fprintf(stderr, "Failed to open GLFW window: %s\n", glfwErrorString(glfwGetError()));
91 exit(EXIT_FAILURE);
92 }
93
Camilla Berglund2f095cc2012-08-10 15:29:45 +020094 glfwMakeContextCurrent(window);
Camilla Berglund5ca21892010-10-04 18:19:04 +020095 glfwSwapInterval(1);
Camilla Berglund2f095cc2012-08-10 15:29:45 +020096
Camilla Berglundce288a82012-02-04 00:51:35 +010097 glfwSetInputMode(window, GLFW_CURSOR_MODE, GLFW_CURSOR_NORMAL);
Camilla Berglund5ca21892010-10-04 18:19:04 +020098
Camilla Berglund18d71c22012-10-28 13:45:11 +010099 glfwSetWindowFocusCallback(window, window_focus_callback);
100 glfwSetKeyCallback(window, window_key_callback);
101 glfwSetWindowCloseCallback(window, window_close_callback);
Camilla Berglund5ca21892010-10-04 18:19:04 +0200102
Camilla Berglund2972cdf2012-08-03 16:20:52 +0200103 while (running)
Camilla Berglund5ca21892010-10-04 18:19:04 +0200104 {
105 glClear(GL_COLOR_BUFFER_BIT);
Camilla Berglund585a8402012-08-06 18:13:37 +0200106 glfwSwapBuffers(window);
Camilla Berglund5ca21892010-10-04 18:19:04 +0200107 glfwWaitEvents();
108 }
109
110 glfwTerminate();
111 exit(EXIT_SUCCESS);
112}
113