src/events/SDL_windowevents.c
author Sam Lantinga
Wed, 16 Dec 2009 00:55:33 +0000
changeset 3573 41cd18e05965
parent 3502 98a819296cdc
child 3685 64ce267332c6
permissions -rw-r--r--
Fixed bug #666

Don't let the event queue overflow with resize events
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@1895
     4
slouken@1895
     5
    This library is free software; you can redistribute it and/or
slouken@1895
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1895
     7
    License as published by the Free Software Foundation; either
slouken@1895
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1895
     9
slouken@1895
    10
    This library is distributed in the hope that it will be useful,
slouken@1895
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1895
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1895
    13
    Lesser General Public License for more details.
slouken@1895
    14
slouken@1895
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1895
    16
    License along with this library; if not, write to the Free Software
slouken@1895
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1895
    18
slouken@1895
    19
    Sam Lantinga
slouken@1895
    20
    slouken@libsdl.org
slouken@1895
    21
*/
slouken@1895
    22
#include "SDL_config.h"
slouken@1895
    23
slouken@1895
    24
/* Window event handling code for SDL */
slouken@1895
    25
slouken@1895
    26
#include "SDL_events.h"
slouken@1895
    27
#include "SDL_events_c.h"
slouken@2794
    28
#include "SDL_mouse_c.h"
slouken@1895
    29
#include "../video/SDL_sysvideo.h"
slouken@1895
    30
slouken@3573
    31
slouken@3573
    32
static int
slouken@3573
    33
RemovePendingSizeEvents(void * userdata, SDL_Event *event)
slouken@3573
    34
{
slouken@3573
    35
    SDL_Event *new_event = (SDL_Event *)userdata;
slouken@3573
    36
slouken@3573
    37
    if (event->type == SDL_WINDOWEVENT &&
slouken@3573
    38
        event->window.event == SDL_WINDOWEVENT_RESIZED &&
slouken@3573
    39
        event->window.windowID == new_event->window.windowID) {
slouken@3573
    40
        /* We're about to post a new size event, drop the old one */
slouken@3573
    41
        return 0;
slouken@3573
    42
    }
slouken@3573
    43
    return 1;
slouken@3573
    44
}
slouken@3573
    45
slouken@1895
    46
int
slouken@1895
    47
SDL_SendWindowEvent(SDL_WindowID windowID, Uint8 windowevent, int data1,
slouken@1895
    48
                    int data2)
slouken@1895
    49
{
slouken@1895
    50
    int posted;
slouken@1895
    51
    SDL_Window *window;
slouken@1895
    52
slouken@1895
    53
    window = SDL_GetWindowFromID(windowID);
slouken@1895
    54
    if (!window) {
slouken@1895
    55
        return 0;
slouken@1895
    56
    }
slouken@1895
    57
    switch (windowevent) {
slouken@1895
    58
    case SDL_WINDOWEVENT_SHOWN:
slouken@1895
    59
        if (window->flags & SDL_WINDOW_SHOWN) {
slouken@1895
    60
            return 0;
slouken@1895
    61
        }
slouken@1895
    62
        window->flags |= SDL_WINDOW_SHOWN;
slouken@1895
    63
        SDL_OnWindowShown(window);
slouken@1895
    64
        break;
slouken@1895
    65
    case SDL_WINDOWEVENT_HIDDEN:
slouken@1895
    66
        if (!(window->flags & SDL_WINDOW_SHOWN)) {
slouken@1895
    67
            return 0;
slouken@1895
    68
        }
slouken@1895
    69
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1895
    70
        SDL_OnWindowHidden(window);
slouken@1895
    71
        break;
slouken@1895
    72
    case SDL_WINDOWEVENT_MOVED:
slouken@1895
    73
        if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
    74
            return 0;
slouken@1895
    75
        }
slouken@2875
    76
        if (data1 == SDL_WINDOWPOS_UNDEFINED) {
slouken@2875
    77
            data1 = window->x;
slouken@2875
    78
        }
slouken@2875
    79
        if (data2 == SDL_WINDOWPOS_UNDEFINED) {
slouken@2875
    80
            data2 = window->y;
slouken@2875
    81
        }
slouken@1895
    82
        if (data1 == window->x && data2 == window->y) {
slouken@1895
    83
            return 0;
slouken@1895
    84
        }
slouken@1915
    85
        window->x = data1;
slouken@1915
    86
        window->y = data2;
slouken@1895
    87
        break;
slouken@1895
    88
    case SDL_WINDOWEVENT_RESIZED:
slouken@1895
    89
        if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
    90
            return 0;
slouken@1895
    91
        }
slouken@1895
    92
        if (data1 == window->w && data2 == window->h) {
slouken@1895
    93
            return 0;
slouken@1895
    94
        }
slouken@1915
    95
        window->w = data1;
slouken@1915
    96
        window->h = data2;
slouken@1970
    97
        SDL_OnWindowResized(window);
slouken@1895
    98
        break;
slouken@1895
    99
    case SDL_WINDOWEVENT_MINIMIZED:
slouken@1895
   100
        if (window->flags & SDL_WINDOW_MINIMIZED) {
slouken@1895
   101
            return 0;
slouken@1895
   102
        }
slouken@1895
   103
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@3502
   104
        SDL_OnWindowMinimized(window);
slouken@1895
   105
        break;
slouken@1895
   106
    case SDL_WINDOWEVENT_MAXIMIZED:
slouken@1895
   107
        if (window->flags & SDL_WINDOW_MAXIMIZED) {
slouken@1895
   108
            return 0;
slouken@1895
   109
        }
slouken@1895
   110
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1895
   111
        break;
slouken@1895
   112
    case SDL_WINDOWEVENT_RESTORED:
slouken@1895
   113
        if (!(window->flags & (SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED))) {
slouken@1895
   114
            return 0;
slouken@1895
   115
        }
slouken@1895
   116
        window->flags &= ~(SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED);
slouken@3502
   117
        SDL_OnWindowRestored(window);
slouken@1895
   118
        break;
slouken@1895
   119
    case SDL_WINDOWEVENT_ENTER:
slouken@1895
   120
        if (window->flags & SDL_WINDOW_MOUSE_FOCUS) {
slouken@1895
   121
            return 0;
slouken@1895
   122
        }
slouken@1895
   123
        window->flags |= SDL_WINDOW_MOUSE_FOCUS;
slouken@1895
   124
        break;
slouken@1895
   125
    case SDL_WINDOWEVENT_LEAVE:
slouken@1895
   126
        if (!(window->flags & SDL_WINDOW_MOUSE_FOCUS)) {
slouken@1895
   127
            return 0;
slouken@1895
   128
        }
slouken@1895
   129
        window->flags &= ~SDL_WINDOW_MOUSE_FOCUS;
slouken@1895
   130
        break;
slouken@1895
   131
    case SDL_WINDOWEVENT_FOCUS_GAINED:
slouken@1895
   132
        if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@1895
   133
            return 0;
slouken@1895
   134
        }
slouken@1895
   135
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@1895
   136
        SDL_OnWindowFocusGained(window);
slouken@1895
   137
        break;
slouken@1895
   138
    case SDL_WINDOWEVENT_FOCUS_LOST:
slouken@1895
   139
        if (!(window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1895
   140
            return 0;
slouken@1895
   141
        }
slouken@1895
   142
        window->flags &= ~SDL_WINDOW_INPUT_FOCUS;
slouken@1895
   143
        SDL_OnWindowFocusLost(window);
slouken@1895
   144
        break;
slouken@1895
   145
    }
slouken@1895
   146
slouken@1895
   147
    /* Post the event, if desired */
slouken@1895
   148
    posted = 0;
slouken@1895
   149
    if (SDL_ProcessEvents[SDL_WINDOWEVENT] == SDL_ENABLE) {
slouken@1895
   150
        SDL_Event event;
slouken@1895
   151
        event.type = SDL_WINDOWEVENT;
slouken@1895
   152
        event.window.event = windowevent;
slouken@1895
   153
        event.window.data1 = data1;
slouken@1895
   154
        event.window.data2 = data2;
slouken@1895
   155
        event.window.windowID = windowID;
slouken@3573
   156
slouken@3573
   157
        /* Fixes queue overflow with resize events that aren't processed */
slouken@3573
   158
        if (windowevent == SDL_WINDOWEVENT_RESIZED) {
slouken@3573
   159
            SDL_FilterEvents(RemovePendingSizeEvents, &event);
slouken@3573
   160
        }
slouken@3573
   161
slouken@1895
   162
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   163
    }
slouken@1895
   164
    return (posted);
slouken@1895
   165
}
slouken@1895
   166
slouken@1895
   167
/* vi: set ts=4 sw=4 expandtab: */