• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //========================================================================
2 // Clipboard 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 clipboard functionality.
27 //
28 //========================================================================
29 
30 #include <glad/glad.h>
31 #include <GLFW/glfw3.h>
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 
36 #include "getopt.h"
37 
38 #if defined(__APPLE__)
39  #define MODIFIER GLFW_MOD_SUPER
40 #else
41  #define MODIFIER GLFW_MOD_CONTROL
42 #endif
43 
usage(void)44 static void usage(void)
45 {
46     printf("Usage: clipboard [-h]\n");
47 }
48 
error_callback(int error,const char * description)49 static void error_callback(int error, const char* description)
50 {
51     fprintf(stderr, "Error: %s\n", description);
52 }
53 
key_callback(GLFWwindow * window,int key,int scancode,int action,int mods)54 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
55 {
56     if (action != GLFW_PRESS)
57         return;
58 
59     switch (key)
60     {
61         case GLFW_KEY_ESCAPE:
62             glfwSetWindowShouldClose(window, GLFW_TRUE);
63             break;
64 
65         case GLFW_KEY_V:
66             if (mods == MODIFIER)
67             {
68                 const char* string;
69 
70                 string = glfwGetClipboardString(window);
71                 if (string)
72                     printf("Clipboard contains \"%s\"\n", string);
73                 else
74                     printf("Clipboard does not contain a string\n");
75             }
76             break;
77 
78         case GLFW_KEY_C:
79             if (mods == MODIFIER)
80             {
81                 const char* string = "Hello GLFW World!";
82                 glfwSetClipboardString(window, string);
83                 printf("Setting clipboard to \"%s\"\n", string);
84             }
85             break;
86     }
87 }
88 
framebuffer_size_callback(GLFWwindow * window,int width,int height)89 static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
90 {
91     glViewport(0, 0, width, height);
92 }
93 
main(int argc,char ** argv)94 int main(int argc, char** argv)
95 {
96     int ch;
97     GLFWwindow* window;
98 
99     while ((ch = getopt(argc, argv, "h")) != -1)
100     {
101         switch (ch)
102         {
103             case 'h':
104                 usage();
105                 exit(EXIT_SUCCESS);
106 
107             default:
108                 usage();
109                 exit(EXIT_FAILURE);
110         }
111     }
112 
113     glfwSetErrorCallback(error_callback);
114 
115     if (!glfwInit())
116     {
117         fprintf(stderr, "Failed to initialize GLFW\n");
118         exit(EXIT_FAILURE);
119     }
120 
121     window = glfwCreateWindow(200, 200, "Clipboard Test", NULL, NULL);
122     if (!window)
123     {
124         glfwTerminate();
125 
126         fprintf(stderr, "Failed to open GLFW window\n");
127         exit(EXIT_FAILURE);
128     }
129 
130     glfwMakeContextCurrent(window);
131     gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
132     glfwSwapInterval(1);
133 
134     glfwSetKeyCallback(window, key_callback);
135     glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
136 
137     glMatrixMode(GL_PROJECTION);
138     glOrtho(-1.f, 1.f, -1.f, 1.f, -1.f, 1.f);
139     glMatrixMode(GL_MODELVIEW);
140 
141     glClearColor(0.5f, 0.5f, 0.5f, 0);
142 
143     while (!glfwWindowShouldClose(window))
144     {
145         glClear(GL_COLOR_BUFFER_BIT);
146 
147         glColor3f(0.8f, 0.2f, 0.4f);
148         glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
149 
150         glfwSwapBuffers(window);
151         glfwWaitEvents();
152     }
153 
154     glfwTerminate();
155     exit(EXIT_SUCCESS);
156 }
157 
158