cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

SDL_cocoaclipboard.m (3149B)


      1/*
      2  Simple DirectMedia Layer
      3  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
      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 be
     16     appreciated but is not required.
     17  2. Altered source versions must be plainly marked as such, and must not be
     18     misrepresented as being the original software.
     19  3. This notice may not be removed or altered from any source distribution.
     20*/
     21#include "../../SDL_internal.h"
     22
     23#if SDL_VIDEO_DRIVER_COCOA
     24
     25#include "SDL_cocoavideo.h"
     26#include "../../events/SDL_clipboardevents_c.h"
     27
     28static NSString *
     29GetTextFormat(_THIS)
     30{
     31    if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5) {
     32        return NSPasteboardTypeString;
     33    } else {
     34        return NSStringPboardType;
     35    }
     36}
     37
     38int
     39Cocoa_SetClipboardText(_THIS, const char *text)
     40{ @autoreleasepool
     41{
     42    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
     43    NSPasteboard *pasteboard;
     44    NSString *format = GetTextFormat(_this);
     45
     46    pasteboard = [NSPasteboard generalPasteboard];
     47    data->clipboard_count = [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil];
     48    [pasteboard setString:[NSString stringWithUTF8String:text] forType:format];
     49
     50    return 0;
     51}}
     52
     53char *
     54Cocoa_GetClipboardText(_THIS)
     55{ @autoreleasepool
     56{
     57    NSPasteboard *pasteboard;
     58    NSString *format = GetTextFormat(_this);
     59    NSString *available;
     60    char *text;
     61
     62    pasteboard = [NSPasteboard generalPasteboard];
     63    available = [pasteboard availableTypeFromArray: [NSArray arrayWithObject:format]];
     64    if ([available isEqualToString:format]) {
     65        NSString* string;
     66        const char *utf8;
     67
     68        string = [pasteboard stringForType:format];
     69        if (string == nil) {
     70            utf8 = "";
     71        } else {
     72            utf8 = [string UTF8String];
     73        }
     74        text = SDL_strdup(utf8);
     75    } else {
     76        text = SDL_strdup("");
     77    }
     78
     79    return text;
     80}}
     81
     82SDL_bool
     83Cocoa_HasClipboardText(_THIS)
     84{
     85    SDL_bool result = SDL_FALSE;
     86    char *text = Cocoa_GetClipboardText(_this);
     87    if (text) {
     88        result = text[0] != '\0' ? SDL_TRUE : SDL_FALSE;
     89        SDL_free(text);
     90    }
     91    return result;
     92}
     93
     94void
     95Cocoa_CheckClipboardUpdate(struct SDL_VideoData * data)
     96{ @autoreleasepool
     97{
     98    NSPasteboard *pasteboard;
     99    NSInteger count;
    100
    101    pasteboard = [NSPasteboard generalPasteboard];
    102    count = [pasteboard changeCount];
    103    if (count != data->clipboard_count) {
    104        if (data->clipboard_count) {
    105            SDL_SendClipboardUpdate();
    106        }
    107        data->clipboard_count = count;
    108    }
    109}}
    110
    111#endif /* SDL_VIDEO_DRIVER_COCOA */
    112
    113/* vi: set ts=4 sw=4 expandtab: */