blob: e2522cc4525c1b5fd80f8304e249559c78e952e4 [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//
Camilla Berglund14355d62012-11-22 17:04:44 +010026// This test is used to test window focusing and iconfication for
Camilla Berglund5ca21892010-10-04 18:19:04 +020027// 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
Camilla Berglund9cc8fc02012-12-30 01:42:14 +010038static void error_callback(int error, const char* description)
39{
40 fprintf(stderr, "Error: %s\n", description);
41}
42
Camilla Berglund9af960e2013-01-05 21:13:28 +010043static void window_focus_callback(GLFWwindow* window, int focused)
Camilla Berglund5ca21892010-10-04 18:19:04 +020044{
45 printf("%0.3f: Window %s\n",
46 glfwGetTime(),
Camilla Berglund14355d62012-11-22 17:04:44 +010047 focused ? "focused" : "defocused");
Camilla Berglund5ca21892010-10-04 18:19:04 +020048}
49
Camilla Berglund9af960e2013-01-05 21:13:28 +010050static void window_key_callback(GLFWwindow* window, int key, int action)
Camilla Berglund5ca21892010-10-04 18:19:04 +020051{
52 if (action != GLFW_PRESS)
53 return;
54
55 switch (key)
56 {
Marcuse3cb5632011-01-03 22:22:14 +010057 case GLFW_KEY_ESCAPE:
Camilla Berglund5ca21892010-10-04 18:19:04 +020058 {
59 printf("%0.3f: User pressed Escape\n", glfwGetTime());
60 running = GL_FALSE;
61 break;
62 }
63
64 case GLFW_KEY_SPACE:
65 {
66 printf("%0.3f: User pressed Space\n", glfwGetTime());
67 glfwIconifyWindow(window);
68 break;
69 }
70 }
71}
72
Camilla Berglund9af960e2013-01-05 21:13:28 +010073static int window_close_callback(GLFWwindow* window)
Camilla Berglund5ca21892010-10-04 18:19:04 +020074{
75 printf("%0.3f: User closed window\n", glfwGetTime());
Camilla Berglund2972cdf2012-08-03 16:20:52 +020076 running = GL_FALSE;
Camilla Berglund5ca21892010-10-04 18:19:04 +020077 return GL_TRUE;
78}
79
80int main(void)
81{
Camilla Berglund9af960e2013-01-05 21:13:28 +010082 GLFWwindow* window;
Camilla Berglund5ca21892010-10-04 18:19:04 +020083
Camilla Berglund9cc8fc02012-12-30 01:42:14 +010084 glfwSetErrorCallback(error_callback);
85
Camilla Berglund0c3b1b52012-02-07 14:58:58 +010086 if (!glfwInit())
Camilla Berglund5ca21892010-10-04 18:19:04 +020087 exit(EXIT_FAILURE);
Camilla Berglund5ca21892010-10-04 18:19:04 +020088
Camilla Berglund1be16362012-09-27 21:37:36 +020089 window = glfwCreateWindow(640, 480, "Fullscreen focus", glfwGetPrimaryMonitor(), NULL);
Camilla Berglund5ca21892010-10-04 18:19:04 +020090 if (!window)
91 {
92 glfwTerminate();
Camilla Berglund5ca21892010-10-04 18:19:04 +020093 exit(EXIT_FAILURE);
94 }
95
Camilla Berglund2f095cc2012-08-10 15:29:45 +020096 glfwMakeContextCurrent(window);
Camilla Berglund5ca21892010-10-04 18:19:04 +020097 glfwSwapInterval(1);
Camilla Berglund2f095cc2012-08-10 15:29:45 +020098
Camilla Berglundce288a82012-02-04 00:51:35 +010099 glfwSetInputMode(window, GLFW_CURSOR_MODE, GLFW_CURSOR_NORMAL);
Camilla Berglund5ca21892010-10-04 18:19:04 +0200100
Camilla Berglund18d71c22012-10-28 13:45:11 +0100101 glfwSetWindowFocusCallback(window, window_focus_callback);
102 glfwSetKeyCallback(window, window_key_callback);
103 glfwSetWindowCloseCallback(window, window_close_callback);
Camilla Berglund5ca21892010-10-04 18:19:04 +0200104
Camilla Berglund2972cdf2012-08-03 16:20:52 +0200105 while (running)
Camilla Berglund5ca21892010-10-04 18:19:04 +0200106 {
107 glClear(GL_COLOR_BUFFER_BIT);
Camilla Berglund585a8402012-08-06 18:13:37 +0200108 glfwSwapBuffers(window);
Camilla Berglund5ca21892010-10-04 18:19:04 +0200109 glfwWaitEvents();
110 }
111
112 glfwTerminate();
113 exit(EXIT_SUCCESS);
114}
115