From ded94b475d643f3e144f677ae6c01bbe5f9a73ce Mon Sep 17 00:00:00 2001 From: jacob1 Date: Fri, 18 Jan 2013 14:37:24 -0500 Subject: [PATCH] move clipboard functions out of misc.cpp without creating errors --- SConscript | 2 +- SConstruct | 2 +- src/Misc.cpp | 141 ------------------------------------ src/Misc.h | 11 --- src/PowderToy.h | 4 +- src/PowderToySDL.cpp | 131 ++++++++++++++++++++++++++++++++- src/interface/Label.cpp | 6 +- src/interface/Label.h | 2 +- src/interface/Textbox.cpp | 8 +- src/interface/Textbox.h | 2 +- src/preview/PreviewView.cpp | 2 +- 11 files changed, 144 insertions(+), 167 deletions(-) diff --git a/SConscript b/SConscript index 8b62dc76b..e1bdede81 100644 --- a/SConscript +++ b/SConscript @@ -164,7 +164,7 @@ if(GetOption('lin')): env.ParseConfig('pkg-config --libs glew gl glu') openGLLibs = ['GL'] env.Append(LIBS=['X11', 'rt']) - env.Append(CPPDEFINES=["LIN","SDL_VIDEO_DRIVER_X11"]) + env.Append(CPPDEFINES=["LIN"]) if GetOption('_64bit'): env.Append(LINKFLAGS=['-m64']) env.Append(CCFLAGS=['-m64']) diff --git a/SConstruct b/SConstruct index 769d58d9b..1360e9fa4 100644 --- a/SConstruct +++ b/SConstruct @@ -1 +1 @@ -SConscript('SConscript', variant_dir='build') +SConscript('SConscript', variant_dir='build', duplicate=0) diff --git a/src/Misc.cpp b/src/Misc.cpp index 830f0ca3a..8c1887183 100644 --- a/src/Misc.cpp +++ b/src/Misc.cpp @@ -49,15 +49,6 @@ std::string URLEscape(std::string source) return finalString; } -#if defined(USE_SDL) && defined(LIN) && defined(SDL_VIDEO_DRIVER_X11) -#include -#include -SDL_SysWMinfo sdl_wminfo; -Atom XA_CLIPBOARD, XA_TARGETS; -#endif - -char *clipboardText = NULL; - char *exe_name(void) { #if defined(WIN) @@ -368,138 +359,6 @@ vector2d v2d_new(float x, float y) return result; } -void clipboard_init() -{ -#if defined (USE_SDL) && defined(LIN) && defined(SDL_VIDEO_DRIVER_X11) - SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE); - SDL_VERSION(&sdl_wminfo.version); - SDL_GetWMInfo(&sdl_wminfo); - sdl_wminfo.info.x11.lock_func(); - XA_CLIPBOARD = XInternAtom(sdl_wminfo.info.x11.display, "CLIPBOARD", 1); - XA_TARGETS = XInternAtom(sdl_wminfo.info.x11.display, "TARGETS", 1); - sdl_wminfo.info.x11.unlock_func(); -#endif -} - -#if defined (USE_SDL) && defined(LIN) && defined(SDL_VIDEO_DRIVER_X11) -void clipboard_event(SDL_Event event) -{ - if (event.syswm.msg->subsystem != SDL_SYSWM_X11) - return; - sdl_wminfo.info.x11.lock_func(); - XEvent xe = event.syswm.msg->event.xevent; - if (xe.type==SelectionClear) - { - if (clipboardText != NULL) { - free(clipboardText); - clipboardText = NULL; - } - } - else if (xe.type==SelectionRequest) - { - XEvent xr; - xr.xselection.type = SelectionNotify; - xr.xselection.requestor = xe.xselectionrequest.requestor; - xr.xselection.selection = xe.xselectionrequest.selection; - xr.xselection.target = xe.xselectionrequest.target; - xr.xselection.property = xe.xselectionrequest.property; - xr.xselection.time = xe.xselectionrequest.time; - if (xe.xselectionrequest.target==XA_TARGETS) - { - // send list of supported formats - Atom targets[] = {XA_TARGETS, XA_STRING}; - xr.xselection.property = xe.xselectionrequest.property; - XChangeProperty(sdl_wminfo.info.x11.display, xe.xselectionrequest.requestor, xe.xselectionrequest.property, XA_ATOM, 32, PropModeReplace, (unsigned char*)targets, (int)(sizeof(targets)/sizeof(Atom))); - } - // TODO: Supporting more targets would be nice - else if (xe.xselectionrequest.target==XA_STRING && clipboardText) - { - XChangeProperty(sdl_wminfo.info.x11.display, xe.xselectionrequest.requestor, xe.xselectionrequest.property, xe.xselectionrequest.target, 8, PropModeReplace, (unsigned char*)clipboardText, strlen(clipboardText)+1); - } - else - { - // refuse clipboard request - xr.xselection.property = None; - } - XSendEvent(sdl_wminfo.info.x11.display, xe.xselectionrequest.requestor, 0, 0, &xr); - } - sdl_wminfo.info.x11.unlock_func(); -} -#endif - -void clipboard_push_text(char * text) -{ - if (clipboardText != NULL) { - free(clipboardText); - clipboardText = NULL; - } - clipboardText = mystrdup(text); -#ifdef MACOSX - PasteboardRef newclipboard; - - if (PasteboardCreate(kPasteboardClipboard, &newclipboard)!=noErr) return; - if (PasteboardClear(newclipboard)!=noErr) return; - PasteboardSynchronize(newclipboard); - - CFDataRef data = CFDataCreate(kCFAllocatorDefault, (const UInt8*)text, strlen(text)); - PasteboardPutItemFlavor(newclipboard, (PasteboardItemID)1, CFSTR("com.apple.traditional-mac-plain-text"), data, 0); -#elif defined(WIN) - if (OpenClipboard(NULL)) - { - HGLOBAL cbuffer; - char * glbuffer; - - EmptyClipboard(); - - cbuffer = GlobalAlloc(GMEM_DDESHARE, strlen(text)+1); - glbuffer = (char*)GlobalLock(cbuffer); - - strcpy(glbuffer, text); - - GlobalUnlock(cbuffer); - SetClipboardData(CF_TEXT, cbuffer); - CloseClipboard(); - } -#elif defined(LIN) && defined(SDL_VIDEO_DRIVER_X11) - sdl_wminfo.info.x11.lock_func(); - XSetSelectionOwner(sdl_wminfo.info.x11.display, XA_CLIPBOARD, sdl_wminfo.info.x11.window, CurrentTime); - XFlush(sdl_wminfo.info.x11.display); - sdl_wminfo.info.x11.unlock_func(); -#else - printf("Not implemented: put text on clipboard \"%s\"\n", text); -#endif -} - -char * clipboard_pull_text() -{ -#ifdef MACOSX - printf("Not implemented: get text from clipboard\n"); -#elif defined(WIN) - if (OpenClipboard(NULL)) - { - HANDLE cbuffer; - char * glbuffer; - - cbuffer = GetClipboardData(CF_TEXT); - glbuffer = (char*)GlobalLock(cbuffer); - GlobalUnlock(cbuffer); - CloseClipboard(); - if(glbuffer!=NULL){ - return mystrdup(glbuffer); - }// else { - // return mystrdup(""); - //} - } -#elif defined(LIN) && defined(SDL_VIDEO_DRIVER_X11) - printf("Not implemented: get text from clipboard\n"); -#else - printf("Not implemented: get text from clipboard\n"); -#endif - if (clipboardText) - return mystrdup(clipboardText); - return mystrdup(""); -} - int register_extension() { #if defined(WIN) diff --git a/src/Misc.h b/src/Misc.h index 382dcccdb..1a43fe34b 100644 --- a/src/Misc.h +++ b/src/Misc.h @@ -74,17 +74,6 @@ void strappend(char *dst, char *src); void *file_load(char *fn, int *size); -void clipboard_init(); - -#if defined (USE_SDL) && defined(LIN) && defined(SDL_VIDEO_DRIVER_X11) -#include -void clipboard_event(SDL_Event event); -#endif - -void clipboard_push_text(char * text); - -char * clipboard_pull_text(); - extern char *clipboard_text; int register_extension(); diff --git a/src/PowderToy.h b/src/PowderToy.h index 2e8cb3687..9141dbb24 100644 --- a/src/PowderToy.h +++ b/src/PowderToy.h @@ -1,3 +1,5 @@ #pragma once -void EngineProcess(); \ No newline at end of file +void EngineProcess(); +void ClipboardPush(char * text); +char * ClipboardPull(); diff --git a/src/PowderToySDL.cpp b/src/PowderToySDL.cpp index fc35cf39d..d1eb62a55 100644 --- a/src/PowderToySDL.cpp +++ b/src/PowderToySDL.cpp @@ -45,6 +45,15 @@ using namespace std; +#if defined(USE_SDL) && defined(LIN) +#include +#endif +#if defined(USE_SDL) && defined(LIN) && defined(SDL_VIDEO_DRIVER_X11) +SDL_SysWMinfo sdl_wminfo; +Atom XA_CLIPBOARD, XA_TARGETS; +#endif + +char *clipboardText = NULL; #ifdef WIN extern "C" IMAGE_DOS_HEADER __ImageBase; @@ -56,6 +65,79 @@ SDL_Surface * sdl_scrn; int scale = 1; bool fullscreen = false; +void ClipboardPush(char * text) +{ + if (clipboardText != NULL) { + free(clipboardText); + clipboardText = NULL; + } + clipboardText = mystrdup(text); +#ifdef MACOSX + PasteboardRef newclipboard; + + if (PasteboardCreate(kPasteboardClipboard, &newclipboard)!=noErr) return; + if (PasteboardClear(newclipboard)!=noErr) return; + PasteboardSynchronize(newclipboard); + + CFDataRef data = CFDataCreate(kCFAllocatorDefault, (const UInt8*)text, strlen(text)); + PasteboardPutItemFlavor(newclipboard, (PasteboardItemID)1, CFSTR("com.apple.traditional-mac-plain-text"), data, 0); +#elif defined(WIN) + if (OpenClipboard(NULL)) + { + HGLOBAL cbuffer; + char * glbuffer; + + EmptyClipboard(); + + cbuffer = GlobalAlloc(GMEM_DDESHARE, strlen(text)+1); + glbuffer = (char*)GlobalLock(cbuffer); + + strcpy(glbuffer, text); + + GlobalUnlock(cbuffer); + SetClipboardData(CF_TEXT, cbuffer); + CloseClipboard(); + } +#elif defined(LIN) && defined(SDL_VIDEO_DRIVER_X11) + sdl_wminfo.info.x11.lock_func(); + XSetSelectionOwner(sdl_wminfo.info.x11.display, XA_CLIPBOARD, sdl_wminfo.info.x11.window, CurrentTime); + XFlush(sdl_wminfo.info.x11.display); + sdl_wminfo.info.x11.unlock_func(); +#else + printf("Not implemented: put text on clipboard \"%s\"\n", text); +#endif +} + +char * ClipboardPull() +{ +#ifdef MACOSX + printf("Not implemented: get text from clipboard\n"); +#elif defined(WIN) + if (OpenClipboard(NULL)) + { + HANDLE cbuffer; + char * glbuffer; + + cbuffer = GetClipboardData(CF_TEXT); + glbuffer = (char*)GlobalLock(cbuffer); + GlobalUnlock(cbuffer); + CloseClipboard(); + if(glbuffer!=NULL){ + return mystrdup(glbuffer); + }// else { + // return mystrdup(""); + //} + } +#elif defined(LIN) && defined(SDL_VIDEO_DRIVER_X11) + printf("Not implemented: get text from clipboard\n"); +#else + printf("Not implemented: get text from clipboard\n"); +#endif + if (clipboardText) + return mystrdup(clipboardText); + return mystrdup(""); +} + #ifdef OGLI void blit() { @@ -364,7 +446,46 @@ void EngineProcess() #endif #if defined (USE_SDL) && defined(LIN) && defined(SDL_VIDEO_DRIVER_X11) case SDL_SYSWMEVENT: - clipboard_event(event); + if (event.syswm.msg->subsystem != SDL_SYSWM_X11) + break; + sdl_wminfo.info.x11.lock_func(); + XEvent xe = event.syswm.msg->event.xevent; + if (xe.type==SelectionClear) + { + if (clipboardText != NULL) { + free(clipboardText); + clipboardText = NULL; + } + } + else if (xe.type==SelectionRequest) + { + XEvent xr; + xr.xselection.type = SelectionNotify; + xr.xselection.requestor = xe.xselectionrequest.requestor; + xr.xselection.selection = xe.xselectionrequest.selection; + xr.xselection.target = xe.xselectionrequest.target; + xr.xselection.property = xe.xselectionrequest.property; + xr.xselection.time = xe.xselectionrequest.time; + if (xe.xselectionrequest.target==XA_TARGETS) + { + // send list of supported formats + Atom targets[] = {XA_TARGETS, XA_STRING}; + xr.xselection.property = xe.xselectionrequest.property; + XChangeProperty(sdl_wminfo.info.x11.display, xe.xselectionrequest.requestor, xe.xselectionrequest.property, XA_ATOM, 32, PropModeReplace, (unsigned char*)targets, (int)(sizeof(targets)/sizeof(Atom))); + } + // TODO: Supporting more targets would be nice + else if (xe.xselectionrequest.target==XA_STRING && clipboardText) + { + XChangeProperty(sdl_wminfo.info.x11.display, xe.xselectionrequest.requestor, xe.xselectionrequest.property, xe.xselectionrequest.target, 8, PropModeReplace, (unsigned char*)clipboardText, strlen(clipboardText)+1); + } + else + { + // refuse clipboard request + xr.xselection.property = None; + } + XSendEvent(sdl_wminfo.info.x11.display, xe.xselectionrequest.requestor, 0, 0, &xr); + } + sdl_wminfo.info.x11.unlock_func(); continue; #endif } @@ -491,7 +612,13 @@ int main(int argc, char * argv[]) exit(-1); } #endif - clipboard_init(); + SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE); + SDL_VERSION(&sdl_wminfo.version); + SDL_GetWMInfo(&sdl_wminfo); + sdl_wminfo.info.x11.lock_func(); + XA_CLIPBOARD = XInternAtom(sdl_wminfo.info.x11.display, "CLIPBOARD", 1); + XA_TARGETS = XInternAtom(sdl_wminfo.info.x11.display, "TARGETS", 1); + sdl_wminfo.info.x11.unlock_func(); ui::Engine::Ref().g = new Graphics(); ui::Engine::Ref().Scale = scale; inputScale = 1.0f/float(scale); diff --git a/src/interface/Label.cpp b/src/interface/Label.cpp index 5cd089c18..24b8035b6 100644 --- a/src/interface/Label.cpp +++ b/src/interface/Label.cpp @@ -211,11 +211,11 @@ void Label::copySelection() std::string currentText = text; if(selectionIndex1 > selectionIndex0) { - clipboard_push_text((char*)currentText.substr(selectionIndex0, selectionIndex1-selectionIndex0).c_str()); + ClipboardPush((char*)currentText.substr(selectionIndex0, selectionIndex1-selectionIndex0).c_str()); } else if(selectionIndex0 > selectionIndex1) { - clipboard_push_text((char*)currentText.substr(selectionIndex1, selectionIndex0-selectionIndex1).c_str()); + ClipboardPush((char*)currentText.substr(selectionIndex1, selectionIndex0-selectionIndex1).c_str()); } else { - clipboard_push_text((char*)currentText.c_str()); + ClipboardPush((char*)currentText.c_str()); } } diff --git a/src/interface/Label.h b/src/interface/Label.h index f5fa1a7f1..d4e5088b6 100644 --- a/src/interface/Label.h +++ b/src/interface/Label.h @@ -4,7 +4,7 @@ #include #include "Component.h" -#include "Misc.h" +#include "PowderToy.h" #include "Colour.h" namespace ui diff --git a/src/interface/Textbox.cpp b/src/interface/Textbox.cpp index c82b6e417..603c894bf 100644 --- a/src/interface/Textbox.cpp +++ b/src/interface/Textbox.cpp @@ -125,20 +125,20 @@ void Textbox::OnContextMenuAction(int item) void Textbox::cutSelection() { char * clipboardText; - clipboardText = clipboard_pull_text(); + clipboardText = ClipboardPull(); std::string newText = std::string(clipboardText); free(clipboardText); if(HasSelection()) { if(getLowerSelectionBound() < 0 || getHigherSelectionBound() > backingText.length()) return; - clipboard_push_text((char*)backingText.substr(getLowerSelectionBound(), getHigherSelectionBound()-getLowerSelectionBound()).c_str()); + ClipboardPush((char*)backingText.substr(getLowerSelectionBound(), getHigherSelectionBound()-getLowerSelectionBound()).c_str()); backingText.erase(backingText.begin()+getLowerSelectionBound(), backingText.begin()+getHigherSelectionBound()); cursor = getLowerSelectionBound(); } else { - clipboard_push_text((char*)backingText.c_str()); + ClipboardPush((char*)backingText.c_str()); backingText.clear(); } ClearSelection(); @@ -181,7 +181,7 @@ void Textbox::selectAll() void Textbox::pasteIntoSelection() { char * clipboardText; - clipboardText = clipboard_pull_text(); + clipboardText = ClipboardPull(); std::string newText = std::string(clipboardText); free(clipboardText); if(HasSelection()) diff --git a/src/interface/Textbox.h b/src/interface/Textbox.h index 7d06111ef..0d5140b0d 100644 --- a/src/interface/Textbox.h +++ b/src/interface/Textbox.h @@ -4,7 +4,7 @@ #include #include "Label.h" -#include "Misc.h" +#include "PowderToy.h" namespace ui { diff --git a/src/preview/PreviewView.cpp b/src/preview/PreviewView.cpp index 95066ebed..238e4cc1a 100644 --- a/src/preview/PreviewView.cpp +++ b/src/preview/PreviewView.cpp @@ -179,7 +179,7 @@ PreviewView::PreviewView(): CopyIDAction(PreviewView * v_){ v = v_; } virtual void ActionCallback(ui::Button * sender) { - clipboard_push_text((char*)v->saveIDTextbox->GetText().c_str()); + ClipboardPush((char*)v->saveIDTextbox->GetText().c_str()); } };