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