commit
67b5bb1d45
15 changed files with 819 additions and 18 deletions
@ -0,0 +1,77 @@ |
||||
//========================================================================
|
||||
// GLFW - An OpenGL library
|
||||
// Platform: Any
|
||||
// API version: 3.0
|
||||
// WWW: http://www.glfw.org/
|
||||
//------------------------------------------------------------------------
|
||||
// Copyright (c) 2010 Camilla Berglund <elmindreda@elmindreda.org>
|
||||
//
|
||||
// This software is provided 'as-is', without any express or implied
|
||||
// warranty. In no event will the authors be held liable for any damages
|
||||
// arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it
|
||||
// freely, subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented; you must not
|
||||
// claim that you wrote the original software. If you use this software
|
||||
// in a product, an acknowledgment in the product documentation would
|
||||
// be appreciated but is not required.
|
||||
//
|
||||
// 2. Altered source versions must be plainly marked as such, and must not
|
||||
// be misrepresented as being the original software.
|
||||
//
|
||||
// 3. This notice may not be removed or altered from any source
|
||||
// distribution.
|
||||
//
|
||||
//========================================================================
|
||||
|
||||
#include "internal.h" |
||||
|
||||
#include <math.h> |
||||
#include <string.h> |
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
////// GLFW public API //////
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//========================================================================
|
||||
// Set the clipboard contents
|
||||
//========================================================================
|
||||
|
||||
GLFWAPI void glfwSetClipboardString(GLFWwindow handle, const char* string) |
||||
{ |
||||
_GLFWwindow* window = (_GLFWwindow*) handle; |
||||
|
||||
if (!_glfwInitialized) |
||||
{ |
||||
_glfwSetError(GLFW_NOT_INITIALIZED, NULL); |
||||
return; |
||||
} |
||||
|
||||
_glfwPlatformSetClipboardString(window, string); |
||||
} |
||||
|
||||
|
||||
//========================================================================
|
||||
// Return the current clipboard contents
|
||||
//========================================================================
|
||||
|
||||
GLFWAPI size_t glfwGetClipboardString(GLFWwindow handle, char* string, size_t size) |
||||
{ |
||||
_GLFWwindow* window = (_GLFWwindow*) handle; |
||||
|
||||
if (!_glfwInitialized) |
||||
{ |
||||
_glfwSetError(GLFW_NOT_INITIALIZED, NULL); |
||||
return 0; |
||||
} |
||||
|
||||
if (!string || !size) |
||||
return 0; |
||||
|
||||
return _glfwPlatformGetClipboardString(window, string, size); |
||||
} |
||||
|
@ -0,0 +1,87 @@ |
||||
//======================================================================== |
||||
// GLFW - An OpenGL library |
||||
// Platform: Cocoa/NSOpenGL |
||||
// API version: 3.0 |
||||
// WWW: http://www.glfw.org/ |
||||
//------------------------------------------------------------------------ |
||||
// Copyright (c) 2010 Camilla Berglund <elmindreda@elmindreda.org> |
||||
// |
||||
// This software is provided 'as-is', without any express or implied |
||||
// warranty. In no event will the authors be held liable for any damages |
||||
// arising from the use of this software. |
||||
// |
||||
// Permission is granted to anyone to use this software for any purpose, |
||||
// including commercial applications, and to alter it and redistribute it |
||||
// freely, subject to the following restrictions: |
||||
// |
||||
// 1. The origin of this software must not be misrepresented; you must not |
||||
// claim that you wrote the original software. If you use this software |
||||
// in a product, an acknowledgment in the product documentation would |
||||
// be appreciated but is not required. |
||||
// |
||||
// 2. Altered source versions must be plainly marked as such, and must not |
||||
// be misrepresented as being the original software. |
||||
// |
||||
// 3. This notice may not be removed or altered from any source |
||||
// distribution. |
||||
// |
||||
//======================================================================== |
||||
|
||||
#include "internal.h" |
||||
|
||||
#include <limits.h> |
||||
#include <string.h> |
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////// |
||||
////// GLFW platform API ////// |
||||
////////////////////////////////////////////////////////////////////////// |
||||
|
||||
//======================================================================== |
||||
// Set the clipboard contents |
||||
//======================================================================== |
||||
|
||||
void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string) |
||||
{ |
||||
NSArray* types = [NSArray arrayWithObjects:NSStringPboardType, nil]; |
||||
|
||||
NSPasteboard* pasteboard = [NSPasteboard generalPasteboard]; |
||||
[pasteboard declareTypes:types owner:nil]; |
||||
[pasteboard setString:[NSString stringWithUTF8String:string] |
||||
forType:NSStringPboardType]; |
||||
} |
||||
|
||||
|
||||
//======================================================================== |
||||
// Return the current clipboard contents |
||||
//======================================================================== |
||||
|
||||
size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t size) |
||||
{ |
||||
const char* source; |
||||
size_t targetSize; |
||||
NSPasteboard* pasteboard = [NSPasteboard generalPasteboard]; |
||||
|
||||
if (![[pasteboard types] containsObject:NSStringPboardType]) |
||||
{ |
||||
_glfwSetError(GLFW_FORMAT_UNAVAILABLE, NULL); |
||||
return 0; |
||||
} |
||||
|
||||
NSString* object = [pasteboard stringForType:NSStringPboardType]; |
||||
if (!object) |
||||
{ |
||||
_glfwSetError(GLFW_PLATFORM_ERROR, |
||||
"Cocoa/NSGL: Failed to retrieve object from pasteboard"); |
||||
return 0; |
||||
} |
||||
|
||||
source = [object UTF8String]; |
||||
targetSize = strlen(source) + 1; |
||||
if (targetSize > size) |
||||
targetSize = size; |
||||
|
||||
strlcpy(string, source, targetSize); |
||||
return 0; |
||||
} |
||||
|
@ -0,0 +1,149 @@ |
||||
//========================================================================
|
||||
// GLFW - An OpenGL library
|
||||
// Platform: Win32/WGL
|
||||
// API version: 3.0
|
||||
// WWW: http://www.glfw.org/
|
||||
//------------------------------------------------------------------------
|
||||
// Copyright (c) 2010 Camilla Berglund <elmindreda@elmindreda.org>
|
||||
//
|
||||
// This software is provided 'as-is', without any express or implied
|
||||
// warranty. In no event will the authors be held liable for any damages
|
||||
// arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it
|
||||
// freely, subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented; you must not
|
||||
// claim that you wrote the original software. If you use this software
|
||||
// in a product, an acknowledgment in the product documentation would
|
||||
// be appreciated but is not required.
|
||||
//
|
||||
// 2. Altered source versions must be plainly marked as such, and must not
|
||||
// be misrepresented as being the original software.
|
||||
//
|
||||
// 3. This notice may not be removed or altered from any source
|
||||
// distribution.
|
||||
//
|
||||
//========================================================================
|
||||
|
||||
#include "internal.h" |
||||
|
||||
#include <limits.h> |
||||
#include <string.h> |
||||
#include <stdlib.h> |
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
////// GLFW platform API //////
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//========================================================================
|
||||
// Set the clipboard contents
|
||||
//========================================================================
|
||||
|
||||
void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string) |
||||
{ |
||||
WCHAR* wideString; |
||||
HANDLE stringHandle; |
||||
size_t wideSize; |
||||
|
||||
wideString = _glfwCreateWideStringFromUTF8(string); |
||||
if (!wideString) |
||||
{ |
||||
_glfwSetError(GLFW_PLATFORM_ERROR, |
||||
"Win32/WGL: Failed to convert clipboard string to " |
||||
"wide string"); |
||||
return; |
||||
} |
||||
|
||||
wideSize = (wcslen(wideString) + 1) * sizeof(WCHAR); |
||||
|
||||
stringHandle = GlobalAlloc(GMEM_MOVEABLE, wideSize); |
||||
if (!stringHandle) |
||||
{ |
||||
free(wideString); |
||||
|
||||
_glfwSetError(GLFW_PLATFORM_ERROR, |
||||
"Win32/WGL: Failed to allocate global handle for clipboard"); |
||||
return; |
||||
} |
||||
|
||||
memcpy(GlobalLock(stringHandle), wideString, wideSize); |
||||
GlobalUnlock(stringHandle); |
||||
|
||||
if (!OpenClipboard(window->Win32.handle)) |
||||
{ |
||||
GlobalFree(stringHandle); |
||||
free(wideString); |
||||
|
||||
_glfwSetError(GLFW_PLATFORM_ERROR, |
||||
"Win32/WGL: Failed to open clipboard"); |
||||
return; |
||||
} |
||||
|
||||
EmptyClipboard(); |
||||
SetClipboardData(CF_UNICODETEXT, stringHandle); |
||||
CloseClipboard(); |
||||
|
||||
free(wideString); |
||||
} |
||||
|
||||
|
||||
//========================================================================
|
||||
// Return the current clipboard contents
|
||||
//========================================================================
|
||||
|
||||
size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t size) |
||||
{ |
||||
HANDLE stringHandle; |
||||
char* utf8String; |
||||
size_t utf8Size; |
||||
|
||||
if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) |
||||
{ |
||||
_glfwSetError(GLFW_FORMAT_UNAVAILABLE, NULL); |
||||
return 0; |
||||
} |
||||
|
||||
if (!OpenClipboard(window->Win32.handle)) |
||||
{ |
||||
_glfwSetError(GLFW_PLATFORM_ERROR, |
||||
"Win32/WGL: Failed to open clipboard"); |
||||
return 0; |
||||
} |
||||
|
||||
stringHandle = GetClipboardData(CF_UNICODETEXT); |
||||
if (!stringHandle) |
||||
{ |
||||
CloseClipboard(); |
||||
|
||||
_glfwSetError(GLFW_PLATFORM_ERROR, |
||||
"Win32/WGL: Failed to retrieve clipboard data"); |
||||
return 0; |
||||
} |
||||
|
||||
utf8String = _glfwCreateUTF8FromWideString(GlobalLock(stringHandle)); |
||||
GlobalUnlock(stringHandle); |
||||
CloseClipboard(); |
||||
|
||||
if (!utf8String) |
||||
{ |
||||
_glfwSetError(GLFW_PLATFORM_ERROR, |
||||
"Win32/WGL: Failed to convert wide string to UTF-8"); |
||||
return 0; |
||||
} |
||||
|
||||
utf8Size = strlen(utf8String) + 1; |
||||
if (utf8Size > size) |
||||
{ |
||||
memcpy(string, utf8String, size); |
||||
string[size - 1] = '\0'; |
||||
} |
||||
else |
||||
memcpy(string, utf8String, utf8Size); |
||||
|
||||
free(utf8String); |
||||
return utf8Size; |
||||
} |
||||
|
@ -0,0 +1,201 @@ |
||||
//========================================================================
|
||||
// GLFW - An OpenGL library
|
||||
// Platform: X11/GLX
|
||||
// API version: 3.0
|
||||
// WWW: http://www.glfw.org/
|
||||
//------------------------------------------------------------------------
|
||||
// Copyright (c) 2010 Camilla Berglund <elmindreda@elmindreda.org>
|
||||
//
|
||||
// This software is provided 'as-is', without any express or implied
|
||||
// warranty. In no event will the authors be held liable for any damages
|
||||
// arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it
|
||||
// freely, subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented; you must not
|
||||
// claim that you wrote the original software. If you use this software
|
||||
// in a product, an acknowledgment in the product documentation would
|
||||
// be appreciated but is not required.
|
||||
//
|
||||
// 2. Altered source versions must be plainly marked as such, and must not
|
||||
// be misrepresented as being the original software.
|
||||
//
|
||||
// 3. This notice may not be removed or altered from any source
|
||||
// distribution.
|
||||
//
|
||||
//========================================================================
|
||||
|
||||
#include "internal.h" |
||||
|
||||
#include <stdio.h> |
||||
#include <limits.h> |
||||
#include <string.h> |
||||
#include <stdlib.h> |
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
////// GLFW internal API //////
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//========================================================================
|
||||
// Save the contents of the specified property
|
||||
//========================================================================
|
||||
|
||||
GLboolean _glfwReadSelection(XSelectionEvent* request) |
||||
{ |
||||
Atom actualType; |
||||
int actualFormat; |
||||
unsigned long itemCount, bytesAfter; |
||||
char* data; |
||||
|
||||
if (request->property == None) |
||||
return GL_FALSE; |
||||
|
||||
XGetWindowProperty(_glfwLibrary.X11.display, |
||||
request->requestor, |
||||
request->property, |
||||
0, LONG_MAX, |
||||
False, |
||||
request->target, |
||||
&actualType, |
||||
&actualFormat, |
||||
&itemCount, |
||||
&bytesAfter, |
||||
(unsigned char**) &data); |
||||
|
||||
if (actualType == None) |
||||
return GL_FALSE; |
||||
|
||||
free(_glfwLibrary.X11.selection.string); |
||||
_glfwLibrary.X11.selection.string = strdup(data); |
||||
|
||||
XFree(data); |
||||
return GL_TRUE; |
||||
} |
||||
|
||||
|
||||
//========================================================================
|
||||
// Set the specified property to the contents of the requested selection
|
||||
//========================================================================
|
||||
|
||||
Atom _glfwWriteSelection(XSelectionRequestEvent* request) |
||||
{ |
||||
int i; |
||||
Atom property = request->property; |
||||
|
||||
if (property == None) |
||||
property = _glfwLibrary.X11.selection.property; |
||||
|
||||
if (request->target == _glfwLibrary.X11.selection.targets) |
||||
{ |
||||
// The list of supported targets was requested
|
||||
|
||||
XChangeProperty(_glfwLibrary.X11.display, |
||||
request->requestor, |
||||
property, |
||||
XA_ATOM, |
||||
32, |
||||
PropModeReplace, |
||||
(unsigned char*) _glfwLibrary.X11.selection.formats, |
||||
_GLFW_CLIPBOARD_FORMAT_COUNT); |
||||
|
||||
return property; |
||||
} |
||||
|
||||
for (i = 0; i < _GLFW_CLIPBOARD_FORMAT_COUNT; i++) |
||||
{ |
||||
if (request->target == _glfwLibrary.X11.selection.formats[i]) |
||||
{ |
||||
// The requested format is one we support
|
||||
|
||||
XChangeProperty(_glfwLibrary.X11.display, |
||||
request->requestor, |
||||
property, |
||||
request->target, |
||||
8, |
||||
PropModeReplace, |
||||
(unsigned char*) _glfwLibrary.X11.selection.string, |
||||
strlen(_glfwLibrary.X11.selection.string)); |
||||
|
||||
return property; |
||||
} |
||||
} |
||||
|
||||
return None; |
||||
} |
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
////// GLFW platform API //////
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//========================================================================
|
||||
// Set the clipboard contents
|
||||
//========================================================================
|
||||
|
||||
void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string) |
||||
{ |
||||
// Store the new string in preparation for a selection request event
|
||||
free(_glfwLibrary.X11.selection.string); |
||||
_glfwLibrary.X11.selection.string = strdup(string); |
||||
|
||||
// Set the specified window as owner of the selection
|
||||
XSetSelectionOwner(_glfwLibrary.X11.display, |
||||
_glfwLibrary.X11.selection.atom, |
||||
window->X11.handle, CurrentTime); |
||||
} |
||||
|
||||
|
||||
//========================================================================
|
||||
// Return the current clipboard contents
|
||||
//========================================================================
|
||||
|
||||
size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t size) |
||||
{ |
||||
int i; |
||||
size_t sourceSize, targetSize; |
||||
|
||||
_glfwLibrary.X11.selection.status = _GLFW_CONVERSION_INACTIVE; |
||||
|
||||
for (i = 0; i < _GLFW_CLIPBOARD_FORMAT_COUNT; i++) |
||||
{ |
||||
// Request conversion to the selected format
|
||||
_glfwLibrary.X11.selection.target = |
||||
_glfwLibrary.X11.selection.formats[i]; |
||||
|
||||
XConvertSelection(_glfwLibrary.X11.display, |
||||
_glfwLibrary.X11.selection.atom, |
||||
_glfwLibrary.X11.selection.target, |
||||
_glfwLibrary.X11.selection.property, |
||||
window->X11.handle, CurrentTime); |
||||
|
||||
// Process the resulting SelectionNotify event
|
||||
XSync(_glfwLibrary.X11.display, False); |
||||
while (_glfwLibrary.X11.selection.status == _GLFW_CONVERSION_INACTIVE) |
||||
_glfwPlatformWaitEvents(); |
||||
|
||||
if (_glfwLibrary.X11.selection.status == _GLFW_CONVERSION_SUCCEEDED) |
||||
break; |
||||
} |
||||
|
||||
if (_glfwLibrary.X11.selection.status == _GLFW_CONVERSION_FAILED) |
||||
{ |
||||
_glfwSetError(GLFW_FORMAT_UNAVAILABLE, |
||||
"X11/GLX: Failed to convert selection to string"); |
||||
return 0; |
||||
} |
||||
|
||||
sourceSize = strlen(_glfwLibrary.X11.selection.string) + 1; |
||||
|
||||
targetSize = sourceSize; |
||||
if (targetSize > size) |
||||
targetSize = size; |
||||
|
||||
memcpy(string, _glfwLibrary.X11.selection.string, targetSize); |
||||
string[targetSize - 1] = '\0'; |
||||
|
||||
return sourceSize; |
||||
} |
||||
|
@ -0,0 +1,156 @@ |
||||
//========================================================================
|
||||
// Gamma correction test program
|
||||
// Copyright (c) Camilla Berglund <elmindreda@elmindreda.org>
|
||||
//
|
||||
// This software is provided 'as-is', without any express or implied
|
||||
// warranty. In no event will the authors be held liable for any damages
|
||||
// arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it
|
||||
// freely, subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented; you must not
|
||||
// claim that you wrote the original software. If you use this software
|
||||
// in a product, an acknowledgment in the product documentation would
|
||||
// be appreciated but is not required.
|
||||
//
|
||||
// 2. Altered source versions must be plainly marked as such, and must not
|
||||
// be misrepresented as being the original software.
|
||||
//
|
||||
// 3. This notice may not be removed or altered from any source
|
||||
// distribution.
|
||||
//
|
||||
//========================================================================
|
||||
//
|
||||
// This program is used to test the clipboard functionality.
|
||||
//
|
||||
//========================================================================
|
||||
|
||||
#include <GL/glfw3.h> |
||||
|
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
|
||||
#include "getopt.h" |
||||
|
||||
static void usage(void) |
||||
{ |
||||
printf("Usage: clipboard [-h]\n"); |
||||
} |
||||
|
||||
static GLboolean control_is_down(GLFWwindow window) |
||||
{ |
||||
return glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) || |
||||
glfwGetKey(window, GLFW_KEY_RIGHT_CONTROL); |
||||
} |
||||
|
||||
static void key_callback(GLFWwindow window, int key, int action) |
||||
{ |
||||
if (action != GLFW_PRESS) |
||||
return; |
||||
|
||||
switch (key) |
||||
{ |
||||
case GLFW_KEY_ESCAPE: |
||||
glfwCloseWindow(window); |
||||
break; |
||||
|
||||
case GLFW_KEY_V: |
||||
if (control_is_down(window)) |
||||
{ |
||||
char buffer[4096]; |
||||
size_t size; |
||||
|
||||
printf("Paste test.\n"); |
||||
|
||||
size = glfwGetClipboardString(window, buffer, sizeof(buffer)); |
||||
if (size >= sizeof(buffer)) |
||||
printf("Buffer wasn't big enough to hold clipboard data.\n"); |
||||
|
||||
printf("[%lu]: %s\n", (unsigned long) size, buffer); |
||||
} |
||||
break; |
||||
|
||||
case GLFW_KEY_C: |
||||
if (control_is_down(window)) |
||||
{ |
||||
const char* string = "Hello GLFW World!"; |
||||
glfwSetClipboardString(window, string); |
||||
printf("Setting clipboard to: %s\n", string); |
||||
} |
||||
break; |
||||
} |
||||
} |
||||
|
||||
static void size_callback(GLFWwindow window, int width, int height) |
||||
{ |
||||
glViewport(0, 0, width, height); |
||||
} |
||||
|
||||
static void error_callback(int error, const char* description) |
||||
{ |
||||
fprintf(stderr, "Error: %s in %s\n", glfwErrorString(error), description); |
||||
} |
||||
|
||||
int main(int argc, char** argv) |
||||
{ |
||||
int ch; |
||||
GLFWwindow window; |
||||
|
||||
while ((ch = getopt(argc, argv, "h")) != -1) |
||||
{ |
||||
switch (ch) |
||||
{ |
||||
case 'h': |
||||
usage(); |
||||
exit(EXIT_SUCCESS); |
||||
|
||||
default: |
||||
usage(); |
||||
exit(EXIT_FAILURE); |
||||
} |
||||
} |
||||
|
||||
glfwSetErrorCallback(error_callback); |
||||
|
||||
if (!glfwInit()) |
||||
{ |
||||
fprintf(stderr, "Failed to initialize GLFW: %s\n", glfwErrorString(glfwGetError())); |
||||
exit(EXIT_FAILURE); |
||||
} |
||||
|
||||
window = glfwOpenWindow(0, 0, GLFW_WINDOWED, "Clipboard Test", NULL); |
||||
if (!window) |
||||
{ |
||||
glfwTerminate(); |
||||
|
||||
fprintf(stderr, "Failed to open GLFW window: %s\n", glfwErrorString(glfwGetError())); |
||||
exit(EXIT_FAILURE); |
||||
} |
||||
|
||||
glfwSwapInterval(1); |
||||
glfwSetKeyCallback(key_callback); |
||||
glfwSetWindowSizeCallback(size_callback); |
||||
|
||||
glMatrixMode(GL_PROJECTION); |
||||
glOrtho(-1.f, 1.f, -1.f, 1.f, -1.f, 1.f); |
||||
glMatrixMode(GL_MODELVIEW); |
||||
|
||||
glClearColor(0.5f, 0.5f, 0.5f, 0); |
||||
|
||||
while (glfwIsWindow(window)) |
||||
{ |
||||
glClear(GL_COLOR_BUFFER_BIT); |
||||
|
||||
glColor3f(0.8f, 0.2f, 0.4f); |
||||
glRectf(-0.5f, -0.5f, 0.5f, 0.5f); |
||||
|
||||
glfwSwapBuffers(); |
||||
glfwPollEvents(); |
||||
} |
||||
|
||||
glfwTerminate(); |
||||
exit(EXIT_SUCCESS); |
||||
} |
||||
|
Loading…
Reference in New Issue