iOS: Implemented clipboard support.
authorAlex Szpakowski <slime73@gmail.com>
Wed, 03 Feb 2016 20:32:55 -0400
changeset 10062c31b24c7babc
parent 10061 698a7d8626b9
child 10063 c5e3a4f88be9
iOS: Implemented clipboard support.
Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj
src/video/uikit/SDL_uikitclipboard.h
src/video/uikit/SDL_uikitclipboard.m
src/video/uikit/SDL_uikitvideo.h
src/video/uikit/SDL_uikitvideo.m
     1.1 --- a/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj	Sun Jan 31 21:11:15 2016 -0800
     1.2 +++ b/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj	Wed Feb 03 20:32:55 2016 -0400
     1.3 @@ -143,7 +143,9 @@
     1.4  		AABCC3941640643D00AB8930 /* SDL_uikitmessagebox.h in Headers */ = {isa = PBXBuildFile; fileRef = AABCC3921640643D00AB8930 /* SDL_uikitmessagebox.h */; };
     1.5  		AABCC3951640643D00AB8930 /* SDL_uikitmessagebox.m in Sources */ = {isa = PBXBuildFile; fileRef = AABCC3931640643D00AB8930 /* SDL_uikitmessagebox.m */; };
     1.6  		AADA5B8F16CCAB7C00107CF7 /* SDL_bits.h in Headers */ = {isa = PBXBuildFile; fileRef = AADA5B8E16CCAB7C00107CF7 /* SDL_bits.h */; };
     1.7 -		FAD4F7021BA3C4E8008346CE /* SDL_sysjoystick_c.h in Headers */ = {isa = PBXBuildFile; fileRef = FAD4F7011BA3C4E8008346CE /* SDL_sysjoystick_c.h */; settings = {ASSET_TAGS = (); }; };
     1.8 +		FA1DC2721C62BE65008F99A0 /* SDL_uikitclipboard.h in Headers */ = {isa = PBXBuildFile; fileRef = FA1DC2701C62BE65008F99A0 /* SDL_uikitclipboard.h */; };
     1.9 +		FA1DC2731C62BE65008F99A0 /* SDL_uikitclipboard.m in Sources */ = {isa = PBXBuildFile; fileRef = FA1DC2711C62BE65008F99A0 /* SDL_uikitclipboard.m */; };
    1.10 +		FAD4F7021BA3C4E8008346CE /* SDL_sysjoystick_c.h in Headers */ = {isa = PBXBuildFile; fileRef = FAD4F7011BA3C4E8008346CE /* SDL_sysjoystick_c.h */; };
    1.11  		FD3F4A760DEA620800C5B771 /* SDL_getenv.c in Sources */ = {isa = PBXBuildFile; fileRef = FD3F4A700DEA620800C5B771 /* SDL_getenv.c */; };
    1.12  		FD3F4A770DEA620800C5B771 /* SDL_iconv.c in Sources */ = {isa = PBXBuildFile; fileRef = FD3F4A710DEA620800C5B771 /* SDL_iconv.c */; };
    1.13  		FD3F4A780DEA620800C5B771 /* SDL_malloc.c in Sources */ = {isa = PBXBuildFile; fileRef = FD3F4A720DEA620800C5B771 /* SDL_malloc.c */; };
    1.14 @@ -340,6 +342,8 @@
    1.15  		AABCC3921640643D00AB8930 /* SDL_uikitmessagebox.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_uikitmessagebox.h; sourceTree = "<group>"; };
    1.16  		AABCC3931640643D00AB8930 /* SDL_uikitmessagebox.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_uikitmessagebox.m; sourceTree = "<group>"; };
    1.17  		AADA5B8E16CCAB7C00107CF7 /* SDL_bits.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_bits.h; sourceTree = "<group>"; };
    1.18 +		FA1DC2701C62BE65008F99A0 /* SDL_uikitclipboard.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_uikitclipboard.h; sourceTree = "<group>"; };
    1.19 +		FA1DC2711C62BE65008F99A0 /* SDL_uikitclipboard.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_uikitclipboard.m; sourceTree = "<group>"; };
    1.20  		FAD4F7011BA3C4E8008346CE /* SDL_sysjoystick_c.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_sysjoystick_c.h; sourceTree = "<group>"; };
    1.21  		FD0BBFEF0E3933DD00D833B1 /* SDL_uikitview.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_uikitview.h; sourceTree = "<group>"; };
    1.22  		FD3F4A700DEA620800C5B771 /* SDL_getenv.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_getenv.c; sourceTree = "<group>"; };
    1.23 @@ -648,6 +652,8 @@
    1.24  				FDC261780E3A3FC8001C4554 /* keyinfotable.h */,
    1.25  				FD689FCD0E26E9D400F90B21 /* SDL_uikitappdelegate.h */,
    1.26  				FD689FCC0E26E9D400F90B21 /* SDL_uikitappdelegate.m */,
    1.27 +				FA1DC2701C62BE65008F99A0 /* SDL_uikitclipboard.h */,
    1.28 +				FA1DC2711C62BE65008F99A0 /* SDL_uikitclipboard.m */,
    1.29  				FD689F0C0E26E5D900F90B21 /* SDL_uikitevents.h */,
    1.30  				FD689F0D0E26E5D900F90B21 /* SDL_uikitevents.m */,
    1.31  				AABCC3921640643D00AB8930 /* SDL_uikitmessagebox.h */,
    1.32 @@ -994,6 +1000,7 @@
    1.33  				04F7808012FB751400FC43C0 /* SDL_drawpoint.h in Headers */,
    1.34  				04F7808412FB753F00FC43C0 /* SDL_nullframebuffer_c.h in Headers */,
    1.35  				0442EC5012FE1C1E004C9285 /* SDL_render_sw_c.h in Headers */,
    1.36 +				FA1DC2721C62BE65008F99A0 /* SDL_uikitclipboard.h in Headers */,
    1.37  				0402A85A12FE70C600CECEE3 /* SDL_shaders_gles2.h in Headers */,
    1.38  				04BAC09C1300C1290055DE28 /* SDL_assert_c.h in Headers */,
    1.39  				56EA86FC13E9EC2B002E47EB /* SDL_coreaudio.h in Headers */,
    1.40 @@ -1139,6 +1146,7 @@
    1.41  				FD65266A0DE8FCDD002AD96B /* SDL_audiotypecvt.c in Sources */,
    1.42  				FD65266B0DE8FCDD002AD96B /* SDL_mixer.c in Sources */,
    1.43  				FD65266F0DE8FCDD002AD96B /* SDL_wave.c in Sources */,
    1.44 +				FA1DC2731C62BE65008F99A0 /* SDL_uikitclipboard.m in Sources */,
    1.45  				FD6526700DE8FCDD002AD96B /* SDL_cpuinfo.c in Sources */,
    1.46  				FD6526710DE8FCDD002AD96B /* SDL_events.c in Sources */,
    1.47  				FD6526720DE8FCDD002AD96B /* SDL_keyboard.c in Sources */,
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/video/uikit/SDL_uikitclipboard.h	Wed Feb 03 20:32:55 2016 -0400
     2.3 @@ -0,0 +1,35 @@
     2.4 +/*
     2.5 + Simple DirectMedia Layer
     2.6 + Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
     2.7 +
     2.8 + This software is provided 'as-is', without any express or implied
     2.9 + warranty.  In no event will the authors be held liable for any damages
    2.10 + arising from the use of this software.
    2.11 +
    2.12 + Permission is granted to anyone to use this software for any purpose,
    2.13 + including commercial applications, and to alter it and redistribute it
    2.14 + freely, subject to the following restrictions:
    2.15 +
    2.16 + 1. The origin of this software must not be misrepresented; you must not
    2.17 + claim that you wrote the original software. If you use this software
    2.18 + in a product, an acknowledgment in the product documentation would be
    2.19 + appreciated but is not required.
    2.20 + 2. Altered source versions must be plainly marked as such, and must not be
    2.21 + misrepresented as being the original software.
    2.22 + 3. This notice may not be removed or altered from any source distribution.
    2.23 + */
    2.24 +#ifndef _SDL_uikitclipboard_h
    2.25 +#define _SDL_uikitclipboard_h
    2.26 +
    2.27 +#include "../SDL_sysvideo.h"
    2.28 +
    2.29 +extern int UIKit_SetClipboardText(_THIS, const char *text);
    2.30 +extern char *UIKit_GetClipboardText(_THIS);
    2.31 +extern SDL_bool UIKit_HasClipboardText(_THIS);
    2.32 +
    2.33 +extern void UIKit_InitClipboard(_THIS);
    2.34 +extern void UIKit_QuitClipboard(_THIS);
    2.35 +
    2.36 +#endif /* _SDL_uikitclipboard_h */
    2.37 +
    2.38 +/* vi: set ts=4 sw=4 expandtab: */
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/video/uikit/SDL_uikitclipboard.m	Wed Feb 03 20:32:55 2016 -0400
     3.3 @@ -0,0 +1,99 @@
     3.4 +/*
     3.5 + Simple DirectMedia Layer
     3.6 + Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
     3.7 +
     3.8 + This software is provided 'as-is', without any express or implied
     3.9 + warranty.  In no event will the authors be held liable for any damages
    3.10 + arising from the use of this software.
    3.11 +
    3.12 + Permission is granted to anyone to use this software for any purpose,
    3.13 + including commercial applications, and to alter it and redistribute it
    3.14 + freely, subject to the following restrictions:
    3.15 +
    3.16 + 1. The origin of this software must not be misrepresented; you must not
    3.17 + claim that you wrote the original software. If you use this software
    3.18 + in a product, an acknowledgment in the product documentation would be
    3.19 + appreciated but is not required.
    3.20 + 2. Altered source versions must be plainly marked as such, and must not be
    3.21 + misrepresented as being the original software.
    3.22 + 3. This notice may not be removed or altered from any source distribution.
    3.23 + */
    3.24 +#include "../../SDL_internal.h"
    3.25 +
    3.26 +#if SDL_VIDEO_DRIVER_UIKIT
    3.27 +
    3.28 +#include "SDL_uikitvideo.h"
    3.29 +#include "../../events/SDL_clipboardevents_c.h"
    3.30 +
    3.31 +#import <UIKit/UIPasteboard.h>
    3.32 +
    3.33 +int
    3.34 +UIKit_SetClipboardText(_THIS, const char *text)
    3.35 +{
    3.36 +    @autoreleasepool {
    3.37 +        [UIPasteboard generalPasteboard].string = @(text);
    3.38 +        return 0;
    3.39 +    }
    3.40 +}
    3.41 +
    3.42 +char *
    3.43 +UIKit_GetClipboardText(_THIS)
    3.44 +{
    3.45 +    @autoreleasepool {
    3.46 +        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    3.47 +        NSString *string = pasteboard.string;
    3.48 +
    3.49 +        if (string != nil) {
    3.50 +            return SDL_strdup(string.UTF8String);
    3.51 +        } else {
    3.52 +            return SDL_strdup("");
    3.53 +        }
    3.54 +    }
    3.55 +}
    3.56 +
    3.57 +SDL_bool
    3.58 +UIKit_HasClipboardText(_THIS)
    3.59 +{
    3.60 +    @autoreleasepool {
    3.61 +        if ([UIPasteboard generalPasteboard].string != nil) {
    3.62 +            return SDL_TRUE;
    3.63 +        }
    3.64 +        return SDL_FALSE;
    3.65 +    }
    3.66 +}
    3.67 +
    3.68 +void
    3.69 +UIKit_InitClipboard(_THIS)
    3.70 +{
    3.71 +    @autoreleasepool {
    3.72 +        SDL_VideoData *data = (__bridge SDL_VideoData *) _this->driverdata;
    3.73 +        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    3.74 +
    3.75 +        id observer = [center addObserverForName:UIPasteboardChangedNotification
    3.76 +                                         object:nil
    3.77 +                                          queue:nil
    3.78 +                                     usingBlock:^(NSNotification *note) {
    3.79 +                                         SDL_SendClipboardUpdate();
    3.80 +                                     }];
    3.81 +
    3.82 +        data.pasteboardObserver = observer;
    3.83 +    }
    3.84 +}
    3.85 +
    3.86 +void
    3.87 +UIKit_QuitClipboard(_THIS)
    3.88 +{
    3.89 +    @autoreleasepool {
    3.90 +        SDL_VideoData *data = (__bridge SDL_VideoData *) _this->driverdata;
    3.91 +
    3.92 +        if (data.pasteboardObserver != nil) {
    3.93 +            [[NSNotificationCenter defaultCenter] removeObserver:data.pasteboardObserver];
    3.94 +        }
    3.95 +
    3.96 +        data.pasteboardObserver = nil;
    3.97 +    }
    3.98 +}
    3.99 +
   3.100 +#endif /* SDL_VIDEO_DRIVER_UIKIT */
   3.101 +
   3.102 +/* vi: set ts=4 sw=4 expandtab: */
     4.1 --- a/src/video/uikit/SDL_uikitvideo.h	Sun Jan 31 21:11:15 2016 -0800
     4.2 +++ b/src/video/uikit/SDL_uikitvideo.h	Wed Feb 03 20:32:55 2016 -0400
     4.3 @@ -25,6 +25,12 @@
     4.4  
     4.5  #include "../SDL_sysvideo.h"
     4.6  
     4.7 +@interface SDL_VideoData : NSObject
     4.8 +
     4.9 +@property (nonatomic) id pasteboardObserver;
    4.10 +
    4.11 +@end
    4.12 +
    4.13  void UIKit_SuspendScreenSaver(_THIS);
    4.14  
    4.15  BOOL UIKit_IsSystemVersionAtLeast(double version);
     5.1 --- a/src/video/uikit/SDL_uikitvideo.m	Sun Jan 31 21:11:15 2016 -0800
     5.2 +++ b/src/video/uikit/SDL_uikitvideo.m	Wed Feb 03 20:32:55 2016 -0400
     5.3 @@ -36,9 +36,14 @@
     5.4  #include "SDL_uikitmodes.h"
     5.5  #include "SDL_uikitwindow.h"
     5.6  #include "SDL_uikitopengles.h"
     5.7 +#include "SDL_uikitclipboard.h"
     5.8  
     5.9  #define UIKITVID_DRIVER_NAME "uikit"
    5.10  
    5.11 +@implementation SDL_VideoData
    5.12 +
    5.13 +@end
    5.14 +
    5.15  /* Initialization/Query functions */
    5.16  static int UIKit_VideoInit(_THIS);
    5.17  static void UIKit_VideoQuit(_THIS);
    5.18 @@ -53,61 +58,75 @@
    5.19  
    5.20  static void UIKit_DeleteDevice(SDL_VideoDevice * device)
    5.21  {
    5.22 -    SDL_free(device);
    5.23 +    @autoreleasepool {
    5.24 +        CFRelease(device->driverdata);
    5.25 +        SDL_free(device);
    5.26 +    }
    5.27  }
    5.28  
    5.29  static SDL_VideoDevice *
    5.30  UIKit_CreateDevice(int devindex)
    5.31  {
    5.32 -    SDL_VideoDevice *device;
    5.33 +    @autoreleasepool {
    5.34 +        SDL_VideoDevice *device;
    5.35 +        SDL_VideoData *data;
    5.36  
    5.37 -    /* Initialize all variables that we clean on shutdown */
    5.38 -    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    5.39 -    if (!device) {
    5.40 -        SDL_free(device);
    5.41 -        SDL_OutOfMemory();
    5.42 -        return (0);
    5.43 -    }
    5.44 +        /* Initialize all variables that we clean on shutdown */
    5.45 +        device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    5.46 +        if (device) {
    5.47 +            data = [SDL_VideoData new];
    5.48 +        } else {
    5.49 +            SDL_free(device);
    5.50 +            SDL_OutOfMemory();
    5.51 +            return (0);
    5.52 +        }
    5.53 +
    5.54 +        device->driverdata = (void *) CFBridgingRetain(data);
    5.55  
    5.56 -    /* Set the function pointers */
    5.57 -    device->VideoInit = UIKit_VideoInit;
    5.58 -    device->VideoQuit = UIKit_VideoQuit;
    5.59 -    device->GetDisplayModes = UIKit_GetDisplayModes;
    5.60 -    device->SetDisplayMode = UIKit_SetDisplayMode;
    5.61 -    device->PumpEvents = UIKit_PumpEvents;
    5.62 -    device->SuspendScreenSaver = UIKit_SuspendScreenSaver;
    5.63 -    device->CreateWindow = UIKit_CreateWindow;
    5.64 -    device->SetWindowTitle = UIKit_SetWindowTitle;
    5.65 -    device->ShowWindow = UIKit_ShowWindow;
    5.66 -    device->HideWindow = UIKit_HideWindow;
    5.67 -    device->RaiseWindow = UIKit_RaiseWindow;
    5.68 -    device->SetWindowBordered = UIKit_SetWindowBordered;
    5.69 -    device->SetWindowFullscreen = UIKit_SetWindowFullscreen;
    5.70 -    device->DestroyWindow = UIKit_DestroyWindow;
    5.71 -    device->GetWindowWMInfo = UIKit_GetWindowWMInfo;
    5.72 -    device->GetDisplayUsableBounds = UIKit_GetDisplayUsableBounds;
    5.73 +        /* Set the function pointers */
    5.74 +        device->VideoInit = UIKit_VideoInit;
    5.75 +        device->VideoQuit = UIKit_VideoQuit;
    5.76 +        device->GetDisplayModes = UIKit_GetDisplayModes;
    5.77 +        device->SetDisplayMode = UIKit_SetDisplayMode;
    5.78 +        device->PumpEvents = UIKit_PumpEvents;
    5.79 +        device->SuspendScreenSaver = UIKit_SuspendScreenSaver;
    5.80 +        device->CreateWindow = UIKit_CreateWindow;
    5.81 +        device->SetWindowTitle = UIKit_SetWindowTitle;
    5.82 +        device->ShowWindow = UIKit_ShowWindow;
    5.83 +        device->HideWindow = UIKit_HideWindow;
    5.84 +        device->RaiseWindow = UIKit_RaiseWindow;
    5.85 +        device->SetWindowBordered = UIKit_SetWindowBordered;
    5.86 +        device->SetWindowFullscreen = UIKit_SetWindowFullscreen;
    5.87 +        device->DestroyWindow = UIKit_DestroyWindow;
    5.88 +        device->GetWindowWMInfo = UIKit_GetWindowWMInfo;
    5.89 +        device->GetDisplayUsableBounds = UIKit_GetDisplayUsableBounds;
    5.90  
    5.91 -#if SDL_IPHONE_KEYBOARD
    5.92 -    device->HasScreenKeyboardSupport = UIKit_HasScreenKeyboardSupport;
    5.93 -    device->ShowScreenKeyboard = UIKit_ShowScreenKeyboard;
    5.94 -    device->HideScreenKeyboard = UIKit_HideScreenKeyboard;
    5.95 -    device->IsScreenKeyboardShown = UIKit_IsScreenKeyboardShown;
    5.96 -    device->SetTextInputRect = UIKit_SetTextInputRect;
    5.97 -#endif
    5.98 +    #if SDL_IPHONE_KEYBOARD
    5.99 +        device->HasScreenKeyboardSupport = UIKit_HasScreenKeyboardSupport;
   5.100 +        device->ShowScreenKeyboard = UIKit_ShowScreenKeyboard;
   5.101 +        device->HideScreenKeyboard = UIKit_HideScreenKeyboard;
   5.102 +        device->IsScreenKeyboardShown = UIKit_IsScreenKeyboardShown;
   5.103 +        device->SetTextInputRect = UIKit_SetTextInputRect;
   5.104 +    #endif
   5.105 +
   5.106 +        device->SetClipboardText = UIKit_SetClipboardText;
   5.107 +        device->GetClipboardText = UIKit_GetClipboardText;
   5.108 +        device->HasClipboardText = UIKit_HasClipboardText;
   5.109  
   5.110 -    /* OpenGL (ES) functions */
   5.111 -    device->GL_MakeCurrent      = UIKit_GL_MakeCurrent;
   5.112 -    device->GL_GetDrawableSize  = UIKit_GL_GetDrawableSize;
   5.113 -    device->GL_SwapWindow       = UIKit_GL_SwapWindow;
   5.114 -    device->GL_CreateContext    = UIKit_GL_CreateContext;
   5.115 -    device->GL_DeleteContext    = UIKit_GL_DeleteContext;
   5.116 -    device->GL_GetProcAddress   = UIKit_GL_GetProcAddress;
   5.117 -    device->GL_LoadLibrary      = UIKit_GL_LoadLibrary;
   5.118 -    device->free = UIKit_DeleteDevice;
   5.119 +        /* OpenGL (ES) functions */
   5.120 +        device->GL_MakeCurrent      = UIKit_GL_MakeCurrent;
   5.121 +        device->GL_GetDrawableSize  = UIKit_GL_GetDrawableSize;
   5.122 +        device->GL_SwapWindow       = UIKit_GL_SwapWindow;
   5.123 +        device->GL_CreateContext    = UIKit_GL_CreateContext;
   5.124 +        device->GL_DeleteContext    = UIKit_GL_DeleteContext;
   5.125 +        device->GL_GetProcAddress   = UIKit_GL_GetProcAddress;
   5.126 +        device->GL_LoadLibrary      = UIKit_GL_LoadLibrary;
   5.127 +        device->free = UIKit_DeleteDevice;
   5.128  
   5.129 -    device->gl_config.accelerated = 1;
   5.130 +        device->gl_config.accelerated = 1;
   5.131  
   5.132 -    return device;
   5.133 +        return device;
   5.134 +    }
   5.135  }
   5.136  
   5.137  VideoBootStrap UIKIT_bootstrap = {