SDL_windowevents.c
Go to the documentation of this file.
/*
Simple DirectMedia Layer
Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../SDL_internal.h"
/* Window event handling code for SDL */
#include "SDL_events.h"
#include "SDL_events_c.h"
#include "SDL_mouse_c.h"
#include "../video/SDL_sysvideo.h"
static int
RemovePendingResizedEvents(void * userdata, SDL_Event *event)
{
SDL_Event *new_event = (SDL_Event *)userdata;
if (event->type == SDL_WINDOWEVENT &&
event->window.event == SDL_WINDOWEVENT_RESIZED &&
event->window.windowID == new_event->window.windowID) {
/* We're about to post a new size event, drop the old one */
return 0;
}
return 1;
}
static int
RemovePendingSizeChangedEvents(void * userdata, SDL_Event *event)
{
SDL_Event *new_event = (SDL_Event *)userdata;
if (event->type == SDL_WINDOWEVENT &&
event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED &&
event->window.windowID == new_event->window.windowID) {
/* We're about to post a new size event, drop the old one */
return 0;
}
return 1;
}
static int
RemovePendingMoveEvents(void * userdata, SDL_Event *event)
{
SDL_Event *new_event = (SDL_Event *)userdata;
if (event->type == SDL_WINDOWEVENT &&
event->window.event == SDL_WINDOWEVENT_MOVED &&
event->window.windowID == new_event->window.windowID) {
/* We're about to post a new move event, drop the old one */
return 0;
}
return 1;
}
int
SDL_SendWindowEvent(SDL_Window * window, Uint8 windowevent, int data1,
int data2)
{
int posted;
if (!window) {
return 0;
}
switch (windowevent) {
case SDL_WINDOWEVENT_SHOWN:
if (window->flags & SDL_WINDOW_SHOWN) {
return 0;
}
window->flags &= ~SDL_WINDOW_HIDDEN;
window->flags |= SDL_WINDOW_SHOWN;
SDL_OnWindowShown(window);
break;
case SDL_WINDOWEVENT_HIDDEN:
if (!(window->flags & SDL_WINDOW_SHOWN)) {
return 0;
}
window->flags &= ~SDL_WINDOW_SHOWN;
window->flags |= SDL_WINDOW_HIDDEN;
SDL_OnWindowHidden(window);
break;
case SDL_WINDOWEVENT_MOVED:
if (SDL_WINDOWPOS_ISUNDEFINED(data1) ||
SDL_WINDOWPOS_ISUNDEFINED(data2)) {
return 0;
}
if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
window->windowed.x = data1;
window->windowed.y = data2;
}
if (data1 == window->x && data2 == window->y) {
return 0;
}
window->x = data1;
window->y = data2;
break;
case SDL_WINDOWEVENT_RESIZED:
if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
window->windowed.w = data1;
window->windowed.h = data2;
}
if (data1 == window->w && data2 == window->h) {
return 0;
}
window->w = data1;
window->h = data2;
SDL_OnWindowResized(window);
break;
case SDL_WINDOWEVENT_MINIMIZED:
if (window->flags & SDL_WINDOW_MINIMIZED) {
return 0;
}
window->flags &= ~SDL_WINDOW_MAXIMIZED;
window->flags |= SDL_WINDOW_MINIMIZED;
SDL_OnWindowMinimized(window);
break;
case SDL_WINDOWEVENT_MAXIMIZED:
if (window->flags & SDL_WINDOW_MAXIMIZED) {
return 0;
}
window->flags &= ~SDL_WINDOW_MINIMIZED;
window->flags |= SDL_WINDOW_MAXIMIZED;
break;
case SDL_WINDOWEVENT_RESTORED:
if (!(window->flags & (SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED))) {
return 0;
}
window->flags &= ~(SDL_WINDOW_MINIMIZED | SDL_WINDOW_MAXIMIZED);
SDL_OnWindowRestored(window);
break;
case SDL_WINDOWEVENT_ENTER:
if (window->flags & SDL_WINDOW_MOUSE_FOCUS) {
return 0;
}
window->flags |= SDL_WINDOW_MOUSE_FOCUS;
SDL_OnWindowEnter(window);
break;
case SDL_WINDOWEVENT_LEAVE:
if (!(window->flags & SDL_WINDOW_MOUSE_FOCUS)) {
return 0;
}
window->flags &= ~SDL_WINDOW_MOUSE_FOCUS;
SDL_OnWindowLeave(window);
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
return 0;
}
window->flags |= SDL_WINDOW_INPUT_FOCUS;
SDL_OnWindowFocusGained(window);
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
if (!(window->flags & SDL_WINDOW_INPUT_FOCUS)) {
return 0;
}
window->flags &= ~SDL_WINDOW_INPUT_FOCUS;
SDL_OnWindowFocusLost(window);
break;
}
/* Post the event, if desired */
posted = 0;
if (SDL_GetEventState(SDL_WINDOWEVENT) == SDL_ENABLE) {
SDL_Event event;
event.type = SDL_WINDOWEVENT;
event.window.event = windowevent;
event.window.data1 = data1;
event.window.data2 = data2;
event.window.windowID = window->id;
/* Fixes queue overflow with resize events that aren't processed */
if (windowevent == SDL_WINDOWEVENT_RESIZED) {
SDL_FilterEvents(RemovePendingResizedEvents, &event);
}
if (windowevent == SDL_WINDOWEVENT_SIZE_CHANGED) {
SDL_FilterEvents(RemovePendingSizeChangedEvents, &event);
}
if (windowevent == SDL_WINDOWEVENT_MOVED) {
SDL_FilterEvents(RemovePendingMoveEvents, &event);
}
posted = (SDL_PushEvent(&event) > 0);
}
if (windowevent == SDL_WINDOWEVENT_CLOSE) {
if ( !window->prev && !window->next ) {
/* This is the last window in the list so send the SDL_QUIT event */
SDL_SendQuit();
}
}
return (posted);
}
/* vi: set ts=4 sw=4 expandtab: */
SDL_WINDOWPOS_ISUNDEFINED
#define SDL_WINDOWPOS_ISUNDEFINED(X)
Definition: SDL_video.h:121
SDL_Window::next
SDL_Window * next
Definition: SDL_sysvideo.h:109
SDL_WINDOWEVENT_MINIMIZED
Definition: SDL_video.h:149
SDL_WINDOW_MOUSE_FOCUS
Definition: SDL_video.h:108
SDL_Window::w
int w
Definition: SDL_sysvideo.h:78
SDL_Window::x
int x
Definition: SDL_sysvideo.h:77
window
SDL_Window * window
Definition: testautomation_render.c:26
SDL_OnWindowResized
void SDL_OnWindowResized(SDL_Window *window)
Definition: SDL_video.c:2301
SDL_events.h
SDL_ENABLE
#define SDL_ENABLE
Definition: SDL_events.h:718
SDL_WINDOWEVENT_MAXIMIZED
Definition: SDL_video.h:150
SDL_SendWindowEvent
int SDL_SendWindowEvent(SDL_Window *window, Uint8 windowevent, int data1, int data2)
Definition: SDL_windowevents.c:74
SDL_OnWindowEnter
void SDL_OnWindowEnter(SDL_Window *window)
Definition: SDL_video.c:2330
SDL_WINDOWEVENT_SIZE_CHANGED
Definition: SDL_video.h:146
SDL_WINDOW_HIDDEN
Definition: SDL_video.h:101
SDL_Window::windowed
SDL_Rect windowed
Definition: SDL_sysvideo.h:85
RemovePendingSizeChangedEvents
static int RemovePendingSizeChangedEvents(void *userdata, SDL_Event *event)
Definition: SDL_windowevents.c:46
RemovePendingResizedEvents
static int RemovePendingResizedEvents(void *userdata, SDL_Event *event)
Definition: SDL_windowevents.c:32
SDL_WINDOWEVENT_HIDDEN
Definition: SDL_video.h:140
SDL_WINDOW_SHOWN
Definition: SDL_video.h:100
SDL_OnWindowHidden
void SDL_OnWindowHidden(SDL_Window *window)
Definition: SDL_video.c:2295
SDL_WINDOWEVENT_FOCUS_GAINED
Definition: SDL_video.h:155
SDL_Event::window
SDL_WindowEvent window
Definition: SDL_events.h:525
SDL_OnWindowMinimized
void SDL_OnWindowMinimized(SDL_Window *window)
Definition: SDL_video.c:2308
SDL_GetEventState
#define SDL_GetEventState(type)
Definition: SDL_events.h:731
SDL_mouse_c.h
SDL_WINDOWEVENT_RESTORED
Definition: SDL_video.h:151
Uint8
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
SDL_PushEvent
#define SDL_PushEvent
Definition: SDL_dynapi_overrides.h:131
SDL_WINDOWEVENT_FOCUS_LOST
Definition: SDL_video.h:156
SDL_WINDOWEVENT
Definition: SDL_events.h:89
SDL_OnWindowLeave
void SDL_OnWindowLeave(SDL_Window *window)
Definition: SDL_video.c:2338
SDL_WINDOW_MINIMIZED
Definition: SDL_video.h:104
SDL_Window::y
int y
Definition: SDL_sysvideo.h:77
SDL_Rect::x
int x
Definition: SDL_rect.h:66
SDL_WindowEvent::event
Uint8 event
Definition: SDL_events.h:176
SDL_WINDOWEVENT_LEAVE
Definition: SDL_video.h:154
SDL_Rect::w
int w
Definition: SDL_rect.h:67
SDL_WINDOWEVENT_CLOSE
Definition: SDL_video.h:157
SDL_Window::h
int h
Definition: SDL_sysvideo.h:78
SDL_events_c.h
SDL_WINDOW_INPUT_FOCUS
Definition: SDL_video.h:107
SDL_WINDOWEVENT_SHOWN
Definition: SDL_video.h:139
SDL_Window::prev
SDL_Window * prev
Definition: SDL_sysvideo.h:108
SDL_Rect::h
int h
Definition: SDL_rect.h:67
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:71
SDL_WINDOW_MAXIMIZED
Definition: SDL_video.h:105
SDL_Window::id
Uint32 id
Definition: SDL_sysvideo.h:74
SDL_FilterEvents
#define SDL_FilterEvents
Definition: SDL_dynapi_overrides.h:136
SDL_WINDOW_FULLSCREEN
Definition: SDL_video.h:98
SDL_WINDOWEVENT_ENTER
Definition: SDL_video.h:153
SDL_OnWindowFocusLost
void SDL_OnWindowFocusLost(SDL_Window *window)
Definition: SDL_video.c:2387
SDL_OnWindowRestored
void SDL_OnWindowRestored(SDL_Window *window)
Definition: SDL_video.c:2314
SDL_Event
General event structure.
Definition: SDL_events.h:521
SDL_OnWindowShown
void SDL_OnWindowShown(SDL_Window *window)
Definition: SDL_video.c:2289
SDL_WINDOWEVENT_RESIZED
Definition: SDL_video.h:145
SDL_Window::flags
Uint32 flags
Definition: SDL_sysvideo.h:81
SDL_WINDOWEVENT_MOVED
Definition: SDL_video.h:143
SDL_Rect::y
int y
Definition: SDL_rect.h:66
SDL_WindowEvent::windowID
Uint32 windowID
Definition: SDL_events.h:175
RemovePendingMoveEvents
static int RemovePendingMoveEvents(void *userdata, SDL_Event *event)
Definition: SDL_windowevents.c:60
SDL_SendQuit
int SDL_SendQuit(void)
Definition: SDL_quit.c:139
SDL_Event::type
Uint32 type
Definition: SDL_events.h:523
SDL_OnWindowFocusGained
void SDL_OnWindowFocusGained(SDL_Window *window)
Definition: SDL_video.c:2343