test/testdropfile.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 12 Aug 2016 19:59:00 -0400
changeset 10266 c09f06c4e8c8
parent 10022 30807689ca1b
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:
7807
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
     1
/*
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9922
diff changeset
     2
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
7807
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
     3
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
     4
  This software is provided 'as-is', without any express or implied
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
     5
  warranty.  In no event will the authors be held liable for any damages
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
     6
  arising from the use of this software.
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
     7
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
     8
  Permission is granted to anyone to use this software for any purpose,
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
     9
  including commercial applications, and to alter it and redistribute it
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    10
  freely.
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    11
*/
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    12
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    13
#include <stdlib.h>
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    14
#include <stdio.h>
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    15
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    16
#include "SDL_test_common.h"
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    17
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    18
static SDLTest_CommonState *state;
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    19
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    20
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    21
static void
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    22
quit(int rc)
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    23
{
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    24
    SDLTest_CommonQuit(state);
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    25
    exit(rc);
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    26
}
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    27
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    28
int
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    29
main(int argc, char *argv[])
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    30
{
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    31
    int i, done;
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    32
    SDL_Event event;
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    33
9922
b066d5fd5fa7 Replaced tabs with spaces in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9619
diff changeset
    34
    /* Enable standard application logging */
7807
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    35
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    36
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    37
    /* Initialize test framework */
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    38
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    39
    if (!state) {
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    40
        return 1;
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    41
    }
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    42
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    43
    for (i = 1; i < argc;) {
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    44
        int consumed;
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    45
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    46
        consumed = SDLTest_CommonArg(state, i);
8776
29aac8b813d9 Changed C++ style comments to fix pedantic warnings.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8174
diff changeset
    47
        /* needed voodoo to allow app to launch via OS X Finder */
8174
a8e60169b648 add testdropfile to OS X Project
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
    48
        if (SDL_strncmp(argv[i], "-psn", 4)==0) {
a8e60169b648 add testdropfile to OS X Project
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
    49
            consumed = 1;
a8e60169b648 add testdropfile to OS X Project
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
    50
        }
7807
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    51
        if (consumed == 0) {
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    52
            consumed = -1;
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    53
        }
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    54
        if (consumed < 0) {
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    55
            SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    56
            quit(1);
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    57
        }
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    58
        i += consumed;
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    59
    }
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    60
    if (!SDLTest_CommonInit(state)) {
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    61
        quit(2);
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    62
    }
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    63
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    64
    for (i = 0; i < state->num_windows; ++i) {
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    65
        SDL_Renderer *renderer = state->renderers[i];
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    66
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    67
        SDL_RenderClear(renderer);
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    68
        SDL_RenderPresent(renderer);
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    69
    }
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    70
8174
a8e60169b648 add testdropfile to OS X Project
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
    71
    SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
a8e60169b648 add testdropfile to OS X Project
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
    72
7807
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    73
    /* Main render loop */
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    74
    done = 0;
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    75
    while (!done) {
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    76
        /* Check for events */
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    77
        while (SDL_PollEvent(&event)) {
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    78
            SDLTest_CommonEvent(state, &event, &done);
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    79
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
            if (event.type == SDL_DROPBEGIN) {
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    81
                SDL_Log("Drop beginning on window %u", (unsigned int) event.drop.windowID);
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    82
            } else if (event.type == 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
    83
                SDL_Log("Drop complete on window %u", (unsigned int) event.drop.windowID);
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    84
            } else if ((event.type == SDL_DROPFILE) || (event.type == SDL_DROPTEXT)) {
10020
08b9e569e1ce Added SDL_DROPTEXT event, for dragging and dropping string data.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    85
                const char *typestr = (event.type == SDL_DROPFILE) ? "File" : "Text";
7807
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    86
                char *dropped_filedir = event.drop.file;
10022
30807689ca1b Added SDL_DROPBEGIN and SDL_DROPCOMPLETE events, plus window IDs for drops.
Ryan C. Gordon <icculus@icculus.org>
parents: 10020
diff changeset
    87
                SDL_Log("%s dropped on window %u: %s", typestr, (unsigned int) event.drop.windowID, dropped_filedir);
7807
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    88
                SDL_free(dropped_filedir);
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    89
            }
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    90
        }
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    91
    }
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    92
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    93
    quit(0);
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    94
    /* keep the compiler happy ... */
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    95
    return(0);
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    96
}
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    97
73da2747c268 added simple Drag & drop test
Edward Rudd <urkle@outoforder.cc>
parents:
diff changeset
    98
/* vi: set ts=4 sw=4 expandtab: */