cscg22-gearboy

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

SDL_windowsclipboard.c (4375B)


      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_WINDOWS
     24
     25#include "SDL_windowsvideo.h"
     26#include "SDL_windowswindow.h"
     27#include "../../events/SDL_clipboardevents_c.h"
     28
     29
     30#ifdef UNICODE
     31#define TEXT_FORMAT  CF_UNICODETEXT
     32#else
     33#define TEXT_FORMAT  CF_TEXT
     34#endif
     35
     36
     37/* Get any application owned window handle for clipboard association */
     38static HWND
     39GetWindowHandle(_THIS)
     40{
     41    SDL_Window *window;
     42
     43    window = _this->windows;
     44    if (window) {
     45        return ((SDL_WindowData *) window->driverdata)->hwnd;
     46    }
     47    return NULL;
     48}
     49
     50int
     51WIN_SetClipboardText(_THIS, const char *text)
     52{
     53    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
     54    int result = 0;
     55
     56    if (OpenClipboard(GetWindowHandle(_this))) {
     57        HANDLE hMem;
     58        LPTSTR tstr;
     59        SIZE_T i, size;
     60
     61        /* Convert the text from UTF-8 to Windows Unicode */
     62        tstr = WIN_UTF8ToString(text);
     63        if (!tstr) {
     64            return -1;
     65        }
     66
     67        /* Find out the size of the data */
     68        for (size = 0, i = 0; tstr[i]; ++i, ++size) {
     69            if (tstr[i] == '\n' && (i == 0 || tstr[i-1] != '\r')) {
     70                /* We're going to insert a carriage return */
     71                ++size;
     72            }
     73        }
     74        size = (size+1)*sizeof(*tstr);
     75
     76        /* Save the data to the clipboard */
     77        hMem = GlobalAlloc(GMEM_MOVEABLE, size);
     78        if (hMem) {
     79            LPTSTR dst = (LPTSTR)GlobalLock(hMem);
     80            if (dst) {
     81                /* Copy the text over, adding carriage returns as necessary */
     82                for (i = 0; tstr[i]; ++i) {
     83                    if (tstr[i] == '\n' && (i == 0 || tstr[i-1] != '\r')) {
     84                        *dst++ = '\r';
     85                    }
     86                    *dst++ = tstr[i];
     87                }
     88                *dst = 0;
     89                GlobalUnlock(hMem);
     90            }
     91
     92            EmptyClipboard();
     93            if (!SetClipboardData(TEXT_FORMAT, hMem)) {
     94                result = WIN_SetError("Couldn't set clipboard data");
     95            }
     96            data->clipboard_count = GetClipboardSequenceNumber();
     97        }
     98        SDL_free(tstr);
     99
    100        CloseClipboard();
    101    } else {
    102        result = WIN_SetError("Couldn't open clipboard");
    103    }
    104    return result;
    105}
    106
    107char *
    108WIN_GetClipboardText(_THIS)
    109{
    110    char *text;
    111
    112    text = NULL;
    113    if (IsClipboardFormatAvailable(TEXT_FORMAT) &&
    114        OpenClipboard(GetWindowHandle(_this))) {
    115        HANDLE hMem;
    116        LPTSTR tstr;
    117
    118        hMem = GetClipboardData(TEXT_FORMAT);
    119        if (hMem) {
    120            tstr = (LPTSTR)GlobalLock(hMem);
    121            text = WIN_StringToUTF8(tstr);
    122            GlobalUnlock(hMem);
    123        } else {
    124            WIN_SetError("Couldn't get clipboard data");
    125        }
    126        CloseClipboard();
    127    }
    128    if (!text) {
    129        text = SDL_strdup("");
    130    }
    131    return text;
    132}
    133
    134SDL_bool
    135WIN_HasClipboardText(_THIS)
    136{
    137    SDL_bool result = SDL_FALSE;
    138    char *text = WIN_GetClipboardText(_this);
    139    if (text) {
    140        result = text[0] != '\0' ? SDL_TRUE : SDL_FALSE;
    141        SDL_free(text);
    142    }
    143    return result;
    144}
    145
    146void
    147WIN_CheckClipboardUpdate(struct SDL_VideoData * data)
    148{
    149    const DWORD count = GetClipboardSequenceNumber();
    150    if (count != data->clipboard_count) {
    151        if (data->clipboard_count) {
    152            SDL_SendClipboardUpdate();
    153        }
    154        data->clipboard_count = count;
    155    }
    156}
    157
    158#endif /* SDL_VIDEO_DRIVER_WINDOWS */
    159
    160/* vi: set ts=4 sw=4 expandtab: */