src/events/SDL_windowevents.c
author Sam Lantinga
Fri, 27 Jan 2017 06:05:50 -0800
changeset 10856 486aa38c6a88
parent 10737 3406a0f8b041
child 11272 d60ede5a0c2e
permissions -rw-r--r--
Added Thrustmaster Wheel FFB entry to the list of wheels
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@1895
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1895
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1895
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1895
    20
*/
icculus@8093
    21
#include "../SDL_internal.h"
slouken@1895
    22
slouken@1895
    23
/* Window event handling code for SDL */
slouken@1895
    24
slouken@1895
    25
#include "SDL_events.h"
slouken@1895
    26
#include "SDL_events_c.h"
slouken@2794
    27
#include "SDL_mouse_c.h"
slouken@1895
    28
#include "../video/SDL_sysvideo.h"
slouken@1895
    29
slouken@3573
    30
slouken@3573
    31
static int
slouken@6512
    32
RemovePendingResizedEvents(void * userdata, SDL_Event *event)
slouken@3573
    33
{
slouken@3573
    34
    SDL_Event *new_event = (SDL_Event *)userdata;
slouken@3573
    35
slouken@3573
    36
    if (event->type == SDL_WINDOWEVENT &&
slouken@6512
    37
        event->window.event == SDL_WINDOWEVENT_RESIZED &&
slouken@6512
    38
        event->window.windowID == new_event->window.windowID) {
slouken@6512
    39
        /* We're about to post a new size event, drop the old one */
slouken@6512
    40
        return 0;
slouken@6512
    41
    }
slouken@6512
    42
    return 1;
slouken@6512
    43
}
slouken@6512
    44
slouken@6512
    45
static int
slouken@6512
    46
RemovePendingSizeChangedEvents(void * userdata, SDL_Event *event)
slouken@6512
    47
{
slouken@6512
    48
    SDL_Event *new_event = (SDL_Event *)userdata;
slouken@6512
    49
slouken@6512
    50
    if (event->type == SDL_WINDOWEVENT &&
slouken@6512
    51
        event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED &&
slouken@3573
    52
        event->window.windowID == new_event->window.windowID) {
slouken@3573
    53
        /* We're about to post a new size event, drop the old one */
slouken@3573
    54
        return 0;
slouken@3573
    55
    }
slouken@3573
    56
    return 1;
slouken@3573
    57
}
slouken@3573
    58
slouken@5042
    59
static int
slouken@5042
    60
RemovePendingMoveEvents(void * userdata, SDL_Event *event)
slouken@5042
    61
{
slouken@5042
    62
    SDL_Event *new_event = (SDL_Event *)userdata;
slouken@5042
    63
slouken@5042
    64
    if (event->type == SDL_WINDOWEVENT &&
slouken@5042
    65
        event->window.event == SDL_WINDOWEVENT_MOVED &&
slouken@5042
    66
        event->window.windowID == new_event->window.windowID) {
slouken@5042
    67
        /* We're about to post a new move event, drop the old one */
slouken@5042
    68
        return 0;
slouken@5042
    69
    }
slouken@5042
    70
    return 1;
slouken@5042
    71
}
slouken@5042
    72
slouken@10431
    73
static int
slouken@10431
    74
RemovePendingExposedEvents(void * userdata, SDL_Event *event)
slouken@10431
    75
{
slouken@10431
    76
    SDL_Event *new_event = (SDL_Event *)userdata;
slouken@10431
    77
slouken@10431
    78
    if (event->type == SDL_WINDOWEVENT &&
slouken@10431
    79
        event->window.event == SDL_WINDOWEVENT_EXPOSED &&
slouken@10431
    80
        event->window.windowID == new_event->window.windowID) {
slouken@10431
    81
        /* We're about to post a new exposed event, drop the old one */
slouken@10431
    82
        return 0;
slouken@10431
    83
    }
slouken@10431
    84
    return 1;
slouken@10431
    85
}
slouken@10431
    86
slouken@1895
    87
int
slouken@3685
    88
SDL_SendWindowEvent(SDL_Window * window, Uint8 windowevent, int data1,
slouken@1895
    89
                    int data2)
slouken@1895
    90
{
slouken@1895
    91
    int posted;
slouken@1895
    92
slouken@1895
    93
    if (!window) {
slouken@1895
    94
        return 0;
slouken@1895
    95
    }
slouken@1895
    96
    switch (windowevent) {
slouken@1895
    97
    case SDL_WINDOWEVENT_SHOWN:
slouken@1895
    98
        if (window->flags & SDL_WINDOW_SHOWN) {
slouken@1895
    99
            return 0;
slouken@1895
   100
        }
slouken@5383
   101
        window->flags &= ~SDL_WINDOW_HIDDEN;
slouken@1895
   102
        window->flags |= SDL_WINDOW_SHOWN;
slouken@1895
   103
        SDL_OnWindowShown(window);
slouken@1895
   104
        break;
slouken@1895
   105
    case SDL_WINDOWEVENT_HIDDEN:
slouken@1895
   106
        if (!(window->flags & SDL_WINDOW_SHOWN)) {
slouken@1895
   107
            return 0;
slouken@1895
   108
        }
slouken@1895
   109
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@5383
   110
        window->flags |= SDL_WINDOW_HIDDEN;
slouken@1895
   111
        SDL_OnWindowHidden(window);
slouken@1895
   112
        break;
slouken@1895
   113
    case SDL_WINDOWEVENT_MOVED:
slouken@5249
   114
        if (SDL_WINDOWPOS_ISUNDEFINED(data1) ||
slouken@5249
   115
            SDL_WINDOWPOS_ISUNDEFINED(data2)) {
slouken@1895
   116
            return 0;
slouken@1895
   117
        }
slouken@5398
   118
        if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@5398
   119
            window->windowed.x = data1;
slouken@5398
   120
            window->windowed.y = data2;
slouken@5398
   121
        }
slouken@1895
   122
        if (data1 == window->x && data2 == window->y) {
slouken@1895
   123
            return 0;
slouken@1895
   124
        }
slouken@1915
   125
        window->x = data1;
slouken@1915
   126
        window->y = data2;
slouken@1895
   127
        break;
slouken@1895
   128
    case SDL_WINDOWEVENT_RESIZED:
slouken@5398
   129
        if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@5398
   130
            window->windowed.w = data1;
slouken@5398
   131
            window->windowed.h = data2;
slouken@5398
   132
        }
slouken@1895
   133
        if (data1 == window->w && data2 == window->h) {
slouken@1895
   134
            return 0;
slouken@1895
   135
        }
slouken@1915
   136
        window->w = data1;
slouken@1915
   137
        window->h = data2;
slouken@5166
   138
        SDL_OnWindowResized(window);
slouken@1895
   139
        break;
slouken@1895
   140
    case SDL_WINDOWEVENT_MINIMIZED:
slouken@1895
   141
        if (window->flags & SDL_WINDOW_MINIMIZED) {
slouken@1895
   142
            return 0;
slouken@1895
   143
        }
dludwig@9968
   144
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1895
   145
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@3502
   146
        SDL_OnWindowMinimized(window);
slouken@1895
   147
        break;
slouken@1895
   148
    case SDL_WINDOWEVENT_MAXIMIZED:
slouken@1895
   149
        if (window->flags & SDL_WINDOW_MAXIMIZED) {
slouken@1895
   150
            return 0;
slouken@1895
   151
        }
dludwig@9968
   152
        window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1895
   153
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1895
   154
        break;
slouken@1895
   155
    case SDL_WINDOWEVENT_RESTORED:
slouken@1895
   156
        if (!(window->flags & (SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED))) {
slouken@1895
   157
            return 0;
slouken@1895
   158
        }
slouken@1895
   159
        window->flags &= ~(SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED);
slouken@3502
   160
        SDL_OnWindowRestored(window);
slouken@1895
   161
        break;
slouken@1895
   162
    case SDL_WINDOWEVENT_ENTER:
slouken@1895
   163
        if (window->flags & SDL_WINDOW_MOUSE_FOCUS) {
slouken@1895
   164
            return 0;
slouken@1895
   165
        }
slouken@1895
   166
        window->flags |= SDL_WINDOW_MOUSE_FOCUS;
jorgen@7089
   167
        SDL_OnWindowEnter(window);
slouken@1895
   168
        break;
slouken@1895
   169
    case SDL_WINDOWEVENT_LEAVE:
slouken@1895
   170
        if (!(window->flags & SDL_WINDOW_MOUSE_FOCUS)) {
slouken@1895
   171
            return 0;
slouken@1895
   172
        }
slouken@1895
   173
        window->flags &= ~SDL_WINDOW_MOUSE_FOCUS;
slouken@7191
   174
        SDL_OnWindowLeave(window);
slouken@1895
   175
        break;
slouken@1895
   176
    case SDL_WINDOWEVENT_FOCUS_GAINED:
slouken@1895
   177
        if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@1895
   178
            return 0;
slouken@1895
   179
        }
slouken@1895
   180
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@1895
   181
        SDL_OnWindowFocusGained(window);
slouken@1895
   182
        break;
slouken@1895
   183
    case SDL_WINDOWEVENT_FOCUS_LOST:
slouken@1895
   184
        if (!(window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1895
   185
            return 0;
slouken@1895
   186
        }
slouken@1895
   187
        window->flags &= ~SDL_WINDOW_INPUT_FOCUS;
slouken@1895
   188
        SDL_OnWindowFocusLost(window);
slouken@1895
   189
        break;
slouken@1895
   190
    }
slouken@1895
   191
slouken@1895
   192
    /* Post the event, if desired */
slouken@1895
   193
    posted = 0;
slouken@4429
   194
    if (SDL_GetEventState(SDL_WINDOWEVENT) == SDL_ENABLE) {
slouken@1895
   195
        SDL_Event event;
slouken@1895
   196
        event.type = SDL_WINDOWEVENT;
slouken@1895
   197
        event.window.event = windowevent;
slouken@1895
   198
        event.window.data1 = data1;
slouken@1895
   199
        event.window.data2 = data2;
slouken@3685
   200
        event.window.windowID = window->id;
slouken@3573
   201
slouken@3573
   202
        /* Fixes queue overflow with resize events that aren't processed */
slouken@6512
   203
        if (windowevent == SDL_WINDOWEVENT_RESIZED) {
slouken@6512
   204
            SDL_FilterEvents(RemovePendingResizedEvents, &event);
slouken@6512
   205
        }
slouken@6512
   206
        if (windowevent == SDL_WINDOWEVENT_SIZE_CHANGED) {
slouken@6512
   207
            SDL_FilterEvents(RemovePendingSizeChangedEvents, &event);
slouken@3573
   208
        }
slouken@5042
   209
        if (windowevent == SDL_WINDOWEVENT_MOVED) {
slouken@5042
   210
            SDL_FilterEvents(RemovePendingMoveEvents, &event);
slouken@5042
   211
        }
slouken@10431
   212
        if (windowevent == SDL_WINDOWEVENT_EXPOSED) {
slouken@10431
   213
            SDL_FilterEvents(RemovePendingExposedEvents, &event);
slouken@10431
   214
        }
slouken@1895
   215
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   216
    }
slouken@7191
   217
icculus@7609
   218
    if (windowevent == SDL_WINDOWEVENT_CLOSE) {
icculus@7609
   219
        if ( !window->prev && !window->next ) {
icculus@7609
   220
            /* This is the last window in the list so send the SDL_QUIT event */
icculus@7609
   221
            SDL_SendQuit();
icculus@7609
   222
        }
icculus@7609
   223
    }
icculus@7609
   224
slouken@1895
   225
    return (posted);
slouken@1895
   226
}
slouken@1895
   227
slouken@1895
   228
/* vi: set ts=4 sw=4 expandtab: */