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