src/events/SDL_windowevents.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 15 Feb 2013 08:47:44 -0800
changeset 6885 700f1b25f77f
parent 6512 4beb596ccfae
child 7089 257fc4e541e1
permissions -rw-r--r--
Happy New Year!
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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
*/
slouken@1895
    21
#include "SDL_config.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@1895
    73
int
slouken@3685
    74
SDL_SendWindowEvent(SDL_Window * window, Uint8 windowevent, int data1,
slouken@1895
    75
                    int data2)
slouken@1895
    76
{
slouken@1895
    77
    int posted;
slouken@1895
    78
slouken@1895
    79
    if (!window) {
slouken@1895
    80
        return 0;
slouken@1895
    81
    }
slouken@1895
    82
    switch (windowevent) {
slouken@1895
    83
    case SDL_WINDOWEVENT_SHOWN:
slouken@1895
    84
        if (window->flags & SDL_WINDOW_SHOWN) {
slouken@1895
    85
            return 0;
slouken@1895
    86
        }
slouken@5383
    87
        window->flags &= ~SDL_WINDOW_HIDDEN;
slouken@1895
    88
        window->flags |= SDL_WINDOW_SHOWN;
slouken@1895
    89
        SDL_OnWindowShown(window);
slouken@1895
    90
        break;
slouken@1895
    91
    case SDL_WINDOWEVENT_HIDDEN:
slouken@1895
    92
        if (!(window->flags & SDL_WINDOW_SHOWN)) {
slouken@1895
    93
            return 0;
slouken@1895
    94
        }
slouken@1895
    95
        window->flags &= ~SDL_WINDOW_SHOWN;
slouken@5383
    96
        window->flags |= SDL_WINDOW_HIDDEN;
slouken@1895
    97
        SDL_OnWindowHidden(window);
slouken@1895
    98
        break;
slouken@1895
    99
    case SDL_WINDOWEVENT_MOVED:
slouken@5249
   100
        if (SDL_WINDOWPOS_ISUNDEFINED(data1) ||
slouken@5249
   101
            SDL_WINDOWPOS_ISUNDEFINED(data2)) {
slouken@1895
   102
            return 0;
slouken@1895
   103
        }
slouken@5398
   104
        if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@5398
   105
            window->windowed.x = data1;
slouken@5398
   106
            window->windowed.y = data2;
slouken@5398
   107
        }
slouken@1895
   108
        if (data1 == window->x && data2 == window->y) {
slouken@1895
   109
            return 0;
slouken@1895
   110
        }
slouken@1915
   111
        window->x = data1;
slouken@1915
   112
        window->y = data2;
slouken@1895
   113
        break;
slouken@1895
   114
    case SDL_WINDOWEVENT_RESIZED:
slouken@5398
   115
        if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@5398
   116
            window->windowed.w = data1;
slouken@5398
   117
            window->windowed.h = data2;
slouken@5398
   118
        }
slouken@1895
   119
        if (data1 == window->w && data2 == window->h) {
slouken@1895
   120
            return 0;
slouken@1895
   121
        }
slouken@1915
   122
        window->w = data1;
slouken@1915
   123
        window->h = data2;
slouken@5166
   124
        SDL_OnWindowResized(window);
slouken@1895
   125
        break;
slouken@1895
   126
    case SDL_WINDOWEVENT_MINIMIZED:
slouken@1895
   127
        if (window->flags & SDL_WINDOW_MINIMIZED) {
slouken@1895
   128
            return 0;
slouken@1895
   129
        }
slouken@1895
   130
        window->flags |= SDL_WINDOW_MINIMIZED;
slouken@3502
   131
        SDL_OnWindowMinimized(window);
slouken@1895
   132
        break;
slouken@1895
   133
    case SDL_WINDOWEVENT_MAXIMIZED:
slouken@1895
   134
        if (window->flags & SDL_WINDOW_MAXIMIZED) {
slouken@1895
   135
            return 0;
slouken@1895
   136
        }
slouken@1895
   137
        window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1895
   138
        break;
slouken@1895
   139
    case SDL_WINDOWEVENT_RESTORED:
slouken@1895
   140
        if (!(window->flags & (SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED))) {
slouken@1895
   141
            return 0;
slouken@1895
   142
        }
slouken@1895
   143
        window->flags &= ~(SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED);
slouken@3502
   144
        SDL_OnWindowRestored(window);
slouken@1895
   145
        break;
slouken@1895
   146
    case SDL_WINDOWEVENT_ENTER:
slouken@1895
   147
        if (window->flags & SDL_WINDOW_MOUSE_FOCUS) {
slouken@1895
   148
            return 0;
slouken@1895
   149
        }
slouken@1895
   150
        window->flags |= SDL_WINDOW_MOUSE_FOCUS;
slouken@1895
   151
        break;
slouken@1895
   152
    case SDL_WINDOWEVENT_LEAVE:
slouken@1895
   153
        if (!(window->flags & SDL_WINDOW_MOUSE_FOCUS)) {
slouken@1895
   154
            return 0;
slouken@1895
   155
        }
slouken@1895
   156
        window->flags &= ~SDL_WINDOW_MOUSE_FOCUS;
slouken@1895
   157
        break;
slouken@1895
   158
    case SDL_WINDOWEVENT_FOCUS_GAINED:
slouken@1895
   159
        if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@1895
   160
            return 0;
slouken@1895
   161
        }
slouken@1895
   162
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@1895
   163
        SDL_OnWindowFocusGained(window);
slouken@1895
   164
        break;
slouken@1895
   165
    case SDL_WINDOWEVENT_FOCUS_LOST:
slouken@1895
   166
        if (!(window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1895
   167
            return 0;
slouken@1895
   168
        }
slouken@1895
   169
        window->flags &= ~SDL_WINDOW_INPUT_FOCUS;
slouken@1895
   170
        SDL_OnWindowFocusLost(window);
slouken@1895
   171
        break;
slouken@1895
   172
    }
slouken@1895
   173
slouken@1895
   174
    /* Post the event, if desired */
slouken@1895
   175
    posted = 0;
slouken@4429
   176
    if (SDL_GetEventState(SDL_WINDOWEVENT) == SDL_ENABLE) {
slouken@1895
   177
        SDL_Event event;
slouken@1895
   178
        event.type = SDL_WINDOWEVENT;
slouken@1895
   179
        event.window.event = windowevent;
slouken@1895
   180
        event.window.data1 = data1;
slouken@1895
   181
        event.window.data2 = data2;
slouken@3685
   182
        event.window.windowID = window->id;
slouken@3573
   183
slouken@3573
   184
        /* Fixes queue overflow with resize events that aren't processed */
slouken@6512
   185
        if (windowevent == SDL_WINDOWEVENT_RESIZED) {
slouken@6512
   186
            SDL_FilterEvents(RemovePendingResizedEvents, &event);
slouken@6512
   187
        }
slouken@6512
   188
        if (windowevent == SDL_WINDOWEVENT_SIZE_CHANGED) {
slouken@6512
   189
            SDL_FilterEvents(RemovePendingSizeChangedEvents, &event);
slouken@3573
   190
        }
slouken@5042
   191
        if (windowevent == SDL_WINDOWEVENT_MOVED) {
slouken@5042
   192
            SDL_FilterEvents(RemovePendingMoveEvents, &event);
slouken@5042
   193
        }
slouken@3573
   194
slouken@1895
   195
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   196
    }
krogoway@5113
   197
	
krogoway@5113
   198
	if (windowevent == SDL_WINDOWEVENT_CLOSE) {
krogoway@5113
   199
		if ( !window->prev && !window->next ) {
krogoway@5113
   200
			// This is the last window in the list so send the SDL_QUIT event
krogoway@5113
   201
			SDL_SendQuit();
krogoway@5113
   202
		}
krogoway@5113
   203
	}
krogoway@5113
   204
slouken@1895
   205
    return (posted);
slouken@1895
   206
}
slouken@1895
   207
slouken@1895
   208
/* vi: set ts=4 sw=4 expandtab: */