1 //========================================================================
2 // Clipboard 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 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(NULL);
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(NULL, string);
83                 printf("Setting clipboard to \"%s\"\n", string);
84             }
85             break;
86     }
87 }
88 
main(int argc,char ** argv)89 int main(int argc, char** argv)
90 {
91     int ch;
92     GLFWwindow* window;
93 
94     while ((ch = getopt(argc, argv, "h")) != -1)
95     {
96         switch (ch)
97         {
98             case 'h':
99                 usage();
100                 exit(EXIT_SUCCESS);
101 
102             default:
103                 usage();
104                 exit(EXIT_FAILURE);
105         }
106     }
107 
108     glfwSetErrorCallback(error_callback);
109 
110     if (!glfwInit())
111     {
112         fprintf(stderr, "Failed to initialize GLFW\n");
113         exit(EXIT_FAILURE);
114     }
115 
116     window = glfwCreateWindow(200, 200, "Clipboard Test", NULL, NULL);
117     if (!window)
118     {
119         glfwTerminate();
120 
121         fprintf(stderr, "Failed to open GLFW window\n");
122         exit(EXIT_FAILURE);
123     }
124 
125     glfwMakeContextCurrent(window);
126     gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
127     glfwSwapInterval(1);
128 
129     glfwSetKeyCallback(window, key_callback);
130 
131     glClearColor(0.5f, 0.5f, 0.5f, 0);
132 
133     while (!glfwWindowShouldClose(window))
134     {
135         glClear(GL_COLOR_BUFFER_BIT);
136 
137         glfwSwapBuffers(window);
138         glfwWaitEvents();
139     }
140 
141     glfwTerminate();
142     exit(EXIT_SUCCESS);
143 }
144 
145