src/events/SDL_dropevents.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 12 Aug 2016 19:59:00 -0400
changeset 10266 c09f06c4e8c8
parent 10042 8546f8015052
permissions -rw-r--r--
emscripten: send fake mouse events for touches, like other targets do. (This really should be handled at the higher level and not in the individual targets, but this fixes the immediate bug.)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6091
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     2
  Simple DirectMedia Layer
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
6091
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     5
  This software is provided 'as-is', without any express or implied
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     7
  arising from the use of this software.
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    11
  freely, subject to the following restrictions:
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    14
     claim that you wrote the original software. If you use this software
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    16
     appreciated but is not required.
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    18
     misrepresented as being the original software.
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
    21
#include "../SDL_internal.h"
6091
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
/* Drag and drop event handling code for SDL */
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
#include "SDL_events.h"
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
#include "SDL_events_c.h"
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
#include "SDL_dropevents_c.h"
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
10022
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    29
#include "../video/SDL_sysvideo.h"  /* for SDL_Window internals. */
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    30
6091
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
10020
08b9e569e1ce Added SDL_DROPTEXT event, for dragging and dropping string data.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    32
static int
10022
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    33
SDL_SendDrop(SDL_Window *window, const SDL_EventType evtype, const char *data)
6091
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
{
10022
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    35
    static SDL_bool app_is_dropping = SDL_FALSE;
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    36
    int posted = 0;
6091
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
    /* Post the event, if desired */
10020
08b9e569e1ce Added SDL_DROPTEXT event, for dragging and dropping string data.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    39
    if (SDL_GetEventState(evtype) == SDL_ENABLE) {
10022
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    40
        const SDL_bool need_begin = window ? !window->is_dropping : !app_is_dropping;
6091
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
        SDL_Event event;
10022
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    42
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    43
        if (need_begin) {
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    44
            SDL_zero(event);
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    45
            event.type = SDL_DROPBEGIN;
10042
8546f8015052 Fixed NULL dereference on drop events with no window associated.
Ryan C. Gordon <icculus@icculus.org>
parents: 10022
diff changeset
    46
8546f8015052 Fixed NULL dereference on drop events with no window associated.
Ryan C. Gordon <icculus@icculus.org>
parents: 10022
diff changeset
    47
            if (window) {
8546f8015052 Fixed NULL dereference on drop events with no window associated.
Ryan C. Gordon <icculus@icculus.org>
parents: 10022
diff changeset
    48
                event.drop.windowID = window->id;
8546f8015052 Fixed NULL dereference on drop events with no window associated.
Ryan C. Gordon <icculus@icculus.org>
parents: 10022
diff changeset
    49
            }
8546f8015052 Fixed NULL dereference on drop events with no window associated.
Ryan C. Gordon <icculus@icculus.org>
parents: 10022
diff changeset
    50
10022
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    51
            posted = (SDL_PushEvent(&event) > 0);
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    52
            if (!posted) {
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    53
                return 0;
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    54
            }
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    55
            if (window) {
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    56
                window->is_dropping = SDL_TRUE;
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    57
            } else {
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    58
                app_is_dropping = SDL_TRUE;
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    59
            }
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    60
        }
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    61
10020
08b9e569e1ce Added SDL_DROPTEXT event, for dragging and dropping string data.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    62
        SDL_zero(event);
08b9e569e1ce Added SDL_DROPTEXT event, for dragging and dropping string data.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    63
        event.type = evtype;
10022
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    64
        event.drop.file = data ? SDL_strdup(data) : NULL;
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    65
        event.drop.windowID = window ? window->id : 0;
6091
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
        posted = (SDL_PushEvent(&event) > 0);
10022
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    67
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    68
        if (posted && (evtype == SDL_DROPCOMPLETE)) {
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    69
            if (window) {
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    70
                window->is_dropping = SDL_FALSE;
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    71
            } else {
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    72
                app_is_dropping = SDL_FALSE;
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    73
            }
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    74
        }
6091
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
    }
10020
08b9e569e1ce Added SDL_DROPTEXT event, for dragging and dropping string data.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    76
    return posted;
08b9e569e1ce Added SDL_DROPTEXT event, for dragging and dropping string data.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    77
}
08b9e569e1ce Added SDL_DROPTEXT event, for dragging and dropping string data.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    78
08b9e569e1ce Added SDL_DROPTEXT event, for dragging and dropping string data.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    79
int
10022
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    80
SDL_SendDropFile(SDL_Window *window, const char *file)
10020
08b9e569e1ce Added SDL_DROPTEXT event, for dragging and dropping string data.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    81
{
10022
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    82
    return SDL_SendDrop(window, SDL_DROPFILE, file);
10020
08b9e569e1ce Added SDL_DROPTEXT event, for dragging and dropping string data.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    83
}
08b9e569e1ce Added SDL_DROPTEXT event, for dragging and dropping string data.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    84
08b9e569e1ce Added SDL_DROPTEXT event, for dragging and dropping string data.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    85
int
10022
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    86
SDL_SendDropText(SDL_Window *window, const char *text)
10020
08b9e569e1ce Added SDL_DROPTEXT event, for dragging and dropping string data.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    87
{
10022
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    88
    return SDL_SendDrop(window, SDL_DROPTEXT, text);
6091
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    89
}
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    90
10022
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    91
int
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    92
SDL_SendDropComplete(SDL_Window *window)
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    93
{
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    94
    return SDL_SendDrop(window, SDL_DROPCOMPLETE, NULL);
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    95
}
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    96
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    97
6091
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    98
/* vi: set ts=4 sw=4 expandtab: */