clipboard.c 3.7 KB

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