src/video/uikit/SDL_uikitclipboard.m
author Sam Lantinga
Fri, 27 Jan 2017 06:05:50 -0800
changeset 10856 486aa38c6a88
parent 10737 3406a0f8b041
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Added Thrustmaster Wheel FFB entry to the list of wheels
slime73@10062
     1
/*
philipp@10064
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slime73@10062
     4
philipp@10064
     5
  This software is provided 'as-is', without any express or implied
philipp@10064
     6
  warranty.  In no event will the authors be held liable for any damages
philipp@10064
     7
  arising from the use of this software.
slime73@10062
     8
philipp@10064
     9
  Permission is granted to anyone to use this software for any purpose,
philipp@10064
    10
  including commercial applications, and to alter it and redistribute it
philipp@10064
    11
  freely, subject to the following restrictions:
slime73@10062
    12
philipp@10064
    13
  1. The origin of this software must not be misrepresented; you must not
philipp@10064
    14
     claim that you wrote the original software. If you use this software
philipp@10064
    15
     in a product, an acknowledgment in the product documentation would be
philipp@10064
    16
     appreciated but is not required.
philipp@10064
    17
  2. Altered source versions must be plainly marked as such, and must not be
philipp@10064
    18
     misrepresented as being the original software.
philipp@10064
    19
  3. This notice may not be removed or altered from any source distribution.
philipp@10064
    20
*/
slime73@10062
    21
#include "../../SDL_internal.h"
slime73@10062
    22
slime73@10062
    23
#if SDL_VIDEO_DRIVER_UIKIT
slime73@10062
    24
slime73@10062
    25
#include "SDL_uikitvideo.h"
slime73@10062
    26
#include "../../events/SDL_clipboardevents_c.h"
slime73@10062
    27
slime73@10062
    28
#import <UIKit/UIPasteboard.h>
slime73@10062
    29
slime73@10062
    30
int
slime73@10062
    31
UIKit_SetClipboardText(_THIS, const char *text)
slime73@10062
    32
{
slime73@10340
    33
#if TARGET_OS_TV
slime73@10340
    34
    return SDL_SetError("The clipboard is not available on tvOS");
slime73@10340
    35
#else
slime73@10062
    36
    @autoreleasepool {
slime73@10062
    37
        [UIPasteboard generalPasteboard].string = @(text);
slime73@10062
    38
        return 0;
slime73@10062
    39
    }
slime73@10340
    40
#endif
slime73@10062
    41
}
slime73@10062
    42
slime73@10062
    43
char *
slime73@10062
    44
UIKit_GetClipboardText(_THIS)
slime73@10062
    45
{
slime73@10340
    46
#if TARGET_OS_TV
slime73@10340
    47
    return SDL_strdup(""); // Unsupported.
slime73@10340
    48
#else
slime73@10062
    49
    @autoreleasepool {
slime73@10062
    50
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
slime73@10062
    51
        NSString *string = pasteboard.string;
slime73@10062
    52
slime73@10062
    53
        if (string != nil) {
slime73@10062
    54
            return SDL_strdup(string.UTF8String);
slime73@10062
    55
        } else {
slime73@10062
    56
            return SDL_strdup("");
slime73@10062
    57
        }
slime73@10062
    58
    }
slime73@10340
    59
#endif
slime73@10062
    60
}
slime73@10062
    61
slime73@10062
    62
SDL_bool
slime73@10062
    63
UIKit_HasClipboardText(_THIS)
slime73@10062
    64
{
slime73@10062
    65
    @autoreleasepool {
slime73@10340
    66
#if !TARGET_OS_TV
slime73@10062
    67
        if ([UIPasteboard generalPasteboard].string != nil) {
slime73@10062
    68
            return SDL_TRUE;
slime73@10062
    69
        }
slime73@10340
    70
#endif
slime73@10062
    71
        return SDL_FALSE;
slime73@10062
    72
    }
slime73@10062
    73
}
slime73@10062
    74
slime73@10062
    75
void
slime73@10062
    76
UIKit_InitClipboard(_THIS)
slime73@10062
    77
{
slime73@10340
    78
#if !TARGET_OS_TV
slime73@10062
    79
    @autoreleasepool {
slime73@10062
    80
        SDL_VideoData *data = (__bridge SDL_VideoData *) _this->driverdata;
slime73@10062
    81
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
slime73@10062
    82
slime73@10062
    83
        id observer = [center addObserverForName:UIPasteboardChangedNotification
slime73@10062
    84
                                         object:nil
slime73@10062
    85
                                          queue:nil
slime73@10062
    86
                                     usingBlock:^(NSNotification *note) {
slime73@10062
    87
                                         SDL_SendClipboardUpdate();
slime73@10062
    88
                                     }];
slime73@10062
    89
slime73@10062
    90
        data.pasteboardObserver = observer;
slime73@10062
    91
    }
slime73@10340
    92
#endif
slime73@10062
    93
}
slime73@10062
    94
slime73@10062
    95
void
slime73@10062
    96
UIKit_QuitClipboard(_THIS)
slime73@10062
    97
{
slime73@10062
    98
    @autoreleasepool {
slime73@10062
    99
        SDL_VideoData *data = (__bridge SDL_VideoData *) _this->driverdata;
slime73@10062
   100
slime73@10062
   101
        if (data.pasteboardObserver != nil) {
slime73@10062
   102
            [[NSNotificationCenter defaultCenter] removeObserver:data.pasteboardObserver];
slime73@10062
   103
        }
slime73@10062
   104
slime73@10062
   105
        data.pasteboardObserver = nil;
slime73@10062
   106
    }
slime73@10062
   107
}
slime73@10062
   108
slime73@10062
   109
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slime73@10062
   110
slime73@10062
   111
/* vi: set ts=4 sw=4 expandtab: */